rss_cache.inc 6.4 KB

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