You are here

function getid3_analyze_existing_files in getID3() 7.2

Performs batch operations to update existing file entity metadata.

Parameters

$context:

Return value

null|string

1 call to getid3_analyze_existing_files()
getid3_update_7202 in ./getid3.install
Moves getID3 metadata information into File Entity.
1 string reference to 'getid3_analyze_existing_files'
getid3_perform_batch_process in ./getid3.module
Triggers the batch operation for updating file entities.

File

./getid3.module, line 234

Code

function getid3_analyze_existing_files(&$context) {

  // Little workaround so update hook and can use same batch.
  if (isset($context['sandbox'])) {
    $sandbox =& $context['sandbox'];
  }
  else {
    $sandbox =& $context;
  }

  // Lets update 10 files at a time.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_fid'] = 0;

    // We only select files that are not in the getid3_meta table.
    $sandbox['max'] = db_query('SELECT COUNT(fid) FROM {file_managed} f WHERE f.filemime LIKE :video OR f.filemime LIKE :audio', array(
      ':video' => 'video%',
      ':audio' => 'audio%',
    ))
      ->fetchField();
  }
  $query = db_select('file_managed', 'f')
    ->fields('f', array(
    'fid',
  ))
    ->condition('fid', $sandbox['current_fid'], '>')
    ->range(0, 10)
    ->orderBy('fid', 'ASC');
  $db_or = db_or();
  $db_or
    ->condition('f.filemime', db_like('video') . '%', 'LIKE');
  $db_or
    ->condition('f.filemime', db_like('audio') . '%', 'LIKE');
  $query
    ->condition($db_or);
  $files = $query
    ->execute()
    ->fetchAll();
  foreach ($files as $file) {

    // We just need to re-save entities.
    $file = file_load($file->fid);
    file_save($file);

    // Update batch
    $sandbox['progress']++;
    $context['results'][] = $file->fid;
    $sandbox['current_fid'] = $file->fid;
  }
  $context['message'] = t('Parsing @current out of @max files', array(
    '@current' => count($context['results']),
    '@max' => $sandbox['max'],
  ));

  // Let Batch API or upgrade hook know if this batch set is completed.
  $context['finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  $sandbox['#finished'] = $context['finished'];
}