b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (Version 1.9) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _dataobjectcache.class.php

Documentation is available at _dataobjectcache.class.php

  1. <?php
  2. /**
  3.  * This file implements the DataObjectCache class.
  4.  *
  5.  * This file is part of the evoCore framework - {@link http://evocore.net/}
  6.  * See also {@link http://sourceforge.net/projects/evocms/}.
  7.  *
  8.  * @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}
  9.  *  Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10.  *  Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.
  11.  *
  12.  *  {@internal License choice
  13.  *  - If you have received this file as part of a package, please find the license.txt file in
  14.  *    the same folder or the closest folder above for complete license terms.
  15.  *  - If you have received this file individually (e-g: from http://cvs.sourceforge.net/viewcvs.py/evocms/)
  16.  *    then you must choose one of the following licenses before using the file:
  17.  *    - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  18.  *    - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  19.  *  }}}
  20.  *
  21.  *  {@internal Open Source relicensing agreement:
  22.  *  Daniel HAHLER grants Francois PLANQUE the right to license
  23.  *  Daniel HAHLER's contributions to this file and the b2evolution project
  24.  *  under any OSI approved OSS license (http://www.opensource.org/licenses/).
  25.  *
  26.  *  PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license
  27.  *  PROGIDISTRI S.A.S.'s contributions to this file and the b2evolution project
  28.  *  under any OSI approved OSS license (http://www.opensource.org/licenses/).
  29.  *  }}}
  30.  *
  31.  * @package evocore
  32.  *
  33.  *  {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  34.  * @author blueyed: Daniel HAHLER.
  35.  * @author fplanque: Francois PLANQUE
  36.  *
  37.  * @version $Id: _dataobjectcache.class.php,v 1.6.2.2 2006/11/04 19:54:53 fplanque Exp $
  38.  */
  39. if!defined('EVO_MAIN_INIT') ) die'Please, do not access this page directly.' );
  40.  
  41. /**
  42.  * Data Object Cache Class
  43.  *
  44.  * @package evocore
  45.  * @version beta
  46.  */
  47. {
  48.     var $dbtablename;
  49.     var $dbprefix;
  50.     var $dbIDname;
  51.  
  52.     /**
  53.      * Class name of objects in this cache:
  54.      */
  55.     var $objtype;
  56.  
  57.     /**
  58.      * Object array
  59.      */
  60.     var $cache = array();
  61.  
  62.     /**
  63.      * Copy of previous object array
  64.      * @see DataObjectCache::clear()
  65.      */
  66.     var $shadow_cache = NULL;
  67.  
  68.     var $load_add = false;
  69.     var $all_loaded = false;
  70.     var $name_field;
  71.     var $order_by;
  72.  
  73.  
  74.     /**
  75.      * Constructor
  76.      *
  77.      * @param string Name of DataObject class we are cacheing
  78.      * @param boolean true if it's OK to just load all items!
  79.      * @param string Name of table in database
  80.      * @param string Prefix of fields in the table
  81.      * @param string Name of the ID field (including prefix)
  82.      */
  83.     function DataObjectCache$objtype$load_all$tablename$prefix ''$dbIDname$name_field NULL$order_by '' )
  84.     {
  85.         $this->objtype = $objtype;
  86.         $this->load_all $load_all;
  87.         $this->dbtablename = $tablename;
  88.         $this->dbprefix = $prefix;
  89.         $this->dbIDname = $dbIDname;
  90.         $this->name_field = $name_field;
  91.         
  92.         ifempty$order_by ) )
  93.         {
  94.             ifempty$name_field ) )
  95.             {    
  96.                 $this->order_by = $dbIDname;
  97.             }
  98.             else 
  99.             {
  100.                 $this->order_by = $name_field;
  101.             }
  102.         }
  103.         else 
  104.         
  105.             $this->order_by = $order_by;
  106.         }
  107.     }
  108.  
  109.     
  110.     /**
  111.      *  TODO
  112.      */
  113.     function new_obj$row NULL )
  114.     {
  115.         $objtype $this->objtype;
  116.             
  117.         // Instantiate a custom object
  118.         $obj new $objtype$row )// COPY !!
  119.         
  120.         return $obj;
  121.     }
  122.     
  123.     
  124.     /**
  125.      * Load the cache **extensively**
  126.      */
  127.     function load_all()
  128.     {
  129.         global $DB$Debuglog;
  130.  
  131.         if$this->all_loaded )
  132.         // Already loaded
  133.             return false;
  134.         }
  135.         
  136.         $this->cleartrue );
  137.  
  138.         $Debuglog->addget_class($this).' - Loading <strong>'.$this->objtype.'(ALL)</strong> into cache''dataobjects' );
  139.         $sql 'SELECT * 
  140.                             FROM '.$this->dbtablename.
  141.                          ORDER BY '.$this->order_by;
  142.                             
  143.         $dbIDname $this->dbIDname;
  144.         $objtype $this->objtype;
  145.         foreach$DB->get_results$sql as $row )
  146.         {
  147.              // Instantiate a custom object
  148.                 $this->instantiate$row );
  149.         }
  150.  
  151.         $this->all_loaded = true;
  152.  
  153.         return true;
  154.     }
  155.  
  156.  
  157.     /**
  158.      * Load a list of objects into the cache
  159.      *
  160.      * @param string list of IDs of objects to load
  161.      */
  162.     function load_list$req_list )
  163.     {
  164.         global $DB$Debuglog;
  165.  
  166.         $Debuglog->add"Loading <strong>$this->objtype($req_list)</strong> into cache"'dataobjects' );
  167.  
  168.         ifempty$req_list ) )
  169.         {
  170.             return false;
  171.         }
  172.  
  173.         $sql "SELECT *
  174.                   FROM $this->dbtablename
  175.                  WHERE $this->dbIDname IN ($req_list)";
  176.         $objtype $this->objtype;
  177.         foreach$DB->get_results$sql as $row )
  178.         {
  179.             $this->addnew $objtype$row ) );
  180.             // TODO: use instantiate()
  181.         }
  182.     }
  183.  
  184.  
  185.     /**
  186.      * Get an array of all (loaded) IDs.
  187.      *
  188.      * @return array 
  189.      */
  190.     function get_ID_array()
  191.     {
  192.         $IDs array();
  193.  
  194.         foreach$this->cache as $obj )
  195.         {
  196.             $IDs[$obj->ID;
  197.         }
  198.  
  199.         return $IDs;
  200.     }
  201.  
  202.  
  203.     /**
  204.      * Add a dataobject to the cache
  205.      */
  206.     function add$Obj )
  207.     {
  208.         global $Debuglog;
  209.  
  210.         ifempty($Obj->ID) )
  211.         {
  212.             $Debuglog->add'No object to add!''dataobjects' );
  213.             return false;
  214.         }
  215.  
  216.         ifisset($this->cache[$Obj->ID]) )
  217.         // fplanque: I don't want an extra (and expensive) comparison here. $this->cache[$Obj->ID] === $Obj. If you need this you're probably misusing the cache.
  218.         {
  219.             $Debuglog->add$this->objtype.': Object with ID '.$Obj->ID.' is already cached''dataobjects' );
  220.             return false;
  221.         }
  222.  
  223.         // If the object is valid and not already cached:
  224.         $this->cache[$Obj->ID$Obj;
  225.  
  226.         return true;
  227.     }
  228.  
  229.  
  230.     /**
  231.      * Instantiate a DataObject from a table row and then cache it.
  232.      *
  233.      * @param Object Database row
  234.      * @return Object 
  235.      */
  236.     function instantiate$db_row )
  237.     {
  238.         // Get ID of the object we'ere preparing to instantiate...
  239.         $obj_ID $db_row->{$this->dbIDname};
  240.  
  241.         ifempty($obj_ID) )
  242.         {
  243.             $Obj NULL;
  244.             return $Obj;
  245.         }
  246.  
  247.         ifisset$this->cache[$obj_ID) )
  248.         // Already in cache, do nothing!
  249.         }
  250.         elseifisset$this->shadow_cache[$obj_ID) )
  251.         {    // Already in shadow, recycle object:
  252.             $this->add$this->shadow_cache[$obj_ID);
  253.         }
  254.         else 
  255.         // Not already cached, add new object:
  256.             $this->add$this->new_obj$db_row ) );
  257.         }
  258.         
  259.         return $this->cache[$obj_ID];
  260.     }
  261.  
  262.  
  263.     /**
  264.      * Clear the cache **extensively**
  265.      * 
  266.      */
  267.     function clear$keep_shadow false )
  268.     {
  269.         if$keep_shadow )
  270.         {    // Keep copy of cache in case we try to re instantiate previous object:
  271.             $this->shadow_cache = $this->cache;
  272.         }
  273.         else 
  274.         {
  275.             $this->shadow_cache = NULL;
  276.         }
  277.         
  278.         $this->cache = array();
  279.         $this->all_loaded = false;
  280.     }
  281.  
  282.  
  283.     /**
  284.      * Get an object from cache by ID
  285.      *
  286.      * Load the cache if necessary (all at once if allowed).
  287.      *
  288.      * @param integer ID of object to load
  289.      * @param boolean true if function should die on error
  290.      * @param boolean true if function should die on empty/null
  291.      * @return reference on cached object
  292.      */
  293.     function get_by_ID$req_ID$halt_on_error true$halt_on_empty true )
  294.     {
  295.         global $DB$Debuglog;
  296.  
  297.         ifempty($req_ID) )
  298.         {
  299.             if($halt_on_empty)
  300.             {
  301.                 debug_die"Requested $this->objtype from $this->dbtablename without ID!);
  302.             }
  303.             $r NULL;
  304.             return $r;
  305.         }
  306.  
  307.         if!empty$this->cache$req_ID ) )
  308.         // Already in cache
  309.             // $Debuglog->add( "Accessing $this->objtype($req_ID) from cache", 'dataobjects' );
  310.             return $this->cache$req_ID ];
  311.         }
  312.         elseif!$this->all_loaded )
  313.         // Not in cache, but not everything is loaded yet
  314.             if$this->load_all )
  315.             // It's ok to just load everything:
  316.                 $this->load_all();
  317.             }
  318.             else
  319.             // Load just the requested object:
  320.                 $Debuglog->add"Loading <strong>$this->objtype($req_ID)</strong> into cache"'dataobjects' );
  321.                 // Note: $req_ID MUST be an unsigned integer. This is how DataObject works.
  322.                 $sql "SELECT *
  323.                           FROM $this->dbtablename
  324.                          WHERE $this->dbIDname = $req_ID";
  325.  
  326.                 if$row $DB->get_row$sqlOBJECT0'DataObjectCache::get_by_ID()' ) )
  327.                 {
  328.                     if$this->instantiate$row ) )
  329.                     {
  330.                         $Debuglog->add'Could not add() object to cache!''dataobjects' );
  331.                     }
  332.                 }
  333.                 else
  334.                 {
  335.                     $Debuglog->add'Could not get DataObject by ID. Query: '.$sql'dataobjects' );
  336.                 }
  337.             }
  338.         }
  339.  
  340.         ifempty$this->cache$req_ID ) )
  341.         // Requested object does not exist
  342.             // $Debuglog->add( 'failure', 'dataobjects' );
  343.             if$halt_on_error )
  344.             {
  345.                 debug_die"Requested $this->objtype does not exist!);
  346.             }
  347.             $r false;
  348.             return $r;
  349.         }
  350.  
  351.         return $this->cache$req_ID ];
  352.     }
  353.  
  354.  
  355.     /**
  356.      * Get an object from cache by name
  357.      *
  358.      * Load the cache if necessary (all at once if allowed).
  359.      *
  360.      * @param integer ID of object to load
  361.      * @param boolean true if function should die on error
  362.      * @param boolean true if function should die on empty/null
  363.      * @return reference on cached object
  364.      */
  365.     function get_by_name$req_name$halt_on_error true$halt_on_empty true )
  366.     {
  367.         global $DB$Debuglog;
  368.  
  369.         ifempty$this->name_field ) )
  370.         {
  371.             debug_die'DataObjectCache::get_by_name() : No name field to query on' );
  372.         }
  373.  
  374.         ifempty($req_name) )
  375.         {
  376.             if($halt_on_emptydebug_die"Requested $this->objtype from $this->dbtablename without name!)}
  377.             $r NULL;
  378.             return $r;
  379.         }
  380.  
  381.         // Load just the requested object:
  382.         $Debuglog->add"Loading <strong>$this->objtype($req_name)</strong>"'dataobjects' );
  383.         $sql "SELECT *
  384.                           FROM $this->dbtablename
  385.                          WHERE $this->name_field = ".$DB->quote($req_name);
  386.  
  387.         if$db_row $DB->get_row$sqlOBJECT0'DataObjectCache::get_by_name()' ) )
  388.         {
  389.             $resolved_ID $db_row->{$this->dbIDname};
  390.             $Debuglog->add'success; ID = '.$resolved_ID'dataobjects' );
  391.             ifisset$this->cache[$resolved_ID) )
  392.             {    // Object is not already in cache:
  393.                 $Debuglog->add'Adding to cache...''dataobjects' );
  394.                 //$Obj = new $this->objtype( $row ); // COPY !!
  395.                 //if( ! $this->add( $this->new_obj( $db_row ) ) )
  396.                 if$this->add$this->new_obj$db_row ) ) )
  397.                 {    // could not add
  398.                     $Debuglog->add'Could not add() object to cache!''dataobjects' );
  399.                 }
  400.             }
  401.             return $this->cache[$resolved_ID];
  402.         }
  403.         else
  404.         {
  405.             $Debuglog->add'Could not get DataObject by name.''dataobjects' );
  406.             if$halt_on_error )
  407.             {
  408.                 debug_die"Requested $this->objtype does not exist!);
  409.             }
  410.             $r NULL;
  411.             return $r;
  412.         }
  413.     }
  414.     
  415.     
  416.     /**
  417.      * Remove an object from cache by ID
  418.      *
  419.      * @param integer ID of object to remove
  420.      */
  421.     function remove_by_ID$req_ID )
  422.     {
  423.         unset$this->cache[$req_ID);
  424.         unset$marcus );
  425.     }
  426.     
  427.     
  428.     /**
  429.      * Delete an object from DB by ID.
  430.      * 
  431.      * @param integer ID of object to delete
  432.      * @return boolean 
  433.      */
  434.     function dbdelete_by_ID$req_ID )
  435.     {
  436.         ifisset$this->cache[$req_ID) )
  437.         {
  438.             // Delete from db
  439.             $this->cache[$req_ID]->dbdelete();
  440.              
  441.             // Remove from cache 
  442.             $this->remove_by_ID$req_ID );
  443.             
  444.             return true;
  445.         }
  446.         else 
  447.         {
  448.             return false;
  449.         }
  450.     }
  451.  
  452.  
  453.     /**
  454.      * Display form option list with cache contents
  455.      *
  456.      * Load the cache if necessary
  457.      *
  458.      * @todo Shouldn't this use {@link option_list_return()}?
  459.      *
  460.      * @param integer selected ID
  461.      * @param boolean provide a choice for "none" with ID ''
  462.      */
  463.     function option_list$default 0$allow_none false$method ='name' )
  464.     {
  465.         if( ($this->all_loaded&& $this->load_all )
  466.         // We have not loaded all items so far, but we're allowed to... so let's go:
  467.             $this->load_all();
  468.         }
  469.  
  470.         if$allow_none )
  471.         {
  472.             echo '<option value=""';
  473.             ifempty($default) ) echo ' selected="selected"';
  474.             echo '>'T_('None','</option>'."\n";
  475.         }
  476.  
  477.         foreach$this->cache as $loop_Obj )
  478.         {
  479.             echo '<option value="'.$loop_Obj->ID.'"';
  480.             if$loop_Obj->ID == $default echo ' selected="selected"';
  481.             echo '>';
  482.             $loop_Obj->$method();
  483.             echo '</option>'."\n";
  484.         }
  485.     }
  486.  
  487.  
  488.     /**
  489.      * Returns form option list with cache contents
  490.      *
  491.      * Load the cache if necessary
  492.      *
  493.      * @param integer selected ID
  494.      * @param boolean provide a choice for "none" with ID ''
  495.      */
  496.     function option_list_return$default 0$allow_none false$method 'name_return' )
  497.     {
  498.         if( ($this->all_loaded&& $this->load_all )
  499.         // We have not loaded all items so far, but we're allowed to... so let's go:
  500.             $this->load_all();
  501.         }
  502.  
  503.         $r '';
  504.  
  505.         if$allow_none )
  506.         {
  507.             $r .= '<option value=""';
  508.             ifempty($default) ) $r .= ' selected="selected"';
  509.             $r .= '>'.T_('None').'</option>'."\n";
  510.         }
  511.  
  512.         foreach$this->cache as $loop_Obj )
  513.         {
  514.             $r .=  '<option value="'.$loop_Obj->ID.'"';
  515.             if$loop_Obj->ID == $default $r .= ' selected="selected"';
  516.             $r .= '>';
  517.             $r .= $loop_Obj->$method();
  518.             $r .=  '</option>'."\n";
  519.         }
  520.  
  521.         return $r;
  522.     }
  523. }
  524.  
  525. /*
  526.  * $Log: _dataobjectcache.class.php,v $
  527.  * Revision 1.6.2.2  2006/11/04 19:54:53  fplanque
  528.  * Reinjected old Log blocks. Removing them from CVS was a bad idea -- especially since Daniel has decided branch 1.9 was his HEAD...
  529.  *
  530.  * Revision 1.6  2006/08/02 16:34:16  yabs
  531.  * corrected $row to $db_row in function get_by_name()
  532.  *
  533.  * Revision 1.5  2006/06/14 17:26:13  fplanque
  534.  * minor
  535.  *
  536.  * Revision 1.4  2006/04/19 20:13:50  fplanque
  537.  * do not restrict to :// (does not catch subdomains, not even www.)
  538.  *
  539.  * Revision 1.3  2006/04/14 19:25:32  fplanque
  540.  * evocore merge with work app
  541.  *
  542.  * Revision 1.2  2006/03/12 23:08:58  fplanque
  543.  * doc cleanup
  544.  *
  545.  * Revision 1.1  2006/02/23 21:11:57  fplanque
  546.  * File reorganization to MVC (Model View Controller) architecture.
  547.  * See index.hml files in folders.
  548.  * (Sorry for all the remaining bugs induced by the reorg... :/)
  549.  *
  550.  * Revision 1.34  2006/02/08 12:24:37  blueyed
  551.  * doc
  552.  *
  553.  * Revision 1.33  2005/12/30 20:13:39  fplanque
  554.  * UI changes mostly (need to double check sync)
  555.  *
  556.  * Revision 1.32  2005/12/12 19:21:21  fplanque
  557.  * big merge; lots of small mods; hope I didn't make to many mistakes :]
  558.  *
  559.  * Revision 1.28  2005/11/16 21:53:49  fplanque
  560.  * minor
  561.  *
  562.  * Revision 1.27  2005/11/16 12:21:15  blueyed
  563.  * use debug_die()
  564.  *
  565.  * Revision 1.26  2005/11/09 03:20:05  blueyed
  566.  * minor
  567.  *
  568.  * Revision 1.25  2005/10/03 22:50:53  blueyed
  569.  * Fixed E_NOTICE for PHP 4.4.0 and probably 5.1.x (again). Functions that return by reference must not return values!
  570.  *
  571.  * Revision 1.24  2005/09/29 15:26:15  fplanque
  572.  * added get_by_name()
  573.  *
  574.  * Revision 1.23  2005/09/18 01:46:55  blueyed
  575.  * Fixed E_NOTICE for return by reference (PHP 4.4.0)
  576.  *
  577.  * Revision 1.22  2005/09/06 17:13:54  fplanque
  578.  * stop processing early if referer spam has been detected
  579.  *
  580.  * Revision 1.21  2005/08/02 18:15:14  fplanque
  581.  * fix for correct NULL handling
  582.  *
  583.  * Revision 1.20  2005/07/15 18:10:07  fplanque
  584.  * allow instantiating of member objects (used for preloads)
  585.  *
  586.  * Revision 1.19  2005/06/10 18:25:44  fplanque
  587.  * refactoring
  588.  *
  589.  * Revision 1.18  2005/05/16 15:17:13  fplanque
  590.  * minor
  591.  *
  592.  * Revision 1.17  2005/05/11 13:21:38  fplanque
  593.  * allow disabling of mediua dir for specific blogs
  594.  *
  595.  * Revision 1.16  2005/04/19 16:23:02  fplanque
  596.  * cleanup
  597.  * added FileCache
  598.  * improved meta data handling
  599.  *
  600.  * Revision 1.15  2005/03/14 20:22:19  fplanque
  601.  * refactoring, some cacheing optimization
  602.  *
  603.  * Revision 1.14  2005/03/02 15:24:29  fplanque
  604.  * allow get_by_ID(NULL) in some situations
  605.  *
  606.  * Revision 1.13  2005/02/28 09:06:32  blueyed
  607.  * removed constants for DB config (allows to override it from _config_TEST.php), introduced EVO_CONFIG_LOADED
  608.  *
  609.  * Revision 1.12  2005/02/14 21:17:45  blueyed
  610.  * optimized cache handling
  611.  *
  612.  * Revision 1.11  2005/02/09 00:27:13  blueyed
  613.  * Removed deprecated globals / userdata handling
  614.  *
  615.  * Revision 1.10  2005/02/08 04:45:02  blueyed
  616.  * improved $DB get_results() handling
  617.  *
  618.  * Revision 1.9  2005/01/20 18:46:26  fplanque
  619.  * debug
  620.  *
  621.  * Revision 1.8  2005/01/13 19:53:50  fplanque
  622.  * Refactoring... mostly by Fabrice... not fully checked :/
  623.  *
  624.  * Revision 1.7  2004/12/27 18:37:58  fplanque
  625.  * changed class inheritence
  626.  *
  627.  * Revision 1.5  2004/12/21 21:18:38  fplanque
  628.  * Finished handling of assigning posts/items to users
  629.  *
  630.  * Revision 1.4  2004/12/17 20:38:52  fplanque
  631.  * started extending item/post capabilities (extra status, type)
  632.  *
  633.  * Revision 1.3  2004/10/14 18:31:25  blueyed
  634.  * granting copyright
  635.  *
  636.  * Revision 1.2  2004/10/14 16:28:40  fplanque
  637.  * minor changes
  638.  *
  639.  * Revision 1.1  2004/10/13 22:46:32  fplanque
  640.  * renamed [b2]evocore/*
  641.  *
  642.  * Revision 1.18  2004/10/12 10:27:18  fplanque
  643.  * Edited code documentation.
  644.  *
  645.  */
  646. ?>

Documentation generated on Tue, 18 Dec 2007 19:13:42 +0100 by phpDocumentor 1.4.0