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