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