* Deprecated Special:Sitesettings
[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 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
80 'Groups' => new SpecialPage( 'Groups' ),
81 );
82
83 global $wgUseValidation ;
84 if ( $wgUseValidation )
85 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
86
87 global $wgDisableCounters;
88 if( !$wgDisableCounters ) {
89 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
90 }
91
92 global $wgDisableInternalSearch;
93 if( !$wgDisableInternalSearch ) {
94 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
95 }
96
97 global $wgEmailAuthentication;
98 if( $wgEmailAuthentication ) {
99 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
100 }
101
102 /**
103 * Parent special page class, also static functions for handling the special
104 * page list
105 * @package MediaWiki
106 */
107 class SpecialPage
108 {
109 /**#@+
110 * @access private
111 */
112 /**
113 * The name of the class, used in the URL.
114 * Also used for the default <h1> heading, @see getDescription()
115 */
116 var $mName;
117 /**
118 * Minimum user level required to access this page, or "" for anyone.
119 * Also used to categorise the pages in Special:Specialpages
120 */
121 var $mRestriction;
122 /**
123 * Listed in Special:Specialpages?
124 */
125 var $mListed;
126 /**
127 * Function name called by the default execute()
128 */
129 var $mFunction;
130 /**
131 * File which needs to be included before the function above can be called
132 */
133 var $mFile;
134 /**#@-*/
135
136 /**
137 * Add a page to the list of valid special pages
138 * $obj->execute() must send HTML to $wgOut then return
139 * Use this for a special page extension
140 * @static
141 */
142 function addPage( &$obj ) {
143 global $wgSpecialPages;
144 $wgSpecialPages[$obj->mName] = $obj;
145 }
146
147 /**
148 * Remove a special page from the list
149 * Occasionally used to disable expensive or dangerous special pages
150 * @static
151 */
152 function removePage( $name ) {
153 global $wgSpecialPages;
154 unset( $wgSpecialPages[$name] );
155 }
156
157 /**
158 * Find the object with a given name and return it (or NULL)
159 * @static
160 * @param string $name
161 */
162 function &getPage( $name ) {
163 global $wgSpecialPages;
164 if ( array_key_exists( $name, $wgSpecialPages ) ) {
165 return $wgSpecialPages[$name];
166 } else {
167 return NULL;
168 }
169 }
170
171 /**
172 * Return categorised listable special pages
173 * Returns a 2d array where the first index is the restriction name
174 * @static
175 */
176 function getPages() {
177 global $wgSpecialPages;
178 $pages = array(
179 '' => array(),
180 'sysop' => array(),
181 'developer' => array()
182 );
183
184 foreach ( $wgSpecialPages as $name => $page ) {
185 if ( $page->isListed() ) {
186 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
187 }
188 }
189 return $pages;
190 }
191
192 /**
193 * Execute a special page path.
194 * The path may contain parameters, e.g. Special:Name/Params
195 * Extracts the special page name and call the execute method, passing the parameters
196 *
197 * @param $title should be a title object
198 */
199 function executePath( &$title ) {
200 global $wgSpecialPages, $wgOut, $wgTitle;
201
202 $bits = split( "/", $title->getDBkey(), 2 );
203 $name = $bits[0];
204 if( !isset( $bits[1] ) ) { // bug 2087
205 $par = NULL;
206 } else {
207 $par = $bits[1];
208 }
209
210 $page =& SpecialPage::getPage( $name );
211 if ( is_null( $page ) ) {
212 $wgOut->setArticleRelated( false );
213 $wgOut->setRobotpolicy( "noindex,follow" );
214 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
215 } else {
216 if($par !== NULL) {
217 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
218 } else {
219 $wgTitle = $title;
220 }
221
222 $page->execute( $par );
223 }
224 }
225
226 /**
227 * Default constructor for special pages
228 * Derivative classes should call this from their constructor
229 * Note that if the user does not have the required level, an error message will
230 * be displayed by the default execute() method, without the global function ever
231 * being called.
232 *
233 * If you override execute(), you can recover the default behaviour with userCanExecute()
234 * and displayRestrictionError()
235 *
236 * @param string $name Name of the special page, as seen in links and URLs
237 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
238 * @param boolean $listed Whether the page is listed in Special:Specialpages
239 * @param string $function Function called by execute(). By default it is constructed from $name
240 * @param string $file File which is included by execute(). It is also constructed from $name by default
241 */
242 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
243 $this->mName = $name;
244 $this->mRestriction = $restriction;
245 $this->mListed = $listed;
246 if ( $function == false ) {
247 $this->mFunction = 'wfSpecial'.$name;
248 } else {
249 $this->mFunction = $function;
250 }
251 if ( $file === 'default' ) {
252 $this->mFile = "Special{$name}.php";
253 } else {
254 $this->mFile = $file;
255 }
256 }
257
258 # Accessor functions, see the descriptions of the associated variables above
259 function getName() { return $this->mName; }
260 function getRestriction() { return $this->mRestriction; }
261 function isListed() { return $this->mListed; }
262 function getFile() { return $this->mFile; }
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 }
356 ?>