b2evolution PHP Cross Reference Blogging Systems

Source: /inc/links/model/_linkcache.class.php - 566 lines - 14044 bytes - Summary - Text - Print

Description: This file implements the LinkCache 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 LinkCache 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   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * }}
  21   *
  22   * @package evocore
  23   *
  24   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  25   * @author fplanque: Francois PLANQUE.
  26   *
  27   * @version $Id: _linkcache.class.php 6136 2014-03-08 07:59:48Z manuel $
  28   */
  29  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  30  
  31  load_class( '_core/model/dataobjects/_dataobjectcache.class.php', 'DataObjectCache' );
  32  
  33  /**
  34   * LinkCache Class
  35   *
  36   * @package evocore
  37   */
  38  class LinkCache extends DataObjectCache
  39  {
  40      /**
  41       * Cache for item -> array of object references
  42       * @access private
  43       * @var array
  44       */
  45      var $cache_item = array();
  46  
  47      /**
  48       * @access private
  49       * @var array Remember full loads
  50       */
  51      var $loaded_cache_item = array();
  52  
  53      /**
  54       * Cache for comment -> array of object references
  55       * @access private
  56       * @var array
  57       */
  58      var $cache_comment = array();
  59  
  60      /**
  61       * @access private
  62       * @var array Remember full loads
  63       */
  64      var $loaded_cache_comment = array();
  65  
  66      /**
  67       * Cache for user -> array of object references
  68       * @access private
  69       * @var array
  70       */
  71      var $cache_user = array();
  72  
  73      /**
  74       * @access private
  75       * @var array Remember full loads
  76       */
  77      var $loaded_cache_user = array();
  78  
  79      /**
  80       * Cache for file -> array of object references
  81       * @access private
  82       * @var array
  83       */
  84      var $cache_file = array();
  85  
  86      /**
  87       * @access private
  88       * @var array Remember full loads
  89       */
  90      var $loaded_cache_file = array();
  91  
  92      /**
  93       * Constructor
  94       */
  95  	function LinkCache()
  96      {
  97          parent::DataObjectCache( 'Link', false, 'T_links', 'link_', 'link_ID' );
  98      }
  99  
 100  
 101      /**
 102       * Add a dataobject to the cache
 103       *
 104       * @param object Link
 105       * @param string Type of cached object ( '' - empty value to cache depend on link owner type, 'file' - to cache file )
 106       * @return boolean TRUE on success
 107       */
 108  	function add( & $Obj, $cache_type = '' )
 109      {
 110          if( ( !isset($Obj->ID) ) || ( $Obj->ID == 0 ) )
 111          { // The object is not cachable, becuase it has no valid ID
 112              return false;
 113          }
 114  
 115          // If the object wasn't already cached and is valid:
 116          $this->cache[$Obj->ID] = & $Obj;
 117          if( $cache_type == 'file' )
 118          { // Cache by File ID
 119              $this->cache_file[$Obj->file_ID][$Obj->ID] = & $Obj;
 120              return true;
 121          }
 122  
 123          // Cache by Item, Comment or User, we need to get the owner
 124          $LinkOwner = & $Obj->get_LinkOwner();
 125          if( $LinkOwner == NULL )
 126          { // LinkOwner is not valid
 127              return false;
 128          }
 129  
 130          $link_object_ID = $LinkOwner->get_ID();
 131          switch( $LinkOwner->type )
 132          {
 133              case 'item': // cache indexed by Item ID
 134                  $this->cache_item[$link_object_ID][$Obj->ID] = & $Obj;
 135                  break;
 136  
 137              case 'comment': // cache indexed by Comment ID
 138                  $this->cache_comment[$link_object_ID][$Obj->ID] = & $Obj;
 139                  break;
 140  
 141              case 'user': // cache indexed by User ID
 142                  $this->cache_user[$link_object_ID][$Obj->ID] = & $Obj;
 143                  break;
 144  
 145              default:
 146                  return false;
 147          }
 148          return true;
 149      }
 150  
 151  
 152      /**
 153       * Remove object from the cache
 154       *
 155       * @param object Link
 156       * @param string Type of cached object ( '' - empty value to cache depend on link owner type, 'file' - to cache file )
 157       * @return boolean TRUE on success
 158       */
 159  	function remove( & $Obj, $cache_type = '' )
 160      {
 161          if( ( !isset($Obj->ID) ) || ( $Obj->ID == 0 ) )
 162          { // The object is not cachable, so it can't be in the cache
 163              return false;
 164          }
 165  
 166          $this->remove_by_ID( $Obj->ID );
 167  
 168          if( ( $cache_type == 'file' ) && ( isset( $this->cache_file[$Obj->file_ID][$Obj->ID] ) ) )
 169          { // Cache by File ID
 170              unset( $this->cache_file[$Obj->file_ID][$Obj->ID] );
 171              return true;
 172          }
 173  
 174          // Cache by Item, Comment or User, we need to get the owner
 175          $LinkOwner = & $Obj->get_LinkOwner();
 176          if( $LinkOwner == NULL )
 177          { // LinkOwner is not valid
 178              return false;
 179          }
 180  
 181          $link_object_ID = $LinkOwner->get_ID();
 182          switch( $LinkOwner->type )
 183          {
 184              case 'item':
 185                  if( isset( $this->cache_item[$link_object_ID][$Obj->ID] ) )
 186                  {
 187                      unset( $this->cache_item[$link_object_ID][$Obj->ID] );
 188                      return true;
 189                  }
 190                  break;
 191  
 192              case 'comment':
 193                  if( isset( $this->cache_comment[$link_object_ID][$Obj->ID] ) )
 194                  {
 195                      unset( $this->cache_comment[$link_object_ID][$Obj->ID] );
 196                      return true;
 197                  }
 198                  break;
 199  
 200              case 'user':
 201                  if( isset( $this->cache_user[$link_object_ID][$Obj->ID] ) )
 202                  {
 203                      unset( $this->cache_user[$link_object_ID][$Obj->ID] );
 204                      return true;
 205                  }
 206                  break;
 207  
 208              case 'file':
 209              default: // Invalid cache type
 210                  break;
 211          }
 212  
 213          // Object was not in the cache
 214          return false;
 215      }
 216  
 217  
 218      /**
 219       * Returns links for a given Item
 220       *
 221       * Loads if necessary
 222       *
 223       * @todo dh> does not get used anywhere (yet)?
 224       *
 225       * @param integer item ID to load links for
 226       * @return array of refs to Link objects
 227       */
 228      function & get_by_item_ID( $item_ID )
 229      {
 230          // Make sure links are loaded:
 231          $this->load_by_item_ID( $item_ID );
 232  
 233          return $this->cache_item[$item_ID];
 234      }
 235  
 236  
 237      /**
 238       * Returns links for a given Comment
 239       *
 240       * Loads if necessary
 241       *
 242       * @param integer comment ID to load links for
 243       * @return array of refs to Link objects
 244       */
 245      function & get_by_comment_ID( $comment_ID )
 246      {
 247          // Make sure links are loaded:
 248          $this->load_by_comment_ID( $comment_ID );
 249  
 250          return $this->cache_comment[$comment_ID];
 251      }
 252  
 253  
 254      /**
 255       * Returns links for a given User
 256       *
 257       * Loads if necessary
 258       *
 259       * @param integer user ID to load links for
 260       * @return array of refs to Link objects
 261       */
 262      function & get_by_user_ID( $user_ID )
 263      {
 264          // Make sure links are loaded:
 265          $this->load_by_user_ID( $user_ID );
 266  
 267          return $this->cache_user[$user_ID];
 268      }
 269  
 270  
 271      /**
 272       * Returns links for a given File
 273       *
 274       * Loads if necessary
 275       *
 276       * @param integer file ID to load links for
 277       * @return array of refs to Link objects
 278       */
 279      function & get_by_file_ID( $file_ID )
 280      {
 281          // Make sure links are loaded:
 282          $this->load_by_file_ID( $file_ID );
 283  
 284          return $this->cache_file[$file_ID];
 285      }
 286  
 287  
 288      /**
 289       * Load links for a given Item
 290       *
 291       * Optimization: If the Item happens to be in the current MainList, Links for the whole MainList will be cached.
 292       *
 293       * @todo cache Link targets before letting the Link constructor handle it
 294       *
 295       * @param integer item ID to load links for
 296       */
 297  	function load_by_item_ID( $item_ID )
 298      {
 299          global $DB, $Debuglog, $MainList, $ItemList;
 300  
 301          if( isset( $this->loaded_cache_item[$item_ID] ) )
 302          {
 303              $Debuglog->add( "Already loaded <strong>$this->objtype(Item #$item_ID)</strong> into cache", 'dataobjects' );
 304              return false;
 305          }
 306          // Check if this Item is part of the MainList
 307          if( $MainList || $ItemList )
 308          {
 309              $prefetch_IDs = array();
 310              if( $MainList )
 311              {
 312                  $prefetch_IDs = array_merge($prefetch_IDs, $MainList->get_page_ID_array());
 313              }
 314              if( $ItemList )
 315              {
 316                  $prefetch_IDs = array_merge($prefetch_IDs, $ItemList->get_page_ID_array());
 317              }
 318              $Debuglog->add( "Loading <strong>$this->objtype(Item #$item_ID)</strong> into cache as part of MainList/ItemList...");
 319              $this->load_by_item_list( $prefetch_IDs );
 320          }
 321          else
 322          {    // NO, load Links for this single Item:
 323  
 324              // Remember this special load:
 325              $this->cache_item[$item_ID] = array();
 326              $this->loaded_cache_item[$item_ID] = true;
 327  
 328              $Debuglog->add( "Loading <strong>$this->objtype(Item #$item_ID)</strong> into cache", 'dataobjects' );
 329  
 330              $sql = 'SELECT *
 331                                  FROM T_links
 332                               WHERE link_itm_ID = '.$item_ID.'
 333                               ORDER BY link_ltype_ID, link_dest_itm_ID, link_file_ID';
 334              foreach( $DB->get_results( $sql ) as $row )
 335              {    // Cache each matching object:
 336                  $this->add( new Link( $row ) );
 337              }
 338          }
 339  
 340          return true;
 341      }
 342  
 343  
 344      /**
 345       * Load links for a given Comment
 346       *
 347       * @param integer comment ID to load links for
 348       */
 349  	function load_by_comment_ID( $comment_ID )
 350      {
 351          global $DB, $Debuglog;
 352  
 353          if( isset( $this->loaded_cache_comment[$comment_ID] ) )
 354          {
 355              $Debuglog->add( "Already loaded <strong>$this->objtype(Comment #$comment_ID)</strong> into cache", 'dataobjects' );
 356              return false;
 357          }
 358  
 359          // Remember this special load:
 360          $this->cache_comment[$comment_ID] = array();
 361          $this->loaded_cache_comment[$comment_ID] = true;
 362  
 363          $Debuglog->add( "Loading <strong>$this->objtype(Comment #$comment_ID)</strong> into cache", 'dataobjects' );
 364  
 365          $sql = 'SELECT *
 366                              FROM T_links
 367                           WHERE link_cmt_ID = '.$comment_ID.'
 368                           ORDER BY link_file_ID';
 369          foreach( $DB->get_results( $sql ) as $row )
 370          {    // Cache each matching object:
 371              $this->add( new Link( $row ) );
 372          }
 373  
 374          return true;
 375      }
 376  
 377  
 378      /**
 379       * Load links for a given User
 380       *
 381       * @param integer user ID to load links for
 382       */
 383  	function load_by_user_ID( $user_ID )
 384      {
 385          global $DB, $Debuglog;
 386  
 387          if( isset( $this->loaded_cache_user[$user_ID] ) )
 388          {
 389              $Debuglog->add( "Already loaded <strong>$this->objtype(User #$user_ID)</strong> into cache", 'dataobjects' );
 390              return false;
 391          }
 392  
 393          // Remember this special load:
 394          $this->cache_user[$user_ID] = array();
 395          $this->loaded_cache_user[$user_ID] = true;
 396  
 397          $Debuglog->add( "Loading <strong>$this->objtype(User #$user_ID)</strong> into cache", 'dataobjects' );
 398  
 399          $SQL = new SQL( 'Get the links by user ID' );
 400          $SQL->SELECT( '*' );
 401          $SQL->FROM( 'T_links' );
 402          $SQL->WHERE( 'link_usr_ID  = '.$DB->quote( $user_ID ) );
 403          $SQL->ORDER_BY( 'link_file_ID' );
 404          $links = $DB->get_results( $SQL->get() );
 405  
 406          // Load linked files into the FileCache
 407          $this->load_linked_files( $links );
 408  
 409          foreach( $links as $row )
 410          { // Cache each matching object:
 411              $this->add( new Link( $row ), 'user' );
 412          }
 413  
 414          return true;
 415      }
 416  
 417  
 418      /**
 419       * Load links for a given Item list
 420       *
 421       * @todo cache Link targets before letting the Link constructor handle it
 422       *
 423       * @param array of of item IDs to load links for
 424       */
 425  	function load_by_item_list( $itemIDarray )
 426      {
 427          global $DB, $Debuglog;
 428  
 429          $item_list = implode( ',', $itemIDarray );
 430  
 431          $Debuglog->add( "Loading <strong>$this->objtype(Items #$item_list)</strong> into cache", 'dataobjects' );
 432  
 433          // For each item in list...
 434          foreach( $itemIDarray as $item_ID )
 435          { // Remember this special load:
 436              $this->cache_item[$item_ID] = array();
 437              $this->loaded_cache_item[$item_ID] = true;
 438          }
 439  
 440          $SQL = new SQL( 'Get the links by item IDs' );
 441          $SQL->SELECT( '*' );
 442          $SQL->FROM( 'T_links' );
 443          $SQL->WHERE( 'link_itm_ID IN ('.$item_list.')' );
 444          $SQL->ORDER_BY( 'link_ID' );
 445          $links = $DB->get_results( $SQL->get() );
 446          foreach( $links as $row )
 447          { // Cache each matching object:
 448              $this->add( new Link( $row ), 'item' );
 449          }
 450  
 451          return true;
 452      }
 453  
 454  
 455      /**
 456       * Load links for a given File
 457       *
 458       * @param integer file ID to load links for
 459       */
 460  	function load_by_file_ID( $file_ID )
 461      {
 462          global $DB, $Debuglog;
 463  
 464          if( isset( $this->loaded_cache_file[$file_ID] ) )
 465          {
 466              $Debuglog->add( "Already loaded <strong>$this->objtype(File #$file_ID)</strong> into cache", 'dataobjects' );
 467              return false;
 468          }
 469  
 470          // Remember this special load:
 471          $this->cache_file[$file_ID] = array();
 472          $this->loaded_cache_file[$file_ID] = true;
 473  
 474          $Debuglog->add( "Loading <strong>$this->objtype(File #$file_ID)</strong> into cache", 'dataobjects' );
 475  
 476          $SQL = new SQL( 'Get the links by file ID' );
 477          $SQL->SELECT( '*' );
 478          $SQL->FROM( 'T_links' );
 479          $SQL->WHERE( 'link_file_ID = '.$DB->quote( $file_ID ) );
 480          $SQL->ORDER_BY( 'link_ID' );
 481          $links = $DB->get_results( $SQL->get() );
 482          foreach( $links as $row )
 483          { // Cache each matching object:
 484              $this->add( new Link( $row ), 'file' );
 485          }
 486  
 487          return true;
 488      }
 489  
 490  
 491      /**
 492       * Load required Files into the FileCache before Link class constructor will be called.
 493       * It's imporatnt to load all Files with one query instead of loading the one by one
 494       * 
 495       * private function
 496       * 
 497       * @param array link rows
 498       */
 499  	function load_linked_files( & $link_rows )
 500      {
 501          if( empty( $link_rows ) )
 502          { // There are nothing to load
 503              return;
 504          }
 505  
 506          // Collect required file Ids
 507          $link_file_ids = array();
 508          foreach( $link_rows as $row )
 509          {
 510              $link_file_ids[] = $row->link_file_ID;
 511          }
 512  
 513          if( !empty( $link_file_ids ) )
 514          { // Load required Files into FileCache
 515              $FileCache = & get_FileCache();
 516              $FileCache->load_where( 'file_ID IN ( '.implode( ',', $link_file_ids ).' )' );
 517          }
 518      }
 519  
 520  
 521      /**
 522       * Clear the cache **extensively**
 523       *
 524       * @param boolean Keep copy of cache in case we try to re instantiate previous object
 525       * @param string What to clear: 'all', 'user', 'item', 'comment', 'file'
 526       * @param integer ID of the clearing object
 527       */
 528  	function clear( $keep_shadow = false, $object = 'all', $object_ID = 0 )
 529      {
 530          parent::clear( $keep_shadow );
 531  
 532          switch( $object )
 533          {
 534              case 'all':
 535                  // Clear all cached objects
 536                  $this->cache_item = array();
 537                  $this->loaded_cache_item = array();
 538                  $this->cache_comment = array();
 539                  $this->loaded_cache_comment = array();
 540                  $this->cache_user = array();
 541                  $this->loaded_cache_user = array();
 542                  $this->cache_file = array();
 543                  $this->loaded_cache_file = array();
 544                  break;
 545  
 546              case 'item':
 547              case 'comment':
 548              case 'user':
 549              case 'file':
 550                  // Clear only the selected type of objects
 551                  if( empty( $object_ID ) )
 552                  { // Clear all cached objects of this type
 553                      $this->{'cache_'.$object} = array();
 554                      $this->{'loaded_cache_'.$object} = array();
 555                  }
 556                  else
 557                  { // Clear a cache only one object
 558                      unset( $this->{'cache_'.$object}[ $object_ID ] );
 559                      unset( $this->{'loaded_cache_'.$object}[ $object_ID ] );
 560                  }
 561                  break;
 562          }
 563      }
 564  }
 565  
 566  ?>

title

Description

title

Description

title

Description

title

title

Body