function metatag_update_7040 in Metatag 7
Fix any robots meta tags that were broken during importing from Nodewords.
File
- ./
metatag.install, line 2131 - Install, update, and uninstall functions for the metatag module.
Code
function metatag_update_7040(array &$sandbox) {
// Process records by groups of 10 (arbitrary value).
// When a group is processed, the batch update engine determines whether it
// should continue processing in the same request or provide progress
// feedback to the user and wait for the next request.
$limit = 10;
// When ran through Drush it's Ok to process a larger number of objects at a
// time.
if (drupal_is_cli()) {
$limit = 100;
}
// Use the sandbox at your convenience to store the information needed
// to track progression between successive calls to the function.
if (!isset($sandbox['progress'])) {
// The count of records visited so far.
$sandbox['progress'] = 0;
// Get a count of {metatag} records that have a robots meta tag definition
// that is not in the correct format.
$records = db_select('metatag', 'm')
->fields('m')
->condition('m.data', '%:6:"robots";%', 'LIKE')
->condition('m.data', '%:6:"robots";a:1:{s:5:"value";%', 'NOT LIKE')
->countQuery()
->execute()
->fetchField();
// If there's no data, don't bother with the extra work.
if (empty($records)) {
watchdog('metatag', 'Update 7040: No robots meta tags need to be fixed.', array(), WATCHDOG_INFO);
if (drupal_is_cli()) {
drupal_set_message(t('Update 7040: No robots meta tags need to be fixed.'));
}
return t('No robots meta tags need to be fixed.');
}
// Total records that must be visited.
$sandbox['max'] = $records;
// A place to store messages during the run.
$sandbox['messages'] = array();
// An initial record of the number of records to be updated.
watchdog('metatag', 'Update 7040: !count records to examine.', array(
'!count' => $sandbox['max'],
), WATCHDOG_INFO);
if (drupal_is_cli()) {
drupal_set_message(t('Update 7040: !count records to examine.', array(
'!count' => $sandbox['max'],
)));
}
// Log how many records are fixed.
$sandbox['fixed'] = 0;
// Because a lot of other processing happens on the first iteration, just do
// ten.
$limit = 10;
}
// Get a list of records that need to be fixed.
$records = db_select('metatag', 'm')
->fields('m')
->condition('m.data', '%:6:"robots";%', 'LIKE')
->condition('m.data', '%:6:"robots";a:1:{s:5:"value";%', 'NOT LIKE')
->range(0, $limit)
->execute();
// Set default values.
foreach ($records as $record) {
// Extract the record.
$record->data = unserialize($record->data);
// See if the record needs to be fixed.
if (!empty($record->data['robots']) && empty($record->data['robots']['value'])) {
// Fix the record.
$robots = $record->data['robots'];
$record->data['robots'] = array(
'value' => $robots,
);
// Update the database.
db_update('metatag')
->fields(array(
'data' => serialize($record->data),
))
->condition('entity_type', $record->entity_type)
->condition('entity_id', $record->entity_id)
->condition('revision_id', $record->revision_id)
->condition('language', $record->language)
->execute();
// Clear the cache for this entity.
entity_get_controller($record->entity_type)
->resetCache(array(
$record->entity_id,
));
// Update our progress information.
$sandbox['fixed']++;
}
// Update our progress information.
$sandbox['progress']++;
}
// Set the "finished" status, to tell batch engine whether this function
// needs to run again. If you set a float, this will indicate the progress of
// the batch so the progress bar will update.
if ($sandbox['progress'] >= $sandbox['max']) {
$sandbox['#finished'] = TRUE;
}
else {
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
// Only display a status message if process finished.
if ($sandbox['#finished'] === TRUE) {
// Clear all caches so the fixed data will be reloaded.
cache_clear_all('*', 'cache_metatag', TRUE);
// A final log of the number of records that were converted.
watchdog('metatag', 'Update 7040: !count records were fixed.', array(
'!count' => $sandbox['fixed'],
), WATCHDOG_INFO);
if (drupal_is_cli()) {
drupal_set_message(t('Update 7040: !count records were fixed.', array(
'!count' => $sandbox['fixed'],
)));
}
// hook_update_N() may optionally return a string which will be displayed
// to the user.
return t('Fixed the Metatag robots values for @count nodes.', array(
'@count' => $sandbox['fixed'],
));
}
}