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