r16929 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r16928‎ | r16929 | r16930 >
Date:08:25, 11 October 2006
Author:tstarling
Status:old
Tags:
Comment:
Renamed CacheManager to HTMLFileCache, to avoid confusion with the other sort of cache.
Modified paths:
  • /trunk/phase3/includes/Article.php (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/CacheManager.php (deleted) (history)
  • /trunk/phase3/includes/Database.php (modified) (history)
  • /trunk/phase3/includes/HTMLCacheUpdate.php (modified) (history)
  • /trunk/phase3/includes/HTMLFileCache.php (added) (history)
  • /trunk/phase3/includes/HTMLFileCache.php (added) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/CacheManager.php
@@ -1,159 +0,0 @@
2 -<?php
3 -/**
4 - * Contain the CacheManager class
5 - * @package MediaWiki
6 - * @subpackage Cache
7 - */
8 -
9 -/**
10 - * Handles talking to the file cache, putting stuff in and taking it back out.
11 - * Mostly called from Article.php, also from DatabaseFunctions.php for the
12 - * emergency abort/fallback to cache.
13 - *
14 - * Global options that affect this module:
15 - * $wgCachePages
16 - * $wgCacheEpoch
17 - * $wgUseFileCache
18 - * $wgFileCacheDirectory
19 - * $wgUseGzip
20 - * @package MediaWiki
21 - */
22 -class CacheManager {
23 - var $mTitle, $mFileCache;
24 -
25 - function CacheManager( &$title ) {
26 - $this->mTitle =& $title;
27 - $this->mFileCache = '';
28 - }
29 -
30 - function fileCacheName() {
31 - global $wgFileCacheDirectory;
32 - if( !$this->mFileCache ) {
33 - $key = $this->mTitle->getPrefixedDbkey();
34 - $hash = md5( $key );
35 - $key = str_replace( '.', '%2E', urlencode( $key ) );
36 -
37 - $hash1 = substr( $hash, 0, 1 );
38 - $hash2 = substr( $hash, 0, 2 );
39 - $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
40 -
41 - if($this->useGzip())
42 - $this->mFileCache .= '.gz';
43 -
44 - wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
45 - }
46 - return $this->mFileCache;
47 - }
48 -
49 - function isFileCached() {
50 - return file_exists( $this->fileCacheName() );
51 - }
52 -
53 - function fileCacheTime() {
54 - return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
55 - }
56 -
57 - function isFileCacheGood( $timestamp ) {
58 - global $wgCacheEpoch;
59 -
60 - if( !$this->isFileCached() ) return false;
61 -
62 - $cachetime = $this->fileCacheTime();
63 - $good = (( $timestamp <= $cachetime ) &&
64 - ( $wgCacheEpoch <= $cachetime ));
65 -
66 - wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
67 - return $good;
68 - }
69 -
70 - function useGzip() {
71 - global $wgUseGzip;
72 - return $wgUseGzip;
73 - }
74 -
75 - /* In handy string packages */
76 - function fetchRawText() {
77 - return file_get_contents( $this->fileCacheName() );
78 - }
79 -
80 - function fetchPageText() {
81 - if( $this->useGzip() ) {
82 - /* Why is there no gzfile_get_contents() or gzdecode()? */
83 - return implode( '', gzfile( $this->fileCacheName() ) );
84 - } else {
85 - return $this->fetchRawText();
86 - }
87 - }
88 -
89 - /* Working directory to/from output */
90 - function loadFromFileCache() {
91 - global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
92 - wfDebug(" loadFromFileCache()\n");
93 -
94 - $filename=$this->fileCacheName();
95 - $wgOut->sendCacheControl();
96 -
97 - header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
98 - header( "Content-language: $wgContLanguageCode" );
99 -
100 - if( $this->useGzip() ) {
101 - if( wfClientAcceptsGzip() ) {
102 - header( 'Content-Encoding: gzip' );
103 - } else {
104 - /* Send uncompressed */
105 - readgzfile( $filename );
106 - return;
107 - }
108 - }
109 - readfile( $filename );
110 - }
111 -
112 - function checkCacheDirs() {
113 - $filename = $this->fileCacheName();
114 - $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
115 - $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
116 -
117 - if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
118 - if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
119 - }
120 -
121 - function saveToFileCache( $origtext ) {
122 - $text = $origtext;
123 - if(strcmp($text,'') == 0) return '';
124 -
125 - wfDebug(" saveToFileCache()\n", false);
126 -
127 - $this->checkCacheDirs();
128 -
129 - $f = fopen( $this->fileCacheName(), 'w' );
130 - if($f) {
131 - $now = wfTimestampNow();
132 - if( $this->useGzip() ) {
133 - $rawtext = str_replace( '</html>',
134 - '<!-- Cached/compressed '.$now." -->\n</html>",
135 - $text );
136 - $text = gzencode( $rawtext );
137 - } else {
138 - $text = str_replace( '</html>',
139 - '<!-- Cached '.$now." -->\n</html>",
140 - $text );
141 - }
142 - fwrite( $f, $text );
143 - fclose( $f );
144 - if( $this->useGzip() ) {
145 - if( wfClientAcceptsGzip() ) {
146 - header( 'Content-Encoding: gzip' );
147 - return $text;
148 - } else {
149 - return $rawtext;
150 - }
151 - } else {
152 - return $text;
153 - }
154 - }
155 - return $text;
156 - }
157 -
158 -}
159 -
160 -?>
Index: trunk/phase3/includes/Article.php
@@ -9,7 +9,7 @@
1010 *
1111 * See design.txt for an overview.
1212 * Note: edit user interface and cache support functions have been
13 - * moved to separate EditPage and CacheManager classes.
 13+ * moved to separate EditPage and HTMLFileCache classes.
1414 *
1515 * @package MediaWiki
1616 */
@@ -2250,7 +2250,7 @@
22512251 $called = true;
22522252 if($this->isFileCacheable()) {
22532253 $touched = $this->mTouched;
2254 - $cache = new CacheManager( $this->mTitle );
 2254+ $cache = new HTMLFileCache( $this->mTitle );
22552255 if($cache->isFileCacheGood( $touched )) {
22562256 wfDebug( "Article::tryFileCache(): about to load file\n" );
22572257 $cache->loadFromFileCache();
@@ -2438,7 +2438,7 @@
24392439
24402440 # File cache
24412441 if ( $wgUseFileCache ) {
2442 - $cm = new CacheManager( $title );
 2442+ $cm = new HTMLFileCache( $title );
24432443 @unlink( $cm->fileCacheName() );
24442444 }
24452445
@@ -2464,7 +2464,7 @@
24652465
24662466 # Clear file cache
24672467 if ( $wgUseFileCache ) {
2468 - $cm = new CacheManager( $title );
 2468+ $cm = new HTMLFileCache( $title );
24692469 @unlink( $cm->fileCacheName() );
24702470 }
24712471 }
Index: trunk/phase3/includes/AutoLoader.php
@@ -22,7 +22,7 @@
2323 'eAccelBagOStuff' => 'includes/BagOStuff.php',
2424 'DBABagOStuff' => 'includes/BagOStuff.php',
2525 'Block' => 'includes/Block.php',
26 - 'CacheManager' => 'includes/CacheManager.php',
 26+ 'HTMLFileCache' => 'includes/HTMLFileCache.php',
2727 'CategoryPage' => 'includes/CategoryPage.php',
2828 'CategoryViewer' => 'includes/CategoryPage.php',
2929 'Categoryfinder' => 'includes/Categoryfinder.php',
Index: trunk/phase3/includes/Title.php
@@ -540,7 +540,7 @@
541541
542542 foreach ( $titles as $title ) {
543543 if ( $wgUseFileCache ) {
544 - $cm = new CacheManager($title);
 544+ $cm = new HTMLFileCache($title);
545545 @unlink($cm->fileCacheName());
546546 }
547547
@@ -1366,7 +1366,7 @@
13671367 );
13681368
13691369 if ($wgUseFileCache) {
1370 - $cache = new CacheManager($this);
 1370+ $cache = new HTMLFileCache($this);
13711371 @unlink($cache->fileCacheName());
13721372 }
13731373
Index: trunk/phase3/includes/HTMLFileCache.php
@@ -0,0 +1,159 @@
 2+<?php
 3+/**
 4+ * Contain the HTMLFileCache class
 5+ * @package MediaWiki
 6+ * @subpackage Cache
 7+ */
 8+
 9+/**
 10+ * Handles talking to the file cache, putting stuff in and taking it back out.
 11+ * Mostly called from Article.php, also from DatabaseFunctions.php for the
 12+ * emergency abort/fallback to cache.
 13+ *
 14+ * Global options that affect this module:
 15+ * $wgCachePages
 16+ * $wgCacheEpoch
 17+ * $wgUseFileCache
 18+ * $wgFileCacheDirectory
 19+ * $wgUseGzip
 20+ * @package MediaWiki
 21+ */
 22+class HTMLFileCache {
 23+ var $mTitle, $mFileCache;
 24+
 25+ function HTMLFileCache( &$title ) {
 26+ $this->mTitle =& $title;
 27+ $this->mFileCache = '';
 28+ }
 29+
 30+ function fileCacheName() {
 31+ global $wgFileCacheDirectory;
 32+ if( !$this->mFileCache ) {
 33+ $key = $this->mTitle->getPrefixedDbkey();
 34+ $hash = md5( $key );
 35+ $key = str_replace( '.', '%2E', urlencode( $key ) );
 36+
 37+ $hash1 = substr( $hash, 0, 1 );
 38+ $hash2 = substr( $hash, 0, 2 );
 39+ $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
 40+
 41+ if($this->useGzip())
 42+ $this->mFileCache .= '.gz';
 43+
 44+ wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
 45+ }
 46+ return $this->mFileCache;
 47+ }
 48+
 49+ function isFileCached() {
 50+ return file_exists( $this->fileCacheName() );
 51+ }
 52+
 53+ function fileCacheTime() {
 54+ return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
 55+ }
 56+
 57+ function isFileCacheGood( $timestamp ) {
 58+ global $wgCacheEpoch;
 59+
 60+ if( !$this->isFileCached() ) return false;
 61+
 62+ $cachetime = $this->fileCacheTime();
 63+ $good = (( $timestamp <= $cachetime ) &&
 64+ ( $wgCacheEpoch <= $cachetime ));
 65+
 66+ wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
 67+ return $good;
 68+ }
 69+
 70+ function useGzip() {
 71+ global $wgUseGzip;
 72+ return $wgUseGzip;
 73+ }
 74+
 75+ /* In handy string packages */
 76+ function fetchRawText() {
 77+ return file_get_contents( $this->fileCacheName() );
 78+ }
 79+
 80+ function fetchPageText() {
 81+ if( $this->useGzip() ) {
 82+ /* Why is there no gzfile_get_contents() or gzdecode()? */
 83+ return implode( '', gzfile( $this->fileCacheName() ) );
 84+ } else {
 85+ return $this->fetchRawText();
 86+ }
 87+ }
 88+
 89+ /* Working directory to/from output */
 90+ function loadFromFileCache() {
 91+ global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
 92+ wfDebug(" loadFromFileCache()\n");
 93+
 94+ $filename=$this->fileCacheName();
 95+ $wgOut->sendCacheControl();
 96+
 97+ header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
 98+ header( "Content-language: $wgContLanguageCode" );
 99+
 100+ if( $this->useGzip() ) {
 101+ if( wfClientAcceptsGzip() ) {
 102+ header( 'Content-Encoding: gzip' );
 103+ } else {
 104+ /* Send uncompressed */
 105+ readgzfile( $filename );
 106+ return;
 107+ }
 108+ }
 109+ readfile( $filename );
 110+ }
 111+
 112+ function checkCacheDirs() {
 113+ $filename = $this->fileCacheName();
 114+ $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
 115+ $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
 116+
 117+ if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
 118+ if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
 119+ }
 120+
 121+ function saveToFileCache( $origtext ) {
 122+ $text = $origtext;
 123+ if(strcmp($text,'') == 0) return '';
 124+
 125+ wfDebug(" saveToFileCache()\n", false);
 126+
 127+ $this->checkCacheDirs();
 128+
 129+ $f = fopen( $this->fileCacheName(), 'w' );
 130+ if($f) {
 131+ $now = wfTimestampNow();
 132+ if( $this->useGzip() ) {
 133+ $rawtext = str_replace( '</html>',
 134+ '<!-- Cached/compressed '.$now." -->\n</html>",
 135+ $text );
 136+ $text = gzencode( $rawtext );
 137+ } else {
 138+ $text = str_replace( '</html>',
 139+ '<!-- Cached '.$now." -->\n</html>",
 140+ $text );
 141+ }
 142+ fwrite( $f, $text );
 143+ fclose( $f );
 144+ if( $this->useGzip() ) {
 145+ if( wfClientAcceptsGzip() ) {
 146+ header( 'Content-Encoding: gzip' );
 147+ return $text;
 148+ } else {
 149+ return $rawtext;
 150+ }
 151+ } else {
 152+ return $text;
 153+ }
 154+ }
 155+ return $text;
 156+ }
 157+
 158+}
 159+
 160+?>
Property changes on: trunk/phase3/includes/HTMLFileCache.php
___________________________________________________________________
Added: svn:eol-style
1161 + native
Added: svn:keywords
2162 + Author Date Id Revision
Index: trunk/phase3/includes/HTMLCacheUpdate.php
@@ -176,7 +176,7 @@
177177 # Update file cache
178178 if ( $wgUseFileCache ) {
179179 foreach ( $titles as $title ) {
180 - $cm = new CacheManager($title);
 180+ $cm = new HTMLFileCache($title);
181181 @unlink($cm->fileCacheName());
182182 }
183183 }
Index: trunk/phase3/includes/Database.php
@@ -152,7 +152,7 @@
153153 }
154154 }
155155
156 - $cache = new CacheManager( $t );
 156+ $cache = new HTMLFileCache( $t );
157157 if( $cache->isFileCached() ) {
158158 $msg = '<p style="color: red"><b>'.$msg."<br />\n" .
159159 $cachederror . "</b></p>\n";