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