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