2 // @codingStandardsIgnoreFile Generic.Arrays.DisallowLongArraySyntax
3 // @codingStandardsIgnoreFile Generic.Files.LineLength
4 // @codingStandardsIgnoreFile MediaWiki.Usage.DirUsage.FunctionFound
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
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.
30 class PHPVersionCheck
{
31 /* @var string The number of the MediaWiki version used */
32 var $mwVersion = '1.30';
33 var $functionsExtensionsMapping = array(
34 'mb_substr' => 'mbstring',
35 'utf8_encode' => 'xml',
36 'ctype_digit' => 'ctype',
37 'json_decode' => 'json',
42 * @var string Which entry point we are protecting. One of:
46 * - mw-config/index.php
49 var $entryPoint = null;
52 * @param string $entryPoint Which entry point we are protecting. One of:
56 * - mw-config/index.php
60 function setEntryPoint( $entryPoint ) {
61 $this->entryPoint
= $entryPoint;
65 * Returns the version of the installed php implementation.
67 * @param string $impl By default, the function returns the info of the currently installed PHP
68 * implementation. Using this parameter the caller can decide, what version info will be
69 * returned. Valid values: HHVM, PHP
70 * @return array An array of information about the php implementation, containing:
71 * - 'version': The version of the php implementation (specific to the implementation, not
72 * the version of the implemented php version)
73 * - 'implementation': The name of the implementation used
74 * - 'vendor': The development group, vendor or developer of the implementation.
75 * - 'upstreamSupported': The minimum version of the implementation supported by the named vendor.
76 * - 'minSupported': The minimum version supported by MediWiki
77 * - 'upgradeURL': The URL to the website of the implementation that contains
78 * upgrade/installation instructions.
80 function getPHPInfo( $impl = false ) {
82 ( defined( 'HHVM_VERSION' ) && $impl !== 'PHP' ) ||
86 'implementation' => 'HHVM',
87 'version' => defined( 'HHVM_VERSION' ) ? HHVM_VERSION
: 'undefined',
88 'vendor' => 'Facebook',
89 'upstreamSupported' => '3.6.5',
90 'minSupported' => '3.6.5',
91 'upgradeURL' => 'https://docs.hhvm.com/hhvm/installation/introduction',
95 'implementation' => 'PHP',
96 'version' => PHP_VERSION
,
97 'vendor' => 'the PHP Group',
98 'upstreamSupported' => '5.5.0',
99 'minSupported' => '5.5.9',
100 'upgradeURL' => 'http://www.php.net/downloads.php',
105 * Displays an error, if the installed php version does not meet the minimum requirement.
109 function checkRequiredPHPVersion() {
110 $phpInfo = $this->getPHPInfo();
111 $minimumVersion = $phpInfo['minSupported'];
112 $otherInfo = $this->getPHPInfo( $phpInfo['implementation'] === 'HHVM' ?
'PHP' : 'HHVM' );
114 !function_exists( 'version_compare' )
115 ||
version_compare( $phpInfo['version'], $minimumVersion ) < 0
117 $shortText = "MediaWiki $this->mwVersion requires at least {$phpInfo['implementation']}"
118 . " version $minimumVersion or {$otherInfo['implementation']} version "
119 . "{$otherInfo['minSupported']}, you are using {$phpInfo['implementation']} "
120 . "{$phpInfo['version']}.";
122 $longText = "Error: You might be using an older {$phpInfo['implementation']} version. \n"
123 . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
124 . " $minimumVersion or higher or {$otherInfo['implementation']} version "
125 . "{$otherInfo['minSupported']}.\n\nCheck if you have a"
126 . " newer php executable with a different name, such as php5.\n\n";
129 Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
130 {$phpInfo['implementation']}</a>.
131 {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
132 longer supported by {$phpInfo['vendor']} and will not receive
133 security or bugfix updates.
136 If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
137 you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
138 older version of MediaWiki from our website.
139 See our<a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
140 for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
143 "Supported {$phpInfo['implementation']} versions",
152 * Displays an error, if the vendor/autoload.php file could not be found.
156 function checkVendorExistence() {
157 if ( !file_exists( dirname( __FILE__
) . '/../vendor/autoload.php' ) ) {
158 $shortText = "Installing some external dependencies (e.g. via composer) is required.";
160 $longText = "Error: You are missing some external dependencies. \n"
161 . "MediaWiki now 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.";
167 MediaWiki now also has some external dependencies that need to be installed via
168 composer or from a separate git repo. Please see
169 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
170 for help on installing the required components.
173 $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
178 * Displays an error, if a PHP extension does not exist.
182 function checkExtensionExistence() {
183 $missingExtensions = array();
184 foreach ( $this->functionsExtensionsMapping
as $function => $extension ) {
185 if ( !function_exists( $function ) ) {
186 $missingExtensions[] = $extension;
190 if ( $missingExtensions ) {
191 $shortText = "Installing some PHP extensions is required.";
193 $missingExtText = '';
194 $missingExtHtml = '';
195 $baseUrl = 'https://secure.php.net';
196 foreach ( $missingExtensions as $ext ) {
197 $missingExtText .= " * $ext <$baseUrl/$ext>\n";
198 $missingExtHtml .= "<li><b>$ext</b> "
199 . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
202 $cliText = "Error: Missing one or more required components of PHP.\n"
203 . "You are missing a required extension to PHP that MediaWiki needs.\n"
204 . "Please install:\n" . $missingExtText;
207 You are missing a required extension to PHP that MediaWiki
208 requires to run. Please install:
214 $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
219 * Output headers that prevents error pages to be cached.
221 function outputHTMLHeader() {
222 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ?
$_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
224 header( "$protocol 500 MediaWiki configuration Error" );
225 // Don't cache error pages! They cause no end of trouble...
226 header( 'Cache-control: none' );
227 header( 'Pragma: no-cache' );
231 * Returns an error page, which is suitable for output to the end user via a web browser.
238 function getIndexErrorOutput( $title, $longHtml, $shortText ) {
239 $pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
240 if ( $this->entryPoint
== 'mw-config/index.php' ) {
241 $dirname = dirname( $pathinfo['dirname'] );
243 $dirname = $pathinfo['dirname'];
246 htmlspecialchars( str_replace( '//', '/', $dirname . '/' ) .
247 'resources/assets/mediawiki.png' );
248 $shortHtml = htmlspecialchars( $shortText );
250 header( 'Content-type: text/html; charset=UTF-8' );
252 $finalOutput = <<<HTML
254 <html lang="en" dir="ltr">
256 <meta charset="UTF-8" />
257 <title>MediaWiki {$this->mwVersion}</title>
258 <style media='screen'>
261 background-color: #fff;
262 font-family: sans-serif;
279 <img src="{$encLogo}" alt='The MediaWiki logo' />
280 <h1>MediaWiki {$this->mwVersion} internal error</h1>
298 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
299 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
300 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
301 * no longer need to be).
303 * Calling this function kills execution immediately.
305 * @param string $title HTML code to be put within an <h2> tag
306 * @param string $shortText
307 * @param string $longText
308 * @param string $longHtml
310 function triggerError( $title, $shortText, $longText, $longHtml ) {
311 switch ( $this->entryPoint
) {
313 $finalOutput = $longText;
316 case 'mw-config/index.php':
317 $this->outputHTMLHeader();
318 $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
321 $this->outputHTMLHeader();
322 $finalOutput = "/* $shortText */";
325 $this->outputHTMLHeader();
326 // Handle everything that's not index.php
327 $finalOutput = $shortText;
330 echo "$finalOutput\n";
336 * Check php version and that external dependencies are installed, and
337 * display an informative error if either condition is not satisfied.
339 * @note Since we can't rely on anything, the minimum PHP versions and MW current
340 * version are hardcoded here
342 function wfEntryPointCheck( $entryPoint ) {
343 $phpVersionCheck = new PHPVersionCheck();
344 $phpVersionCheck->setEntryPoint( $entryPoint );
345 $phpVersionCheck->checkRequiredPHPVersion();
346 $phpVersionCheck->checkVendorExistence();
347 $phpVersionCheck->checkExtensionExistence();