b2evolution PHP Cross Reference Blogging Systems

Source: /inc/items/model/_itemcache.class.php - 166 lines - 5447 bytes - Summary - Text - Print

Description: This file implements the ItemCache class. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the ItemCache 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-2014 by Francois Planque - {@link http://fplanque.com/}
   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10   *
  11   * {@internal License choice
  12   * - If you have received this file as part of a package, please find the license.txt file in
  13   *   the same folder or the closest folder above for complete license terms.
  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15   *   then you must choose one of the following licenses before using the file:
  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18   * }}
  19   *
  20   * {@internal Open Source relicensing agreement:
  21   * Daniel HAHLER grants Francois PLANQUE the right to license
  22   * Daniel HAHLER's contributions to this file and the b2evolution project
  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  24   * }}
  25   *
  26   * @package evocore
  27   *
  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  29   * @author blueyed: Daniel HAHLER.
  30   * @author fplanque: Francois PLANQUE.
  31   *
  32   * @version $Id: _itemcache.class.php 6136 2014-03-08 07:59:48Z manuel $
  33   */
  34  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  35  
  36  load_class( '_core/model/dataobjects/_dataobjectcache.class.php', 'DataObjectCache' );
  37  
  38  load_class( 'items/model/_item.class.php', 'Item' );
  39  
  40  /**
  41   * Item Cache Class
  42   *
  43   * @package evocore
  44   */
  45  class ItemCache extends DataObjectCache
  46  {
  47      /**
  48       * Lazy filled index of url titles
  49       */
  50      var $urltitle_index = array();
  51  
  52      /**
  53       * Constructor
  54       *
  55       * @param string object type of elements in Cache
  56       * @param string Name of the DB table
  57       * @param string Prefix of fields in the table
  58       * @param string Name of the ID field (including prefix)
  59       */
  60  	function ItemCache( $objType = 'Item', $dbtablename = 'T_items__item', $dbprefix = 'post_', $dbIDname = 'post_ID' )
  61      {
  62          parent::DataObjectCache( $objType, false, $dbtablename, $dbprefix, $dbIDname );
  63      }
  64  
  65  
  66      /**
  67       * Get an object from cache by its urltitle
  68       *
  69       * Load into cache if necessary
  70       *
  71       * @param string stub of object to load
  72       * @param boolean false if you want to return false on error
  73       * @param boolean true if function should die on empty/null
  74       */
  75      function & get_by_urltitle( $req_urltitle, $halt_on_error = true, $halt_on_empty = true )
  76      {
  77          global $DB, $Debuglog;
  78  
  79          if( !isset( $this->urltitle_index[$req_urltitle] ) )
  80          { // not yet in cache:
  81          // Get from SlugCache
  82              $SlugCache = & get_SlugCache();
  83              $req_Slug =  $SlugCache->get_by_name( $req_urltitle, $halt_on_error, $halt_on_empty );
  84  
  85              if( $req_Slug && $req_Slug->get( 'type' ) == 'item' )
  86              {    // It is in SlugCache
  87                  $itm_ID = $req_Slug->get( 'itm_ID' );
  88                  if( $Item = $this->get_by_ID( $itm_ID, $halt_on_error, $halt_on_empty ) )
  89                  {
  90                      $this->urltitle_index[$req_urltitle] = $Item;
  91                  }
  92                  else
  93                  {    // Item does not exist
  94                      if( $halt_on_error ) debug_die( "Requested $this->objtype does not exist!" );
  95                      $this->urltitle_index[$req_urltitle] = false;
  96                  }
  97              }
  98              else
  99              {    // not in the slugCache
 100                  if( $halt_on_error ) debug_die( "Requested $this->objtype does not exist!" );
 101                  $this->urltitle_index[$req_urltitle] = false;
 102              }
 103          }
 104          else
 105          {
 106              $Debuglog->add( "Retrieving <strong>$this->objtype($req_urltitle)</strong> from cache" );
 107          }
 108  
 109          return $this->urltitle_index[$req_urltitle];
 110      }
 111  
 112  
 113      /**
 114       * Load a list of item referenced by their urltitle into the cache
 115       *
 116       * @param array of urltitles of Items to load
 117       */
 118  	function load_urltitle_array( $req_array )
 119      {
 120          global $DB, $Debuglog;
 121  
 122          $req_list = "'".implode( "','", $req_array)."'";
 123          $Debuglog->add( "Loading <strong>$this->objtype($req_list)</strong> into cache", 'dataobjects' );
 124          $sql = "SELECT * FROM $this->dbtablename WHERE post_urltitle IN ( $req_list )";
 125          $dbIDname = $this->dbIDname;
 126          $objtype = $this->objtype;
 127          foreach( $DB->get_results( $sql ) as $row )
 128          {
 129              $this->cache[ $row->$dbIDname ] = new $objtype( $row ); // COPY!
 130              // $obj = $this->cache[ $row->$dbIDname ];
 131              // $obj->disp( 'name' );
 132  
 133              // put into index:
 134              $this->urltitle_index[$row->post_urltitle] = & $this->cache[ $row->$dbIDname ];
 135  
 136              $Debuglog->add( "Cached <strong>$this->objtype($row->post_urltitle)</strong>" );
 137          }
 138  
 139          // Set cache from Slug table:
 140          foreach( $req_array as $urltitle )
 141          {
 142              if( !isset( $this->urltitle_index[$urltitle] ) )
 143              { // not yet in cache:
 144                  $SlugCache = & get_SlugCache();
 145                  if( $req_Slug = $SlugCache->get_by_name( $urltitle, false, false ) )
 146                  {
 147                      if( $req_Slug->get( 'type' ) == 'item' )
 148                      {    // Is item slug
 149                          if( $Item = $this->get_by_ID( $req_Slug->get( 'itm_ID' ), false ) )
 150                          {    // Set cache 
 151                              $this->urltitle_index[$urltitle] = $Item;
 152                              $Debuglog->add( "Cached <strong>$this->objtype($urltitle)</strong>" );
 153                              continue;
 154                          }
 155                      }
 156                  }
 157                  // Set cache for non found objects:
 158                  $this->urltitle_index[$urltitle] = false; // Remember it doesn't exist in DB either
 159                  $Debuglog->add( "Cached <strong>$this->objtype($urltitle)</strong> as NON EXISTENT" );
 160              }
 161          }
 162      }
 163  
 164  }
 165  
 166  ?>

title

Description

title

Description

title

Description

title

title

Body