3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 // phpcs:disable Generic.Arrays.DisallowLongArraySyntax,PSR2.Classes.PropertyDeclaration,MediaWiki.Usage.DirUsage
22 // phpcs:disable Squiz.Scope.MemberVarScope.Missing,Squiz.Scope.MethodScope.Missing
23 // @phan-file-suppress PhanPluginDuplicateConditionalNullCoalescing
25 * Check PHP Version, as well as for composer dependencies in entry points,
26 * and display something vaguely comprehensible in the event of a totally
27 * unrecoverable error.
29 * @note Since we can't rely on anything external, the minimum PHP versions
30 * and MW current version are hardcoded in this class.
32 * @note This class uses setter methods instead of a constructor so that
33 * it can be compatible with PHP 4, PHP 5 and PHP 7 (without warnings).
35 class PHPVersionCheck
{
36 /* @var string The number of the MediaWiki version used. */
37 var $mwVersion = '1.34';
39 /* @var array A mapping of PHP functions to PHP extensions. */
40 var $functionsExtensionsMapping = array(
41 'mb_substr' => 'mbstring',
42 'xml_parser_create' => 'xml',
43 'ctype_digit' => 'ctype',
44 'json_decode' => 'json',
46 'mime_content_type' => 'fileinfo',
50 * @var string $format The format used for errors. One of "text" or "html"
55 * @var string $scriptPath
57 var $scriptPath = '/';
60 * Set the format used for errors.
62 * @param string $format One of "text" or "html"
64 function setFormat( $format ) {
65 $this->format
= $format;
69 * Set the script path used for images in HTML-formatted errors.
71 * @param string $scriptPath
73 function setScriptPath( $scriptPath ) {
74 $this->scriptPath
= $scriptPath;
78 * Return the version of the installed PHP implementation.
80 * @param string|false $impl By default, the function returns the info of the currently installed
81 * PHP implementation. Using this parameter the caller can decide, what version info will be
82 * returned. Valid values: HHVM, PHP
83 * @return array An array of information about the PHP implementation, containing:
84 * - 'version': The version of the PHP implementation (specific to the implementation, not
85 * the version of the implemented PHP version)
86 * - 'implementation': The name of the implementation used
87 * - 'vendor': The development group, vendor or developer of the implementation.
88 * - 'upstreamSupported': The minimum version of the implementation supported by the named vendor.
89 * - 'minSupported': The minimum version supported by MediaWiki
90 * - 'upgradeURL': The URL to the website of the implementation that contains
91 * upgrade/installation instructions.
93 function getPHPInfo( $impl = false ) {
95 ( defined( 'HHVM_VERSION' ) && $impl !== 'PHP' ) ||
99 'implementation' => 'HHVM',
100 'version' => defined( 'HHVM_VERSION' ) ? HHVM_VERSION
: 'undefined',
101 'vendor' => 'Facebook',
102 'upstreamSupported' => '3.18.5',
103 'minSupported' => '3.18.5',
104 'upgradeURL' => 'https://docs.hhvm.com/hhvm/installation/introduction',
108 'implementation' => 'PHP',
109 'version' => PHP_VERSION
,
110 'vendor' => 'the PHP Group',
111 'upstreamSupported' => '7.1.0',
112 'minSupported' => '7.2.0',
113 'upgradeURL' => 'https://www.php.net/downloads.php',
118 * Displays an error, if the installed PHP version does not meet the minimum requirement.
120 function checkRequiredPHPVersion() {
121 $phpInfo = $this->getPHPInfo();
122 $minimumVersion = $phpInfo['minSupported'];
123 $otherInfo = $this->getPHPInfo( $phpInfo['implementation'] === 'HHVM' ?
'PHP' : 'HHVM' );
124 if ( version_compare( $phpInfo['version'], $minimumVersion ) < 0 ) {
125 $shortText = "MediaWiki $this->mwVersion requires at least {$phpInfo['implementation']}"
126 . " version $minimumVersion or {$otherInfo['implementation']} version "
127 . "{$otherInfo['minSupported']}, you are using {$phpInfo['implementation']} "
128 . "{$phpInfo['version']}.";
130 $longText = "Error: You might be using an older {$phpInfo['implementation']} version "
131 . "({$phpInfo['implementation']} {$phpInfo['version']}). \n"
132 . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
133 . " $minimumVersion or higher or {$otherInfo['implementation']} version "
134 . "{$otherInfo['minSupported']}.\n\nCheck if you have a"
135 . " newer PHP executable with a different name.\n\n";
137 // phpcs:disable Generic.Files.LineLength
139 Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
140 {$phpInfo['implementation']}</a>.
141 {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
142 longer supported by {$phpInfo['vendor']} and will not receive
143 security or bugfix updates.
146 If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
147 you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
148 older version of MediaWiki from our website.
149 See our <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
150 for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
152 // phpcs:enable Generic.Files.LineLength
154 "Supported {$phpInfo['implementation']} versions",
163 * Displays an error, if the vendor/autoload.php file could not be found.
165 function checkVendorExistence() {
166 if ( !file_exists( dirname( __FILE__
) . '/../vendor/autoload.php' ) ) {
167 $shortText = "Installing some external dependencies (e.g. via composer) is required.";
169 $longText = "Error: You are missing some external dependencies. \n"
170 . "MediaWiki now also has some external dependencies that need to be installed\n"
171 . "via composer or from a separate git repo. Please see\n"
172 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
173 . "for help on installing the required components.";
175 // phpcs:disable Generic.Files.LineLength
177 MediaWiki now also has some external dependencies that need to be installed via
178 composer or from a separate git repo. Please see
179 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
180 for help on installing the required components.
182 // phpcs:enable Generic.Files.LineLength
184 $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
189 * Displays an error, if a PHP extension does not exist.
191 function checkExtensionExistence() {
192 $missingExtensions = array();
193 foreach ( $this->functionsExtensionsMapping
as $function => $extension ) {
194 if ( !function_exists( $function ) ) {
195 $missingExtensions[] = $extension;
199 if ( $missingExtensions ) {
200 $shortText = "Installing some PHP extensions is required.";
202 $missingExtText = '';
203 $missingExtHtml = '';
204 $baseUrl = 'https://www.php.net';
205 foreach ( $missingExtensions as $ext ) {
206 $missingExtText .= " * $ext <$baseUrl/$ext>\n";
207 $missingExtHtml .= "<li><b>$ext</b> "
208 . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
211 $cliText = "Error: Missing one or more required components of PHP.\n"
212 . "You are missing a required extension to PHP that MediaWiki needs.\n"
213 . "Please install:\n" . $missingExtText;
216 You are missing a required extension to PHP that MediaWiki
217 requires to run. Please install:
223 $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
228 * Output headers that prevents error pages to be cached.
230 function outputHTMLHeader() {
231 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ?
$_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
233 header( "$protocol 500 MediaWiki configuration Error" );
234 // Don't cache error pages! They cause no end of trouble...
235 header( 'Cache-control: none' );
236 header( 'Pragma: no-cache' );
240 * Returns an error page, which is suitable for output to the end user via a web browser.
242 * @param string $title
243 * @param string $longHtml
244 * @param string $shortText
247 function getIndexErrorOutput( $title, $longHtml, $shortText ) {
249 htmlspecialchars( str_replace( '//', '/', $this->scriptPath
. '/' ) .
250 'resources/assets/mediawiki.png' );
251 $shortHtml = htmlspecialchars( $shortText );
253 header( 'Content-type: text/html; charset=UTF-8' );
255 $finalOutput = <<<HTML
257 <html lang="en" dir="ltr">
259 <meta charset="UTF-8" />
260 <title>MediaWiki {$this->mwVersion}</title>
261 <style media='screen'>
264 background-color: #fff;
265 font-family: sans-serif;
282 <img src="{$encLogo}" alt='The MediaWiki logo' />
283 <h1>MediaWiki {$this->mwVersion} internal error</h1>
301 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
302 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
303 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
304 * no longer need to be).
306 * Calling this function kills execution immediately.
308 * @param string $title HTML code to be put within an <h2> tag
309 * @param string $shortText
310 * @param string $longText
311 * @param string $longHtml
313 function triggerError( $title, $shortText, $longText, $longHtml ) {
314 if ( $this->format
=== 'html' ) {
315 // Used by index.php and mw-config/index.php
316 $this->outputHTMLHeader();
317 $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
319 // Used by Maintenance.php (CLI)
320 $finalOutput = $longText;
323 echo "$finalOutput\n";
329 * Check PHP version and that external dependencies are installed, and
330 * display an informative error if either condition is not satisfied.
332 * @param string $format One of "text" or "html"
333 * @param string $scriptPath Used when an error is formatted as HTML.
335 function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
336 $phpVersionCheck = new PHPVersionCheck();
337 $phpVersionCheck->setFormat( $format );
338 $phpVersionCheck->setScriptPath( $scriptPath );
339 $phpVersionCheck->checkRequiredPHPVersion();
340 $phpVersionCheck->checkVendorExistence();
341 $phpVersionCheck->checkExtensionExistence();