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