RedirectSpecialPage.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Shortcuts to construct a special page alias.
  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. */
  23. /**
  24. * Shortcut to construct a special page alias.
  25. *
  26. * @ingroup SpecialPage
  27. */
  28. abstract class RedirectSpecialPage extends UnlistedSpecialPage {
  29. // Query parameters that can be passed through redirects
  30. protected $mAllowedRedirectParams = [];
  31. // Query parameters added by redirects
  32. protected $mAddedRedirectParams = [];
  33. /**
  34. * @param string|null $subpage
  35. * @return Title|bool
  36. */
  37. public function execute( $subpage ) {
  38. $redirect = $this->getRedirect( $subpage );
  39. $query = $this->getRedirectQuery();
  40. // Redirect to a page title with possible query parameters
  41. if ( $redirect instanceof Title ) {
  42. $url = $redirect->getFullUrlForRedirect( $query );
  43. $this->getOutput()->redirect( $url );
  44. return $redirect;
  45. } elseif ( $redirect === true ) {
  46. // Redirect to index.php with query parameters
  47. $url = wfAppendQuery( wfScript( 'index' ), $query );
  48. $this->getOutput()->redirect( $url );
  49. return $redirect;
  50. } else {
  51. $this->showNoRedirectPage();
  52. }
  53. }
  54. /**
  55. * If the special page is a redirect, then get the Title object it redirects to.
  56. * False otherwise.
  57. *
  58. * @param string|null $subpage
  59. * @return Title|bool
  60. */
  61. abstract public function getRedirect( $subpage );
  62. /**
  63. * Return part of the request string for a special redirect page
  64. * This allows passing, e.g. action=history to Special:Mypage, etc.
  65. *
  66. * @return array|bool
  67. */
  68. public function getRedirectQuery() {
  69. $params = [];
  70. $request = $this->getRequest();
  71. foreach ( array_merge( $this->mAllowedRedirectParams,
  72. [ 'uselang', 'useskin', 'debug' ] // parameters which can be passed to all pages
  73. ) as $arg ) {
  74. if ( $request->getVal( $arg, null ) !== null ) {
  75. $params[$arg] = $request->getVal( $arg );
  76. } elseif ( $request->getArray( $arg, null ) !== null ) {
  77. $params[$arg] = $request->getArray( $arg );
  78. }
  79. }
  80. foreach ( $this->mAddedRedirectParams as $arg => $val ) {
  81. $params[$arg] = $val;
  82. }
  83. return count( $params )
  84. ? $params
  85. : false;
  86. }
  87. /**
  88. * Indicate if the target of this redirect can be used to identify
  89. * a particular user of this wiki (e.g., if the redirect is to the
  90. * user page of a User). See T109724.
  91. *
  92. * @since 1.27
  93. * @return bool
  94. */
  95. public function personallyIdentifiableTarget() {
  96. return false;
  97. }
  98. protected function showNoRedirectPage() {
  99. $class = static::class;
  100. throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
  101. }
  102. }
  103. /**
  104. * @ingroup SpecialPage
  105. */
  106. abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
  107. /** @var string Name of redirect target */
  108. protected $redirName;
  109. /** @var string Name of subpage of redirect target */
  110. protected $redirSubpage;
  111. function __construct(
  112. $name, $redirName, $redirSubpage = false,
  113. $allowedRedirectParams = [], $addedRedirectParams = []
  114. ) {
  115. parent::__construct( $name );
  116. $this->redirName = $redirName;
  117. $this->redirSubpage = $redirSubpage;
  118. $this->mAllowedRedirectParams = $allowedRedirectParams;
  119. $this->mAddedRedirectParams = $addedRedirectParams;
  120. }
  121. /**
  122. * @param string|null $subpage
  123. * @return Title|bool
  124. */
  125. public function getRedirect( $subpage ) {
  126. if ( $this->redirSubpage === false ) {
  127. return SpecialPage::getTitleFor( $this->redirName, $subpage );
  128. }
  129. return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
  130. }
  131. }
  132. /**
  133. * Superclass for any RedirectSpecialPage which redirects the user
  134. * to a particular article (as opposed to user contributions, logs, etc.).
  135. *
  136. * For security reasons these special pages are restricted to pass on
  137. * the following subset of GET parameters to the target page while
  138. * removing all others:
  139. *
  140. * - useskin, uselang, printable: to alter the appearance of the resulting page
  141. *
  142. * - redirect: allows viewing one's user page or talk page even if it is a
  143. * redirect.
  144. *
  145. * - rdfrom: allows redirecting to one's user page or talk page from an
  146. * external wiki with the "Redirect from..." notice.
  147. *
  148. * - limit, offset: Useful for linking to history of one's own user page or
  149. * user talk page. For example, this would be a link to "the last edit to your
  150. * user talk page in the year 2010":
  151. * https://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
  152. *
  153. * - feed: would allow linking to the current user's RSS feed for their user
  154. * talk page:
  155. * https://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
  156. *
  157. * - preloadtitle: Can be used to provide a default section title for a
  158. * preloaded new comment on one's own talk page.
  159. *
  160. * - summary : Can be used to provide a default edit summary for a preloaded
  161. * edit to one's own user page or talk page.
  162. *
  163. * - preview: Allows showing/hiding preview on first edit regardless of user
  164. * preference, useful for preloaded edits where you know preview wouldn't be
  165. * useful.
  166. *
  167. * - redlink: Affects the message the user sees if their talk page/user talk
  168. * page does not currently exist. Avoids confusion for newbies with no user
  169. * pages over why they got a "permission error" following this link:
  170. * https://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
  171. *
  172. * - debug: determines whether the debug parameter is passed to load.php,
  173. * which disables reformatting and allows scripts to be debugged. Useful
  174. * when debugging scripts that manipulate one's own user page or talk page.
  175. *
  176. * @par Hook extension:
  177. * Extensions can add to the redirect parameters list by using the hook
  178. * RedirectSpecialArticleRedirectParams
  179. *
  180. * This hook allows extensions which add GET parameters like FlaggedRevs to
  181. * retain those parameters when redirecting using special pages.
  182. *
  183. * @par Hook extension example:
  184. * @code
  185. * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
  186. * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
  187. * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
  188. * $redirectParams[] = 'stable';
  189. * return true;
  190. * }
  191. * @endcode
  192. *
  193. * @ingroup SpecialPage
  194. */
  195. abstract class RedirectSpecialArticle extends RedirectSpecialPage {
  196. function __construct( $name ) {
  197. parent::__construct( $name );
  198. $redirectParams = [
  199. 'action',
  200. 'redirect', 'rdfrom',
  201. # Options for preloaded edits
  202. 'preload', 'preloadparams', 'editintro', 'preloadtitle', 'summary', 'nosummary',
  203. # Options for overriding user settings
  204. 'preview', 'minor', 'watchthis',
  205. # Options for history/diffs
  206. 'section', 'oldid', 'diff', 'dir',
  207. 'limit', 'offset', 'feed',
  208. # Misc options
  209. 'redlink',
  210. # Options for action=raw; missing ctype can break JS or CSS in some browsers
  211. 'ctype', 'maxage', 'smaxage',
  212. ];
  213. Hooks::run( "RedirectSpecialArticleRedirectParams", [ &$redirectParams ] );
  214. $this->mAllowedRedirectParams = $redirectParams;
  215. }
  216. }