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