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