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.23 2008/11/14 16:18:50 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.23 2008/11/14 16:18:50 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 self 
  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.         return $this;
  200.     }
  201.  
  202.     // }}}
  203.     // {{{ getLang()
  204.  
  205.     /**
  206.      * get lang info
  207.      *
  208.      * Get some extra information about the language (its full name,
  209.      * the localized error text, ...)
  210.      *
  211.      * @param string $langID language ID
  212.      * @param string $format ['name', 'meta', 'error_text', 'array']
  213.      *
  214.      * @return mixed [string | array], depending on $format
  215.      */
  216.     function getLang($langID = null$format 'name')
  217.     {
  218.         $langs $this->getLangs('array');
  219.  
  220.         if (is_null($langID)) {
  221.             if (!isset($this->lang['id']|| !array_key_exists($this->lang['id']$langs)) {
  222.                 $msg 'Translation2::getLang(): unknown language "'.$langID.'".'
  223.                       .' Use Translation2::setLang() to set a default language.';
  224.                 return $this->storage->raiseError($msgTRANSLATION2_ERROR_UNKNOWN_LANG);
  225.             }
  226.             $langID $this->lang['id'];
  227.         }
  228.  
  229.         if ($format == 'array'{
  230.             return $langs[$langID];
  231.         elseif (isset($langs[$langID][$format])) {
  232.             return $langs[$langID][$format];
  233.         elseif (isset($langs[$langID]['name'])) {
  234.             return $langs[$langID]['name'];
  235.         }
  236.         $msg 'Translation2::getLang(): unknown language "'.$langID.'".'
  237.               .' Use Translation2::setLang() to set a default language.';
  238.         return $this->storage->raiseError($msgTRANSLATION2_ERROR_UNKNOWN_LANG);
  239.     }
  240.  
  241.     // }}}
  242.     // {{{ getLangs()
  243.  
  244.     /**
  245.      * get langs
  246.      *
  247.      * Get some extra information about the languages (their full names,
  248.      * the localized error text, their codes, ...)
  249.      *
  250.      * @param string $format ['ids', 'names', 'array']
  251.      *
  252.      * @return array 
  253.      */
  254.     function getLangs($format 'name')
  255.     {
  256.         // WITHOUT THIS, IT DOESN'T WORK
  257.         global $translation2_cachelitefunction_temp;
  258.         //generate temp variable
  259.         $translation2_cachelitefunction_temp $this->translation2;
  260.  
  261.         $this->_prepare();
  262.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getLangs',
  263.             $format);
  264.     }
  265.  
  266.     // }}}
  267.     // {{{ getRaw()
  268.  
  269.     /**
  270.      * Get translated string (as-is)
  271.      *
  272.      * First check if the string is cached, if not => fetch the page
  273.      * from the container and cache it for later use.
  274.      *
  275.      * @param string $stringID    string ID
  276.      * @param string $pageID      page/group ID
  277.      * @param string $langID      language ID
  278.      * @param string $defaultText Text to display when the strings in both
  279.      *                             the default and the fallback lang are empty
  280.      *
  281.      * @return string 
  282.      */
  283.     function getRaw($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText '')
  284.     {
  285.         // WITHOUT THIS, IT DOESN'T WORK
  286.         global $translation2_cachelitefunction_temp;
  287.         //generate temp variable
  288.         $translation2_cachelitefunction_temp $this->translation2;
  289.  
  290.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  291.             $pageID $this->translation2->currentPageID;
  292.         }
  293.         $langID = empty($langID$this->translation2->lang['id'$langID;
  294.  
  295.         $this->_prepare();
  296.  
  297.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getRaw',
  298.             $stringID$pageID$langID$defaultText);
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ get()
  303.  
  304.     /**
  305.      * Get translated string
  306.      *
  307.      * First check if the string is cached, if not => fetch the page
  308.      * from the container and cache it for later use.
  309.      *
  310.      * @param string $stringID    string ID
  311.      * @param string $pageID      page/group ID
  312.      * @param string $langID      language ID
  313.      * @param string $defaultText Text to display when the strings in both
  314.      *                             the default and the fallback lang are empty
  315.      *
  316.      * @return string 
  317.      */
  318.     function get($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText '')
  319.     {
  320.         // WITHOUT THIS, IT DOESN'T WORK
  321.         global $translation2_cachelitefunction_temp;
  322.         //generate temp variable
  323.         $translation2_cachelitefunction_temp $this->translation2->storage;
  324.  
  325.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  326.             $pageID $this->translation2->currentPageID;
  327.         }
  328.         $langID = empty($langID$this->translation2->lang['id'$langID;
  329.  
  330.         $this->_prepare();
  331.  
  332.         $string $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getOne',
  333.             $stringID$pageID$langID);
  334.         if (empty($string)) {
  335.             return $defaultText;
  336.         }
  337.         return $this->translation2->_replaceParams($string);
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ getRawPage()
  342.  
  343.     /**
  344.      * Get the array of strings in a page
  345.      *
  346.      * First check if the strings are cached, if not => fetch the page
  347.      * from the container and cache it for later use.
  348.      *
  349.      * @param string $pageID page/group ID
  350.      * @param string $langID language ID
  351.      *
  352.      * @return array 
  353.      */
  354.     function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  355.     {
  356.         // WITHOUT THIS, IT DOESN'T WORK
  357.         global $translation2_cachelitefunction_temp;
  358.         //generate temp variable
  359.         $translation2_cachelitefunction_temp $this->translation2;
  360.  
  361.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  362.             $pageID $this->translation2->currentPageID;
  363.         }
  364.         $langID = empty($langID$this->translation2->lang['id'$langID;
  365.  
  366.         $this->_prepare();
  367.  
  368.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getRawPage',
  369.             $pageID$langID);
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ getPage()
  374.  
  375.     /**
  376.      * Same as getRawPage, but resort to fallback language and
  377.      * replace parameters when needed
  378.      *
  379.      * @param string $pageID page/group ID
  380.      * @param string $langID language ID
  381.      *
  382.      * @return array 
  383.      */
  384.     function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  385.     {
  386.         // WITHOUT THIS, IT DOESN'T WORK
  387.         global $translation2_cachelitefunction_temp;
  388.         //generate temp variable
  389.         $translation2_cachelitefunction_temp $this->translation2;
  390.  
  391.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  392.             $pageID $this->translation2->currentPageID;
  393.         }
  394.         $langID = empty($langID$this->translation2->lang['id'$langID;
  395.  
  396.         $this->_prepare();
  397.  
  398.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getPage',
  399.             $pageID$langID);
  400.     }
  401.  
  402.     // }}}
  403.     // {{{ getStringID()
  404.  
  405.     /**
  406.      * Get translated string
  407.      *
  408.      * @param string $string This is NOT the stringID, this is a real string.
  409.      *                        The method will search for its matching stringID,
  410.      *                        and then it will return the associate string in the
  411.      *                        selected language.
  412.      * @param string $pageID page/group ID
  413.      *
  414.      * @return string 
  415.      */
  416.     function getStringID($string$pageID=TRANSLATION2_DEFAULT_PAGEID)
  417.     {
  418.         // WITHOUT THIS, IT DOESN'T WORK
  419.         global $translation2_cachelitefunction_temp;
  420.         //generate temp variable
  421.         $translation2_cachelitefunction_temp $this->translation2;
  422.  
  423.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  424.             $pageID $this->translation2->currentPageID;
  425.         }
  426.         $this->_prepare();
  427.  
  428.         return $this->cacheLiteFunction->call('translation2_cachelitefunction_temp->getStringID',
  429.             $string$pageID);
  430.     }
  431.  
  432.     // }}}
  433.     // {{{ _cleanCache()
  434.  
  435.     /**
  436.      * Statistically purge the cache
  437.      *
  438.      * @return void 
  439.      */
  440.     function _cleanCache()
  441.     {
  442.         if ($this->cleaningFrequency > 0{
  443.             if (mt_rand(1$this->cleaningFrequency== 1{
  444.                 $this->cacheLiteFunction->clean($this->defaultGroup);
  445.             }
  446.         }
  447.     }
  448.  
  449.     // }}}
  450. }
  451. ?>

Documentation generated on Fri, 14 Nov 2008 11:30:11 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.