You are here

cacheexclude.module in CacheExclude 5.2

Same filename and directory in other branches
  1. 5 cacheexclude.module
  2. 6.2 cacheexclude.module
  3. 7.2 cacheexclude.module

File

cacheexclude.module
View source
<?php

function cacheexclude_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/cacheexclude',
      'title' => t('Cache exclusions'),
      'description' => t('Configure pages to exclude from caching.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'cacheexclude_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}
function cacheexclude_admin_settings() {
  $form['cacheexclude_list'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages to exclude from caching'),
    '#default_value' => variable_get('cacheexclude_list', ''),
    '#width' => 40,
    '#height' => 10,
    '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_init().
 */
function cacheexclude_init() {
  $pages = trim(variable_get('cacheexclude_list', ''));

  // If the current page is one we want to exclude from the cache,
  // set the page caching to false temporarily.
  if (strlen($pages) && drupal_match_path($_GET['q'], $pages)) {
    $GLOBALS['conf']['cache'] = FALSE;
  }
}

// This function is new in Drupal 6, but is oh-so-useful here that I'm backporting it.
// When upgrading this module to Drupal 6, remove this function.
if (!function_exists('drupal_match_path')) {

  /**
   * Check if a path matches any pattern in a set of patterns.
   *
   * @param $path
   *   The path to match.
   * @param $patterns
   *   String containing a set of patterns separated by \n, \r or \r\n.
   *
   * @return
   *   Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
   */
  function drupal_match_path($path, $patterns) {
    static $regexps;

    // This part is not in Drupal 6, but is necessary because the regex is broken.
    // for the front page, as always.
    if ($path == variable_get('site_frontpage', 'node') && in_array('<front>', array_map('trim', explode("\n", $patterns)))) {
      return true;
    }
    if (!isset($regexps[$patterns])) {
      $regexps[$patterns] = '/^(' . preg_replace(array(
        '/(\\r\\n?|\\n)/',
        '/\\\\\\*/',
        '/(^|\\|)\\\\<front\\\\>($|\\|)/',
      ), array(
        '|',
        '.*',
        '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
      ), preg_quote($patterns, '/')) . ')$/';
    }
    return preg_match($regexps[$patterns], $path);
  }
}