You are here

dynamic_cache_test.module in Dynamic Cache 6

Same filename and directory in other branches
  1. 7 dynamic_cache_test.module

Dynamic Cache Test module.

Provides a test page for Dynamic Cache.

File

dynamic_cache_test.module
View source
<?php

/**
 * @file
 * Dynamic Cache Test module.
 *
 * Provides a test page for Dynamic Cache.
 */

/**
 * Implements hook_boot().
 */
function dynamic_cache_test_boot() {
  if (empty($GLOBALS['conf']['cache'])) {
    $GLOBALS['conf']['cache_original'] = 0;
  }
  else {
    $GLOBALS['conf']['cache_original'] = $GLOBALS['conf']['cache'];
  }
  if (dynamic_cache_test_is_dynamic()) {
    $GLOBALS['conf']['cache'] = CACHE_DISABLED;
  }
}

/**
 * Implements hook_menu().
 */
function dynamic_cache_test_menu() {
  $items = array();
  $items['dynamic_cache_test'] = array(
    'title' => 'Dynamic Cache Test',
    'page callback' => 'dynamic_cache_test_page',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['dynamic_cache_test/set_cookie'] = array(
    'page callback' => 'dynamic_cache_test_set_cookie',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['dynamic_cache_test/clear_cookie'] = array(
    'page callback' => 'dynamic_cache_test_clear_cookie',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback for dynamic_cache/test.
 */
function dynamic_cache_test_page() {
  $output = '';

  // Print info about Anonymous status, cache status, and test cookie.
  global $user, $conf;

  // Track conditions necessary for normal anonymous caching to be active.
  $anon_cache = 0;
  if (array_key_exists(DRUPAL_ANONYMOUS_RID, $user->roles)) {
    $output .= '<p style="background:#8f8;">' . t('Current user is ANONYMOUS.') . '</p>';
    $anon_cache++;
  }
  elseif (array_key_exists(DRUPAL_AUTHENTICATED_RID, $user->roles)) {
    $output .= '<p style="background:#f88;">' . t('Current user is AUTHENTICATED (Dynamic Cache only affects Anonymous users).') . '</p>';
  }
  switch ($GLOBALS['conf']['cache_original']) {
    case CACHE_AGGRESSIVE:
      $output .= '<p style="background:#f88;">' . t('Cache is set to CACHE_AGGRESSIVE (Dynamic Cache will not work).') . '</p>';
      break;
    case CACHE_DISABLED:
      $output .= '<p style="background:#f88;">' . t('Cache is set to CACHE_DISABLED (Dynamic Cache has no effect).') . '</p>';
      break;
    case CACHE_NORMAL:
      $output .= '<p style="background:#8f8;">' . t('Cache is set to CACHE_NORMAL.') . '</p>';
      $anon_cache++;
      break;
  }
  $generation_type = $anon_cache >= 2 && !dynamic_cache_test_is_dynamic() ? t('CACHED') : t('DYNAMIC');
  $output .= '<p>' . t('This page was generated at') . ':</p><h2>' . date('H:i:s') . ' (' . $generation_type . ')</h2><p>(' . date('D M d Y') . ').</p>';
  $output .= '<h3>' . t('Options') . '</h3>';
  $output .= '<ul><li>' . l(t('Set Cookie'), 'dynamic_cache_test/set_cookie') . ' ' . t('(this cookie will activate Dynamic Cache, allowing anonymous users to see a dynamic page).') . '</li>';
  $output .= '<li>' . l(t('Clear Cookie'), 'dynamic_cache_test/clear_cookie') . ' ' . t('(clears the cookie, returning anonymous users to seeing the cached page).') . '</li></ul>';
  $output .= '<p>' . t('Cookie') . ': ' . (empty($_COOKIE['dynamic_cache_test']) ? '0' : '1') . '</p>';
  return $output;
}

/**
 * Menu callback for dynamic_cache_test/set_cookie.
 */
function dynamic_cache_test_set_cookie() {
  setcookie('dynamic_cache_test', '1');
  drupal_goto('dynamic_cache_test');
}

/**
 * Menu callback for dynamic_cache_test/clear_cookie.
 */
function dynamic_cache_test_clear_cookie() {
  setcookie('dynamic_cache_test', '0');
  drupal_goto('dynamic_cache_test');
}

/**
 * Test if current page should be dynamic.
 */
function dynamic_cache_test_is_dynamic() {
  return $_GET['q'] == 'dynamic_cache_test' && !empty($_COOKIE['dynamic_cache_test']);
}

Functions

Namesort descending Description
dynamic_cache_test_boot Implements hook_boot().
dynamic_cache_test_clear_cookie Menu callback for dynamic_cache_test/clear_cookie.
dynamic_cache_test_is_dynamic Test if current page should be dynamic.
dynamic_cache_test_menu Implements hook_menu().
dynamic_cache_test_page Menu callback for dynamic_cache/test.
dynamic_cache_test_set_cookie Menu callback for dynamic_cache_test/set_cookie.