/libraries/joomla/cache/controller/page.php
PHP | 196 lines | 93 code | 23 blank | 80 comment | 22 complexity | 0fe0e3a2e37a689d4de7eb9cb1b5a20c MD5 | raw file
Possible License(s): LGPL-2.1
1<?php 2/** 3 * @package Joomla.Platform 4 * @subpackage Cache 5 * 6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 7 * @license GNU General Public License version 2 or later; see LICENSE 8 */ 9 10defined('JPATH_PLATFORM') or die; 11 12/** 13 * Joomla! Cache page type object 14 * 15 * @package Joomla.Platform 16 * @subpackage Cache 17 * @since 11.1 18 */ 19class JCacheControllerPage extends JCacheController 20{ 21 /** 22 * @var integer ID property for the cache page object. 23 * @since 11.1 24 */ 25 protected $_id; 26 27 /** 28 * @var string Cache group 29 * @since 11.1 30 */ 31 protected $_group; 32 33 /** 34 * @var object Cache lock test 35 * @since 11.1 36 */ 37 protected $_locktest = null; 38 39 /** 40 * Get the cached page data 41 * 42 * @param string $id The cache data id 43 * @param string $group The cache data group 44 * @param boolean $wrkarounds True to use wrkarounds 45 * 46 * @return boolean True if the cache is hit (false else) 47 * 48 * @since 11.1 49 */ 50 public function get($id = false, $group = 'page', $wrkarounds = true) 51 { 52 // If an id is not given, generate it from the request 53 if ($id == false) 54 { 55 $id = $this->_makeId(); 56 } 57 58 // If the etag matches the page id ... set a no change header and exit : utilize browser cache 59 if (!headers_sent() && isset($_SERVER['HTTP_IF_NONE_MATCH'])) 60 { 61 $etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']); 62 if ($etag == $id) 63 { 64 $browserCache = isset($this->options['browsercache']) ? $this->options['browsercache'] : false; 65 if ($browserCache) 66 { 67 $this->_noChange(); 68 } 69 } 70 } 71 72 // We got a cache hit... set the etag header and echo the page data 73 $data = $this->cache->get($id, $group); 74 75 $this->_locktest = new stdClass; 76 $this->_locktest->locked = null; 77 $this->_locktest->locklooped = null; 78 79 if ($data === false) 80 { 81 $this->_locktest = $this->cache->lock($id, $group); 82 if ($this->_locktest->locked == true && $this->_locktest->locklooped == true) 83 { 84 $data = $this->cache->get($id, $group); 85 } 86 } 87 88 if ($data !== false) 89 { 90 $data = unserialize(trim($data)); 91 if ($wrkarounds === true) 92 { 93 $data = JCache::getWorkarounds($data); 94 } 95 96 $this->_setEtag($id); 97 if ($this->_locktest->locked == true) 98 { 99 $this->cache->unlock($id, $group); 100 } 101 return $data; 102 } 103 104 // Set id and group placeholders 105 $this->_id = $id; 106 $this->_group = $group; 107 return false; 108 } 109 110 /** 111 * Stop the cache buffer and store the cached data 112 * 113 * @param boolean $wrkarounds True to use wrkarounds 114 * 115 * @return boolean True if cache stored 116 * 117 * @since 11.1 118 */ 119 public function store($wrkarounds = true) 120 { 121 // Get page data from JResponse body 122 $data = JResponse::getBody(); 123 124 // Get id and group and reset the placeholders 125 $id = $this->_id; 126 $group = $this->_group; 127 $this->_id = null; 128 $this->_group = null; 129 130 // Only attempt to store if page data exists 131 if ($data) 132 { 133 $data = $wrkarounds == false ? $data : JCache::setWorkarounds($data); 134 135 if ($this->_locktest->locked == false) 136 { 137 $this->_locktest = $this->cache->lock($id, $group); 138 } 139 140 $sucess = $this->cache->store(serialize($data), $id, $group); 141 142 if ($this->_locktest->locked == true) 143 { 144 $this->cache->unlock($id, $group); 145 } 146 147 return $sucess; 148 } 149 return false; 150 } 151 152 /** 153 * Generate a page cache id 154 * 155 * @return string MD5 Hash : page cache id 156 * 157 * @since 11.1 158 * @todo Discuss whether this should be coupled to a data hash or a request 159 * hash ... perhaps hashed with a serialized request 160 */ 161 protected function _makeId() 162 { 163 return JCache::makeId(); 164 } 165 166 /** 167 * There is no change in page data so send an 168 * unmodified header and die gracefully 169 * 170 * @return void 171 * 172 * @since 11.1 173 */ 174 protected function _noChange() 175 { 176 $app = JFactory::getApplication(); 177 178 // Send not modified header and exit gracefully 179 header('HTTP/1.x 304 Not Modified', true); 180 $app->close(); 181 } 182 183 /** 184 * Set the ETag header in the response 185 * 186 * @param string $etag The entity tag (etag) to set 187 * 188 * @return void 189 * 190 * @since 11.1 191 */ 192 protected function _setEtag($etag) 193 { 194 JResponse::setHeader('ETag', $etag, true); 195 } 196}