abfbcb9eae49992aea045f3867da6d938c219970
[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 IncludableSpecialPage( '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 'Random' => 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 IncludableSpecialPage( '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 # Redirects
93 'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' ),
94 );
95
96 global $wgUseValidation ;
97 if ( $wgUseValidation )
98 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
99
100 global $wgDisableCounters;
101 if( !$wgDisableCounters ) {
102 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
103 }
104
105 global $wgDisableInternalSearch;
106 if( !$wgDisableInternalSearch ) {
107 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
108 }
109
110 global $wgEmailAuthentication;
111 if( $wgEmailAuthentication ) {
112 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
113 }
114
115 /**
116 * Parent special page class, also static functions for handling the special
117 * page list
118 * @package MediaWiki
119 */
120 class SpecialPage
121 {
122 /**#@+
123 * @access private
124 */
125 /**
126 * The name of the class, used in the URL.
127 * Also used for the default <h1> heading, @see getDescription()
128 */
129 var $mName;
130 /**
131 * Minimum user level required to access this page, or "" for anyone.
132 * Also used to categorise the pages in Special:Specialpages
133 */
134 var $mRestriction;
135 /**
136 * Listed in Special:Specialpages?
137 */
138 var $mListed;
139 /**
140 * Function name called by the default execute()
141 */
142 var $mFunction;
143 /**
144 * File which needs to be included before the function above can be called
145 */
146 var $mFile;
147 /**
148 * Whether or not this special page is being included from an article
149 */
150 var $mIncluding;
151 /**
152 * Whether the special page can be included in an article
153 */
154 var $mIncludable;
155
156
157 /**#@-*/
158
159
160 /**
161 * Add a page to the list of valid special pages
162 * $obj->execute() must send HTML to $wgOut then return
163 * Use this for a special page extension
164 * @static
165 */
166 function addPage( &$obj ) {
167 global $wgSpecialPages;
168 $wgSpecialPages[$obj->mName] = $obj;
169 }
170
171 /**
172 * Remove a special page from the list
173 * Occasionally used to disable expensive or dangerous special pages
174 * @static
175 */
176 function removePage( $name ) {
177 global $wgSpecialPages;
178 unset( $wgSpecialPages[$name] );
179 }
180
181 /**
182 * Find the object with a given name and return it (or NULL)
183 * @static
184 * @param string $name
185 */
186 function &getPage( $name ) {
187 global $wgSpecialPages;
188 if ( array_key_exists( $name, $wgSpecialPages ) ) {
189 return $wgSpecialPages[$name];
190 } else {
191 return NULL;
192 }
193 }
194
195 /**
196 * @static
197 * @param string $name
198 * @return mixed Title object if the redirect exists, otherwise NULL
199 */
200 function &getRedirect( $name ) {
201 global $wgSpecialPageRedirects;
202 if ( array_key_exists( $name, $wgSpecialPageRedirects ) ) {
203 return $wgSpecialPageRedirects[$name];
204 } else {
205 return NULL;
206 }
207 }
208
209 /**
210 * Return categorised listable special pages
211 * Returns a 2d array where the first index is the restriction name
212 * @static
213 */
214 function getPages() {
215 global $wgSpecialPages;
216 $pages = array(
217 '' => array(),
218 'sysop' => array(),
219 'developer' => array()
220 );
221
222 foreach ( $wgSpecialPages as $name => $page ) {
223 if ( $page->isListed() ) {
224 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
225 }
226 }
227 return $pages;
228 }
229
230 /**
231 * Execute a special page path.
232 * The path may contain parameters, e.g. Special:Name/Params
233 * Extracts the special page name and call the execute method, passing the parameters
234 *
235 * Returns a title object if the page is redirected, false if there was no such special
236 * page, and true if it was successful.
237 *
238 * @param $title a title object
239 * @param $including output is being captured for use in {{special:whatever}}
240 */
241 function executePath( &$title, $including = false ) {
242 global $wgSpecialPages, $wgOut, $wgTitle;
243
244 $bits = split( "/", $title->getDBkey(), 2 );
245 $name = $bits[0];
246 if( !isset( $bits[1] ) ) { // bug 2087
247 $par = NULL;
248 } else {
249 $par = $bits[1];
250 }
251
252 $page =& SpecialPage::getPage( $name );
253 if ( is_null( $page ) ) {
254 $redir =& SpecialPage::getRedirect( $name );
255 if ( isset( $redir ) ) {
256 if ( isset( $par ) )
257 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
258 else
259 $wgOut->redirect( $redir->getFullURL() );
260 $retVal = $redir;
261 } else {
262 $wgOut->setArticleRelated( false );
263 $wgOut->setRobotpolicy( "noindex,follow" );
264 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
265 $retVal = false;
266 }
267 } else {
268 if ( $including && !$page->includable() ) {
269 return false;
270 }
271 if($par !== NULL) {
272 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
273 } else {
274 $wgTitle = $title;
275 }
276 $page->including( $including );
277
278 $page->execute( $par );
279 $retVal = true;
280 }
281 return $retVal;
282 }
283
284 /**
285 * Just like executePath() except it returns the HTML instead of outputting it
286 * Returns false if there was no such special page, or a title object if it was
287 * a redirect.
288 * @static
289 */
290 function capturePath( &$title ) {
291 global $wgOut, $wgTitle;
292
293 $oldTitle = $wgTitle;
294 $oldOut = $wgOut;
295 $wgOut = new OutputPage;
296
297 $ret = SpecialPage::executePath( $title, true );
298 if ( $ret === true ) {
299 $ret = $wgOut->getHTML();
300 }
301 $wgTitle = $oldTitle;
302 $wgOut = $oldOut;
303 return $ret;
304 }
305
306 /**
307 * Default constructor for special pages
308 * Derivative classes should call this from their constructor
309 * Note that if the user does not have the required level, an error message will
310 * be displayed by the default execute() method, without the global function ever
311 * being called.
312 *
313 * If you override execute(), you can recover the default behaviour with userCanExecute()
314 * and displayRestrictionError()
315 *
316 * @param string $name Name of the special page, as seen in links and URLs
317 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
318 * @param boolean $listed Whether the page is listed in Special:Specialpages
319 * @param string $function Function called by execute(). By default it is constructed from $name
320 * @param string $file File which is included by execute(). It is also constructed from $name by default
321 */
322 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
323 $this->mName = $name;
324 $this->mRestriction = $restriction;
325 $this->mListed = $listed;
326 $this->mIncludable = $includable;
327 if ( $function == false ) {
328 $this->mFunction = 'wfSpecial'.$name;
329 } else {
330 $this->mFunction = $function;
331 }
332 if ( $file === 'default' ) {
333 $this->mFile = "Special{$name}.php";
334 } else {
335 $this->mFile = $file;
336 }
337 }
338
339 # Accessor functions, see the descriptions of the associated variables above
340 function getName() { return $this->mName; }
341 function getRestriction() { return $this->mRestriction; }
342 function isListed() { return $this->mListed; }
343 function getFile() { return $this->mFile; }
344 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
345 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
346
347 /**
348 * Checks if the given user (identified by an object) can execute this
349 * special page (as defined by $mRestriction)
350 */
351 function userCanExecute( &$user ) {
352 if ( $this->mRestriction == "" ) {
353 return true;
354 } else {
355 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
356 return true;
357 } else {
358 return false;
359 }
360 }
361 }
362
363 /**
364 * Output an error message telling the user what access level they have to have
365 */
366 function displayRestrictionError() {
367 global $wgOut;
368 if ( $this->mRestriction == "developer" ) {
369 $wgOut->developerRequired();
370 } else {
371 $wgOut->sysopRequired();
372 }
373 }
374
375 /**
376 * Sets headers - this should be called from the execute() method of all derived classes!
377 */
378 function setHeaders() {
379 global $wgOut;
380 $wgOut->setArticleRelated( false );
381 $wgOut->setRobotPolicy( "noindex,follow" );
382 $wgOut->setPageTitle( $this->getDescription() );
383 }
384
385 /**
386 * Default execute method
387 * Checks user permissions, calls the function given in mFunction
388 */
389 function execute( $par ) {
390 global $wgUser, $wgOut, $wgTitle;
391
392 $this->setHeaders();
393
394 if ( $this->userCanExecute( $wgUser ) ) {
395 if ( $this->mFile ) {
396 require_once( $this->mFile );
397 }
398 $func = $this->mFunction;
399 $func( $par, $this );
400 } else {
401 $this->displayRestrictionError();
402 }
403 }
404
405 # Returns the name that goes in the <h1> in the special page itself, and also the name that
406 # will be listed in Special:Specialpages
407 #
408 # Derived classes can override this, but usually it is easier to keep the default behaviour.
409 # Messages can be added at run-time, see MessageCache.php
410 function getDescription() {
411 return wfMsg( strtolower( $this->mName ) );
412 }
413
414 /**
415 * Get a self-referential title object
416 */
417 function getTitle() {
418 return Title::makeTitle( NS_SPECIAL, $this->mName );
419 }
420
421 /**
422 * Set whether this page is listed in Special:Specialpages, at run-time
423 */
424 function setListed( $listed ) {
425 return wfSetVar( $this->mListed, $listed );
426 }
427
428 }
429
430 /**
431 * Shortcut to construct a special page which is unlisted by default
432 * @package MediaWiki
433 */
434 class UnlistedSpecialPage extends SpecialPage
435 {
436 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
437 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
438 }
439 }
440
441 /**
442 * Shortcut to construct an includable special page
443 */
444 class IncludableSpecialPage extends SpecialPage
445 {
446 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
447 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
448 }
449 }
450 ?>