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