PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cache.class.php

http://wikiplot.googlecode.com/
PHP | 181 lines | 78 code | 11 blank | 92 comment | 11 complexity | ebfd5d25047210f6a1583cbd3a523762 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. Copyright (C) 2006 by the WikiPlot project authors (See http://code.google.com/p/WikiPlot).
  4. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7. */
  8. /**
  9. * File used to control cache
  10. *
  11. * This file provides functions to control the content of the cache.
  12. * This file is made to make the software more maintain able, and as an interface to the cache for third party developers.
  13. *
  14. * @package WikiPlot
  15. * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
  16. * @author WikiPlot development team.
  17. * @copyright Copyright 2006, WikiPlot development team.
  18. */
  19. /**
  20. * Require local settings
  21. *
  22. * This file is needed to control the cache correctly.
  23. */
  24. require_once("WikiPlotSettings.php");
  25. /**
  26. * Cache controlling class
  27. *
  28. * Class used to control the cache.
  29. *
  30. * @package WikiPlot
  31. * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
  32. * @author WikiPlot development team.
  33. * @copyright Copyright 2006, WikiPlot development team.
  34. */
  35. class Cache
  36. {
  37. /**
  38. *Cleanup the cache
  39. *
  40. *Cleans up the cache by removing old and unused files.
  41. *
  42. *@access public
  43. *@uses CleanupMaxAge()
  44. *@uses CleanupUnused()
  45. */
  46. function CleanupCache()
  47. {
  48. $this->CleanupMaxAge();
  49. $this->CleanupUnused();
  50. }
  51. /**
  52. * Cleanup cache from old files
  53. *
  54. * Removes old files from the cache, see LocalSettings.php for settings.
  55. *
  56. * @access public
  57. */
  58. function CleanupMaxAge()
  59. {
  60. $CachePath = $_SERVER["DOCUMENT_ROOT"] . WikiPlotCachePath;
  61. if ($cache = opendir($CachePath))
  62. {
  63. $MaxFileAge = time() - (WikiPlotCacheAge * 24 * 60 * 60);
  64. while (false !== ($file = readdir($cache)))
  65. {
  66. $FileAge = filemtime($CachePath . "/" . $file);
  67. if($FileAge>$MaxFileAge)
  68. {
  69. if(!(unlink($CachePath . "/" . $file)))
  70. {
  71. //TODO: throw some error!
  72. }
  73. }
  74. }
  75. closedir($cache);
  76. }else{
  77. //TODO: throw some error!
  78. }
  79. }
  80. /**
  81. * Cleanup unused files from cache
  82. *
  83. * Removes old unused files from the cache, see LocalSettings.php for settings.
  84. * This functions indentifies files as unused if they havn't been accessed for a long time.
  85. *
  86. * @access public
  87. */
  88. function CleanupUnused()
  89. {
  90. $CachePath = $_SERVER["DOCUMENT_ROOT"] . WikiPlotCachePath;
  91. if ($cache = opendir($CachePath))
  92. {
  93. $MaxFileAge = time() - (WikiPlotMaxUnusedAge * 24 * 60 * 60);
  94. while (false !== ($file = readdir($cache)))
  95. {
  96. $FileAge = fileatime($CachePath . "/" . $file);
  97. if($FileAge>$MaxFileAge)
  98. {
  99. if(!(unlink($CachePath . "/" . $file)))
  100. {
  101. //TODO: throw some error!
  102. }
  103. }
  104. }
  105. closedir($cache);
  106. }else{
  107. //TODO: throw some error!
  108. }
  109. }
  110. /**
  111. * Does file exist in cache
  112. *
  113. * Returns true or false depending on whether or not FileName Exist in cache.
  114. *
  115. * @access public
  116. * @param string $FileName Filename relative to cache.
  117. * @return boolean Whether or not FileName exist.
  118. */
  119. function FileExist($FileName)
  120. {
  121. return file_exists($_SERVER["DOCUMENT_ROOT"] . WikiPlotCachePath . $FileName);
  122. }
  123. /**
  124. * Get file URL
  125. *
  126. * Gets the URL og the given FileName, returns false if the files doen't exist.
  127. *
  128. * @access public
  129. * @uses FileExist()
  130. * @param string $FileName Filename relative to cache.
  131. * @return string Returns the URL of the file.
  132. */
  133. function FileURL($FileName)
  134. {
  135. if($this->FileExist($FileName))
  136. {
  137. return WikiPlotCacheURL . "/" . $FileName;
  138. }else{
  139. return false;
  140. }
  141. }
  142. /**
  143. * Get cache Path
  144. *
  145. * Get absolute path to the cache, returns false if FileName exists.
  146. *
  147. *@access public
  148. *@uses FileExist()
  149. *@param string $FileName Filename you want the path to, shortcut to detecting if file exists.
  150. *@return string Path to the cache, false if FileName exists.
  151. */
  152. function CachePath($FileName = null)
  153. {
  154. if(!is_null($FileName))
  155. {
  156. if($this->FileExist($FileName))
  157. {
  158. return false;
  159. }else{
  160. return $_SERVER["DOCUMENT_ROOT"] . WikiPlotCachePath . $FileName;
  161. }
  162. }else{
  163. return $_SERVER["DOCUMENT_ROOT"] . WikiPlotCachePath;
  164. }
  165. }
  166. }
  167. ?>