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