D6EdlpTaxonomyTermEntree.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Drupal\edlp_migrate\Plugin\migrate\source;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\migrate\Row;
  5. use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
  6. /**
  7. * D6 Edlp Taxonomy Entree term source from database.
  8. *
  9. * @MigrateSource(
  10. * id = "d6_edlp_taxonomy_term_entree"
  11. * )
  12. */
  13. class D6EdlpTaxonomyTermEntree extends DrupalSqlBase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function query() {
  18. // drush_print('D6EdlpTaxonomyTermEntree query');
  19. $query = $this->select('term_data', 'td')
  20. ->fields('td')
  21. ->distinct()
  22. ->orderBy('td.tid');
  23. if (isset($this->configuration['bundle'])) {
  24. $query->condition('td.vid', (array) $this->configuration['bundle'], 'IN');
  25. }
  26. return $query;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function fields() {
  32. $fields = [
  33. 'tid' => $this->t('The term ID.'),
  34. 'vid' => $this->t('Existing term VID'),
  35. 'name' => $this->t('The name of the term.'),
  36. 'description' => $this->t('The term description.'),
  37. 'weight' => $this->t('Weight'),
  38. 'parent' => $this->t("The Drupal term IDs of the term's parents."),
  39. 'node_nid' => $this->t("The node's nid of entree's description."),
  40. ];
  41. if (isset($this->configuration['translations'])) {
  42. $fields['language'] = $this->t('The term language.');
  43. $fields['trid'] = $this->t('Translation ID.');
  44. }
  45. // print_r($fields);
  46. // print_r("\n");
  47. return $fields;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function prepareRow(Row $row) {
  53. // drush_print_r($row);
  54. // Find parents for this row.
  55. $parents = $this->select('term_hierarchy', 'th')
  56. ->fields('th', ['parent', 'tid'])
  57. ->condition('tid', $row->getSourceProperty('tid'))
  58. ->execute()
  59. ->fetchCol();
  60. $row->setSourceProperty('parent', $parents);
  61. // find node attached to this term for description
  62. $query = $this->select('content_type_entree', 'cte');
  63. // $query->fields('cte', ['nid', 'vid', 'field_entree_value']);
  64. $query->condition('cte.field_entree_value', $row->getSourceProperty('tid'));
  65. $query->join('node_revisions', 'nr', 'cte.nid = nr.nid');
  66. $query->addField('nr', 'body');
  67. $description = $query->execute()
  68. ->fetchField();
  69. // drush_print_r($node);
  70. // empty enties to keep migrate
  71. $empty_entries = array(267, 291, 309, 310);
  72. if($description == "" && in_array($row->getSourceProperty('tid'), $empty_entries)){
  73. $description = "a remplir";
  74. }
  75. $row->setSourceProperty('description', $description);
  76. // get the node type page:notice from source and add it to notice field
  77. // find node attached to this term for description
  78. // get all notice
  79. $query = $this->select('term_node', 'tn');
  80. $query->condition('tn.tid', 8 ); // page type notice tid
  81. $query->addField('tn', 'nid');
  82. $notices_nids = $query->execute()->fetchCol();
  83. // get all nodes taged with the current entree
  84. $query = $this->select('term_node', 'tn');
  85. $query->condition('tn.tid', $row->getSourceProperty('tid'));
  86. $query->addField('tn', 'nid');
  87. $nodes_nids = $query->execute()->fetchCol();
  88. $notice_nid = array_shift(array_intersect($notices_nids, $nodes_nids));
  89. // drush_print('notice_nid : ', 0, null, false);
  90. // drush_print_r($notice_nid);
  91. $query = $this->select('node_revisions', 'nr');
  92. $query->condition('nr.nid', $notice_nid ); // page type notice tid
  93. $query->addField('nr', 'body');
  94. $notices_body = $query->execute()->fetchField();
  95. // drush_print('notices_body : ', 0, null, false);
  96. // drush_print_r($notices_body);
  97. // convert inner text links to internal nodes to new url
  98. preg_match_all('/href="(\/?corpus\/[^"]+)"/', $notices_body, $links);
  99. // drush_print('links : ', 0, null, false);
  100. // drush_print_r($links);
  101. foreach ($links[1] as $key => $path) {
  102. $path = preg_replace('/^\//', '', $path);
  103. // drush_print('path : ', 0, null, false);
  104. // drush_print_r($path);
  105. // find the source path
  106. $query = $this->select('url_alias', 'ua');
  107. $query->condition('dst', $path);
  108. $query->addField('ua', 'src');
  109. $src = $query->execute()->fetchField();
  110. // drush_print('src : ', 0, null, false);
  111. // drush_print_r($src);
  112. if($src){
  113. // get the nid
  114. $nid = str_replace('node/', '', $src);
  115. // drush_print('nid : ', 0, null, false);
  116. // drush_print_r($nid);
  117. // find the new nid of the enregistrement (corpus item) from migration map
  118. // get the d6_edlp_migration
  119. // drush_print_r($this->migration->getIdMap()->lookupDestinationId(array($nid)));
  120. $new_nid = Database::getConnection('default', 'default')
  121. ->select('migrate_map_d6_edlp_corpus', 'm')
  122. ->fields('m', ['destid1'])
  123. ->condition('sourceid1', $nid)
  124. ->execute()
  125. ->fetchField();
  126. // drush_print('new_nid : ', 0, null, false);
  127. // drush_print_r($new_nid);
  128. if($new_nid){
  129. $notices_body = str_replace($path, "node/".$new_nid, $notices_body);
  130. }
  131. }else{
  132. drush_print('no source for path : ', 0, null, false);
  133. drush_print_r($path);
  134. }
  135. }
  136. $row->setSourceProperty('notice', $notices_body);
  137. return parent::prepareRow($row);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getIds() {
  143. $ids['tid']['type'] = 'integer';
  144. return $ids;
  145. }
  146. }