Merge "Isolate globals for ContentSecurityPolicy tests"
[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 'PermanentLink' => \SpecialPermanentLink::class,
204 'Redirect' => \SpecialRedirect::class,
205 'Revisiondelete' => \SpecialRevisionDelete::class,
206 'RunJobs' => \SpecialRunJobs::class,
207 'Specialpages' => \SpecialSpecialpages::class,
208 'PageData' => \SpecialPageData::class,
209 ];
210
211 /** @var array Special page name => class name */
212 private $list;
213
214 /** @var array */
215 private $aliases;
216
217 /** @var ServiceOptions */
218 private $options;
219
220 /** @var Language */
221 private $contLang;
222
223 /**
224 * TODO Make this a const when HHVM support is dropped (T192166)
225 *
226 * @var array
227 * @since 1.33
228 * */
229 public static $constructorOptions = [
230 'ContentHandlerUseDB',
231 'DisableInternalSearch',
232 'EmailAuthentication',
233 'EnableEmail',
234 'EnableJavaScriptTest',
235 'EnableSpecialMute',
236 'PageLanguageUseDB',
237 'SpecialPages',
238 ];
239
240 /**
241 * @param ServiceOptions $options
242 * @param Language $contLang
243 */
244 public function __construct( ServiceOptions $options, Language $contLang ) {
245 $options->assertRequiredOptions( self::$constructorOptions );
246 $this->options = $options;
247 $this->contLang = $contLang;
248 }
249
250 /**
251 * Returns a list of canonical special page names.
252 * May be used to iterate over all registered special pages.
253 *
254 * @return string[]
255 */
256 public function getNames() : array {
257 return array_keys( $this->getPageList() );
258 }
259
260 /**
261 * Get the special page list as an array
262 *
263 * @return array
264 */
265 private function getPageList() : array {
266 if ( !is_array( $this->list ) ) {
267 $this->list = self::$coreList;
268
269 if ( !$this->options->get( 'DisableInternalSearch' ) ) {
270 $this->list['Search'] = \SpecialSearch::class;
271 }
272
273 if ( $this->options->get( 'EmailAuthentication' ) ) {
274 $this->list['Confirmemail'] = \EmailConfirmation::class;
275 $this->list['Invalidateemail'] = \EmailInvalidation::class;
276 }
277
278 if ( $this->options->get( 'EnableEmail' ) ) {
279 $this->list['ChangeEmail'] = \SpecialChangeEmail::class;
280 }
281
282 if ( $this->options->get( 'EnableJavaScriptTest' ) ) {
283 $this->list['JavaScriptTest'] = \SpecialJavaScriptTest::class;
284 }
285
286 if ( $this->options->get( 'EnableSpecialMute' ) ) {
287 $this->list['Mute'] = \SpecialMute::class;
288 }
289
290 if ( $this->options->get( 'PageLanguageUseDB' ) ) {
291 $this->list['PageLanguage'] = \SpecialPageLanguage::class;
292 }
293
294 if ( $this->options->get( 'ContentHandlerUseDB' ) ) {
295 $this->list['ChangeContentModel'] = \SpecialChangeContentModel::class;
296 }
297
298 // Add extension special pages
299 $this->list = array_merge( $this->list, $this->options->get( 'SpecialPages' ) );
300
301 // This hook can be used to disable unwanted core special pages
302 // or conditionally register special pages.
303 Hooks::run( 'SpecialPage_initList', [ &$this->list ] );
304
305 }
306
307 return $this->list;
308 }
309
310 /**
311 * Initialise and return the list of special page aliases. Returns an array where
312 * the key is an alias, and the value is the canonical name of the special page.
313 * All registered special pages are guaranteed to map to themselves.
314 * @return array
315 */
316 private function getAliasList() : array {
317 if ( is_null( $this->aliases ) ) {
318 $aliases = $this->contLang->getSpecialPageAliases();
319 $pageList = $this->getPageList();
320
321 $this->aliases = [];
322 $keepAlias = [];
323
324 // Force every canonical name to be an alias for itself.
325 foreach ( $pageList as $name => $stuff ) {
326 $caseFoldedAlias = $this->contLang->caseFold( $name );
327 $this->aliases[$caseFoldedAlias] = $name;
328 $keepAlias[$caseFoldedAlias] = 'canonical';
329 }
330
331 // Check for $aliases being an array since Language::getSpecialPageAliases can return null
332 if ( is_array( $aliases ) ) {
333 foreach ( $aliases as $realName => $aliasList ) {
334 $aliasList = array_values( $aliasList );
335 foreach ( $aliasList as $i => $alias ) {
336 $caseFoldedAlias = $this->contLang->caseFold( $alias );
337
338 if ( isset( $this->aliases[$caseFoldedAlias] ) &&
339 $realName === $this->aliases[$caseFoldedAlias]
340 ) {
341 // Ignore same-realName conflicts
342 continue;
343 }
344
345 if ( !isset( $keepAlias[$caseFoldedAlias] ) ) {
346 $this->aliases[$caseFoldedAlias] = $realName;
347 if ( !$i ) {
348 $keepAlias[$caseFoldedAlias] = 'first';
349 }
350 } elseif ( !$i ) {
351 wfWarn( "First alias '$alias' for $realName conflicts with " .
352 "{$keepAlias[$caseFoldedAlias]} alias for " .
353 $this->aliases[$caseFoldedAlias]
354 );
355 }
356 }
357 }
358 }
359 }
360
361 return $this->aliases;
362 }
363
364 /**
365 * Given a special page name with a possible subpage, return an array
366 * where the first element is the special page name and the second is the
367 * subpage.
368 *
369 * @param string $alias
370 * @return array [ String, String|null ], or [ null, null ] if the page is invalid
371 */
372 public function resolveAlias( $alias ) {
373 $bits = explode( '/', $alias, 2 );
374
375 $caseFoldedAlias = $this->contLang->caseFold( $bits[0] );
376 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
377 $aliases = $this->getAliasList();
378 if ( !isset( $aliases[$caseFoldedAlias] ) ) {
379 return [ null, null ];
380 }
381 $name = $aliases[$caseFoldedAlias];
382 $par = $bits[1] ?? null; // T4087
383
384 return [ $name, $par ];
385 }
386
387 /**
388 * Check if a given name exist as a special page or as a special page alias
389 *
390 * @param string $name Name of a special page
391 * @return bool True if a special page exists with this name
392 */
393 public function exists( $name ) {
394 list( $title, /*...*/ ) = $this->resolveAlias( $name );
395
396 $specialPageList = $this->getPageList();
397 return isset( $specialPageList[$title] );
398 }
399
400 /**
401 * Find the object with a given name and return it (or NULL)
402 *
403 * @param string $name Special page name, may be localised and/or an alias
404 * @return SpecialPage|null SpecialPage object or null if the page doesn't exist
405 */
406 public function getPage( $name ) {
407 list( $realName, /*...*/ ) = $this->resolveAlias( $name );
408
409 $specialPageList = $this->getPageList();
410
411 if ( isset( $specialPageList[$realName] ) ) {
412 $rec = $specialPageList[$realName];
413
414 if ( is_callable( $rec ) ) {
415 // Use callback to instantiate the special page
416 $page = $rec();
417 } elseif ( is_string( $rec ) ) {
418 $className = $rec;
419 $page = new $className;
420 } elseif ( $rec instanceof SpecialPage ) {
421 $page = $rec; // XXX: we should deep clone here
422 } else {
423 $page = null;
424 }
425
426 if ( $page instanceof SpecialPage ) {
427 return $page;
428 }
429
430 // It's not a classname, nor a callback, nor a legacy constructor array,
431 // nor a special page object. Give up.
432 wfLogWarning( "Cannot instantiate special page $realName: bad spec!" );
433 }
434
435 return null;
436 }
437
438 /**
439 * Return categorised listable special pages which are available
440 * for the current user, and everyone.
441 *
442 * @param User $user User object to check permissions
443 * provided
444 * @return array ( string => Specialpage )
445 */
446 public function getUsablePages( User $user ) : array {
447 $pages = [];
448 foreach ( $this->getPageList() as $name => $rec ) {
449 $page = $this->getPage( $name );
450 if ( $page ) { // not null
451 $page->setContext( RequestContext::getMain() );
452 if ( $page->isListed()
453 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
454 ) {
455 $pages[$name] = $page;
456 }
457 }
458 }
459
460 return $pages;
461 }
462
463 /**
464 * Return categorised listable special pages for all users
465 *
466 * @return array ( string => Specialpage )
467 */
468 public function getRegularPages() : array {
469 $pages = [];
470 foreach ( $this->getPageList() as $name => $rec ) {
471 $page = $this->getPage( $name );
472 if ( $page && $page->isListed() && !$page->isRestricted() ) {
473 $pages[$name] = $page;
474 }
475 }
476
477 return $pages;
478 }
479
480 /**
481 * Return categorised listable special pages which are available
482 * for the current user, but not for everyone
483 *
484 * @param User $user User object to use
485 * @return array ( string => Specialpage )
486 */
487 public function getRestrictedPages( User $user ) : array {
488 $pages = [];
489 foreach ( $this->getPageList() as $name => $rec ) {
490 $page = $this->getPage( $name );
491 if ( $page
492 && $page->isListed()
493 && $page->isRestricted()
494 && $page->userCanExecute( $user )
495 ) {
496 $pages[$name] = $page;
497 }
498 }
499
500 return $pages;
501 }
502
503 /**
504 * Execute a special page path.
505 * The path may contain parameters, e.g. Special:Name/Params
506 * Extracts the special page name and call the execute method, passing the parameters
507 *
508 * Returns a title object if the page is redirected, false if there was no such special
509 * page, and true if it was successful.
510 *
511 * @param Title &$title
512 * @param IContextSource &$context
513 * @param bool $including Bool output is being captured for use in {{special:whatever}}
514 * @param LinkRenderer|null $linkRenderer (since 1.28)
515 *
516 * @return bool|Title
517 */
518 public function executePath( Title &$title, IContextSource &$context, $including = false,
519 LinkRenderer $linkRenderer = null
520 ) {
521 // @todo FIXME: Redirects broken due to this call
522 $bits = explode( '/', $title->getDBkey(), 2 );
523 $name = $bits[0];
524 $par = $bits[1] ?? null; // T4087
525
526 $page = $this->getPage( $name );
527 if ( !$page ) {
528 $context->getOutput()->setArticleRelated( false );
529 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
530
531 global $wgSend404Code;
532 if ( $wgSend404Code ) {
533 $context->getOutput()->setStatusCode( 404 );
534 }
535
536 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
537
538 return false;
539 }
540
541 if ( !$including ) {
542 // Narrow DB query expectations for this HTTP request
543 $trxLimits = $context->getConfig()->get( 'TrxProfilerLimits' );
544 $trxProfiler = Profiler::instance()->getTransactionProfiler();
545 if ( $context->getRequest()->wasPosted() && !$page->doesWrites() ) {
546 $trxProfiler->setExpectations( $trxLimits['POST-nonwrite'], __METHOD__ );
547 $context->getRequest()->markAsSafeRequest();
548 }
549 }
550
551 // Page exists, set the context
552 $page->setContext( $context );
553
554 if ( !$including ) {
555 // Redirect to canonical alias for GET commands
556 // Not for POST, we'd lose the post data, so it's best to just distribute
557 // the request. Such POST requests are possible for old extensions that
558 // generate self-links without being aware that their default name has
559 // changed.
560 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
561 $query = $context->getRequest()->getQueryValues();
562 unset( $query['title'] );
563 $title = $page->getPageTitle( $par );
564 $url = $title->getFullURL( $query );
565 $context->getOutput()->redirect( $url );
566
567 return $title;
568 }
569
570 $context->setTitle( $page->getPageTitle( $par ) );
571 } elseif ( !$page->isIncludable() ) {
572 return false;
573 }
574
575 $page->including( $including );
576 if ( $linkRenderer ) {
577 $page->setLinkRenderer( $linkRenderer );
578 }
579
580 // Execute special page
581 $page->run( $par );
582
583 return true;
584 }
585
586 /**
587 * Just like executePath() but will override global variables and execute
588 * the page in "inclusion" mode. Returns true if the execution was
589 * successful or false if there was no such special page, or a title object
590 * if it was a redirect.
591 *
592 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
593 * variables so that the special page will get the context it'd expect on a
594 * normal request, and then restores them to their previous values after.
595 *
596 * @param Title $title
597 * @param IContextSource $context
598 * @param LinkRenderer|null $linkRenderer (since 1.28)
599 * @return string HTML fragment
600 */
601 public function capturePath(
602 Title $title, IContextSource $context, LinkRenderer $linkRenderer = null
603 ) {
604 global $wgTitle, $wgOut, $wgRequest, $wgUser, $wgLang;
605 $main = RequestContext::getMain();
606
607 // Save current globals and main context
608 $glob = [
609 'title' => $wgTitle,
610 'output' => $wgOut,
611 'request' => $wgRequest,
612 'user' => $wgUser,
613 'language' => $wgLang,
614 ];
615 $ctx = [
616 'title' => $main->getTitle(),
617 'output' => $main->getOutput(),
618 'request' => $main->getRequest(),
619 'user' => $main->getUser(),
620 'language' => $main->getLanguage(),
621 ];
622 if ( $main->canUseWikiPage() ) {
623 $ctx['wikipage'] = $main->getWikiPage();
624 }
625
626 // Override
627 $wgTitle = $title;
628 $wgOut = $context->getOutput();
629 $wgRequest = $context->getRequest();
630 $wgUser = $context->getUser();
631 $wgLang = $context->getLanguage();
632 $main->setTitle( $title );
633 $main->setOutput( $context->getOutput() );
634 $main->setRequest( $context->getRequest() );
635 $main->setUser( $context->getUser() );
636 $main->setLanguage( $context->getLanguage() );
637
638 // The useful part
639 $ret = $this->executePath( $title, $context, true, $linkRenderer );
640
641 // Restore old globals and context
642 $wgTitle = $glob['title'];
643 $wgOut = $glob['output'];
644 $wgRequest = $glob['request'];
645 $wgUser = $glob['user'];
646 $wgLang = $glob['language'];
647 $main->setTitle( $ctx['title'] );
648 $main->setOutput( $ctx['output'] );
649 $main->setRequest( $ctx['request'] );
650 $main->setUser( $ctx['user'] );
651 $main->setLanguage( $ctx['language'] );
652 if ( isset( $ctx['wikipage'] ) ) {
653 $main->setWikiPage( $ctx['wikipage'] );
654 }
655
656 return $ret;
657 }
658
659 /**
660 * Get the local name for a specified canonical name
661 *
662 * @param string $name
663 * @param string|bool $subpage
664 * @return string
665 */
666 public function getLocalNameFor( $name, $subpage = false ) {
667 $aliases = $this->contLang->getSpecialPageAliases();
668 $aliasList = $this->getAliasList();
669
670 // Find the first alias that maps back to $name
671 if ( isset( $aliases[$name] ) ) {
672 $found = false;
673 foreach ( $aliases[$name] as $alias ) {
674 $caseFoldedAlias = $this->contLang->caseFold( $alias );
675 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
676 if ( isset( $aliasList[$caseFoldedAlias] ) &&
677 $aliasList[$caseFoldedAlias] === $name
678 ) {
679 $name = $alias;
680 $found = true;
681 break;
682 }
683 }
684 if ( !$found ) {
685 wfWarn( "Did not find a usable alias for special page '$name'. " .
686 "It seems all defined aliases conflict?" );
687 }
688 } else {
689 // Check if someone misspelled the correct casing
690 if ( is_array( $aliases ) ) {
691 foreach ( $aliases as $n => $values ) {
692 if ( strcasecmp( $name, $n ) === 0 ) {
693 wfWarn( "Found alias defined for $n when searching for " .
694 "special page aliases for $name. Case mismatch?" );
695 return $this->getLocalNameFor( $n, $subpage );
696 }
697 }
698 }
699
700 wfWarn( "Did not find alias for special page '$name'. " .
701 "Perhaps no aliases are defined for it?" );
702 }
703
704 if ( $subpage !== false && !is_null( $subpage ) ) {
705 // Make sure it's in dbkey form
706 $subpage = str_replace( ' ', '_', $subpage );
707 $name = "$name/$subpage";
708 }
709
710 return $this->contLang->ucfirst( $name );
711 }
712
713 /**
714 * Get a title for a given alias
715 *
716 * @param string $alias
717 * @return Title|null Title or null if there is no such alias
718 */
719 public function getTitleForAlias( $alias ) {
720 list( $name, $subpage ) = $this->resolveAlias( $alias );
721 if ( $name != null ) {
722 return SpecialPage::getTitleFor( $name, $subpage );
723 }
724
725 return null;
726 }
727 }