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