RedirectSpecialPage.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. */
  36. public function execute( $subpage ) {
  37. $redirect = $this->getRedirect( $subpage );
  38. $query = $this->getRedirectQuery( $subpage );
  39. if ( $redirect instanceof Title ) {
  40. // Redirect to a page title with possible query parameters
  41. $url = $redirect->getFullUrlForRedirect( $query );
  42. $this->getOutput()->redirect( $url );
  43. } elseif ( $redirect === true ) {
  44. // Redirect to index.php with query parameters
  45. $url = wfAppendQuery( wfScript( 'index' ), $query );
  46. $this->getOutput()->redirect( $url );
  47. } else {
  48. $this->showNoRedirectPage();
  49. }
  50. }
  51. /**
  52. * If the special page is a redirect, then get the Title object it redirects to.
  53. * False otherwise.
  54. *
  55. * @param string|null $subpage
  56. * @return Title|bool
  57. */
  58. abstract public function getRedirect( $subpage );
  59. /**
  60. * Return part of the request string for a special redirect page
  61. * This allows passing, e.g. action=history to Special:Mypage, etc.
  62. *
  63. * @param string|null $subpage
  64. * @return array|bool
  65. */
  66. public function getRedirectQuery( $subpage ) {
  67. $params = [];
  68. $request = $this->getRequest();
  69. foreach ( array_merge( $this->mAllowedRedirectParams,
  70. [ 'uselang', 'useskin', 'debug', 'safemode' ] // parameters which can be passed to all pages
  71. ) as $arg ) {
  72. if ( $request->getVal( $arg, null ) !== null ) {
  73. $params[$arg] = $request->getVal( $arg );
  74. } elseif ( $request->getArray( $arg, null ) !== null ) {
  75. $params[$arg] = $request->getArray( $arg );
  76. }
  77. }
  78. foreach ( $this->mAddedRedirectParams as $arg => $val ) {
  79. $params[$arg] = $val;
  80. }
  81. return count( $params )
  82. ? $params
  83. : false;
  84. }
  85. /**
  86. * Indicate if the target of this redirect can be used to identify
  87. * a particular user of this wiki (e.g., if the redirect is to the
  88. * user page of a User). See T109724.
  89. *
  90. * @since 1.27
  91. * @return bool
  92. */
  93. public function personallyIdentifiableTarget() {
  94. return false;
  95. }
  96. protected function showNoRedirectPage() {
  97. $class = static::class;
  98. throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
  99. }
  100. }