43 public static function handle( $s, $phase ) {
47 if ( ( $phase & PHP_OUTPUT_HANDLER_CLEAN ) === PHP_OUTPUT_HANDLER_CLEAN ) {
48 $logger = LoggerFactory::getInstance(
'output' );
49 $logger->debug( __METHOD__ .
" entrypoint={entry}; size={size}; phase=$phase", [
51 'size' => strlen( $s ),
59 $phpHandlesCompression = (
60 ini_get(
'output_handler' ) ===
'ob_gzhandler' ||
61 ini_get(
'zlib.output_handler' ) ===
'ob_gzhandler' ||
63 strtolower( ini_get(
'zlib.output_compression' ) ),
70 !$phpHandlesCompression &&
72 !defined(
'MW_NO_OUTPUT_COMPRESSION' ) &&
74 !$disableOutputCompression
76 $s = self::handleGzip( $s );
81 !$phpHandlesCompression &&
83 !ini_get(
'output_handler' ) &&
84 !ini_get(
'zlib.output_handler' )
86 self::emitContentLength( strlen( $s ) );
102 private static function findUriExtension() {
104 if ( isset( $_SERVER[
'REQUEST_URI'] ) ) {
106 $path = explode(
'?', $_SERVER[
'REQUEST_URI'], 2 )[0];
107 } elseif ( isset( $_SERVER[
'SCRIPT_NAME'] ) ) {
109 $path = $_SERVER[
'SCRIPT_NAME'];
115 $period = strrpos(
$path,
'.' );
116 if ( $period !==
false ) {
117 return strtolower( substr(
$path, $period ) );
132 private static function handleGzip( $s ) {
133 if ( !function_exists(
'gzencode' ) ) {
134 wfDebug( __METHOD__ .
"() skipping compression (gzencode unavailable)" );
137 if ( headers_sent() ) {
138 wfDebug( __METHOD__ .
"() skipping compression (headers already sent)" );
142 $ext = self::findUriExtension();
143 if ( $ext ==
'.gz' || $ext ==
'.tgz' ) {
159 wfDebug( __METHOD__ .
"() is compressing output" );
160 header(
'Content-Encoding: gzip' );
161 $s = gzencode( $s, 6 );
165 $headers = headers_list();
167 foreach ( $headers as
$header ) {
168 $headerName = strtolower( substr(
$header, 0, 5 ) );
169 if ( $headerName ==
'vary:' ) {
175 header(
'Vary: Accept-Encoding' );
195 private static function emitContentLength( $length ) {
196 if ( headers_sent() ) {
197 wfDebug( __METHOD__ .
"() headers already sent" );
202 in_array( http_response_code(), [ 200, 404 ],
true ) ||
203 ( $_SERVER[
'SERVER_PROTOCOL'] ??
null ) ===
'HTTP/1.0'
205 header(
"Content-Length: $length" );
211class_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()