MediaWiki  master
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 
73  function setScriptPath( $scriptPath ) {
74  $this->scriptPath = $scriptPath;
75  }
76 
81  $minimumVersion = '7.2.22';
82 
96  $knownBad = array(
97  'CVE-2019-11048' => '7.3.0 - 7.3.18',
98  // https://bugs.php.net/bug.php?id=79174 as a regression from https://bugs.php.net/bug.php?id=78929
99  'T243667, T291127' => '7.4.0 - 7.4.2'
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 $task => $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 v7.3.0 are no longer supported by the PHP Group and will not receive
138  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 
159  function checkVendorExistence() {
160  if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
161  $cliText = "Error: You are missing some external dependencies. \n"
162  . "MediaWiki also has some external dependencies that need to be installed\n"
163  . "via composer or from a separate git repo. Please see\n"
164  . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
165  . "for help on installing the required components.";
166 
167  $web = array();
168  $web['intro'] = "Installing some external dependencies (e.g. via composer) is required.";
169  $web['longTitle'] = 'External dependencies';
170  // phpcs:disable Generic.Files.LineLength
171  $web['longHtml'] = <<<HTML
172  <p>
173  MediaWiki also has some external dependencies that need to be installed via
174  composer or from a separate git repo. Please see the
175  <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">instructions
176  for installing libraries</a> on mediawiki.org for help on installing the required components.
177  </p>
178 HTML;
179  // phpcs:enable Generic.Files.LineLength
180 
181  $this->triggerError( $web, $cliText );
182  }
183  }
184 
189  $missingExtensions = array();
190  foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
191  if ( !function_exists( $function ) ) {
192  $missingExtensions[] = $extension;
193  }
194  }
195 
196  if ( $missingExtensions ) {
197  $missingExtText = '';
198  $missingExtHtml = '';
199  $baseUrl = 'https://www.php.net';
200  foreach ( $missingExtensions as $ext ) {
201  $missingExtText .= " * $ext <$baseUrl/$ext>\n";
202  $missingExtHtml .= "<li><b>$ext</b> "
203  . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
204  }
205 
206  $cliText = "Error: Missing one or more required components of PHP.\n"
207  . "You are missing a required extension to PHP that MediaWiki needs.\n"
208  . "Please install:\n" . $missingExtText;
209 
210  $web = array();
211  $web['intro'] = "Installing some PHP extensions is required.";
212  $web['longTitle'] = 'Required components';
213  $web['longHtml'] = <<<HTML
214  <p>
215  You are missing a required extension to PHP that MediaWiki
216  requires to run. Please install:
217  </p>
218  <ul>
219  $missingExtHtml
220  </ul>
221 HTML;
222 
223  $this->triggerError( $web, $cliText );
224  }
225  }
226 
230  function outputHTMLHeader() {
231  $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
232 
233  header( "$protocol 500 MediaWiki configuration Error" );
234  // Don't cache error pages! They cause no end of trouble...
235  header( 'Cache-control: none' );
236  header( 'Pragma: no-cache' );
237  }
238 
247  function getIndexErrorOutput( $introText, $longTitle, $longHtml ) {
248  $encLogo =
249  htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
250  'resources/assets/mediawiki.png' );
251 
252  $introHtml = htmlspecialchars( $introText );
253  $longTitleHtml = htmlspecialchars( $longTitle );
254 
255  header( 'Content-type: text/html; charset=UTF-8' );
256 
257  $finalOutput = <<<HTML
258 <!DOCTYPE html>
259 <html lang="en" dir="ltr">
260  <head>
261  <meta charset="UTF-8" />
262  <title>MediaWiki {$this->mwVersion}</title>
263  <style media="screen">
264  body {
265  color: #000;
266  background-color: #fff;
267  font-family: sans-serif;
268  padding: 2em;
269  text-align: center;
270  }
271  p, img, h1, h2, ul {
272  text-align: left;
273  margin: 0.5em 0 1em;
274  }
275  h1 {
276  font-size: 120%;
277  }
278  h2 {
279  font-size: 110%;
280  }
281  </style>
282  </head>
283  <body>
284  <img src="{$encLogo}" alt="The MediaWiki logo" />
285  <h1>MediaWiki {$this->mwVersion} internal error</h1>
286  <p>
287  {$introHtml}
288  </p>
289  <h2>{$longTitleHtml}</h2>
290  {$longHtml}
291  </body>
292 </html>
293 HTML;
294 
295  return $finalOutput;
296  }
297 
312  function triggerError( $web, $cliText ) {
313  if ( $this->format === 'html' ) {
314  // Used by index.php and mw-config/index.php
315  $this->outputHTMLHeader();
316  $finalOutput = $this->getIndexErrorOutput(
317  $web['intro'],
318  $web['longTitle'],
319  $web['longHtml']
320  );
321  } else {
322  // Used by Maintenance.php (CLI)
323  $finalOutput = $cliText;
324  }
325 
326  echo "$finalOutput\n";
327  die( 1 );
328  }
329 }
330 
338 function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
339  $phpVersionCheck = new PHPVersionCheck();
340  $phpVersionCheck->setFormat( $format );
341  $phpVersionCheck->setScriptPath( $scriptPath );
342  $phpVersionCheck->checkRequiredPHPVersion();
343  $phpVersionCheck->checkVendorExistence();
344  $phpVersionCheck->checkExtensionExistence();
345 }
wfEntryPointCheck( $format='text', $scriptPath='/')
Check PHP version and that external dependencies are installed, and display an informative error if e...
The MediaWiki class is the helper class for the index.php entry point.
Definition: MediaWiki.php:38
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.
if(!is_readable( $file)) $ext
Definition: router.php:48