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