MediaWiki  REL1_21
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         }
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                 if ( $this->context->canUseWikiPage() ) {
00335                         // Try to use request context wiki page, as there
00336                         // is already data from db saved in per process
00337                         // cache there from this->getAction() call.
00338                         $page = $this->context->getWikiPage();
00339                         $article = Article::newFromWikiPage( $page, $this->context );
00340                 } else {
00341                         // This case should not happen, but just in case.
00342                         $article = Article::newFromTitle( $title, $this->context );
00343                         $this->context->setWikiPage( $article->getPage() );
00344                 }
00345 
00346                 // NS_MEDIAWIKI has no redirects.
00347                 // It is also used for CSS/JS, so performance matters here...
00348                 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
00349                         wfProfileOut( __METHOD__ );
00350                         return $article;
00351                 }
00352 
00353                 $request = $this->context->getRequest();
00354 
00355                 // Namespace might change when using redirects
00356                 // Check for redirects ...
00357                 $action = $request->getVal( 'action', 'view' );
00358                 $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
00359                 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
00360                         && !$request->getVal( 'oldid' ) && // ... and are not old revisions
00361                         !$request->getVal( 'diff' ) && // ... and not when showing diff
00362                         $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
00363                         // ... and the article is not a non-redirect image page with associated file
00364                         !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
00365                 {
00366                         // Give extensions a change to ignore/handle redirects as needed
00367                         $ignoreRedirect = $target = false;
00368 
00369                         wfRunHooks( 'InitializeArticleMaybeRedirect',
00370                                 array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
00371 
00372                         // Follow redirects only for... redirects.
00373                         // If $target is set, then a hook wanted to redirect.
00374                         if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) {
00375                                 // Is the target already set by an extension?
00376                                 $target = $target ? $target : $article->followRedirect();
00377                                 if ( is_string( $target ) ) {
00378                                         if ( !$wgDisableHardRedirects ) {
00379                                                 // we'll need to redirect
00380                                                 wfProfileOut( __METHOD__ );
00381                                                 return $target;
00382                                         }
00383                                 }
00384                                 if ( is_object( $target ) ) {
00385                                         // Rewrite environment to redirected article
00386                                         $rarticle = Article::newFromTitle( $target, $this->context );
00387                                         $rarticle->loadPageData();
00388                                         if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
00389                                                 $rarticle->setRedirectedFrom( $title );
00390                                                 $article = $rarticle;
00391                                                 $this->context->setTitle( $target );
00392                                                 $this->context->setWikiPage( $article->getPage() );
00393                                         }
00394                                 }
00395                         } else {
00396                                 $this->context->setTitle( $article->getTitle() );
00397                                 $this->context->setWikiPage( $article->getPage() );
00398                         }
00399                 }
00400 
00401                 wfProfileOut( __METHOD__ );
00402                 return $article;
00403         }
00404 
00411         private function performAction( Page $page, Title $requestTitle ) {
00412                 global $wgUseSquid, $wgSquidMaxage;
00413 
00414                 wfProfileIn( __METHOD__ );
00415 
00416                 $request = $this->context->getRequest();
00417                 $output = $this->context->getOutput();
00418                 $title = $this->context->getTitle();
00419                 $user = $this->context->getUser();
00420 
00421                 if ( !wfRunHooks( 'MediaWikiPerformAction',
00422                         array( $output, $page, $title, $user, $request, $this ) ) )
00423                 {
00424                         wfProfileOut( __METHOD__ );
00425                         return;
00426                 }
00427 
00428                 $act = $this->getAction();
00429 
00430                 $action = Action::factory( $act, $page );
00431                 if ( $action instanceof Action ) {
00432                         # Let Squid cache things if we can purge them.
00433                         if ( $wgUseSquid &&
00434                                 in_array( $request->getFullRequestURL(), $requestTitle->getSquidURLs() )
00435                         ) {
00436                                 $output->setSquidMaxage( $wgSquidMaxage );
00437                         }
00438 
00439                         $action->show();
00440                         wfProfileOut( __METHOD__ );
00441                         return;
00442                 }
00443 
00444                 if ( wfRunHooks( 'UnknownAction', array( $request->getVal( 'action', 'view' ), $page ) ) ) {
00445                         $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
00446                 }
00447 
00448                 wfProfileOut( __METHOD__ );
00449         }
00450 
00455         public function run() {
00456                 try {
00457                         $this->checkMaxLag();
00458                         $this->main();
00459                         $this->restInPeace();
00460                 } catch ( Exception $e ) {
00461                         MWExceptionHandler::handle( $e );
00462                 }
00463         }
00464 
00470         private function checkMaxLag() {
00471                 global $wgShowHostnames;
00472 
00473                 wfProfileIn( __METHOD__ );
00474                 $maxLag = $this->context->getRequest()->getVal( 'maxlag' );
00475                 if ( !is_null( $maxLag ) ) {
00476                         list( $host, $lag ) = wfGetLB()->getMaxLag();
00477                         if ( $lag > $maxLag ) {
00478                                 $resp = $this->context->getRequest()->response();
00479                                 $resp->header( 'HTTP/1.1 503 Service Unavailable' );
00480                                 $resp->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
00481                                 $resp->header( 'X-Database-Lag: ' . intval( $lag ) );
00482                                 $resp->header( 'Content-Type: text/plain' );
00483                                 if( $wgShowHostnames ) {
00484                                         echo "Waiting for $host: $lag seconds lagged\n";
00485                                 } else {
00486                                         echo "Waiting for a database server: $lag seconds lagged\n";
00487                                 }
00488 
00489                                 wfProfileOut( __METHOD__ );
00490 
00491                                 exit;
00492                         }
00493                 }
00494                 wfProfileOut( __METHOD__ );
00495                 return true;
00496         }
00497 
00498         private function main() {
00499                 global $wgUseFileCache, $wgTitle, $wgUseAjax;
00500 
00501                 wfProfileIn( __METHOD__ );
00502 
00503                 $request = $this->context->getRequest();
00504 
00505                 if ( $request->getCookie( 'forceHTTPS' )
00506                         && $request->detectProtocol() == 'http'
00507                         && $request->getMethod() == 'GET'
00508                 ) {
00509                         $redirUrl = $request->getFullRequestURL();
00510                         $redirUrl = str_replace( 'http://', 'https://', $redirUrl );
00511 
00512                         // Setup dummy Title, otherwise OutputPage::redirect will fail
00513                         $title = Title::newFromText( NS_MAIN, 'REDIR' );
00514                         $this->context->setTitle( $title );
00515                         $output = $this->context->getOutput();
00516                         $output->redirect( $redirUrl );
00517                         $output->output();
00518                         wfProfileOut( __METHOD__ );
00519                         return;
00520                 }
00521 
00522                 // Send Ajax requests to the Ajax dispatcher.
00523                 if ( $wgUseAjax && $request->getVal( 'action', 'view' ) == 'ajax' ) {
00524 
00525                         // Set a dummy title, because $wgTitle == null might break things
00526                         $title = Title::makeTitle( NS_MAIN, 'AJAX' );
00527                         $this->context->setTitle( $title );
00528                         $wgTitle = $title;
00529 
00530                         $dispatcher = new AjaxDispatcher();
00531                         $dispatcher->performAction();
00532                         wfProfileOut( __METHOD__ );
00533                         return;
00534                 }
00535 
00536                 // Get title from request parameters,
00537                 // is set on the fly by parseTitle the first time.
00538                 $title = $this->getTitle();
00539                 $action = $this->getAction();
00540                 $wgTitle = $title;
00541 
00542                 if ( $wgUseFileCache && $title->getNamespace() >= 0 ) {
00543                         wfProfileIn( 'main-try-filecache' );
00544                         if ( HTMLFileCache::useFileCache( $this->context ) ) {
00545                                 // Try low-level file cache hit
00546                                 $cache = HTMLFileCache::newFromTitle( $title, $action );
00547                                 if ( $cache->isCacheGood( /* Assume up to date */ ) ) {
00548                                         // Check incoming headers to see if client has this cached
00549                                         $timestamp = $cache->cacheTimestamp();
00550                                         if ( !$this->context->getOutput()->checkLastModified( $timestamp ) ) {
00551                                                 $cache->loadFromFileCache( $this->context );
00552                                         }
00553                                         // Do any stats increment/watchlist stuff
00554                                         $this->context->getWikiPage()->doViewUpdates( $this->context->getUser() );
00555                                         // Tell OutputPage that output is taken care of
00556                                         $this->context->getOutput()->disable();
00557                                         wfProfileOut( 'main-try-filecache' );
00558                                         wfProfileOut( __METHOD__ );
00559                                         return;
00560                                 }
00561                         }
00562                         wfProfileOut( 'main-try-filecache' );
00563                 }
00564 
00565                 $this->performRequest();
00566 
00567                 // Now commit any transactions, so that unreported errors after
00568                 // output() don't roll back the whole DB transaction
00569                 wfGetLBFactory()->commitMasterChanges();
00570 
00571                 // Output everything!
00572                 $this->context->getOutput()->output();
00573 
00574                 wfProfileOut( __METHOD__ );
00575         }
00576 
00580         public function restInPeace() {
00581                 // Do any deferred jobs
00582                 DeferredUpdates::doUpdates( 'commit' );
00583 
00584                 // Execute a job from the queue
00585                 $this->doJobs();
00586 
00587                 // Log profiling data, e.g. in the database or UDP
00588                 wfLogProfilingData();
00589 
00590                 // Commit and close up!
00591                 $factory = wfGetLBFactory();
00592                 $factory->commitMasterChanges();
00593                 $factory->shutdown();
00594 
00595                 wfDebug( "Request ended normally\n" );
00596         }
00597 
00601         private function doJobs() {
00602                 global $wgJobRunRate;
00603 
00604                 if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
00605                         return;
00606                 }
00607 
00608                 if ( $wgJobRunRate < 1 ) {
00609                         $max = mt_getrandmax();
00610                         if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
00611                                 return; // the higher $wgJobRunRate, the less likely we return here
00612                         }
00613                         $n = 1;
00614                 } else {
00615                         $n = intval( $wgJobRunRate );
00616                 }
00617 
00618                 $group = JobQueueGroup::singleton();
00619                 do {
00620                         $job = $group->pop( JobQueueGroup::USE_CACHE ); // job from any queue
00621                         if ( $job ) {
00622                                 $output = $job->toString() . "\n";
00623                                 $t = - microtime( true );
00624                                 $success = $job->run();
00625                                 $group->ack( $job ); // done
00626                                 $t += microtime( true );
00627                                 $t = round( $t * 1000 );
00628                                 if ( !$success ) {
00629                                         $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
00630                                 } else {
00631                                         $output .= "Success, Time: $t ms\n";
00632                                 }
00633                                 wfDebugLog( 'jobqueue', $output );
00634                         }
00635                 } while ( --$n && $job );
00636         }
00637 }