MediaWiki master
PHPVersionCheck.php
Go to the documentation of this file.
1<?php
6
7// phpcs:disable PSR12.Properties.ConstantVisibility.NotFound
8
9namespace MediaWiki;
10
12
36 const MW_VERSION = '1.48';
37
42 private static $functionsExtensionsMapping = [
43 'mb_substr' => 'mbstring',
44 'xml_parser_create' => 'xml',
45 'ctype_digit' => 'ctype',
46 'iconv' => 'iconv',
47 'mime_content_type' => 'fileinfo',
48 'intl_is_failure' => 'intl',
49 ];
50
54 private $format;
55
59 private $scriptPath;
60
65 public function __construct( $format = 'text', $scriptPath = '/' ) {
66 $this->format = $format;
67 $this->scriptPath = $scriptPath;
68 }
69
73 private function checkRequiredPHPVersion() {
74 // NOTE: Keep this in sync with composer.json and ScopeStructureTest.php
75 $minimumVersion = '8.3.0';
76
90 $knownBad = [
91 ];
92
93 $passes = version_compare( PHP_VERSION, $minimumVersion, '>=' );
94
95 $versionString = "PHP $minimumVersion or higher";
96
97 // Left as a programmatic check to make it easier to update.
98 if ( count( $knownBad ) ) {
99 $versionString .= ' (and not ' . implode( ', ', array_values( $knownBad ) ) . ')';
100
101 foreach ( $knownBad as $range ) {
102 // As we don't have composer at this point, we have to do our own version range checking.
103 if ( strpos( $range, '-' ) ) {
104 $passes = $passes && !(
105 version_compare( PHP_VERSION, trim( strstr( $range, '-', true ) ), '>=' )
106 && version_compare( PHP_VERSION, trim( substr( strstr( $range, '-', false ), 1 ) ), '<' )
107 );
108 } else {
109 $passes = $passes && version_compare( PHP_VERSION, trim( $range ), '<>' );
110 }
111 }
112 }
113
114 if ( !$passes ) {
115 $mwVersion = self::MW_VERSION;
116 $cliText = "Error: You are using an unsupported PHP version (PHP " . PHP_VERSION . ").\n"
117 . "MediaWiki $mwVersion needs $versionString.\n\nCheck if you might have a newer "
118 . "PHP executable with a different name.\n\n";
119
120 $web = [];
121 $web['intro'] = "MediaWiki $mwVersion requires $versionString; you are using PHP "
122 . PHP_VERSION . ".";
123
124 $web['longTitle'] = "Supported PHP versions";
125 // phpcs:disable Generic.Files.LineLength
126 $web['longHtml'] = <<<HTML
127 <p>
128 Please consider <a href="https://www.php.net/downloads.php">upgrading your copy of PHP</a>.
129 PHP versions less than v8.2.0 are no longer <a href="https://www.php.net/supported-versions.php">supported</a>
130 by the PHP Group and will not receive security or bugfix updates.
131 </p>
132 <p>
133 If for some reason you are unable to upgrade your PHP version, you will need to
134 <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version of
135 MediaWiki from our website. See our
136 <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
137 for details of which versions are compatible with prior versions of PHP.
138 </p>
139HTML;
140 // phpcs:enable Generic.Files.LineLength
141 $this->triggerError(
142 $web,
143 $cliText
144 );
145 }
146 }
147
151 private function checkVendorExistence() {
152 if ( !file_exists( __DIR__ . '/../vendor/autoload.php' ) ) {
153 $cliText = "Error: You are missing some dependencies. \n"
154 . "MediaWiki has dependencies that need to be installed via Composer\n"
155 . "or from a separate repository. Please see\n"
156 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
157 . "for help with installing them.";
158
159 $web = [];
160 $web['intro'] = "Installing some dependencies is required.";
161 $web['longTitle'] = 'Dependencies';
162 // phpcs:disable Generic.Files.LineLength
163 $web['longHtml'] = <<<HTML
164 <p>
165 MediaWiki has dependencies that need to be installed via Composer
166 or from a separate repository. Please see the
167 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">instructions
168 for installing external libraries</a> on MediaWiki.org.
169 </p>
170HTML;
171 // phpcs:enable Generic.Files.LineLength
172
173 $this->triggerError( $web, $cliText );
174 }
175 }
176
180 private function checkExtensionExistence() {
181 $missingExtensions = [];
182 foreach ( self::$functionsExtensionsMapping as $function => $extension ) {
183 if ( !function_exists( $function ) ) {
184 $missingExtensions[] = [ $extension ];
185 }
186 }
187
188 // Special case: either of those is required, but only on 32-bit systems (T391169)
189 if ( PHP_INT_SIZE < 8 && !extension_loaded( 'gmp' ) && !extension_loaded( 'bcmath' ) ) {
190 $missingExtensions[] = [ 'bcmath', 'gmp' ];
191 }
192
193 if ( $missingExtensions ) {
194 $missingExtText = '';
195 $missingExtHtml = '';
196 $baseUrl = 'https://www.php.net';
197 foreach ( $missingExtensions as $extNames ) {
198 $plaintextLinks = [];
199 $htmlLinks = [];
200 foreach ( $extNames as $ext ) {
201 $plaintextLinks[] = "$ext <$baseUrl/$ext>";
202 $htmlLinks[] = "<b>$ext</b> (<a href=\"$baseUrl/$ext\">more information</a>)";
203 }
204
205 $missingExtText .= ' * ' . implode( ' or ', $plaintextLinks ) . "\n";
206 $missingExtHtml .= "<li>" . implode( ' or ', $htmlLinks ) . "</li>";
207 }
208
209 $cliText = "Error: Missing one or more required PHP extensions. Please see\n"
210 . "https://www.mediawiki.org/wiki/Manual:Installation_requirements#PHP\n"
211 . "for help with installing them.\n"
212 . "Please install or enable:\n" . $missingExtText;
213
214 $web = [];
215 $web['intro'] = "Installing some PHP extensions is required.";
216 $web['longTitle'] = 'Required PHP extensions';
217 $web['longHtml'] = <<<HTML
218 <p>
219 You are missing one or more extensions to PHP that MediaWiki requires to run. Please see the
220 <a href="https://www.mediawiki.org/wiki/Manual:Installation_requirements#PHP">PHP
221 installation requirements</a> on MediaWiki.org.
222 </p>
223 <p>Please install or enable:</p>
224 <ul>
225 $missingExtHtml
226 </ul>
227HTML;
228
229 $this->triggerError( $web, $cliText );
230 }
231 }
232
236 private function outputHTMLHeader() {
237 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
238
239 HttpHeader::send( "$protocol 500 MediaWiki configuration Error" );
240 // Don't cache error pages! They cause no end of trouble...
241 HttpHeader::send( 'Cache-Control: no-cache' );
242 }
243
252 private function getIndexErrorOutput( $introText, $longTitle, $longHtml ) {
253 $encLogo =
254 htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
255 'resources/assets/mediawiki.png' );
256
257 $introHtml = htmlspecialchars( $introText );
258 $longTitleHtml = htmlspecialchars( $longTitle );
259
260 HttpHeader::send( 'Content-type: text/html; charset=UTF-8' );
261
262 $mwVersion = self::MW_VERSION;
263 $finalOutput = <<<HTML
264<!DOCTYPE html>
265<html lang="en" dir="ltr">
266 <head>
267 <meta charset="UTF-8" />
268 <title>MediaWiki {$mwVersion}</title>
269 <style media="screen">
270 body {
271 color: #000;
272 background-color: #fff;
273 font-family: sans-serif;
274 padding: 2em;
275 text-align: center;
276 }
277 p, img, h1, h2, ul {
278 text-align: left;
279 margin: 0.5em 0 1em;
280 }
281 h1 {
282 font-size: 120%;
283 }
284 h2 {
285 font-size: 110%;
286 }
287 </style>
288 </head>
289 <body>
290 <img src="{$encLogo}" alt="The MediaWiki logo" />
291 <h1>MediaWiki {$mwVersion} internal error</h1>
292 <p>
293 {$introHtml}
294 </p>
295 <h2>{$longTitleHtml}</h2>
296 {$longHtml}
297 </body>
298</html>
299HTML;
300
301 return $finalOutput;
302 }
303
316 private function triggerError( array $web, $cliText ) {
317 if ( $this->format === 'html' ) {
318 // Used by index.php and mw-config/index.php
319 $this->outputHTMLHeader();
320 $finalOutput = $this->getIndexErrorOutput(
321 $web['intro'],
322 $web['longTitle'],
323 $web['longHtml']
324 );
325 } else {
326 // Used by Maintenance.php (CLI)
327 $finalOutput = $cliText;
328 }
329
330 echo "$finalOutput\n";
331 die( 1 );
332 }
333
340 public function run() {
341 $this->checkRequiredPHPVersion();
342 $this->checkVendorExistence();
343 $this->checkExtensionExistence();
344 }
345}
Check PHP Version, as well as for composer dependencies in entry points, and display something vaguel...
__construct( $format='text', $scriptPath='/')
run()
Check PHP version and that external dependencies are installed, and display an informative error if e...
const MW_VERSION
The number of the MediaWiki version used.
This class wraps the native header functions in such a way that no headers are sent during PHPUnit te...