Переглянути джерело

added pagetypes and pages migrations, moved notices from content to entree term field, fixed corpus links in notices

Bachir Soussi Chiadmi 7 роки тому
батько
коміт
ccf007b118

+ 11 - 3
sites/all/modules/figli/edlp_migrate/config/install/migrate_plus.migration.d6_edlp_entrees.yml

@@ -8,20 +8,28 @@ source:
   bundle: 2
   bundle: 2
 process:
 process:
   name: name
   name: name
-  description:
+  # get the node type entrée from source and add it to description field
+  description/value:
     -
     -
       plugin: get
       plugin: get
       source: description
       source: description
     -
     -
       plugin: skip_on_empty
       plugin: skip_on_empty
       method: row
       method: row
+  description/format:
+    plugin: default_value
+    default_value: wysiwyg
+
+  # get the node type page:notice from source and add it to notice field
+  field_notice/value: notice
+  field_notice/format:
+    plugin: default_value
+    default_value: wysiwyg
 
 
   vid:
   vid:
     plugin: default_value
     plugin: default_value
     default_value: entrees
     default_value: entrees
   weight: weight
   weight: weight
 
 
-# todo : get the node type entrée from source and add it to description field (we need a plugin for that)
-
 destination:
 destination:
   plugin: entity:taxonomy_term
   plugin: entity:taxonomy_term

+ 106 - 0
sites/all/modules/figli/edlp_migrate/config/install/migrate_plus.migration.d6_edlp_pages.yml

@@ -0,0 +1,106 @@
+id: d6_edlp_page
+label: Edlp Drupal 6 page
+migration_tags:
+  - Drupal 6
+deriver: Drupal\node\Plugin\migrate\D6NodeDeriver
+migration_group: d6_edlp
+source:
+  plugin: d6_edlp_pages
+  node_type: page
+process:
+  # In D6, nodes always have a tnid, but it's zero for untranslated nodes.
+  # We normalize it to equal the nid in that case.
+  # @see \Drupal\node\Plugin\migrate\source\d6\Node::prepareRow().
+  # If you are using this file to build a custom migration consider removing
+  # the nid and vid fields to allow incremental migrations.
+  # nid: tnid
+  # vid: vid
+  type:
+    plugin: default_value
+    default_value: page
+  langcode:
+    plugin: default_value
+    source: language
+    default_value: "und"
+  title:
+    -
+      plugin: get
+      source: title
+    -
+      plugin: skip_on_empty
+      method: row
+
+  uid:
+    -
+      plugin: migration_lookup
+      migration: d6_edlp_users
+      source: node_uid
+    -
+      plugin: default_value
+      default_value: 1
+
+  # status: status
+  created: created
+  changed: changed
+  promote: promote
+  sticky: sticky
+  revision_uid: revision_uid
+  revision_log: log
+  revision_timestamp: timestamp
+
+  'body/value': body
+  'body/format':
+    plugin: default_value
+    default_value: wysiwyg
+
+  # taxo term -> entity ref terms
+  # use this one for fetchCol in plugin
+  field_entrees:
+    plugin: migration_lookup
+    migration: d6_edlp_entrees
+    source: entrees
+
+  # taxo term -> entity ref terms
+  # use this one for fetchCol in plugin
+  field_page_type:
+    plugin: migration_lookup
+    migration: d6_edlp_pagetype
+    source: page_type
+
+  # TODO: exclude pages of type notice (will be incorporated directly into the entree taxo term)
+
+  # # file
+  # # field_mp3 -> field_son
+  # 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 ??
+  field_son:
+    plugin: migration_lookup
+    migration: d6_edlp_allfiles
+    source: sons_fid
+
+  # # field_visuel -> field_visuel
+  field_visuel:
+    plugin: migration_lookup
+    migration: d6_edlp_allfiles
+    source: visuels_fid
+
+  # workflow
+  status:
+    plugin: static_map
+    source: workflow
+    map:
+      1: 0
+      2: 0
+      3: 1
+      4: 1
+
+  # fichiers attachés ??
+
+destination:
+  plugin: entity:node
+migration_dependencies:
+  required:
+    - d6_edlp_users
+    - d6_edlp_entrees
+    - d6_edlp_pagetype
+    - d6_edlp_sons
+    - d6_edlp_allfiles

+ 17 - 0
sites/all/modules/figli/edlp_migrate/config/install/migrate_plus.migration.d6_edlp_pagetype.yml

@@ -0,0 +1,17 @@
+id: d6_edlp_pagetype
+label: Edlp Drupal 6 type de page
+migration_tags:
+  - Drupal 6
+migration_group: d6_edlp
+source:
+  plugin: d6_taxonomy_term
+  bundle: 1
+process:
+  name: name
+  description: description
+  vid:
+    plugin: default_value
+    default_value: page_type
+
+destination:
+  plugin: entity:taxonomy_term

+ 4 - 0
sites/all/modules/figli/edlp_migrate/readme.md

@@ -21,3 +21,7 @@ https://evolvingweb.ca/blog/drupal-8-migration-migrating-taxonomy-term-reference
 [D6 Taxonomy Term Reference Field is not migrated to D8 Field](https://www.drupal.org/node/2884240)
 [D6 Taxonomy Term Reference Field is not migrated to D8 Field](https://www.drupal.org/node/2884240)
 
 
 [Migrer un site Drupal 6 ou Drupal 7 vers Drupal 8](https://www.drupalfacile.org/sites/drupalfacile/files/blog/2016/06/dcnantes2016-migrerd6d7versd8.pdf)
 [Migrer un site Drupal 6 ou Drupal 7 vers Drupal 8](https://www.drupalfacile.org/sites/drupalfacile/files/blog/2016/06/dcnantes2016-migrerd6d7versd8.pdf)
+
+[Migrating Aliases and Redirects to Drupal 8](https://evolvingweb.ca/blog/migrating-aliases-and-redirects-drupal-8)
+
+[Migrating Drupal 7 redirects to Drupal 8](http://activelamp.com/blog/drupal/migrating-drupal-8-redirects/)

+ 1 - 2
sites/all/modules/figli/edlp_migrate/src/Plugin/migrate/source/D6EdlpCorpus.php

@@ -210,8 +210,7 @@ class D6EdlpCorpus extends D6Node {
     }
     }
     $row->setSourceProperty('sons_fid', $sons);
     $row->setSourceProperty('sons_fid', $sons);
 
 
-    // TODO: get the workflow status
-    // get the entree values
+    // get the workflow state (will be converted on status)
     $query = $this->select('workflow_node', 'wn');
     $query = $this->select('workflow_node', 'wn');
     $query->fields('wn', ['sid']);
     $query->fields('wn', ['sid']);
     $query->condition('wn.nid', $row_nid);
     $query->condition('wn.nid', $row_nid);

+ 220 - 0
sites/all/modules/figli/edlp_migrate/src/Plugin/migrate/source/D6EdlpPages.php

@@ -0,0 +1,220 @@
+<?php
+
+namespace Drupal\edlp_migrate\Plugin\migrate\source;
+
+use Drupal\migrate\Row;
+use Drupal\node\Plugin\migrate\source\d6\Node as D6Node;
+
+/**
+ * Source plugin for edlp corpus migration.
+ *
+ * @MigrateSource(
+ *   id = "d6_edlp_pages"
+ * )
+ */
+class D6EdlpPages extends D6Node {
+
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    $query = $this->select('node_revisions', 'nr');
+    $query->innerJoin('node', 'n', static::JOIN);
+    $this->handleTranslations($query);
+
+    $query->fields('n', [
+        'nid',
+        'type',
+        'language',
+        'status',
+        'created',
+        'changed',
+        'comment',
+        'promote',
+        'moderate',
+        'sticky',
+        'tnid',
+        'translate',
+      ])
+      ->fields('nr', [
+        'title',
+        'body',
+        'teaser',
+        'log',
+        'timestamp',
+        'format',
+        'vid',
+      ]);
+    $query->addField('n', 'uid', 'node_uid');
+    $query->addField('nr', 'uid', 'revision_uid');
+
+    // If the content_translation module is enabled, get the source langcode
+    // to fill the content_translation_source field.
+    if ($this->moduleHandler->moduleExists('content_translation')) {
+      $query->leftJoin('node', 'nt', 'n.tnid = nt.nid');
+      $query->addField('nt', 'language', 'source_langcode');
+    }
+
+    if (isset($this->configuration['node_type'])) {
+      $query->condition('n.type', $this->configuration['node_type']);
+    }
+
+    return $query;
+  }
+
+
+  /**
+   * {@inheritdoc}
+   */
+  public function fields() {
+
+    $fields = [
+      'nid' => $this->t('Node ID'),
+      'type' => $this->t('Type'),
+      'title' => $this->t('Title'),
+      'body' => $this->t('Body'),
+      'format' => $this->t('Format'),
+      'teaser' => $this->t('Teaser'),
+      'node_uid' => $this->t('Node authored by (uid)'),
+      'revision_uid' => $this->t('Revision authored by (uid)'),
+      'created' => $this->t('Created timestamp'),
+      'changed' => $this->t('Modified timestamp'),
+      'status' => $this->t('Published'),
+      'promote' => $this->t('Promoted to front page'),
+      'sticky' => $this->t('Sticky at top of lists'),
+      'revision' => $this->t('Create new revision'),
+      'language' => $this->t('Language (fr, en, ...)'),
+      'tnid' => $this->t('The translation set id for this node'),
+      'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
+    ];
+    return $fields;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function prepareRow(Row $row) {
+    drush_print(' ');
+    drush_print('- - - - - - - - - - - - - - - - - - - - - - - -');
+    drush_print($row->getSourceProperty('title'));
+
+
+    // format = 0 can happen when the body field is hidden. Set the format to 1
+    // to avoid migration map issues (since the body field isn't used anyway).
+    if ($row->getSourceProperty('format') === '0') {
+      $row->setSourceProperty('format', $this->filterDefaultFormat);
+    }
+
+    if ($this->moduleExists('content') && $this->getModuleSchemaVersion('content') >= 6001) {
+      foreach ($this->getFieldValues($row) as $field => $values) {
+        $row->setSourceProperty($field, $values);
+      }
+    }
+    $row_nid = $row->getSourceProperty('nid');
+
+
+    // Make sure we always have a translation set.
+    if ($row->getSourceProperty('tnid') == 0) {
+      $row->setSourceProperty('tnid', $row_nid);
+    }
+
+    // get the entree values
+    $query = $this->select('term_node', 'tn');
+    // $query->fields('cte', ['field_entree_value']);
+    $query->condition('tn.nid', $row_nid);
+    $query->join('term_data', 'td', 'td.tid = tn.tid');
+    $query->condition('td.vid', 2); // vid of voc entrees
+    $query->addField('tn', 'tid');
+    $query->distinct();
+    $results = $query->execute()->fetchAll();
+    $entrees = [];
+    foreach ($results as $key => $value) {
+      $entrees[] = array($value['tid']);
+    }
+    $row->setSourceProperty('entrees', $entrees);
+
+    // Page type
+    $query = $this->select('term_node', 'tn');
+    $query->condition('tn.nid', $row_nid);
+    $query->join('term_data', 'td', 'td.tid = tn.tid');
+    $query->condition('td.vid', 1); // vid of voc page type
+    $query->addField('tn', 'tid');
+    $query->distinct();
+    $results = $query->execute()->fetchAll();
+    $pagetype = [];
+    foreach ($results as $key => $value) {
+      $pagetype[] = array($value['tid']);
+    }
+    $row->setSourceProperty('page_type', $pagetype);
+
+    // son
+    // 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 ??
+    // $sons = [];
+    // foreach ($row->getSourceProperty('field_mp3') as $key => $value) {
+    //   $sons[] = array($value['fid']);
+    // }
+    // $row->setSourceProperty('sons_fid', $sons);
+    $body = $row->getSourceProperty('body');
+    preg_match_all('/href="(\/?sons\/[^"]+)"/', $body, $links);
+    if(isset($links[1][0])){
+      drush_print('links : ', 0, null, false);
+      drush_print_r($links);
+      $path = preg_replace('/^\//', '', $links[1][0]);
+      // find the source path
+      $query = $this->select('url_alias', 'ua');
+      $query->condition('dst', $path);
+      $query->addField('ua', 'src');
+      $src = $query->execute()->fetchField();
+      drush_print('src : ', 0, null, false);
+      drush_print_r($src);
+      if($src){
+        // get the nid
+        $nid = str_replace('node/', '', $src);
+        drush_print('nid : ', 0, null, false);
+        drush_print_r($nid);
+        // find the new nid of the enregistrement (corpus item) from migration map
+        // get the d6_edlp_migration
+        // drush_print_r($this->migration->getIdMap()->lookupDestinationId(array($nid)));
+        $new_nid = Database::getConnection('default', 'default')
+          ->select('migrate_map_d6_edlp_sons', 'm')
+          ->fields('m', ['destid1'])
+          ->condition('sourceid1', $nid)
+          ->execute()
+          ->fetchField();
+        // drush_print('new_nid : ', 0, null, false);
+        // drush_print_r($new_nid);
+        if($new_nid){
+          $body = str_replace($path, "node/".$new_nid, $body);
+        }
+      }else{
+        drush_print('no source for path : ', 0, null, false);
+        drush_print_r($path);
+      }
+    }
+    $row->setSourceProperty('body', $body);
+
+    // visuel
+    $visuels = [];
+    foreach ($row->getSourceProperty('field_visuel') as $key => $value) {
+      $visuels[] = array($value['fid']);
+    }
+    $row->setSourceProperty('visuels_fid', $visuels);
+
+    // get the workflow status
+    $query = $this->select('workflow_node', 'wn');
+    $query->fields('wn', ['sid']);
+    $query->condition('wn.nid', $row_nid);
+    $results = $query->execute()->fetchField();
+    // drush_print('workflow query results : ', 0, null, false);
+    // drush_print_r($results);
+    $row->setSourceProperty('workflow', $results);
+
+    // fichiers attachés ??
+
+    $row = parent::prepareRow($row);
+    return $row;
+  }
+
+
+}

+ 68 - 1
sites/all/modules/figli/edlp_migrate/src/Plugin/migrate/source/D6EdlpTaxonomyTermEntree.php

@@ -2,6 +2,7 @@
 
 
 namespace Drupal\edlp_migrate\Plugin\migrate\source;
 namespace Drupal\edlp_migrate\Plugin\migrate\source;
 
 
+use Drupal\Core\Database\Database;
 use Drupal\migrate\Row;
 use Drupal\migrate\Row;
 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
 
 
@@ -76,10 +77,76 @@ class D6EdlpTaxonomyTermEntree extends DrupalSqlBase {
     $query->addField('nr', 'body');
     $query->addField('nr', 'body');
     $description = $query->execute()
     $description = $query->execute()
       ->fetchField();
       ->fetchField();
-
     // drush_print_r($node);
     // drush_print_r($node);
     $row->setSourceProperty('description', $description);
     $row->setSourceProperty('description', $description);
 
 
+    // TODO : get the node type page:notice from source and add it to notice field
+    // find node attached to this term for description
+    // get all notice
+    $query = $this->select('term_node', 'tn');
+    $query->condition('tn.tid', 8 ); // page type notice tid
+    $query->addField('tn', 'nid');
+    $notices_nids = $query->execute()->fetchCol();
+    // get all nodes taged with the current entree
+    $query = $this->select('term_node', 'tn');
+    $query->condition('tn.tid', $row->getSourceProperty('tid'));
+    $query->addField('tn', 'nid');
+    $nodes_nids = $query->execute()->fetchCol();
+
+    $notice_nid = array_shift(array_intersect($notices_nids, $nodes_nids));
+    // drush_print('notice_nid : ', 0, null, false);
+    // drush_print_r($notice_nid);
+    $query = $this->select('node_revisions', 'nr');
+    $query->condition('nr.nid', $notice_nid ); // page type notice tid
+    $query->addField('nr', 'body');
+    $notices_body = $query->execute()->fetchField();
+    // drush_print('notices_body : ', 0, null, false);
+    // drush_print_r($notices_body);
+
+    // convert inner text links to internal nodes to new url
+    preg_match_all('/href="(\/?corpus\/[^"]+)"/', $notices_body, $links);
+    // drush_print('links : ', 0, null, false);
+    // drush_print_r($links);
+    foreach ($links[1] as $key => $path) {
+      $path = preg_replace('/^\//', '', $path);
+      // drush_print('path : ', 0, null, false);
+      // drush_print_r($path);
+
+      // find the source path
+      $query = $this->select('url_alias', 'ua');
+      $query->condition('dst', $path);
+      $query->addField('ua', 'src');
+      $src = $query->execute()->fetchField();
+      // drush_print('src : ', 0, null, false);
+      // drush_print_r($src);
+      if($src){
+        // get the nid
+        $nid = str_replace('node/', '', $src);
+        // drush_print('nid : ', 0, null, false);
+        // drush_print_r($nid);
+        // find the new nid of the enregistrement (corpus item) from migration map
+        // get the d6_edlp_migration
+        // drush_print_r($this->migration->getIdMap()->lookupDestinationId(array($nid)));
+        $new_nid = Database::getConnection('default', 'default')
+          ->select('migrate_map_d6_edlp_corpus', 'm')
+          ->fields('m', ['destid1'])
+          ->condition('sourceid1', $nid)
+          ->execute()
+          ->fetchField();
+        // drush_print('new_nid : ', 0, null, false);
+        // drush_print_r($new_nid);
+        if($new_nid){
+          $notices_body = str_replace($path, "node/".$new_nid, $notices_body);
+        }
+      }else{
+        drush_print('no source for path : ', 0, null, false);
+        drush_print_r($path);
+      }
+
+    }
+
+    $row->setSourceProperty('notice', $notices_body);
+
     return parent::prepareRow($row);
     return parent::prepareRow($row);
   }
   }