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