Removed configuration storage in the MediaWiki class:
[lhc/web/wiklou.git] / includes / Wiki.php
1 <?php
2 /**
3 * MediaWiki is the to-be base class for this whole project
4 *
5 * @internal documentation reviewed 15 Mar 2010
6 */
7 class MediaWiki {
8
9 /**
10 * TODO: fold $output, etc, into this
11 * @var RequestContext
12 */
13 private $context;
14
15 public function request( WebRequest $x = null ){
16 return wfSetVar( $this->context->request, $x );
17 }
18
19 public function output( OutputPage $x = null ){
20 return wfSetVar( $this->context->output, $x );
21 }
22
23 public function __construct( RequestContext $context ){
24 $this->context = $context;
25 $this->context->setTitle( $this->parseTitle() );
26 }
27
28 /**
29 * Initialization of ... everything
30 * Performs the request too
31 *
32 * @param $article Article
33 */
34 public function performRequestForTitle( &$article ) {
35 wfProfileIn( __METHOD__ );
36
37 if ( $this->context->request->getVal( 'printable' ) === 'yes' ) {
38 $this->context->output->setPrintable();
39 }
40
41 wfRunHooks( 'BeforeInitialize', array(
42 &$this->context->title,
43 &$article,
44 &$this->context->output,
45 &$this->context->user,
46 $this->context->request,
47 $this
48 ) );
49
50 // If the user is not logged in, the Namespace:title of the article must be in
51 // the Read array in order for the user to see it. (We have to check here to
52 // catch special pages etc. We check again in Article::view())
53 if ( !is_null( $this->context->title ) && !$this->context->title->userCanRead() ) {
54 $this->context->output->loginToUse();
55 $this->finalCleanup();
56 $this->context->output->disable();
57 wfProfileOut( __METHOD__ );
58 return false;
59 }
60
61 // Call handleSpecialCases() to deal with all special requests...
62 if ( !$this->handleSpecialCases() ) {
63 // ...otherwise treat it as an article view. The article
64 // may be a redirect to another article or URL.
65 $new_article = $this->initializeArticle();
66 if ( is_object( $new_article ) ) {
67 $article = $new_article;
68 $this->performAction( $article );
69 } elseif ( is_string( $new_article ) ) {
70 $this->context->output->redirect( $new_article );
71 } else {
72 wfProfileOut( __METHOD__ );
73 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
74 }
75 }
76 wfProfileOut( __METHOD__ );
77 }
78
79 /**
80 * Parse $request to get the Title object
81 *
82 * @return Title object to be $wgTitle
83 */
84 private function parseTitle() {
85 global $wgContLang;
86
87 $curid = $this->context->request->getInt( 'curid' );
88 $title = $this->context->request->getVal( 'title' );
89
90 if ( $this->context->request->getCheck( 'search' ) ) {
91 // Compatibility with old search URLs which didn't use Special:Search
92 // Just check for presence here, so blank requests still
93 // show the search page when using ugly URLs (bug 8054).
94 $ret = SpecialPage::getTitleFor( 'Search' );
95 } elseif ( $curid ) {
96 // URLs like this are generated by RC, because rc_title isn't always accurate
97 $ret = Title::newFromID( $curid );
98 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
99 $ret = Title::newMainPage();
100 } else {
101 $ret = Title::newFromURL( $title );
102 // check variant links so that interwiki links don't have to worry
103 // about the possible different language variants
104 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
105 $wgContLang->findVariantLink( $title, $ret );
106 }
107 }
108 // For non-special titles, check for implicit titles
109 if ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
110 // We can have urls with just ?diff=,?oldid= or even just ?diff=
111 $oldid = $this->context->request->getInt( 'oldid' );
112 $oldid = $oldid ? $oldid : $this->context->request->getInt( 'diff' );
113 // Allow oldid to override a changed or missing title
114 if ( $oldid ) {
115 $rev = Revision::newFromId( $oldid );
116 $ret = $rev ? $rev->getTitle() : $ret;
117 }
118 }
119
120 if( $ret === null || ( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
121 $ret = new BadTitle;
122 }
123 return $ret;
124 }
125
126 /**
127 * Get the Title object that we'll be acting on, as specified in the WebRequest
128 * @return Title
129 */
130 public function getTitle(){
131 if( $this->context->title === null ){
132 $this->context->title = $this->parseTitle();
133 }
134 return $this->context->title;
135 }
136
137 /**
138 * Initialize some special cases:
139 * - bad titles
140 * - local interwiki redirects
141 * - redirect loop
142 * - special pages
143 *
144 * @return bool true if the request is already executed
145 */
146 private function handleSpecialCases() {
147 global $wgServer, $wgUsePathInfo;
148
149 wfProfileIn( __METHOD__ );
150
151 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
152 if ( $this->context->title instanceof BadTitle ) {
153 // Die now before we mess up $wgArticle and the skin stops working
154 throw new ErrorPageError( 'badtitle', 'badtitletext' );
155
156 // Interwiki redirects
157 } else if ( $this->context->title->getInterwiki() != '' ) {
158 $rdfrom = $this->context->request->getVal( 'rdfrom' );
159 if ( $rdfrom ) {
160 $url = $this->context->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
161 } else {
162 $query = $this->context->request->getValues();
163 unset( $query['title'] );
164 $url = $this->context->title->getFullURL( $query );
165 }
166 // Check for a redirect loop
167 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $this->context->title->isLocal() ) {
168 // 301 so google et al report the target as the actual url.
169 $this->context->output->redirect( $url, 301 );
170 } else {
171 $this->context->title = new BadTitle;
172 wfProfileOut( __METHOD__ );
173 throw new ErrorPageError( 'badtitle', 'badtitletext' );
174 }
175 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
176 } else if ( $this->context->request->getVal( 'action', 'view' ) == 'view' && !$this->context->request->wasPosted()
177 && ( $this->context->request->getVal( 'title' ) === null || $this->context->title->getPrefixedDBKey() != $this->context->request->getVal( 'title' ) )
178 && !count( array_diff( array_keys( $this->context->request->getValues() ), array( 'action', 'title' ) ) ) )
179 {
180 if ( $this->context->title->getNamespace() == NS_SPECIAL ) {
181 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $this->context->title->getDBkey() );
182 if ( $name ) {
183 $this->context->title = SpecialPage::getTitleFor( $name, $subpage );
184 }
185 }
186 $targetUrl = $this->context->title->getFullURL();
187 // Redirect to canonical url, make it a 301 to allow caching
188 if ( $targetUrl == $this->context->request->getFullRequestURL() ) {
189 $message = "Redirect loop detected!\n\n" .
190 "This means the wiki got confused about what page was " .
191 "requested; this sometimes happens when moving a wiki " .
192 "to a new server or changing the server configuration.\n\n";
193
194 if ( $wgUsePathInfo ) {
195 $message .= "The wiki is trying to interpret the page " .
196 "title from the URL path portion (PATH_INFO), which " .
197 "sometimes fails depending on the web server. Try " .
198 "setting \"\$wgUsePathInfo = false;\" in your " .
199 "LocalSettings.php, or check that \$wgArticlePath " .
200 "is correct.";
201 } else {
202 $message .= "Your web server was detected as possibly not " .
203 "supporting URL path components (PATH_INFO) correctly; " .
204 "check your LocalSettings.php for a customized " .
205 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
206 "to true.";
207 }
208 wfHttpError( 500, "Internal error", $message );
209 wfProfileOut( __METHOD__ );
210 return false;
211 } else {
212 $this->context->output->setSquidMaxage( 1200 );
213 $this->context->output->redirect( $targetUrl, '301' );
214 }
215 // Special pages
216 } else if ( NS_SPECIAL == $this->context->title->getNamespace() ) {
217 // actions that need to be made when we have a special pages
218 SpecialPageFactory::executePath( $this->context->title, $this->context );
219 } else {
220 // No match to special cases
221 wfProfileOut( __METHOD__ );
222 return false;
223 }
224 // Did match a special case
225 wfProfileOut( __METHOD__ );
226 return true;
227 }
228
229 /**
230 * Create an Article object of the appropriate class for the given page.
231 *
232 * @param $title Title
233 * @param $context RequestContext
234 * @return Article object
235 */
236 public static function articleFromTitle( $title, RequestContext $context ) {
237 if ( NS_MEDIA == $title->getNamespace() ) {
238 // FIXME: where should this go?
239 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
240 }
241
242 $article = null;
243 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
244 if ( $article ) {
245 return $article;
246 }
247
248 switch( $title->getNamespace() ) {
249 case NS_FILE:
250 $page = new ImagePage( $title );
251 break;
252 case NS_CATEGORY:
253 $page = new CategoryPage( $title );
254 break;
255 default:
256 $page = new Article( $title );
257 }
258 $page->setContext( $context );
259 return $page;
260 }
261
262 /**
263 * Returns the action that will be executed, not necesserly the one passed
264 * passed through the "action" parameter. Actions disabled in
265 * $wgDisabledActions will be replaced by "nosuchaction"
266 *
267 * @return String: action
268 */
269 public function getAction() {
270 global $wgDisabledActions;
271
272 $action = $this->context->request->getVal( 'action', 'view' );
273
274 // Check for disabled actions
275 if ( in_array( $action, $wgDisabledActions ) ) {
276 return 'nosuchaction';
277 }
278
279 // Workaround for bug #20966: inability of IE to provide an action dependent
280 // on which submit button is clicked.
281 if ( $action === 'historysubmit' ) {
282 if ( $this->context->request->getBool( 'revisiondelete' ) ) {
283 return 'revisiondelete';
284 } else {
285 return 'view';
286 }
287 } elseif ( $action == 'editredlink' ) {
288 return 'edit';
289 }
290
291 return $action;
292 }
293
294 /**
295 * Initialize the object to be known as $wgArticle for "standard" actions
296 * Create an Article object for the page, following redirects if needed.
297 *
298 * @return mixed an Article, or a string to redirect to another URL
299 */
300 private function initializeArticle() {
301 global $wgDisableHardRedirects;
302
303 wfProfileIn( __METHOD__ );
304
305 $action = $this->context->request->getVal( 'action', 'view' );
306 $article = self::articleFromTitle( $this->context->title, $this->context );
307 // NS_MEDIAWIKI has no redirects.
308 // It is also used for CSS/JS, so performance matters here...
309 if ( $this->context->title->getNamespace() == NS_MEDIAWIKI ) {
310 wfProfileOut( __METHOD__ );
311 return $article;
312 }
313 // Namespace might change when using redirects
314 // Check for redirects ...
315 $file = ( $this->context->title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
316 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
317 && !$this->context->request->getVal( 'oldid' ) && // ... and are not old revisions
318 !$this->context->request->getVal( 'diff' ) && // ... and not when showing diff
319 $this->context->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
320 // ... and the article is not a non-redirect image page with associated file
321 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
322 {
323 // Give extensions a change to ignore/handle redirects as needed
324 $ignoreRedirect = $target = false;
325
326 wfRunHooks( 'InitializeArticleMaybeRedirect',
327 array( &$this->context->title, &$this->context->request, &$ignoreRedirect, &$target, &$article ) );
328
329 // Follow redirects only for... redirects.
330 // If $target is set, then a hook wanted to redirect.
331 if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) {
332 // Is the target already set by an extension?
333 $target = $target ? $target : $article->followRedirect();
334 if ( is_string( $target ) ) {
335 if ( !$wgDisableHardRedirects ) {
336 // we'll need to redirect
337 wfProfileOut( __METHOD__ );
338 return $target;
339 }
340 }
341 if ( is_object( $target ) ) {
342 // Rewrite environment to redirected article
343 $rarticle = self::articleFromTitle( $target, $this->context );
344 $rarticle->loadPageData();
345 if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
346 $rarticle->setRedirectedFrom( $this->context->title );
347 $article = $rarticle;
348 $this->context->title = $target;
349 }
350 }
351 } else {
352 $this->context->title = $article->getTitle();
353 }
354 }
355 wfProfileOut( __METHOD__ );
356 return $article;
357 }
358
359 /**
360 * Cleaning up request by doing deferred updates, DB transaction, and the output
361 */
362 public function finalCleanup() {
363 wfProfileIn( __METHOD__ );
364 // Now commit any transactions, so that unreported errors after
365 // output() don't roll back the whole DB transaction
366 $factory = wfGetLBFactory();
367 $factory->commitMasterChanges();
368 // Output everything!
369 $this->context->output->output();
370 // Do any deferred jobs
371 wfDoUpdates( 'commit' );
372 // Close the session so that jobs don't access the current session
373 session_write_close();
374 $this->doJobs();
375 wfProfileOut( __METHOD__ );
376 }
377
378 /**
379 * Do a job from the job queue
380 */
381 private function doJobs() {
382 global $wgJobRunRate;
383
384 if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
385 return;
386 }
387 if ( $wgJobRunRate < 1 ) {
388 $max = mt_getrandmax();
389 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
390 return;
391 }
392 $n = 1;
393 } else {
394 $n = intval( $wgJobRunRate );
395 }
396
397 while ( $n-- && false != ( $job = Job::pop() ) ) {
398 $output = $job->toString() . "\n";
399 $t = -wfTime();
400 $success = $job->run();
401 $t += wfTime();
402 $t = round( $t * 1000 );
403 if ( !$success ) {
404 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
405 } else {
406 $output .= "Success, Time: $t ms\n";
407 }
408 wfDebugLog( 'jobqueue', $output );
409 }
410 }
411
412 /**
413 * Ends this task peacefully
414 */
415 public function restInPeace() {
416 MessageCache::logMessages();
417 wfLogProfilingData();
418 // Commit and close up!
419 $factory = wfGetLBFactory();
420 $factory->commitMasterChanges();
421 $factory->shutdown();
422 wfDebug( "Request ended normally\n" );
423 }
424
425 /**
426 * Perform one of the "standard" actions
427 *
428 * @param $article Article
429 */
430 private function performAction( $article ) {
431 global $wgSquidMaxage, $wgUseExternalEditor,
432 $wgEnableDublinCoreRdf, $wgEnableCreativeCommonsRdf;
433
434 wfProfileIn( __METHOD__ );
435
436 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
437 $this->context->output, $article, $this->context->title,
438 $this->context->user, $this->context->request, $this ) ) )
439 {
440 wfProfileOut( __METHOD__ );
441 return;
442 }
443
444 $act = $this->getAction();
445
446 $action = Action::factory( $this->getAction(), $article );
447 if( $action instanceof Action ){
448 $action->show();
449 wfProfileOut( __METHOD__ );
450 return;
451 }
452
453 switch( $act ) {
454 case 'view':
455 $this->context->output->setSquidMaxage( $wgSquidMaxage );
456 $article->view();
457 break;
458 case 'raw': // includes JS/CSS
459 wfProfileIn( __METHOD__ . '-raw' );
460 $raw = new RawPage( $article );
461 $raw->view();
462 wfProfileOut( __METHOD__ . '-raw' );
463 break;
464 case 'delete':
465 case 'revert':
466 case 'rollback':
467 case 'protect':
468 case 'unprotect':
469 case 'info':
470 case 'markpatrolled':
471 case 'render':
472 case 'deletetrackback':
473 $article->$act();
474 break;
475 case 'dublincore':
476 if ( !$wgEnableDublinCoreRdf ) {
477 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
478 } else {
479 $rdf = new DublinCoreRdf( $article );
480 $rdf->show();
481 }
482 break;
483 case 'creativecommons':
484 if ( !$wgEnableCreativeCommonsRdf ) {
485 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
486 } else {
487 $rdf = new CreativeCommonsRdf( $article );
488 $rdf->show();
489 }
490 break;
491 case 'submit':
492 if ( session_id() == '' ) {
493 // Send a cookie so anons get talk message notifications
494 wfSetupSession();
495 }
496 // Continue...
497 case 'edit':
498 if ( wfRunHooks( 'CustomEditor', array( $article, $this->context->user ) ) ) {
499 $internal = $this->context->request->getVal( 'internaledit' );
500 $external = $this->context->request->getVal( 'externaledit' );
501 $section = $this->context->request->getVal( 'section' );
502 $oldid = $this->context->request->getVal( 'oldid' );
503 if ( !$wgUseExternalEditor || $act == 'submit' || $internal ||
504 $section || $oldid || ( !$this->context->user->getOption( 'externaleditor' ) && !$external ) ) {
505 $editor = new EditPage( $article );
506 $editor->submit();
507 } elseif ( $wgUseExternalEditor && ( $external || $this->context->user->getOption( 'externaleditor' ) ) ) {
508 $mode = $this->context->request->getVal( 'mode' );
509 $extedit = new ExternalEdit( $article, $mode );
510 $extedit->edit();
511 }
512 }
513 break;
514 case 'history':
515 if ( $this->context->request->getFullRequestURL() == $this->context->title->getInternalURL( 'action=history' ) ) {
516 $this->context->output->setSquidMaxage( $wgSquidMaxage );
517 }
518 $history = new HistoryPage( $article );
519 $history->history();
520 break;
521 case 'revisiondelete':
522 // For show/hide submission from history page
523 $special = SpecialPageFactory::getPage( 'Revisiondelete' );
524 $special->execute( '' );
525 break;
526 default:
527 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
528 $this->context->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
529 }
530 }
531 wfProfileOut( __METHOD__ );
532 }
533 }