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