You are here

public function WordPressAttachment::postImport in WordPress Migrate 7.2

Same name and namespace in other branches
  1. 7 wordpress_attachment.inc \WordPressAttachment::postImport()

Called after all attachments are imported - fix up references to the imported attachments in node bodies.

Overrides WordPressMigration::postImport

File

./wordpress_attachment.inc, line 293

Class

WordPressAttachment
Implementation of WordPressMigration, for attachments

Code

public function postImport() {
  parent::postImport();
  migrate_instrument_start('WordPressAttachment postImport');

  // We'd like to just go through nodes with attachments (i.e., loop
  // through wordpress_migrate_attachment), but a node may reference
  // other posts' attachments without having any itself.
  foreach ($this->dependencies as $migration_name) {
    $migration = Migration::getInstance($migration_name);
    $map_table = $migration
      ->getMap()
      ->getMapTable();
    $nids = db_select($map_table, 'be')
      ->fields('be', array(
      'destid1',
    ))
      ->isNotNull('destid1')
      ->execute();
    foreach ($nids as $nid_row) {
      $node = node_load($nid_row->destid1);
      if ($node && is_array($node->body)) {
        foreach ($node->body as $language => $body) {
          $body = $body[0]['value'];

          // If we have the media module, rewrite the img tags to media tags if
          // we can.
          if (module_exists('media')) {
            $this->referencedFiles = array();
            $body = preg_replace_callback('|<img +(.*?)>|i', array(
              $this,
              'replaceImgs',
            ), $body);

            // Add any referenced files to the node's attachment field, if
            // one is configured.
            $migration_arguments = $migration
              ->getArguments();
            if (!empty($migration_arguments['attachment_field'])) {
              $attachment_field = $migration_arguments['attachment_field'];
              foreach ($this->referencedFiles as $file) {
                $node->{$attachment_field}[LANGUAGE_NONE][] = $file;
              }
            }
          }

          // See if any remaining images can be directly replaced.
          $result = db_select('wordpress_migrate_attachment', 'a')
            ->fields('a', array(
            'original_url',
            'new_uri',
            'new_fid',
          ))
            ->condition('parent_nid', $nid_row->destid1)
            ->execute();
          foreach ($result as $row) {
            $body = str_replace($row->original_url, $row->new_uri, $body);
          }
          $node->body[$language][0]['value'] = $body;
        }

        // Maintain the original update datestamp
        $changed = $node->changed;
        $this
          ->disablePathauto($node);
        node_save($node);
        db_update('node')
          ->fields(array(
          'changed' => $changed,
        ))
          ->condition('nid', $node->nid)
          ->execute();
      }
    }
  }
  migrate_instrument_stop('WordPressAttachment postImport');
}