You are here

public function WordPressAttachment::complete in WordPress Migrate 7

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

Called after file object is saved - maintain a mapping from the URL on the original WordPress blog to the URI in Drupal.

Parameters

stdClass $file:

stdClass $row:

File

./wordpress_attachment.inc, line 197

Class

WordPressAttachment
Implementation of WordPressMigration, for attachments

Code

public function complete(stdClass $file, stdClass $row) {
  db_merge('wordpress_migrate_attachment')
    ->key(array(
    'filename' => $this->wxrFile,
    'original_url' => $row->attachment_url,
  ))
    ->fields(array(
    'new_uri' => parse_url(file_create_url($file->uri), PHP_URL_PATH),
  ))
    ->execute();

  // If media_gallery is enabled, add this image to the user's gallery.
  // Lazy-create the gallery node if it doesn't already exist
  // TODO: Needs generalization, takes for granted blog module
  // TODO: Cache fids to add, do them all at once
  if (module_exists('media_gallery')) {
    global $user;
    $blog_title = t("@name's blog", array(
      '@name' => format_username($user),
    ));
    $gallery_nid = db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->condition('type', 'media_gallery')
      ->condition('title', $blog_title)
      ->execute()
      ->fetchField();
    if ($gallery_nid) {
      $gallery_node = node_load($gallery_nid);
    }
    else {
      $gallery_node = new stdClass();
      $gallery_node->type = 'media_gallery';
      $gallery_node->title = $blog_title;
      $gallery_node->uid = $user->uid;
      $gallery_node->language = LANGUAGE_NONE;
    }
    $gallery_node->media_gallery_media[LANGUAGE_NONE][] = array(
      'fid' => $file->fid,
    );
    node_save($gallery_node);
  }
}