You are here

function media_wysiwyg_upgrade_content_tokens_batch in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 modules/media_wysiwyg/includes/media_wysiwyg.upgrade.inc \media_wysiwyg_upgrade_content_tokens_batch()

Batch API callback.

See also

media_wysiwyg_upgrade_content_tokens()

1 string reference to 'media_wysiwyg_upgrade_content_tokens_batch'
media_wysiwyg_upgrade_content_tokens in modules/media_wysiwyg/includes/media_wysiwyg.pages.inc
Upgrade media tokens in content.

File

modules/media_wysiwyg/includes/media_wysiwyg.pages.inc, line 373
Common pages for the Media WYSIWYG module.

Code

function media_wysiwyg_upgrade_content_tokens_batch(&$context) {
  $items_per_run = 20;
  $query = db_select('file_managed', 'fm');
  $query
    ->innerJoin('file_usage', 'fu', 'fm.fid = fu.fid');
  $query
    ->fields('fu', array(
    'type',
    'id',
  ))
    ->distinct();
  if (empty($context['results']['!entities_total'])) {

    // The countQuery() returns a cloned SelectQuery object, so we can happily
    // execute it without disturbing the original query. Also, the keys in the
    // 'results' entry are prefixed with '!' so the $results variable can be
    // sendt directly to t() in the batch finished callback.
    $context['results']['!entities_total'] = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    $context['results']['!entities_upgraded'] = 0;
    $context['results']['!tokens_total'] = 0;
    $context['results']['!tokens_upgraded'] = 0;
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_type'] = '';
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['run'] = 0;
  }
  $query
    ->orderBy('fu.type')
    ->orderBy('fu.id')
    ->range($context['sandbox']['run'] * $items_per_run, $items_per_run);
  foreach ($query
    ->execute() as $usage) {
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id'] = $usage->id;
    $context['sandbox']['current_type'] = $usage->type;
    $context['message'] = t("Upgrading tokens: Processing entity # !progress/!total. Last examined: !entity_type/!entity_id", array(
      '!progress' => $context['sandbox']['progress'],
      '!total' => $context['results']['!entities_total'],
      '!entity_type' => $usage->type,
      '!entity_id' => $usage->id,
    ));
    try {
      $report = media_wysiwyg_upgrade_entity_tokens($usage->type, $usage->id);
    } catch (Exception $e) {
      watchdog('media', "Failed to upgrade tokens in entity %entity_id with id %entity_id", array(
        '%entity_type' => $usage->type,
        '%entity_id' => $usage->id,
      ), WATCHDOG_WARNING);
    }
    $context['results']['!tokens_total'] += $report['found'];
    $context['results']['!tokens_upgraded'] += $report['upgraded'];
    if ($report['upgraded']) {
      $context['results']['!entities_upgraded']++;
    }
  }
  if ($context['sandbox']['progress'] < $context['results']['!entities_total']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['results']['!entities_total'];
  }
  $context['sandbox']['run']++;
}