index.php now fits on a single screen :-)
[lhc/web/wiklou.git] / includes / Wiki.php
1 <?php
2 /**
3 * MediaWiki is the to-be base class for this whole project
4 */
5
6 class MediaWiki {
7
8 var $GET; /* Stores the $_GET variables at time of creation, can be changed */
9 var $params = array();
10
11 /**
12 * Constructor
13 */
14 function MediaWiki () {
15 $this->GET = $_GET;
16 }
17
18 /**
19 * Stores key/value pairs to circumvent global variables
20 * Note that keys are case-insensitive!
21 */
22 function setVal( $key, &$value ) {
23 $key = strtolower( $key );
24 $this->params[$key] =& $value;
25 }
26
27 /**
28 * Retieves key/value pairs to circumvent global variables
29 * Note that keys are case-insensitive!
30 */
31 function getVal( $key, $default = "" ) {
32 $key = strtolower( $key );
33 if( isset( $this->params[$key] ) ) {
34 return $this->params[$key];
35 }
36 return $default;
37 }
38
39 /**
40 * Wrapper for getrusage, if it exists
41 * getrusage() does not exist on the Window$ platform, catching this
42 */
43 function getRUsage() {
44 if ( function_exists ( 'getrusage' ) ) {
45 return getrusage();
46 } else {
47 return array();
48 }
49 }
50
51 /**
52 * CHeck for $GLOBALS vulnerability
53 */
54 function ckeckGlobalsVulnerability() {
55 @ini_set( 'allow_url_fopen', 0 ); # For security...
56 if ( isset( $_REQUEST['GLOBALS'] ) ) {
57 die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>');
58 }
59 }
60
61 /**
62 * Checks if the wiki is set up at all, or configured but not activated
63 */
64 function checkSetup() {
65 if ( file_exists( './LocalSettings.php' ) ) {
66 /* LocalSettings exists, commerce normally */
67 return;
68 }
69
70 /* LocalSettings is not in the right place, do something */
71 $IP = ".";
72 require_once( 'includes/DefaultSettings.php' ); # used for printing the version
73 $out = file_get_contents( "./setup_message.html" );
74 $out = str_replace( "$1", $wgVersion, $out );
75 if ( file_exists( 'config/LocalSettings.php' ) ) {
76 $msg = "To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory.";
77 } else {
78 $msg = "Please <a href='config/index.php' title='setup'>setup the wiki</a> first.";
79 }
80 $out = str_replace( "$2", $msg, $out );
81 echo $out;
82 die();
83 }
84
85 /**
86 * Reads title and action values from request
87 */
88 function initializeActionTitle () {
89 $request = $this->getVal( 'Request' );
90 $this->setVal( 'action', $request->getVal( 'action', 'view' ) );
91 $this->setVal( 'urltitle', $request->getVal( 'title' ) );
92 }
93
94 /**
95 * Initialization of ... everything
96 @return Article either the object to become $wgArticle, or NULL
97 */
98 function initialize ( &$title, &$output, &$user ) {
99 wfProfileIn( 'MediaWiki::initialize' );
100 $request = $this->getVal( 'Request' );
101 $this->preliminaryChecks ( $title, $output );
102 $article = NULL;
103 if ( !$this->initializeSpecialCases( $title, $output ) ) {
104 $article = $this->initializeArticle( $title );
105 if( is_object( $article ) ) {
106 $this->performAction( $output, $article, $title, $user );
107 } elseif( is_string( $article ) ) {
108 $output->redirect( $article );
109 } else {
110 wfDebugDieBacktrace( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
111 }
112 }
113 wfProfileOut( 'MediaWiki::initialize' );
114 return $article;
115 }
116
117 /**
118 * Checks some initial queries
119 */
120 function checkInitialQueries( &$output, $lang) {
121 wfProfileIn( 'MediaWiki::checkInitialQueries' );
122 $request = $this->getVal( 'Request' );
123 $action = $this->getVal( 'action' );
124 $title = $this->getVal( 'urltitle' );
125 if ($request->getVal( 'printable' ) == 'yes') {
126 $output->setPrintable();
127 }
128
129 $ret = NULL;
130
131
132 if ( '' == $title && 'delete' != $action ) {
133 $ret = Title::newFromText( wfMsgForContent( 'mainpage' ) );
134 } elseif ( $curid = $request->getInt( 'curid' ) ) {
135 # URLs like this are generated by RC, because rc_title isn't always accurate
136 $ret = Title::newFromID( $curid );
137 } else {
138 $ret = Title::newFromURL( $title );
139 /* check variant links so that interwiki links don't have to worry about
140 the possible different language variants
141 */
142 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
143 $lang->findVariantLink( $title, $ret );
144
145 }
146 wfProfileOut( 'MediaWiki::checkInitialQueries' );
147 return $ret;
148 }
149
150 /**
151 * Checks for search query and anon-cannot-read case
152 */
153 function preliminaryChecks ( &$title, &$output ) {
154 $request = $this->getVal( 'Request' );
155 # Debug statement for user levels
156 // print_r($wgUser);
157
158 $search = $request->getText( 'search' );
159 if( !is_null( $search ) && $search !== '' ) {
160 // Compatibility with old search URLs which didn't use Special:Search
161 // Do this above the read whitelist check for security...
162 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
163 }
164 $this->setVal( "Search", $search );
165
166 # If the user is not logged in, the Namespace:title of the article must be in
167 # the Read array in order for the user to see it. (We have to check here to
168 # catch special pages etc. We check again in Article::view())
169 if ( !is_null( $title ) && !$title->userCanRead() ) {
170 $output->loginToUse();
171 $output->output();
172 exit;
173 }
174
175 }
176
177 /**
178 * Initialize the object to be known as $wgArticle for special cases
179 */
180 function initializeSpecialCases ( &$title, &$output ) {
181 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
182 $request = $this->getVal( 'Request' );
183
184 $search = $this->getVal('Search');
185 $action = $this->getVal('Action');
186 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
187 require_once( 'includes/SpecialSearch.php' );
188 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
189 wfSpecialSearch();
190 } else if( !$title or $title->getDBkey() == '' ) {
191 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
192 $output->errorpage( 'badtitle', 'badtitletext' );
193 } else if ( $title->getInterwiki() != '' ) {
194 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
195 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
196 } else {
197 $url = $title->getFullURL();
198 }
199 /* Check for a redirect loop */
200 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
201 $output->redirect( $url );
202 } else {
203 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
204 $output->errorpage( 'badtitle', 'badtitletext' );
205 }
206 } else if ( ( $action == 'view' ) &&
207 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
208 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
209 {
210 /* Redirect to canonical url, make it a 301 to allow caching */
211 $output->setSquidMaxage( 1200 );
212 $output->redirect( $title->getFullURL(), '301');
213 } else if ( NS_SPECIAL == $title->getNamespace() ) {
214 /* actions that need to be made when we have a special pages */
215 SpecialPage::executePath( $title );
216 } else {
217 /* No match to special cases */
218 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
219 return false;
220 }
221 /* Did match a special case */
222 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
223 return true;
224 }
225
226 /**
227 * Create an Article object of the appropriate class for the given page.
228 * @param Title $title
229 * @return Article
230 */
231 function articleFromTitle( $title ) {
232 if( NS_MEDIA == $title->getNamespace() ) {
233 // FIXME: where should this go?
234 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
235 }
236
237 switch( $title->getNamespace() ) {
238 case NS_IMAGE:
239 require_once( 'includes/ImagePage.php' );
240 return new ImagePage( $title );
241 case NS_CATEGORY:
242 require_once( 'includes/CategoryPage.php' );
243 return new CategoryPage( $title );
244 default:
245 return new Article( $title );
246 }
247 }
248
249 /**
250 * Initialize the object to be known as $wgArticle for "standard" actions
251 * Create an Article object for the page, following redirects if needed.
252 * @param Title $title
253 * @return mixed an Article, or a string to redirect to another URL
254 */
255 function initializeArticle( $title ) {
256 wfProfileIn( 'MediaWiki::initializeArticle' );
257
258 $request = $this->getVal( 'Request' );
259 $action = $this->getVal('Action');
260 $article = $this->articleFromTitle( $title );
261
262 // Namespace might change when using redirects
263 if( $action == 'view' && !$request->getVal( 'oldid' ) && $request->getVal( 'redirect' ) != 'no' ) {
264 $target = $article->followRedirect();
265 if( is_string( $target ) ) {
266 global $wgDisableHardRedirects;
267 if( !$wgDisableHardRedirects ) {
268 // we'll need to redirect
269 return $target;
270 }
271 }
272 if( is_object( $target ) ) {
273 // evil globals hack!
274 global $wgTitle;
275 $wgTitle = $target;
276
277 $article = $this->articleFromTitle( $target );
278 $article->setRedirectedFrom( $title );
279 }
280 }
281 wfProfileOut( 'MediaWiki::initializeArticle' );
282 return $article;
283 }
284
285 /**
286 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
287 */
288 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
289 wfProfileIn( 'MediaWiki::finalCleanup' );
290 $this->doUpdates( $deferredUpdates );
291 $loadBalancer->saveMasterPos();
292 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
293 $loadBalancer->commitAll();
294 $output->output();
295 wfProfileOut( 'MediaWiki::finalCleanup' );
296 }
297
298 /**
299 * Deferred updates aren't really deferred anymore. It's important to report errors to the
300 * user, and that means doing this before OutputPage::output(). Note that for page saves,
301 * the client will wait until the script exits anyway before following the redirect.
302 */
303 function doUpdates ( &$updates ) {
304 wfProfileIn( 'MediaWiki::doUpdates' );
305 foreach( $updates as $up ) {
306 $up->doUpdate();
307 }
308 wfProfileOut( 'MediaWiki::doUpdates' );
309 }
310
311 /**
312 * Ends this task peacefully
313 */
314 function restInPeace ( &$loadBalancer ) {
315 wfProfileClose();
316 logProfilingData();
317 $loadBalancer->closeAll();
318 wfDebug( "Request ended normally\n" );
319 }
320
321 /**
322 * Perform one of the "standard" actions
323 */
324 function performAction( &$output, &$article, &$title, &$user ) {
325 wfProfileIn( 'MediaWiki::performAction' );
326
327 $request = $this->getVal( 'Request' );
328 $action = $this->getVal('Action');
329 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
330 /* No such action; this will switch to the default case */
331 $action = "nosuchaction";
332 }
333
334 switch( $action ) {
335 case 'view':
336 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
337 $article->view();
338 break;
339 case 'watch':
340 case 'unwatch':
341 case 'delete':
342 case 'revert':
343 case 'rollback':
344 case 'protect':
345 case 'unprotect':
346 case 'info':
347 case 'markpatrolled':
348 case 'validate':
349 case 'render':
350 case 'deletetrackback':
351 case 'purge':
352 $article->$action();
353 break;
354 case 'print':
355 $article->view();
356 break;
357 case 'dublincore':
358 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
359 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
360 } else {
361 require_once( 'includes/Metadata.php' );
362 wfDublinCoreRdf( $article );
363 }
364 break;
365 case 'creativecommons':
366 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
367 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
368 } else {
369 require_once( 'includes/Metadata.php' );
370 wfCreativeCommonsRdf( $article );
371 }
372 break;
373 case 'credits':
374 require_once( 'includes/Credits.php' );
375 showCreditsPage( $article );
376 break;
377 case 'submit':
378 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
379 /* Send a cookie so anons get talk message notifications */
380 User::SetupSession();
381 }
382 /* Continue... */
383 case 'edit':
384 $internal = $request->getVal( 'internaledit' );
385 $external = $request->getVal( 'externaledit' );
386 $section = $request->getVal( 'section' );
387 $oldid = $request->getVal( 'oldid' );
388 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
389 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
390 require_once( 'includes/EditPage.php' );
391 $editor = new EditPage( $article );
392 $editor->submit();
393 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
394 require_once( 'includes/ExternalEdit.php' );
395 $mode = $request->getVal( 'mode' );
396 $extedit = new ExternalEdit( $article, $mode );
397 $extedit->edit();
398 }
399 break;
400 case 'history':
401 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
402 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
403 }
404 require_once( 'includes/PageHistory.php' );
405 $history = new PageHistory( $article );
406 $history->history();
407 break;
408 case 'raw':
409 require_once( 'includes/RawPage.php' );
410 $raw = new RawPage( $article );
411 $raw->view();
412 break;
413 default:
414 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
415 $output->errorpage( 'nosuchaction', 'nosuchactiontext' );
416 }
417 wfProfileOut( 'MediaWiki::performAction' );
418 }
419
420
421 }
422
423 }; /* End of class MediaWiki */
424
425 ?>