You are here

acquia_purge.admin.inc in Acquia Purge 6

Same filename and directory in other branches
  1. 7 acquia_purge.admin.inc

Admin page callbacks and theme functions for the Acquia Purge module.

File

acquia_purge.admin.inc
View source
<?php

/**
 * @file
 * Admin page callbacks and theme functions for the Acquia Purge module.
 */

/**
 * Menu callback; process a chunk of purge items via AJAX.
 *
 * @returns
 *   Statistics array encoded as JSON, including a 'widget' HTML snippet.
 */
function acquia_purge_ajax_processor() {
  $status = _acquia_purge_queue_stats();

  // Define a key 'error' that describes a potential error condition.
  $status['error'] = $status['locked'] = FALSE;

  // Lock acquiring assures us that nothing is purging the same paths at the
  // same time. All ways that trigger purging (ajax, drush) respect the locks.
  if (lock_acquire('acquia_purge_ajax_processor', ACQUIA_PURGE_QUEUE_LOCK_TIMEOUT)) {

    // Pop items from the queue and immediately process them.
    _acquia_purge_queue_pop('_acquia_purge_queue_processpurge');

    // Refresh the statistics post-run, so override most fields in $status.
    foreach (_acquia_purge_queue_stats() as $key => $value) {
      $status[$key] = $value;
    }

    // Ask our built-in diagnostics system to preliminary find issues that are
    // so risky we can expect problems. Everything starting with
    // ACQUIA_PURGE_SEVLEVEL_ERROR will cause purging to cease and log messages
    // to be written. Warn the user on any of them.
    if (count($e = _acquia_purge_get_diagnosis(ACQUIA_PURGE_SEVLEVEL_ERROR))) {
      $error = current($e);
      $status['error'] = $error['description'];
    }
    elseif (empty($status['purgehistory'])) {
      $status['error'] = t("The system seems to be having difficulties\n        refreshing recent content changes. Your work won't be lost, but please\n        do ask your technical administrator to check the logs.");
    }

    // We're done so lets release the lock.
    lock_release('acquia_purge_ajax_processor');
  }
  else {
    $status['locked'] = TRUE;
  }

  // Render the status widget and add it to the statistics array.
  $status['widget'] = theme('acquia_purge_status_bar_widget', $status);

  // Return the status array with statistics...
  return drupal_json($status);
}

/**
 * Menu callback to drupal_get_form; let users manually purge pages.
 *
 * @returns
 *   A standard Form API form-array.
 */
function acquia_purge_manualpurge_form(&$form_state) {
  $fields_amount = 7;
  $schemes = _acquia_purge_get_protocol_schemes();
  $domains = _acquia_purge_get_domains();
  $form['description'] = array(
    '#type' => 'item',
    '#value' => t('<p>This form allows you to purge one or more paths from your
    Acquia Cloud load balancers, e.g.: <b>&lt;front&gt; ?page=1</b>. This
    functionality is not intended for day-to-day use but rather for emergency
    cases when an outdated copy of a page is served. It is highly recommended to
    automate your site by creating rules for these paths, so everybody editing
    content can rely on your site refreshing properly.</p>'),
  );
  $form['domains'] = array(
    '#type' => 'item',
    '#value' => '<p>' . theme('item_list', $domains, t('Paths will be purged on:')) . '</p>',
  );

  // We'll group all paths both in a HTML wrapper and logically in the form.
  $form['#tree'] = TRUE;
  $form['paths'] = array(
    '#prefix' => '<p><h3>' . t('Paths to be purged:') . '</h3><ul>',
    '#suffix' => '</ul></p>',
  );

  // Fill paths with the right amount of form items according to fields_amount.
  $prefix = sprintf('<b>%s://%s%s</b>', $schemes[0], $domains[0], base_path());
  for ($i = 0; $i < $fields_amount; $i++) {
    $form['paths']['path'][$i] = array(
      '#type' => 'textfield',
      '#field_prefix' => $prefix,
      '#size' => 30,
      '#prefix' => '<li>',
      '#suffix' => '</li>',
    );
  }

  // Render the submit button and return the form.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t("I know the risks, purge!"),
  );
  return $form;
}

/**
 * Callback to drupal_get_form: validate the form.
 */
function acquia_purge_manualpurge_form_validate($form, &$form_state) {
  $paths = array();
  foreach ($form_state['values']['paths']['path'] as $id => $path) {
    if (empty($path)) {
      continue;
    }
    if (!is_string($path)) {
      form_set_error('paths][path][' . $id, t("The path has to be a string!"));
    }
    elseif (stristr($path, 'http:') || stristr($path, 'https:')) {
      form_set_error('paths][path][' . $id, t("You can't provide a URL, only paths!"));
    }
    elseif (preg_match('/\\s/', $path)) {
      form_set_error('paths][path][' . $id, t('The path can not contain a space!'));
    }
    elseif (in_array($path, $paths)) {
      form_set_error('paths][path][' . $id, t('You have already listed this path!'));
    }
    $paths[] = $path;
  }
}

/**
 * Callback to drupal_get_form: handle the form submit.
 */
function acquia_purge_manualpurge_form_submit($form, &$form_state) {
  foreach ($form_state['values']['paths']['path'] as $id => $value) {
    if (empty($value)) {
      unset($form_state['values']['paths']['path'][$id]);
    }
  }
  acquia_purge_purge_paths($form_state['values']['paths']['path']);
}

/**
 * Returns HTML for the Acquia Purge status widget.
 *
 * @param array $variables
 *   An associative array containing:
 *   - total: Total amount of actions initially queued for purging.
 *   - remaining: Remaining number of purge actions still to be processed.
 *   - processed: Number of purge actions that have already taken place.
 *   - percent: Value between 0-100 representing the progress percentage.
 *   - running: Whether URLs are being purged or not.
 *   - purgehistory: Array with recently purged URL's.
 *
 * @ingroup themeable
 */
function theme_acquia_purge_status_widget($variables) {
  $progress_bar = theme('acquia_purge_status_bar_widget', $variables);

  // Prepare the table which we use as container.
  $table_variables = array(
    'header' => array(),
    'rows' => array(
      array(
        $progress_bar,
      ),
    ),
  );

  // Render the history list and add it to the table.
  if (count($variables['purgehistory'])) {
    $table_variables['rows'][] = array(
      theme('item_list', array(
        'items' => $variables['purgehistory'],
        'title' => t('Recently refreshed:'),
      )),
    );
  }

  // Render the table.
  $table = theme('table', $table_variables);

  // Return the HTML.
  return '<div id="acquia-purge-status-widget">' . $table . '</div>';
}

/**
 * Returns HTML for the Acquia Purge progress bar widget.
 *
 * @param array $variables
 *   An associative array containing:
 *   - total: Total amount of actions initially queued for purging.
 *   - remaining: Remaining number of purge actions still to be processed.
 *   - processed: Number of purge actions that have already taken place.
 *   - percent: Value between 0-100 representing the progress percentage.
 *   - running: Whether URLs are being purged or not.
 *   - purgehistory: Array with recently purged URL's.
 *
 * @ingroup themeable
 */
function theme_acquia_purge_status_bar_widget($variables) {

  // Determine status messages based on what the statistics tells us.
  if ($variables['running']) {
    $message = t("Site content is being refreshed, please wait a moment...\n    %remaining items to go...", array(
      '%remaining' => $variables['remaining'],
    ));
  }
  else {
    $message = t("Content changes are now visible for everybody!");
  }
  return theme('progress_bar', $variables['percent'], $message);
}

Functions

Namesort descending Description
acquia_purge_ajax_processor Menu callback; process a chunk of purge items via AJAX.
acquia_purge_manualpurge_form Menu callback to drupal_get_form; let users manually purge pages.
acquia_purge_manualpurge_form_submit Callback to drupal_get_form: handle the form submit.
acquia_purge_manualpurge_form_validate Callback to drupal_get_form: validate the form.
theme_acquia_purge_status_bar_widget Returns HTML for the Acquia Purge progress bar widget.
theme_acquia_purge_status_widget Returns HTML for the Acquia Purge status widget.