8134c9a8296433edca848ae6fbe965804db0188f
[lhc/web/wiklou.git] / includes / specialpage / 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 namespace MediaWiki\Special;
26
27 use Hooks;
28 use IContextSource;
29 use Language;
30 use MediaWiki\Config\ServiceOptions;
31 use MediaWiki\Linker\LinkRenderer;
32 use Profiler;
33 use RequestContext;
34 use SpecialPage;
35 use Title;
36 use User;
37
38 /**
39 * Factory for handling the special page list and generating SpecialPage objects.
40 *
41 * To add a special page in an extension, add to $wgSpecialPages either
42 * an object instance or an array containing the name and constructor
43 * parameters. The latter is preferred for performance reasons.
44 *
45 * The object instantiated must be either an instance of SpecialPage or a
46 * sub-class thereof. It must have an execute() method, which sends the HTML
47 * for the special page to $wgOut. The parent class has an execute() method
48 * which distributes the call to the historical global functions. Additionally,
49 * execute() also checks if the user has the necessary access privileges
50 * and bails out if not.
51 *
52 * To add a core special page, use the similar static list in
53 * SpecialPageFactory::$list. To remove a core static special page at runtime, use
54 * a SpecialPage_initList hook.
55 *
56 * @note There are two classes called SpecialPageFactory. You should use this first one, in
57 * namespace MediaWiki\Special, which is a service. \SpecialPageFactory is a deprecated collection
58 * of static methods that forwards to the global service.
59 *
60 * @ingroup SpecialPage
61 * @since 1.17
62 */
63 class SpecialPageFactory {
64 /**
65 * List of special page names to the subclass of SpecialPage which handles them.
66 * @todo Make this a const when we drop HHVM support (T192166). It can still be private in PHP
67 * 7.1.
68 */
69 private static $coreList = [
70 // Maintenance Reports
71 'BrokenRedirects' => \BrokenRedirectsPage::class,
72 'Deadendpages' => \DeadendPagesPage::class,
73 'DoubleRedirects' => \DoubleRedirectsPage::class,
74 'Longpages' => \LongPagesPage::class,
75 'Ancientpages' => \AncientPagesPage::class,
76 'Lonelypages' => \LonelyPagesPage::class,
77 'Fewestrevisions' => \FewestrevisionsPage::class,
78 'Withoutinterwiki' => \WithoutInterwikiPage::class,
79 'Protectedpages' => \SpecialProtectedpages::class,
80 'Protectedtitles' => \SpecialProtectedtitles::class,
81 'Shortpages' => \ShortPagesPage::class,
82 'Uncategorizedcategories' => \UncategorizedCategoriesPage::class,
83 'Uncategorizedimages' => \UncategorizedImagesPage::class,
84 'Uncategorizedpages' => \UncategorizedPagesPage::class,
85 'Uncategorizedtemplates' => \UncategorizedTemplatesPage::class,
86 'Unusedcategories' => \UnusedCategoriesPage::class,
87 'Unusedimages' => \UnusedimagesPage::class,
88 'Unusedtemplates' => \UnusedtemplatesPage::class,
89 'Unwatchedpages' => \UnwatchedpagesPage::class,
90 'Wantedcategories' => \WantedCategoriesPage::class,
91 'Wantedfiles' => \WantedFilesPage::class,
92 'Wantedpages' => \WantedPagesPage::class,
93 'Wantedtemplates' => \WantedTemplatesPage::class,
94
95 // List of pages
96 'Allpages' => \SpecialAllPages::class,
97 'Prefixindex' => \SpecialPrefixindex::class,
98 'Categories' => \SpecialCategories::class,
99 'Listredirects' => \ListredirectsPage::class,
100 'PagesWithProp' => \SpecialPagesWithProp::class,
101 'TrackingCategories' => \SpecialTrackingCategories::class,
102
103 // Authentication
104 'Userlogin' => \SpecialUserLogin::class,
105 'Userlogout' => \SpecialUserLogout::class,
106 'CreateAccount' => \SpecialCreateAccount::class,
107 'LinkAccounts' => \SpecialLinkAccounts::class,
108 'UnlinkAccounts' => \SpecialUnlinkAccounts::class,
109 'ChangeCredentials' => \SpecialChangeCredentials::class,
110 'RemoveCredentials' => \SpecialRemoveCredentials::class,
111
112 // Users and rights
113 'Activeusers' => \SpecialActiveUsers::class,
114 'Block' => \SpecialBlock::class,
115 'Unblock' => \SpecialUnblock::class,
116 'BlockList' => \SpecialBlockList::class,
117 'AutoblockList' => \SpecialAutoblockList::class,
118 'ChangePassword' => \SpecialChangePassword::class,
119 'BotPasswords' => \SpecialBotPasswords::class,
120 'PasswordReset' => \SpecialPasswordReset::class,
121 'DeletedContributions' => \DeletedContributionsPage::class,
122 'Preferences' => \SpecialPreferences::class,
123 'ResetTokens' => \SpecialResetTokens::class,
124 'Contributions' => \SpecialContributions::class,
125 'Listgrouprights' => \SpecialListGroupRights::class,
126 'Listgrants' => \SpecialListGrants::class,
127 'Listusers' => \SpecialListUsers::class,
128 'Listadmins' => \SpecialListAdmins::class,
129 'Listbots' => \SpecialListBots::class,
130 'Userrights' => \UserrightsPage::class,
131 'EditWatchlist' => \SpecialEditWatchlist::class,
132 'PasswordPolicies' => \SpecialPasswordPolicies::class,
133
134 // Recent changes and logs
135 'Newimages' => \SpecialNewFiles::class,
136 'Log' => \SpecialLog::class,
137 'Watchlist' => \SpecialWatchlist::class,
138 'Newpages' => \SpecialNewpages::class,
139 'Recentchanges' => \SpecialRecentChanges::class,
140 'Recentchangeslinked' => \SpecialRecentChangesLinked::class,
141 'Tags' => \SpecialTags::class,
142
143 // Media reports and uploads
144 'Listfiles' => \SpecialListFiles::class,
145 'Filepath' => \SpecialFilepath::class,
146 'MediaStatistics' => \MediaStatisticsPage::class,
147 'MIMEsearch' => \MIMEsearchPage::class,
148 'FileDuplicateSearch' => \FileDuplicateSearchPage::class,
149 'Upload' => \SpecialUpload::class,
150 'UploadStash' => \SpecialUploadStash::class,
151 'ListDuplicatedFiles' => \ListDuplicatedFilesPage::class,
152
153 // Data and tools
154 'ApiSandbox' => \SpecialApiSandbox::class,
155 'Statistics' => \SpecialStatistics::class,
156 'Allmessages' => \SpecialAllMessages::class,
157 'Version' => \SpecialVersion::class,
158 'Lockdb' => \SpecialLockdb::class,
159 'Unlockdb' => \SpecialUnlockdb::class,
160
161 // Redirecting special pages
162 'LinkSearch' => \LinkSearchPage::class,
163 'Randompage' => \RandomPage::class,
164 'RandomInCategory' => \SpecialRandomInCategory::class,
165 'Randomredirect' => \SpecialRandomredirect::class,
166 'Randomrootpage' => \SpecialRandomrootpage::class,
167 'GoToInterwiki' => \SpecialGoToInterwiki::class,
168
169 // High use pages
170 'Mostlinkedcategories' => \MostlinkedCategoriesPage::class,
171 'Mostimages' => \MostimagesPage::class,
172 'Mostinterwikis' => \MostinterwikisPage::class,
173 'Mostlinked' => \MostlinkedPage::class,
174 'Mostlinkedtemplates' => \MostlinkedTemplatesPage::class,
175 'Mostcategories' => \MostcategoriesPage::class,
176 'Mostrevisions' => \MostrevisionsPage::class,
177
178 // Page tools
179 'ComparePages' => \SpecialComparePages::class,
180 'Export' => \SpecialExport::class,
181 'Import' => \SpecialImport::class,
182 'Undelete' => \SpecialUndelete::class,
183 'Whatlinkshere' => \SpecialWhatLinksHere::class,
184 'MergeHistory' => \SpecialMergeHistory::class,
185 'ExpandTemplates' => \SpecialExpandTemplates::class,
186
187 // Other
188 'Booksources' => \SpecialBookSources::class,
189
190 // Unlisted / redirects
191 'ApiHelp' => \SpecialApiHelp::class,
192 'Blankpage' => \SpecialBlankpage::class,
193 'Diff' => \SpecialDiff::class,
194 'EditTags' => \SpecialEditTags::class,
195 'Emailuser' => \SpecialEmailUser::class,
196 'Movepage' => \MovePageForm::class,
197 'Mycontributions' => \SpecialMycontributions::class,
198 'MyLanguage' => \SpecialMyLanguage::class,
199 'Mypage' => \SpecialMypage::class,
200 'Mytalk' => \SpecialMytalk::class,
201 'Myuploads' => \SpecialMyuploads::class,
202 'AllMyUploads' => \SpecialAllMyUploads::class,
203 'NewSection' => \SpecialNewSection::class,
204 'PermanentLink' => \SpecialPermanentLink::class,
205 'Redirect' => \SpecialRedirect::class,
206 'Revisiondelete' => \SpecialRevisionDelete::class,
207 'RunJobs' => \SpecialRunJobs::class,
208 'Specialpages' => \SpecialSpecialpages::class,
209 'PageData' => \SpecialPageData::class,
210 ];
211
212 /** @var array Special page name => class name */
213 private $list;
214
215 /** @var array */
216 private $aliases;
217
218 /** @var ServiceOptions */
219 private $options;
220
221 /** @var Language */
222 private $contLang;
223
224 /**
225 * TODO Make this a const when HHVM support is dropped (T192166)
226 *
227 * @var array
228 * @since 1.33
229 */
230 public static $constructorOptions = [
231 'ContentHandlerUseDB',
232 'DisableInternalSearch',
233 'EmailAuthentication',
234 'EnableEmail',
235 'EnableJavaScriptTest',
236 'EnableSpecialMute',
237 'PageLanguageUseDB',
238 'SpecialPages',
239 ];
240
241 /**
242 * @param ServiceOptions $options
243 * @param Language $contLang
244 */
245 public function __construct( ServiceOptions $options, Language $contLang ) {
246 $options->assertRequiredOptions( self::$constructorOptions );
247 $this->options = $options;
248 $this->contLang = $contLang;
249 }
250
251 /**
252 * Returns a list of canonical special page names.
253 * May be used to iterate over all registered special pages.
254 *
255 * @return string[]
256 */
257 public function getNames() : array {
258 return array_keys( $this->getPageList() );
259 }
260
261 /**
262 * Get the special page list as an array
263 *
264 * @return array
265 */
266 private function getPageList() : array {
267 if ( !is_array( $this->list ) ) {
268 $this->list = self::$coreList;
269
270 if ( !$this->options->get( 'DisableInternalSearch' ) ) {
271 $this->list['Search'] = \SpecialSearch::class;
272 }
273
274 if ( $this->options->get( 'EmailAuthentication' ) ) {
275 $this->list['Confirmemail'] = \EmailConfirmation::class;
276 $this->list['Invalidateemail'] = \EmailInvalidation::class;
277 }
278
279 if ( $this->options->get( 'EnableEmail' ) ) {
280 $this->list['ChangeEmail'] = \SpecialChangeEmail::class;
281 }
282
283 if ( $this->options->get( 'EnableJavaScriptTest' ) ) {
284 $this->list['JavaScriptTest'] = \SpecialJavaScriptTest::class;
285 }
286
287 if ( $this->options->get( 'EnableSpecialMute' ) ) {
288 $this->list['Mute'] = \SpecialMute::class;
289 }
290
291 if ( $this->options->get( 'PageLanguageUseDB' ) ) {
292 $this->list['PageLanguage'] = \SpecialPageLanguage::class;
293 }
294
295 if ( $this->options->get( 'ContentHandlerUseDB' ) ) {
296 $this->list['ChangeContentModel'] = \SpecialChangeContentModel::class;
297 }
298
299 // Add extension special pages
300 $this->list = array_merge( $this->list, $this->options->get( 'SpecialPages' ) );
301
302 // This hook can be used to disable unwanted core special pages
303 // or conditionally register special pages.
304 Hooks::run( 'SpecialPage_initList', [ &$this->list ] );
305
306 }
307
308 return $this->list;
309 }
310
311 /**
312 * Initialise and return the list of special page aliases. Returns an array where
313 * the key is an alias, and the value is the canonical name of the special page.
314 * All registered special pages are guaranteed to map to themselves.
315 * @return array
316 */
317 private function getAliasList() : array {
318 if ( is_null( $this->aliases ) ) {
319 $aliases = $this->contLang->getSpecialPageAliases();
320 $pageList = $this->getPageList();
321
322 $this->aliases = [];
323 $keepAlias = [];
324
325 // Force every canonical name to be an alias for itself.
326 foreach ( $pageList as $name => $stuff ) {
327 $caseFoldedAlias = $this->contLang->caseFold( $name );
328 $this->aliases[$caseFoldedAlias] = $name;
329 $keepAlias[$caseFoldedAlias] = 'canonical';
330 }
331
332 // Check for $aliases being an array since Language::getSpecialPageAliases can return null
333 if ( is_array( $aliases ) ) {
334 foreach ( $aliases as $realName => $aliasList ) {
335 $aliasList = array_values( $aliasList );
336 foreach ( $aliasList as $i => $alias ) {
337 $caseFoldedAlias = $this->contLang->caseFold( $alias );
338
339 if ( isset( $this->aliases[$caseFoldedAlias] ) &&
340 $realName === $this->aliases[$caseFoldedAlias]
341 ) {
342 // Ignore same-realName conflicts
343 continue;
344 }
345
346 if ( !isset( $keepAlias[$caseFoldedAlias] ) ) {
347 $this->aliases[$caseFoldedAlias] = $realName;
348 if ( !$i ) {
349 $keepAlias[$caseFoldedAlias] = 'first';
350 }
351 } elseif ( !$i ) {
352 wfWarn( "First alias '$alias' for $realName conflicts with " .
353 "{$keepAlias[$caseFoldedAlias]} alias for " .
354 $this->aliases[$caseFoldedAlias]
355 );
356 }
357 }
358 }
359 }
360 }
361
362 return $this->aliases;
363 }
364
365 /**
366 * Given a special page name with a possible subpage, return an array
367 * where the first element is the special page name and the second is the
368 * subpage.
369 *
370 * @param string $alias
371 * @return array [ String, String|null ], or [ null, null ] if the page is invalid
372 */
373 public function resolveAlias( $alias ) {
374 $bits = explode( '/', $alias, 2 );
375
376 $caseFoldedAlias = $this->contLang->caseFold( $bits[0] );
377 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
378 $aliases = $this->getAliasList();
379 if ( !isset( $aliases[$caseFoldedAlias] ) ) {
380 return [ null, null ];
381 }
382 $name = $aliases[$caseFoldedAlias];
383 $par = $bits[1] ?? null; // T4087
384
385 return [ $name, $par ];
386 }
387
388 /**
389 * Check if a given name exist as a special page or as a special page alias
390 *
391 * @param string $name Name of a special page
392 * @return bool True if a special page exists with this name
393 */
394 public function exists( $name ) {
395 list( $title, /*...*/ ) = $this->resolveAlias( $name );
396
397 $specialPageList = $this->getPageList();
398 return isset( $specialPageList[$title] );
399 }
400
401 /**
402 * Find the object with a given name and return it (or NULL)
403 *
404 * @param string $name Special page name, may be localised and/or an alias
405 * @return SpecialPage|null SpecialPage object or null if the page doesn't exist
406 */
407 public function getPage( $name ) {
408 list( $realName, /*...*/ ) = $this->resolveAlias( $name );
409
410 $specialPageList = $this->getPageList();
411
412 if ( isset( $specialPageList[$realName] ) ) {
413 $rec = $specialPageList[$realName];
414
415 if ( is_callable( $rec ) ) {
416 // Use callback to instantiate the special page
417 $page = $rec();
418 } elseif ( is_string( $rec ) ) {
419 $className = $rec;
420 $page = new $className;
421 } elseif ( $rec instanceof SpecialPage ) {
422 $page = $rec; // XXX: we should deep clone here
423 } else {
424 $page = null;
425 }
426
427 if ( $page instanceof SpecialPage ) {
428 return $page;
429 }
430
431 // It's not a classname, nor a callback, nor a legacy constructor array,
432 // nor a special page object. Give up.
433 wfLogWarning( "Cannot instantiate special page $realName: bad spec!" );
434 }
435
436 return null;
437 }
438
439 /**
440 * Return categorised listable special pages which are available
441 * for the current user, and everyone.
442 *
443 * @param User $user User object to check permissions
444 * provided
445 * @return array ( string => Specialpage )
446 */
447 public function getUsablePages( User $user ) : array {
448 $pages = [];
449 foreach ( $this->getPageList() as $name => $rec ) {
450 $page = $this->getPage( $name );
451 if ( $page ) { // not null
452 $page->setContext( RequestContext::getMain() );
453 if ( $page->isListed()
454 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
455 ) {
456 $pages[$name] = $page;
457 }
458 }
459 }
460
461 return $pages;
462 }
463
464 /**
465 * Return categorised listable special pages for all users
466 *
467 * @return array ( string => Specialpage )
468 */
469 public function getRegularPages() : array {
470 $pages = [];
471 foreach ( $this->getPageList() as $name => $rec ) {
472 $page = $this->getPage( $name );
473 if ( $page && $page->isListed() && !$page->isRestricted() ) {
474 $pages[$name] = $page;
475 }
476 }
477
478 return $pages;
479 }
480
481 /**
482 * Return categorised listable special pages which are available
483 * for the current user, but not for everyone
484 *
485 * @param User $user User object to use
486 * @return array ( string => Specialpage )
487 */
488 public function getRestrictedPages( User $user ) : array {
489 $pages = [];
490 foreach ( $this->getPageList() as $name => $rec ) {
491 $page = $this->getPage( $name );
492 if ( $page
493 && $page->isListed()
494 && $page->isRestricted()
495 && $page->userCanExecute( $user )
496 ) {
497 $pages[$name] = $page;
498 }
499 }
500
501 return $pages;
502 }
503
504 /**
505 * Execute a special page path.
506 * The path may contain parameters, e.g. Special:Name/Params
507 * Extracts the special page name and call the execute method, passing the parameters
508 *
509 * Returns a title object if the page is redirected, false if there was no such special
510 * page, and true if it was successful.
511 *
512 * @param Title &$title
513 * @param IContextSource &$context
514 * @param bool $including Bool output is being captured for use in {{special:whatever}}
515 * @param LinkRenderer|null $linkRenderer (since 1.28)
516 *
517 * @return bool|Title
518 */
519 public function executePath( Title &$title, IContextSource &$context, $including = false,
520 LinkRenderer $linkRenderer = null
521 ) {
522 // @todo FIXME: Redirects broken due to this call
523 $bits = explode( '/', $title->getDBkey(), 2 );
524 $name = $bits[0];
525 $par = $bits[1] ?? null; // T4087
526
527 $page = $this->getPage( $name );
528 if ( !$page ) {
529 $context->getOutput()->setArticleRelated( false );
530 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
531
532 global $wgSend404Code;
533 if ( $wgSend404Code ) {
534 $context->getOutput()->setStatusCode( 404 );
535 }
536
537 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
538
539 return false;
540 }
541
542 if ( !$including ) {
543 // Narrow DB query expectations for this HTTP request
544 $trxLimits = $context->getConfig()->get( 'TrxProfilerLimits' );
545 $trxProfiler = Profiler::instance()->getTransactionProfiler();
546 if ( $context->getRequest()->wasPosted() && !$page->doesWrites() ) {
547 $trxProfiler->setExpectations( $trxLimits['POST-nonwrite'], __METHOD__ );
548 $context->getRequest()->markAsSafeRequest();
549 }
550 }
551
552 // Page exists, set the context
553 $page->setContext( $context );
554
555 if ( !$including ) {
556 // Redirect to canonical alias for GET commands
557 // Not for POST, we'd lose the post data, so it's best to just distribute
558 // the request. Such POST requests are possible for old extensions that
559 // generate self-links without being aware that their default name has
560 // changed.
561 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
562 $query = $context->getRequest()->getQueryValues();
563 unset( $query['title'] );
564 $title = $page->getPageTitle( $par );
565 $url = $title->getFullURL( $query );
566 $context->getOutput()->redirect( $url );
567
568 return $title;
569 }
570
571 // @phan-suppress-next-line PhanUndeclaredMethod
572 $context->setTitle( $page->getPageTitle( $par ) );
573 } elseif ( !$page->isIncludable() ) {
574 return false;
575 }
576
577 $page->including( $including );
578 if ( $linkRenderer ) {
579 $page->setLinkRenderer( $linkRenderer );
580 }
581
582 // Execute special page
583 $page->run( $par );
584
585 return true;
586 }
587
588 /**
589 * Just like executePath() but will override global variables and execute
590 * the page in "inclusion" mode. Returns true if the execution was
591 * successful or false if there was no such special page, or a title object
592 * if it was a redirect.
593 *
594 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
595 * variables so that the special page will get the context it'd expect on a
596 * normal request, and then restores them to their previous values after.
597 *
598 * @param Title $title
599 * @param IContextSource $context
600 * @param LinkRenderer|null $linkRenderer (since 1.28)
601 * @return string HTML fragment
602 */
603 public function capturePath(
604 Title $title, IContextSource $context, LinkRenderer $linkRenderer = null
605 ) {
606 global $wgTitle, $wgOut, $wgRequest, $wgUser, $wgLang;
607 $main = RequestContext::getMain();
608
609 // Save current globals and main context
610 $glob = [
611 'title' => $wgTitle,
612 'output' => $wgOut,
613 'request' => $wgRequest,
614 'user' => $wgUser,
615 'language' => $wgLang,
616 ];
617 $ctx = [
618 'title' => $main->getTitle(),
619 'output' => $main->getOutput(),
620 'request' => $main->getRequest(),
621 'user' => $main->getUser(),
622 'language' => $main->getLanguage(),
623 ];
624 if ( $main->canUseWikiPage() ) {
625 $ctx['wikipage'] = $main->getWikiPage();
626 }
627
628 // Override
629 $wgTitle = $title;
630 $wgOut = $context->getOutput();
631 $wgRequest = $context->getRequest();
632 $wgUser = $context->getUser();
633 $wgLang = $context->getLanguage();
634 $main->setTitle( $title );
635 $main->setOutput( $context->getOutput() );
636 $main->setRequest( $context->getRequest() );
637 $main->setUser( $context->getUser() );
638 $main->setLanguage( $context->getLanguage() );
639
640 // The useful part
641 $ret = $this->executePath( $title, $context, true, $linkRenderer );
642
643 // Restore old globals and context
644 $wgTitle = $glob['title'];
645 $wgOut = $glob['output'];
646 $wgRequest = $glob['request'];
647 $wgUser = $glob['user'];
648 $wgLang = $glob['language'];
649 $main->setTitle( $ctx['title'] );
650 $main->setOutput( $ctx['output'] );
651 $main->setRequest( $ctx['request'] );
652 $main->setUser( $ctx['user'] );
653 $main->setLanguage( $ctx['language'] );
654 if ( isset( $ctx['wikipage'] ) ) {
655 $main->setWikiPage( $ctx['wikipage'] );
656 }
657
658 return $ret;
659 }
660
661 /**
662 * Get the local name for a specified canonical name
663 *
664 * @param string $name
665 * @param string|bool $subpage
666 * @return string
667 */
668 public function getLocalNameFor( $name, $subpage = false ) {
669 $aliases = $this->contLang->getSpecialPageAliases();
670 $aliasList = $this->getAliasList();
671
672 // Find the first alias that maps back to $name
673 if ( isset( $aliases[$name] ) ) {
674 $found = false;
675 foreach ( $aliases[$name] as $alias ) {
676 $caseFoldedAlias = $this->contLang->caseFold( $alias );
677 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
678 if ( isset( $aliasList[$caseFoldedAlias] ) &&
679 $aliasList[$caseFoldedAlias] === $name
680 ) {
681 $name = $alias;
682 $found = true;
683 break;
684 }
685 }
686 if ( !$found ) {
687 wfWarn( "Did not find a usable alias for special page '$name'. " .
688 "It seems all defined aliases conflict?" );
689 }
690 } else {
691 // Check if someone misspelled the correct casing
692 if ( is_array( $aliases ) ) {
693 foreach ( $aliases as $n => $values ) {
694 if ( strcasecmp( $name, $n ) === 0 ) {
695 wfWarn( "Found alias defined for $n when searching for " .
696 "special page aliases for $name. Case mismatch?" );
697 return $this->getLocalNameFor( $n, $subpage );
698 }
699 }
700 }
701
702 wfWarn( "Did not find alias for special page '$name'. " .
703 "Perhaps no aliases are defined for it?" );
704 }
705
706 if ( $subpage !== false && !is_null( $subpage ) ) {
707 // Make sure it's in dbkey form
708 $subpage = str_replace( ' ', '_', $subpage );
709 $name = "$name/$subpage";
710 }
711
712 return $this->contLang->ucfirst( $name );
713 }
714
715 /**
716 * Get a title for a given alias
717 *
718 * @param string $alias
719 * @return Title|null Title or null if there is no such alias
720 */
721 public function getTitleForAlias( $alias ) {
722 list( $name, $subpage ) = $this->resolveAlias( $alias );
723 if ( $name != null ) {
724 return SpecialPage::getTitleFor( $name, $subpage );
725 }
726
727 return null;
728 }
729 }