New updateSpecialPages.php script, to update the QueryPage cache. Disabled recache...
[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;
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 'Mytalk' => new UnlistedSpecialPage( 'Mytalk'),
38 'Mycontributions' => new UnlistedSpecialPage( 'Mycontributions'),
39 'Mypage' => new UnlistedSpecialPage( 'Mypage'),
40
41 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
42 'Upload' => new SpecialPage( 'Upload' ),
43 'Imagelist' => new SpecialPage( 'Imagelist' ),
44 'Newimages' => new SpecialPage( 'Newimages' ),
45 'Listusers' => new SpecialPage( 'Listusers' ),
46 'Listadmins' => new UnlistedSpecialPage( 'Listadmins' ),
47 'Statistics' => new SpecialPage( 'Statistics' ),
48 'Randompage' => new SpecialPage( 'Randompage' ),
49 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
50 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
51 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
52 'Unusedimages' => new SpecialPage( 'Unusedimages' )
53 );
54
55 global $wgUseValidation ;
56 if ( $wgUseValidation )
57 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
58
59 global $wgDisableCounters;
60 if( !$wgDisableCounters ) {
61 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
62 }
63
64 global $wgDisableInternalSearch;
65 if( !$wgDisableInternalSearch ) {
66 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
67 }
68
69 $wgSpecialPages = array_merge($wgSpecialPages, array (
70 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
71 'Shortpages' => new SpecialPage( 'Shortpages' ),
72 'Longpages' => new SpecialPage( 'Longpages' ),
73 'Newpages' => new SpecialPage( 'Newpages' ),
74 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
75 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
76 'Allpages' => new SpecialPage( 'Allpages' ),
77 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
78 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
79 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
80 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
81 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
82 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
83 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
84 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
85 'Booksources' => new SpecialPage( 'Booksources' ),
86 'Categories' => new SpecialPage( 'Categories' ),
87 'Export' => new SpecialPage( 'Export' ),
88 'Version' => new SpecialPage( 'Version' ),
89 'Allmessages' => new SpecialPage( 'Allmessages' ),
90 'Log' => new SpecialPage( 'Log' ),
91 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
92 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
93 "Import" => new SpecialPage( "Import", 'import' ),
94 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
95 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
96 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
97 'Userlevels' => new SpecialPage( 'Userlevels', 'userrights' ),
98 'Grouplevels' => new SpecialPage( 'Grouplevels', 'grouprights' ),
99 ));
100
101 /**
102 * Parent special page class, also static functions for handling the special
103 * page list
104 * @package MediaWiki
105 */
106 class SpecialPage
107 {
108 /**#@+
109 * @access private
110 */
111 /**
112 * The name of the class, used in the URL.
113 * Also used for the default <h1> heading, @see getDescription()
114 */
115 var $mName;
116 /**
117 * Minimum user level required to access this page, or "" for anyone.
118 * Also used to categorise the pages in Special:Specialpages
119 */
120 var $mRestriction;
121 /**
122 * Listed in Special:Specialpages?
123 */
124 var $mListed;
125 /**
126 * Function name called by the default execute()
127 */
128 var $mFunction;
129 /**
130 * File which needs to be included before the function above can be called
131 */
132 var $mFile;
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 * Return categorised listable special pages
172 * Returns a 2d array where the first index is the restriction name
173 * @static
174 */
175 function getPages() {
176 global $wgSpecialPages;
177 $pages = array(
178 '' => array(),
179 'sysop' => array(),
180 'developer' => array()
181 );
182
183 foreach ( $wgSpecialPages as $name => $page ) {
184 if ( $page->isListed() ) {
185 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
186 }
187 }
188 return $pages;
189 }
190
191 /**
192 * Execute a special page path.
193 * The path may contain parameters, e.g. Special:Name/Params
194 * Extracts the special page name and call the execute method, passing the parameters
195 *
196 * @param $title should be a title object
197 */
198 function executePath( &$title ) {
199 global $wgSpecialPages, $wgOut, $wgTitle;
200
201 $bits = split( "/", $title->getDBkey(), 2 );
202 $name = $bits[0];
203 if( empty( $bits[1] ) ) {
204 $par = NULL;
205 } else {
206 $par = $bits[1];
207 }
208
209 $page =& SpecialPage::getPage( $name );
210 if ( is_null( $page ) ) {
211 $wgOut->setArticleRelated( false );
212 $wgOut->setRobotpolicy( "noindex,follow" );
213 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
214 } else {
215 if($par !== NULL) {
216 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
217 } else {
218 $wgTitle = $title;
219 }
220
221 $page->execute( $par );
222 }
223 }
224
225 /**
226 * Default constructor for special pages
227 * Derivative classes should call this from their constructor
228 * Note that if the user does not have the required level, an error message will
229 * be displayed by the default execute() method, without the global function ever
230 * being called.
231 *
232 * If you override execute(), you can recover the default behaviour with userCanExecute()
233 * and displayRestrictionError()
234 *
235 * @param string $name Name of the special page, as seen in links and URLs
236 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
237 * @param boolean $listed Whether the page is listed in Special:Specialpages
238 * @param string $function Function called by execute(). By default it is constructed from $name
239 * @param string $file File which is included by execute(). It is also constructed from $name by default
240 */
241 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
242 $this->mName = $name;
243 $this->mRestriction = $restriction;
244 $this->mListed = $listed;
245 if ( $function == false ) {
246 $this->mFunction = 'wfSpecial'.$name;
247 } else {
248 $this->mFunction = $function;
249 }
250 if ( $file === 'default' ) {
251 $this->mFile = "Special{$name}.php";
252 } else {
253 $this->mFile = $file;
254 }
255 }
256
257 # Accessor functions, see the descriptions of the associated variables above
258 function getName() { return $this->mName; }
259 function getRestriction() { return $this->mRestriction; }
260 function isListed() { return $this->mListed; }
261 function getFile() { return $this->mFile; }
262
263 /**
264 * Checks if the given user (identified by an object) can execute this
265 * special page (as defined by $mRestriction)
266 */
267 function userCanExecute( &$user ) {
268 if ( $this->mRestriction == "" ) {
269 return true;
270 } else {
271 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
272 return true;
273 } else {
274 return false;
275 }
276 }
277 }
278
279 /**
280 * Output an error message telling the user what access level they have to have
281 */
282 function displayRestrictionError() {
283 global $wgOut;
284 if ( $this->mRestriction == "developer" ) {
285 $wgOut->developerRequired();
286 } else {
287 $wgOut->sysopRequired();
288 }
289 }
290
291 /**
292 * Sets headers - this should be called from the execute() method of all derived classes!
293 */
294 function setHeaders() {
295 global $wgOut;
296 $wgOut->setArticleRelated( false );
297 $wgOut->setRobotPolicy( "noindex,follow" );
298 $wgOut->setPageTitle( $this->getDescription() );
299 }
300
301 /**
302 * Default execute method
303 * Checks user permissions, calls the function given in mFunction
304 */
305 function execute( $par ) {
306 global $wgUser, $wgOut, $wgTitle;
307
308 $this->setHeaders();
309
310 if ( $this->userCanExecute( $wgUser ) ) {
311 if ( $this->mFile ) {
312 require_once( $this->mFile );
313 }
314 $func = $this->mFunction;
315 $func( $par );
316 } else {
317 $this->displayRestrictionError();
318 }
319 }
320
321 # Returns the name that goes in the <h1> in the special page itself, and also the name that
322 # will be listed in Special:Specialpages
323 #
324 # Derived classes can override this, but usually it is easier to keep the default behaviour.
325 # Messages can be added at run-time, see MessageCache.php
326 function getDescription() {
327 return wfMsg( strtolower( $this->mName ) );
328 }
329
330 /**
331 * Get a self-referential title object
332 */
333 function getTitle() {
334 return Title::makeTitle( NS_SPECIAL, $this->mName );
335 }
336
337 /**
338 * Set whether this page is listed in Special:Specialpages, at run-time
339 */
340 function setListed( $listed ) {
341 return wfSetVar( $this->mListed, $listed );
342 }
343 }
344
345 /**
346 * Shortcut to construct a special page which is unlisted by default
347 * @package MediaWiki
348 */
349 class UnlistedSpecialPage extends SpecialPage
350 {
351 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
352 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
353 }
354 }
355 ?>