45020a8c85fef60a2f09cdf155b549a828451afc
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof
4 * $wgSpecialPages is a list of all SpecialPage objects. These objects are
5 * either instances of SpecialPage or a sub-class thereof. They have an
6 * execute() method, which sends the HTML for the special page to $wgOut.
7 * The parent class has an execute() method which distributes the call to
8 * the historical global functions. Additionally, execute() also checks if the
9 * user has the necessary access privileges and bails out if not.
10 *
11 * To add a special page at run-time, use SpecialPage::addPage().
12 * DO NOT manipulate this array at run-time.
13 *
14 * @package MediaWiki
15 * @subpackage SpecialPage
16 */
17
18
19 /**
20 *
21 */
22 global $wgSpecialPages, $wgSpecialPageRedirects, $wgUser;
23
24 /**
25 * @access private
26 */
27 $wgSpecialPages = array(
28 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
29 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
30 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
31
32 'Userlogin' => new SpecialPage( 'Userlogin' ),
33 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
34 'Preferences' => new SpecialPage( 'Preferences' ),
35 'Watchlist' => new SpecialPage( 'Watchlist' ),
36
37 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
38 'Upload' => new SpecialPage( 'Upload' ),
39 'Imagelist' => new SpecialPage( 'Imagelist' ),
40 'Newimages' => new SpecialPage( 'Newimages' ),
41 'Listusers' => new SpecialPage( 'Listusers' ),
42 'Statistics' => new SpecialPage( 'Statistics' ),
43 'Randompage' => new SpecialPage( 'Randompage' ),
44 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
45 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
46 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
47 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
48 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
49 'Shortpages' => new SpecialPage( 'Shortpages' ),
50 'Longpages' => new SpecialPage( 'Longpages' ),
51 'Newpages' => new SpecialPage( 'Newpages' ),
52 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
53 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
54 'Allpages' => new SpecialPage( 'Allpages' ),
55 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
56 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
57 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
58 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
59 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
60 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
61 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
62 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
63 'Booksources' => new SpecialPage( 'Booksources' ),
64 'Categories' => new SpecialPage( 'Categories' ),
65 'Export' => new SpecialPage( 'Export' ),
66 'Version' => new SpecialPage( 'Version' ),
67 'Allmessages' => new SpecialPage( 'Allmessages' ),
68 'Log' => new SpecialPage( 'Log' ),
69 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
70 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
71 "Import" => new SpecialPage( "Import", 'import' ),
72 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
73 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
74 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
75 'Groups' => new SpecialPage( 'Groups' ),
76 );
77
78 /**
79 * Redirect Special:$key to somewhere else.
80 *
81 * @access private
82 */
83 $wgSpecialPageRedirects = array(
84 # My*
85 'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
86 'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
87 'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
88
89 # Deprecated specialpages
90 'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers' ),
91 );
92
93 global $wgUseValidation ;
94 if ( $wgUseValidation )
95 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
96
97 global $wgDisableCounters;
98 if( !$wgDisableCounters ) {
99 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
100 }
101
102 global $wgDisableInternalSearch;
103 if( !$wgDisableInternalSearch ) {
104 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
105 }
106
107 global $wgEmailAuthentication;
108 if( $wgEmailAuthentication ) {
109 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
110 }
111
112 /**
113 * Parent special page class, also static functions for handling the special
114 * page list
115 * @package MediaWiki
116 */
117 class SpecialPage
118 {
119 /**#@+
120 * @access private
121 */
122 /**
123 * The name of the class, used in the URL.
124 * Also used for the default <h1> heading, @see getDescription()
125 */
126 var $mName;
127 /**
128 * Minimum user level required to access this page, or "" for anyone.
129 * Also used to categorise the pages in Special:Specialpages
130 */
131 var $mRestriction;
132 /**
133 * Listed in Special:Specialpages?
134 */
135 var $mListed;
136 /**
137 * Function name called by the default execute()
138 */
139 var $mFunction;
140 /**
141 * File which needs to be included before the function above can be called
142 */
143 var $mFile;
144 /**#@-*/
145
146 /**
147 * Add a page to the list of valid special pages
148 * $obj->execute() must send HTML to $wgOut then return
149 * Use this for a special page extension
150 * @static
151 */
152 function addPage( &$obj ) {
153 global $wgSpecialPages;
154 $wgSpecialPages[$obj->mName] = $obj;
155 }
156
157 /**
158 * Remove a special page from the list
159 * Occasionally used to disable expensive or dangerous special pages
160 * @static
161 */
162 function removePage( $name ) {
163 global $wgSpecialPages;
164 unset( $wgSpecialPages[$name] );
165 }
166
167 /**
168 * Find the object with a given name and return it (or NULL)
169 * @static
170 * @param string $name
171 */
172 function &getPage( $name ) {
173 global $wgSpecialPages;
174 if ( array_key_exists( $name, $wgSpecialPages ) ) {
175 return $wgSpecialPages[$name];
176 } else {
177 return NULL;
178 }
179 }
180
181 /**
182 * @static
183 * @param string $name
184 * @return mixed Title object if the redirect exists, otherwise NULL
185 */
186 function &getRedirect( $name ) {
187 global $wgSpecialPageRedirects;
188 if ( array_key_exists( $name, $wgSpecialPageRedirects ) ) {
189 return $wgSpecialPageRedirects[$name];
190 } else {
191 return NULL;
192 }
193 }
194
195 /**
196 * Return categorised listable special pages
197 * Returns a 2d array where the first index is the restriction name
198 * @static
199 */
200 function getPages() {
201 global $wgSpecialPages;
202 $pages = array(
203 '' => array(),
204 'sysop' => array(),
205 'developer' => array()
206 );
207
208 foreach ( $wgSpecialPages as $name => $page ) {
209 if ( $page->isListed() ) {
210 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
211 }
212 }
213 return $pages;
214 }
215
216 /**
217 * Execute a special page path.
218 * The path may contain parameters, e.g. Special:Name/Params
219 * Extracts the special page name and call the execute method, passing the parameters
220 *
221 * @param $title should be a title object
222 */
223 function executePath( &$title ) {
224 global $wgSpecialPages, $wgOut, $wgTitle;
225
226 $bits = split( "/", $title->getDBkey(), 2 );
227 $name = $bits[0];
228 if( !isset( $bits[1] ) ) { // bug 2087
229 $par = NULL;
230 } else {
231 $par = $bits[1];
232 }
233
234 $page =& SpecialPage::getPage( $name );
235 if ( is_null( $page ) ) {
236 $redir =& SpecialPage::getRedirect( $name );
237 if ( isset( $redir ) ) {
238 $wgOut->redirect ($redir->getFullURL());
239 } else {
240 $wgOut->setArticleRelated( false );
241 $wgOut->setRobotpolicy( "noindex,follow" );
242 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
243 }
244 } else {
245 if($par !== NULL) {
246 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
247 } else {
248 $wgTitle = $title;
249 }
250
251 $page->execute( $par );
252 }
253 }
254
255 /**
256 * Default constructor for special pages
257 * Derivative classes should call this from their constructor
258 * Note that if the user does not have the required level, an error message will
259 * be displayed by the default execute() method, without the global function ever
260 * being called.
261 *
262 * If you override execute(), you can recover the default behaviour with userCanExecute()
263 * and displayRestrictionError()
264 *
265 * @param string $name Name of the special page, as seen in links and URLs
266 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
267 * @param boolean $listed Whether the page is listed in Special:Specialpages
268 * @param string $function Function called by execute(). By default it is constructed from $name
269 * @param string $file File which is included by execute(). It is also constructed from $name by default
270 */
271 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
272 $this->mName = $name;
273 $this->mRestriction = $restriction;
274 $this->mListed = $listed;
275 if ( $function == false ) {
276 $this->mFunction = 'wfSpecial'.$name;
277 } else {
278 $this->mFunction = $function;
279 }
280 if ( $file === 'default' ) {
281 $this->mFile = "Special{$name}.php";
282 } else {
283 $this->mFile = $file;
284 }
285 }
286
287 # Accessor functions, see the descriptions of the associated variables above
288 function getName() { return $this->mName; }
289 function getRestriction() { return $this->mRestriction; }
290 function isListed() { return $this->mListed; }
291 function getFile() { return $this->mFile; }
292
293 /**
294 * Checks if the given user (identified by an object) can execute this
295 * special page (as defined by $mRestriction)
296 */
297 function userCanExecute( &$user ) {
298 if ( $this->mRestriction == "" ) {
299 return true;
300 } else {
301 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
302 return true;
303 } else {
304 return false;
305 }
306 }
307 }
308
309 /**
310 * Output an error message telling the user what access level they have to have
311 */
312 function displayRestrictionError() {
313 global $wgOut;
314 if ( $this->mRestriction == "developer" ) {
315 $wgOut->developerRequired();
316 } else {
317 $wgOut->sysopRequired();
318 }
319 }
320
321 /**
322 * Sets headers - this should be called from the execute() method of all derived classes!
323 */
324 function setHeaders() {
325 global $wgOut;
326 $wgOut->setArticleRelated( false );
327 $wgOut->setRobotPolicy( "noindex,follow" );
328 $wgOut->setPageTitle( $this->getDescription() );
329 }
330
331 /**
332 * Default execute method
333 * Checks user permissions, calls the function given in mFunction
334 */
335 function execute( $par ) {
336 global $wgUser, $wgOut, $wgTitle;
337
338 $this->setHeaders();
339
340 if ( $this->userCanExecute( $wgUser ) ) {
341 if ( $this->mFile ) {
342 require_once( $this->mFile );
343 }
344 $func = $this->mFunction;
345 $func( $par );
346 } else {
347 $this->displayRestrictionError();
348 }
349 }
350
351 # Returns the name that goes in the <h1> in the special page itself, and also the name that
352 # will be listed in Special:Specialpages
353 #
354 # Derived classes can override this, but usually it is easier to keep the default behaviour.
355 # Messages can be added at run-time, see MessageCache.php
356 function getDescription() {
357 return wfMsg( strtolower( $this->mName ) );
358 }
359
360 /**
361 * Get a self-referential title object
362 */
363 function getTitle() {
364 return Title::makeTitle( NS_SPECIAL, $this->mName );
365 }
366
367 /**
368 * Set whether this page is listed in Special:Specialpages, at run-time
369 */
370 function setListed( $listed ) {
371 return wfSetVar( $this->mListed, $listed );
372 }
373 }
374
375 /**
376 * Shortcut to construct a special page which is unlisted by default
377 * @package MediaWiki
378 */
379 class UnlistedSpecialPage extends SpecialPage
380 {
381 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
382 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
383 }
384 }
385 ?>