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