You are here

function panelizer_update_7119 in Panelizer 7.3

Fix any {panelizer_entity} records that were broken by update 7115. This may take some time.

File

./panelizer.install, line 913
Install, update and uninstall functions for the panelizer module.

Code

function panelizer_update_7119(&$sandbox) {

  // Process records by groups of 10 (arbitrary value).
  $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;
  }

  // The update hasn't been ran before.
  if (!isset($sandbox['progress'])) {

    // The count of records visited so far.
    $sandbox['progress'] = 0;

    // Load any 'panelizer_entity' values that are corrupt.
    $records = db_query("SELECT DISTINCT entity_type, entity_id\n      FROM {panelizer_entity}\n      WHERE contexts LIKE 's:%'\n        OR relationships LIKE 's:%'\n        OR extra LIKE 's:%'");

    // If there are no records, there's nothing to do.
    if ($records
      ->rowCount() == 0) {
      return t('No {panelizer_entity} records were corrupted by update 7115.');
    }

    // Total records that must be processed.
    $sandbox['max'] = $records
      ->rowCount();
    watchdog('panelizer', 'Need to fix {panelizer_entity} records for @count entities that were damaged by update 7115.', array(
      '@count' => $records
        ->rowCount(),
    ));
  }

  // Get a small batch of records.
  $records = db_query_range("SELECT DISTINCT entity_type, entity_id\n    FROM {panelizer_entity}\n    WHERE contexts LIKE 's:%'\n      OR relationships LIKE 's:%'\n      OR extra LIKE 's:%'", 0, $limit);

  // Loop over each record.
  foreach ($records as $record) {

    // Track whether this entity needs to be fixed.
    $entity_updated = FALSE;

    // Get each {panelizer_record} for this entity.
    $entity_records = db_query("SELECT *\n      FROM {panelizer_entity}\n      WHERE entity_type = :entity_type\n        AND entity_id = :entity_id\n      ORDER BY revision_id", (array) $record);
    foreach ($entity_records as $panelizer) {
      $last_display = NULL;

      // Unserialize each of the serialized values.
      foreach (array(
        'contexts',
        'relationships',
        'extra',
      ) as $val) {
        $panelizer->{$val} = unserialize($panelizer->{$val});
      }

      // Keep track of whether the record needs to be saved.
      $panelizer_updated = FALSE;

      // Verify each of the items is an array or if it was double-serialized.
      foreach (array(
        'contexts',
        'relationships',
        'extra',
      ) as $val) {
        if (is_string($panelizer->{$val})) {
          $panelizer->{$val} = unserialize($panelizer->{$val});
          $panelizer_updated = TRUE;
          $entity_updated = TRUE;
        }
      }

      // Update the {panelizer_entity} record.
      if ($panelizer_updated) {
        db_update('panelizer_entity')
          ->fields(array(
          'contexts' => serialize($panelizer->contexts),
          'relationships' => serialize($panelizer->relationships),
          'extra' => serialize($panelizer->extra),
        ))
          ->condition('entity_type', $panelizer->entity_type)
          ->condition('entity_id', $panelizer->entity_id)
          ->condition('revision_id', $panelizer->revision_id)
          ->condition('view_mode', $panelizer->view_mode)
          ->execute();
      }
    }

    // The entity was updated so clear its cache.
    if ($entity_updated) {
      entity_get_controller($panelizer->entity_type)
        ->resetCache(array(
        $panelizer->entity_id,
      ));
    }
    $sandbox['progress']++;
  }
  $sandbox['#finished'] = $sandbox['progress'] >= $sandbox['max'] ? TRUE : $sandbox['progress'] / $sandbox['max'];
  return t('Fixed {panelizer_entity} records for @count entities out of @total total.', array(
    '@count' => $sandbox['progress'],
    '@total' => $sandbox['max'],
  ));
}