00001 <?php
00005 class MediaWiki {
00006
00007 var $GET;
00008 var $params = array();
00009
00011 function __construct() {
00012 $this->GET = $_GET;
00013 }
00014
00022 function setVal( $key, &$value ) {
00023 $key = strtolower( $key );
00024 $this->params[$key] =& $value;
00025 }
00026
00034 function getVal( $key, $default = '' ) {
00035 $key = strtolower( $key );
00036 if( isset( $this->params[$key] ) ) {
00037 return $this->params[$key];
00038 }
00039 return $default;
00040 }
00041
00052 function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) {
00053 wfProfileIn( __METHOD__ );
00054
00055 $output->setTitle( $title );
00056
00057 wfRunHooks( 'BeforeInitialize', array( &$title, &$article, &$output, &$user, $request, $this ) );
00058
00059 if( !$this->preliminaryChecks( $title, $output, $request ) ) {
00060 wfProfileOut( __METHOD__ );
00061 return;
00062 }
00063
00064 if( !$this->handleSpecialCases( $title, $output, $request ) ) {
00065
00066
00067 $new_article = $this->initializeArticle( $title, $output, $request );
00068 if( is_object( $new_article ) ) {
00069 $article = $new_article;
00070 $this->performAction( $output, $article, $title, $user, $request );
00071 } elseif( is_string( $new_article ) ) {
00072 $output->redirect( $new_article );
00073 } else {
00074 wfProfileOut( __METHOD__ );
00075 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
00076 }
00077 }
00078 wfProfileOut( __METHOD__ );
00079 }
00080
00089 function checkMaxLag( $maxLag ) {
00090 list( $host, $lag ) = wfGetLB()->getMaxLag();
00091 if( $lag > $maxLag ) {
00092 wfMaxlagError( $host, $lag, $maxLag );
00093 return false;
00094 } else {
00095 return true;
00096 }
00097 }
00098
00107 function checkInitialQueries( $title, $action ) {
00108 global $wgOut, $wgRequest, $wgContLang;
00109 if( $wgRequest->getVal( 'printable' ) === 'yes' ) {
00110 $wgOut->setPrintable();
00111 }
00112 $ret = null;
00113 if( $curid = $wgRequest->getInt( 'curid' ) ) {
00114 # URLs like this are generated by RC, because rc_title isn't always accurate
00115 $ret = Title::newFromID( $curid );
00116 } elseif( $title == '' && $action != 'delete' ) {
00117 $ret = Title::newMainPage();
00118 } else {
00119 $ret = Title::newFromURL( $title );
00120
00121
00122 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
00123 $wgContLang->findVariantLink( $title, $ret );
00124 }
00125 # For non-special titles, check for implicit titles
00126 if( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
00127
00128 $oldid = $wgRequest->getInt( 'oldid' );
00129 $oldid = $oldid ? $oldid : $wgRequest->getInt( 'diff' );
00130
00131 if( $oldid ) {
00132 $rev = Revision::newFromId( $oldid );
00133 $ret = $rev ? $rev->getTitle() : $ret;
00134 }
00135 }
00136 return $ret;
00137 }
00138
00146 function preliminaryChecks( &$title, &$output, $request ) {
00147 if( $request->getCheck( 'search' ) ) {
00148
00149
00150
00151
00152
00153 $title = SpecialPage::getTitleFor( 'Search' );
00154 }
00155 # If the user is not logged in, the Namespace:title of the article must be in
00156 # the Read array in order for the user to see it. (We have to check here to
00157 # catch special pages etc. We check again in Article::view())
00158 if( !is_null( $title ) && !$title->userCanRead() ) {
00159 global $wgDeferredUpdateList;
00160 $output->loginToUse();
00161 $this->finalCleanup( $wgDeferredUpdateList, $output );
00162 $output->disable();
00163 return false;
00164 }
00165 return true;
00166 }
00167
00180 function handleSpecialCases( &$title, &$output, $request ) {
00181 wfProfileIn( __METHOD__ );
00182 global $wgContLang, $wgUser;
00183 $action = $this->getVal( 'Action' );
00184 $perferred = $wgContLang->getPreferredVariant( false );
00185
00186
00187 if( is_null($title) || ( ($title->getDBkey() == '') && ($title->getInterwiki() == '') ) ) {
00188 $title = SpecialPage::getTitleFor( 'Badtitle' );
00189 # Die now before we mess up $wgArticle and the skin stops working
00190 throw new ErrorPageError( 'badtitle', 'badtitletext' );
00191
00192
00193 } else if( $title->getInterwiki() != '' ) {
00194 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
00195 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
00196 } else {
00197 $query = $request->getValues();
00198 unset( $query['title'] );
00199 $url = $title->getFullURL( $query );
00200 }
00201
00202 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
00203 $output->redirect( $url );
00204 } else {
00205 $title = SpecialPage::getTitleFor( 'Badtitle' );
00206 wfProfileOut( __METHOD__ );
00207 throw new ErrorPageError( 'badtitle', 'badtitletext' );
00208 }
00209
00210 } else if( $action == 'view' && !$request->wasPosted() &&
00211 ( ( !isset($this->GET['title']) || $title->getPrefixedDBKey() != $this->GET['title'] ) ||
00212
00213
00214 ( !isset($this->GET['variant']) && $wgContLang->hasVariants() && !$wgUser->isLoggedIn() ) ) &&
00215 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
00216 {
00217 if( !$wgUser->isLoggedIn() ) {
00218 $pref = $wgContLang->getPreferredVariant( false, $fromHeader = true );
00219 $targetUrl = $title->getFullURL( '', $variant = $pref );
00220 }
00221 else
00222 $targetUrl = $title->getFullURL();
00223
00224 if( $targetUrl == $request->getFullRequestURL() ) {
00225 $message = "Redirect loop detected!\n\n" .
00226 "This means the wiki got confused about what page was " .
00227 "requested; this sometimes happens when moving a wiki " .
00228 "to a new server or changing the server configuration.\n\n";
00229
00230 if( $this->getVal( 'UsePathInfo' ) ) {
00231 $message .= "The wiki is trying to interpret the page " .
00232 "title from the URL path portion (PATH_INFO), which " .
00233 "sometimes fails depending on the web server. Try " .
00234 "setting \"\$wgUsePathInfo = false;\" in your " .
00235 "LocalSettings.php, or check that \$wgArticlePath " .
00236 "is correct.";
00237 } else {
00238 $message .= "Your web server was detected as possibly not " .
00239 "supporting URL path components (PATH_INFO) correctly; " .
00240 "check your LocalSettings.php for a customized " .
00241 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
00242 "to true.";
00243 }
00244 wfHttpError( 500, "Internal error", $message );
00245 wfProfileOut( __METHOD__ );
00246 return false;
00247 } else {
00248 $output->setSquidMaxage( 1200 );
00249 $output->redirect( $targetUrl, '301' );
00250 }
00251
00252 } else if( NS_SPECIAL == $title->getNamespace() ) {
00253
00254 SpecialPage::executePath( $title );
00255 } else {
00256
00257 wfProfileOut( __METHOD__ );
00258 return false;
00259 }
00260
00261 wfProfileOut( __METHOD__ );
00262 return true;
00263 }
00264
00271 static function articleFromTitle( &$title ) {
00272 if( NS_MEDIA == $title->getNamespace() ) {
00273
00274 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
00275 }
00276
00277 $article = null;
00278 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
00279 if( $article ) {
00280 return $article;
00281 }
00282
00283 switch( $title->getNamespace() ) {
00284 case NS_FILE:
00285 return new ImagePage( $title );
00286 case NS_CATEGORY:
00287 return new CategoryPage( $title );
00288 default:
00289 return new Article( $title );
00290 }
00291 }
00292
00302 function initializeArticle( &$title, &$output, $request ) {
00303 wfProfileIn( __METHOD__ );
00304
00305 $action = $this->getVal( 'action', 'view' );
00306 $article = self::articleFromTitle( $title );
00307 # NS_MEDIAWIKI has no redirects.
00308 # It is also used for CSS/JS, so performance matters here...
00309 if( $title->getNamespace() == NS_MEDIAWIKI ) {
00310 wfProfileOut( __METHOD__ );
00311 return $article;
00312 }
00313
00314
00315 $file = ($title->getNamespace() == NS_FILE) ? $article->getFile() : null;
00316 if( ( $action == 'view' || $action == 'render' )
00317 && !$request->getVal( 'oldid' ) &&
00318 $request->getVal( 'redirect' ) != 'no' &&
00319
00320 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
00321 {
00322 # Give extensions a change to ignore/handle redirects as needed
00323 $ignoreRedirect = $target = false;
00324
00325 $dbr = wfGetDB( DB_SLAVE );
00326 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
00327
00328 wfRunHooks( 'InitializeArticleMaybeRedirect',
00329 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
00330
00331
00332
00333 if( !$ignoreRedirect && ($target || $article->isRedirect()) ) {
00334 # Is the target already set by an extension?
00335 $target = $target ? $target : $article->followRedirect();
00336 if( is_string( $target ) ) {
00337 if( !$this->getVal( 'DisableHardRedirects' ) ) {
00338
00339 wfProfileOut( __METHOD__ );
00340 return $target;
00341 }
00342 }
00343 if( is_object($target) ) {
00344
00345 $rarticle = self::articleFromTitle( $target );
00346 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
00347 if( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
00348 $rarticle->setRedirectedFrom( $title );
00349 $article = $rarticle;
00350 $title = $target;
00351 $output->setTitle( $title );
00352 }
00353 }
00354 } else {
00355 $title = $article->getTitle();
00356 }
00357 }
00358 wfProfileOut( __METHOD__ );
00359 return $article;
00360 }
00361
00369 function finalCleanup( &$deferredUpdates, &$output ) {
00370 wfProfileIn( __METHOD__ );
00371 # Now commit any transactions, so that unreported errors after
00372 # output() don't roll back the whole DB transaction
00373 $factory = wfGetLBFactory();
00374 $factory->commitMasterChanges();
00375 # Output everything!
00376 $output->output();
00377 # Do any deferred jobs
00378 $this->doUpdates( $deferredUpdates );
00379 $this->doJobs();
00380 wfProfileOut( __METHOD__ );
00381 }
00382
00391 function doUpdates( &$updates ) {
00392 wfProfileIn( __METHOD__ );
00393
00394 if (!$updates) {
00395 wfProfileOut( __METHOD__ );
00396 return;
00397 }
00398
00399 $dbw = wfGetDB( DB_MASTER );
00400 foreach( $updates as $up ) {
00401 $up->doUpdate();
00402
00403 # Commit after every update to prevent lock contention
00404 if( $dbw->trxLevel() ) {
00405 $dbw->commit();
00406 }
00407 }
00408 wfProfileOut( __METHOD__ );
00409 }
00410
00414 function doJobs() {
00415 $jobRunRate = $this->getVal( 'JobRunRate' );
00416
00417 if( $jobRunRate <= 0 || wfReadOnly() ) {
00418 return;
00419 }
00420 if( $jobRunRate < 1 ) {
00421 $max = mt_getrandmax();
00422 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
00423 return;
00424 }
00425 $n = 1;
00426 } else {
00427 $n = intval( $jobRunRate );
00428 }
00429
00430 while ( $n-- && false != ( $job = Job::pop() ) ) {
00431 $output = $job->toString() . "\n";
00432 $t = -wfTime();
00433 $success = $job->run();
00434 $t += wfTime();
00435 $t = round( $t*1000 );
00436 if( !$success ) {
00437 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
00438 } else {
00439 $output .= "Success, Time: $t ms\n";
00440 }
00441 wfDebugLog( 'jobqueue', $output );
00442 }
00443 }
00444
00448 function restInPeace() {
00449 wfLogProfilingData();
00450 # Commit and close up!
00451 $factory = wfGetLBFactory();
00452 $factory->commitMasterChanges();
00453 $factory->shutdown();
00454 wfDebug( "Request ended normally\n" );
00455 }
00456
00466 function performAction( &$output, &$article, &$title, &$user, &$request ) {
00467 wfProfileIn( __METHOD__ );
00468
00469 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
00470 wfProfileOut( __METHOD__ );
00471 return;
00472 }
00473
00474 $action = $this->getVal( 'Action' );
00475 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
00476
00477 $action = 'nosuchaction';
00478 }
00479
00480 # Workaround for bug #20966: inability of IE to provide an action dependent
00481 # on which submit button is clicked.
00482 if ( $action === 'historysubmit' ) {
00483 if ( $request->getBool( 'revisiondelete' ) ) {
00484 $action = 'revisiondelete';
00485 } else {
00486 $action = 'view';
00487 }
00488 }
00489
00490 switch( $action ) {
00491 case 'view':
00492 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
00493 $article->view();
00494 break;
00495 case 'raw':
00496 wfProfileIn( __METHOD__.'-raw' );
00497 $raw = new RawPage( $article );
00498 $raw->view();
00499 wfProfileOut( __METHOD__.'-raw' );
00500 break;
00501 case 'watch':
00502 case 'unwatch':
00503 case 'delete':
00504 case 'revert':
00505 case 'rollback':
00506 case 'protect':
00507 case 'unprotect':
00508 case 'info':
00509 case 'markpatrolled':
00510 case 'render':
00511 case 'deletetrackback':
00512 case 'purge':
00513 $article->$action();
00514 break;
00515 case 'print':
00516 $article->view();
00517 break;
00518 case 'dublincore':
00519 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
00520 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
00521 } else {
00522 $rdf = new DublinCoreRdf( $article );
00523 $rdf->show();
00524 }
00525 break;
00526 case 'creativecommons':
00527 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
00528 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
00529 } else {
00530 $rdf = new CreativeCommonsRdf( $article );
00531 $rdf->show();
00532 }
00533 break;
00534 case 'credits':
00535 Credits::showPage( $article );
00536 break;
00537 case 'submit':
00538 if( session_id() == '' ) {
00539
00540 wfSetupSession();
00541 }
00542
00543 case 'edit':
00544 case 'editredlink':
00545 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
00546 $internal = $request->getVal( 'internaledit' );
00547 $external = $request->getVal( 'externaledit' );
00548 $section = $request->getVal( 'section' );
00549 $oldid = $request->getVal( 'oldid' );
00550 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
00551 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
00552 $editor = new EditPage( $article );
00553 $editor->submit();
00554 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
00555 $mode = $request->getVal( 'mode' );
00556 $extedit = new ExternalEdit( $article, $mode );
00557 $extedit->edit();
00558 }
00559 }
00560 break;
00561 case 'history':
00562 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
00563 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
00564 }
00565 $history = new HistoryPage( $article );
00566 $history->history();
00567 break;
00568 case 'revisiondelete':
00569 # For show/hide submission from history page
00570 $special = SpecialPage::getPage( 'Revisiondelete' );
00571 $special->execute( '' );
00572 break;
00573 default:
00574 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
00575 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
00576 }
00577 }
00578 wfProfileOut( __METHOD__ );
00579
00580 }
00581
00582 };