Index: trunk/phase3/includes/Article.php |
— | — | @@ -1537,12 +1537,25 @@ |
1538 | 1538 | } |
1539 | 1539 | |
1540 | 1540 | function loadFromFileCache() { |
| 1541 | + global $wgUseGzip; |
1541 | 1542 | 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 ); |
1543 | 1555 | } |
1544 | 1556 | |
1545 | 1557 | function saveToFileCache( $text ) { |
1546 | | - |
| 1558 | + global $wgUseGzip, $wgCompressByDefault; |
| 1559 | + |
1547 | 1560 | wfDebug(" saveToFileCache()\n"); |
1548 | 1561 | $filename=$this->fileCacheName(); |
1549 | 1562 | $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2 |
— | — | @@ -1551,10 +1564,38 @@ |
1552 | 1565 | if(!file_exists($mydir2)) { mkdir($mydir2,0777); } |
1553 | 1566 | $f = fopen( $filename, "w" ); |
1554 | 1567 | if($f) { |
| 1568 | + $now = wfTimestampNow(); |
1555 | 1569 | fwrite( $f, str_replace( "</html>", |
1556 | | - "<!-- Cached " . wfTimestampNow() . " -->\n</html>", |
| 1570 | + "<!-- Cached $now -->\n</html>", |
1557 | 1571 | $text ) ); |
1558 | 1572 | 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 | + } |
1559 | 1600 | } |
1560 | 1601 | return $text; |
1561 | 1602 | } |
Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -499,4 +499,20 @@ |
500 | 500 | return $s; |
501 | 501 | } |
502 | 502 | |
| 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 | + |
503 | 519 | ?> |
Index: trunk/phase3/includes/DefaultSettings.php |
— | — | @@ -75,6 +75,12 @@ |
76 | 76 | $wgUseTeX = false; |
77 | 77 | $wgProfiling = false; # Enable for more detailed by-function times in debug log |
78 | 78 | |
| 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 | + |
79 | 85 | # Which namespaces should support subpages? |
80 | 86 | # See Language.php for a list of namespaces. |
81 | 87 | # |