MediaWiki  REL1_20
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 
00174         private function performRequest() {
00175                 global $wgServer, $wgUsePathInfo, $wgTitle;
00176 
00177                 wfProfileIn( __METHOD__ );
00178 
00179                 $request = $this->context->getRequest();
00180                 $title = $this->context->getTitle();
00181                 $output = $this->context->getOutput();
00182                 $user = $this->context->getUser();
00183 
00184                 if ( $request->getVal( 'printable' ) === 'yes' ) {
00185                         $output->setPrintable();
00186                 }
00187 
00188                 $unused = null; // To pass it by reference
00189                 wfRunHooks( 'BeforeInitialize', array( &$title, &$unused, &$output, &$user, $request, $this ) );
00190 
00191                 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
00192                 if ( is_null( $title ) || ( $title->getDBkey() == '' && $title->getInterwiki() == '' ) ||
00193                         $title->isSpecial( 'Badtitle' ) )
00194                 {
00195                         $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
00196                         wfProfileOut( __METHOD__ );
00197                         throw new BadTitleError();
00198                 }
00199 
00200                 // Check user's permissions to read this page.
00201                 // We have to check here to catch special pages etc.
00202                 // We will check again in Article::view().
00203                 $permErrors = $title->getUserPermissionsErrors( 'read', $user );
00204                 if ( count( $permErrors ) ) {
00205                         // Bug 32276: allowing the skin to generate output with $wgTitle or
00206                         // $this->context->title set to the input title would allow anonymous users to
00207                         // determine whether a page exists, potentially leaking private data. In fact, the
00208                         // curid and oldid request  parameters would allow page titles to be enumerated even
00209                         // when they are not guessable. So we reset the title to Special:Badtitle before the
00210                         // permissions error is displayed.
00211                         //
00212                         // The skin mostly uses $this->context->getTitle() these days, but some extensions
00213                         // still use $wgTitle.
00214 
00215                         $badTitle = SpecialPage::getTitleFor( 'Badtitle' );
00216                         $this->context->setTitle( $badTitle );
00217                         $wgTitle = $badTitle;
00218 
00219                         wfProfileOut( __METHOD__ );
00220                         throw new PermissionsError( 'read', $permErrors );
00221                 }
00222 
00223                 $pageView = false; // was an article or special page viewed?
00224 
00225                 // Interwiki redirects
00226                 if ( $title->getInterwiki() != '' ) {
00227                         $rdfrom = $request->getVal( 'rdfrom' );
00228                         if ( $rdfrom ) {
00229                                 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
00230                         } else {
00231                                 $query = $request->getValues();
00232                                 unset( $query['title'] );
00233                                 $url = $title->getFullURL( $query );
00234                         }
00235                         // Check for a redirect loop
00236                         if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url )
00237                                 && $title->isLocal() )
00238                         {
00239                                 // 301 so google et al report the target as the actual url.
00240                                 $output->redirect( $url, 301 );
00241                         } else {
00242                                 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
00243                                 wfProfileOut( __METHOD__ );
00244                                 throw new BadTitleError();
00245                         }
00246                 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
00247                 } elseif ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted()
00248                         && ( $request->getVal( 'title' ) === null ||
00249                                 $title->getPrefixedDBKey() != $request->getVal( 'title' ) )
00250                         && !count( $request->getValueNames( array( 'action', 'title' ) ) )
00251                         && wfRunHooks( 'TestCanonicalRedirect', array( $request, $title, $output ) ) )
00252                 {
00253                         if ( $title->isSpecialPage() ) {
00254                                 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
00255                                 if ( $name ) {
00256                                         $title = SpecialPage::getTitleFor( $name, $subpage );
00257                                 }
00258                         }
00259                         $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00260                         // Redirect to canonical url, make it a 301 to allow caching
00261                         if ( $targetUrl == $request->getFullRequestURL() ) {
00262                                 $message = "Redirect loop detected!\n\n" .
00263                                         "This means the wiki got confused about what page was " .
00264                                         "requested; this sometimes happens when moving a wiki " .
00265                                         "to a new server or changing the server configuration.\n\n";
00266 
00267                                 if ( $wgUsePathInfo ) {
00268                                         $message .= "The wiki is trying to interpret the page " .
00269                                                 "title from the URL path portion (PATH_INFO), which " .
00270                                                 "sometimes fails depending on the web server. Try " .
00271                                                 "setting \"\$wgUsePathInfo = false;\" in your " .
00272                                                 "LocalSettings.php, or check that \$wgArticlePath " .
00273                                                 "is correct.";
00274                                 } else {
00275                                         $message .= "Your web server was detected as possibly not " .
00276                                                 "supporting URL path components (PATH_INFO) correctly; " .
00277                                                 "check your LocalSettings.php for a customized " .
00278                                                 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
00279                                                 "to true.";
00280                                 }
00281                                 throw new HttpError( 500, $message );
00282                         } else {
00283                                 $output->setSquidMaxage( 1200 );
00284                                 $output->redirect( $targetUrl, '301' );
00285                         }
00286                 // Special pages
00287                 } elseif ( NS_SPECIAL == $title->getNamespace() ) {
00288                         $pageView = true;
00289                         // Actions that need to be made when we have a special pages
00290                         SpecialPageFactory::executePath( $title, $this->context );
00291                 } else {
00292                         // ...otherwise treat it as an article view. The article
00293                         // may be a redirect to another article or URL.
00294                         $article = $this->initializeArticle();
00295                         if ( is_object( $article ) ) {
00296                                 $pageView = true;
00301                                 global $wgArticle;
00302                                 $wgArticle = new DeprecatedGlobal( 'wgArticle', $article, '1.18' );
00303 
00304                                 $this->performAction( $article );
00305                         } elseif ( is_string( $article ) ) {
00306                                 $output->redirect( $article );
00307                         } else {
00308                                 wfProfileOut( __METHOD__ );
00309                                 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
00310                         }
00311                 }
00312 
00313                 if ( $pageView ) {
00314                         // Promote user to any groups they meet the criteria for
00315                         $user->addAutopromoteOnceGroups( 'onView' );
00316                 }
00317 
00318                 wfProfileOut( __METHOD__ );
00319         }
00320 
00327         private function initializeArticle() {
00328                 global $wgDisableHardRedirects;
00329 
00330                 wfProfileIn( __METHOD__ );
00331 
00332                 $title = $this->context->getTitle();
00333                 $article = Article::newFromTitle( $title, $this->context );
00334                 $this->context->setWikiPage( $article->getPage() );
00335                 // NS_MEDIAWIKI has no redirects.
00336                 // It is also used for CSS/JS, so performance matters here...
00337                 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
00338                         wfProfileOut( __METHOD__ );
00339                         return $article;
00340                 }
00341 
00342                 $request = $this->context->getRequest();
00343 
00344                 // Namespace might change when using redirects
00345                 // Check for redirects ...
00346                 $action = $request->getVal( 'action', 'view' );
00347                 $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
00348                 if ( ( $action == 'view' || $action == 'render' )       // ... for actions that show content
00349                         && !$request->getVal( 'oldid' ) &&    // ... and are not old revisions
00350                         !$request->getVal( 'diff' ) &&    // ... and not when showing diff
00351                         $request->getVal( 'redirect' ) != 'no' &&       // ... unless explicitly told not to
00352                         // ... and the article is not a non-redirect image page with associated file
00353                         !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
00354                 {
00355                         // Give extensions a change to ignore/handle redirects as needed
00356                         $ignoreRedirect = $target = false;
00357 
00358                         wfRunHooks( 'InitializeArticleMaybeRedirect',
00359                                 array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
00360 
00361                         // Follow redirects only for... redirects.
00362                         // If $target is set, then a hook wanted to redirect.
00363                         if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) {
00364                                 // Is the target already set by an extension?
00365                                 $target = $target ? $target : $article->followRedirect();
00366                                 if ( is_string( $target ) ) {
00367                                         if ( !$wgDisableHardRedirects ) {
00368                                                 // we'll need to redirect
00369                                                 wfProfileOut( __METHOD__ );
00370                                                 return $target;
00371                                         }
00372                                 }
00373                                 if ( is_object( $target ) ) {
00374                                         // Rewrite environment to redirected article
00375                                         $rarticle = Article::newFromTitle( $target, $this->context );
00376                                         $rarticle->loadPageData();
00377                                         if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
00378                                                 $rarticle->setRedirectedFrom( $title );
00379                                                 $article = $rarticle;
00380                                                 $this->context->setTitle( $target );
00381                                                 $this->context->setWikiPage( $article->getPage() );
00382                                         }
00383                                 }
00384                         } else {
00385                                 $this->context->setTitle( $article->getTitle() );
00386                                 $this->context->setWikiPage( $article->getPage() );
00387                         }
00388                 }
00389 
00390                 wfProfileOut( __METHOD__ );
00391                 return $article;
00392         }
00393 
00399         private function performAction( Page $page ) {
00400                 global $wgUseSquid, $wgSquidMaxage;
00401 
00402                 wfProfileIn( __METHOD__ );
00403 
00404                 $request = $this->context->getRequest();
00405                 $output = $this->context->getOutput();
00406                 $title = $this->context->getTitle();
00407                 $user = $this->context->getUser();
00408 
00409                 if ( !wfRunHooks( 'MediaWikiPerformAction',
00410                         array( $output, $page, $title, $user, $request, $this ) ) )
00411                 {
00412                         wfProfileOut( __METHOD__ );
00413                         return;
00414                 }
00415 
00416                 $act = $this->getAction();
00417 
00418                 $action = Action::factory( $act, $page );
00419                 if ( $action instanceof Action ) {
00420                         # Let Squid cache things if we can purge them.
00421                         if ( $wgUseSquid &&
00422                                 in_array( $request->getFullRequestURL(), $title->getSquidURLs() )
00423                         ) {
00424                                 $output->setSquidMaxage( $wgSquidMaxage );
00425                         }
00426 
00427                         $action->show();
00428                         wfProfileOut( __METHOD__ );
00429                         return;
00430                 }
00431 
00432                 if ( wfRunHooks( 'UnknownAction', array( $request->getVal( 'action', 'view' ), $page ) ) ) {
00433                         $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
00434                 }
00435 
00436                 wfProfileOut( __METHOD__ );
00437         }
00438 
00443         public function run() {
00444                 try {
00445                         $this->checkMaxLag();
00446                         $this->main();
00447                         $this->restInPeace();
00448                 } catch ( Exception $e ) {
00449                         MWExceptionHandler::handle( $e );
00450                 }
00451         }
00452 
00458         private function checkMaxLag() {
00459                 global $wgShowHostnames;
00460 
00461                 wfProfileIn( __METHOD__ );
00462                 $maxLag = $this->context->getRequest()->getVal( 'maxlag' );
00463                 if ( !is_null( $maxLag ) ) {
00464                         list( $host, $lag ) = wfGetLB()->getMaxLag();
00465                         if ( $lag > $maxLag ) {
00466                                 $resp = $this->context->getRequest()->response();
00467                                 $resp->header( 'HTTP/1.1 503 Service Unavailable' );
00468                                 $resp->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
00469                                 $resp->header( 'X-Database-Lag: ' . intval( $lag ) );
00470                                 $resp->header( 'Content-Type: text/plain' );
00471                                 if( $wgShowHostnames ) {
00472                                         echo "Waiting for $host: $lag seconds lagged\n";
00473                                 } else {
00474                                         echo "Waiting for a database server: $lag seconds lagged\n";
00475                                 }
00476 
00477                                 wfProfileOut( __METHOD__ );
00478 
00479                                 exit;
00480                         }
00481                 }
00482                 wfProfileOut( __METHOD__ );
00483                 return true;
00484         }
00485 
00486         private function main() {
00487                 global $wgUseFileCache, $wgTitle, $wgUseAjax;
00488 
00489                 wfProfileIn( __METHOD__ );
00490 
00491                 $request = $this->context->getRequest();
00492 
00493                 // Send Ajax requests to the Ajax dispatcher.
00494                 if ( $wgUseAjax && $request->getVal( 'action', 'view' ) == 'ajax' ) {
00495 
00496                         // Set a dummy title, because $wgTitle == null might break things
00497                         $title = Title::makeTitle( NS_MAIN, 'AJAX' );
00498                         $this->context->setTitle( $title );
00499                         $wgTitle = $title;
00500 
00501                         $dispatcher = new AjaxDispatcher();
00502                         $dispatcher->performAction();
00503                         wfProfileOut( __METHOD__ );
00504                         return;
00505                 }
00506 
00507                 // Get title from request parameters,
00508                 // is set on the fly by parseTitle the first time.
00509                 $title = $this->getTitle();
00510                 $action = $this->getAction();
00511                 $wgTitle = $title;
00512 
00513                 if ( $wgUseFileCache && $title->getNamespace() >= 0 ) {
00514                         wfProfileIn( 'main-try-filecache' );
00515                         if ( HTMLFileCache::useFileCache( $this->context ) ) {
00516                                 // Try low-level file cache hit
00517                                 $cache = HTMLFileCache::newFromTitle( $title, $action );
00518                                 if ( $cache->isCacheGood( /* Assume up to date */ ) ) {
00519                                         // Check incoming headers to see if client has this cached
00520                                         $timestamp = $cache->cacheTimestamp();
00521                                         if ( !$this->context->getOutput()->checkLastModified( $timestamp ) ) {
00522                                                 $cache->loadFromFileCache( $this->context );
00523                                         }
00524                                         // Do any stats increment/watchlist stuff
00525                                         $this->context->getWikiPage()->doViewUpdates( $this->context->getUser() );
00526                                         // Tell OutputPage that output is taken care of
00527                                         $this->context->getOutput()->disable();
00528                                         wfProfileOut( 'main-try-filecache' );
00529                                         wfProfileOut( __METHOD__ );
00530                                         return;
00531                                 }
00532                         }
00533                         wfProfileOut( 'main-try-filecache' );
00534                 }
00535 
00536                 $this->performRequest();
00537 
00538                 // Now commit any transactions, so that unreported errors after
00539                 // output() don't roll back the whole DB transaction
00540                 wfGetLBFactory()->commitMasterChanges();
00541 
00542                 // Output everything!
00543                 $this->context->getOutput()->output();
00544 
00545                 wfProfileOut( __METHOD__ );
00546         }
00547 
00551         public function restInPeace() {
00552                 // Do any deferred jobs
00553                 DeferredUpdates::doUpdates( 'commit' );
00554 
00555                 // Execute a job from the queue
00556                 $this->doJobs();
00557 
00558                 // Log message usage, if $wgAdaptiveMessageCache is set to true
00559                 MessageCache::logMessages();
00560 
00561                 // Log profiling data, e.g. in the database or UDP
00562                 wfLogProfilingData();
00563 
00564                 // Commit and close up!
00565                 $factory = wfGetLBFactory();
00566                 $factory->commitMasterChanges();
00567                 $factory->shutdown();
00568 
00569                 wfDebug( "Request ended normally\n" );
00570         }
00571 
00575         private function doJobs() {
00576                 global $wgJobRunRate;
00577 
00578                 if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
00579                         return;
00580                 }
00581                 if ( $wgJobRunRate < 1 ) {
00582                         $max = mt_getrandmax();
00583                         if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
00584                                 return;
00585                         }
00586                         $n = 1;
00587                 } else {
00588                         $n = intval( $wgJobRunRate );
00589                 }
00590 
00591                 while ( $n-- && false != ( $job = Job::pop() ) ) {
00592                         $output = $job->toString() . "\n";
00593                         $t = - microtime( true );
00594                         $success = $job->run();
00595                         $t += microtime( true );
00596                         $t = round( $t * 1000 );
00597                         if ( !$success ) {
00598                                 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
00599                         } else {
00600                                 $output .= "Success, Time: $t ms\n";
00601                         }
00602                         wfDebugLog( 'jobqueue', $output );
00603                 }
00604         }
00605 }