Debian Codegraph @

includes/PHPVersionCheck.php

raw symbols SCIP
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 // phpcs:disable Generic.Arrays.DisallowLongArraySyntax,PSR2.Classes.PropertyDeclaration,MediaWiki.Usage.DirUsage
22 // phpcs:disable Squiz.Scope.MemberVarScope.Missing,Squiz.Scope.MethodScope.Missing
23 // phpcs:disable MediaWiki.Usage.StaticClosure.StaticClosure
24 /**
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.
28 *
29 * @note Since we can't rely on anything external, the minimum PHP versions
30 * and MW current version are hardcoded in this class.
31 *
32 * @note This class uses setter methods instead of a constructor so that
33 * it can be compatible with PHP 4 through PHP 8 (without warnings).
34 */
35 class PHPVersionCheck {
36 /** @var string The number of the MediaWiki version used. If you're updating MW_VERSION in Defines.php, you must also update this value. */
37 var $mwVersion = '1.43';
38
39 /** @var string[] 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',
45 'iconv' => 'iconv',
46 'mime_content_type' => 'fileinfo',
47 'intl_is_failure' => 'intl',
48 );
49
50 /**
51 * @var string The format used for errors. One of "text" or "html"
52 */
53 var $format = 'text';
54
55 /**
56 * @var string
57 */
58 var $scriptPath = '/';
59
60 /**
61 * Set the format used for errors.
62 *
63 * @param string $format One of "text" or "html"
64 */
65 function setFormat( $format ) {
66 $this->format = $format;
67 }
68
69 /**
70 * Set the script path used for images in HTML-formatted errors.
71 *
72 * @param string $scriptPath
73 */
74 function setScriptPath( $scriptPath ) {
75 $this->scriptPath = $scriptPath;
76 }
77
78 /**
79 * Displays an error, if the installed PHP version does not meet the minimum requirement.
80 */
81 function checkRequiredPHPVersion() {
82 // This value should match the PHP version specified in composer.json,
83 // .phan/config.php, and ScopeStructureTest.php
84 $minimumVersion = '8.1.0';
85
86 /**
87 * This is a list of known-bad ranges of PHP versions. Syntax is like SemVer – either:
88 *
89 * - '1.2.3' to prohibit a single version of PHP, or
90 * - '1.2.3 – 1.2.5' to block a range, inclusive.
91 *
92 * Whitespace will be ignored.
93 *
94 * The key is not shown to users; use it to prompt future developers as to why this was
95 * chosen, ideally one or more Phabricator task references.
96 *
97 * Remember to drop irrelevant ranges when bumping $minimumVersion.
98 */
99 $knownBad = array(
100 );
101
102 $passes = version_compare( PHP_VERSION, $minimumVersion, '>=' );
103
104 $versionString = "PHP $minimumVersion or higher";
105
106 // Left as a programmatic check to make it easier to update.
107 if ( count( $knownBad ) ) {
108 $versionString .= ' (and not ' . implode( ', ', array_values( $knownBad ) ) . ')';
109
110 foreach ( $knownBad as $range ) {
111 // As we don't have composer at this point, we have to do our own version range checking.
112 if ( strpos( $range, '-' ) ) {
113 $passes = $passes && !(
114 version_compare( PHP_VERSION, trim( strstr( $range, '-', true ) ), '>=' )
115 && version_compare( PHP_VERSION, trim( substr( strstr( $range, '-', false ), 1 ) ), '<' )
116 );
117 } else {
118 $passes = $passes && version_compare( PHP_VERSION, trim( $range ), '<>' );
119 }
120 }
121 }
122
123 if ( !$passes ) {
124 $cliText = "Error: You are using an unsupported PHP version (PHP " . PHP_VERSION . ").\n"
125 . "MediaWiki $this->mwVersion needs $versionString.\n\nCheck if you might have a newer "
126 . "PHP executable with a different name.\n\n";
127
128 $web = array();
129 $web['intro'] = "MediaWiki $this->mwVersion requires $versionString; you are using PHP "
130 . PHP_VERSION . ".";
131
132 $web['longTitle'] = "Supported PHP versions";
133 // phpcs:disable Generic.Files.LineLength
134 $web['longHtml'] = <<<HTML
135 <p>
136 Please consider <a href="https://www.php.net/downloads.php">upgrading your copy of PHP</a>.
137 PHP versions less than v8.1.0 are no longer <a href="https://www.php.net/supported-versions.php">supported</a>
138 by the PHP Group and will not receive security or bugfix updates.
139 </p>
140 <p>
141 If for some reason you are unable to upgrade your PHP version, you will need to
142 <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version of
143 MediaWiki from our website. See our
144 <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
145 for details of which versions are compatible with prior versions of PHP.
146 </p>
147 HTML;
148 // phpcs:enable Generic.Files.LineLength
149 $this->triggerError(
150 $web,
151 $cliText
152 );
153 }
154 }
155
156 /**
157 * Displays an error, if the vendor/autoload.php file could not be found.
158 */
159 function checkVendorExistence() {
160 if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
161 $cliText = "Error: You are missing some dependencies. \n"
162 . "MediaWiki has dependencies that need to be installed via Composer\n"
163 . "or from a separate repository. Please see\n"
164 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
165 . "for help with installing them.";
166
167 $web = array();
168 $web['intro'] = "Installing some dependencies is required.";
169 $web['longTitle'] = 'Dependencies';
170 // phpcs:disable Generic.Files.LineLength
171 $web['longHtml'] = <<<HTML
172 <p>
173 MediaWiki has dependencies that need to be installed via Composer
174 or from a separate repository. Please see the
175 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">instructions
176 for installing external libraries</a> on MediaWiki.org.
177 </p>
178 HTML;
179 // phpcs:enable Generic.Files.LineLength
180
181 $this->triggerError( $web, $cliText );
182 }
183 }
184
185 /**
186 * Displays an error, if a PHP extension does not exist.
187 */
188 function checkExtensionExistence() {
189 $missingExtensions = array();
190 foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
191 if ( !function_exists( $function ) ) {
192 $missingExtensions[] = array( $extension );
193 }
194 }
195
196 // Special case: either of those is required, but only on 32-bit systems (T391169)
197 if ( PHP_INT_SIZE < 8 && !extension_loaded( 'gmp' ) && !extension_loaded( 'bcmath' ) ) {
198 $missingExtensions[] = array( 'bcmath', 'gmp' );
199 }
200
201 if ( $missingExtensions ) {
202 $missingExtText = '';
203 $missingExtHtml = '';
204 $baseUrl = 'https://www.php.net';
205 foreach ( $missingExtensions as $extNames ) {
206 $plaintextLinks = array();
207 $htmlLinks = array();
208 foreach ( $extNames as $ext ) {
209 $plaintextLinks[] = "$ext <$baseUrl/$ext>";
210 $htmlLinks[] = "<b>$ext</b> (<a href=\"$baseUrl/$ext\">more information</a>)";
211 }
212
213 $missingExtText .= ' * ' . implode( ' or ', $plaintextLinks ) . "\n";
214 $missingExtHtml .= "<li>" . implode( ' or ', $htmlLinks ) . "</li>";
215 }
216
217 $cliText = "Error: Missing one or more required PHP extensions. Please see\n"
218 . "https://www.mediawiki.org/wiki/Manual:Installation_requirements#PHP\n"
219 . "for help with installing them.\n"
220 . "Please install or enable:\n" . $missingExtText;
221
222 $web = array();
223 $web['intro'] = "Installing some PHP extensions is required.";
224 $web['longTitle'] = 'Required PHP extensions';
225 $web['longHtml'] = <<<HTML
226 <p>
227 You are missing one or more extensions to PHP that MediaWiki requires to run. Please see the
228 <a href="https://www.mediawiki.org/wiki/Manual:Installation_requirements#PHP">PHP
229 installation requirements</a> on MediaWiki.org.
230 </p>
231 <p>Please install or enable:</p>
232 <ul>
233 $missingExtHtml
234 </ul>
235 HTML;
236
237 $this->triggerError( $web, $cliText );
238 }
239 }
240
241 /**
242 * Output headers that prevents error pages to be cached.
243 */
244 function outputHTMLHeader() {
245 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
246
247 header( "$protocol 500 MediaWiki configuration Error" );
248 // Don't cache error pages! They cause no end of trouble...
249 header( 'Cache-Control: no-cache' );
250 }
251
252 /**
253 * Returns an error page, which is suitable for output to the end user via a web browser.
254 *
255 * @param string $introText
256 * @param string $longTitle
257 * @param string $longHtml
258 * @return string
259 */
260 function getIndexErrorOutput( $introText, $longTitle, $longHtml ) {
261 $encLogo =
262 htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
263 'resources/assets/mediawiki.png' );
264
265 $introHtml = htmlspecialchars( $introText );
266 $longTitleHtml = htmlspecialchars( $longTitle );
267
268 header( 'Content-type: text/html; charset=UTF-8' );
269
270 $finalOutput = <<<HTML
271 <!DOCTYPE html>
272 <html lang="en" dir="ltr">
273 <head>
274 <meta charset="UTF-8" />
275 <title>MediaWiki {$this->mwVersion}</title>
276 <style media="screen">
277 body {
278 color: #000;
279 background-color: #fff;
280 font-family: sans-serif;
281 padding: 2em;
282 text-align: center;
283 }
284 p, img, h1, h2, ul {
285 text-align: left;
286 margin: 0.5em 0 1em;
287 }
288 h1 {
289 font-size: 120%;
290 }
291 h2 {
292 font-size: 110%;
293 }
294 </style>
295 </head>
296 <body>
297 <img src="{$encLogo}" alt="The MediaWiki logo" />
298 <h1>MediaWiki {$this->mwVersion} internal error</h1>
299 <p>
300 {$introHtml}
301 </p>
302 <h2>{$longTitleHtml}</h2>
303 {$longHtml}
304 </body>
305 </html>
306 HTML;
307
308 return $finalOutput;
309 }
310
311 /**
312 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
313 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
314 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
315 * no longer need to be).
316 *
317 * This function immediately terminates the PHP process.
318 *
319 * @param string[] $web
320 * - (string) intro: Short error message, displayed on top.
321 * - (string) longTitle: Title for the longer message.
322 * - (string) longHtml: The longer message, as raw HTML.
323 * @param string $cliText
324 */
325 function triggerError( $web, $cliText ) {
326 if ( $this->format === 'html' ) {
327 // Used by index.php and mw-config/index.php
328 $this->outputHTMLHeader();
329 $finalOutput = $this->getIndexErrorOutput(
330 $web['intro'],
331 $web['longTitle'],
332 $web['longHtml']
333 );
334 } else {
335 // Used by Maintenance.php (CLI)
336 $finalOutput = $cliText;
337 }
338
339 echo "$finalOutput\n";
340 die( 1 );
341 }
342 }
343
344 /**
345 * Check PHP version and that external dependencies are installed, and
346 * display an informative error if either condition is not satisfied.
347 *
348 * @param string $format One of "text" or "html"
349 * @param string $scriptPath Used when an error is formatted as HTML.
350 */
351 function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
352 $phpVersionCheck = new PHPVersionCheck();
353 $phpVersionCheck->setFormat( $format );
354 $phpVersionCheck->setScriptPath( $scriptPath );
355 $phpVersionCheck->checkRequiredPHPVersion();
356 $phpVersionCheck->checkVendorExistence();
357 $phpVersionCheck->checkExtensionExistence();
358 }