MediaWiki
master
|
00001 <?php 00028 class MediaWiki { 00029 00034 private $context; 00035 00040 public function request( WebRequest $x = null ) { 00041 $old = $this->context->getRequest(); 00042 $this->context->setRequest( $x ); 00043 return $old; 00044 } 00045 00050 public function output( OutputPage $x = null ) { 00051 $old = $this->context->getOutput(); 00052 $this->context->setOutput( $x ); 00053 return $old; 00054 } 00055 00059 public function __construct( IContextSource $context = null ) { 00060 if ( !$context ) { 00061 $context = RequestContext::getMain(); 00062 } 00063 00064 $this->context = $context; 00065 } 00066 00072 private function parseTitle() { 00073 global $wgContLang; 00074 00075 $request = $this->context->getRequest(); 00076 $curid = $request->getInt( 'curid' ); 00077 $title = $request->getVal( 'title' ); 00078 $action = $request->getVal( 'action', 'view' ); 00079 00080 if ( $request->getCheck( 'search' ) ) { 00081 // Compatibility with old search URLs which didn't use Special:Search 00082 // Just check for presence here, so blank requests still 00083 // show the search page when using ugly URLs (bug 8054). 00084 $ret = SpecialPage::getTitleFor( 'Search' ); 00085 } elseif ( $curid ) { 00086 // URLs like this are generated by RC, because rc_title isn't always accurate 00087 $ret = Title::newFromID( $curid ); 00088 } elseif ( $title == '' && $action != 'delete' ) { 00089 $ret = Title::newMainPage(); 00090 } else { 00091 $ret = Title::newFromURL( $title ); 00092 // Alias NS_MEDIA page URLs to NS_FILE...we only use NS_MEDIA 00093 // in wikitext links to tell Parser to make a direct file link 00094 if ( !is_null( $ret ) && $ret->getNamespace() == NS_MEDIA ) { 00095 $ret = Title::makeTitle( NS_FILE, $ret->getDBkey() ); 00096 } 00097 // Check variant links so that interwiki links don't have to worry 00098 // about the possible different language variants 00099 if ( count( $wgContLang->getVariants() ) > 1 00100 && !is_null( $ret ) && $ret->getArticleID() == 0 ) 00101 { 00102 $wgContLang->findVariantLink( $title, $ret ); 00103 } 00104 } 00105 // For non-special titles, check for implicit titles 00106 if ( is_null( $ret ) || !$ret->isSpecialPage() ) { 00107 // We can have urls with just ?diff=,?oldid= or even just ?diff= 00108 $oldid = $request->getInt( 'oldid' ); 00109 $oldid = $oldid ? $oldid : $request->getInt( 'diff' ); 00110 // Allow oldid to override a changed or missing title 00111 if ( $oldid ) { 00112 $rev = Revision::newFromId( $oldid ); 00113 $ret = $rev ? $rev->getTitle() : $ret; 00114 } 00115 } 00116 00117 if ( $ret === null || ( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ) { 00118 $ret = SpecialPage::getTitleFor( 'Badtitle' ); 00119 } 00120 00121 return $ret; 00122 } 00123 00128 public function getTitle() { 00129 if( $this->context->getTitle() === null ){ 00130 $this->context->setTitle( $this->parseTitle() ); 00131 } 00132 return $this->context->getTitle(); 00133 } 00134 00140 public function getAction() { 00141 static $action = null; 00142 00143 if ( $action === null ) { 00144 $action = Action::getActionName( $this->context ); 00145 } 00146 00147 return $action; 00148 } 00149 00158 public static function articleFromTitle( $title, IContextSource $context ) { 00159 wfDeprecated( __METHOD__, '1.18' ); 00160 return Article::newFromTitle( $title, $context ); 00161 } 00162 00175 private function performRequest() { 00176 global $wgServer, $wgUsePathInfo, $wgTitle; 00177 00178 wfProfileIn( __METHOD__ ); 00179 00180 $request = $this->context->getRequest(); 00181 $requestTitle = $title = $this->context->getTitle(); 00182 $output = $this->context->getOutput(); 00183 $user = $this->context->getUser(); 00184 00185 if ( $request->getVal( 'printable' ) === 'yes' ) { 00186 $output->setPrintable(); 00187 } 00188 00189 $unused = null; // To pass it by reference 00190 wfRunHooks( 'BeforeInitialize', array( &$title, &$unused, &$output, &$user, $request, $this ) ); 00191 00192 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty. 00193 if ( is_null( $title ) || ( $title->getDBkey() == '' && $title->getInterwiki() == '' ) || 00194 $title->isSpecial( 'Badtitle' ) ) 00195 { 00196 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) ); 00197 wfProfileOut( __METHOD__ ); 00198 throw new BadTitleError(); 00199 } 00200 00201 // Check user's permissions to read this page. 00202 // We have to check here to catch special pages etc. 00203 // We will check again in Article::view(). 00204 $permErrors = $title->getUserPermissionsErrors( 'read', $user ); 00205 if ( count( $permErrors ) ) { 00206 // Bug 32276: allowing the skin to generate output with $wgTitle or 00207 // $this->context->title set to the input title would allow anonymous users to 00208 // determine whether a page exists, potentially leaking private data. In fact, the 00209 // curid and oldid request parameters would allow page titles to be enumerated even 00210 // when they are not guessable. So we reset the title to Special:Badtitle before the 00211 // permissions error is displayed. 00212 // 00213 // The skin mostly uses $this->context->getTitle() these days, but some extensions 00214 // still use $wgTitle. 00215 00216 $badTitle = SpecialPage::getTitleFor( 'Badtitle' ); 00217 $this->context->setTitle( $badTitle ); 00218 $wgTitle = $badTitle; 00219 00220 wfProfileOut( __METHOD__ ); 00221 throw new PermissionsError( 'read', $permErrors ); 00222 } 00223 00224 $pageView = false; // was an article or special page viewed? 00225 00226 // Interwiki redirects 00227 if ( $title->getInterwiki() != '' ) { 00228 $rdfrom = $request->getVal( 'rdfrom' ); 00229 if ( $rdfrom ) { 00230 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) ); 00231 } else { 00232 $query = $request->getValues(); 00233 unset( $query['title'] ); 00234 $url = $title->getFullURL( $query ); 00235 } 00236 // Check for a redirect loop 00237 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) 00238 && $title->isLocal() ) 00239 { 00240 // 301 so google et al report the target as the actual url. 00241 $output->redirect( $url, 301 ); 00242 } else { 00243 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) ); 00244 wfProfileOut( __METHOD__ ); 00245 throw new BadTitleError(); 00246 } 00247 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant 00248 } elseif ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted() 00249 && ( $request->getVal( 'title' ) === null || 00250 $title->getPrefixedDBKey() != $request->getVal( 'title' ) ) 00251 && !count( $request->getValueNames( array( 'action', 'title' ) ) ) 00252 && wfRunHooks( 'TestCanonicalRedirect', array( $request, $title, $output ) ) ) 00253 { 00254 if ( $title->isSpecialPage() ) { 00255 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() ); 00256 if ( $name ) { 00257 $title = SpecialPage::getTitleFor( $name, $subpage ); 00258 } 00259 } 00260 $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ); 00261 // Redirect to canonical url, make it a 301 to allow caching 00262 if ( $targetUrl == $request->getFullRequestURL() ) { 00263 $message = "Redirect loop detected!\n\n" . 00264 "This means the wiki got confused about what page was " . 00265 "requested; this sometimes happens when moving a wiki " . 00266 "to a new server or changing the server configuration.\n\n"; 00267 00268 if ( $wgUsePathInfo ) { 00269 $message .= "The wiki is trying to interpret the page " . 00270 "title from the URL path portion (PATH_INFO), which " . 00271 "sometimes fails depending on the web server. Try " . 00272 "setting \"\$wgUsePathInfo = false;\" in your " . 00273 "LocalSettings.php, or check that \$wgArticlePath " . 00274 "is correct."; 00275 } else { 00276 $message .= "Your web server was detected as possibly not " . 00277 "supporting URL path components (PATH_INFO) correctly; " . 00278 "check your LocalSettings.php for a customized " . 00279 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " . 00280 "to true."; 00281 } 00282 throw new HttpError( 500, $message ); 00283 } else { 00284 $output->setSquidMaxage( 1200 ); 00285 $output->redirect( $targetUrl, '301' ); 00286 } 00287 // Special pages 00288 } elseif ( NS_SPECIAL == $title->getNamespace() ) { 00289 $pageView = true; 00290 // Actions that need to be made when we have a special pages 00291 SpecialPageFactory::executePath( $title, $this->context ); 00292 } else { 00293 // ...otherwise treat it as an article view. The article 00294 // may be a redirect to another article or URL. 00295 $article = $this->initializeArticle(); 00296 if ( is_object( $article ) ) { 00297 $pageView = true; 00302 global $wgArticle; 00303 $wgArticle = new DeprecatedGlobal( 'wgArticle', $article, '1.18' ); 00304 00305 $this->performAction( $article, $requestTitle ); 00306 } elseif ( is_string( $article ) ) { 00307 $output->redirect( $article ); 00308 } else { 00309 wfProfileOut( __METHOD__ ); 00310 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" ); 00311 } 00312 } 00313 00314 if ( $pageView ) { 00315 // Promote user to any groups they meet the criteria for 00316 $user->addAutopromoteOnceGroups( 'onView' ); 00317 } 00318 00319 wfProfileOut( __METHOD__ ); 00320 } 00321 00328 private function initializeArticle() { 00329 global $wgDisableHardRedirects; 00330 00331 wfProfileIn( __METHOD__ ); 00332 00333 $title = $this->context->getTitle(); 00334 $article = Article::newFromTitle( $title, $this->context ); 00335 $this->context->setWikiPage( $article->getPage() ); 00336 // NS_MEDIAWIKI has no redirects. 00337 // It is also used for CSS/JS, so performance matters here... 00338 if ( $title->getNamespace() == NS_MEDIAWIKI ) { 00339 wfProfileOut( __METHOD__ ); 00340 return $article; 00341 } 00342 00343 $request = $this->context->getRequest(); 00344 00345 // Namespace might change when using redirects 00346 // Check for redirects ... 00347 $action = $request->getVal( 'action', 'view' ); 00348 $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null; 00349 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content 00350 && !$request->getVal( 'oldid' ) && // ... and are not old revisions 00351 !$request->getVal( 'diff' ) && // ... and not when showing diff 00352 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to 00353 // ... and the article is not a non-redirect image page with associated file 00354 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) ) 00355 { 00356 // Give extensions a change to ignore/handle redirects as needed 00357 $ignoreRedirect = $target = false; 00358 00359 wfRunHooks( 'InitializeArticleMaybeRedirect', 00360 array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) ); 00361 00362 // Follow redirects only for... redirects. 00363 // If $target is set, then a hook wanted to redirect. 00364 if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) { 00365 // Is the target already set by an extension? 00366 $target = $target ? $target : $article->followRedirect(); 00367 if ( is_string( $target ) ) { 00368 if ( !$wgDisableHardRedirects ) { 00369 // we'll need to redirect 00370 wfProfileOut( __METHOD__ ); 00371 return $target; 00372 } 00373 } 00374 if ( is_object( $target ) ) { 00375 // Rewrite environment to redirected article 00376 $rarticle = Article::newFromTitle( $target, $this->context ); 00377 $rarticle->loadPageData(); 00378 if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) { 00379 $rarticle->setRedirectedFrom( $title ); 00380 $article = $rarticle; 00381 $this->context->setTitle( $target ); 00382 $this->context->setWikiPage( $article->getPage() ); 00383 } 00384 } 00385 } else { 00386 $this->context->setTitle( $article->getTitle() ); 00387 $this->context->setWikiPage( $article->getPage() ); 00388 } 00389 } 00390 00391 wfProfileOut( __METHOD__ ); 00392 return $article; 00393 } 00394 00401 private function performAction( Page $page, Title $requestTitle ) { 00402 global $wgUseSquid, $wgSquidMaxage; 00403 00404 wfProfileIn( __METHOD__ ); 00405 00406 $request = $this->context->getRequest(); 00407 $output = $this->context->getOutput(); 00408 $title = $this->context->getTitle(); 00409 $user = $this->context->getUser(); 00410 00411 if ( !wfRunHooks( 'MediaWikiPerformAction', 00412 array( $output, $page, $title, $user, $request, $this ) ) ) 00413 { 00414 wfProfileOut( __METHOD__ ); 00415 return; 00416 } 00417 00418 $act = $this->getAction(); 00419 00420 $action = Action::factory( $act, $page ); 00421 if ( $action instanceof Action ) { 00422 # Let Squid cache things if we can purge them. 00423 if ( $wgUseSquid && 00424 in_array( $request->getFullRequestURL(), $requestTitle->getSquidURLs() ) 00425 ) { 00426 $output->setSquidMaxage( $wgSquidMaxage ); 00427 } 00428 00429 $action->show(); 00430 wfProfileOut( __METHOD__ ); 00431 return; 00432 } 00433 00434 if ( wfRunHooks( 'UnknownAction', array( $request->getVal( 'action', 'view' ), $page ) ) ) { 00435 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' ); 00436 } 00437 00438 wfProfileOut( __METHOD__ ); 00439 } 00440 00445 public function run() { 00446 try { 00447 $this->checkMaxLag(); 00448 $this->main(); 00449 $this->restInPeace(); 00450 } catch ( Exception $e ) { 00451 MWExceptionHandler::handle( $e ); 00452 } 00453 } 00454 00460 private function checkMaxLag() { 00461 global $wgShowHostnames; 00462 00463 wfProfileIn( __METHOD__ ); 00464 $maxLag = $this->context->getRequest()->getVal( 'maxlag' ); 00465 if ( !is_null( $maxLag ) ) { 00466 list( $host, $lag ) = wfGetLB()->getMaxLag(); 00467 if ( $lag > $maxLag ) { 00468 $resp = $this->context->getRequest()->response(); 00469 $resp->header( 'HTTP/1.1 503 Service Unavailable' ); 00470 $resp->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) ); 00471 $resp->header( 'X-Database-Lag: ' . intval( $lag ) ); 00472 $resp->header( 'Content-Type: text/plain' ); 00473 if( $wgShowHostnames ) { 00474 echo "Waiting for $host: $lag seconds lagged\n"; 00475 } else { 00476 echo "Waiting for a database server: $lag seconds lagged\n"; 00477 } 00478 00479 wfProfileOut( __METHOD__ ); 00480 00481 exit; 00482 } 00483 } 00484 wfProfileOut( __METHOD__ ); 00485 return true; 00486 } 00487 00488 private function main() { 00489 global $wgUseFileCache, $wgTitle, $wgUseAjax; 00490 00491 wfProfileIn( __METHOD__ ); 00492 00493 $request = $this->context->getRequest(); 00494 00495 if ( $request->getCookie( 'forceHTTPS' ) 00496 && $request->detectProtocol() == 'http' 00497 && $request->getMethod() == 'GET' 00498 ) { 00499 $redirUrl = $request->getFullRequestURL(); 00500 $redirUrl = str_replace( 'http://' , 'https://' , $redirUrl ); 00501 00502 // Setup dummy Title, otherwise OutputPage::redirect will fail 00503 $title = Title::newFromText( NS_MAIN, 'REDIR' ); 00504 $this->context->setTitle( $title ); 00505 $output = $this->context->getOutput(); 00506 $output->redirect( $redirUrl ); 00507 $output->output(); 00508 wfProfileOut( __METHOD__ ); 00509 return; 00510 } 00511 00512 // Send Ajax requests to the Ajax dispatcher. 00513 if ( $wgUseAjax && $request->getVal( 'action', 'view' ) == 'ajax' ) { 00514 00515 // Set a dummy title, because $wgTitle == null might break things 00516 $title = Title::makeTitle( NS_MAIN, 'AJAX' ); 00517 $this->context->setTitle( $title ); 00518 $wgTitle = $title; 00519 00520 $dispatcher = new AjaxDispatcher(); 00521 $dispatcher->performAction(); 00522 wfProfileOut( __METHOD__ ); 00523 return; 00524 } 00525 00526 // Get title from request parameters, 00527 // is set on the fly by parseTitle the first time. 00528 $title = $this->getTitle(); 00529 $action = $this->getAction(); 00530 $wgTitle = $title; 00531 00532 if ( $wgUseFileCache && $title->getNamespace() >= 0 ) { 00533 wfProfileIn( 'main-try-filecache' ); 00534 if ( HTMLFileCache::useFileCache( $this->context ) ) { 00535 // Try low-level file cache hit 00536 $cache = HTMLFileCache::newFromTitle( $title, $action ); 00537 if ( $cache->isCacheGood( /* Assume up to date */ ) ) { 00538 // Check incoming headers to see if client has this cached 00539 $timestamp = $cache->cacheTimestamp(); 00540 if ( !$this->context->getOutput()->checkLastModified( $timestamp ) ) { 00541 $cache->loadFromFileCache( $this->context ); 00542 } 00543 // Do any stats increment/watchlist stuff 00544 $this->context->getWikiPage()->doViewUpdates( $this->context->getUser() ); 00545 // Tell OutputPage that output is taken care of 00546 $this->context->getOutput()->disable(); 00547 wfProfileOut( 'main-try-filecache' ); 00548 wfProfileOut( __METHOD__ ); 00549 return; 00550 } 00551 } 00552 wfProfileOut( 'main-try-filecache' ); 00553 } 00554 00555 $this->performRequest(); 00556 00557 // Now commit any transactions, so that unreported errors after 00558 // output() don't roll back the whole DB transaction 00559 wfGetLBFactory()->commitMasterChanges(); 00560 00561 // Output everything! 00562 $this->context->getOutput()->output(); 00563 00564 wfProfileOut( __METHOD__ ); 00565 } 00566 00570 public function restInPeace() { 00571 // Do any deferred jobs 00572 DeferredUpdates::doUpdates( 'commit' ); 00573 00574 // Execute a job from the queue 00575 $this->doJobs(); 00576 00577 // Log profiling data, e.g. in the database or UDP 00578 wfLogProfilingData(); 00579 00580 // Commit and close up! 00581 $factory = wfGetLBFactory(); 00582 $factory->commitMasterChanges(); 00583 $factory->shutdown(); 00584 00585 wfDebug( "Request ended normally\n" ); 00586 } 00587 00591 private function doJobs() { 00592 global $wgJobRunRate; 00593 00594 if ( $wgJobRunRate <= 0 || wfReadOnly() ) { 00595 return; 00596 } 00597 00598 if ( $wgJobRunRate < 1 ) { 00599 $max = mt_getrandmax(); 00600 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) { 00601 return; // the higher $wgJobRunRate, the less likely we return here 00602 } 00603 $n = 1; 00604 } else { 00605 $n = intval( $wgJobRunRate ); 00606 } 00607 00608 $group = JobQueueGroup::singleton(); 00609 $types = $group->getDefaultQueueTypes(); 00610 shuffle( $types ); // avoid starvation 00611 00612 // Scan the queues for a job N times... 00613 do { 00614 $jobFound = false; // found a job in any queue? 00615 // Find a queue with a job on it and run it... 00616 foreach ( $types as $i => $type ) { 00617 $queue = $group->get( $type ); 00618 if ( $queue->isEmpty() ) { 00619 unset( $types[$i] ); // don't keep checking this queue 00620 continue; 00621 } 00622 $job = $queue->pop(); 00623 if ( $job ) { 00624 $jobFound = true; 00625 $output = $job->toString() . "\n"; 00626 $t = - microtime( true ); 00627 $success = $job->run(); 00628 $queue->ack( $job ); // done 00629 $t += microtime( true ); 00630 $t = round( $t * 1000 ); 00631 if ( !$success ) { 00632 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n"; 00633 } else { 00634 $output .= "Success, Time: $t ms\n"; 00635 } 00636 wfDebugLog( 'jobqueue', $output ); 00637 break; 00638 } else { 00639 unset( $types[$i] ); // don't keep checking this queue 00640 } 00641 } 00642 } while ( --$n && $jobFound ); 00643 } 00644 }