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