Make mediawiki.special.pageLanguage work again
[lhc/web/wiklou.git] / includes / PHPVersionCheck.php
1 <?php
2 /**
3 * Check PHP Version, as well as for composer dependencies in entry points,
4 * and display something vaguely comprehensible in the event of a totally
5 * unrecoverable error.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 /**
26 * Check php version and that external dependencies are installed, and
27 * display an informative error if either condition is not satisfied.
28 *
29 * @note Since we can't rely on anything, the minimum PHP versions and MW current
30 * version are hardcoded here
31 */
32 function wfEntryPointCheck( $entryPoint ) {
33 $mwVersion = '1.27';
34 $minimumVersionPHP = '5.3.3';
35 $phpVersion = PHP_VERSION;
36
37 if ( !function_exists( 'version_compare' )
38 || version_compare( $phpVersion, $minimumVersionPHP ) < 0
39 ) {
40 wfPHPVersionError( $entryPoint, $mwVersion, $minimumVersionPHP, $phpVersion );
41 }
42
43 // @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound
44 if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
45 // @codingStandardsIgnoreEnd
46 wfMissingVendorError( $entryPoint, $mwVersion );
47 }
48 }
49
50 /**
51 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
52 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
53 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
54 * no longer need to be).
55 *
56 * Calling this function kills execution immediately.
57 *
58 * @param string $type Which entry point we are protecting. One of:
59 * - index.php
60 * - load.php
61 * - api.php
62 * - mw-config/index.php
63 * - cli
64 * @param string $mwVersion The number of the MediaWiki version used
65 * @param string $title HTML code to be put within an <h2> tag
66 * @param string $shortText
67 * @param string $longText
68 * @param string $longHtml
69 */
70 function wfGenericError( $type, $mwVersion, $title, $shortText, $longText, $longHtml ) {
71 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
72
73 if ( $type == 'cli' ) {
74 $finalOutput = $longText;
75 } else {
76 header( "$protocol 500 MediaWiki configuration Error" );
77 // Don't cache error pages! They cause no end of trouble...
78 header( 'Cache-control: none' );
79 header( 'Pragma: no-cache' );
80
81 if ( $type == 'index.php' || $type == 'mw-config/index.php' ) {
82 $pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
83 if ( $type == 'mw-config/index.php' ) {
84 $dirname = dirname( $pathinfo['dirname'] );
85 } else {
86 $dirname = $pathinfo['dirname'];
87 }
88 $encLogo = htmlspecialchars(
89 str_replace( '//', '/', $dirname . '/' ) .
90 'resources/assets/mediawiki.png'
91 );
92 $shortHtml = htmlspecialchars( $shortText );
93
94 header( 'Content-type: text/html; charset=UTF-8' );
95
96 $finalOutput = <<<HTML
97 <!DOCTYPE html>
98 <html lang="en" dir="ltr">
99 <head>
100 <meta charset="UTF-8" />
101 <title>MediaWiki {$mwVersion}</title>
102 <style media='screen'>
103 body {
104 color: #000;
105 background-color: #fff;
106 font-family: sans-serif;
107 padding: 2em;
108 text-align: center;
109 }
110 p, img, h1, h2 {
111 text-align: left;
112 margin: 0.5em 0 1em;
113 }
114 h1 {
115 font-size: 120%;
116 }
117 h2 {
118 font-size: 110%;
119 }
120 </style>
121 </head>
122 <body>
123 <img src="{$encLogo}" alt='The MediaWiki logo' />
124 <h1>MediaWiki {$mwVersion} internal error</h1>
125 <div class='error'>
126 <p>
127 {$shortHtml}
128 </p>
129 <h2>{$title}</h2>
130 <p>
131 {$longHtml}
132 </p>
133 </div>
134 </body>
135 </html>
136 HTML;
137 // Handle everything that's not index.php
138 } else {
139 // So nothing thinks this is JS or CSS
140 $finalOutput = ( $type == 'load.php' ) ? "/* $shortText */" : $shortText;
141 }
142 }
143 echo "$finalOutput\n";
144 die( 1 );
145 }
146
147 /**
148 * Display an error for the minimum PHP version requirement not being satisfied.
149 *
150 * @param string $type See wfGenericError
151 * @param string $mwVersion See wfGenericError
152 * @param string $minimumVersionPHP The minimum PHP version supported by MediaWiki
153 * @param string $phpVersion The current PHP version
154 */
155 function wfPHPVersionError( $type, $mwVersion, $minimumVersionPHP, $phpVersion ) {
156 $shortText = "MediaWiki $mwVersion requires at least "
157 . "PHP version $minimumVersionPHP, you are using PHP $phpVersion.";
158
159 $longText = "Error: You might be using on older PHP version. \n"
160 . "MediaWiki $mwVersion needs PHP $minimumVersionPHP or higher.\n\n"
161 . "Check if you have a newer php executable with a different name, such as php5.\n\n";
162
163 $longHtml = <<<HTML
164 Please consider <a href="http://www.php.net/downloads.php">upgrading your copy of PHP</a>.
165 PHP versions less than 5.5.0 are no longer supported by the PHP Group and will not receive
166 security or bugfix updates.
167 </p>
168 <p>
169 If for some reason you are unable to upgrade your PHP version, you will need to
170 <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version
171 of MediaWiki from our website. See our
172 <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
173 for details of which versions are compatible with prior versions of PHP.
174 HTML;
175 wfGenericError( $type, $mwVersion, 'Supported PHP versions', $shortText, $longText, $longHtml );
176 }
177
178 /**
179 * Display an error for the vendor/autoload.php file not being found.
180 *
181 * @param string $type See wfGenericError
182 * @param string $mwVersion See wfGenericError
183 */
184 function wfMissingVendorError( $type, $mwVersion ) {
185 $shortText = "Installing some external dependencies (e.g. via composer) is required.";
186
187 $longText = "Error: You are missing some external dependencies. \n"
188 . "MediaWiki now also has some external dependencies that need to be installed\n"
189 . "via composer or from a separate git repo. Please see\n"
190 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
191 . "for help on installing the required components.";
192
193 // @codingStandardsIgnoreStart Generic.Files.LineLength
194 $longHtml = <<<HTML
195 MediaWiki now also has some external dependencies that need to be installed via
196 composer or from a separate git repo. Please see
197 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
198 for help on installing the required components.
199 HTML;
200 // @codingStandardsIgnoreEnd
201
202 wfGenericError( $type, $mwVersion, 'External dependencies', $shortText, $longText, $longHtml );
203 }