r1344 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r1343‎ | r1344 | r1345 >
Date:09:30, 20 May 2003
Author:vibber
Status:old
Tags:
Comment:
Experimental optional compression support for page cache
Modified paths:
  • /trunk/phase3/includes/Article.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Article.php
@@ -1537,12 +1537,25 @@
15381538 }
15391539
15401540 function loadFromFileCache() {
 1541+ global $wgUseGzip;
15411542 wfDebug(" loadFromFileCache()\n");
1542 - readfile($this->fileCacheName());
 1543+ $filename=$this->fileCacheName();
 1544+ $filenamegz = "{$filename}.gz";
 1545+ if( $wgUseGzip
 1546+ && wfClientAcceptsGzip()
 1547+ && file_exists( $filenamegz)
 1548+ && ( filemtime( $filenamegz ) >= filemtime( $filename ) ) ) {
 1549+ wfDebug(" sending gzip\n");
 1550+ header( "Content-Encoding: gzip" );
 1551+ header( "Vary: Accept-Encoding" );
 1552+ $filename = $filenamegz;
 1553+ }
 1554+ readfile( $filename );
15431555 }
15441556
15451557 function saveToFileCache( $text ) {
1546 -
 1558+ global $wgUseGzip, $wgCompressByDefault;
 1559+
15471560 wfDebug(" saveToFileCache()\n");
15481561 $filename=$this->fileCacheName();
15491562 $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2
@@ -1551,10 +1564,38 @@
15521565 if(!file_exists($mydir2)) { mkdir($mydir2,0777); }
15531566 $f = fopen( $filename, "w" );
15541567 if($f) {
 1568+ $now = wfTimestampNow();
15551569 fwrite( $f, str_replace( "</html>",
1556 - "<!-- Cached " . wfTimestampNow() . " -->\n</html>",
 1570+ "<!-- Cached $now -->\n</html>",
15571571 $text ) );
15581572 fclose( $f );
 1573+ if( $wgUseGzip and $wgCompressByDefault ) {
 1574+ $start = microtime();
 1575+ wfDebug(" saving gzip\n");
 1576+ $gzout = gzencode( str_replace( "</html>",
 1577+ "<!-- Cached/compressed $now -->\n</html>",
 1578+ $text ) );
 1579+ if( $gzout === false ) {
 1580+ wfDebug(" failed to gzip compress, sending plaintext\n");
 1581+ return $text;
 1582+ }
 1583+ if( $f = fopen( "{$filename}.gz", "w" ) ) {
 1584+ fwrite( $f, $gzout );
 1585+ fclose( $f );
 1586+ $end = microtime();
 1587+
 1588+ list($usec1, $sec1) = explode(" ",$start);
 1589+ list($usec2, $sec2) = explode(" ",$end);
 1590+ $interval = ((float)$usec2 + (float)$sec2) -
 1591+ ((float)$usec1 + (float)$sec1);
 1592+ wfDebug(" saved gzip in $interval\n");
 1593+ } else {
 1594+ wfDebug(" failed to write gzip, still sending\n" );
 1595+ }
 1596+ header( "Content-Encoding: gzip" );
 1597+ header( "Vary: Accept-Encoding" );
 1598+ return $gzout;
 1599+ }
15591600 }
15601601 return $text;
15611602 }
Index: trunk/phase3/includes/GlobalFunctions.php
@@ -499,4 +499,20 @@
500500 return $s;
501501 }
502502
 503+function wfClientAcceptsGzip() {
 504+ global $wgUseGzip;
 505+ if( $wgUseGzip ) {
 506+ # FIXME: we may want to blacklist some broken browsers
 507+ if( preg_match(
 508+ '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
 509+ $_SERVER["HTTP_ACCEPT_ENCODING"],
 510+ $m ) ) {
 511+ if( ( $m[1] == "q" ) && ( $m[2] == 0 ) ) return false;
 512+ wfDebug( " accepts gzip\n" );
 513+ return true;
 514+ }
 515+ }
 516+ return false;
 517+}
 518+
503519 ?>
Index: trunk/phase3/includes/DefaultSettings.php
@@ -75,6 +75,12 @@
7676 $wgUseTeX = false;
7777 $wgProfiling = false; # Enable for more detailed by-function times in debug log
7878
 79+# We can serve pages compressed in order to save bandwidth,
 80+# but this will increase CPU usage.
 81+# Requires zlib support enabled in PHP.
 82+$wgUseGzip = false;
 83+$wgCompressByDefault = true;
 84+
7985 # Which namespaces should support subpages?
8086 # See Language.php for a list of namespaces.
8187 #

Status & tagging log