Index: trunk/phase3/includes/MessageCache.php |
— | — | @@ -113,14 +113,58 @@ |
114 | 114 | @chmod( $filename, 0666 ); |
115 | 115 | } |
116 | 116 | |
| 117 | + function loadFromScript( $hash ) { |
| 118 | + global $wgLocalMessageCache, $wgDBname; |
| 119 | + if ( $wgLocalMessageCache === false ) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + $filename = "$wgLocalMessageCache/messages-$wgDBname"; |
| 124 | + |
| 125 | + wfSuppressWarnings(); |
| 126 | + $file = fopen( $filename, 'r' ); |
| 127 | + wfRestoreWarnings(); |
| 128 | + if ( !$file ) { |
| 129 | + return; |
| 130 | + } |
| 131 | + $localHash=substr(fread($file,40),8); |
| 132 | + fclose($file); |
| 133 | + if ($hash!=$localHash) { |
| 134 | + return; |
| 135 | + } |
| 136 | + require("$wgLocalMessageCache/messages-$wgDBname"); |
| 137 | + } |
| 138 | + |
| 139 | + function saveToScript($array, $hash) { |
| 140 | + global $wgLocalMessageCache, $wgDBname; |
| 141 | + if ( $wgLocalMessageCache === false ) { |
| 142 | + return; |
| 143 | + } |
117 | 144 | |
| 145 | + $filename = "$wgLocalMessageCache/messages-$wgDBname"; |
| 146 | + $oldUmask = umask( 0 ); |
| 147 | + wfMkdirParents( $wgLocalMessageCache, 0777 ); |
| 148 | + umask( $oldUmask ); |
| 149 | + $file = fopen( $filename.'.tmp', 'w'); |
| 150 | + fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array("); |
| 151 | + |
| 152 | + $re="/(?<!\\\\)'/"; |
| 153 | + foreach ($array as $key => $message) { |
| 154 | + fwrite($file, "'". preg_replace($re, "\'", $key). |
| 155 | + "' => '" . preg_replace( $re, "\'", $message) . "',\n"); |
| 156 | + } |
| 157 | + fwrite($file,");\n?>"); |
| 158 | + fclose($file); |
| 159 | + rename($filename.'.tmp',$filename); |
| 160 | + } |
| 161 | + |
118 | 162 | /** |
119 | 163 | * Loads messages either from memcached or the database, if not disabled |
120 | 164 | * On error, quietly switches to a fallback mode |
121 | 165 | * Returns false for a reportable error, true otherwise |
122 | 166 | */ |
123 | 167 | function load() { |
124 | | - global $wgLocalMessageCache; |
| 168 | + global $wgLocalMessageCache, $wgLocalMessageCacheSerialized; |
125 | 169 | |
126 | 170 | if ( $this->mDisable ) { |
127 | 171 | static $shownDisabled = false; |
— | — | @@ -141,7 +185,11 @@ |
142 | 186 | wfProfileIn( $fname.'-fromlocal' ); |
143 | 187 | $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" ); |
144 | 188 | if ( $hash ) { |
145 | | - $this->loadFromLocal( $hash ); |
| 189 | + if ($wgLocalMessageCacheSerialized) { |
| 190 | + $this->loadFromLocal( $hash ); |
| 191 | + } else { |
| 192 | + $this->loadFromScript( $hash ); |
| 193 | + } |
146 | 194 | } |
147 | 195 | wfProfileOut( $fname.'-fromlocal' ); |
148 | 196 | |
— | — | @@ -157,7 +205,11 @@ |
158 | 206 | $hash = md5( $serialized ); |
159 | 207 | $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); |
160 | 208 | } |
161 | | - $this->saveToLocal( $serialized, $hash ); |
| 209 | + if ($wgLocalMessageCacheSerialized) { |
| 210 | + $this->saveToLocal( $serialized,$hash ); |
| 211 | + } else { |
| 212 | + $this->saveToScript( $this->mCache, $hash ); |
| 213 | + } |
162 | 214 | } |
163 | 215 | wfProfileOut( $fname.'-fromcache' ); |
164 | 216 | } |
— | — | @@ -188,7 +240,11 @@ |
189 | 241 | $serialized = serialize( $this->mCache ); |
190 | 242 | $hash = md5( $serialized ); |
191 | 243 | $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); |
192 | | - $this->saveToLocal( $serialized, $hash ); |
| 244 | + if ($wgLocalMessageCacheSerialized) { |
| 245 | + $this->saveToLocal( $serialized,$hash ); |
| 246 | + } else { |
| 247 | + $this->saveToScript( $this->mCache, $hash ); |
| 248 | + } |
193 | 249 | } |
194 | 250 | |
195 | 251 | wfProfileOut( $fname.'-save' ); |
— | — | @@ -289,7 +345,7 @@ |
290 | 346 | } |
291 | 347 | |
292 | 348 | function replace( $title, $text ) { |
293 | | - global $wgLocalMessageCache, $parserMemc, $wgDBname; |
| 349 | + global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname; |
294 | 350 | |
295 | 351 | $this->lock(); |
296 | 352 | $this->load(); |
— | — | @@ -303,7 +359,11 @@ |
304 | 360 | $serialized = serialize( $this->mCache ); |
305 | 361 | $hash = md5( $serialized ); |
306 | 362 | $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); |
307 | | - $this->saveToLocal( $serialized, $hash ); |
| 363 | + if ($wgLocalMessageCacheSerialized) { |
| 364 | + $this->saveToLocal( $serialized,$hash ); |
| 365 | + } else { |
| 366 | + $this->saveToScript( $this->mCache, $hash ); |
| 367 | + } |
308 | 368 | } |
309 | 369 | |
310 | 370 | |
Index: trunk/phase3/includes/DefaultSettings.php |
— | — | @@ -547,6 +547,12 @@ |
548 | 548 | * Directory for local copy of message cache, for use in addition to memcached |
549 | 549 | */ |
550 | 550 | $wgLocalMessageCache = false; |
| 551 | +/** |
| 552 | + * Defines format of local cache |
| 553 | + * true - Serialized object |
| 554 | + * false - PHP source file (Warning - security risk) |
| 555 | + */ |
| 556 | +$wgLocalMessageCacheSerialized = true; |
551 | 557 | |
552 | 558 | /** |
553 | 559 | * Directory for compiled constant message array databases |