You are here

dynamic_cache_test.module in Dynamic Cache 7

Same filename and directory in other branches
  1. 6 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.
 */
global $dynamic_cache_test_boot_count;
$dynamic_cache_test_boot_count = 0;

/**
 * Implements hook_boot().
 */
function dynamic_cache_test_boot() {
  $GLOBALS['dynamic_cache_test_boot_count']++;
  if (array_key_exists('cache_original', $GLOBALS)) {
    drupal_set_message('hook_boot() called twice!', 'error');
  }
  else {
    if (empty($GLOBALS['conf']['cache'])) {
      $GLOBALS['cache_original'] = false;
    }
    else {
      $GLOBALS['cache_original'] = $GLOBALS['conf']['cache'];
    }
  }
  if (dynamic_cache_test_is_dynamic()) {
    $GLOBALS['conf']['cache'] = false;
  }
}

/**
 * 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>';
  }
  if (empty($GLOBALS['cache_original'])) {
    $output .= '<p style="background:#f88;">' . t('Cache is DISABLED (Dynamic Cache has no effect).') . '</p>';
  }
  else {
    $output .= '<p style="background:#8f8;">' . t('Cache is ENABLED.') . '</p>';
    $anon_cache++;
  }
  if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) {
    $output .= '<p style="background:#8f8;">' . t('Bootstrap phase') . ': ' . DRUPAL_BOOTSTRAP_FULL . ' {DRUPAL_BOOTSTRAP_FULL}</p>';
  }
  else {
    $output .= '<p style="background:#f88;">' . t('Bootstrap phase') . ': ' . drupal_get_bootstrap_phase() . ' (' . t('Should be') . ' ' . DRUPAL_BOOTSTRAP_FULL . ' {DRUPAL_BOOTSTRAP_FULL})</p>';
  }
  if ($GLOBALS['dynamic_cache_test_boot_count'] == 1) {
    $output .= '<p style="background:#8f8;">hook_boot() ' . t('count') . ': 1</p>';
  }
  else {
    $output .= '<p style="background:#f88;">hook_boot() ' . t('count') . ': ' . $GLOBALS['dynamic_cache_test_boot_count'] . ' (' . t('Should be') . ' 1)</p>';
  }
  $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.

Globals

Namesort descending Description
$dynamic_cache_test_boot_count @file Dynamic Cache Test module.