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