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