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