[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/includes/magpierss/ -> rss_cache.inc (source)

   1  <?php
   2  /*
   3   * Project:     MagpieRSS: a simple RSS integration tool
   4   * File:        rss_cache.inc, a simple, rolling(no GC), cache 
   5   *              for RSS objects, keyed on URL.
   6   * Author:      Kellan Elliott-McCrea <kellan@protest.net>
   7   * Version:     0.51
   8   * License:     GPL
   9   *
  10   * The lastest version of MagpieRSS can be obtained from:
  11   * http://magpierss.sourceforge.net
  12   *
  13   * For questions, help, comments, discussion, etc., please join the
  14   * Magpie mailing list:
  15   * http://lists.sourceforge.net/lists/listinfo/magpierss-general
  16   *
  17   */
  18  
  19  class RSSCache {
  20      var $BASE_CACHE = './cache';    // where the cache files are stored
  21      var $MAX_AGE    = 3600;         // when are files stale, default one hour
  22      var $ERROR      = "";           // accumulate error messages
  23      
  24      function RSSCache ($base='', $age='') {
  25          if ( $base ) {
  26              $this->BASE_CACHE = $base;
  27          }
  28          if ( $age ) {
  29              $this->MAX_AGE = $age;
  30          }
  31          
  32          // attempt to make the cache directory
  33          if ( ! file_exists( $this->BASE_CACHE ) ) {
  34              $status = @mkdir( $this->BASE_CACHE, 0755 );
  35              
  36              // if make failed 
  37              if ( ! $status ) {
  38                  $this->error(
  39                      "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
  40                  );
  41              }
  42          }
  43      }
  44      
  45  /*=======================================================================*\
  46      Function:   set
  47      Purpose:    add an item to the cache, keyed on url
  48      Input:      url from wich the rss file was fetched
  49      Output:     true on sucess  
  50  \*=======================================================================*/
  51      function set ($url, $rss) {
  52          $this->ERROR = "";
  53          $cache_file = $this->file_name( $url );
  54          $fp = @fopen( $cache_file, 'w' );
  55          
  56          if ( ! $fp ) {
  57              $this->error(
  58                  "Cache unable to open file for writing: $cache_file"
  59              );
  60              return 0;
  61          }
  62          
  63          
  64          $data = $this->serialize( $rss );
  65          fwrite( $fp, $data );
  66          fclose( $fp );
  67          
  68          return $cache_file;
  69      }
  70      
  71  /*=======================================================================*\
  72      Function:   get
  73      Purpose:    fetch an item from the cache
  74      Input:      url from wich the rss file was fetched
  75      Output:     cached object on HIT, false on MISS 
  76  \*=======================================================================*/ 
  77      function get ($url) {
  78          $this->ERROR = "";
  79          $cache_file = $this->file_name( $url );
  80          
  81          if ( ! file_exists( $cache_file ) ) {
  82              $this->debug( 
  83                  "Cache doesn't contain: $url (cache file: $cache_file)"
  84              );
  85              return 0;
  86          }
  87          
  88          $fp = @fopen($cache_file, 'r');
  89          if ( ! $fp ) {
  90              $this->error(
  91                  "Failed to open cache file for reading: $cache_file"
  92              );
  93              return 0;
  94          }
  95          
  96          $data = fread( $fp, filesize($cache_file) );
  97          $rss = $this->unserialize( $data );
  98          
  99          return $rss;
 100      }
 101  
 102  /*=======================================================================*\
 103      Function:   check_cache
 104      Purpose:    check a url for membership in the cache
 105                  and whether the object is older then MAX_AGE (ie. STALE)
 106      Input:      url from wich the rss file was fetched
 107      Output:     cached object on HIT, false on MISS 
 108  \*=======================================================================*/     
 109      function check_cache ( $url ) {
 110          $this->ERROR = "";
 111          $filename = $this->file_name( $url );
 112          
 113          if ( file_exists( $filename ) ) {
 114              // find how long ago the file was added to the cache
 115              // and whether that is longer then MAX_AGE
 116              $mtime = filemtime( $filename );
 117              $age = time() - $mtime;
 118              if ( $this->MAX_AGE > $age ) {
 119                  // object exists and is current
 120                  return 'HIT';
 121              }
 122              else {
 123                  // object exists but is old
 124                  return 'STALE';
 125              }
 126          }
 127          else {
 128              // object does not exist
 129              return 'MISS';
 130          }
 131      }
 132  
 133  /*=======================================================================*\
 134      Function:   serialize
 135  \*=======================================================================*/     
 136      function serialize ( $rss ) {
 137          return serialize( $rss );
 138      }
 139  
 140  /*=======================================================================*\
 141      Function:   unserialize
 142  \*=======================================================================*/     
 143      function unserialize ( $data ) {
 144          return unserialize( $data );
 145      }
 146      
 147  /*=======================================================================*\
 148      Function:   file_name
 149      Purpose:    map url to location in cache
 150      Input:      url from wich the rss file was fetched
 151      Output:     a file name
 152  \*=======================================================================*/     
 153      function file_name ($url) {
 154          $filename = md5( $url );
 155          return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
 156      }
 157  
 158  /*=======================================================================*\
 159      Function:   error
 160      Purpose:    register error
 161  \*=======================================================================*/         
 162      function error ($errormsg, $lvl=E_USER_WARNING) {
 163          // append PHP's error message if track_errors enabled
 164          if ( isset($php_errormsg) ) { 
 165              $errormsg .= " ($php_errormsg)";
 166          }
 167          $this->ERROR = $errormsg;
 168          if ( MAGPIE_DEBUG ) {
 169              trigger_error( $errormsg, $lvl);
 170          }
 171          else {
 172              error_log( $errormsg, 0);
 173          }
 174      }
 175      
 176      function debug ($debugmsg, $lvl=E_USER_NOTICE) {
 177          if ( MAGPIE_DEBUG ) {
 178              $this->error("MagpieRSS [debug] $debugmsg", $lvl);
 179          }
 180      }
 181  
 182  }
 183  
 184  ?>


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics