Documentation
[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 * @return array
196 */
197 static function getAliasList() {
198 if ( !is_object( self::$mAliases ) ) {
199 global $wgContLang;
200 $aliases = $wgContLang->getSpecialPageAliases();
201
202 // Objects are passed by reference by default, need to create a copy
203 $missingPages = clone self::getList();
204
205 self::$mAliases = array();
206 foreach ( $aliases as $realName => $aliasList ) {
207 foreach ( $aliasList as $alias ) {
208 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
209 }
210 unset( $missingPages->$realName );
211 }
212 foreach ( $missingPages as $name => $stuff ) {
213 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
214 }
215
216 // Cast to object: func()[$key] doesn't work, but func()->$key does
217 self::$mAliases = (object)self::$mAliases;
218 }
219 return self::$mAliases;
220 }
221
222 /**
223 * Given a special page name with a possible subpage, return an array
224 * where the first element is the special page name and the second is the
225 * subpage.
226 *
227 * @param $alias String
228 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
229 */
230 public static function resolveAlias( $alias ) {
231 global $wgContLang;
232 $bits = explode( '/', $alias, 2 );
233
234 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
235 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
236 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
237 $name = self::getAliasList()->$caseFoldedAlias;
238 } else {
239 return array( null, null );
240 }
241
242 if ( !isset( $bits[1] ) ) { // bug 2087
243 $par = null;
244 } else {
245 $par = $bits[1];
246 }
247
248 return array( $name, $par );
249 }
250
251 /**
252 * Add a page to a certain display group for Special:SpecialPages
253 *
254 * @param $page Mixed: SpecialPage or string
255 * @param $group String
256 */
257 public static function setGroup( $page, $group ) {
258 global $wgSpecialPageGroups;
259 $name = is_object( $page ) ? $page->mName : $page;
260 $wgSpecialPageGroups[$name] = $group;
261 }
262
263 /**
264 * Add a page to a certain display group for Special:SpecialPages
265 *
266 * @param $page SpecialPage
267 */
268 public static function getGroup( &$page ) {
269 global $wgSpecialPageGroups;
270 static $specialPageGroupsCache = array();
271 if ( isset( $specialPageGroupsCache[$page->mName] ) ) {
272 return $specialPageGroupsCache[$page->mName];
273 }
274 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName ) );
275 if ( !$msg->isBlank() ) {
276 $group = $msg->text();
277 } else {
278 $group = isset( $wgSpecialPageGroups[$page->mName] )
279 ? $wgSpecialPageGroups[$page->mName]
280 : '-';
281 }
282 if ( $group == '-' ) {
283 $group = 'other';
284 }
285 $specialPageGroupsCache[$page->mName] = $group;
286 return $group;
287 }
288
289 /**
290 * Check if a given name exist as a special page or as a special page alias
291 *
292 * @param $name String: name of a special page
293 * @return Boolean: true if a special page exists with this name
294 */
295 public static function exists( $name ) {
296 list( $title, /*...*/ ) = self::resolveAlias( $name );
297 return property_exists( self::getList(), $title );
298 }
299
300 /**
301 * Find the object with a given name and return it (or NULL)
302 *
303 * @param $name String Special page name, may be localised and/or an alias
304 * @return SpecialPage object or null if the page doesn't exist
305 */
306 public static function getPage( $name ) {
307 list( $realName, /*...*/ ) = self::resolveAlias( $name );
308 if ( property_exists( self::getList(), $realName ) ) {
309 $rec = self::getList()->$realName;
310 if ( is_string( $rec ) ) {
311 $className = $rec;
312 self::getList()->$realName = new $className;
313 } elseif ( is_array( $rec ) ) {
314 // @deprecated, officially since 1.18, unofficially since forever
315 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
316 $className = array_shift( $rec );
317 self::getList()->$realName = MWFunction::newObj( $className, $rec );
318 }
319 return self::getList()->$realName;
320 } else {
321 return null;
322 }
323 }
324
325 /**
326 * Return categorised listable special pages which are available
327 * for the current user, and everyone.
328 *
329 * @return Array( String => Specialpage )
330 */
331 public static function getUsablePages() {
332 global $wgUser;
333 $pages = array();
334 foreach ( self::getList() as $name => $rec ) {
335 $page = self::getPage( $name );
336 if ( $page->isListed()
337 && (
338 !$page->isRestricted()
339 || $page->userCanExecute( $wgUser )
340 )
341 ) {
342 $pages[$name] = $page;
343 }
344 }
345 return $pages;
346 }
347
348 /**
349 * Return categorised listable special pages for all users
350 *
351 * @return Array( String => Specialpage )
352 */
353 public static function getRegularPages() {
354 $pages = array();
355 foreach ( self::getList() as $name => $rec ) {
356 $page = self::getPage( $name );
357 if ( $page->isListed() && !$page->isRestricted() ) {
358 $pages[$name] = $page;
359 }
360 }
361 return $pages;
362 }
363
364 /**
365 * Return categorised listable special pages which are available
366 * for the current user, but not for everyone
367 *
368 * @return Array( String => Specialpage )
369 */
370 public static function getRestrictedPages() {
371 global $wgUser;
372 $pages = array();
373 foreach ( self::getList() as $name => $rec ) {
374 $page = self::getPage( $name );
375 if (
376 $page->isListed()
377 && $page->isRestricted()
378 && $page->userCanExecute( $wgUser )
379 ) {
380 $pages[$name] = $page;
381 }
382 }
383 return $pages;
384 }
385
386 /**
387 * Execute a special page path.
388 * The path may contain parameters, e.g. Special:Name/Params
389 * Extracts the special page name and call the execute method, passing the parameters
390 *
391 * Returns a title object if the page is redirected, false if there was no such special
392 * page, and true if it was successful.
393 *
394 * @param $title Title object
395 * @param $context RequestContext
396 * @param $including Bool output is being captured for use in {{special:whatever}}
397 *
398 * @return bool
399 */
400 public static function executePath( Title &$title, RequestContext &$context, $including = false ) {
401 wfProfileIn( __METHOD__ );
402
403 // FIXME: redirects broken due to this call
404 $bits = explode( '/', $title->getDBkey(), 2 );
405 $name = $bits[0];
406 if ( !isset( $bits[1] ) ) { // bug 2087
407 $par = null;
408 } else {
409 $par = $bits[1];
410 }
411 $page = self::getPage( $name );
412 // Nonexistent?
413 if ( !$page ) {
414 $context->output->setArticleRelated( false );
415 $context->output->setRobotPolicy( 'noindex,nofollow' );
416 $context->output->setStatusCode( 404 );
417 $context->output->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
418 wfProfileOut( __METHOD__ );
419 return false;
420 }
421
422 // Page exists, set the context
423 $page->setContext( $context );
424
425 if ( !$including ) {
426 // Redirect to canonical alias for GET commands
427 // Not for POST, we'd lose the post data, so it's best to just distribute
428 // the request. Such POST requests are possible for old extensions that
429 // generate self-links without being aware that their default name has
430 // changed.
431 if ( $name != $page->getLocalName() && !$context->request->wasPosted() ) {
432 $query = $_GET;
433 unset( $query['title'] );
434 $query = wfArrayToCGI( $query );
435 $title = $page->getTitle( $par );
436 $url = $title->getFullUrl( $query );
437 $context->output->redirect( $url );
438 wfProfileOut( __METHOD__ );
439 return $title;
440 } else {
441 $context->title = $page->getTitle();
442 }
443
444 } elseif ( !$page->isIncludable() ) {
445 wfProfileOut( __METHOD__ );
446 return false;
447 }
448
449 $page->including( $including );
450
451 // Execute special page
452 $profName = 'Special:' . $page->getName();
453 wfProfileIn( $profName );
454 $page->execute( $par );
455 wfProfileOut( $profName );
456 wfProfileOut( __METHOD__ );
457 return true;
458 }
459
460 /**
461 * Just like executePath() except it returns the HTML instead of outputting it
462 * Returns false if there was no such special page, or a title object if it was
463 * a redirect.
464 *
465 * @param $title Title
466 *
467 * @return String: HTML fragment
468 */
469 static function capturePath( &$title ) {
470 global $wgOut, $wgTitle;
471
472 $oldTitle = $wgTitle;
473 $oldOut = $wgOut;
474
475 $context = new RequestContext;
476 $context->setTitle( $title );
477 $wgOut = $context->getOutput();
478
479 $ret = self::executePath( $title, $context, true );
480 if ( $ret === true ) {
481 $ret = $wgOut->getHTML();
482 }
483 $wgTitle = $oldTitle;
484 $wgOut = $oldOut;
485 return $ret;
486 }
487
488 /**
489 * Get the local name for a specified canonical name
490 *
491 * @param $name String
492 * @param $subpage String|Bool
493 *
494 * @return String
495 */
496 static function getLocalNameFor( $name, $subpage = false ) {
497 global $wgContLang;
498 $aliases = $wgContLang->getSpecialPageAliases();
499 if ( isset( $aliases[$name][0] ) ) {
500 $name = $aliases[$name][0];
501 } else {
502 // Try harder in case someone misspelled the correct casing
503 $found = false;
504 foreach ( $aliases as $n => $values ) {
505 if ( strcasecmp( $name, $n ) === 0 ) {
506 wfWarn( "Found alias defined for $n when searching for " .
507 "special page aliases for $name. Case mismatch?" );
508 $name = $values[0];
509 $found = true;
510 break;
511 }
512 }
513 if ( !$found ) {
514 wfWarn( "Did not find alias for special page '$name'. " .
515 "Perhaps no aliases are defined for it?" );
516 }
517 }
518 if ( $subpage !== false && !is_null( $subpage ) ) {
519 $name = "$name/$subpage";
520 }
521 return $wgContLang->ucfirst( $name );
522 }
523
524 /**
525 * Get a title for a given alias
526 *
527 * @param $alias String
528 *
529 * @return Title or null if there is no such alias
530 */
531 static function getTitleForAlias( $alias ) {
532 $name = self::resolveAlias( $alias );
533 if ( $name ) {
534 return SpecialPage::getTitleFor( $name );
535 } else {
536 return null;
537 }
538 }
539 }