MediaWiki REL1_39
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.39';
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.4.3';
82
96 $knownBad = array();
97
98 $passes = version_compare( PHP_VERSION, $minimumVersion, '>=' );
99
100 $versionString = "PHP $minimumVersion or higher";
101
102 // Left as a programmatic check to make it easier to update.
103 if ( count( $knownBad ) ) {
104 $versionString .= ' (and not ' . implode( ', ', array_values( $knownBad ) ) . ')';
105
106 foreach ( $knownBad as $task => $range ) {
107 // As we don't have composer at this point, we have to do our own version range checking.
108 if ( strpos( $range, '-' ) ) {
109 $passes = $passes && !(
110 version_compare( PHP_VERSION, trim( strstr( $range, '-', true ) ), '>=' )
111 && version_compare( PHP_VERSION, trim( substr( strstr( $range, '-', false ), 1 ) ), '<' )
112 );
113 } else {
114 $passes = $passes && version_compare( PHP_VERSION, trim( $range ), '<>' );
115 }
116 }
117 }
118
119 if ( !$passes ) {
120 $cliText = "Error: You are using an unsupported PHP version (PHP " . PHP_VERSION . ").\n"
121 . "MediaWiki $this->mwVersion needs $versionString.\n\nCheck if you might have a newer "
122 . "PHP executable with a different name.\n\n";
123
124 $web = array();
125 $web['intro'] = "MediaWiki $this->mwVersion requires $versionString; you are using PHP "
126 . PHP_VERSION . ".";
127
128 $web['longTitle'] = "Supported PHP versions";
129 // phpcs:disable Generic.Files.LineLength
130 $web['longHtml'] = <<<HTML
131 <p>
132 Please consider <a href="https://www.php.net/downloads.php">upgrading your copy of PHP</a>.
133 PHP versions less than v8.1.0 are no longer <a href="https://www.php.net/supported-versions.php">supported</a>
134 by the PHP Group and will not receive security or bugfix updates.
135 </p>
136 <p>
137 If for some reason you are unable to upgrade your PHP version, you will need to
138 <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version of
139 MediaWiki from our website. See our
140 <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
141 for details of which versions are compatible with prior versions of PHP.
142 </p>
143HTML;
144 // phpcs:enable Generic.Files.LineLength
145 $this->triggerError(
146 $web,
147 $cliText
148 );
149 }
150 }
151
156 if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
157 $cliText = "Error: You are missing some external dependencies. \n"
158 . "MediaWiki also has some external dependencies that need to be installed\n"
159 . "via composer or from a separate git repo. Please see\n"
160 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
161 . "for help on installing the required components.";
162
163 $web = array();
164 $web['intro'] = "Installing some external dependencies (e.g. via composer) is required.";
165 $web['longTitle'] = 'External dependencies';
166 // phpcs:disable Generic.Files.LineLength
167 $web['longHtml'] = <<<HTML
168 <p>
169 MediaWiki also has some external dependencies that need to be installed via
170 composer or from a separate git repo. Please see the
171 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">instructions
172 for installing libraries</a> on mediawiki.org for help on installing the required components.
173 </p>
174HTML;
175 // phpcs:enable Generic.Files.LineLength
176
177 $this->triggerError( $web, $cliText );
178 }
179 }
180
185 $missingExtensions = array();
186 foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
187 if ( !function_exists( $function ) ) {
188 $missingExtensions[] = $extension;
189 }
190 }
191
192 if ( $missingExtensions ) {
193 $missingExtText = '';
194 $missingExtHtml = '';
195 $baseUrl = 'https://www.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>";
200 }
201
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;
205
206 $web = array();
207 $web['intro'] = "Installing some PHP extensions is required.";
208 $web['longTitle'] = 'Required components';
209 $web['longHtml'] = <<<HTML
210 <p>
211 You are missing a required extension to PHP that MediaWiki
212 requires to run. Please install:
213 </p>
214 <ul>
215 $missingExtHtml
216 </ul>
217HTML;
218
219 $this->triggerError( $web, $cliText );
220 }
221 }
222
226 function outputHTMLHeader() {
227 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
228
229 header( "$protocol 500 MediaWiki configuration Error" );
230 // Don't cache error pages! They cause no end of trouble...
231 header( 'Cache-control: none' );
232 header( 'Pragma: no-cache' );
233 }
234
243 function getIndexErrorOutput( $introText, $longTitle, $longHtml ) {
244 $encLogo =
245 htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
246 'resources/assets/mediawiki.png' );
247
248 $introHtml = htmlspecialchars( $introText );
249 $longTitleHtml = htmlspecialchars( $longTitle );
250
251 header( 'Content-type: text/html; charset=UTF-8' );
252
253 $finalOutput = <<<HTML
254<!DOCTYPE html>
255<html lang="en" dir="ltr">
256 <head>
257 <meta charset="UTF-8" />
258 <title>MediaWiki {$this->mwVersion}</title>
259 <style media="screen">
260 body {
261 color: #000;
262 background-color: #fff;
263 font-family: sans-serif;
264 padding: 2em;
265 text-align: center;
266 }
267 p, img, h1, h2, ul {
268 text-align: left;
269 margin: 0.5em 0 1em;
270 }
271 h1 {
272 font-size: 120%;
273 }
274 h2 {
275 font-size: 110%;
276 }
277 </style>
278 </head>
279 <body>
280 <img src="{$encLogo}" alt="The MediaWiki logo" />
281 <h1>MediaWiki {$this->mwVersion} internal error</h1>
282 <p>
283 {$introHtml}
284 </p>
285 <h2>{$longTitleHtml}</h2>
286 {$longHtml}
287 </body>
288</html>
289HTML;
290
291 return $finalOutput;
292 }
293
308 function triggerError( $web, $cliText ) {
309 if ( $this->format === 'html' ) {
310 // Used by index.php and mw-config/index.php
311 $this->outputHTMLHeader();
312 $finalOutput = $this->getIndexErrorOutput(
313 $web['intro'],
314 $web['longTitle'],
315 $web['longHtml']
316 );
317 } else {
318 // Used by Maintenance.php (CLI)
319 $finalOutput = $cliText;
320 }
321
322 echo "$finalOutput\n";
323 die( 1 );
324 }
325}
326
334function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
335 $phpVersionCheck = new PHPVersionCheck();
336 $phpVersionCheck->setFormat( $format );
337 $phpVersionCheck->setScriptPath( $scriptPath );
338 $phpVersionCheck->checkRequiredPHPVersion();
339 $phpVersionCheck->checkVendorExistence();
340 $phpVersionCheck->checkExtensionExistence();
341}
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