Merge "Additional tests to catch future Parsoid regressions."
[lhc/web/wiklou.git] / includes / Setup.php
1 <?php
2 /**
3 * Include most things that's need to customize the site.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * This file is not a valid entry point, perform no further processing unless
25 * MEDIAWIKI is defined
26 */
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 exit( 1 );
29 }
30
31 # The main wiki script and things like database
32 # conversion and maintenance scripts all share a
33 # common setup of including lots of classes and
34 # setting up a few globals.
35 #
36
37 $fname = 'Setup.php';
38 wfProfileIn( $fname );
39
40 // Check to see if we are at the file scope
41 if ( !isset( $wgVersion ) ) {
42 echo "Error, Setup.php must be included from the file scope, after DefaultSettings.php\n";
43 die( 1 );
44 }
45
46 // Set various default paths sensibly...
47 if ( $wgScript === false ) $wgScript = "$wgScriptPath/index$wgScriptExtension";
48 if ( $wgRedirectScript === false ) $wgRedirectScript = "$wgScriptPath/redirect$wgScriptExtension";
49 if ( $wgLoadScript === false ) $wgLoadScript = "$wgScriptPath/load$wgScriptExtension";
50
51 if ( $wgArticlePath === false ) {
52 if ( $wgUsePathInfo ) {
53 $wgArticlePath = "$wgScript/$1";
54 } else {
55 $wgArticlePath = "$wgScript?title=$1";
56 }
57 }
58
59 if ( !empty( $wgActionPaths ) && !isset( $wgActionPaths['view'] ) ) {
60 # 'view' is assumed the default action path everywhere in the code
61 # but is rarely filled in $wgActionPaths
62 $wgActionPaths['view'] = $wgArticlePath;
63 }
64
65 if ( !empty( $wgActionPaths ) && !isset( $wgActionPaths['view'] ) ) {
66 # 'view' is assumed the default action path everywhere in the code
67 # but is rarely filled in $wgActionPaths
68 $wgActionPaths['view'] = $wgArticlePath;
69 }
70
71 if ( $wgStylePath === false ) $wgStylePath = "$wgScriptPath/skins";
72 if ( $wgLocalStylePath === false ) $wgLocalStylePath = "$wgScriptPath/skins";
73 if ( $wgStyleDirectory === false ) $wgStyleDirectory = "$IP/skins";
74 if ( $wgExtensionAssetsPath === false ) $wgExtensionAssetsPath = "$wgScriptPath/extensions";
75
76 if ( $wgLogo === false ) $wgLogo = "$wgStylePath/common/images/wiki.png";
77
78 if ( $wgUploadPath === false ) $wgUploadPath = "$wgScriptPath/images";
79 if ( $wgUploadDirectory === false ) $wgUploadDirectory = "$IP/images";
80 if ( $wgReadOnlyFile === false ) $wgReadOnlyFile = "{$wgUploadDirectory}/lock_yBgMBwiR";
81 if ( $wgFileCacheDirectory === false ) $wgFileCacheDirectory = "{$wgUploadDirectory}/cache";
82 if ( $wgDeletedDirectory === false ) $wgDeletedDirectory = "{$wgUploadDirectory}/deleted";
83
84 if ( isset( $wgFileStore['deleted']['directory'] ) ) {
85 $wgDeletedDirectory = $wgFileStore['deleted']['directory'];
86 }
87
88 if ( isset( $wgFooterIcons['copyright'] ) &&
89 isset( $wgFooterIcons['copyright']['copyright'] ) &&
90 $wgFooterIcons['copyright']['copyright'] === array() )
91 {
92 if ( isset( $wgCopyrightIcon ) && $wgCopyrightIcon ) {
93 $wgFooterIcons['copyright']['copyright'] = $wgCopyrightIcon;
94 } elseif ( $wgRightsIcon || $wgRightsText ) {
95 $wgFooterIcons['copyright']['copyright'] = array(
96 'url' => $wgRightsUrl,
97 'src' => $wgRightsIcon,
98 'alt' => $wgRightsText,
99 );
100 } else {
101 unset( $wgFooterIcons['copyright']['copyright'] );
102 }
103 }
104
105 if ( isset( $wgFooterIcons['poweredby'] ) &&
106 isset( $wgFooterIcons['poweredby']['mediawiki'] ) &&
107 $wgFooterIcons['poweredby']['mediawiki']['src'] === null )
108 {
109 $wgFooterIcons['poweredby']['mediawiki']['src'] = "$wgStylePath/common/images/poweredby_mediawiki_88x31.png";
110 }
111
112 /**
113 * Unconditional protection for NS_MEDIAWIKI since otherwise it's too easy for a
114 * sysadmin to set $wgNamespaceProtection incorrectly and leave the wiki insecure.
115 *
116 * Note that this is the definition of editinterface and it can be granted to
117 * all users if desired.
118 */
119 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
120
121 /**
122 * The canonical names of namespaces 6 and 7 are, as of v1.14, "File"
123 * and "File_talk". The old names "Image" and "Image_talk" are
124 * retained as aliases for backwards compatibility.
125 */
126 $wgNamespaceAliases['Image'] = NS_FILE;
127 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
128
129 /**
130 * Initialise $wgLockManagers to include basic FS version
131 */
132 $wgLockManagers[] = array(
133 'name' => 'fsLockManager',
134 'class' => 'FSLockManager',
135 'lockDirectory' => "{$wgUploadDirectory}/lockdir",
136 );
137 $wgLockManagers[] = array(
138 'name' => 'nullLockManager',
139 'class' => 'NullLockManager',
140 );
141
142 /**
143 * Initialise $wgLocalFileRepo from backwards-compatible settings
144 */
145 if ( !$wgLocalFileRepo ) {
146 if ( isset( $wgFileStore['deleted']['hash'] ) ) {
147 $deletedHashLevel = $wgFileStore['deleted']['hash'];
148 } else {
149 $deletedHashLevel = $wgHashedUploadDirectory ? 3 : 0;
150 }
151 $wgLocalFileRepo = array(
152 'class' => 'LocalRepo',
153 'name' => 'local',
154 'directory' => $wgUploadDirectory,
155 'scriptDirUrl' => $wgScriptPath,
156 'scriptExtension' => $wgScriptExtension,
157 'url' => $wgUploadBaseUrl ? $wgUploadBaseUrl . $wgUploadPath : $wgUploadPath,
158 'hashLevels' => $wgHashedUploadDirectory ? 2 : 0,
159 'thumbScriptUrl' => $wgThumbnailScriptPath,
160 'transformVia404' => !$wgGenerateThumbnailOnParse,
161 'deletedDir' => $wgDeletedDirectory,
162 'deletedHashLevels' => $deletedHashLevel
163 );
164 }
165 /**
166 * Initialise shared repo from backwards-compatible settings
167 */
168 if ( $wgUseSharedUploads ) {
169 if ( $wgSharedUploadDBname ) {
170 $wgForeignFileRepos[] = array(
171 'class' => 'ForeignDBRepo',
172 'name' => 'shared',
173 'directory' => $wgSharedUploadDirectory,
174 'url' => $wgSharedUploadPath,
175 'hashLevels' => $wgHashedSharedUploadDirectory ? 2 : 0,
176 'thumbScriptUrl' => $wgSharedThumbnailScriptPath,
177 'transformVia404' => !$wgGenerateThumbnailOnParse,
178 'dbType' => $wgDBtype,
179 'dbServer' => $wgDBserver,
180 'dbUser' => $wgDBuser,
181 'dbPassword' => $wgDBpassword,
182 'dbName' => $wgSharedUploadDBname,
183 'dbFlags' => ( $wgDebugDumpSql ? DBO_DEBUG : 0 ) | DBO_DEFAULT,
184 'tablePrefix' => $wgSharedUploadDBprefix,
185 'hasSharedCache' => $wgCacheSharedUploads,
186 'descBaseUrl' => $wgRepositoryBaseUrl,
187 'fetchDescription' => $wgFetchCommonsDescriptions,
188 );
189 } else {
190 $wgForeignFileRepos[] = array(
191 'class' => 'FileRepo',
192 'name' => 'shared',
193 'directory' => $wgSharedUploadDirectory,
194 'url' => $wgSharedUploadPath,
195 'hashLevels' => $wgHashedSharedUploadDirectory ? 2 : 0,
196 'thumbScriptUrl' => $wgSharedThumbnailScriptPath,
197 'transformVia404' => !$wgGenerateThumbnailOnParse,
198 'descBaseUrl' => $wgRepositoryBaseUrl,
199 'fetchDescription' => $wgFetchCommonsDescriptions,
200 );
201 }
202 }
203 if ( $wgUseInstantCommons ) {
204 $wgForeignFileRepos[] = array(
205 'class' => 'ForeignAPIRepo',
206 'name' => 'wikimediacommons',
207 'apibase' => WebRequest::detectProtocol() === 'https' ?
208 'https://commons.wikimedia.org/w/api.php' :
209 'http://commons.wikimedia.org/w/api.php',
210 'hashLevels' => 2,
211 'fetchDescription' => true,
212 'descriptionCacheExpiry' => 43200,
213 'apiThumbCacheExpiry' => 86400,
214 );
215 }
216 /*
217 * Add on default file backend config for file repos.
218 * FileBackendGroup will handle initializing the backends.
219 */
220 if ( !isset( $wgLocalFileRepo['backend'] ) ) {
221 $wgLocalFileRepo['backend'] = $wgLocalFileRepo['name'] . '-backend';
222 }
223 foreach ( $wgForeignFileRepos as &$repo ) {
224 if ( !isset( $repo['directory'] ) && $repo['class'] === 'ForeignAPIRepo' ) {
225 $repo['directory'] = $wgUploadDirectory; // b/c
226 }
227 if ( !isset( $repo['backend'] ) ) {
228 $repo['backend'] = $repo['name'] . '-backend';
229 }
230 }
231 unset( $repo ); // no global pollution; destroy reference
232
233 if ( is_null( $wgEnableAutoRotation ) ) {
234 // Only enable auto-rotation when the bitmap handler can rotate
235 $wgEnableAutoRotation = BitmapHandler::canRotate();
236 }
237
238 if ( $wgRCFilterByAge ) {
239 # # Trim down $wgRCLinkDays so that it only lists links which are valid
240 # # as determined by $wgRCMaxAge.
241 # # Note that we allow 1 link higher than the max for things like 56 days but a 60 day link.
242 sort( $wgRCLinkDays );
243 for ( $i = 0; $i < count( $wgRCLinkDays ); $i++ ) {
244 if ( $wgRCLinkDays[$i] >= $wgRCMaxAge / ( 3600 * 24 ) ) {
245 $wgRCLinkDays = array_slice( $wgRCLinkDays, 0, $i + 1, false );
246 break;
247 }
248 }
249 }
250
251 if ( $wgSkipSkin ) {
252 $wgSkipSkins[] = $wgSkipSkin;
253 }
254
255 # Set default shared prefix
256 if ( $wgSharedPrefix === false ) {
257 $wgSharedPrefix = $wgDBprefix;
258 }
259
260 if ( !$wgCookiePrefix ) {
261 if ( $wgSharedDB && $wgSharedPrefix && in_array( 'user', $wgSharedTables ) ) {
262 $wgCookiePrefix = $wgSharedDB . '_' . $wgSharedPrefix;
263 } elseif ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
264 $wgCookiePrefix = $wgSharedDB;
265 } elseif ( $wgDBprefix ) {
266 $wgCookiePrefix = $wgDBname . '_' . $wgDBprefix;
267 } else {
268 $wgCookiePrefix = $wgDBname;
269 }
270 }
271 $wgCookiePrefix = strtr( $wgCookiePrefix, '=,; +."\'\\[', '__________' );
272
273 $wgUseEnotif = $wgEnotifUserTalk || $wgEnotifWatchlist;
274
275 if ( $wgMetaNamespace === false ) {
276 $wgMetaNamespace = str_replace( ' ', '_', $wgSitename );
277 }
278
279 /**
280 * Definitions of the NS_ constants are in Defines.php
281 * @private
282 */
283 $wgCanonicalNamespaceNames = array(
284 NS_MEDIA => 'Media',
285 NS_SPECIAL => 'Special',
286 NS_TALK => 'Talk',
287 NS_USER => 'User',
288 NS_USER_TALK => 'User_talk',
289 NS_PROJECT => 'Project',
290 NS_PROJECT_TALK => 'Project_talk',
291 NS_FILE => 'File',
292 NS_FILE_TALK => 'File_talk',
293 NS_MEDIAWIKI => 'MediaWiki',
294 NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
295 NS_TEMPLATE => 'Template',
296 NS_TEMPLATE_TALK => 'Template_talk',
297 NS_HELP => 'Help',
298 NS_HELP_TALK => 'Help_talk',
299 NS_CATEGORY => 'Category',
300 NS_CATEGORY_TALK => 'Category_talk',
301 );
302
303 /// @todo UGLY UGLY
304 if( is_array( $wgExtraNamespaces ) ) {
305 $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces;
306 }
307
308 # These are now the same, always
309 # To determine the user language, use $wgLang->getCode()
310 $wgContLanguageCode = $wgLanguageCode;
311
312 # Easy to forget to falsify $wgShowIPinHeader for static caches.
313 # If file cache or squid cache is on, just disable this (DWIMD).
314 # Do the same for $wgDebugToolbar.
315 if ( $wgUseFileCache || $wgUseSquid ) {
316 $wgShowIPinHeader = false;
317 $wgDebugToolbar = false;
318 }
319
320 # Doesn't make sense to have if disabled.
321 if ( !$wgEnotifMinorEdits ) {
322 $wgHiddenPrefs[] = 'enotifminoredits';
323 }
324
325 # $wgDisabledActions is deprecated as of 1.18
326 foreach( $wgDisabledActions as $action ) {
327 $wgActions[$action] = false;
328 }
329
330 if ( !$wgHtml5Version && $wgHtml5 && $wgAllowRdfaAttributes ) {
331 # see http://www.w3.org/TR/rdfa-in-html/#document-conformance
332 if ( $wgMimeType == 'application/xhtml+xml' ) {
333 $wgHtml5Version = 'XHTML+RDFa 1.0';
334 } else {
335 $wgHtml5Version = 'HTML+RDFa 1.0';
336 }
337 }
338
339 # Blacklisted file extensions shouldn't appear on the "allowed" list
340 $wgFileExtensions = array_values( array_diff ( $wgFileExtensions, $wgFileBlacklist ) );
341
342 if ( $wgArticleCountMethod === null ) {
343 $wgArticleCountMethod = $wgUseCommaCount ? 'comma' : 'link';
344 }
345
346 if ( $wgInvalidateCacheOnLocalSettingsChange ) {
347 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( "$IP/LocalSettings.php" ) ) );
348 }
349
350 if ( $wgAjaxUploadDestCheck ) {
351 $wgAjaxExportList[] = 'SpecialUpload::ajaxGetExistsWarning';
352 }
353
354 if ( $wgNewUserLog ) {
355 # Add a new log type
356 $wgLogTypes[] = 'newusers';
357 $wgLogNames['newusers'] = 'newuserlogpage';
358 $wgLogHeaders['newusers'] = 'newuserlogpagetext';
359 $wgLogActionsHandlers['newusers/newusers'] = 'NewUsersLogFormatter';
360 $wgLogActionsHandlers['newusers/create'] = 'NewUsersLogFormatter';
361 $wgLogActionsHandlers['newusers/create2'] = 'NewUsersLogFormatter';
362 $wgLogActionsHandlers['newusers/byemail'] = 'NewUsersLogFormatter';
363 $wgLogActionsHandlers['newusers/autocreate'] = 'NewUsersLogFormatter';
364 }
365
366 if ( $wgCookieSecure === 'detect' ) {
367 $wgCookieSecure = ( WebRequest::detectProtocol() === 'https' );
368 }
369
370 // Disable MWDebug for command line mode, this prevents MWDebug from eating up
371 // all the memory from logging SQL queries on maintenance scripts
372 global $wgCommandLineMode;
373 if ( $wgDebugToolbar && !$wgCommandLineMode ) {
374 MWDebug::init();
375 }
376
377 if ( !defined( 'MW_COMPILED' ) ) {
378 if ( !MWInit::classExists( 'AutoLoader' ) ) {
379 require_once( "$IP/includes/AutoLoader.php" );
380 }
381
382 wfProfileIn( $fname . '-exception' );
383 MWExceptionHandler::installHandler();
384 wfProfileOut( $fname . '-exception' );
385
386 wfProfileIn( $fname . '-includes' );
387 require_once( "$IP/includes/normal/UtfNormalUtil.php" );
388 require_once( "$IP/includes/GlobalFunctions.php" );
389 require_once( "$IP/includes/ProxyTools.php" );
390 require_once( "$IP/includes/normal/UtfNormalDefines.php" );
391 wfProfileOut( $fname . '-includes' );
392 }
393
394 if ( $wgSecureLogin && substr( $wgServer, 0, 2 ) !== '//' ) {
395 $wgSecureLogin = false;
396 wfWarn( 'Secure login was enabled on a server that only supports HTTP or HTTPS. Disabling secure login.' );
397 }
398
399 # Now that GlobalFunctions is loaded, set defaults that depend
400 # on it.
401 if ( $wgTmpDirectory === false ) {
402 $wgTmpDirectory = wfTempDir();
403 }
404
405 if ( $wgCanonicalServer === false ) {
406 $wgCanonicalServer = wfExpandUrl( $wgServer, PROTO_HTTP );
407 }
408
409 // Initialize $wgHTCPMulticastRouting from backwards-compatible settings
410 if ( !$wgHTCPMulticastRouting && $wgHTCPMulticastAddress ) {
411 $wgHTCPMulticastRouting = array(
412 '' => array(
413 'host' => $wgHTCPMulticastAddress,
414 'port' => $wgHTCPPort,
415 )
416 );
417 }
418
419 wfProfileIn( $fname . '-misc1' );
420
421 # Raise the memory limit if it's too low
422 wfMemoryLimit();
423
424 /**
425 * Set up the timezone, suppressing the pseudo-security warning in PHP 5.1+
426 * that happens whenever you use a date function without the timezone being
427 * explicitly set. Inspired by phpMyAdmin's treatment of the problem.
428 */
429 if ( is_null( $wgLocaltimezone) ) {
430 wfSuppressWarnings();
431 $wgLocaltimezone = date_default_timezone_get();
432 wfRestoreWarnings();
433 }
434
435 date_default_timezone_set( $wgLocaltimezone );
436 if( is_null( $wgLocalTZoffset ) ) {
437 $wgLocalTZoffset = date( 'Z' ) / 60;
438 }
439
440 # Useful debug output
441 if ( $wgCommandLineMode ) {
442 $wgRequest = new FauxRequest( array() );
443
444 wfDebug( "\n\nStart command line script $self\n" );
445 } else {
446 # Can't stub this one, it sets up $_GET and $_REQUEST in its constructor
447 $wgRequest = new WebRequest;
448
449 $debug = "\n\nStart request {$wgRequest->getMethod()} {$wgRequest->getRequestURL()}\n";
450
451 if ( $wgDebugPrintHttpHeaders ) {
452 $debug .= "HTTP HEADERS:\n";
453
454 foreach ( $wgRequest->getAllHeaders() as $name => $value ) {
455 $debug .= "$name: $value\n";
456 }
457 }
458 wfDebug( $debug );
459 }
460
461 wfProfileOut( $fname . '-misc1' );
462 wfProfileIn( $fname . '-memcached' );
463
464 $wgMemc = wfGetMainCache();
465 $messageMemc = wfGetMessageCacheStorage();
466 $parserMemc = wfGetParserCacheStorage();
467 $wgLangConvMemc = wfGetLangConverterCacheStorage();
468
469 wfDebug( 'CACHES: ' . get_class( $wgMemc ) . '[main] ' .
470 get_class( $messageMemc ) . '[message] ' .
471 get_class( $parserMemc ) . "[parser]\n" );
472
473 wfProfileOut( $fname . '-memcached' );
474
475 # # Most of the config is out, some might want to run hooks here.
476 wfRunHooks( 'SetupAfterCache' );
477
478 wfProfileIn( $fname . '-session' );
479
480 # If session.auto_start is there, we can't touch session name
481 if ( !wfIniGetBool( 'session.auto_start' ) ) {
482 session_name( $wgSessionName ? $wgSessionName : $wgCookiePrefix . '_session' );
483 }
484
485 if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) {
486 if ( $wgRequest->checkSessionCookie() || isset( $_COOKIE[$wgCookiePrefix . 'Token'] ) ) {
487 wfSetupSession();
488 $wgSessionStarted = true;
489 } else {
490 $wgSessionStarted = false;
491 }
492 }
493
494 wfProfileOut( $fname . '-session' );
495 wfProfileIn( $fname . '-globals' );
496
497 $wgContLang = Language::factory( $wgLanguageCode );
498 $wgContLang->initEncoding();
499 $wgContLang->initContLang();
500
501 // Now that variant lists may be available...
502 $wgRequest->interpolateTitle();
503 $wgUser = RequestContext::getMain()->getUser(); # BackCompat
504
505 /**
506 * @var $wgLang Language
507 */
508 $wgLang = new StubUserLang;
509
510 /**
511 * @var OutputPage
512 */
513 $wgOut = RequestContext::getMain()->getOutput(); # BackCompat
514
515 /**
516 * @var $wgParser Parser
517 */
518 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
519
520 if ( !is_object( $wgAuth ) ) {
521 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
522 wfRunHooks( 'AuthPluginSetup', array( &$wgAuth ) );
523 }
524
525 # Placeholders in case of DB error
526 $wgTitle = null;
527
528 $wgDeferredUpdateList = array();
529
530 wfProfileOut( $fname . '-globals' );
531 wfProfileIn( $fname . '-extensions' );
532
533 # Extension setup functions for extensions other than skins
534 # Entries should be added to this variable during the inclusion
535 # of the extension file. This allows the extension to perform
536 # any necessary initialisation in the fully initialised environment
537 foreach ( $wgExtensionFunctions as $func ) {
538 # Allow closures in PHP 5.3+
539 if ( is_object( $func ) && $func instanceof Closure ) {
540 $profName = $fname . '-extensions-closure';
541 } elseif ( is_array( $func ) ) {
542 if ( is_object( $func[0] ) )
543 $profName = $fname . '-extensions-' . get_class( $func[0] ) . '::' . $func[1];
544 else
545 $profName = $fname . '-extensions-' . implode( '::', $func );
546 } else {
547 $profName = $fname . '-extensions-' . strval( $func );
548 }
549
550 wfProfileIn( $profName );
551 call_user_func( $func );
552 wfProfileOut( $profName );
553 }
554
555 wfDebug( "Fully initialised\n" );
556 $wgFullyInitialised = true;
557
558 wfProfileOut( $fname . '-extensions' );
559 wfProfileOut( $fname );