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
23 // @phan-file-suppress PhanPluginDuplicateConditionalNullCoalescing
36  /* @var string The number of the MediaWiki version used. */
37  var $mwVersion = '1.35';
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 
81  $minimumVersion = '7.2.22';
82 
83  if ( version_compare( PHP_VERSION, $minimumVersion ) < 0 ) {
84  $cliText = "Error: You might be using an older PHP version (PHP " . PHP_VERSION . ").\n"
85  . "MediaWiki $this->mwVersion needs PHP $minimumVersion or higher.\n\nCheck if you "
86  . "have a newer PHP executable with a different name.\n\n";
87 
88  $web = array();
89  $web['intro'] = "MediaWiki $this->mwVersion requires at least PHP version $minimumVersion; "
90  . "you are using PHP " . PHP_VERSION . ".";
91 
92  $web['longTitle'] = "Supported PHP versions";
93  // phpcs:disable Generic.Files.LineLength
94  $web['longHtml'] = <<<HTML
95  <p>
96  Please consider <a href="https://www.php.net/downloads.php">upgrading your copy of PHP</a>.
97  PHP versions less than v7.2.0 are no longer supported by the PHP Group and will not receive
98  security or bugfix updates.
99  </p>
100  <p>
101  If for some reason you are unable to upgrade your PHP version, you will need to
102  <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version of
103  MediaWiki from our website. See our
104  <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
105  for details of which versions are compatible with prior versions of PHP.
106  </p>
107 HTML;
108  // phpcs:enable Generic.Files.LineLength
109  $this->triggerError(
110  $web,
111  $cliText
112  );
113  }
114  }
115 
119  function checkVendorExistence() {
120  if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
121  $cliText = "Error: You are missing some external dependencies. \n"
122  . "MediaWiki also has some external dependencies that need to be installed\n"
123  . "via composer or from a separate git repo. Please see\n"
124  . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
125  . "for help on installing the required components.";
126 
127  $web = array();
128  $web['intro'] = "Installing some external dependencies (e.g. via composer) is required.";
129  $web['longTitle'] = 'External dependencies';
130  // phpcs:disable Generic.Files.LineLength
131  $web['longHtml'] = <<<HTML
132  <p>
133  MediaWiki also has some external dependencies that need to be installed via
134  composer or from a separate git repo. Please see
135  <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
136  for help on installing the required components.
137  </p>
138 HTML;
139  // phpcs:enable Generic.Files.LineLength
140 
141  $this->triggerError( $web, $cliText );
142  }
143  }
144 
149  $missingExtensions = array();
150  foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
151  if ( !function_exists( $function ) ) {
152  $missingExtensions[] = $extension;
153  }
154  }
155 
156  if ( $missingExtensions ) {
157  $missingExtText = '';
158  $missingExtHtml = '';
159  $baseUrl = 'https://www.php.net';
160  foreach ( $missingExtensions as $ext ) {
161  $missingExtText .= " * $ext <$baseUrl/$ext>\n";
162  $missingExtHtml .= "<li><b>$ext</b> "
163  . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
164  }
165 
166  $cliText = "Error: Missing one or more required components of PHP.\n"
167  . "You are missing a required extension to PHP that MediaWiki needs.\n"
168  . "Please install:\n" . $missingExtText;
169 
170  $web = array();
171  $web['intro'] = "Installing some PHP extensions is required.";
172  $web['longTitle'] = 'Required components';
173  $web['longHtml'] = <<<HTML
174  <p>
175  You are missing a required extension to PHP that MediaWiki
176  requires to run. Please install:
177  </p>
178  <ul>
179  $missingExtHtml
180  </ul>
181 HTML;
182 
183  $this->triggerError( $web, $cliText );
184  }
185  }
186 
190  function outputHTMLHeader() {
191  $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
192 
193  header( "$protocol 500 MediaWiki configuration Error" );
194  // Don't cache error pages! They cause no end of trouble...
195  header( 'Cache-control: none' );
196  header( 'Pragma: no-cache' );
197  }
198 
207  function getIndexErrorOutput( $introText, $longTitle, $longHtml ) {
208  $encLogo =
209  htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
210  'resources/assets/mediawiki.png' );
211 
212  $introHtml = htmlspecialchars( $introText );
213  $longTitleHtml = htmlspecialchars( $longTitle );
214 
215  header( 'Content-type: text/html; charset=UTF-8' );
216 
217  $finalOutput = <<<HTML
218 <!DOCTYPE html>
219 <html lang="en" dir="ltr">
220  <head>
221  <meta charset="UTF-8" />
222  <title>MediaWiki {$this->mwVersion}</title>
223  <style media="screen">
224  body {
225  color: #000;
226  background-color: #fff;
227  font-family: sans-serif;
228  padding: 2em;
229  text-align: center;
230  }
231  p, img, h1, h2, ul {
232  text-align: left;
233  margin: 0.5em 0 1em;
234  }
235  h1 {
236  font-size: 120%;
237  }
238  h2 {
239  font-size: 110%;
240  }
241  </style>
242  </head>
243  <body>
244  <img src="{$encLogo}" alt="The MediaWiki logo" />
245  <h1>MediaWiki {$this->mwVersion} internal error</h1>
246  <p>
247  {$introHtml}
248  </p>
249  <h2>{$longTitleHtml}</h2>
250  {$longHtml}
251  </body>
252 </html>
253 HTML;
254 
255  return $finalOutput;
256  }
257 
272  function triggerError( $web, $cliText ) {
273  if ( $this->format === 'html' ) {
274  // Used by index.php and mw-config/index.php
275  $this->outputHTMLHeader();
276  $finalOutput = $this->getIndexErrorOutput(
277  $web['intro'],
278  $web['longTitle'],
279  $web['longHtml']
280  );
281  } else {
282  // Used by Maintenance.php (CLI)
283  $finalOutput = $cliText;
284  }
285 
286  echo "$finalOutput\n";
287  die( 1 );
288  }
289 }
290 
298 function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
299  $phpVersionCheck = new PHPVersionCheck();
300  $phpVersionCheck->setFormat( $format );
301  $phpVersionCheck->setScriptPath( $scriptPath );
302  $phpVersionCheck->checkRequiredPHPVersion();
303  $phpVersionCheck->checkVendorExistence();
304  $phpVersionCheck->checkExtensionExistence();
305 }
PHPVersionCheck\checkVendorExistence
checkVendorExistence()
Displays an error, if the vendor/autoload.php file could not be found.
Definition: PHPVersionCheck.php:119
PHPVersionCheck\checkExtensionExistence
checkExtensionExistence()
Displays an error, if a PHP extension does not exist.
Definition: PHPVersionCheck.php:148
PHPVersionCheck\setScriptPath
setScriptPath( $scriptPath)
Set the script path used for images in HTML-formatted errors.
Definition: PHPVersionCheck.php:73
PHPVersionCheck\getIndexErrorOutput
getIndexErrorOutput( $introText, $longTitle, $longHtml)
Returns an error page, which is suitable for output to the end user via a web browser.
Definition: PHPVersionCheck.php:207
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:190
MediaWiki
A helper class for throttling authentication attempts.
PHPVersionCheck\triggerError
triggerError( $web, $cliText)
Display something vaguely comprehensible in the event of a totally unrecoverable error.
Definition: PHPVersionCheck.php:272
PHPVersionCheck\$format
string $format
The format used for errors.
Definition: PHPVersionCheck.php:52
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
$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:298
PHPVersionCheck\checkRequiredPHPVersion
checkRequiredPHPVersion()
Displays an error, if the installed PHP version does not meet the minimum requirement.
Definition: PHPVersionCheck.php:80
PHPVersionCheck\$mwVersion
$mwVersion
Definition: PHPVersionCheck.php:37