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