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