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