function _metatag_importer_migrate in Metatag 7
Batch API callback to convert Nodewords data to the Metatag module.
1 string reference to '_metatag_importer_migrate'
- _metatag_importer_import in metatag_importer/
metatag_importer.nodewords.inc - Migrates Nodewords data to the Metatag module.
File
- metatag_importer/
metatag_importer.nodewords.inc, line 166 - Convert data from Nodewords to Metatag.
Code
function _metatag_importer_migrate(array $types = array(), &$context = array()) {
// Process this number of {nodewords} records at a time.
$limit = 50;
if (empty($context['sandbox'])) {
// @todo Expand this so it can handle other types of things.
foreach ($types as $key => $val) {
$types[$key] = str_replace('nodewords:', '', $val);
}
$context['sandbox']['progress'] = 0;
$context['sandbox']['current'] = 0;
$query = db_select('nodewords', 'nw')
->fields('nw', array(
'mtid',
))
->orderBy('nw.mtid');
if (!empty($types)) {
$query
->condition('nw.type', $types, 'IN');
}
$context['sandbox']['dataset'] = array_keys($query
->execute()
->fetchAllAssoc('mtid', PDO::FETCH_ASSOC));
$context['sandbox']['max'] = count($context['sandbox']['dataset']);
// Track all of the entities that could not be loaded.
$context['sandbox']['skipped'] = array();
}
// Retrieve Nodewords data.
$query = db_select('nodewords', 'nw')
->fields('nw', array(
'mtid',
'type',
'id',
'name',
'content',
))
->condition('nw.mtid', $context['sandbox']['current'], '>')
->orderBy('nw.mtid');
// @todo Finish off / test the $types handling.
// @code
// if (!empty($types)) {
// $query->condition('nw.type', $types, 'IN');
// }
// @endcode
$query
->range(0, $limit);
$results = $query
->execute();
// Records that are being converted.
$records = array();
// Track records that are converted and will be ready to be deleted.
$to_delete = array();
// Convert Nodewords data into the Metatag format.
foreach ($results as $result) {
// Log the progress.
$context['sandbox']['current'] = $result->mtid;
$context['sandbox']['progress']++;
// Convert the Nodewords record 'type' into something Metatag can use.
$type = _metatag_importer_convert_type($result->type);
// Skip record types we're not handling just yet.
if (empty($type)) {
continue;
}
// This could be an entity ID, but also possibly just a placeholder integer.
$record_id = $result->id;
// Check if this record was skipped previously.
if (isset($context['sandbox']['skipped'][$type][$record_id])) {
// Delete this record anyway.
$to_delete[] = $result->mtid;
continue;
}
// If this record is for an entity, verify that the entity exists.
if (in_array($type, array(
'node',
'taxonomy_term',
'user',
))) {
$entity = entity_load($type, array(
$record_id,
));
if (empty($entity)) {
$context['sandbox']['skipped'][$type][$record_id] = $record_id;
watchdog('metatag_importer', 'Unable to load @entity_type ID @id', array(
'@entity_type' => $type,
'@id' => $record_id,
), WATCHDOG_WARNING);
// Delete this record anyway.
$to_delete[] = $result->mtid;
continue;
}
}
// Process the meta tag value, possibly also rename the meta tag name
// itself.
list($meta_tag, $value) = _metatag_importer_convert_data($result->name, unserialize($result->content));
// Don't import empty values.
if (!empty($value)) {
// Add the value to the stack.
$records[$type][$record_id][$meta_tag] = $value;
}
// Note that this record is ready to be deleted.
$to_delete[] = $result->mtid;
}
// Update or create Metatag records.
foreach ($records as $type => $data) {
foreach ($data as $record_id => $values) {
switch ($type) {
// Standard D7 entities are converted to {metatag} records using
// metatag_metatags_save().
case 'node':
case 'taxonomy_term':
case 'user':
// @code
// watchdog('metatag_importer', 'Importing meta tags for @entity_type ID @id..', array('@entity_type' => $type, '@id' => $record_id), WATCHDOG_INFO);
// @endcode
$entity = entity_load($type, array(
$record_id,
));
$entity = reset($entity);
$langcode = metatag_entity_get_language($type, $entity);
list($entity_id, $revision_id, $bundle) = entity_extract_ids($type, $entity);
// Add these meta tags to the entity, overwriting anything that's
// already there.
foreach ($values as $name => $value) {
$entity->metatags[$langcode][$name] = $value;
}
metatag_metatags_save($type, $entity_id, $revision_id, $entity->metatags);
// @code
// watchdog('metatag_importer', 'Imported meta tags for @entity_type ID @id.', array('@entity_type' => $type, '@id' => $record_id), WATCHDOG_INFO);
// @endcode
break;
// Other Nodewords settings are converted to {metatag_config} records
// using metatag_config_save().
case 'global':
case 'global:frontpage':
case 'global:404':
$config = metatag_config_load($type);
// If a configuration was not found create a config object.
if (empty($config)) {
$config = (object) array(
'instance' => $type,
);
}
// Add these meta tags to the configuration, overwriting anything
// that's already there.
foreach ($values as $name => $value) {
$config->config[$name] = $value;
}
// Save the configuration.
metatag_config_save($config);
break;
// @todo A 'vocabulary' setting becomes a default configuration?
case 'vocabulary':
// @code
// $metatags = metatag_metatags_load($record->entity_type, $record->entity_id);
// $metatags = array_merge($metatags, $record->data);
// $vocabulary = taxonomy_vocabulary_load($record->entity_id);
// metatag_metatags_save($record->entity_type, $record->entity_id, $vocabulary->vid, $metatags);
// @endcode
break;
}
}
}
// Delete some records.
if (!empty($to_delete)) {
db_delete('nodewords')
->condition('mtid', $to_delete)
->execute();
}
$context['finished'] = empty($context['sandbox']['max']) || $context['sandbox']['progress'] >= $context['sandbox']['max'] ? TRUE : $context['sandbox']['progress'] / $context['sandbox']['max'];
if ($context['finished'] === TRUE) {
$message = 'Imported @imported Nodewords records.';
$vars = array(
'@imported' => $context['sandbox']['progress'],
);
if (drupal_is_cli() && function_exists('drush_print')) {
drush_print(dt($message, $vars));
}
else {
drupal_set_message(t($message, $vars));
}
if (!empty($context['sandbox']['skipped'])) {
$message = '@skipped records were skipped because the corresponding entities were previously deleted.';
$vars = array(
'@skipped' => count($context['sandbox']['skipped']),
);
if (drupal_is_cli() && function_exists('drush_print')) {
drush_print(dt($message, $vars));
}
else {
drupal_set_message(t($message, $vars));
}
}
}
}