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