SpecialPageFactory.php 22 KB

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