Add a way for packagers to override some installation details
[lhc/web/wiklou.git] / includes / SpecialPageFactory.php
1 <?php
2 /**
3 * Factory for handling the special page list and generating SpecialPage objects.
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 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
23 */
24
25 /**
26 * Factory for handling the special page list and generating SpecialPage objects.
27 *
28 * To add a special page in an extension, add to $wgSpecialPages either
29 * an object instance or an array containing the name and constructor
30 * parameters. The latter is preferred for performance reasons.
31 *
32 * The object instantiated must be either an instance of SpecialPage or a
33 * sub-class thereof. It must have an execute() method, which sends the HTML
34 * for the special page to $wgOut. The parent class has an execute() method
35 * which distributes the call to the historical global functions. Additionally,
36 * execute() also checks if the user has the necessary access privileges
37 * and bails out if not.
38 *
39 * To add a core special page, use the similar static list in
40 * SpecialPage::$mList. To remove a core static special page at runtime, use
41 * a SpecialPage_initList hook.
42 *
43 * @ingroup SpecialPage
44 * @since 1.17
45 */
46 class SpecialPageFactory {
47
48 /**
49 * List of special page names to the subclass of SpecialPage which handles them.
50 */
51 private static $mList = array(
52 // Maintenance Reports
53 'BrokenRedirects' => 'BrokenRedirectsPage',
54 'Deadendpages' => 'DeadendpagesPage',
55 'DoubleRedirects' => 'DoubleRedirectsPage',
56 'Longpages' => 'LongpagesPage',
57 'Ancientpages' => 'AncientpagesPage',
58 'Lonelypages' => 'LonelypagesPage',
59 'Fewestrevisions' => 'FewestrevisionsPage',
60 'Withoutinterwiki' => 'WithoutinterwikiPage',
61 'Protectedpages' => 'SpecialProtectedpages',
62 'Protectedtitles' => 'SpecialProtectedtitles',
63 'Shortpages' => 'ShortpagesPage',
64 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
65 'Uncategorizedimages' => 'UncategorizedimagesPage',
66 'Uncategorizedpages' => 'UncategorizedpagesPage',
67 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
68 'Unusedcategories' => 'UnusedcategoriesPage',
69 'Unusedimages' => 'UnusedimagesPage',
70 'Unusedtemplates' => 'UnusedtemplatesPage',
71 'Unwatchedpages' => 'UnwatchedpagesPage',
72 'Wantedcategories' => 'WantedcategoriesPage',
73 'Wantedfiles' => 'WantedfilesPage',
74 'Wantedpages' => 'WantedpagesPage',
75 'Wantedtemplates' => 'WantedtemplatesPage',
76
77 // List of pages
78 'Allpages' => 'SpecialAllpages',
79 'Prefixindex' => 'SpecialPrefixindex',
80 'Categories' => 'SpecialCategories',
81 'Disambiguations' => 'DisambiguationsPage',
82 'Listredirects' => 'ListredirectsPage',
83
84 // Login/create account
85 'Userlogin' => 'LoginForm',
86 'CreateAccount' => 'SpecialCreateAccount',
87
88 // Users and rights
89 'Block' => 'SpecialBlock',
90 'Unblock' => 'SpecialUnblock',
91 'BlockList' => 'SpecialBlockList',
92 'ChangePassword' => 'SpecialChangePassword',
93 'PasswordReset' => 'SpecialPasswordReset',
94 'DeletedContributions' => 'DeletedContributionsPage',
95 'Preferences' => 'SpecialPreferences',
96 'Contributions' => 'SpecialContributions',
97 'Listgrouprights' => 'SpecialListGroupRights',
98 'Listusers' => 'SpecialListUsers' ,
99 'Listadmins' => 'SpecialListAdmins',
100 'Listbots' => 'SpecialListBots',
101 'Activeusers' => 'SpecialActiveUsers',
102 'Userrights' => 'UserrightsPage',
103 'EditWatchlist' => 'SpecialEditWatchlist',
104
105 // Recent changes and logs
106 'Newimages' => 'SpecialNewFiles',
107 'Log' => 'SpecialLog',
108 'Watchlist' => 'SpecialWatchlist',
109 'Newpages' => 'SpecialNewpages',
110 'Recentchanges' => 'SpecialRecentchanges',
111 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
112 'Tags' => 'SpecialTags',
113
114 // Media reports and uploads
115 'Listfiles' => 'SpecialListFiles',
116 'Filepath' => 'SpecialFilepath',
117 'MIMEsearch' => 'MIMEsearchPage',
118 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
119 'Upload' => 'SpecialUpload',
120 'UploadStash' => 'SpecialUploadStash',
121
122 // Wiki data and tools
123 'Statistics' => 'SpecialStatistics',
124 'Allmessages' => 'SpecialAllmessages',
125 'Version' => 'SpecialVersion',
126 'Lockdb' => 'SpecialLockdb',
127 'Unlockdb' => 'SpecialUnlockdb',
128
129 // Redirecting special pages
130 'LinkSearch' => 'LinkSearchPage',
131 'Randompage' => 'Randompage',
132 'Randomredirect' => 'SpecialRandomredirect',
133
134 // High use pages
135 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
136 'Mostimages' => 'MostimagesPage',
137 'Mostlinked' => 'MostlinkedPage',
138 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
139 'Mostcategories' => 'MostcategoriesPage',
140 'Mostrevisions' => 'MostrevisionsPage',
141
142 // Page tools
143 'ComparePages' => 'SpecialComparePages',
144 'Export' => 'SpecialExport',
145 'Import' => 'SpecialImport',
146 'Undelete' => 'SpecialUndelete',
147 'Whatlinkshere' => 'SpecialWhatlinkshere',
148 'MergeHistory' => 'SpecialMergeHistory',
149
150 // Other
151 'Booksources' => 'SpecialBookSources',
152
153 // Unlisted / redirects
154 'Blankpage' => 'SpecialBlankpage',
155 'Blockme' => 'SpecialBlockme',
156 'Emailuser' => 'SpecialEmailUser',
157 'JavaScriptTest' => 'SpecialJavaScriptTest',
158 'Movepage' => 'MovePageForm',
159 'Mycontributions' => 'SpecialMycontributions',
160 'Mypage' => 'SpecialMypage',
161 'Mytalk' => 'SpecialMytalk',
162 'Myuploads' => 'SpecialMyuploads',
163 'PermanentLink' => 'SpecialPermanentLink',
164 'Revisiondelete' => 'SpecialRevisionDelete',
165 'Specialpages' => 'SpecialSpecialpages',
166 'Userlogout' => 'SpecialUserlogout',
167 );
168
169 private static $mAliases;
170
171 /**
172 * Initialise the special page list
173 * This must be called before accessing SpecialPage::$mList
174 *
175 * @return array
176 */
177 static function getList() {
178 global $wgSpecialPages;
179 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
180 global $wgEnableEmail;
181
182 if ( !is_object( self::$mList ) ) {
183 wfProfileIn( __METHOD__ );
184
185 if ( !$wgDisableCounters ) {
186 self::$mList['Popularpages'] = 'PopularpagesPage';
187 }
188
189 if ( !$wgDisableInternalSearch ) {
190 self::$mList['Search'] = 'SpecialSearch';
191 }
192
193 if ( $wgEmailAuthentication ) {
194 self::$mList['Confirmemail'] = 'EmailConfirmation';
195 self::$mList['Invalidateemail'] = 'EmailInvalidation';
196 }
197
198 if ( $wgEnableEmail ) {
199 self::$mList['ChangeEmail'] = 'SpecialChangeEmail';
200 }
201
202 // Add extension special pages
203 self::$mList = array_merge( self::$mList, $wgSpecialPages );
204
205 // Run hooks
206 // This hook can be used to remove undesired built-in special pages
207 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
208
209 // Cast to object: func()[$key] doesn't work, but func()->$key does
210 settype( self::$mList, 'object' );
211
212 wfProfileOut( __METHOD__ );
213 }
214 return self::$mList;
215 }
216
217 /**
218 * Initialise and return the list of special page aliases. Returns an object with
219 * properties which can be accessed $obj->pagename - each property is an array of
220 * aliases; the first in the array is the cannonical alias. All registered special
221 * pages are guaranteed to have a property entry, and for that property array to
222 * contain at least one entry (English fallbacks will be added if necessary).
223 * @return Object
224 */
225 static function getAliasList() {
226 if ( !is_object( self::$mAliases ) ) {
227 global $wgContLang;
228 $aliases = $wgContLang->getSpecialPageAliases();
229
230 // Objects are passed by reference by default, need to create a copy
231 $missingPages = clone self::getList();
232
233 self::$mAliases = array();
234 foreach ( $aliases as $realName => $aliasList ) {
235 foreach ( $aliasList as $alias ) {
236 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
237 }
238 unset( $missingPages->$realName );
239 }
240 foreach ( $missingPages as $name => $stuff ) {
241 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
242 }
243
244 // Cast to object: func()[$key] doesn't work, but func()->$key does
245 self::$mAliases = (object)self::$mAliases;
246 }
247 return self::$mAliases;
248 }
249
250 /**
251 * Given a special page name with a possible subpage, return an array
252 * where the first element is the special page name and the second is the
253 * subpage.
254 *
255 * @param $alias String
256 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
257 */
258 public static function resolveAlias( $alias ) {
259 global $wgContLang;
260 $bits = explode( '/', $alias, 2 );
261
262 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
263 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
264 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
265 $name = self::getAliasList()->$caseFoldedAlias;
266 } else {
267 return array( null, null );
268 }
269
270 if ( !isset( $bits[1] ) ) { // bug 2087
271 $par = null;
272 } else {
273 $par = $bits[1];
274 }
275
276 return array( $name, $par );
277 }
278
279 /**
280 * Add a page to a certain display group for Special:SpecialPages
281 *
282 * @param $page Mixed: SpecialPage or string
283 * @param $group String
284 */
285 public static function setGroup( $page, $group ) {
286 global $wgSpecialPageGroups;
287 $name = is_object( $page ) ? $page->getName() : $page;
288 $wgSpecialPageGroups[$name] = $group;
289 }
290
291 /**
292 * Get the group that the special page belongs in on Special:SpecialPage
293 *
294 * @param $page SpecialPage
295 * @return String
296 */
297 public static function getGroup( &$page ) {
298 $name = $page->getName();
299
300 global $wgSpecialPageGroups;
301 static $specialPageGroupsCache = array();
302 if ( isset( $specialPageGroupsCache[$name] ) ) {
303 return $specialPageGroupsCache[$name];
304 }
305 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $name ) );
306 if ( !$msg->isBlank() ) {
307 $group = $msg->text();
308 } else {
309 $group = isset( $wgSpecialPageGroups[$name] )
310 ? $wgSpecialPageGroups[$name]
311 : '-';
312 }
313 if ( $group == '-' ) {
314 $group = 'other';
315 }
316 $specialPageGroupsCache[$name] = $group;
317 return $group;
318 }
319
320 /**
321 * Check if a given name exist as a special page or as a special page alias
322 *
323 * @param $name String: name of a special page
324 * @return Boolean: true if a special page exists with this name
325 */
326 public static function exists( $name ) {
327 list( $title, /*...*/ ) = self::resolveAlias( $name );
328 return property_exists( self::getList(), $title );
329 }
330
331 /**
332 * Find the object with a given name and return it (or NULL)
333 *
334 * @param $name String Special page name, may be localised and/or an alias
335 * @return SpecialPage object or null if the page doesn't exist
336 */
337 public static function getPage( $name ) {
338 list( $realName, /*...*/ ) = self::resolveAlias( $name );
339 if ( property_exists( self::getList(), $realName ) ) {
340 $rec = self::getList()->$realName;
341 if ( is_string( $rec ) ) {
342 $className = $rec;
343 return new $className;
344 } elseif ( is_array( $rec ) ) {
345 // @deprecated, officially since 1.18, unofficially since forever
346 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
347 $className = array_shift( $rec );
348 self::getList()->$realName = MWFunction::newObj( $className, $rec );
349 }
350 return self::getList()->$realName;
351 } else {
352 return null;
353 }
354 }
355
356 /**
357 * Return categorised listable special pages which are available
358 * for the current user, and everyone.
359 *
360 * @param $user User object to check permissions, $wgUser will be used
361 * if not provided
362 * @return Array( String => Specialpage )
363 */
364 public static function getUsablePages( User $user = null ) {
365 $pages = array();
366 if ( $user === null ) {
367 global $wgUser;
368 $user = $wgUser;
369 }
370 foreach ( self::getList() as $name => $rec ) {
371 $page = self::getPage( $name );
372 if ( $page // not null
373 && $page->isListed()
374 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
375 ) {
376 $pages[$name] = $page;
377 }
378 }
379 return $pages;
380 }
381
382 /**
383 * Return categorised listable special pages for all users
384 *
385 * @return Array( String => Specialpage )
386 */
387 public static function getRegularPages() {
388 $pages = array();
389 foreach ( self::getList() as $name => $rec ) {
390 $page = self::getPage( $name );
391 if ( $page->isListed() && !$page->isRestricted() ) {
392 $pages[$name] = $page;
393 }
394 }
395 return $pages;
396 }
397
398 /**
399 * Return categorised listable special pages which are available
400 * for the current user, but not for everyone
401 *
402 * @return Array( String => Specialpage )
403 */
404 public static function getRestrictedPages() {
405 global $wgUser;
406 $pages = array();
407 foreach ( self::getList() as $name => $rec ) {
408 $page = self::getPage( $name );
409 if (
410 $page->isListed()
411 && $page->isRestricted()
412 && $page->userCanExecute( $wgUser )
413 ) {
414 $pages[$name] = $page;
415 }
416 }
417 return $pages;
418 }
419
420 /**
421 * Execute a special page path.
422 * The path may contain parameters, e.g. Special:Name/Params
423 * Extracts the special page name and call the execute method, passing the parameters
424 *
425 * Returns a title object if the page is redirected, false if there was no such special
426 * page, and true if it was successful.
427 *
428 * @param $title Title object
429 * @param $context IContextSource
430 * @param $including Bool output is being captured for use in {{special:whatever}}
431 *
432 * @return bool
433 */
434 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
435 wfProfileIn( __METHOD__ );
436
437 // @todo FIXME: Redirects broken due to this call
438 $bits = explode( '/', $title->getDBkey(), 2 );
439 $name = $bits[0];
440 if ( !isset( $bits[1] ) ) { // bug 2087
441 $par = null;
442 } else {
443 $par = $bits[1];
444 }
445 $page = self::getPage( $name );
446 // Nonexistent?
447 if ( !$page ) {
448 $context->getOutput()->setArticleRelated( false );
449 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
450
451 global $wgSend404Code;
452 if ( $wgSend404Code ) {
453 $context->getOutput()->setStatusCode( 404 );
454 }
455
456 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
457 wfProfileOut( __METHOD__ );
458 return false;
459 }
460
461 // Page exists, set the context
462 $page->setContext( $context );
463
464 if ( !$including ) {
465 // Redirect to canonical alias for GET commands
466 // Not for POST, we'd lose the post data, so it's best to just distribute
467 // the request. Such POST requests are possible for old extensions that
468 // generate self-links without being aware that their default name has
469 // changed.
470 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
471 $query = $context->getRequest()->getQueryValues();
472 unset( $query['title'] );
473 $query = wfArrayToCGI( $query );
474 $title = $page->getTitle( $par );
475 $url = $title->getFullUrl( $query );
476 $context->getOutput()->redirect( $url );
477 wfProfileOut( __METHOD__ );
478 return $title;
479 } else {
480 $context->setTitle( $page->getTitle( $par ) );
481 }
482
483 } elseif ( !$page->isIncludable() ) {
484 wfProfileOut( __METHOD__ );
485 return false;
486 }
487
488 $page->including( $including );
489
490 // Execute special page
491 $profName = 'Special:' . $page->getName();
492 wfProfileIn( $profName );
493 $page->execute( $par );
494 wfProfileOut( $profName );
495 wfProfileOut( __METHOD__ );
496 return true;
497 }
498
499 /**
500 * Just like executePath() but will override global variables and execute
501 * the page in "inclusion" mode. Returns true if the execution was
502 * successful or false if there was no such special page, or a title object
503 * if it was a redirect.
504 *
505 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
506 * variables so that the special page will get the context it'd expect on a
507 * normal request, and then restores them to their previous values after.
508 *
509 * @param $title Title
510 * @param $context IContextSource
511 *
512 * @return String: HTML fragment
513 */
514 static function capturePath( Title $title, IContextSource $context ) {
515 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
516
517 // Save current globals
518 $oldTitle = $wgTitle;
519 $oldOut = $wgOut;
520 $oldRequest = $wgRequest;
521 $oldUser = $wgUser;
522 $oldLang = $wgLang;
523
524 // Set the globals to the current context
525 $wgTitle = $title;
526 $wgOut = $context->getOutput();
527 $wgRequest = $context->getRequest();
528 $wgUser = $context->getUser();
529 $wgLang = $context->getLanguage();
530
531 // The useful part
532 $ret = self::executePath( $title, $context, true );
533
534 // And restore the old globals
535 $wgTitle = $oldTitle;
536 $wgOut = $oldOut;
537 $wgRequest = $oldRequest;
538 $wgUser = $oldUser;
539 $wgLang = $oldLang;
540
541 return $ret;
542 }
543
544 /**
545 * Get the local name for a specified canonical name
546 *
547 * @param $name String
548 * @param $subpage String|Bool
549 *
550 * @return String
551 */
552 static function getLocalNameFor( $name, $subpage = false ) {
553 global $wgContLang;
554 $aliases = $wgContLang->getSpecialPageAliases();
555
556 if ( isset( $aliases[$name][0] ) ) {
557 $name = $aliases[$name][0];
558 } else {
559 // Try harder in case someone misspelled the correct casing
560 $found = false;
561 foreach ( $aliases as $n => $values ) {
562 if ( strcasecmp( $name, $n ) === 0 ) {
563 wfWarn( "Found alias defined for $n when searching for " .
564 "special page aliases for $name. Case mismatch?" );
565 $name = $values[0];
566 $found = true;
567 break;
568 }
569 }
570 if ( !$found ) {
571 wfWarn( "Did not find alias for special page '$name'. " .
572 "Perhaps no aliases are defined for it?" );
573 }
574 }
575 if ( $subpage !== false && !is_null( $subpage ) ) {
576 $name = "$name/$subpage";
577 }
578 return $wgContLang->ucfirst( $name );
579 }
580
581 /**
582 * Get a title for a given alias
583 *
584 * @param $alias String
585 *
586 * @return Title or null if there is no such alias
587 */
588 static function getTitleForAlias( $alias ) {
589 $name = self::resolveAlias( $alias );
590 if ( $name ) {
591 return SpecialPage::getTitleFor( $name );
592 } else {
593 return null;
594 }
595 }
596 }