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