29 public static function handle( $s, $phase ) {
33 if ( ( $phase & PHP_OUTPUT_HANDLER_CLEAN ) === PHP_OUTPUT_HANDLER_CLEAN ) {
34 $logger = LoggerFactory::getInstance(
'output' );
35 $logger->debug( __METHOD__ .
" entrypoint={entry}; size={size}; phase=$phase", [
37 'size' => strlen( $s ),
45 $phpHandlesCompression = (
46 ini_get(
'output_handler' ) ===
'ob_gzhandler' ||
47 ini_get(
'zlib.output_handler' ) ===
'ob_gzhandler' ||
49 strtolower( ini_get(
'zlib.output_compression' ) ),
56 !$phpHandlesCompression &&
58 !defined(
'MW_NO_OUTPUT_COMPRESSION' ) &&
60 !$disableOutputCompression
62 $s = self::handleGzip( $s );
67 !$phpHandlesCompression &&
69 !ini_get(
'output_handler' ) &&
70 !ini_get(
'zlib.output_handler' )
72 self::emitContentLength( strlen( $s ) );
88 private static function findUriExtension() {
90 if ( isset( $_SERVER[
'REQUEST_URI'] ) ) {
92 $path = explode(
'?', $_SERVER[
'REQUEST_URI'], 2 )[0];
93 } elseif ( isset( $_SERVER[
'SCRIPT_NAME'] ) ) {
95 $path = $_SERVER[
'SCRIPT_NAME'];
101 $period = strrpos(
$path,
'.' );
102 if ( $period !==
false ) {
103 return strtolower( substr(
$path, $period ) );
118 private static function handleGzip( $s ) {
119 if ( !function_exists(
'gzencode' ) ) {
120 wfDebug( __METHOD__ .
"() skipping compression (gzencode unavailable)" );
123 if ( headers_sent() ) {
124 wfDebug( __METHOD__ .
"() skipping compression (headers already sent)" );
128 $ext = self::findUriExtension();
129 if ( $ext ==
'.gz' || $ext ==
'.tgz' ) {
145 wfDebug( __METHOD__ .
"() is compressing output" );
146 header(
'Content-Encoding: gzip' );
147 $s = gzencode( $s, 6 );
151 if ( !preg_grep(
'/^Vary:/i', headers_list() ) ) {
152 header(
'Vary: Accept-Encoding' );
172 private static function emitContentLength( $length ) {
173 if ( headers_sent() ) {
174 wfDebug( __METHOD__ .
"() headers already sent" );
179 in_array( http_response_code(), [ 200, 404 ],
true ) ||
180 ( $_SERVER[
'SERVER_PROTOCOL'] ??
null ) ===
'HTTP/1.0'
182 header(
"Content-Length: $length" );
188class_alias( OutputHandler::class,
'MediaWiki\\OutputHandler' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfClientAcceptsGzip( $force=false)
Whether the client accept gzip encoding.
A class containing constants representing the names of configuration variables.
const DisableOutputCompression
Name constant for the DisableOutputCompression setting, for use with Config::get()