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