RedirectSpecialArticle.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Superclass for any RedirectSpecialPage which redirects the user
  25. * to a particular article (as opposed to user contributions, logs, etc.).
  26. *
  27. * For security reasons these special pages are restricted to pass on
  28. * the following subset of GET parameters to the target page while
  29. * removing all others:
  30. *
  31. * - useskin, uselang, printable: to alter the appearance of the resulting page
  32. *
  33. * - redirect: allows viewing one's user page or talk page even if it is a
  34. * redirect.
  35. *
  36. * - rdfrom: allows redirecting to one's user page or talk page from an
  37. * external wiki with the "Redirect from..." notice.
  38. *
  39. * - limit, offset: Useful for linking to history of one's own user page or
  40. * user talk page. For example, this would be a link to "the last edit to your
  41. * user talk page in the year 2010":
  42. * https://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
  43. *
  44. * - feed: would allow linking to the current user's RSS feed for their user
  45. * talk page:
  46. * https://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
  47. *
  48. * - preloadtitle: Can be used to provide a default section title for a
  49. * preloaded new comment on one's own talk page.
  50. *
  51. * - summary : Can be used to provide a default edit summary for a preloaded
  52. * edit to one's own user page or talk page.
  53. *
  54. * - preview: Allows showing/hiding preview on first edit regardless of user
  55. * preference, useful for preloaded edits where you know preview wouldn't be
  56. * useful.
  57. *
  58. * - redlink: Affects the message the user sees if their talk page/user talk
  59. * page does not currently exist. Avoids confusion for newbies with no user
  60. * pages over why they got a "permission error" following this link:
  61. * https://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
  62. *
  63. * - debug: determines whether the debug parameter is passed to load.php,
  64. * which disables reformatting and allows scripts to be debugged. Useful
  65. * when debugging scripts that manipulate one's own user page or talk page.
  66. *
  67. * @par Hook extension:
  68. * Extensions can add to the redirect parameters list by using the hook
  69. * RedirectSpecialArticleRedirectParams
  70. *
  71. * This hook allows extensions which add GET parameters like FlaggedRevs to
  72. * retain those parameters when redirecting using special pages.
  73. *
  74. * @par Hook extension example:
  75. * @code
  76. * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
  77. * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
  78. * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
  79. * $redirectParams[] = 'stable';
  80. * return true;
  81. * }
  82. * @endcode
  83. *
  84. * @ingroup SpecialPage
  85. */
  86. abstract class RedirectSpecialArticle extends RedirectSpecialPage {
  87. function __construct( $name ) {
  88. parent::__construct( $name );
  89. $redirectParams = [
  90. 'action',
  91. 'redirect', 'rdfrom',
  92. # Options for preloaded edits
  93. 'preload', 'preloadparams', 'editintro', 'preloadtitle', 'summary', 'nosummary',
  94. # Options for overriding user settings
  95. 'preview', 'minor', 'watchthis',
  96. # Options for history/diffs
  97. 'section', 'oldid', 'diff', 'dir',
  98. 'limit', 'offset', 'feed',
  99. # Misc options
  100. 'redlink',
  101. # Options for action=raw; missing ctype can break JS or CSS in some browsers
  102. 'ctype', 'maxage', 'smaxage',
  103. ];
  104. Hooks::run( "RedirectSpecialArticleRedirectParams", [ &$redirectParams ] );
  105. $this->mAllowedRedirectParams = $redirectParams;
  106. }
  107. /**
  108. * @inheritDoc
  109. */
  110. public function getRedirectQuery( $subpage ) {
  111. $query = parent::getRedirectQuery( $subpage );
  112. $title = $this->getRedirect( $subpage );
  113. // Avoid double redirect for action=edit&redlink=1 for existing pages
  114. // (compare to the check in EditPage::edit)
  115. if (
  116. $query && isset( $query['action'] ) && isset( $query['redlink'] ) &&
  117. ( $query['action'] === 'edit' || $query['action'] === 'submit' ) &&
  118. (bool)$query['redlink'] &&
  119. $title instanceof Title &&
  120. $title->exists()
  121. ) {
  122. return false;
  123. }
  124. return $query;
  125. }
  126. }