View source
<?php
class WordPressAttachmentSource extends WordPressItemSource {
protected $attachmentFields = array(
'attachment_url' => 'The URL of the attached file',
'_wp_attached_file' => 'Local (to WordPress) filespec',
'_wp_attachment_metadata' => 'Serialized metadata (image size etc.)',
);
public function __construct($filename) {
parent::__construct($filename, 'attachment');
$this->fields += $this->attachmentFields;
}
}
class WordPressAttachment extends WordPressMigration {
public function __construct(array $arguments = array()) {
parent::__construct($arguments);
$this->dependencies = array(
$this
->generateMachineName('WordPressBlogEntry'),
$this
->generateMachineName('WordPressPage'),
);
$dest_options = array(
'copy_file' => TRUE,
);
if (module_exists('media') && module_exists('migrate_extras')) {
$dest_options['source_migrations'] = $this->dependencies;
$this->destination = new MigrateDestinationMedia('image', $dest_options);
$keySchema = MigrateDestinationMedia::getKeySchema();
$this
->addFieldMapping('media_title', 'title');
$this
->addFieldMapping('media_description', 'content');
$this
->addFieldMapping('file')
->issueGroup(t('DNM'));
}
else {
$this->destination = new MigrateDestinationFile($dest_options);
$keySchema = MigrateDestinationFile::getKeySchema();
$this
->addFieldMapping(NULL, 'title')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'content')
->issueGroup(t('DNM'));
}
$this->map = new MigrateSQLMap($this->machineName, array(
'post_id' => array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'description' => 'WordPress post ID',
),
), $keySchema);
$this->source = new WordPressAttachmentSource($this->wxrFile);
$this
->addFieldMapping('fid')
->issueGroup(t('DNM'));
$this
->addFieldMapping('uid', 'creator')
->description('Use matching username if any, otherwise current user');
$this
->addFieldMapping('filename')
->description(t('Defaults to basename(uri)'));
$this
->addFieldMapping('uri', 'attachment_url');
$this
->addFieldMapping('filemime')
->description(t('Determined by default'));
$this
->addFieldMapping('status')
->defaultValue(FILE_STATUS_PERMANENT);
$this
->addFieldMapping('timestamp', 'post_date');
$this
->addFieldMapping(NULL, 'post_parent')
->description('For attachments, indicates item attached to')
->issueGroup(t('Open issues'))
->issuePriority(MigrateFieldMapping::ISSUE_PRIORITY_MEDIUM);
$this
->addFieldMapping('path')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'link')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'guid')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'description')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'excerpt')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'post_id')
->description(t('Primary key of source'))
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'pubDate')
->description('Use post_date')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'post_date_gmt')
->description('Use post_date')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'comment_status')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'ping_status')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'post_name')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'status')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'menu_order')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'post_type')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'post_password')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'is_sticky')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, 'category')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, '_wp_attached_file')
->issueGroup(t('DNM'));
$this
->addFieldMapping(NULL, '_wp_attachment_metadata')
->issueGroup(t('DNM'));
}
public function prepareRow($row) {
if ($row->post_date == '0000-00-00 00:00:00') {
$row->post_date = time();
}
return TRUE;
}
public function prepare(stdClass $file, stdClass $row) {
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['user_map'] =& drupal_static(__FUNCTION__);
$drupal_static_fast['default_user'] =& drupal_static(__FUNCTION__ . 'DefaultUser');
}
$user_map =& $drupal_static_fast['user_map'];
if (!isset($user_map[$row->creator])) {
$user_map[$row->creator] = db_select('users', 'u')
->fields('u', array(
'uid',
))
->condition('name', $row->creator)
->execute()
->fetchField();
if (!$user_map[$row->creator]) {
$default_user =& $drupal_static_fast['default_user'];
if (!isset($default_user)) {
$default_user = db_select('wordpress_migrate', 'wpm')
->fields('wpm', array(
'uid',
))
->condition('filename', $this->wxrFile)
->execute()
->fetchField();
}
$user_map[$row->creator] = $default_user;
}
}
$file->uid = $user_map[$row->creator];
}
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 (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);
}
}
public function postImport() {
parent::postImport();
migrate_instrument_start('WordPressAttachment postImport');
$source_migrations = array(
$this
->generateMachineName('WordPressBlogEntry'),
$this
->generateMachineName('WordPressPage'),
);
foreach ($source_migrations as $source_migration) {
$migration = MigrationBase::getInstance($source_migration);
$map = $migration
->getMap();
foreach ($map as $map_row) {
$nid = $map_row->destid1;
$node = node_load($nid);
$body = $node->body[LANGUAGE_NONE][0]['value'];
$result = db_select('wordpress_migrate_attachment', 'a')
->fields('a', array(
'original_url',
'new_uri',
))
->condition('filename', $this->wxrFile)
->execute();
foreach ($result as $row) {
$body = str_replace($row->original_url, $row->new_uri, $body);
}
$node->body[LANGUAGE_NONE][0]['value'] = $body;
node_save($node);
}
}
migrate_instrument_stop('WordPressAttachment postImport');
}
public function postRollback() {
db_delete('wordpress_migrate_attachment')
->condition('filename', $this->wxrFile)
->execute();
}
}