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