redirect.migrate.inc in Redirect 7.2
Migrate support for Redirect module.
File
redirect.migrate.inc
View source
<?php
class MigrateRedirectEntityHandler extends MigrateDestinationHandler {
public function __construct() {
$this
->registerTypes(array(
'entity',
));
}
public function fields() {
return array(
'migrate_redirects' => t('Original path(s) to redirect from.'),
'migrate_redirects_language' => t('The language this redirect applies to.'),
);
}
protected function redirectValidate($redirect) {
$redirect = (object) $redirect;
$migration = Migration::currentMigration();
if (url($redirect->source) == url($redirect->redirect)) {
$migration
->saveMessage(t('Redirect to self (!redirect) ignored', array(
'!redirect' => $redirect->redirect,
)), MigrationBase::MESSAGE_INFORMATIONAL);
return FALSE;
}
redirect_hash($redirect);
if ($existing = redirect_load_by_hash($redirect->hash)) {
if ($redirect->rid != $existing->rid) {
$migration
->saveMessage(t('The source path is already being redirected.'), MigrationBase::MESSAGE_INFORMATIONAL);
return FALSE;
}
}
return TRUE;
}
protected function getRedirects($entity, $row) {
if (!empty($row->migrate_redirects)) {
$migrate_redirects = $row->migrate_redirects;
}
else {
$migrate_redirects = isset($entity->migrate_redirects) ? $entity->migrate_redirects : NULL;
}
if ($migrate_redirects && !is_array($migrate_redirects)) {
$migrate_redirects = array(
$migrate_redirects,
);
}
return $migrate_redirects;
}
protected function getRedirectLanguage($entity, $row) {
if (!empty($row->migrate_redirects_language)) {
$language = $row->migrate_redirects_language;
}
elseif (!empty($entity->migrate_redirects_language)) {
$language = $entity->migrate_redirects_language;
}
elseif (!empty($entity->language)) {
$language = $entity->language;
}
else {
$language = LANGUAGE_NONE;
}
return $language;
}
public function complete($entity, stdClass $row) {
$migration = Migration::currentMigration();
$destination = $migration
->getDestination();
$entity_type = $destination
->getEntityType();
$migrate_redirects = $this
->getRedirects($entity, $row);
$redirect_destination = entity_uri($entity_type, $entity);
if (!empty($migrate_redirects) && !empty($redirect_destination)) {
foreach ($migrate_redirects as $path) {
$redirect_defaults = array(
'status_code' => 301,
);
if (isset($entity->uid)) {
$redirect_defaults['uid'] = $entity->uid;
}
$redirect_defaults['language'] = $this
->getRedirectLanguage($entity, $row);
$redirect = new stdClass();
redirect_object_prepare($redirect, $redirect_defaults);
$redirect->redirect = $redirect_destination['path'];
$parsed = redirect_parse_url($path);
$redirect->source = isset($parsed['path']) ? ltrim($parsed['path'], '/') : '';
if (!empty($parsed['query'])) {
$redirect->source_options['query'] = $parsed['query'];
}
if ($this
->redirectValidate($redirect)) {
redirect_save($redirect);
}
}
}
elseif (!empty($migrate_redirects) && empty($redirect_destination)) {
$migration
->saveMessage(t('The redirect path is empty.'), MigrationBase::MESSAGE_INFORMATIONAL);
}
}
}
function redirect_migrate_api() {
$api = array(
'api' => 2,
'destination handlers' => array(
'MigrateRedirectEntityHandler',
),
);
return $api;
}