Merge "Added a Redis client for object cache storage"
[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 # $wgAllowRealName and $wgAllowUserSkin were removed in 1.16
321 # in favor of $wgHiddenPrefs, handle b/c here
322 if ( !$wgAllowRealName ) {
323 $wgHiddenPrefs[] = 'realname';
324 }
325
326 # Doesn't make sense to have if disabled.
327 if ( !$wgEnotifMinorEdits ) {
328 $wgHiddenPrefs[] = 'enotifminoredits';
329 }
330
331 # $wgDisabledActions is deprecated as of 1.18
332 foreach( $wgDisabledActions as $action ){
333 $wgActions[$action] = false;
334 }
335 if( !$wgAllowPageInfo ){
336 $wgActions['info'] = false;
337 }
338
339 if ( !$wgHtml5Version && $wgHtml5 && $wgAllowRdfaAttributes ) {
340 # see http://www.w3.org/TR/rdfa-in-html/#document-conformance
341 if ( $wgMimeType == 'application/xhtml+xml' ) {
342 $wgHtml5Version = 'XHTML+RDFa 1.0';
343 } else {
344 $wgHtml5Version = 'HTML+RDFa 1.0';
345 }
346 }
347
348 # Blacklisted file extensions shouldn't appear on the "allowed" list
349 $wgFileExtensions = array_diff ( $wgFileExtensions, $wgFileBlacklist );
350
351 if ( $wgArticleCountMethod === null ) {
352 $wgArticleCountMethod = $wgUseCommaCount ? 'comma' : 'link';
353 }
354
355 if ( $wgInvalidateCacheOnLocalSettingsChange ) {
356 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( "$IP/LocalSettings.php" ) ) );
357 }
358
359 if ( $wgAjaxUploadDestCheck ) {
360 $wgAjaxExportList[] = 'SpecialUpload::ajaxGetExistsWarning';
361 }
362
363 if ( $wgNewUserLog ) {
364 # Add a new log type
365 $wgLogTypes[] = 'newusers';
366 $wgLogNames['newusers'] = 'newuserlogpage';
367 $wgLogHeaders['newusers'] = 'newuserlogpagetext';
368 $wgLogActionsHandlers['newusers/newusers'] = 'NewUsersLogFormatter';
369 $wgLogActionsHandlers['newusers/create'] = 'NewUsersLogFormatter';
370 $wgLogActionsHandlers['newusers/create2'] = 'NewUsersLogFormatter';
371 $wgLogActionsHandlers['newusers/autocreate'] = 'NewUsersLogFormatter';
372 }
373
374 if ( $wgCookieSecure === 'detect' ) {
375 $wgCookieSecure = ( substr( $wgServer, 0, 6 ) === 'https:' );
376 }
377
378 // Disable MWDebug for command line mode, this prevents MWDebug from eating up
379 // all the memory from logging SQL queries on maintenance scripts
380 global $wgCommandLineMode;
381 if ( $wgDebugToolbar && !$wgCommandLineMode ) {
382 MWDebug::init();
383 }
384
385 if ( !defined( 'MW_COMPILED' ) ) {
386 if ( !MWInit::classExists( 'AutoLoader' ) ) {
387 require_once( "$IP/includes/AutoLoader.php" );
388 }
389
390 wfProfileIn( $fname . '-exception' );
391 MWExceptionHandler::installHandler();
392 wfProfileOut( $fname . '-exception' );
393
394 wfProfileIn( $fname . '-includes' );
395 require_once( "$IP/includes/normal/UtfNormalUtil.php" );
396 require_once( "$IP/includes/GlobalFunctions.php" );
397 require_once( "$IP/includes/ProxyTools.php" );
398 require_once( "$IP/includes/normal/UtfNormalDefines.php" );
399 wfProfileOut( $fname . '-includes' );
400 }
401
402 # Now that GlobalFunctions is loaded, set defaults that depend
403 # on it.
404 if ( $wgTmpDirectory === false ) {
405 $wgTmpDirectory = wfTempDir();
406 }
407
408 if ( $wgCanonicalServer === false ) {
409 $wgCanonicalServer = wfExpandUrl( $wgServer, PROTO_HTTP );
410 }
411
412 // Initialize $wgHTCPMulticastRouting from backwards-compatible settings
413 if ( !$wgHTCPMulticastRouting && $wgHTCPMulticastAddress ) {
414 $wgHTCPMulticastRouting = array(
415 '' => array(
416 'host' => $wgHTCPMulticastAddress,
417 'port' => $wgHTCPPort,
418 )
419 );
420 }
421
422 wfProfileIn( $fname . '-misc1' );
423
424 # Raise the memory limit if it's too low
425 wfMemoryLimit();
426
427 /**
428 * Set up the timezone, suppressing the pseudo-security warning in PHP 5.1+
429 * that happens whenever you use a date function without the timezone being
430 * explicitly set. Inspired by phpMyAdmin's treatment of the problem.
431 */
432 if ( is_null( $wgLocaltimezone) ) {
433 wfSuppressWarnings();
434 $wgLocaltimezone = date_default_timezone_get();
435 wfRestoreWarnings();
436 }
437
438 date_default_timezone_set( $wgLocaltimezone );
439 if( is_null( $wgLocalTZoffset ) ) {
440 $wgLocalTZoffset = date( 'Z' ) / 60;
441 }
442
443 # Useful debug output
444 if ( $wgCommandLineMode ) {
445 $wgRequest = new FauxRequest( array() );
446
447 wfDebug( "\n\nStart command line script $self\n" );
448 } else {
449 # Can't stub this one, it sets up $_GET and $_REQUEST in its constructor
450 $wgRequest = new WebRequest;
451
452 $debug = "\n\nStart request {$wgRequest->getMethod()} {$wgRequest->getRequestURL()}\n";
453
454 if ( $wgDebugPrintHttpHeaders ) {
455 $debug .= "HTTP HEADERS:\n";
456
457 foreach ( $wgRequest->getAllHeaders() as $name => $value ) {
458 $debug .= "$name: $value\n";
459 }
460 }
461 wfDebug( $debug );
462 }
463
464 wfProfileOut( $fname . '-misc1' );
465 wfProfileIn( $fname . '-memcached' );
466
467 $wgMemc = wfGetMainCache();
468 $messageMemc = wfGetMessageCacheStorage();
469 $parserMemc = wfGetParserCacheStorage();
470 $wgLangConvMemc = wfGetLangConverterCacheStorage();
471
472 wfDebug( 'CACHES: ' . get_class( $wgMemc ) . '[main] ' .
473 get_class( $messageMemc ) . '[message] ' .
474 get_class( $parserMemc ) . "[parser]\n" );
475
476 wfProfileOut( $fname . '-memcached' );
477
478 # # Most of the config is out, some might want to run hooks here.
479 wfRunHooks( 'SetupAfterCache' );
480
481 wfProfileIn( $fname . '-session' );
482
483 # If session.auto_start is there, we can't touch session name
484 if ( !wfIniGetBool( 'session.auto_start' ) ) {
485 session_name( $wgSessionName ? $wgSessionName : $wgCookiePrefix . '_session' );
486 }
487
488 if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) {
489 if ( $wgRequest->checkSessionCookie() || isset( $_COOKIE[$wgCookiePrefix . 'Token'] ) ) {
490 wfSetupSession();
491 $wgSessionStarted = true;
492 } else {
493 $wgSessionStarted = false;
494 }
495 }
496
497 wfProfileOut( $fname . '-session' );
498 wfProfileIn( $fname . '-globals' );
499
500 $wgContLang = Language::factory( $wgLanguageCode );
501 $wgContLang->initEncoding();
502 $wgContLang->initContLang();
503
504 // Now that variant lists may be available...
505 $wgRequest->interpolateTitle();
506 $wgUser = RequestContext::getMain()->getUser(); # BackCompat
507
508 /**
509 * @var $wgLang Language
510 */
511 $wgLang = new StubUserLang;
512
513 /**
514 * @var OutputPage
515 */
516 $wgOut = RequestContext::getMain()->getOutput(); # BackCompat
517
518 /**
519 * @var $wgParser Parser
520 */
521 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
522
523 if ( !is_object( $wgAuth ) ) {
524 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
525 wfRunHooks( 'AuthPluginSetup', array( &$wgAuth ) );
526 }
527
528 # Placeholders in case of DB error
529 $wgTitle = null;
530
531 $wgDeferredUpdateList = array();
532
533 wfProfileOut( $fname . '-globals' );
534 wfProfileIn( $fname . '-extensions' );
535
536 # Extension setup functions for extensions other than skins
537 # Entries should be added to this variable during the inclusion
538 # of the extension file. This allows the extension to perform
539 # any necessary initialisation in the fully initialised environment
540 foreach ( $wgExtensionFunctions as $func ) {
541 # Allow closures in PHP 5.3+
542 if ( is_object( $func ) && $func instanceof Closure ) {
543 $profName = $fname . '-extensions-closure';
544 } elseif ( is_array( $func ) ) {
545 if ( is_object( $func[0] ) )
546 $profName = $fname . '-extensions-' . get_class( $func[0] ) . '::' . $func[1];
547 else
548 $profName = $fname . '-extensions-' . implode( '::', $func );
549 } else {
550 $profName = $fname . '-extensions-' . strval( $func );
551 }
552
553 wfProfileIn( $profName );
554 call_user_func( $func );
555 wfProfileOut( $profName );
556 }
557
558 wfDebug( "Fully initialised\n" );
559 $wgFullyInitialised = true;
560
561 wfProfileOut( $fname . '-extensions' );
562 wfProfileOut( $fname );