updated
[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 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
40 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
41 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
42 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
43 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
44 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
45 'Mostlinked' => new SpecialPage( 'Mostlinked' ),
46 'Shortpages' => new SpecialPage( 'Shortpages' ),
47 'Longpages' => new SpecialPage( 'Longpages' ),
48 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
49 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
50 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
51 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
52 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
53 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
54 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
55 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
56 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
57 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
58 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
59 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
60 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
61 'Booksources' => new SpecialPage( 'Booksources' ),
62 'Categories' => new SpecialPage( 'Categories' ),
63 'Export' => new SpecialPage( 'Export' ),
64 'Version' => new SpecialPage( 'Version' ),
65 'Allmessages' => new SpecialPage( 'Allmessages' ),
66 'Log' => new SpecialPage( 'Log' ),
67 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
68 'Undelete' => new SpecialPage( 'Undelete' ),
69 "Import" => new SpecialPage( "Import", 'import' ),
70 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
71 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
72 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
73 'MIMEsearch' => new SpecialPage( 'MIMEsearch' ),
74 );
75
76 if ( $wgUseValidation )
77 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
78
79 if( !$wgDisableCounters ) {
80 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
81 }
82
83 if( !$wgDisableInternalSearch ) {
84 $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
85 }
86
87 if( $wgEmailAuthentication ) {
88 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
89 }
90
91 if ( $wgEnableUnwatchedpages )
92 $wgSpecialPages['Unwatchedpages'] = new SpecialPage( 'Unwatchedpages' );
93
94 /**
95 * Parent special page class, also static functions for handling the special
96 * page list
97 * @package MediaWiki
98 */
99 class SpecialPage
100 {
101 /**#@+
102 * @access private
103 */
104 /**
105 * The name of the class, used in the URL.
106 * Also used for the default <h1> heading, @see getDescription()
107 */
108 var $mName;
109 /**
110 * Minimum user level required to access this page, or "" for anyone.
111 * Also used to categorise the pages in Special:Specialpages
112 */
113 var $mRestriction;
114 /**
115 * Listed in Special:Specialpages?
116 */
117 var $mListed;
118 /**
119 * Function name called by the default execute()
120 */
121 var $mFunction;
122 /**
123 * File which needs to be included before the function above can be called
124 */
125 var $mFile;
126 /**
127 * Whether or not this special page is being included from an article
128 */
129 var $mIncluding;
130 /**
131 * Whether the special page can be included in an article
132 */
133 var $mIncludable;
134
135
136 /**#@-*/
137
138
139 /**
140 * Add a page to the list of valid special pages
141 * $obj->execute() must send HTML to $wgOut then return
142 * Use this for a special page extension
143 * @static
144 */
145 function addPage( &$obj ) {
146 global $wgSpecialPages;
147 $wgSpecialPages[$obj->mName] = $obj;
148 }
149
150 /**
151 * Remove a special page from the list
152 * Occasionally used to disable expensive or dangerous special pages
153 * @static
154 */
155 function removePage( $name ) {
156 global $wgSpecialPages;
157 unset( $wgSpecialPages[$name] );
158 }
159
160 /**
161 * Find the object with a given name and return it (or NULL)
162 * @static
163 * @param string $name
164 */
165 function getPage( $name ) {
166 global $wgSpecialPages;
167 if ( array_key_exists( $name, $wgSpecialPages ) ) {
168 return $wgSpecialPages[$name];
169 } else {
170 return NULL;
171 }
172 }
173
174 /**
175 * @static
176 * @param string $name
177 * @return mixed Title object if the redirect exists, otherwise NULL
178 */
179 function getRedirect( $name ) {
180 global $wgUser;
181 switch ( $name ) {
182 case 'Mypage':
183 return Title::makeTitle( NS_USER, $wgUser->getName() );
184 case 'Mytalk':
185 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
186 case 'Mycontributions':
187 return Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() );
188 case 'Listadmins':
189 return Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ); # @bug 2832
190 case 'Randompage':
191 return Title::makeTitle( NS_SPECIAL, 'Random' );
192 default:
193 return NULL;
194 }
195 }
196
197 /**
198 * Return categorised listable special pages
199 * Returns a 2d array where the first index is the restriction name
200 * @static
201 */
202 function getPages() {
203 global $wgSpecialPages;
204 $pages = array(
205 '' => array(),
206 'sysop' => array(),
207 'developer' => array()
208 );
209
210 foreach ( $wgSpecialPages as $name => $page ) {
211 if ( $page->isListed() ) {
212 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
213 }
214 }
215 return $pages;
216 }
217
218 /**
219 * Execute a special page path.
220 * The path may contain parameters, e.g. Special:Name/Params
221 * Extracts the special page name and call the execute method, passing the parameters
222 *
223 * Returns a title object if the page is redirected, false if there was no such special
224 * page, and true if it was successful.
225 *
226 * @param $title a title object
227 * @param $including output is being captured for use in {{special:whatever}}
228 */
229 function executePath( &$title, $including = false ) {
230 global $wgSpecialPages, $wgOut, $wgTitle;
231 $fname = 'SpecialPage::executePath';
232 wfProfileIn( $fname );
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 wfProfileOut( $fname );
246 return false;
247 } else {
248 $redir = SpecialPage::getRedirect( $name );
249 if ( isset( $redir ) ) {
250 if ( isset( $par ) )
251 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
252 else
253 $wgOut->redirect( $redir->getFullURL() );
254 $retVal = $redir;
255 } else {
256 $wgOut->setArticleRelated( false );
257 $wgOut->setRobotpolicy( "noindex,follow" );
258 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
259 $retVal = false;
260 }
261 }
262 } else {
263 if ( $including && !$page->includable() ) {
264 wfProfileOut( $fname );
265 return false;
266 }
267 if($par !== NULL) {
268 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
269 } else {
270 $wgTitle = $title;
271 }
272 $page->including( $including );
273
274 $profName = 'Special:' . $page->getName();
275 wfProfileIn( $profName );
276 $page->execute( $par );
277 wfProfileOut( $profName );
278 $retVal = true;
279 }
280 wfProfileOut( $fname );
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 $wgOut->permissionRequired( $this->mRestriction );
369 }
370
371 /**
372 * Sets headers - this should be called from the execute() method of all derived classes!
373 */
374 function setHeaders() {
375 global $wgOut;
376 $wgOut->setArticleRelated( false );
377 $wgOut->setRobotPolicy( "noindex,follow" );
378 $wgOut->setPageTitle( $this->getDescription() );
379 }
380
381 /**
382 * Default execute method
383 * Checks user permissions, calls the function given in mFunction
384 */
385 function execute( $par ) {
386 global $wgUser, $wgOut, $wgTitle;
387
388 $this->setHeaders();
389
390 if ( $this->userCanExecute( $wgUser ) ) {
391 $func = $this->mFunction;
392 // only load file if the function does not exist
393 if(!function_exists($func) and $this->mFile) {
394 require_once( $this->mFile );
395 }
396 $func( $par, $this );
397 } else {
398 $this->displayRestrictionError();
399 }
400 }
401
402 # Returns the name that goes in the <h1> in the special page itself, and also the name that
403 # will be listed in Special:Specialpages
404 #
405 # Derived classes can override this, but usually it is easier to keep the default behaviour.
406 # Messages can be added at run-time, see MessageCache.php
407 function getDescription() {
408 return wfMsg( strtolower( $this->mName ) );
409 }
410
411 /**
412 * Get a self-referential title object
413 */
414 function getTitle() {
415 return Title::makeTitle( NS_SPECIAL, $this->mName );
416 }
417
418 /**
419 * Set whether this page is listed in Special:Specialpages, at run-time
420 */
421 function setListed( $listed ) {
422 return wfSetVar( $this->mListed, $listed );
423 }
424
425 }
426
427 /**
428 * Shortcut to construct a special page which is unlisted by default
429 * @package MediaWiki
430 */
431 class UnlistedSpecialPage extends SpecialPage
432 {
433 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
434 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
435 }
436 }
437
438 /**
439 * Shortcut to construct an includable special page
440 * @package MediaWiki
441 */
442 class IncludableSpecialPage extends SpecialPage
443 {
444 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
445 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
446 }
447 }
448 ?>