fix some issues with phpdoc
[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, $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 IncludableSpecialPage( '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 );
76
77 global $wgUseValidation ;
78 if ( $wgUseValidation )
79 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
80
81 global $wgDisableCounters;
82 if( !$wgDisableCounters ) {
83 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
84 }
85
86 global $wgDisableInternalSearch;
87 if( !$wgDisableInternalSearch ) {
88 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
89 }
90
91 global $wgEmailAuthentication;
92 if( $wgEmailAuthentication ) {
93 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
94 }
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' );
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
234 $bits = split( "/", $title->getDBkey(), 2 );
235 $name = $bits[0];
236 if( !isset( $bits[1] ) ) { // bug 2087
237 $par = NULL;
238 } else {
239 $par = $bits[1];
240 }
241
242 $page =& SpecialPage::getPage( $name );
243 if ( is_null( $page ) ) {
244 if ( $including ) {
245 return false;
246 } else {
247 $redir =& SpecialPage::getRedirect( $name );
248 if ( isset( $redir ) ) {
249 if ( isset( $par ) )
250 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
251 else
252 $wgOut->redirect( $redir->getFullURL() );
253 $retVal = $redir;
254 } else {
255 $wgOut->setArticleRelated( false );
256 $wgOut->setRobotpolicy( "noindex,follow" );
257 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
258 $retVal = false;
259 }
260 }
261 } else {
262 if ( $including && !$page->includable() ) {
263 return false;
264 }
265 if($par !== NULL) {
266 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
267 } else {
268 $wgTitle = $title;
269 }
270 $page->including( $including );
271
272 $page->execute( $par );
273 $retVal = true;
274 }
275 return $retVal;
276 }
277
278 /**
279 * Just like executePath() except it returns the HTML instead of outputting it
280 * Returns false if there was no such special page, or a title object if it was
281 * a redirect.
282 * @static
283 */
284 function capturePath( &$title ) {
285 global $wgOut, $wgTitle;
286
287 $oldTitle = $wgTitle;
288 $oldOut = $wgOut;
289 $wgOut = new OutputPage;
290
291 $ret = SpecialPage::executePath( $title, true );
292 if ( $ret === true ) {
293 $ret = $wgOut->getHTML();
294 }
295 $wgTitle = $oldTitle;
296 $wgOut = $oldOut;
297 return $ret;
298 }
299
300 /**
301 * Default constructor for special pages
302 * Derivative classes should call this from their constructor
303 * Note that if the user does not have the required level, an error message will
304 * be displayed by the default execute() method, without the global function ever
305 * being called.
306 *
307 * If you override execute(), you can recover the default behaviour with userCanExecute()
308 * and displayRestrictionError()
309 *
310 * @param string $name Name of the special page, as seen in links and URLs
311 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
312 * @param boolean $listed Whether the page is listed in Special:Specialpages
313 * @param string $function Function called by execute(). By default it is constructed from $name
314 * @param string $file File which is included by execute(). It is also constructed from $name by default
315 */
316 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
317 $this->mName = $name;
318 $this->mRestriction = $restriction;
319 $this->mListed = $listed;
320 $this->mIncludable = $includable;
321 if ( $function == false ) {
322 $this->mFunction = 'wfSpecial'.$name;
323 } else {
324 $this->mFunction = $function;
325 }
326 if ( $file === 'default' ) {
327 $this->mFile = "Special{$name}.php";
328 } else {
329 $this->mFile = $file;
330 }
331 }
332
333 # Accessor functions, see the descriptions of the associated variables above
334 function getName() { return $this->mName; }
335 function getRestriction() { return $this->mRestriction; }
336 function isListed() { return $this->mListed; }
337 function getFile() { return $this->mFile; }
338 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
339 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
340
341 /**
342 * Checks if the given user (identified by an object) can execute this
343 * special page (as defined by $mRestriction)
344 */
345 function userCanExecute( &$user ) {
346 if ( $this->mRestriction == "" ) {
347 return true;
348 } else {
349 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
350 return true;
351 } else {
352 return false;
353 }
354 }
355 }
356
357 /**
358 * Output an error message telling the user what access level they have to have
359 */
360 function displayRestrictionError() {
361 global $wgOut;
362 $wgOut->permissionRequired( $this->mRestriction );
363 }
364
365 /**
366 * Sets headers - this should be called from the execute() method of all derived classes!
367 */
368 function setHeaders() {
369 global $wgOut;
370 $wgOut->setArticleRelated( false );
371 $wgOut->setRobotPolicy( "noindex,follow" );
372 $wgOut->setPageTitle( $this->getDescription() );
373 }
374
375 /**
376 * Default execute method
377 * Checks user permissions, calls the function given in mFunction
378 */
379 function execute( $par ) {
380 global $wgUser, $wgOut, $wgTitle;
381
382 $this->setHeaders();
383
384 if ( $this->userCanExecute( $wgUser ) ) {
385 if ( $this->mFile ) {
386 require_once( $this->mFile );
387 }
388 $func = $this->mFunction;
389 $func( $par, $this );
390 } else {
391 $this->displayRestrictionError();
392 }
393 }
394
395 # Returns the name that goes in the <h1> in the special page itself, and also the name that
396 # will be listed in Special:Specialpages
397 #
398 # Derived classes can override this, but usually it is easier to keep the default behaviour.
399 # Messages can be added at run-time, see MessageCache.php
400 function getDescription() {
401 return wfMsg( strtolower( $this->mName ) );
402 }
403
404 /**
405 * Get a self-referential title object
406 */
407 function getTitle() {
408 return Title::makeTitle( NS_SPECIAL, $this->mName );
409 }
410
411 /**
412 * Set whether this page is listed in Special:Specialpages, at run-time
413 */
414 function setListed( $listed ) {
415 return wfSetVar( $this->mListed, $listed );
416 }
417
418 }
419
420 /**
421 * Shortcut to construct a special page which is unlisted by default
422 * @package MediaWiki
423 */
424 class UnlistedSpecialPage extends SpecialPage
425 {
426 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
427 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
428 }
429 }
430
431 /**
432 * Shortcut to construct an includable special page
433 * @package MediaWiki
434 */
435 class IncludableSpecialPage extends SpecialPage
436 {
437 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
438 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
439 }
440 }
441 ?>