You are here

agls.install in AGLS Metadata 7

Update scripts for the AGLS module.

File

agls.install
View source
<?php

/**
 * @file
 * Update scripts for the AGLS module.
 */

/**
 * Fix Metatag records for DC and AGLS.
 */
function agls_update_7001(&$sandbox) {
  return agls_install_update_tags($sandbox, '_agls_fix_tags_7001', '7001');
}

/**
 * Replace AGLSTERMS.isBasisFor and AGLSTERMS.isBasedOn with dcterms.
 */
function agls_update_7002(&$sandbox) {
  return agls_install_update_tags($sandbox, '_agls_fix_tags_7002', '7002');
}

/**
 * Helper to update tags.
 *
 * @param array $sandbox
 *   Batch process sandbox.
 * @param string $batch_callback
 *   Callback to pass individual records to. See _agls_fix_tags_7001() 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 naming of the tags for update 7001.
 */
function _agls_fix_tags_7001($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;
}

/**
 * Fix naming of the tags for update 7002.
 */
function _agls_fix_tags_7002($record) {
  if (!empty($record->data)) {
    foreach ($record->data as $tag_name => $tag_data) {

      // Fix 'isBasisFor'.
      if ($tag_name == 'AGLSTERMS.isBasisFor') {
        unset($record->data[$tag_name]);
        $record->data['dcterms.isBasisFor'] = $tag_data;
      }

      // Fix 'isBasisFor'.
      if ($tag_name == 'AGLSTERMS.isBasedOn') {
        unset($record->data[$tag_name]);
        $record->data['dcterms.isBasedOn'] = $tag_data;
      }
    }
  }
  return $record;
}

Functions

Namesort descending Description
agls_install_update_tags Helper to update tags.
agls_update_7001 Fix Metatag records for DC and AGLS.
agls_update_7002 Replace AGLSTERMS.isBasisFor and AGLSTERMS.isBasedOn with dcterms.
_agls_fix_tags_7001 Fix naming of the tags for update 7001.
_agls_fix_tags_7002 Fix naming of the tags for update 7002.