MediaWiki
master
Toggle main menu visibility
OutputHandler.php
Go to the documentation of this file.
1
<?php
8
9
namespace
MediaWiki\Output;
10
11
use MediaWiki\Logger\LoggerFactory;
12
use Wikimedia\Http\HttpHeader;
13
17
class
OutputHandler
{
28
public
static
function
handle
( $s, $phase ) {
29
// NOTE: Use global instead of MediaWikiServices::getMainConfig which is unsafe
30
// during Setup.php before reaching MediaWikiServices::allowGlobalInstance.
31
// For example, when serving ExtensionDependencyError after loading an extension
32
// with a missing dependency.
33
global
$wgDisableOutputCompression
;
34
35
// Don't send headers if output is being discarded (T278579)
36
if
( ( $phase & PHP_OUTPUT_HANDLER_CLEAN ) === PHP_OUTPUT_HANDLER_CLEAN ) {
37
$logger = LoggerFactory::getInstance(
'output'
);
38
$logger->debug( __METHOD__ .
" entrypoint={entry}; size={size}; phase=$phase"
, [
39
'entry'
=>
MW_ENTRY_POINT
,
40
'size'
=> strlen( $s ),
41
] );
42
43
return
$s;
44
}
45
46
// Check if a compression output buffer is already enabled via php.ini. Such
47
// buffers exists at the start of the request and are reflected by ob_get_level().
48
$phpHandlesCompression = (
49
ini_get(
'output_handler'
) ===
'ob_gzhandler'
||
50
ini_get(
'zlib.output_handler'
) ===
'ob_gzhandler'
||
51
!in_array(
52
strtolower( ini_get(
'zlib.output_compression'
) ),
53
[
''
,
'off'
,
'0'
]
54
)
55
);
56
57
if
(
58
// Compression is not already handled by an internal PHP buffer
59
!$phpHandlesCompression &&
60
// Compression is not disabled by the application entry point
61
!defined(
'MW_NO_OUTPUT_COMPRESSION'
) &&
62
// Compression is not disabled by site configuration
63
!
$wgDisableOutputCompression
64
) {
65
$s = self::handleGzip( $s );
66
}
67
68
if
(
69
// Response body length does not depend on internal PHP compression buffer
70
!$phpHandlesCompression &&
71
// Response body length does not depend on mangling by a custom buffer
72
!ini_get(
'output_handler'
) &&
73
!ini_get(
'zlib.output_handler'
)
74
) {
75
self::emitContentLength( strlen( $s ) );
76
}
77
78
return
$s;
79
}
80
91
private
static
function
findUriExtension() {
92
// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
93
if
( isset( $_SERVER[
'REQUEST_URI'
] ) ) {
94
// Strip the query string...
95
$path
= explode(
'?'
, $_SERVER[
'REQUEST_URI'
], 2 )[0];
96
} elseif ( isset( $_SERVER[
'SCRIPT_NAME'
] ) ) {
97
// Probably IIS. QUERY_STRING appears separately.
98
$path
= $_SERVER[
'SCRIPT_NAME'
];
99
}
else
{
100
// Can't get the path from the server? :(
101
return
''
;
102
}
103
104
$period = strrpos(
$path
,
'.'
);
105
if
( $period !==
false
) {
106
return
strtolower( substr(
$path
, $period ) );
107
}
108
return
''
;
109
}
110
121
private
static
function
handleGzip( $s ) {
122
if
( !function_exists(
'gzencode'
) ) {
123
wfDebug
( __METHOD__ .
"() skipping compression (gzencode unavailable)"
);
124
return
$s;
125
}
126
if
( HttpHeader::headersSent() ) {
127
wfDebug
( __METHOD__ .
"() skipping compression (headers already sent)"
);
128
return
$s;
129
}
130
131
$ext = self::findUriExtension();
132
if
( $ext ==
'.gz'
|| $ext ==
'.tgz'
) {
133
// Don't do gzip compression if the URL path ends in .gz or .tgz
134
// This confuses Safari and triggers a download of the page,
135
// even though it's pretty clearly labeled as viewable HTML.
136
// Bad Safari! Bad!
137
return
$s;
138
}
139
140
if
( $s ===
''
) {
141
// Do not gzip empty HTTP responses since that would not only bloat the body
142
// length, but it would result in invalid HTTP responses when the HTTP status code
143
// is one that must not be accompanied by a body (e.g. "204 No Content").
144
return
$s;
145
}
146
147
if
(
wfClientAcceptsGzip
() ) {
148
wfDebug
( __METHOD__ .
"() is compressing output"
);
149
HttpHeader::send(
'Content-Encoding: gzip'
);
150
$s = gzencode( $s, 6 );
151
}
152
153
// Set vary header if it hasn't been set already
154
if
( !preg_grep(
'/^Vary:/i'
, headers_list() ) ) {
155
HttpHeader::send(
'Vary: Accept-Encoding'
);
156
}
157
return
$s;
158
}
159
175
private
static
function
emitContentLength( $length ) {
176
if
( HttpHeader::headersSent() ) {
177
wfDebug
( __METHOD__ .
"() headers already sent"
);
178
return
;
179
}
180
181
if
(
182
in_array( http_response_code(), [ 200, 404 ],
true
) ||
183
( $_SERVER[
'SERVER_PROTOCOL'
] ??
null
) ===
'HTTP/1.0'
184
) {
185
HttpHeader::send(
"Content-Length: $length"
);
186
}
187
}
188
}
189
191
class_alias( OutputHandler::class,
'MediaWiki\\OutputHandler'
);
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition
GlobalFunctions.php:360
wfClientAcceptsGzip
wfClientAcceptsGzip( $force=false)
Whether the client accept gzip encoding.
Definition
GlobalFunctions.php:750
$path
$path
Definition
NoLocalSettings.php:14
MW_ENTRY_POINT
const MW_ENTRY_POINT
Definition
api.php:21
MediaWiki\Output\OutputHandler
Definition
OutputHandler.php:17
MediaWiki\Output\OutputHandler\handle
static handle( $s, $phase)
Standard output handler for use with ob_start.
Definition
OutputHandler.php:28
$wgDisableOutputCompression
$wgDisableOutputCompression
Config variable stub for the DisableOutputCompression setting, for use by phpdoc and IDEs.
Definition
config-vars.php:2031
includes
Output
OutputHandler.php
Generated on
for MediaWiki by
1.18.0