You are here

function metatag_update_7104 in Metatag 7

Remove the entity revision ID from the translation strings.

File

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

Code

function metatag_update_7104(array &$sandbox) {

  // Verify that locales are being used in the first place.
  if (!module_exists('locale') || !db_table_exists('locales_source')) {
    return t('Metatag: No translations to fix as the locale system is not enabled.');
  }

  // No need to do anything if output translation is disabled.
  if (!variable_get('metatag_i18n_translate_output', FALSE)) {
    return t('Metatag: Output translation is disabled, so no need to update anything.');
  }

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

    // Total records that must be visited.
    $sandbox['max'] = db_query("SELECT COUNT(lid)\n      FROM {locales_source}\n      WHERE textgroup = 'metatag'\n      AND context LIKE 'output:%:%:%:%'")
      ->fetchField();

    // If there's no data, don't bother with the extra work.
    if (empty($sandbox['max'])) {
      watchdog('metatag', 'Update 7104: No nodes need the translation entity string fixed.', array(), WATCHDOG_INFO);
      if (drupal_is_cli()) {
        drupal_set_message(t('Update 7104: No nodes need the translation entity string fixed.'));
      }
      return t('No nodes need the Metatag language values fixed.');
    }

    // 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 7104: !count records to update.', array(
      '!count' => $sandbox['max'],
    ), WATCHDOG_INFO);
    if (drupal_is_cli()) {
      drupal_set_message(t('Update 7104: !count records to update.', array(
        '!count' => $sandbox['max'],
      )));
    }
  }

  // Get a batch of records that need to be fixed.
  $records = db_query_range("SELECT lid, location, context\n    FROM {locales_source}\n    WHERE textgroup = 'metatag'\n    AND context LIKE 'output:%:%:%:%'", 0, $limit);

  // Update each of the records.
  foreach ($records as $record) {
    $old_location = '/metatag:output:([^:]*):([^:]*):([^:]*):([^:]*)/';
    $new_location = 'metatag:output:$1:$2:$4';
    $location = preg_replace($old_location, $new_location, $record->location);
    $old_context = '/output:([^:]*):([^:]*):([^:]*):([^:]*)/';
    $new_context = 'output:$1:$2:$4';
    $context = preg_replace($old_context, $new_context, $record->context);
    drupal_set_message($location);
    drupal_set_message($context);
    db_update('locales_source')
      ->fields(array(
      'location' => $location,
      'context' => $context,
    ))
      ->condition('lid', $record->lid)
      ->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);

    // 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'],
    ));
  }
}