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