You are here

function panelizer_update_7120 in Panelizer 7.3

Update 7115 may have resulted in {panels_pane} records not being created properly. This will create them, and may take some time.

File

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

Code

function panelizer_update_7120(&$sandbox) {

  // Process records by groups of 1 (arbitrary value). Doing this one at a time
  // because some sites can have a LOT of revisions.
  $limit = 1;

  // When ran through Drush it's Ok to process a larger number of objects at a
  // time.
  if (drupal_is_cli()) {
    $limit = 10;
  }

  // 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} pe\n        LEFT OUTER JOIN {panels_pane} pp\n          ON pe.did = pp.did\n      WHERE pe.did > 0\n        AND pp.did IS NULL");

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

    // Total records that must be processed.
    $sandbox['max'] = $records
      ->rowCount();
    watchdog('panelizer', 'Need to fix panes for @count entities that were lost 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} pe\n      LEFT OUTER JOIN {panels_pane} pp\n        ON pe.did = pp.did\n    WHERE pe.did > 0\n      AND pp.did IS NULL", 0, $limit);
  watchdog('panelizer', 'Fixing panes for @count entities that were lost by update 7115.', array(
    '@count' => $records
      ->rowCount(),
  ));

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

    // Track whether this entity was changed.
    $entity_updated = FALSE;

    // Keep track of the last display for each entity.
    $last_display = NULL;

    // Get each {panelizer_record} for this entity. Load these in REVERSE order
    // because the panes were moved to the last revision.
    $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 DESC", (array) $record);
    foreach ($entity_records as $panelizer) {

      // If this is a custom display, load it.
      if (!empty($panelizer->did)) {
        $display = panels_load_display($panelizer->did);

        // Check if the display is bereft of panes.
        if (empty($display->content)) {

          // Hopefully the "last display" won't be blank.
          if (empty($last_display)) {
            watchdog('panelizer', "Unable to load records for display did for entity_type entity_id. Sorry.", (array) $panelizer);
          }
          else {
            foreach ($last_display->content as $pane) {

              // Clone the pane to avoid accidental damage.
              $new_pane = clone $pane;

              // Erase the pid so a new record can be saved.
              unset($new_pane->pid);

              // Tie the pane to this display.
              $new_pane->did = $display->did;

              // Update the pane's UUID.
              $new_pane->uuid = ctools_uuid_generate();

              // Serialize some of the fields prior to saving.
              foreach (array(
                'access',
                'configuration',
                'cache',
                'style',
                'css',
                'extras',
                'locks',
              ) as $val) {
                $new_pane->{$val} = serialize($new_pane->{$val});
              }

              // Create the pane record.
              db_insert('panels_pane')
                ->fields((array) $new_pane)
                ->execute();

              // Tell the world.
              module_invoke_all('panels_pane_insert', $new_pane);
            }
          }
        }
        else {
          $last_display = $display;
        }
      }
    }

    // The entity was updated, to 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('Recovered panes for @count entities out of @total total.', array(
    '@count' => $sandbox['progress'],
    '@total' => $sandbox['max'],
  ));
}