removed crippled enotif code from checkPassword(), this is roughly how it was in...
[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, $wgSpecialPageRedirects, $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 SpecialPage( '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 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
48 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
49 'Shortpages' => new SpecialPage( 'Shortpages' ),
50 'Longpages' => new SpecialPage( 'Longpages' ),
51 'Newpages' => new SpecialPage( 'Newpages' ),
52 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
53 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
54 'Allpages' => new SpecialPage( 'Allpages' ),
55 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
56 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
57 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
58 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
59 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
60 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
61 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
62 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
63 'Booksources' => new SpecialPage( 'Booksources' ),
64 'Categories' => new SpecialPage( 'Categories' ),
65 'Export' => new SpecialPage( 'Export' ),
66 'Version' => new SpecialPage( 'Version' ),
67 'Allmessages' => new SpecialPage( 'Allmessages' ),
68 'Log' => new SpecialPage( 'Log' ),
69 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
70 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
71 "Import" => new SpecialPage( "Import", 'import' ),
72 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
73 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
74 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
75 'Groups' => new SpecialPage( 'Groups' ),
76 );
77
78 /**
79 * Redirect Special:$key to somewhere else.
80 *
81 * @access private
82 */
83 $wgSpecialPageRedirects = array(
84 # My*
85 'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
86 'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
87 'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
88
89 # Deprecated specialpages
90 'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers' ),
91
92 # Redirects
93 'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' ),
94 );
95
96 global $wgUseValidation ;
97 if ( $wgUseValidation )
98 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
99
100 global $wgDisableCounters;
101 if( !$wgDisableCounters ) {
102 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
103 }
104
105 global $wgDisableInternalSearch;
106 if( !$wgDisableInternalSearch ) {
107 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
108 }
109
110 global $wgEmailAuthentication;
111 if( $wgEmailAuthentication ) {
112 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
113 }
114
115 /**
116 * Parent special page class, also static functions for handling the special
117 * page list
118 * @package MediaWiki
119 */
120 class SpecialPage
121 {
122 /**#@+
123 * @access private
124 */
125 /**
126 * The name of the class, used in the URL.
127 * Also used for the default <h1> heading, @see getDescription()
128 */
129 var $mName;
130 /**
131 * Minimum user level required to access this page, or "" for anyone.
132 * Also used to categorise the pages in Special:Specialpages
133 */
134 var $mRestriction;
135 /**
136 * Listed in Special:Specialpages?
137 */
138 var $mListed;
139 /**
140 * Function name called by the default execute()
141 */
142 var $mFunction;
143 /**
144 * File which needs to be included before the function above can be called
145 */
146 var $mFile;
147 /**#@-*/
148
149 /**
150 * Add a page to the list of valid special pages
151 * $obj->execute() must send HTML to $wgOut then return
152 * Use this for a special page extension
153 * @static
154 */
155 function addPage( &$obj ) {
156 global $wgSpecialPages;
157 $wgSpecialPages[$obj->mName] = $obj;
158 }
159
160 /**
161 * Remove a special page from the list
162 * Occasionally used to disable expensive or dangerous special pages
163 * @static
164 */
165 function removePage( $name ) {
166 global $wgSpecialPages;
167 unset( $wgSpecialPages[$name] );
168 }
169
170 /**
171 * Find the object with a given name and return it (or NULL)
172 * @static
173 * @param string $name
174 */
175 function &getPage( $name ) {
176 global $wgSpecialPages;
177 if ( array_key_exists( $name, $wgSpecialPages ) ) {
178 return $wgSpecialPages[$name];
179 } else {
180 return NULL;
181 }
182 }
183
184 /**
185 * @static
186 * @param string $name
187 * @return mixed Title object if the redirect exists, otherwise NULL
188 */
189 function &getRedirect( $name ) {
190 global $wgSpecialPageRedirects;
191 if ( array_key_exists( $name, $wgSpecialPageRedirects ) ) {
192 return $wgSpecialPageRedirects[$name];
193 } else {
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 * @param $title should be a title object
225 */
226 function executePath( &$title ) {
227 global $wgSpecialPages, $wgOut, $wgTitle;
228
229 $bits = split( "/", $title->getDBkey(), 2 );
230 $name = $bits[0];
231 if( !isset( $bits[1] ) ) { // bug 2087
232 $par = NULL;
233 } else {
234 $par = $bits[1];
235 }
236
237 $page =& SpecialPage::getPage( $name );
238 if ( is_null( $page ) ) {
239 $redir =& SpecialPage::getRedirect( $name );
240 if ( isset( $redir ) ) {
241 if ( isset( $par ) )
242 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
243 else
244 $wgOut->redirect( $redir->getFullURL() );
245 } else {
246 $wgOut->setArticleRelated( false );
247 $wgOut->setRobotpolicy( "noindex,follow" );
248 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
249 }
250 } else {
251 if($par !== NULL) {
252 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
253 } else {
254 $wgTitle = $title;
255 }
256
257 $page->execute( $par );
258 }
259 }
260
261 /**
262 * Default constructor for special pages
263 * Derivative classes should call this from their constructor
264 * Note that if the user does not have the required level, an error message will
265 * be displayed by the default execute() method, without the global function ever
266 * being called.
267 *
268 * If you override execute(), you can recover the default behaviour with userCanExecute()
269 * and displayRestrictionError()
270 *
271 * @param string $name Name of the special page, as seen in links and URLs
272 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
273 * @param boolean $listed Whether the page is listed in Special:Specialpages
274 * @param string $function Function called by execute(). By default it is constructed from $name
275 * @param string $file File which is included by execute(). It is also constructed from $name by default
276 */
277 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
278 $this->mName = $name;
279 $this->mRestriction = $restriction;
280 $this->mListed = $listed;
281 if ( $function == false ) {
282 $this->mFunction = 'wfSpecial'.$name;
283 } else {
284 $this->mFunction = $function;
285 }
286 if ( $file === 'default' ) {
287 $this->mFile = "Special{$name}.php";
288 } else {
289 $this->mFile = $file;
290 }
291 }
292
293 # Accessor functions, see the descriptions of the associated variables above
294 function getName() { return $this->mName; }
295 function getRestriction() { return $this->mRestriction; }
296 function isListed() { return $this->mListed; }
297 function getFile() { return $this->mFile; }
298
299 /**
300 * Checks if the given user (identified by an object) can execute this
301 * special page (as defined by $mRestriction)
302 */
303 function userCanExecute( &$user ) {
304 if ( $this->mRestriction == "" ) {
305 return true;
306 } else {
307 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
308 return true;
309 } else {
310 return false;
311 }
312 }
313 }
314
315 /**
316 * Output an error message telling the user what access level they have to have
317 */
318 function displayRestrictionError() {
319 global $wgOut;
320 if ( $this->mRestriction == "developer" ) {
321 $wgOut->developerRequired();
322 } else {
323 $wgOut->sysopRequired();
324 }
325 }
326
327 /**
328 * Sets headers - this should be called from the execute() method of all derived classes!
329 */
330 function setHeaders() {
331 global $wgOut;
332 $wgOut->setArticleRelated( false );
333 $wgOut->setRobotPolicy( "noindex,follow" );
334 $wgOut->setPageTitle( $this->getDescription() );
335 }
336
337 /**
338 * Default execute method
339 * Checks user permissions, calls the function given in mFunction
340 */
341 function execute( $par ) {
342 global $wgUser, $wgOut, $wgTitle;
343
344 $this->setHeaders();
345
346 if ( $this->userCanExecute( $wgUser ) ) {
347 if ( $this->mFile ) {
348 require_once( $this->mFile );
349 }
350 $func = $this->mFunction;
351 $func( $par );
352 } else {
353 $this->displayRestrictionError();
354 }
355 }
356
357 # Returns the name that goes in the <h1> in the special page itself, and also the name that
358 # will be listed in Special:Specialpages
359 #
360 # Derived classes can override this, but usually it is easier to keep the default behaviour.
361 # Messages can be added at run-time, see MessageCache.php
362 function getDescription() {
363 return wfMsg( strtolower( $this->mName ) );
364 }
365
366 /**
367 * Get a self-referential title object
368 */
369 function getTitle() {
370 return Title::makeTitle( NS_SPECIAL, $this->mName );
371 }
372
373 /**
374 * Set whether this page is listed in Special:Specialpages, at run-time
375 */
376 function setListed( $listed ) {
377 return wfSetVar( $this->mListed, $listed );
378 }
379 }
380
381 /**
382 * Shortcut to construct a special page which is unlisted by default
383 * @package MediaWiki
384 */
385 class UnlistedSpecialPage extends SpecialPage
386 {
387 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
388 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
389 }
390 }
391 ?>