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