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