Translation2
[ class tree: Translation2 ] [ index: Translation2 ] [ all elements ]

Source for file CacheLiteFunction.php

Documentation is available at CacheLiteFunction.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Decorator_CacheLiteFunction class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category  Internationalization
  31.  * @package   Translation2
  32.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  33.  * @copyright 2004-2007 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: CacheLiteFunction.php,v 1.22 2007/12/16 20:27:07 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * Load Translation2 decorator base class
  41.  * and Cache_Lite_Function class
  42.  */
  43. require_once 'Translation2/Decorator.php';
  44. require_once 'Cache/Lite/Function.php';
  45.  
  46. /**
  47.  * Decorator to cache fetched data using the Cache_Lite_Function class.
  48.  *
  49.  * @category  Internationalization
  50.  * @package   Translation2
  51.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  52.  * @copyright 2004-2007 Lorenzo Alberton
  53.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  54.  * @version   CVS: $Id: CacheLiteFunction.php,v 1.22 2007/12/16 20:27:07 quipo Exp $
  55.  * @link      http://pear.php.net/package/Translation2
  56.  */
  57. {
  58.     // {{{ class vars
  59.  
  60.     /**
  61.      * Cache_Lite_Function object
  62.      * @var object 
  63.      */
  64.     var $cacheLiteFunction = null;
  65.  
  66.     /**
  67.      * @var int (default 1)
  68.      * @access private
  69.      */
  70.     var $tempVarNameGenerator = 1;
  71.  
  72.     /**
  73.      * @var string 
  74.      * @access private
  75.      */
  76.     var $tempVarName = null;
  77.  
  78.     /**
  79.      * Cache lifetime (in seconds)
  80.      * @var int $lifeTime 
  81.      * @access private
  82.      */
  83.     var $lifeTime = 3600;
  84.  
  85.     /**
  86.      * Directory where to put the cache files
  87.      * (make sure to add a trailing slash)
  88.      * @var string $cacheDir 
  89.      * @access private
  90.      */
  91.     var $cacheDir '/tmp/';
  92.  
  93.     /**
  94.      * Enable / disable fileLocking. Can avoid cache corruption under bad
  95.      * circumstances.
  96.      * @var string $cacheDir 
  97.      * @access private
  98.      */
  99.     var $fileLocking = true;
  100.  
  101.     /**
  102.      * Enable / disable caching
  103.      * (can be very useful to debug cached scripts)
  104.      * @var boolean $caching 
  105.      */
  106.     var $caching = true;
  107.  
  108.     /**
  109.      * Frequency of cache cleaning.
  110.      * Higher values mean lower cleaning probability.
  111.      * Set 0 to disable. Set 1 to clean at every request.
  112.      * @var boolean $caching 
  113.      */
  114.     var $cleaningFrequency = 0;
  115.  
  116.     /**
  117.      * Name of default cache group.
  118.      * @var    string    $defaultGroup 
  119.      */
  120.      var $defaultGroup = 'Translation2';
  121.  
  122.     // }}}
  123.     // {{{ _prepare()
  124.  
  125.     /**
  126.      * Istanciate a new Cache_Lite_Function object
  127.      * and get the name for an unused global variable,
  128.      * needed by Cache_Lite_Function
  129.      *
  130.      * @return void 
  131.      * @access private
  132.      */
  133.     function _prepare()
  134.     {
  135.         if (is_null($this->cacheLiteFunction)) {
  136.             $cache_options = array(
  137.                 'caching'      => $this->caching,
  138.                 'cacheDir'     => $this->cacheDir,
  139.                 'lifeTime'     => $this->lifeTime,
  140.                 'fileLocking'  => $this->fileLocking,
  141.                 'defaultGroup' => $this->defaultGroup,
  142.  
  143.             );
  144.             $this->cacheLiteFunction = new Cache_Lite_Function($cache_options);
  145.         }
  146.  
  147.         $this->_cleanCache();
  148.     }
  149.  
  150.     // }}}
  151.     // {{{ setLang()
  152.  
  153.     /**
  154.      * Set default lang
  155.      *
  156.      * Set the language that shall be used when retrieving strings.
  157.      *
  158.      * @param string $langID language code (for instance, 'en' or 'it')
  159.      *
  160.      * @return void 
  161.      */
  162.     function setLang($langID)
  163.     {
  164.         // WITHOUT THIS, IT DOESN'T WORK
  165.         global $translation2_storage_cachelitefunction_temp;
  166.         //generate temp variable
  167.         $translation2_storage_cachelitefunction_temp $this->translation2->storage;
  168.  
  169.         $this->_prepare();
  170.         $res $this->cacheLiteFunction->call(
  171.             'translation2_storage_cachelitefunction_temp->setLang'$langID);
  172.         if (PEAR::isError($res)) {
  173.             return $res;
  174.         }
  175.         $this->translation2->lang = $res;
  176.  
  177.     }
  178.  
  179.     // }}}
  180.     // {{{ setCacheOption()
  181.  
  182.     /**
  183.      * Set a Cache_Lite option
  184.      *
  185.      * Passes a Cache_Lite option forward to the Cache_Lite object
  186.      * See Cache_Lite constructor for available options
  187.      *
  188.      * @param string $name  name of the option
  189.      * @param string $value new value of the option
  190.      *
  191.      * @return void 
  192.      * @access public
  193.      * @see Cache_Lite::setOption()
  194.      */
  195.     function setCacheOption($name$value)
  196.     {
  197.         $this->_prepare();
  198.         $this->cacheLiteFunction->setOption($name$value);
  199.     }
  200.  
  201.     // }}}
  202.     // {{{ getLang()
  203.  
  204.     /**
  205.      * get lang info
  206.      *
  207.      * Get some extra information about the language (its full name,
  208.      * the localized error text, ...)
  209.      *
  210.      * @param string $langID language ID
  211.      * @param string $format ['name', 'meta', 'error_text', 'array']
  212.      *
  213.      * @return mixed [string | array], depending on $format
  214.      */
  215.     function getLang($langID = null$format 'name')
  216.     {
  217.         $langs $this->getLangs('array');
  218.  
  219.         if (is_null($langID)) {
  220.             if (!isset($this->lang['id']|| !array_key_exists($this->lang['id']$langs)) {
  221.                 $msg 'Translation2::getLang(): unknown language "'.$langID.'".'
  222.                       .' Use Translation2::setLang() to set a default language.';
  223.                 return $this->storage->raiseError($msgTRANSLATION2_ERROR_UNKNOWN_LANG);
  224.             }
  225.             $langID $this->lang['id'];
  226.         }
  227.  
  228.         if ($format == 'array'{
  229.             return $langs[$langID];
  230.         elseif (isset($langs[$langID][$format])) {
  231.             return $langs[$langID][$format];
  232.         elseif (isset($langs[$langID]['name'])) {
  233.             return $langs[$langID]['name'];
  234.         }
  235.         $msg 'Translation2::getLang(): unknown language "'.$langID.'".'
  236.               .' Use Translation2::setLang() to set a default language.';
  237.         return $this->storage->raiseError($msgTRANSLATION2_ERROR_UNKNOWN_LANG);
  238.     }
  239.  
  240.     // }}}
  241.     // {{{ getLangs()
  242.  
  243.     /**
  244.      * get langs
  245.      *
  246.      * Get some extra information about the languages (their full names,
  247.      * the localized error text, their codes, ...)
  248.      *
  249.      * @param string $format ['ids', 'names', 'array']
  250.      *
  251.      * @return array 
  252.      */
  253.     function getLangs($format 'name')
  254.     {
  255.         // WITHOUT THIS, IT DOESN'T WORK
  256.         global $translation2_cachelitefunction_temp;
  257.         //generate temp variable
  258.         $translation2_cachelitefunction_temp $this->translation2;
  259.  
  260.         $this->_prepare();
  261.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getLangs',
  262.             $format);
  263.     }
  264.  
  265.     // }}}
  266.     // {{{ getRaw()
  267.  
  268.     /**
  269.      * Get translated string (as-is)
  270.      *
  271.      * First check if the string is cached, if not => fetch the page
  272.      * from the container and cache it for later use.
  273.      *
  274.      * @param string $stringID    string ID
  275.      * @param string $pageID      page/group ID
  276.      * @param string $langID      language ID
  277.      * @param string $defaultText Text to display when the strings in both
  278.      *                             the default and the fallback lang are empty
  279.      *
  280.      * @return string 
  281.      */
  282.     function getRaw($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText '')
  283.     {
  284.         // WITHOUT THIS, IT DOESN'T WORK
  285.         global $translation2_cachelitefunction_temp;
  286.         //generate temp variable
  287.         $translation2_cachelitefunction_temp $this->translation2;
  288.  
  289.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  290.             $pageID $this->translation2->currentPageID;
  291.         }
  292.         $langID = empty($langID$this->translation2->lang['id'$langID;
  293.  
  294.         $this->_prepare();
  295.  
  296.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getRaw',
  297.             $stringID$pageID$langID$defaultText);
  298.     }
  299.  
  300.     // }}}
  301.     // {{{ get()
  302.  
  303.     /**
  304.      * Get translated string
  305.      *
  306.      * First check if the string is cached, if not => fetch the page
  307.      * from the container and cache it for later use.
  308.      *
  309.      * @param string $stringID    string ID
  310.      * @param string $pageID      page/group ID
  311.      * @param string $langID      language ID
  312.      * @param string $defaultText Text to display when the strings in both
  313.      *                             the default and the fallback lang are empty
  314.      *
  315.      * @return string 
  316.      */
  317.     function get($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText '')
  318.     {
  319.         // WITHOUT THIS, IT DOESN'T WORK
  320.         global $translation2_cachelitefunction_temp;
  321.         //generate temp variable
  322.         $translation2_cachelitefunction_temp $this->translation2->storage;
  323.  
  324.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  325.             $pageID $this->translation2->currentPageID;
  326.         }
  327.         $langID = empty($langID$this->translation2->lang['id'$langID;
  328.  
  329.         $this->_prepare();
  330.  
  331.         $string $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getOne',
  332.             $stringID$pageID$langID);
  333.         if (empty($string)) {
  334.             return $defaultText;
  335.         }
  336.         return $this->translation2->_replaceParams($string);
  337.     }
  338.  
  339.     // }}}
  340.     // {{{ getRawPage()
  341.  
  342.     /**
  343.      * Get the array of strings in a page
  344.      *
  345.      * First check if the strings are cached, if not => fetch the page
  346.      * from the container and cache it for later use.
  347.      *
  348.      * @param string $pageID page/group ID
  349.      * @param string $langID language ID
  350.      *
  351.      * @return array 
  352.      */
  353.     function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  354.     {
  355.         // WITHOUT THIS, IT DOESN'T WORK
  356.         global $translation2_cachelitefunction_temp;
  357.         //generate temp variable
  358.         $translation2_cachelitefunction_temp $this->translation2;
  359.  
  360.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  361.             $pageID $this->translation2->currentPageID;
  362.         }
  363.         $langID = empty($langID$this->translation2->lang['id'$langID;
  364.  
  365.         $this->_prepare();
  366.  
  367.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getRawPage',
  368.             $pageID$langID);
  369.     }
  370.  
  371.     // }}}
  372.     // {{{ getPage()
  373.  
  374.     /**
  375.      * Same as getRawPage, but resort to fallback language and
  376.      * replace parameters when needed
  377.      *
  378.      * @param string $pageID page/group ID
  379.      * @param string $langID language ID
  380.      *
  381.      * @return array 
  382.      */
  383.     function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  384.     {
  385.         // WITHOUT THIS, IT DOESN'T WORK
  386.         global $translation2_cachelitefunction_temp;
  387.         //generate temp variable
  388.         $translation2_cachelitefunction_temp $this->translation2;
  389.  
  390.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  391.             $pageID $this->translation2->currentPageID;
  392.         }
  393.         $langID = empty($langID$this->translation2->lang['id'$langID;
  394.  
  395.         $this->_prepare();
  396.  
  397.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getPage',
  398.             $pageID$langID);
  399.     }
  400.  
  401.     // }}}
  402.     // {{{ getStringID()
  403.  
  404.     /**
  405.      * Get translated string
  406.      *
  407.      * @param string $string This is NOT the stringID, this is a real string.
  408.      *                        The method will search for its matching stringID,
  409.      *                        and then it will return the associate string in the
  410.      *                        selected language.
  411.      * @param string $pageID page/group ID
  412.      *
  413.      * @return string 
  414.      */
  415.     function getStringID($string$pageID=TRANSLATION2_DEFAULT_PAGEID)
  416.     {
  417.         // WITHOUT THIS, IT DOESN'T WORK
  418.         global $translation2_cachelitefunction_temp;
  419.         //generate temp variable
  420.         $translation2_cachelitefunction_temp $this->translation2;
  421.  
  422.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  423.             $pageID $this->translation2->currentPageID;
  424.         }
  425.         $this->_prepare();
  426.  
  427.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getStringID',
  428.             $string$pageID);
  429.     }
  430.  
  431.     // }}}
  432.     // {{{ _cleanCache()
  433.  
  434.     /**
  435.      * Statistically purge the cache
  436.      *
  437.      * @return void 
  438.      */
  439.     function _cleanCache()
  440.     {
  441.         if ($this->cleaningFrequency > 0{
  442.             if (mt_rand(1$this->cleaningFrequency== 1{
  443.                 $this->cacheLiteFunction->clean($this->defaultGroup);
  444.             }
  445.         }
  446.     }
  447.  
  448.     // }}}
  449. }
  450. ?>

Documentation generated on Tue, 06 May 2008 06:00:14 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.