You are here

function metatag_update_7011 in Metatag 7

Fix {metatag} records for nodes.

File

./metatag.install, line 1105
Install, update, and uninstall functions for the metatag module.

Code

function metatag_update_7011(array &$sandbox) {

  // Fix the {metatag} table first.
  metatag_update_7015();

  // Only proceed if Entity_Translation is not enabled as it allows each node
  // record to have multiple languages available.
  if (module_exists('entity_translation')) {
    drupal_set_message(t("Entity Translation is enabled, so node meta tags will not be updated, to avoid accidental dataloss."));
    return;
  }

  // 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;

    // Remove duplicates.
    _metatag_remove_dupes('node');

    // Update Node values.
    $nodes = db_query("SELECT n.nid, n.language FROM {node} n INNER JOIN {metatag} m ON n.nid = m.entity_id WHERE m.entity_type = 'node' AND n.language != m.language ORDER BY nid");
    $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('metatag', 'Update 7011: No nodes need the Metatag language values fixed.', array(), WATCHDOG_INFO);
      if (drupal_is_cli()) {
        drupal_set_message(t('Update 7011: No nodes need the Metatag language values fixed.'));
      }
      return t('No nodes need the Metatag language 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('metatag', 'Update 7011: !count records to update.', array(
      '!count' => $sandbox['max'],
    ), WATCHDOG_INFO);
    if (drupal_is_cli()) {
      drupal_set_message(t('Update 7011: !count records to update.', array(
        '!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;
  }

  // Set default values.
  for ($ctr = 0; $ctr < $limit; $ctr++) {
    $sandbox['current_record']++;
    if (empty($sandbox['records'][$sandbox['current_record']])) {
      break;
    }

    // Shortcuts for later.
    $langcode = $sandbox['records'][$sandbox['current_record']]->language;
    $nid = $sandbox['records'][$sandbox['current_record']]->nid;
    db_update('metatag')
      ->fields(array(
      'language' => $langcode,
    ))
      ->condition('entity_type', 'node')
      ->condition('entity_id', $nid)
      ->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.
  if ($sandbox['progress'] >= $sandbox['max']) {
    $sandbox['#finished'] = TRUE;
  }
  else {
    $sandbox['#finished'] = $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('metatag', 'Update 7011: !count records were updated in total.', array(
      '!count' => $sandbox['progress'],
    ), WATCHDOG_INFO);
    if (drupal_is_cli()) {
      drupal_set_message(t('Update 7011: !count records were updated.', array(
        '!count' => $sandbox['progress'],
      )));
    }

    // hook_update_N() may optionally return a string which will be displayed
    // to the user.
    return t('Fixed the Metatag language values for @count nodes.', array(
      '@count' => $sandbox['progress'],
    ));
  }
}