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