MediaWiki REL1_37
PHPVersionCheck.php
Go to the documentation of this file.
1<?php
21// phpcs:disable Generic.Arrays.DisallowLongArraySyntax,PSR2.Classes.PropertyDeclaration,MediaWiki.Usage.DirUsage
22// phpcs:disable Squiz.Scope.MemberVarScope.Missing,Squiz.Scope.MethodScope.Missing
36 var $mwVersion = '1.37';
37
40 'mb_substr' => 'mbstring',
41 'xml_parser_create' => 'xml',
42 'ctype_digit' => 'ctype',
43 'json_decode' => 'json',
44 'iconv' => 'iconv',
45 'mime_content_type' => 'fileinfo',
46 'intl_is_failure' => 'intl',
47 );
48
52 var $format = 'text';
53
57 var $scriptPath = '/';
58
64 function setFormat( $format ) {
65 $this->format = $format;
66 }
67
74 $this->scriptPath = $scriptPath;
75 }
76
81 $minimumVersion = '7.3.19';
82
96 $knownBad = array(
97 // https://bugs.php.net/bug.php?id=79174 as a regression from https://bugs.php.net/bug.php?id=78929
98 'T243667, T291127' => '7.4.0 - 7.4.2'
99 );
100
101 $passes = version_compare( PHP_VERSION, $minimumVersion, '>=' );
102
103 $versionString = "PHP $minimumVersion or higher";
104
105 // Left as a programmatic check to make it easier to update.
106 if ( count( $knownBad ) ) {
107 $versionString .= ' (and not ' . implode( ', ', array_values( $knownBad ) ) . ')';
108
109 foreach ( $knownBad as $task => $range ) {
110 // As we don't have composer at this point, we have to do our own version range checking.
111 if ( strpos( $range, '-' ) ) {
112 $passes = $passes && !(
113 version_compare( PHP_VERSION, trim( strstr( $range, '-', true ) ), '>=' )
114 && version_compare( PHP_VERSION, trim( substr( strstr( $range, '-', false ), 1 ) ), '<' )
115 );
116 } else {
117 $passes = $passes && version_compare( PHP_VERSION, trim( $range ), '<>' );
118 }
119 }
120 }
121
122 if ( !$passes ) {
123 $cliText = "Error: You are using an unsupported PHP version (PHP " . PHP_VERSION . ").\n"
124 . "MediaWiki $this->mwVersion needs $versionString.\n\nCheck if you might have a newer "
125 . "PHP executable with a different name.\n\n";
126
127 $web = array();
128 $web['intro'] = "MediaWiki $this->mwVersion requires $versionString; you are using PHP "
129 . PHP_VERSION . ".";
130
131 $web['longTitle'] = "Supported PHP versions";
132 // phpcs:disable Generic.Files.LineLength
133 $web['longHtml'] = <<<HTML
134 <p>
135 Please consider <a href="https://www.php.net/downloads.php">upgrading your copy of PHP</a>.
136 PHP versions less than v7.3.0 are no longer supported by the PHP Group and will not receive
137 security or bugfix updates.
138 </p>
139 <p>
140 If for some reason you are unable to upgrade your PHP version, you will need to
141 <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version of
142 MediaWiki from our website. See our
143 <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
144 for details of which versions are compatible with prior versions of PHP.
145 </p>
146HTML;
147 // phpcs:enable Generic.Files.LineLength
148 $this->triggerError(
149 $web,
150 $cliText
151 );
152 }
153 }
154
159 if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
160 $cliText = "Error: You are missing some external dependencies. \n"
161 . "MediaWiki also has some external dependencies that need to be installed\n"
162 . "via composer or from a separate git repo. Please see\n"
163 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
164 . "for help on installing the required components.";
165
166 $web = array();
167 $web['intro'] = "Installing some external dependencies (e.g. via composer) is required.";
168 $web['longTitle'] = 'External dependencies';
169 // phpcs:disable Generic.Files.LineLength
170 $web['longHtml'] = <<<HTML
171 <p>
172 MediaWiki also has some external dependencies that need to be installed via
173 composer or from a separate git repo. Please see the
174 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">instructions
175 for installing libraries</a> on mediawiki.org for help on installing the required components.
176 </p>
177HTML;
178 // phpcs:enable Generic.Files.LineLength
179
180 $this->triggerError( $web, $cliText );
181 }
182 }
183
188 $missingExtensions = array();
189 foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
190 if ( !function_exists( $function ) ) {
191 $missingExtensions[] = $extension;
192 }
193 }
194
195 if ( $missingExtensions ) {
196 $missingExtText = '';
197 $missingExtHtml = '';
198 $baseUrl = 'https://www.php.net';
199 foreach ( $missingExtensions as $ext ) {
200 $missingExtText .= " * $ext <$baseUrl/$ext>\n";
201 $missingExtHtml .= "<li><b>$ext</b> "
202 . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
203 }
204
205 $cliText = "Error: Missing one or more required components of PHP.\n"
206 . "You are missing a required extension to PHP that MediaWiki needs.\n"
207 . "Please install:\n" . $missingExtText;
208
209 $web = array();
210 $web['intro'] = "Installing some PHP extensions is required.";
211 $web['longTitle'] = 'Required components';
212 $web['longHtml'] = <<<HTML
213 <p>
214 You are missing a required extension to PHP that MediaWiki
215 requires to run. Please install:
216 </p>
217 <ul>
218 $missingExtHtml
219 </ul>
220HTML;
221
222 $this->triggerError( $web, $cliText );
223 }
224 }
225
229 function outputHTMLHeader() {
230 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
231
232 header( "$protocol 500 MediaWiki configuration Error" );
233 // Don't cache error pages! They cause no end of trouble...
234 header( 'Cache-control: none' );
235 header( 'Pragma: no-cache' );
236 }
237
246 function getIndexErrorOutput( $introText, $longTitle, $longHtml ) {
247 $encLogo =
248 htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
249 'resources/assets/mediawiki.png' );
250
251 $introHtml = htmlspecialchars( $introText );
252 $longTitleHtml = htmlspecialchars( $longTitle );
253
254 header( 'Content-type: text/html; charset=UTF-8' );
255
256 $finalOutput = <<<HTML
257<!DOCTYPE html>
258<html lang="en" dir="ltr">
259 <head>
260 <meta charset="UTF-8" />
261 <title>MediaWiki {$this->mwVersion}</title>
262 <style media="screen">
263 body {
264 color: #000;
265 background-color: #fff;
266 font-family: sans-serif;
267 padding: 2em;
268 text-align: center;
269 }
270 p, img, h1, h2, ul {
271 text-align: left;
272 margin: 0.5em 0 1em;
273 }
274 h1 {
275 font-size: 120%;
276 }
277 h2 {
278 font-size: 110%;
279 }
280 </style>
281 </head>
282 <body>
283 <img src="{$encLogo}" alt="The MediaWiki logo" />
284 <h1>MediaWiki {$this->mwVersion} internal error</h1>
285 <p>
286 {$introHtml}
287 </p>
288 <h2>{$longTitleHtml}</h2>
289 {$longHtml}
290 </body>
291</html>
292HTML;
293
294 return $finalOutput;
295 }
296
311 function triggerError( $web, $cliText ) {
312 if ( $this->format === 'html' ) {
313 // Used by index.php and mw-config/index.php
314 $this->outputHTMLHeader();
315 $finalOutput = $this->getIndexErrorOutput(
316 $web['intro'],
317 $web['longTitle'],
318 $web['longHtml']
319 );
320 } else {
321 // Used by Maintenance.php (CLI)
322 $finalOutput = $cliText;
323 }
324
325 echo "$finalOutput\n";
326 die( 1 );
327 }
328}
329
337function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
338 $phpVersionCheck = new PHPVersionCheck();
339 $phpVersionCheck->setFormat( $format );
340 $phpVersionCheck->setScriptPath( $scriptPath );
341 $phpVersionCheck->checkRequiredPHPVersion();
342 $phpVersionCheck->checkVendorExistence();
343 $phpVersionCheck->checkExtensionExistence();
344}
wfEntryPointCheck( $format='text', $scriptPath='/')
Check PHP version and that external dependencies are installed, and display an informative error if e...
Check PHP Version, as well as for composer dependencies in entry points, and display something vaguel...
setFormat( $format)
Set the format used for errors.
string $mwVersion
The number of the MediaWiki version used.
outputHTMLHeader()
Output headers that prevents error pages to be cached.
setScriptPath( $scriptPath)
Set the script path used for images in HTML-formatted errors.
triggerError( $web, $cliText)
Display something vaguely comprehensible in the event of a totally unrecoverable error.
getIndexErrorOutput( $introText, $longTitle, $longHtml)
Returns an error page, which is suitable for output to the end user via a web browser.
checkExtensionExistence()
Displays an error, if a PHP extension does not exist.
checkRequiredPHPVersion()
Displays an error, if the installed PHP version does not meet the minimum requirement.
string[] $functionsExtensionsMapping
A mapping of PHP functions to PHP extensions.
checkVendorExistence()
Displays an error, if the vendor/autoload.php file could not be found.
string $format
The format used for errors.
A helper class for throttling authentication attempts.
if(!is_readable( $file)) $ext
Definition router.php:48