EVOLUTION-NINJA
Edit File: agls.install
<?php /** * @file * Install file for AGLS. * * @copyright Copyright(c) 2012 Previous Next Pty Ltd * @license GPL v2 http://www.fsf.org/licensing/licenses/gpl.html * @author Chris Skene chris at previousnext dot com dot au */ /** * Fix Metatag records for DC and AGLS. */ function agls_update_7001(&$sandbox) { return agls_install_update_tags($sandbox, '_agls_fix_tag_names', '7001'); } /** * Helper to update tags. * * @param array $sandbox * Batch process sandbox. * @param string $batch_callback * Callback to pass individual records to. See _agls_fix_tag_names() for an * example. * @param string $update_number * The hook_update_N number. * * @return null|string * A message. */ function agls_install_update_tags(&$sandbox, $batch_callback, $update_number) { // Process records by groups of 20 (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 = 20; // 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; // Update Node values. $nodes = db_select('metatag', 'm') ->fields('m') ->execute() ->fetchAll(); $sandbox['records'] = array(); foreach ($nodes as $record) { $sandbox['records'][] = $record; } // If there's no data, don't bother with the extra work. if (empty($sandbox['records'])) { watchdog('agls', 'Update ' . $update_number . ': No nodes need the Metatag values fixed.', array(), WATCHDOG_INFO); if (drupal_is_cli()) { drupal_set_message(t('Update %num: No nodes need the Metatag values fixed.', array( '%num' => $update_number, ))); } return t('No nodes need the Metatag values fixed.'); } // Total records that must be visited. $sandbox['max'] = count($sandbox['records']); // A place to store messages during the run. $sandbox['messages'] = array(); // An initial record of the number of records to be updated. watchdog('agls', 'Update ' . $update_number . ': !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO); if (drupal_is_cli()) { drupal_set_message(t('Update %num: !count records to update.', array( '%num' => $update_number, '!count' => $sandbox['max'], ))); } // Last record processed. $sandbox['current_record'] = -1; // Because a lot of other processing happens on the first iteration, just do // one. $limit = 1; } // The for loop will run as normal when ran via update.php, but when ran via // Drush it'll just run 'til it's finished. $increment = 1; if (drupal_is_cli()) { $increment = 0; } // Set default values. for ($ctr = 0; $ctr < $limit; $ctr += $increment) { $sandbox['current_record']++; if (empty($sandbox['records'][$sandbox['current_record']])) { break; } $sandbox['records'][$sandbox['current_record']]->data = unserialize($sandbox['records'][$sandbox['current_record']]->data); if (function_exists($batch_callback) || is_callable($sandbox['records'][$sandbox['current_record']])) { $sandbox['records'][$sandbox['current_record']] = $batch_callback($sandbox['records'][$sandbox['current_record']]); } db_update('metatag') ->fields(array('data' => serialize($sandbox['records'][$sandbox['current_record']]->data))) ->condition('entity_type', 'node') ->condition('entity_id', $sandbox['records'][$sandbox['current_record']]->entity_id) ->execute(); // 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. $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']); if ($sandbox['#finished']) { // 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('agls', 'Update ' . $update_number . ': !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO); if (drupal_is_cli()) { drupal_set_message(t('Update %num: !count records were updated.', array( '%num' => $update_number, '!count' => $sandbox['progress'], ))); } // hook_update_N() may optionally return a string which will be displayed // to the user. return t('Fixed the Metatag values for @count nodes.', array('@count' => $sandbox['progress'])); } } /** * Fix tags. */ function _agls_fix_tag_names($record) { if (!empty($record->data)) { foreach ($record->data as $tag_name => $tag_data) { // Determine if this tag specifies a schema. if (strpos($tag_name, '.')) { list($schema, $schema_property) = explode('.', $tag_name); } elseif (strpos($tag_name, ':')) { list($schema, $schema_property) = explode(':', $tag_name); } else { continue; } // Correct DCTERMS, which changed case when moved to metatag_dc. if ($schema == 'DCTERMS') { unset($record->data[$tag_name]); $record->data[strtolower($tag_name)] = $tag_data; } // Correct AGLSTERMS, which changed case and format in beta2. if (strtolower($schema) == 'aglsterms') { unset($record->data[$tag_name]); $record->data['AGLSTERMS.' . $schema_property] = $tag_data; } } } return $record; }