fgcf.import.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * @file
  4. * Export / import functions.
  5. *
  6. * The file is not included by default. The import / export process is run using
  7. * drush php-eval.
  8. */
  9. /**
  10. * Export objects the fgcf_data_import table.
  11. *
  12. * Objects must be exported in the order in which they need to be imported,
  13. * with respect to dependencies order (terms, files, noderef fields...).
  14. */
  15. function fgcf_export() {
  16. // Export terms.
  17. foreach (taxonomy_get_vocabularies() as $vocab) {
  18. $tree = taxonomy_get_tree($vocab->vid);
  19. foreach ($tree as $term) {
  20. fgcf_export_term($term->tid);
  21. }
  22. }
  23. // Export files.
  24. $result = db_query("SELECT fid FROM {files}");
  25. while ($row = db_fetch_array($result)) {
  26. fgcf_export_file($row['fid']);
  27. }
  28. // Export nodes.
  29. $types = array('fgcf_thematique', 'fgcf_prestataire', 'fgcf_fiche');
  30. foreach ($types as $type) {
  31. $result = db_query("SELECT nid FROM {node} WHERE type='%s'", $type);
  32. while ($row = db_fetch_array($result)) {
  33. fgcf_export_node($row['nid']);
  34. }
  35. }
  36. }
  37. /**
  38. * Export a term in the fgcf_data_import table.
  39. */
  40. function fgcf_export_term($tid) {
  41. if ($term = taxonomy_get_term($tid)) {
  42. // Add parenting information.
  43. $term->parent = 0;
  44. if ($parents = taxonomy_get_parents($tid)) {
  45. $parent = current($parents);
  46. $term->parent = $parent->tid;
  47. }
  48. db_query("INSERT INTO {fgcf_data_import} (drupal_id, data, type) VALUES (%d, '%s', '%s')", $term->tid, serialize($term), 'term');
  49. }
  50. }
  51. /**
  52. * Export a file in the fgcf_data_import table.
  53. */
  54. function fgcf_export_file($fid) {
  55. if ($file = (object) field_file_load($fid)) {
  56. db_query("INSERT INTO {fgcf_data_import} (drupal_id, data, type) VALUES (%d, '%s', '%s')", $file->fid, serialize($file), 'file');
  57. }
  58. }
  59. /**
  60. * Export a node in the fgcf_data_import table.
  61. */
  62. function fgcf_export_node($nid) {
  63. if ($node = node_load($nid)) {
  64. db_query("INSERT INTO {fgcf_data_import} (drupal_id, data, type) VALUES (%d, '%s', '%s')", $node->nid, serialize($node), 'node');
  65. }
  66. }
  67. function fgcf_import() {
  68. $result = db_query("SELECT * FROM {fgcf_data_import} ORDER BY id ASC");
  69. while ($row = db_fetch_object($result)) {
  70. $function = 'fgcf_import_' .$row->type;
  71. $data = unserialize($row->data);
  72. $function($data);
  73. db_query("DELETE FROM {fgcf_data_import} WHERE id=%d", $row->id);
  74. }
  75. }
  76. /**
  77. * Import one term.
  78. */
  79. function fgcf_import_term($term) {
  80. // Replace vocabulary.
  81. // Those mappings have been initialized manually after the creation of the
  82. // vocabs.
  83. $term->vid = fgcf_map_get('vocabulary', $term->vid);
  84. // Replace parent.
  85. if ($term->parent) {
  86. $term->parent = fgcf_map_get('term', $term->parent);
  87. }
  88. $old_id = $term->tid;
  89. unset($term->tid);
  90. // taxonomy_save_term() takes an array of 'form values'...
  91. $term = (array) $term;
  92. taxonomy_save_term($term);
  93. $term = (object) $term;
  94. // Save mapping.
  95. fgcf_map_set('term', $old_id, $term->tid);
  96. return $term;
  97. }
  98. /**
  99. * Import one file.
  100. */
  101. function fgcf_import_file($file) {
  102. // Replace filepath.
  103. $file->filepath = preg_replace('|^sites/fg.local/files/|', file_directory_path() .'/', $file->filepath);
  104. $old_id = $file->fid;
  105. unset($file->fid);
  106. drupal_write_record('files', $file);
  107. // Save mapping.
  108. fgcf_map_set('file', $old_id, $file->fid);
  109. return $term;
  110. }
  111. /**
  112. * Import one node.
  113. */
  114. function fgcf_import_node($node) {
  115. switch ($node->type) {
  116. case 'fgcf_thematique':
  117. // Translate NAT associations.
  118. if (!empty($node->nat)) {
  119. $old_nat_term = current($node->nat);
  120. $nat_tid = fgcf_map_get('term', $old_nat_term->tid);
  121. $nat_term = taxonomy_get_term($nat_tid);
  122. // Just replace the $node property (probably useless), the association
  123. // will be saved after node creation.
  124. $node->nat = array($nat_term->tid => $nat_term);
  125. }
  126. // Translate file
  127. if (!empty($node->field_fgcf_thematique_logo)) {
  128. foreach ($node->field_fgcf_thematique_logo as &$item) {
  129. if (!empty($item['fid'])) {
  130. $item = array('fid' => fgcf_map_get('file', $item['fid']));
  131. }
  132. }
  133. }
  134. break;
  135. case 'fgcf_prestataire':
  136. // Translate file
  137. if (!empty($node->field_fgcf_prestataire_logo)) {
  138. foreach ($node->field_fgcf_prestataire_logo as &$item) {
  139. if (!empty($item['fid'])) {
  140. $item = array('fid' => fgcf_map_get('file', $item['fid']));
  141. }
  142. }
  143. }
  144. case 'fgcf_fiche':
  145. // Translate nodereference
  146. if (!empty($node->field_fgcf_fiche_organisme)) {
  147. foreach ($node->field_fgcf_fiche_organisme as &$item) {
  148. if (!empty($item['nid'])) {
  149. $item = array('nid' => fgcf_map_get('node', $item['nid']));
  150. }
  151. }
  152. }
  153. // Translate taxo fields.
  154. $fields = array('field_fgcf_fiche_format', 'field_fgcf_fiche_supports', 'field_fgcf_fiche_sanction', 'field_fgcf_fiche_public');
  155. foreach ($fields as $field) {
  156. if (!empty($node->$field)) {
  157. foreach ($node->$field as &$item) {
  158. if (!empty($item['value'])) {
  159. $item = array('value' => fgcf_map_get('term', $item['value']));
  160. }
  161. }
  162. }
  163. }
  164. // Translate regular taxonomy.
  165. if (!empty($node->taxonomy)) {
  166. $terms = array();
  167. foreach ($node->taxonomy as $old_term) {
  168. $tid = fgcf_map_get('term', $old_term->tid);
  169. $term = taxonomy_get_term($tid);
  170. $terms[$tid] = $term;
  171. }
  172. $node->taxonomy = $terms;
  173. }
  174. break;
  175. }
  176. $old_id = $node->nid;
  177. unset($node->nid, $node->vid);
  178. node_save($node);
  179. // Record NAT association if needed.
  180. if (isset($nat_term)) {
  181. // Pas de drupal_write_record() : NAT est désactivé pour éviter qu'il crée
  182. // un terme.
  183. db_query('INSERT INTO {nat} (nid, tid, vid) VALUES (%d, %d, %d)', $node->nid, $nat_term->tid, $nat_term->vid);
  184. }
  185. // Save mapping.
  186. fgcf_map_set('node', $old_id, $node->nid);
  187. return $node;
  188. }
  189. /**
  190. * Stores a mapping for imported data.
  191. *
  192. * @param $type
  193. * A string: 'vocabulary', 'term', 'node', 'file'
  194. * @param $old_id
  195. * The id of the original object.
  196. * @param $new_id
  197. * The id of the corresponding imported object.
  198. */
  199. function fgcf_map_set($type, $old_id, $new_id) {
  200. $map = variable_get('fgcf_map', array());
  201. if (is_null($new_id)) {
  202. // Read.
  203. if (isset($map[$type][$old_id])) {
  204. return $map[$type][$old_id];
  205. }
  206. else {
  207. // dsm("unknown mapping: $type $old_id");
  208. }
  209. }
  210. else {
  211. // Write.
  212. $map[$type][$old_id] = $new_id;
  213. variable_set('fgcf_map', $map);
  214. }
  215. }
  216. /**
  217. * Raads a mapping for imported data.
  218. *
  219. * @param $type
  220. * A string: 'vocabulary', 'term', 'node', 'file'
  221. * @param $old_id
  222. * The id of the original object.
  223. *
  224. * @return
  225. * The id of the corresponding imported object, or NULL if unknown.
  226. */
  227. function fgcf_map_get($type, $old_id) {
  228. return fgcf_map_set($type, $old_id, NULL);
  229. }