Split includes/SpecialPage.php
[mediawiki.git] / includes / specialpage / RedirectSpecialPage.php
blobeb8557a81525f9cfc3e26588f4e3c2089b16d31a
1 <?php
2 /**
3 * Shortcuts to construct a special page alias.
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Shortcut to construct a special page alias.
27 * @ingroup SpecialPage
29 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
31 // Query parameters that can be passed through redirects
32 protected $mAllowedRedirectParams = array();
34 // Query parameters added by redirects
35 protected $mAddedRedirectParams = array();
37 public function execute( $par ) {
38 $redirect = $this->getRedirect( $par );
39 $query = $this->getRedirectQuery();
40 // Redirect to a page title with possible query parameters
41 if ( $redirect instanceof Title ) {
42 $url = $redirect->getFullURL( $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 $class = get_class( $this );
52 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
56 /**
57 * If the special page is a redirect, then get the Title object it redirects to.
58 * False otherwise.
60 * @param string $par Subpage string
61 * @return Title|bool
63 abstract public function getRedirect( $par );
65 /**
66 * Return part of the request string for a special redirect page
67 * This allows passing, e.g. action=history to Special:Mypage, etc.
69 * @return String
71 public function getRedirectQuery() {
72 $params = array();
74 foreach ( $this->mAllowedRedirectParams as $arg ) {
75 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
76 $params[$arg] = $this->getRequest()->getVal( $arg );
80 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
81 $params[$arg] = $val;
84 return count( $params )
85 ? $params
86 : false;
90 /**
91 * @ingroup SpecialPage
93 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
94 // @todo FIXME: Visibility must be declared
95 var $redirName, $redirSubpage;
97 function __construct(
98 $name, $redirName, $redirSubpage = false,
99 $allowedRedirectParams = array(), $addedRedirectParams = array()
101 parent::__construct( $name );
102 $this->redirName = $redirName;
103 $this->redirSubpage = $redirSubpage;
104 $this->mAllowedRedirectParams = $allowedRedirectParams;
105 $this->mAddedRedirectParams = $addedRedirectParams;
108 public function getRedirect( $subpage ) {
109 if ( $this->redirSubpage === false ) {
110 return SpecialPage::getTitleFor( $this->redirName, $subpage );
111 } else {
112 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
118 * Superclass for any RedirectSpecialPage which redirects the user
119 * to a particular article (as opposed to user contributions, logs, etc.).
121 * For security reasons these special pages are restricted to pass on
122 * the following subset of GET parameters to the target page while
123 * removing all others:
125 * - useskin, uselang, printable: to alter the appearance of the resulting page
127 * - redirect: allows viewing one's user page or talk page even if it is a
128 * redirect.
130 * - rdfrom: allows redirecting to one's user page or talk page from an
131 * external wiki with the "Redirect from..." notice.
133 * - limit, offset: Useful for linking to history of one's own user page or
134 * user talk page. For example, this would be a link to "the last edit to your
135 * user talk page in the year 2010":
136 * http://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
138 * - feed: would allow linking to the current user's RSS feed for their user
139 * talk page:
140 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
142 * - preloadtitle: Can be used to provide a default section title for a
143 * preloaded new comment on one's own talk page.
145 * - summary : Can be used to provide a default edit summary for a preloaded
146 * edit to one's own user page or talk page.
148 * - preview: Allows showing/hiding preview on first edit regardless of user
149 * preference, useful for preloaded edits where you know preview wouldn't be
150 * useful.
152 * - internaledit, externaledit, mode: Allows forcing the use of the
153 * internal/external editor, e.g. to force the internal editor for
154 * short/simple preloaded edits.
156 * - redlink: Affects the message the user sees if their talk page/user talk
157 * page does not currently exist. Avoids confusion for newbies with no user
158 * pages over why they got a "permission error" following this link:
159 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
161 * - debug: determines whether the debug parameter is passed to load.php,
162 * which disables reformatting and allows scripts to be debugged. Useful
163 * when debugging scripts that manipulate one's own user page or talk page.
165 * @par Hook extension:
166 * Extensions can add to the redirect parameters list by using the hook
167 * RedirectSpecialArticleRedirectParams
169 * This hook allows extensions which add GET parameters like FlaggedRevs to
170 * retain those parameters when redirecting using special pages.
172 * @par Hook extension example:
173 * @code
174 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
175 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
176 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
177 * $redirectParams[] = 'stable';
178 * return true;
180 * @endcode
182 * @ingroup SpecialPage
184 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
185 function __construct( $name ) {
186 parent::__construct( $name );
187 $redirectParams = array(
188 'action',
189 'redirect', 'rdfrom',
190 # Options for preloaded edits
191 'preload', 'editintro', 'preloadtitle', 'summary', 'nosummary',
192 # Options for overriding user settings
193 'preview', 'internaledit', 'externaledit', 'mode', 'minor', 'watchthis',
194 # Options for history/diffs
195 'section', 'oldid', 'diff', 'dir',
196 'limit', 'offset', 'feed',
197 # Misc options
198 'redlink', 'debug',
199 # Options for action=raw; missing ctype can break JS or CSS in some browsers
200 'ctype', 'maxage', 'smaxage',
203 wfRunHooks( "RedirectSpecialArticleRedirectParams", array( &$redirectParams ) );
204 $this->mAllowedRedirectParams = $redirectParams;