Source for file page.php

Documentation is available at page.php

  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.  */
  19. {
  20.     /**
  21.      * @var    integer  ID property for the cache page object.
  22.      * @since  11.1
  23.      */
  24.     protected $_id;
  25.  
  26.     /**
  27.      * @var    string  Cache group
  28.      * @since  11.1
  29.      */
  30.     protected $_group;
  31.  
  32.     /**
  33.      * @var    object  Cache lock test
  34.      * @since  11.1
  35.      */
  36.     protected $_locktest = null;
  37.  
  38.     /**
  39.      * Get the cached page data
  40.      *
  41.      * @param   string   $id          The cache data id
  42.      * @param   string   $group       The cache data group
  43.      *
  44.      * @return  boolean  True if the cache is hit (false else)
  45.      *
  46.      * @since   11.1
  47.      */
  48.     public function get($id false$group 'page')
  49.     {
  50.         // If an id is not given, generate it from the request
  51.         if ($id == false)
  52.         {
  53.             $id $this->_makeId();
  54.         }
  55.  
  56.         // If the etag matches the page id ... set a no change header and exit : utilize browser cache
  57.         if (!headers_sent(&& isset($_SERVER['HTTP_IF_NONE_MATCH']))
  58.         {
  59.             $etag stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
  60.             if ($etag == $id)
  61.             {
  62.                 $browserCache = isset($this->options['browsercache']$this->options['browsercache'false;
  63.                 if ($browserCache)
  64.                 {
  65.                     $this->_noChange();
  66.                 }
  67.             }
  68.         }
  69.  
  70.         // We got a cache hit... set the etag header and echo the page data
  71.         $data $this->cache->get($id$group);
  72.  
  73.         $this->_locktest = new stdClass;
  74.         $this->_locktest->locked null;
  75.         $this->_locktest->locklooped null;
  76.  
  77.         if ($data === false)
  78.         {
  79.             $this->_locktest = $this->cache->lock($id$group);
  80.             if ($this->_locktest->locked == true && $this->_locktest->locklooped == true)
  81.             {
  82.                 $data $this->cache->get($id$group);
  83.             }
  84.         }
  85.  
  86.         if ($data !== false)
  87.         {
  88.             $data unserialize(trim($data));
  89.  
  90.             $data JCache::getWorkarounds($data);
  91.  
  92.             $this->_setEtag($id);
  93.             if ($this->_locktest->locked == true)
  94.             {
  95.                 $this->cache->unlock($id$group);
  96.             }
  97.             return $data;
  98.         }
  99.  
  100.         // Set id and group placeholders
  101.         $this->_id         = $id;
  102.         $this->_group     = $group;
  103.  
  104.         return false;
  105.     }
  106.  
  107.     /**
  108.      * Stop the cache buffer and store the cached data
  109.      *
  110.      * @param   mixed    $data        The data to store
  111.      * @param   string   $id          The cache data id
  112.      * @param   string   $group       The cache data group
  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($data$id$group null$wrkarounds true)
  120.     {
  121.         // Get page data from the application object
  122.         if (empty($data))
  123.         {
  124.             $data JFactory::getApplication()->getBody();
  125.         }
  126.  
  127.         // Get id and group and reset the placeholders
  128.         if (empty($id))
  129.         {
  130.             $id $this->_id;
  131.         }
  132.         if (empty($group))
  133.         {
  134.             $group $this->_group;
  135.         }
  136.  
  137.         // Only attempt to store if page data exists
  138.         if ($data)
  139.         {
  140.             if ($wrkarounds{
  141.                 $data JCache::setWorkarounds($dataarray(
  142.                     'nopathway' => 1,
  143.                     'nohead'     => 1,
  144.                     'nomodules' => 1,
  145.                     'headers'     => true
  146.                 ));
  147.             }
  148.  
  149.             if ($this->_locktest->locked == false)
  150.             {
  151.                 $this->_locktest = $this->cache->lock($id$group);
  152.             }
  153.  
  154.             $sucess $this->cache->store(serialize($data)$id$group);
  155.  
  156.             if ($this->_locktest->locked == true)
  157.             {
  158.                 $this->cache->unlock($id$group);
  159.             }
  160.  
  161.             return $sucess;
  162.         }
  163.         return false;
  164.     }
  165.  
  166.     /**
  167.      * Generate a page cache id
  168.      *
  169.      * @return  string  MD5 Hash : page cache id
  170.      *
  171.      * @since   11.1
  172.      * @todo    Discuss whether this should be coupled to a data hash or a request
  173.      *  hash ... perhaps hashed with a serialized request
  174.      */
  175.     protected function _makeId()
  176.     {
  177.         return JCache::makeId();
  178.     }
  179.  
  180.     /**
  181.      * There is no change in page data so send an
  182.      * unmodified header and die gracefully
  183.      *
  184.      * @return  void 
  185.      *
  186.      * @since   11.1
  187.      */
  188.     protected function _noChange()
  189.     {
  190.         $app JFactory::getApplication();
  191.  
  192.         // Send not modified header and exit gracefully
  193.         header('HTTP/1.x 304 Not Modified'true);
  194.         $app->close();
  195.     }
  196.  
  197.     /**
  198.      * Set the ETag header in the response
  199.      *
  200.      * @param   string  $etag  The entity tag (etag) to set
  201.      *
  202.      * @return  void 
  203.      *
  204.      * @since   11.1
  205.      */
  206.     protected function _setEtag($etag)
  207.     {
  208.         JFactory::getApplication()->setHeader('ETag'$etagtrue);
  209.     }
  210. }

Documentation generated on Tue, 19 Nov 2013 15:10:03 +0100 by phpDocumentor 1.4.3