r14439 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r14438‎ | r14439 | r14440 >
Date:18:29, 28 May 2006
Author:midom
Status:old
Tags:
Comment:
Allow storing local message cache as PHP source instead of serialized array, this way opcode cache is enjoyed :-) Might be not that secure, so disabled by default.
Modified paths:
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/MessageCache.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/MessageCache.php
@@ -113,14 +113,58 @@
114114 @chmod( $filename, 0666 );
115115 }
116116
 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+ }
117144
 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+
118162 /**
119163 * Loads messages either from memcached or the database, if not disabled
120164 * On error, quietly switches to a fallback mode
121165 * Returns false for a reportable error, true otherwise
122166 */
123167 function load() {
124 - global $wgLocalMessageCache;
 168+ global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
125169
126170 if ( $this->mDisable ) {
127171 static $shownDisabled = false;
@@ -141,7 +185,11 @@
142186 wfProfileIn( $fname.'-fromlocal' );
143187 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
144188 if ( $hash ) {
145 - $this->loadFromLocal( $hash );
 189+ if ($wgLocalMessageCacheSerialized) {
 190+ $this->loadFromLocal( $hash );
 191+ } else {
 192+ $this->loadFromScript( $hash );
 193+ }
146194 }
147195 wfProfileOut( $fname.'-fromlocal' );
148196
@@ -157,7 +205,11 @@
158206 $hash = md5( $serialized );
159207 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
160208 }
161 - $this->saveToLocal( $serialized, $hash );
 209+ if ($wgLocalMessageCacheSerialized) {
 210+ $this->saveToLocal( $serialized,$hash );
 211+ } else {
 212+ $this->saveToScript( $this->mCache, $hash );
 213+ }
162214 }
163215 wfProfileOut( $fname.'-fromcache' );
164216 }
@@ -188,7 +240,11 @@
189241 $serialized = serialize( $this->mCache );
190242 $hash = md5( $serialized );
191243 $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+ }
193249 }
194250
195251 wfProfileOut( $fname.'-save' );
@@ -289,7 +345,7 @@
290346 }
291347
292348 function replace( $title, $text ) {
293 - global $wgLocalMessageCache, $parserMemc, $wgDBname;
 349+ global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
294350
295351 $this->lock();
296352 $this->load();
@@ -303,7 +359,11 @@
304360 $serialized = serialize( $this->mCache );
305361 $hash = md5( $serialized );
306362 $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+ }
308368 }
309369
310370
Index: trunk/phase3/includes/DefaultSettings.php
@@ -547,6 +547,12 @@
548548 * Directory for local copy of message cache, for use in addition to memcached
549549 */
550550 $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;
551557
552558 /**
553559 * Directory for compiled constant message array databases

Status & tagging log