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