profiling
[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 SpecialPage( '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 );
74
75 if ( $wgUseValidation )
76 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
77
78 if( !$wgDisableCounters ) {
79 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
80 }
81
82 if( !$wgDisableInternalSearch ) {
83 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
84 }
85
86 if( $wgEmailAuthentication ) {
87 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
88 }
89
90 /**
91 * Parent special page class, also static functions for handling the special
92 * page list
93 * @package MediaWiki
94 */
95 class SpecialPage
96 {
97 /**#@+
98 * @access private
99 */
100 /**
101 * The name of the class, used in the URL.
102 * Also used for the default <h1> heading, @see getDescription()
103 */
104 var $mName;
105 /**
106 * Minimum user level required to access this page, or "" for anyone.
107 * Also used to categorise the pages in Special:Specialpages
108 */
109 var $mRestriction;
110 /**
111 * Listed in Special:Specialpages?
112 */
113 var $mListed;
114 /**
115 * Function name called by the default execute()
116 */
117 var $mFunction;
118 /**
119 * File which needs to be included before the function above can be called
120 */
121 var $mFile;
122 /**
123 * Whether or not this special page is being included from an article
124 */
125 var $mIncluding;
126 /**
127 * Whether the special page can be included in an article
128 */
129 var $mIncludable;
130
131
132 /**#@-*/
133
134
135 /**
136 * Add a page to the list of valid special pages
137 * $obj->execute() must send HTML to $wgOut then return
138 * Use this for a special page extension
139 * @static
140 */
141 function addPage( &$obj ) {
142 global $wgSpecialPages;
143 $wgSpecialPages[$obj->mName] = $obj;
144 }
145
146 /**
147 * Remove a special page from the list
148 * Occasionally used to disable expensive or dangerous special pages
149 * @static
150 */
151 function removePage( $name ) {
152 global $wgSpecialPages;
153 unset( $wgSpecialPages[$name] );
154 }
155
156 /**
157 * Find the object with a given name and return it (or NULL)
158 * @static
159 * @param string $name
160 */
161 function getPage( $name ) {
162 global $wgSpecialPages;
163 if ( array_key_exists( $name, $wgSpecialPages ) ) {
164 return $wgSpecialPages[$name];
165 } else {
166 return NULL;
167 }
168 }
169
170 /**
171 * @static
172 * @param string $name
173 * @return mixed Title object if the redirect exists, otherwise NULL
174 */
175 function getRedirect( $name ) {
176 global $wgUser;
177 switch ( $name ) {
178 case 'Mypage':
179 return Title::makeTitle( NS_USER, $wgUser->getName() );
180 case 'Mytalk':
181 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
182 case 'Mycontributions':
183 return Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() );
184 case 'Listadmins':
185 return Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ); # @bug 2832
186 case 'Randompage':
187 return Title::makeTitle( NS_SPECIAL, 'Random' );
188 default:
189 return NULL;
190 }
191 }
192
193 /**
194 * Return categorised listable special pages
195 * Returns a 2d array where the first index is the restriction name
196 * @static
197 */
198 function getPages() {
199 global $wgSpecialPages;
200 $pages = array(
201 '' => array(),
202 'sysop' => array(),
203 'developer' => array()
204 );
205
206 foreach ( $wgSpecialPages as $name => $page ) {
207 if ( $page->isListed() ) {
208 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
209 }
210 }
211 return $pages;
212 }
213
214 /**
215 * Execute a special page path.
216 * The path may contain parameters, e.g. Special:Name/Params
217 * Extracts the special page name and call the execute method, passing the parameters
218 *
219 * Returns a title object if the page is redirected, false if there was no such special
220 * page, and true if it was successful.
221 *
222 * @param $title a title object
223 * @param $including output is being captured for use in {{special:whatever}}
224 */
225 function executePath( &$title, $including = false ) {
226 global $wgSpecialPages, $wgOut, $wgTitle;
227 $fname = 'SpecialPage::executePath';
228 wfProfileIn( $fname );
229
230 $bits = split( "/", $title->getDBkey(), 2 );
231 $name = $bits[0];
232 if( !isset( $bits[1] ) ) { // bug 2087
233 $par = NULL;
234 } else {
235 $par = $bits[1];
236 }
237
238 $page = SpecialPage::getPage( $name );
239 if ( is_null( $page ) ) {
240 if ( $including ) {
241 wfProfileOut( $fname );
242 return false;
243 } else {
244 $redir = SpecialPage::getRedirect( $name );
245 if ( isset( $redir ) ) {
246 if ( isset( $par ) )
247 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
248 else
249 $wgOut->redirect( $redir->getFullURL() );
250 $retVal = $redir;
251 } else {
252 $wgOut->setArticleRelated( false );
253 $wgOut->setRobotpolicy( "noindex,follow" );
254 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
255 $retVal = false;
256 }
257 }
258 } else {
259 if ( $including && !$page->includable() ) {
260 wfProfileOut( $fname );
261 return false;
262 }
263 if($par !== NULL) {
264 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
265 } else {
266 $wgTitle = $title;
267 }
268 $page->including( $including );
269
270 $profName = 'Special:' . $page->getName();
271 wfProfileIn( $profName );
272 $page->execute( $par );
273 wfProfileOut( $profName );
274 $retVal = true;
275 }
276 wfProfileOut( $fname );
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 ?>