Moving Special:Userlevels and Special:Grouplevels to Special:Userrights and Special...
[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 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
54 'Shortpages' => new SpecialPage( 'Shortpages' ),
55 'Longpages' => new SpecialPage( 'Longpages' ),
56 'Newpages' => new SpecialPage( 'Newpages' ),
57 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
58 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
59 'Allpages' => new SpecialPage( 'Allpages' ),
60 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
61 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
62 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
63 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
64 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
65 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
66 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
67 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
68 'Booksources' => new SpecialPage( 'Booksources' ),
69 'Categories' => new SpecialPage( 'Categories' ),
70 'Export' => new SpecialPage( 'Export' ),
71 'Version' => new SpecialPage( 'Version' ),
72 'Allmessages' => new SpecialPage( 'Allmessages' ),
73 'Log' => new SpecialPage( 'Log' ),
74 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
75 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
76 "Import" => new SpecialPage( "Import", 'import' ),
77 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
78 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
79 # 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
80 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
81 'Groups' => new SpecialPage( 'Groups', 'grouprights' ),
82 );
83
84 global $wgUseValidation ;
85 if ( $wgUseValidation )
86 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
87
88 global $wgDisableCounters;
89 if( !$wgDisableCounters ) {
90 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
91 }
92
93 global $wgDisableInternalSearch;
94 if( !$wgDisableInternalSearch ) {
95 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
96 }
97
98 global $wgEmailAuthentication;
99 if( $wgEmailAuthentication ) {
100 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
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 function getFile() { return $this->mFile; }
264
265 /**
266 * Checks if the given user (identified by an object) can execute this
267 * special page (as defined by $mRestriction)
268 */
269 function userCanExecute( &$user ) {
270 if ( $this->mRestriction == "" ) {
271 return true;
272 } else {
273 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
274 return true;
275 } else {
276 return false;
277 }
278 }
279 }
280
281 /**
282 * Output an error message telling the user what access level they have to have
283 */
284 function displayRestrictionError() {
285 global $wgOut;
286 if ( $this->mRestriction == "developer" ) {
287 $wgOut->developerRequired();
288 } else {
289 $wgOut->sysopRequired();
290 }
291 }
292
293 /**
294 * Sets headers - this should be called from the execute() method of all derived classes!
295 */
296 function setHeaders() {
297 global $wgOut;
298 $wgOut->setArticleRelated( false );
299 $wgOut->setRobotPolicy( "noindex,follow" );
300 $wgOut->setPageTitle( $this->getDescription() );
301 }
302
303 /**
304 * Default execute method
305 * Checks user permissions, calls the function given in mFunction
306 */
307 function execute( $par ) {
308 global $wgUser, $wgOut, $wgTitle;
309
310 $this->setHeaders();
311
312 if ( $this->userCanExecute( $wgUser ) ) {
313 if ( $this->mFile ) {
314 require_once( $this->mFile );
315 }
316 $func = $this->mFunction;
317 $func( $par );
318 } else {
319 $this->displayRestrictionError();
320 }
321 }
322
323 # Returns the name that goes in the <h1> in the special page itself, and also the name that
324 # will be listed in Special:Specialpages
325 #
326 # Derived classes can override this, but usually it is easier to keep the default behaviour.
327 # Messages can be added at run-time, see MessageCache.php
328 function getDescription() {
329 return wfMsg( strtolower( $this->mName ) );
330 }
331
332 /**
333 * Get a self-referential title object
334 */
335 function getTitle() {
336 return Title::makeTitle( NS_SPECIAL, $this->mName );
337 }
338
339 /**
340 * Set whether this page is listed in Special:Specialpages, at run-time
341 */
342 function setListed( $listed ) {
343 return wfSetVar( $this->mListed, $listed );
344 }
345 }
346
347 /**
348 * Shortcut to construct a special page which is unlisted by default
349 * @package MediaWiki
350 */
351 class UnlistedSpecialPage extends SpecialPage
352 {
353 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
354 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
355 }
356 }
357 ?>