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