b2evolution PHP Cross Reference Blogging Systems

Source: /inc/files/model/_filetypecache.class.php - 170 lines - 4739 bytes - Summary - Text - Print

Description: This file implements the file type cache 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 file type cache 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)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.
  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   * PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license
  22   * PROGIDISTRI S.A.S.'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 fplanque: Francois PLANQUE.
  30   * @author mbruneau: Marc BRUNEAU / PROGIDISTRI
  31   *
  32   * @version $Id: _filetypecache.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  /**
  39   * FiletypeCache Class
  40   *
  41   * @package evocore
  42   */
  43  class FiletypeCache extends DataObjectCache
  44  {
  45      var $extension_cache = array();
  46  
  47      /**
  48       * Constructor
  49       */
  50  	function FiletypeCache()
  51      {
  52          // Call parent constructor:
  53          parent::DataObjectCache( 'Filetype', true, 'T_filetypes', 'ftyp_', 'ftyp_ID', 'ftyp_extensions' );
  54      }
  55  
  56  
  57      /**
  58       * Add a dataobject to the cache
  59       */
  60  	function add( & $Obj )
  61      {
  62          global $Debuglog;
  63  
  64          if( empty($Obj->ID) )
  65          {
  66              $Debuglog->add( 'No object to add!', 'dataobjects' );
  67              return false;
  68          }
  69  
  70          if( isset($this->cache[$Obj->ID]) )
  71          {
  72              $Debuglog->add( $this->objtype.': Object with ID '.$Obj->ID.' is already cached', 'dataobjects' );
  73              return false;
  74          }
  75  
  76          // If the object is valid and not already cached:
  77          $this->cache[$Obj->ID] = & $Obj;
  78  
  79          // cache all extensions
  80          $extensions = explode( ' ', $Obj->extensions );
  81  
  82          foreach( $extensions as $extension )
  83          {
  84              $this->extension_cache[$extension] = $Obj; // not & $Obj
  85          }
  86  
  87          $this->mimetype_cache[$Obj->mimetype] = $Obj; // not & $Obj
  88  
  89          return true;
  90      }
  91  
  92  
  93       /**
  94       * Get an object from cache by extensions ID
  95       *
  96       * Load the cache if necessary (all at once if allowed).
  97       *
  98       * @param string Extension string of object to load
  99       * @param boolean true if function should die on error
 100       * @param boolean true if function should die on empty/null
 101       * @return reference on cached object
 102       */
 103      function & get_by_extension( $req_ID, $halt_on_error = true, $halt_on_empty = true )
 104      {
 105          global $DB, $Debuglog;
 106  
 107          if( empty($req_ID) )
 108          {
 109              if($halt_on_empty) { debug_die( "Requested $this->objtype from $this->dbtablename without ID!" ); }
 110              $r = NULL;
 111              return $r;
 112          }
 113  
 114          $this->load_all();
 115  
 116          if( empty( $this->extension_cache[ $req_ID ] ) )
 117          { // Requested object does not exist
 118              // $Debuglog->add( 'failure', 'dataobjects' );
 119              if( $halt_on_error )
 120              {
 121                  debug_die( "Requested $this->objtype does not exist!" );
 122              }
 123              $r = false;
 124              return $r;
 125          }
 126  
 127          return $this->extension_cache[ $req_ID ];
 128      }
 129  
 130  
 131       /**
 132       * Get an object from cache by mimetype.
 133       *
 134       * Load the cache if necessary (all at once if allowed).
 135       *
 136       * @todo dh> this copies nearly the whole code of get_by_extension! Have not checked DataObjectCache, but this needs refactoring.
 137       *
 138       * @param string Mimetype string of object to load
 139       * @param boolean true if function should die on error
 140       * @param boolean true if function should die on empty/null
 141       * @return reference on cached object
 142       */
 143      function & get_by_mimetype( $mimetype, $halt_on_error = true, $halt_on_empty = true )
 144      {
 145          global $DB, $Debuglog;
 146  
 147          if( empty($mimetype) )
 148          {
 149              if($halt_on_empty) { debug_die( "Requested $this->objtype from $this->dbtablename without mimetype!" ); }
 150              $r = NULL;
 151              return $r;
 152          }
 153  
 154          $this->load_all();
 155  
 156          if( empty( $this->mimetype_cache[ $mimetype ] ) )
 157          { // Requested object does not exist
 158              if( $halt_on_error )
 159              {
 160                  debug_die( "Requested $this->objtype does not exist!" );
 161              }
 162              $r = false;
 163              return $r;
 164          }
 165  
 166          return $this->mimetype_cache[ $mimetype ];
 167      }
 168  }
 169  
 170  ?>

title

Description

title

Description

title

Description

title

title

Body