MediaWiki  1.34.0
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
23 // @phan-file-suppress PhanPluginDuplicateConditionalNullCoalescing
36  /* @var string The number of the MediaWiki version used. */
37  var $mwVersion = '1.34';
38 
39  /* @var array A mapping of PHP functions to PHP extensions. */
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  );
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 
92  function getPHPInfo() {
93  return array(
94  'implementation' => 'PHP',
95  'version' => PHP_VERSION,
96  'vendor' => 'the PHP Group',
97  'upstreamSupported' => '7.1.0',
98  'minSupported' => '7.2.9',
99  'upgradeURL' => 'https://www.php.net/downloads.php',
100  );
101  }
102 
107  $phpInfo = $this->getPHPInfo();
108  $minimumVersion = $phpInfo['minSupported'];
109  if ( version_compare( $phpInfo['version'], $minimumVersion ) < 0 ) {
110  $shortText = "MediaWiki $this->mwVersion requires at least {$phpInfo['implementation']}"
111  . " version $minimumVersion, you are using {$phpInfo['implementation']} "
112  . "{$phpInfo['version']}.";
113 
114  $longText = "Error: You might be using an older {$phpInfo['implementation']} version "
115  . "({$phpInfo['implementation']} {$phpInfo['version']}). \n"
116  . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
117  . " $minimumVersion or higher.\n\nCheck if you have a"
118  . " newer PHP executable with a different name.\n\n";
119 
120  // phpcs:disable Generic.Files.LineLength
121  $longHtml = <<<HTML
122  Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
123  {$phpInfo['implementation']}</a>.
124  {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
125  longer supported by {$phpInfo['vendor']} and will not receive
126  security or bugfix updates.
127  </p>
128  <p>
129  If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
130  you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
131  older version of MediaWiki from our website.
132  See our <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
133  for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
134 HTML;
135  // phpcs:enable Generic.Files.LineLength
136  $this->triggerError(
137  "Supported {$phpInfo['implementation']} versions",
138  $shortText,
139  $longText,
140  $longHtml
141  );
142  }
143  }
144 
148  function checkVendorExistence() {
149  if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
150  $shortText = "Installing some external dependencies (e.g. via composer) is required.";
151 
152  $longText = "Error: You are missing some external dependencies. \n"
153  . "MediaWiki now also has some external dependencies that need to be installed\n"
154  . "via composer or from a separate git repo. Please see\n"
155  . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
156  . "for help on installing the required components.";
157 
158  // phpcs:disable Generic.Files.LineLength
159  $longHtml = <<<HTML
160  MediaWiki now also has some external dependencies that need to be installed via
161  composer or from a separate git repo. Please see
162  <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
163  for help on installing the required components.
164 HTML;
165  // phpcs:enable Generic.Files.LineLength
166 
167  $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
168  }
169  }
170 
175  $missingExtensions = array();
176  foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
177  if ( !function_exists( $function ) ) {
178  $missingExtensions[] = $extension;
179  }
180  }
181 
182  if ( $missingExtensions ) {
183  $shortText = "Installing some PHP extensions is required.";
184 
185  $missingExtText = '';
186  $missingExtHtml = '';
187  $baseUrl = 'https://www.php.net';
188  foreach ( $missingExtensions as $ext ) {
189  $missingExtText .= " * $ext <$baseUrl/$ext>\n";
190  $missingExtHtml .= "<li><b>$ext</b> "
191  . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
192  }
193 
194  $cliText = "Error: Missing one or more required components of PHP.\n"
195  . "You are missing a required extension to PHP that MediaWiki needs.\n"
196  . "Please install:\n" . $missingExtText;
197 
198  $longHtml = <<<HTML
199  You are missing a required extension to PHP that MediaWiki
200  requires to run. Please install:
201  <ul>
202  $missingExtHtml
203  </ul>
204 HTML;
205 
206  $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
207  }
208  }
209 
213  function outputHTMLHeader() {
214  $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
215 
216  header( "$protocol 500 MediaWiki configuration Error" );
217  // Don't cache error pages! They cause no end of trouble...
218  header( 'Cache-control: none' );
219  header( 'Pragma: no-cache' );
220  }
221 
230  function getIndexErrorOutput( $title, $longHtml, $shortText ) {
231  $encLogo =
232  htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
233  'resources/assets/mediawiki.png' );
234  $shortHtml = htmlspecialchars( $shortText );
235 
236  header( 'Content-type: text/html; charset=UTF-8' );
237 
238  $finalOutput = <<<HTML
239 <!DOCTYPE html>
240 <html lang="en" dir="ltr">
241  <head>
242  <meta charset="UTF-8" />
243  <title>MediaWiki {$this->mwVersion}</title>
244  <style media="screen">
245  body {
246  color: #000;
247  background-color: #fff;
248  font-family: sans-serif;
249  padding: 2em;
250  text-align: center;
251  }
252  p, img, h1, h2, ul {
253  text-align: left;
254  margin: 0.5em 0 1em;
255  }
256  h1 {
257  font-size: 120%;
258  }
259  h2 {
260  font-size: 110%;
261  }
262  </style>
263  </head>
264  <body>
265  <img src="{$encLogo}" alt="The MediaWiki logo" />
266  <h1>MediaWiki {$this->mwVersion} internal error</h1>
267  <div class="error">
268  <p>
269  {$shortHtml}
270  </p>
271  <h2>{$title}</h2>
272  <p>
273  {$longHtml}
274  </p>
275  </div>
276  </body>
277 </html>
278 HTML;
279 
280  return $finalOutput;
281  }
282 
296  function triggerError( $title, $shortText, $longText, $longHtml ) {
297  if ( $this->format === 'html' ) {
298  // Used by index.php and mw-config/index.php
299  $this->outputHTMLHeader();
300  $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
301  } else {
302  // Used by Maintenance.php (CLI)
303  $finalOutput = $longText;
304  }
305 
306  echo "$finalOutput\n";
307  die( 1 );
308  }
309 }
310 
318 function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
319  $phpVersionCheck = new PHPVersionCheck();
320  $phpVersionCheck->setFormat( $format );
321  $phpVersionCheck->setScriptPath( $scriptPath );
322  $phpVersionCheck->checkRequiredPHPVersion();
323  $phpVersionCheck->checkVendorExistence();
324  $phpVersionCheck->checkExtensionExistence();
325 }
PHPVersionCheck\getIndexErrorOutput
getIndexErrorOutput( $title, $longHtml, $shortText)
Returns an error page, which is suitable for output to the end user via a web browser.
Definition: PHPVersionCheck.php:230
PHPVersionCheck\$format
$format
Definition: PHPVersionCheck.php:52
PHPVersionCheck\checkVendorExistence
checkVendorExistence()
Displays an error, if the vendor/autoload.php file could not be found.
Definition: PHPVersionCheck.php:148
PHPVersionCheck\checkExtensionExistence
checkExtensionExistence()
Displays an error, if a PHP extension does not exist.
Definition: PHPVersionCheck.php:174
PHPVersionCheck\triggerError
triggerError( $title, $shortText, $longText, $longHtml)
Display something vaguely comprehensible in the event of a totally unrecoverable error.
Definition: PHPVersionCheck.php:296
PHPVersionCheck\setScriptPath
setScriptPath( $scriptPath)
Set the script path used for images in HTML-formatted errors.
Definition: PHPVersionCheck.php:73
PHPVersionCheck
Check PHP Version, as well as for composer dependencies in entry points, and display something vaguel...
Definition: PHPVersionCheck.php:35
PHPVersionCheck\outputHTMLHeader
outputHTMLHeader()
Output headers that prevents error pages to be cached.
Definition: PHPVersionCheck.php:213
MediaWiki
This class serves as a utility class for this extension.
$title
$title
Definition: testCompression.php:34
PHPVersionCheck\$functionsExtensionsMapping
$functionsExtensionsMapping
Definition: PHPVersionCheck.php:40
PHPVersionCheck\setFormat
setFormat( $format)
Set the format used for errors.
Definition: PHPVersionCheck.php:64
PHPVersionCheck\$scriptPath
$scriptPath
Definition: PHPVersionCheck.php:57
PHPVersionCheck\getPHPInfo
getPHPInfo()
Return the version of the installed PHP implementation.
Definition: PHPVersionCheck.php:92
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
wfEntryPointCheck
wfEntryPointCheck( $format='text', $scriptPath='/')
Check PHP version and that external dependencies are installed, and display an informative error if e...
Definition: PHPVersionCheck.php:254
PHPVersionCheck\checkRequiredPHPVersion
checkRequiredPHPVersion()
Displays an error, if the installed PHP version does not meet the minimum requirement.
Definition: PHPVersionCheck.php:106
PHPVersionCheck\$mwVersion
$mwVersion
Definition: PHPVersionCheck.php:37