D6EdlpPages.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Drupal\edlp_migrate\Plugin\migrate\source;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\migrate\Row;
  5. use Drupal\node\Plugin\migrate\source\d6\Node as D6Node;
  6. /**
  7. * Source plugin for edlp corpus migration.
  8. *
  9. * @MigrateSource(
  10. * id = "d6_edlp_pages"
  11. * )
  12. */
  13. class D6EdlpPages extends D6Node {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function query() {
  18. $query = $this->select('node_revisions', 'nr');
  19. $query->innerJoin('node', 'n', static::JOIN);
  20. $this->handleTranslations($query);
  21. $query->fields('n', [
  22. 'nid',
  23. 'type',
  24. 'language',
  25. 'status',
  26. 'created',
  27. 'changed',
  28. 'comment',
  29. 'promote',
  30. 'moderate',
  31. 'sticky',
  32. 'tnid',
  33. 'translate',
  34. ])
  35. ->fields('nr', [
  36. 'title',
  37. 'body',
  38. 'teaser',
  39. 'log',
  40. 'timestamp',
  41. 'format',
  42. 'vid',
  43. ]);
  44. $query->addField('n', 'uid', 'node_uid');
  45. $query->addField('nr', 'uid', 'revision_uid');
  46. // If the content_translation module is enabled, get the source langcode
  47. // to fill the content_translation_source field.
  48. if ($this->moduleHandler->moduleExists('content_translation')) {
  49. $query->leftJoin('node', 'nt', 'n.tnid = nt.nid');
  50. $query->addField('nt', 'language', 'source_langcode');
  51. }
  52. if (isset($this->configuration['node_type'])) {
  53. $query->condition('n.type', $this->configuration['node_type']);
  54. }
  55. return $query;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function fields() {
  61. $fields = [
  62. 'nid' => $this->t('Node ID'),
  63. 'type' => $this->t('Type'),
  64. 'title' => $this->t('Title'),
  65. 'body' => $this->t('Body'),
  66. 'format' => $this->t('Format'),
  67. 'teaser' => $this->t('Teaser'),
  68. 'node_uid' => $this->t('Node authored by (uid)'),
  69. 'revision_uid' => $this->t('Revision authored by (uid)'),
  70. 'created' => $this->t('Created timestamp'),
  71. 'changed' => $this->t('Modified timestamp'),
  72. 'status' => $this->t('Published'),
  73. 'promote' => $this->t('Promoted to front page'),
  74. 'sticky' => $this->t('Sticky at top of lists'),
  75. 'revision' => $this->t('Create new revision'),
  76. 'language' => $this->t('Language (fr, en, ...)'),
  77. 'tnid' => $this->t('The translation set id for this node'),
  78. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  79. ];
  80. return $fields;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function prepareRow(Row $row) {
  86. drush_print(' ');
  87. drush_print('- - - - - - - - - - - - - - - - - - - - - - - -');
  88. drush_print($row->getSourceProperty('title'));
  89. // format = 0 can happen when the body field is hidden. Set the format to 1
  90. // to avoid migration map issues (since the body field isn't used anyway).
  91. if ($row->getSourceProperty('format') === '0') {
  92. $row->setSourceProperty('format', $this->filterDefaultFormat);
  93. }
  94. if ($this->moduleExists('content') && $this->getModuleSchemaVersion('content') >= 6001) {
  95. foreach ($this->getFieldValues($row) as $field => $values) {
  96. $row->setSourceProperty($field, $values);
  97. }
  98. }
  99. $row_nid = $row->getSourceProperty('nid');
  100. // Make sure we always have a translation set.
  101. if ($row->getSourceProperty('tnid') == 0) {
  102. $row->setSourceProperty('tnid', $row_nid);
  103. }
  104. // get the entree values
  105. $query = $this->select('term_node', 'tn');
  106. // $query->fields('cte', ['field_entree_value']);
  107. $query->condition('tn.nid', $row_nid);
  108. $query->join('term_data', 'td', 'td.tid = tn.tid');
  109. $query->condition('td.vid', 2); // vid of voc entrees
  110. $query->addField('tn', 'tid');
  111. $query->distinct();
  112. $results = $query->execute()->fetchAll();
  113. $entrees = [];
  114. foreach ($results as $key => $value) {
  115. $entrees[] = array($value['tid']);
  116. }
  117. $row->setSourceProperty('entrees', $entrees);
  118. // Page type
  119. $query = $this->select('term_node', 'tn');
  120. $query->condition('tn.nid', $row_nid);
  121. $query->join('term_data', 'td', 'td.tid = tn.tid');
  122. $query->condition('td.vid', 1); // vid of voc page type
  123. $query->addField('tn', 'tid');
  124. $query->distinct();
  125. $results = $query->execute()->fetchAll();
  126. $pagetype = [];
  127. foreach ($results as $key => $value) {
  128. // avoid notice type (tid 8)
  129. // row will be skiped if empty
  130. if($value['tid'] !== "8"){
  131. drush_print('page type : '.$value['tid']);
  132. $pagetype[] = array($value['tid']);
  133. }else{
  134. drush_print('----- NOTICE -----');
  135. }
  136. }
  137. drush_print('pagetype empty : '.empty($pagetype));
  138. // if(count($pagetype) < 1){
  139. // $pagetype = FALSE;
  140. // }
  141. $row->setSourceProperty('page_type', !empty($pagetype) ? $pagetype : 0);
  142. // son
  143. // TODO: pour les pieces sonores les fichiers ne sont pas enregistrés dans le champ car trop lourd. Ils sont sous forme de lien dans le corps du texte. Comment normaliser ça ??
  144. // $sons = [];
  145. // foreach ($row->getSourceProperty('field_mp3') as $key => $value) {
  146. // $sons[] = array($value['fid']);
  147. // }
  148. // $row->setSourceProperty('sons_fid', $sons);
  149. $body = $row->getSourceProperty('body');
  150. preg_match_all('/href="(\/?sons\/[^"]+)"/', $body, $links);
  151. if(isset($links[1][0])){
  152. // drush_print('links : ', 0, null, false);
  153. // drush_print_r($links);
  154. $path = preg_replace('/^\//', '', $links[1][0]);
  155. // find the source path
  156. $query = $this->select('url_alias', 'ua');
  157. $query->condition('dst', $path);
  158. $query->addField('ua', 'src');
  159. $src = $query->execute()->fetchField();
  160. // drush_print('src : ', 0, null, false);
  161. // drush_print_r($src);
  162. if($src){
  163. // get the nid
  164. $nid = str_replace('node/', '', $src);
  165. // drush_print('nid : ', 0, null, false);
  166. // drush_print_r($nid);
  167. // find the new nid of the enregistrement (corpus item) from migration map
  168. // get the d6_edlp_migration
  169. // TODO: is this working for both corpus and sons elements ??
  170. $new_nid = Database::getConnection('default', 'default')
  171. ->select('migrate_map_d6_edlp_sons', 'm')
  172. ->fields('m', ['destid1'])
  173. ->condition('sourceid1', $nid)
  174. ->execute()
  175. ->fetchField();
  176. // drush_print('new_nid : ', 0, null, false);
  177. // drush_print_r($new_nid);
  178. if($new_nid){
  179. $body = str_replace($path, "node/".$new_nid, $body);
  180. }
  181. }else{
  182. // drush_print('no source for path : ', 0, null, false);
  183. // drush_print_r($path);
  184. }
  185. }
  186. $row->setSourceProperty('body', $body);
  187. // visuel
  188. $visuels = [];
  189. foreach ($row->getSourceProperty('field_visuel') as $key => $value) {
  190. $visuels[] = array($value['fid']);
  191. }
  192. $row->setSourceProperty('visuels_fid', $visuels);
  193. // get the workflow status
  194. $query = $this->select('workflow_node', 'wn');
  195. $query->fields('wn', ['sid']);
  196. $query->condition('wn.nid', $row_nid);
  197. $results = $query->execute()->fetchField();
  198. // drush_print('workflow query results : ', 0, null, false);
  199. // drush_print_r($results);
  200. $row->setSourceProperty('workflow', $results);
  201. // fichiers attachés ??
  202. $row = parent::prepareRow($row);
  203. return $row;
  204. }
  205. }