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