$wgArticle is deprecated! Possible removal in 1.20 or 1.21!
[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 throw new ErrorPageError( 'badtitle', 'badtitletext' );
154
155 // Interwiki redirects
156 } else if ( $this->context->title->getInterwiki() != '' ) {
157 $rdfrom = $this->context->request->getVal( 'rdfrom' );
158 if ( $rdfrom ) {
159 $url = $this->context->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
160 } else {
161 $query = $this->context->request->getValues();
162 unset( $query['title'] );
163 $url = $this->context->title->getFullURL( $query );
164 }
165 // Check for a redirect loop
166 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $this->context->title->isLocal() ) {
167 // 301 so google et al report the target as the actual url.
168 $this->context->output->redirect( $url, 301 );
169 } else {
170 $this->context->title = new BadTitle;
171 wfProfileOut( __METHOD__ );
172 throw new ErrorPageError( 'badtitle', 'badtitletext' );
173 }
174 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
175 } else if ( $this->context->request->getVal( 'action', 'view' ) == 'view' && !$this->context->request->wasPosted()
176 && ( $this->context->request->getVal( 'title' ) === null || $this->context->title->getPrefixedDBKey() != $this->context->request->getVal( 'title' ) )
177 && !count( array_diff( array_keys( $this->context->request->getValues() ), array( 'action', 'title' ) ) ) )
178 {
179 if ( $this->context->title->getNamespace() == NS_SPECIAL ) {
180 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $this->context->title->getDBkey() );
181 if ( $name ) {
182 $this->context->title = SpecialPage::getTitleFor( $name, $subpage );
183 }
184 }
185 $targetUrl = $this->context->title->getFullURL();
186 // Redirect to canonical url, make it a 301 to allow caching
187 if ( $targetUrl == $this->context->request->getFullRequestURL() ) {
188 $message = "Redirect loop detected!\n\n" .
189 "This means the wiki got confused about what page was " .
190 "requested; this sometimes happens when moving a wiki " .
191 "to a new server or changing the server configuration.\n\n";
192
193 if ( $wgUsePathInfo ) {
194 $message .= "The wiki is trying to interpret the page " .
195 "title from the URL path portion (PATH_INFO), which " .
196 "sometimes fails depending on the web server. Try " .
197 "setting \"\$wgUsePathInfo = false;\" in your " .
198 "LocalSettings.php, or check that \$wgArticlePath " .
199 "is correct.";
200 } else {
201 $message .= "Your web server was detected as possibly not " .
202 "supporting URL path components (PATH_INFO) correctly; " .
203 "check your LocalSettings.php for a customized " .
204 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
205 "to true.";
206 }
207 wfHttpError( 500, "Internal error", $message );
208 wfProfileOut( __METHOD__ );
209 return false;
210 } else {
211 $this->context->output->setSquidMaxage( 1200 );
212 $this->context->output->redirect( $targetUrl, '301' );
213 }
214 // Special pages
215 } else if ( NS_SPECIAL == $this->context->title->getNamespace() ) {
216 // actions that need to be made when we have a special pages
217 SpecialPageFactory::executePath( $this->context->title, $this->context );
218 } else {
219 // No match to special cases
220 wfProfileOut( __METHOD__ );
221 return false;
222 }
223 // Did match a special case
224 wfProfileOut( __METHOD__ );
225 return true;
226 }
227
228 /**
229 * Create an Article object of the appropriate class for the given page.
230 *
231 * @param $title Title
232 * @param $context RequestContext
233 * @return Article object
234 */
235 public static function articleFromTitle( $title, RequestContext $context ) {
236 if ( NS_MEDIA == $title->getNamespace() ) {
237 // @todo FIXME: Where should this go?
238 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
239 }
240
241 $article = null;
242 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
243 if ( $article ) {
244 return $article;
245 }
246
247 switch( $title->getNamespace() ) {
248 case NS_FILE:
249 $page = new ImagePage( $title );
250 break;
251 case NS_CATEGORY:
252 $page = new CategoryPage( $title );
253 break;
254 default:
255 $page = new Article( $title );
256 }
257 $page->setContext( $context );
258 return $page;
259 }
260
261 /**
262 * Returns the action that will be executed, not necesserly the one passed
263 * passed through the "action" parameter. Actions disabled in
264 * $wgDisabledActions will be replaced by "nosuchaction"
265 *
266 * @return String: action
267 */
268 public function getAction() {
269 global $wgDisabledActions;
270
271 $action = $this->context->request->getVal( 'action', 'view' );
272
273 // Check for disabled actions
274 if ( in_array( $action, $wgDisabledActions ) ) {
275 return 'nosuchaction';
276 }
277
278 // Workaround for bug #20966: inability of IE to provide an action dependent
279 // on which submit button is clicked.
280 if ( $action === 'historysubmit' ) {
281 if ( $this->context->request->getBool( 'revisiondelete' ) ) {
282 return 'revisiondelete';
283 } else {
284 return 'view';
285 }
286 } elseif ( $action == 'editredlink' ) {
287 return 'edit';
288 }
289
290 return $action;
291 }
292
293 /**
294 * Initialize the main Article object for "standard" actions (view, etc)
295 * Create an Article object for the page, following redirects if needed.
296 *
297 * @return mixed an Article, or a string to redirect to another URL
298 */
299 private function initializeArticle() {
300 global $wgDisableHardRedirects;
301
302 wfProfileIn( __METHOD__ );
303
304 $action = $this->context->request->getVal( 'action', 'view' );
305 $article = self::articleFromTitle( $this->context->title, $this->context );
306 // NS_MEDIAWIKI has no redirects.
307 // It is also used for CSS/JS, so performance matters here...
308 if ( $this->context->title->getNamespace() == NS_MEDIAWIKI ) {
309 wfProfileOut( __METHOD__ );
310 return $article;
311 }
312 // Namespace might change when using redirects
313 // Check for redirects ...
314 $file = ( $this->context->title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
315 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
316 && !$this->context->request->getVal( 'oldid' ) && // ... and are not old revisions
317 !$this->context->request->getVal( 'diff' ) && // ... and not when showing diff
318 $this->context->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
319 // ... and the article is not a non-redirect image page with associated file
320 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
321 {
322 // Give extensions a change to ignore/handle redirects as needed
323 $ignoreRedirect = $target = false;
324
325 wfRunHooks( 'InitializeArticleMaybeRedirect',
326 array( &$this->context->title, &$this->context->request, &$ignoreRedirect, &$target, &$article ) );
327
328 // Follow redirects only for... redirects.
329 // If $target is set, then a hook wanted to redirect.
330 if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) {
331 // Is the target already set by an extension?
332 $target = $target ? $target : $article->followRedirect();
333 if ( is_string( $target ) ) {
334 if ( !$wgDisableHardRedirects ) {
335 // we'll need to redirect
336 wfProfileOut( __METHOD__ );
337 return $target;
338 }
339 }
340 if ( is_object( $target ) ) {
341 // Rewrite environment to redirected article
342 $rarticle = self::articleFromTitle( $target, $this->context );
343 $rarticle->loadPageData();
344 if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
345 $rarticle->setRedirectedFrom( $this->context->title );
346 $article = $rarticle;
347 $this->context->title = $target;
348 }
349 }
350 } else {
351 $this->context->title = $article->getTitle();
352 }
353 }
354 wfProfileOut( __METHOD__ );
355 return $article;
356 }
357
358 /**
359 * Cleaning up request by doing deferred updates, DB transaction, and the output
360 */
361 public function finalCleanup() {
362 wfProfileIn( __METHOD__ );
363 // Now commit any transactions, so that unreported errors after
364 // output() don't roll back the whole DB transaction
365 $factory = wfGetLBFactory();
366 $factory->commitMasterChanges();
367 // Output everything!
368 $this->context->output->output();
369 // Do any deferred jobs
370 wfDoUpdates( 'commit' );
371 // Close the session so that jobs don't access the current session
372 session_write_close();
373 $this->doJobs();
374 wfProfileOut( __METHOD__ );
375 }
376
377 /**
378 * Do a job from the job queue
379 */
380 private function doJobs() {
381 global $wgJobRunRate;
382
383 if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
384 return;
385 }
386 if ( $wgJobRunRate < 1 ) {
387 $max = mt_getrandmax();
388 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
389 return;
390 }
391 $n = 1;
392 } else {
393 $n = intval( $wgJobRunRate );
394 }
395
396 while ( $n-- && false != ( $job = Job::pop() ) ) {
397 $output = $job->toString() . "\n";
398 $t = -wfTime();
399 $success = $job->run();
400 $t += wfTime();
401 $t = round( $t * 1000 );
402 if ( !$success ) {
403 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
404 } else {
405 $output .= "Success, Time: $t ms\n";
406 }
407 wfDebugLog( 'jobqueue', $output );
408 }
409 }
410
411 /**
412 * Ends this task peacefully
413 */
414 public function restInPeace() {
415 MessageCache::logMessages();
416 wfLogProfilingData();
417 // Commit and close up!
418 $factory = wfGetLBFactory();
419 $factory->commitMasterChanges();
420 $factory->shutdown();
421 wfDebug( "Request ended normally\n" );
422 }
423
424 /**
425 * Perform one of the "standard" actions
426 *
427 * @param $article Article
428 */
429 private function performAction( $article ) {
430 global $wgSquidMaxage, $wgUseExternalEditor,
431 $wgEnableDublinCoreRdf, $wgEnableCreativeCommonsRdf;
432
433 wfProfileIn( __METHOD__ );
434
435 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
436 $this->context->output, $article, $this->context->title,
437 $this->context->user, $this->context->request, $this ) ) )
438 {
439 wfProfileOut( __METHOD__ );
440 return;
441 }
442
443 $act = $this->getAction();
444
445 $action = Action::factory( $this->getAction(), $article );
446 if( $action instanceof Action ){
447 $action->show();
448 wfProfileOut( __METHOD__ );
449 return;
450 }
451
452 switch( $act ) {
453 case 'view':
454 $this->context->output->setSquidMaxage( $wgSquidMaxage );
455 $article->view();
456 break;
457 case 'raw': // includes JS/CSS
458 wfProfileIn( __METHOD__ . '-raw' );
459 $raw = new RawPage( $article );
460 $raw->view();
461 wfProfileOut( __METHOD__ . '-raw' );
462 break;
463 case 'delete':
464 case 'revert':
465 case 'rollback':
466 case 'protect':
467 case 'unprotect':
468 case 'info':
469 case 'markpatrolled':
470 case 'render':
471 case 'deletetrackback':
472 $article->$act();
473 break;
474 case 'dublincore':
475 if ( !$wgEnableDublinCoreRdf ) {
476 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
477 } else {
478 $rdf = new DublinCoreRdf( $article );
479 $rdf->show();
480 }
481 break;
482 case 'creativecommons':
483 if ( !$wgEnableCreativeCommonsRdf ) {
484 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
485 } else {
486 $rdf = new CreativeCommonsRdf( $article );
487 $rdf->show();
488 }
489 break;
490 case 'submit':
491 if ( session_id() == '' ) {
492 // Send a cookie so anons get talk message notifications
493 wfSetupSession();
494 }
495 // Continue...
496 case 'edit':
497 if ( wfRunHooks( 'CustomEditor', array( $article, $this->context->user ) ) ) {
498 $internal = $this->context->request->getVal( 'internaledit' );
499 $external = $this->context->request->getVal( 'externaledit' );
500 $section = $this->context->request->getVal( 'section' );
501 $oldid = $this->context->request->getVal( 'oldid' );
502 if ( !$wgUseExternalEditor || $act == 'submit' || $internal ||
503 $section || $oldid || ( !$this->context->user->getOption( 'externaleditor' ) && !$external ) ) {
504 $editor = new EditPage( $article );
505 $editor->submit();
506 } elseif ( $wgUseExternalEditor && ( $external || $this->context->user->getOption( 'externaleditor' ) ) ) {
507 $mode = $this->context->request->getVal( 'mode' );
508 $extedit = new ExternalEdit( $article, $mode );
509 $extedit->edit();
510 }
511 }
512 break;
513 case 'history':
514 if ( $this->context->request->getFullRequestURL() == $this->context->title->getInternalURL( 'action=history' ) ) {
515 $this->context->output->setSquidMaxage( $wgSquidMaxage );
516 }
517 $history = new HistoryPage( $article );
518 $history->history();
519 break;
520 case 'revisiondelete':
521 // For show/hide submission from history page
522 $special = SpecialPageFactory::getPage( 'Revisiondelete' );
523 $special->execute( '' );
524 break;
525 default:
526 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
527 $this->context->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
528 }
529 }
530 wfProfileOut( __METHOD__ );
531 }
532 }