SpecialPageFactory.php 23 KB

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