Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
page.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Cache
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('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  */
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  *
45  * @return boolean True if the cache is hit (false else)
46  *
47  * @since 11.1
48  */
49  public function get($id = false, $group = 'page')
50  {
51  // If an id is not given, generate it from the request
52  if ($id == false)
53  {
54  $id = $this->_makeId();
55  }
56 
57  // If the etag matches the page id ... set a no change header and exit : utilize browser cache
58  if (!headers_sent() && isset($_SERVER['HTTP_IF_NONE_MATCH']))
59  {
60  $etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
61  if ($etag == $id)
62  {
63  $browserCache = isset($this->options['browsercache']) ? $this->options['browsercache'] : false;
64  if ($browserCache)
65  {
66  $this->_noChange();
67  }
68  }
69  }
70 
71  // We got a cache hit... set the etag header and echo the page data
72  $data = $this->cache->get($id, $group);
73 
74  $this->_locktest = new stdClass;
75  $this->_locktest->locked = null;
76  $this->_locktest->locklooped = null;
77 
78  if ($data === false)
79  {
80  $this->_locktest = $this->cache->lock($id, $group);
81  if ($this->_locktest->locked == true && $this->_locktest->locklooped == true)
82  {
83  $data = $this->cache->get($id, $group);
84  }
85  }
86 
87  if ($data !== false)
88  {
89  $data = unserialize(trim($data));
90 
91  $data = JCache::getWorkarounds($data);
92 
93  $this->_setEtag($id);
94  if ($this->_locktest->locked == true)
95  {
96  $this->cache->unlock($id, $group);
97  }
98  return $data;
99  }
100 
101  // Set id and group placeholders
102  $this->_id = $id;
103  $this->_group = $group;
104 
105  return false;
106  }
107 
108  /**
109  * Stop the cache buffer and store the cached data
110  *
111  * @param mixed $data The data to store
112  * @param string $id The cache data id
113  * @param string $group The cache data group
114  * @param boolean $wrkarounds True to use wrkarounds
115  *
116  * @return boolean True if cache stored
117  *
118  * @since 11.1
119  */
120  public function store($data, $id, $group = null, $wrkarounds = true)
121  {
122  // Get page data from the application object
123  if (empty($data))
124  {
125  $data = JFactory::getApplication()->getBody();
126  }
127 
128  // Get id and group and reset the placeholders
129  if (empty($id))
130  {
131  $id = $this->_id;
132  }
133  if (empty($group))
134  {
135  $group = $this->_group;
136  }
137 
138  // Only attempt to store if page data exists
139  if ($data)
140  {
141  if ($wrkarounds) {
142  $data = JCache::setWorkarounds($data, array(
143  'nopathway' => 1,
144  'nohead' => 1,
145  'nomodules' => 1,
146  'headers' => true
147  ));
148  }
149 
150  if ($this->_locktest->locked == false)
151  {
152  $this->_locktest = $this->cache->lock($id, $group);
153  }
154 
155  $sucess = $this->cache->store(serialize($data), $id, $group);
156 
157  if ($this->_locktest->locked == true)
158  {
159  $this->cache->unlock($id, $group);
160  }
161 
162  return $sucess;
163  }
164  return false;
165  }
166 
167  /**
168  * Generate a page cache id
169  *
170  * @return string MD5 Hash : page cache id
171  *
172  * @since 11.1
173  * @todo Discuss whether this should be coupled to a data hash or a request
174  * hash ... perhaps hashed with a serialized request
175  */
176  protected function _makeId()
177  {
178  return JCache::makeId();
179  }
180 
181  /**
182  * There is no change in page data so send an
183  * unmodified header and die gracefully
184  *
185  * @return void
186  *
187  * @since 11.1
188  */
189  protected function _noChange()
190  {
191  $app = JFactory::getApplication();
192 
193  // Send not modified header and exit gracefully
194  header('HTTP/1.x 304 Not Modified', true);
195  $app->close();
196  }
197 
198  /**
199  * Set the ETag header in the response
200  *
201  * @param string $etag The entity tag (etag) to set
202  *
203  * @return void
204  *
205  * @since 11.1
206  */
207  protected function _setEtag($etag)
208  {
209  JFactory::getApplication()->setHeader('ETag', $etag, true);
210  }
211 }