From: Brion Vibber Date: Tue, 20 May 2003 09:30:40 +0000 (+0000) Subject: Experimental optional compression support for page cache X-Git-Tag: 1.1.0~534 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=764331c963ee7256f9e04193ba8a0fa9ea3822e7;p=lhc%2Fweb%2Fwiklou.git Experimental optional compression support for page cache --- diff --git a/includes/Article.php b/includes/Article.php index 4e81c274c6..52f77f7a03 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -1537,12 +1537,25 @@ name=\"wpSummary\" maxlength=200 size=60>
} function loadFromFileCache() { + global $wgUseGzip; wfDebug(" loadFromFileCache()\n"); - readfile($this->fileCacheName()); + $filename=$this->fileCacheName(); + $filenamegz = "{$filename}.gz"; + if( $wgUseGzip + && wfClientAcceptsGzip() + && file_exists( $filenamegz) + && ( filemtime( $filenamegz ) >= filemtime( $filename ) ) ) { + wfDebug(" sending gzip\n"); + header( "Content-Encoding: gzip" ); + header( "Vary: Accept-Encoding" ); + $filename = $filenamegz; + } + readfile( $filename ); } function saveToFileCache( $text ) { - + global $wgUseGzip, $wgCompressByDefault; + wfDebug(" saveToFileCache()\n"); $filename=$this->fileCacheName(); $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2 @@ -1551,10 +1564,38 @@ name=\"wpSummary\" maxlength=200 size=60>
if(!file_exists($mydir2)) { mkdir($mydir2,0777); } $f = fopen( $filename, "w" ); if($f) { + $now = wfTimestampNow(); fwrite( $f, str_replace( "", - "\n", + "\n", $text ) ); fclose( $f ); + if( $wgUseGzip and $wgCompressByDefault ) { + $start = microtime(); + wfDebug(" saving gzip\n"); + $gzout = gzencode( str_replace( "", + "\n", + $text ) ); + if( $gzout === false ) { + wfDebug(" failed to gzip compress, sending plaintext\n"); + return $text; + } + if( $f = fopen( "{$filename}.gz", "w" ) ) { + fwrite( $f, $gzout ); + fclose( $f ); + $end = microtime(); + + list($usec1, $sec1) = explode(" ",$start); + list($usec2, $sec2) = explode(" ",$end); + $interval = ((float)$usec2 + (float)$sec2) - + ((float)$usec1 + (float)$sec1); + wfDebug(" saved gzip in $interval\n"); + } else { + wfDebug(" failed to write gzip, still sending\n" ); + } + header( "Content-Encoding: gzip" ); + header( "Vary: Accept-Encoding" ); + return $gzout; + } } return $text; } diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 56724b29b2..d3128abcfc 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -75,6 +75,12 @@ $wgMiserMode = false; # Disable database-intensive features $wgUseTeX = false; $wgProfiling = false; # Enable for more detailed by-function times in debug log +# We can serve pages compressed in order to save bandwidth, +# but this will increase CPU usage. +# Requires zlib support enabled in PHP. +$wgUseGzip = false; +$wgCompressByDefault = true; + # Which namespaces should support subpages? # See Language.php for a list of namespaces. # diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index e2244c6fc1..05d4ea5b68 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -499,4 +499,20 @@ function wfNumLink( $offset, $limit, $link, $query = "" ) return $s; } +function wfClientAcceptsGzip() { + global $wgUseGzip; + if( $wgUseGzip ) { + # FIXME: we may want to blacklist some broken browsers + if( preg_match( + '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/', + $_SERVER["HTTP_ACCEPT_ENCODING"], + $m ) ) { + if( ( $m[1] == "q" ) && ( $m[2] == 0 ) ) return false; + wfDebug( " accepts gzip\n" ); + return true; + } + } + return false; +} + ?>