Merge "Add MediaWikiTestCase convenience method for mocking a logger"
[lhc/web/wiklou.git] / includes / WebStart.php
1 <?php
2 /**
3 * This does the initial set up for a web request.
4 * It does some security checks, starts the profiler and loads the
5 * configuration, and optionally loads Setup.php depending on whether
6 * MW_NO_SETUP is defined.
7 *
8 * Setup.php (if loaded) then sets up GlobalFunctions, the AutoLoader,
9 * and the configuration globals.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29
30 # bug 15461: Make IE8 turn off content sniffing. Everybody else should ignore this
31 # We're adding it here so that it's *always* set, even for alternate entry
32 # points and when $wgOut gets disabled or overridden.
33 header( 'X-Content-Type-Options: nosniff' );
34
35 /**
36 * @var float Request start time as fractional seconds since epoch
37 * @deprecated since 1.25; use $_SERVER['REQUEST_TIME_FLOAT'] or
38 * WebRequest::getElapsedTime() instead.
39 */
40 $wgRequestTime = $_SERVER['REQUEST_TIME_FLOAT'];
41
42 unset( $IP );
43
44 # Valid web server entry point, enable includes.
45 # Please don't move this line to includes/Defines.php. This line essentially
46 # defines a valid entry point. If you put it in includes/Defines.php, then
47 # any script that includes it becomes an entry point, thereby defeating
48 # its purpose.
49 define( 'MEDIAWIKI', true );
50
51 # Full path to working directory.
52 # Makes it possible to for example to have effective exclude path in apc.
53 # __DIR__ breaks symlinked includes, but realpath() returns false
54 # if we don't have permissions on parent directories.
55 $IP = getenv( 'MW_INSTALL_PATH' );
56 if ( $IP === false ) {
57 $IP = realpath( '.' ) ?: dirname( __DIR__ );
58 }
59
60 # Grab profiling functions
61 require_once "$IP/includes/profiler/ProfilerFunctions.php";
62
63 # Start the autoloader, so that extensions can derive classes from core files
64 require_once "$IP/includes/AutoLoader.php";
65
66 # Load up some global defines.
67 require_once "$IP/includes/Defines.php";
68
69 # Start the profiler
70 $wgProfiler = array();
71 if ( file_exists( "$IP/StartProfiler.php" ) ) {
72 require "$IP/StartProfiler.php";
73 }
74
75 # Load default settings
76 require_once "$IP/includes/DefaultSettings.php";
77
78 # Load global functions
79 require_once "$IP/includes/GlobalFunctions.php";
80
81 # Load composer's autoloader if present
82 if ( is_readable( "$IP/vendor/autoload.php" ) ) {
83 require_once "$IP/vendor/autoload.php";
84 }
85
86 # Assert that composer dependencies were successfully loaded
87 # Purposely no leading \ due to it breaking HHVM RepoAuthorative mode
88 # PHP works fine with both versions
89 # See https://github.com/facebook/hhvm/issues/5833
90 if ( !interface_exists( 'Psr\Log\LoggerInterface' ) ) {
91 $message = (
92 'MediaWiki requires the <a href="https://github.com/php-fig/log">PSR-3 logging ' .
93 "library</a> to be present. This library is not embedded directly in MediaWiki's " .
94 "git repository and must be installed separately by the end user.\n\n" .
95 'Please see <a href="https://www.mediawiki.org/wiki/Download_from_Git' .
96 '#Fetch_external_libraries">mediawiki.org</a> for help on installing ' .
97 'the required components.'
98 );
99 echo $message;
100 trigger_error( $message, E_USER_ERROR );
101 die( 1 );
102 }
103
104 if ( defined( 'MW_CONFIG_CALLBACK' ) ) {
105 # Use a callback function to configure MediaWiki
106 call_user_func( MW_CONFIG_CALLBACK );
107 } else {
108 if ( !defined( 'MW_CONFIG_FILE' ) ) {
109 define( 'MW_CONFIG_FILE', "$IP/LocalSettings.php" );
110 }
111
112 # LocalSettings.php is the per site customization file. If it does not exist
113 # the wiki installer needs to be launched or the generated file uploaded to
114 # the root wiki directory. Give a hint, if it is not readable by the server.
115 if ( !is_readable( MW_CONFIG_FILE ) ) {
116 require_once "$IP/includes/NoLocalSettings.php";
117 die();
118 }
119
120 # Include site settings. $IP may be changed (hopefully before the AutoLoader is invoked)
121 require_once MW_CONFIG_FILE;
122 }
123
124 # Initialise output buffering
125 # Check that there is no previous output or previously set up buffers, because
126 # that would cause us to potentially mix gzip and non-gzip output, creating a
127 # big mess.
128 if ( ob_get_level() == 0 ) {
129 require_once "$IP/includes/OutputHandler.php";
130 ob_start( 'wfOutputHandler' );
131 }
132
133 if ( !defined( 'MW_NO_SETUP' ) ) {
134 require_once "$IP/includes/Setup.php";
135 }
136
137 # Multiple DBs or commits might be used; keep the request as transactional as possible
138 if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) {
139 ignore_user_abort( true );
140 }
141
142 if ( !defined( 'MW_API' ) &&
143 RequestContext::getMain()->getRequest()->getHeader( 'Promise-Non-Write-API-Action' )
144 ) {
145 header( 'Cache-Control: no-cache' );
146 header( 'Content-Type: text/html; charset=utf-8' );
147 HttpStatus::header( 400 );
148 $error = wfMessage( 'nonwrite-api-promise-error' )->escaped();
149 $content = <<<EOT
150 <!DOCTYPE html>
151 <html>
152 <head><meta charset="UTF-8" /></head>
153 <body>
154 $error
155 </body>
156 </html>
157
158 EOT;
159 header( 'Content-Length: ' . strlen( $content ) );
160 echo $content;
161 die();
162 }