Update message initialiser to use Revision functions for backend-independence. No...
[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 global $wgSpecialPages;
22
23 /**
24 * @access private
25 */
26 $wgSpecialPages = array(
27 'DoubleRedirects' => new UnlistedSpecialPage ( 'DoubleRedirects' ),
28 'BrokenRedirects' => new UnlistedSpecialPage ( 'BrokenRedirects' ),
29 'Disambiguations' => new UnlistedSpecialPage ( 'Disambiguations' ),
30
31 'Userlogin' => new SpecialPage( 'Userlogin' ),
32 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
33 'Preferences' => new SpecialPage( 'Preferences' ),
34 'Watchlist' => new SpecialPage( 'Watchlist' ),
35
36 'Mytalk' => new UnlistedSpecialPage( 'Mytalk'),
37 'Mycontributions' => new UnlistedSpecialPage( 'Mycontributions'),
38 'Mypage' => new UnlistedSpecialPage( 'Mypage'),
39
40 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
41 'Upload' => new SpecialPage( 'Upload' ),
42 'Imagelist' => new SpecialPage( 'Imagelist' ),
43 'Newimages' => new SpecialPage( 'Newimages' ),
44 'Listusers' => new SpecialPage( 'Listusers' ),
45 'Listadmins' => new UnlistedSpecialPage( 'Listadmins' ),
46 'Statistics' => new SpecialPage( 'Statistics' ),
47 'Randompage' => new SpecialPage( 'Randompage' ),
48 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
49 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
50 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
51 'Unusedimages' => new SpecialPage( 'Unusedimages' )
52 );
53
54 global $wgUseValidation ;
55 if ( $wgUseValidation )
56 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
57
58 global $wgDisableCounters;
59 if( !$wgDisableCounters ) {
60 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
61 }
62
63 global $wgDisableInternalSearch;
64 if( !$wgDisableInternalSearch ) {
65 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
66 }
67
68 $wgSpecialPages = array_merge($wgSpecialPages, array (
69 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
70 'Shortpages' => new SpecialPage( 'Shortpages' ),
71 'Longpages' => new SpecialPage( 'Longpages' ),
72 'Newpages' => new SpecialPage( 'Newpages' ),
73 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
74 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
75 'Allpages' => new SpecialPage( 'Allpages' ),
76 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
77 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
78 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
79 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
80 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
81 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
82 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
83 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
84 'Booksources' => new SpecialPage( 'Booksources' ),
85 'Categories' => new SpecialPage( 'Categories' ),
86 'Export' => new SpecialPage( 'Export' ),
87 'Version' => new SpecialPage( 'Version' ),
88 'Allmessages' => new SpecialPage( 'Allmessages' ),
89 'Log' => new SpecialPage( 'Log' ),
90 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
91 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
92 // Makesysop is obsolete, replaced by Special:Userlevels [av]
93 # 'Makesysop' => new SpecialPage( 'Makesysop', 'userrights' ),
94
95 # Special:Import is half-written
96 "Import" => new SpecialPage( "Import", "sysop" ),
97 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
98 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
99 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
100 'Userlevels' => new SpecialPage( 'Userlevels', 'userrights' ),
101 'Grouplevels' => new SpecialPage( 'Grouplevels', 'grouprights' ),
102 ));
103
104 /**
105 * Parent special page class, also static functions for handling the special
106 * page list
107 * @package MediaWiki
108 */
109 class SpecialPage
110 {
111 /**#@+
112 * @access private
113 */
114 /**
115 * The name of the class, used in the URL.
116 * Also used for the default <h1> heading, @see getDescription()
117 */
118 var $mName;
119 /**
120 * Minimum user level required to access this page, or "" for anyone.
121 * Also used to categorise the pages in Special:Specialpages
122 */
123 var $mRestriction;
124 /**
125 * Listed in Special:Specialpages?
126 */
127 var $mListed;
128 /**
129 * Function name called by the default execute()
130 */
131 var $mFunction;
132 /**
133 * File which needs to be included before the function above can be called
134 */
135 var $mFile;
136 /**#@-*/
137
138 /**
139 * Add a page to the list of valid special pages
140 * $obj->execute() must send HTML to $wgOut then return
141 * Use this for a special page extension
142 * @static
143 */
144 function addPage( &$obj ) {
145 global $wgSpecialPages;
146 $wgSpecialPages[$obj->mName] = $obj;
147 }
148
149 /**
150 * Remove a special page from the list
151 * Occasionally used to disable expensive or dangerous special pages
152 * @static
153 */
154 function removePage( $name ) {
155 global $wgSpecialPages;
156 unset( $wgSpecialPages[$name] );
157 }
158
159 /**
160 * Find the object with a given name and return it (or NULL)
161 * @static
162 * @param string $name
163 */
164 function &getPage( $name ) {
165 global $wgSpecialPages;
166 if ( array_key_exists( $name, $wgSpecialPages ) ) {
167 return $wgSpecialPages[$name];
168 } else {
169 return NULL;
170 }
171 }
172
173 /**
174 * Return categorised listable special pages
175 * Returns a 2d array where the first index is the restriction name
176 * @static
177 */
178 function getPages() {
179 global $wgSpecialPages;
180 $pages = array(
181 '' => array(),
182 'sysop' => array(),
183 'developer' => array()
184 );
185
186 foreach ( $wgSpecialPages as $name => $page ) {
187 if ( $page->isListed() ) {
188 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
189 }
190 }
191 return $pages;
192 }
193
194 /**
195 * Execute a special page path.
196 * The path may contain parameters, e.g. Special:Name/Params
197 * Extracts the special page name and call the execute method, passing the parameters
198 *
199 * @param $title should be a title object
200 */
201 function executePath( &$title ) {
202 global $wgSpecialPages, $wgOut, $wgTitle;
203
204 $bits = split( "/", $title->getDBkey(), 2 );
205 $name = $bits[0];
206 if( empty( $bits[1] ) ) {
207 $par = NULL;
208 } else {
209 $par = $bits[1];
210 }
211
212 $page =& SpecialPage::getPage( $name );
213 if ( is_null( $page ) ) {
214 $wgOut->setArticleRelated( false );
215 $wgOut->setRobotpolicy( "noindex,follow" );
216 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
217 } else {
218 if($par !== NULL) {
219 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
220 } else {
221 $wgTitle = $title;
222 }
223
224 $page->execute( $par );
225 }
226 }
227
228 /**
229 * Default constructor for special pages
230 * Derivative classes should call this from their constructor
231 * Note that if the user does not have the required level, an error message will
232 * be displayed by the default execute() method, without the global function ever
233 * being called.
234 *
235 * If you override execute(), you can recover the default behaviour with userCanExecute()
236 * and displayRestrictionError()
237 *
238 * @param string $name Name of the special page, as seen in links and URLs
239 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
240 * @param boolean $listed Whether the page is listed in Special:Specialpages
241 * @param string $function Function called by execute(). By default it is constructed from $name
242 * @param string $file File which is included by execute(). It is also constructed from $name by default
243 */
244 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
245 $this->mName = $name;
246 $this->mRestriction = $restriction;
247 $this->mListed = $listed;
248 if ( $function == false ) {
249 $this->mFunction = 'wfSpecial'.$name;
250 } else {
251 $this->mFunction = $function;
252 }
253 if ( $file === 'default' ) {
254 $this->mFile = "Special{$name}.php";
255 } else {
256 $this->mFile = $file;
257 }
258 }
259
260 # Accessor functions, see the descriptions of the associated variables above
261 function getName() { return $this->mName; }
262 function getRestriction() { return $this->mRestriction; }
263 function isListed() { return $this->mListed; }
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 }