From e8d6b5fb7a6be4b0e55cc450f79f2c3f76cee9c8 Mon Sep 17 00:00:00 2001
From: Lorenz Schori <lo@znerol.ch>
Date: Sun, 6 Jan 2013 15:22:56 +0100
Subject: [PATCH] Cleanup authcache.inc after d7 migration

* Clarify header documentation
* Replace references to DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE with new name
  DRUPAL_BOOTSTRAP_PAGE_CACHE.
* Remove special handling when memcache is in use.
* Use drupal_bootstrap to initialize database.
* Add X-Drupal-Cache HTTP headers in the spirit of 7 core.
---
 authcache.inc |   65 +++++++++++++++++++++++++--------------------------------
 1 file changed, 29 insertions(+), 36 deletions(-)

diff --git a/authcache.inc b/authcache.inc
index d94b436..2dd1694 100644
--- a/authcache.inc
+++ b/authcache.inc
@@ -2,43 +2,43 @@
 
 /**
  * @file
- * Includes the cache handler (e.g., CacheRouter, Memcache API, Drupal core),
- * retrieves cached paged via page_cache_fastpath(),
- * and executes Ajax phase if XML/JS request detected
+ * Attempt to deliver a cached version of a page depending on the users role.
  *
- * @var $conf['authcache_handler'] = display during debug mode
- * @see DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE in boostrap.inc
+ * This file gets included by _drupal_bootstrap_page_cache in bootstrap.inc
+ * during the drupal bootstrap stage DRUPAL_BOOTSTRAP_PAGE_CACHE. This script
+ * serves two purposes:
+ * - Delegate the request to the underlying cache handler and deliver a cached
+ *   version of a page for this request. Then exit.
+ * - Execute Ajax callbacks necessary for customizing a page delivered before
+ *   and then exit.
+ *
+ * In the event of a cache-miss or if a page is not cachable, execution is
+ * passed back to _drupal_bootstrap_page_cache and the cache will be served
+ * using a full bootstrap.
+ *
+ * @see _drupal_bootstrap_page_cache in boostrap.inc
  */
 
-global $conf;
-
 // Check if in Ajax phase and return JSON.
-// This is a custom HTTP header defined by the authcache.js XML request
+// This is a custom HTTP header defined by the authcache.js XHR
 if (isset($_SERVER['HTTP_AUTHCACHE'])) {
   require_once dirname(__FILE__) .'/ajax/authcache.php';
   exit;
 }
 
-if (empty($conf['cache_class_cache_page'])) {
-	// fallback to Drupal core cache handler (database cache tables)
-	$conf['authcache_is_db'] = TRUE;
-	$conf['cache_class_cache_page'] = 'DrupalDatabaseCache';
-	//require_once DRUPAL_ROOT . 'includes/cache.inc'; //simg: <-- is this still needed?
-} else {
-	$conf['authcache_is_db'] = FALSE;
+// Attempt to deliver the page from cache
+$delivered = authcache_retrieve_cache_page();
+if ($delivered) {
+  exit;
 }
 
-// if not using db for cache then we can query the cache now before any further bootstrapping is done
-if (authcache_retrieve_cache_page()) die();
-
-
 /**
- * Main callback from DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE phase.
- * Sends cached page to browser, if found.
- * @return boolean for bootstrap (if TRUE, PHP will exit)
+ * Send cached page to browser, if found.
+ *
+ * @return boolean TRUE if page was delivered, FALSE otherwise
  */
 function authcache_retrieve_cache_page(){
-	global $base_root, $cache, $conf;
+	global $base_root;
 
 	// User is logged in but their role should not receive any cached pages
 	// (i.e., cached anonymous pages, since they have no authcache key)
@@ -62,17 +62,10 @@ function authcache_retrieve_cache_page(){
 	// Attempt to retrieve page from cache
 	if (empty($_POST)) {
 		
-		// Connect to database if default database cache hander is selected (or memcached which currently requires database access to run variable_initialize)
-		if ($conf['authcache_is_db'] || variable_get('cache_class_cache_page', '') == 'MemCacheDrupal') {
-			require_once DRUPAL_ROOT . '/includes/database/database.inc';
-			db_set_active();
-		}
-
-		// Memcache module's cache_get function requires variables to be loaded
-		//if (isset($conf['authcache_handler']) && $conf['authcache_handler'] == 'memcache.inc' && $vars_exist = cache_get('variables', 'cache')) {
-		//TODO D7: simg: not sure if cache_get(variables) still needed?
-		if (variable_get('cache_class_cache_page', '') == 'MemCacheDrupal') {
-			$conf = variable_initialize($conf);
+		// Connect to database if default database cache hander is
+		// selected
+		if (variable_get('cache_class_cache_page', 'DrupalDatabaseCache') == 'DrupalDatabaseCache') {
+			drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE, FALSE);
 		}
 
 		// Authenticated cache role(s) key
@@ -83,6 +76,7 @@ function authcache_retrieve_cache_page(){
 		
 		if (!empty($cache)) {
 			// Cached page found
+			header('X-Drupal-Cache: HIT');
 			
 			// render cache benchmark
 			if (isset($_COOKIE['authcache_debug'])) {
@@ -94,10 +88,9 @@ function authcache_retrieve_cache_page(){
 			
 		} else {
 			//cache miss
+			header('X-Drupal-Cache: MISS');
 		}
 	} else {
 		//form post detected so allow normal page processing
 	}
 }
-
-
-- 
1.7.10.4

