public function WordPressAttachment::complete in WordPress Migrate 7.2
Same name and namespace in other branches
- 7 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 180
Class
- WordPressAttachment
- Implementation of WordPressMigration, for attachments
Code
public function complete(stdClass $file, stdClass $row) {
// Skip if no file entity was saved.
if (!$file->fid) {
return;
}
// Find the parent nid
$row->{'wp:post_parent'} = $this
->handleSourceMigration($this->dependencies, $row->{'wp:post_parent'});
// Populate the parent node's attachment field, if applicable
if ($row->{'wp:post_parent'}) {
// Which migration did it come from, posts or pages?
$post_migration_name = $this->group
->getName() . 'BlogPost';
$exists = db_select('migrate_status', 'ms')
->fields('ms', array(
'machine_name',
))
->condition('machine_name', $post_migration_name)
->execute()
->fetchField();
if (!$exists) {
// Legacy migrations used BlogEntry.
$post_migration_name = $this->group
->getName() . 'BlogEntry';
}
/** @var Migration $post_migration */
$post_migration = Migration::getInstance($post_migration_name);
$post_map_row = $post_migration
->getMap()
->getRowByDestination(array(
$row->{'wp:post_parent'},
));
if (!empty($post_map_row)) {
$attachment_field = $this->arguments['blog_attachment_field'];
}
else {
/** @var Migration $page_migration */
$page_migration = Migration::getInstance($this->group
->getName() . 'Page');
$page_map_row = $page_migration
->getMap()
->getRowByDestination(array(
$row->{'wp:post_parent'},
));
if (!empty($page_map_row)) {
$attachment_field = $this->arguments['page_attachment_field'];
}
else {
$attachment_field = '';
}
}
if ($attachment_field) {
$parent_node = node_load($row->{'wp:post_parent'});
if ($parent_node) {
$file->display = 1;
$file->description = '';
// node_load will give us an empty value here, so adding a value with
// [] will mess things up - remove it.
if (isset($parent_node->{$attachment_field}[LANGUAGE_NONE]) && !is_array($parent_node->{$attachment_field}[LANGUAGE_NONE][0])) {
unset($parent_node->{$attachment_field}[LANGUAGE_NONE][0]);
}
$parent_node->{$attachment_field}[LANGUAGE_NONE][] = (array) $file;
$this
->disablePathauto($parent_node);
// Maintain the original update datestamp
$changed = $parent_node->changed;
node_save($parent_node);
db_update('node')
->fields(array(
'changed' => $changed,
))
->condition('nid', $parent_node->nid)
->execute();
file_usage_add($file, 'file', 'node', $parent_node->nid);
}
}
}
// Record the attachment, so we can fix up the parent bodies later
$fields['new_uri'] = parse_url(file_create_url($file->uri), PHP_URL_PATH);
$fields['new_fid'] = $file->fid;
if ($row->{'wp:post_parent'}) {
$fields['parent_nid'] = $row->{'wp:post_parent'};
}
db_merge('wordpress_migrate_attachment')
->key(array(
'blog_id' => $this->blog
->getBlogID(),
'original_url' => $row->{'wp:attachment_url'},
))
->fields($fields)
->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,
);
$this
->disablePathauto($gallery_node);
node_save($gallery_node);
}
}