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
00053 function initialize( &$title, &$article, &$output, &$user, $request ) {
00054 wfProfileIn( __METHOD__ );
00055 if( !$this->preliminaryChecks( $title, $output, $request ) ) {
00056 wfProfileOut( __METHOD__ );
00057 return;
00058 }
00059 if( !$this->initializeSpecialCases( $title, $output, $request ) ) {
00060 $new_article = $this->initializeArticle( $title, $request );
00061 if( is_object( $new_article ) ) {
00062 $article = $new_article;
00063 $this->performAction( $output, $article, $title, $user, $request );
00064 } elseif( is_string( $new_article ) ) {
00065 $output->redirect( $new_article );
00066 } else {
00067 wfProfileOut( __METHOD__ );
00068 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
00069 }
00070 }
00071 wfProfileOut( __METHOD__ );
00072 }
00073
00082 function checkMaxLag( $maxLag ) {
00083 list( $host, $lag ) = wfGetLB()->getMaxLag();
00084 if( $lag > $maxLag ) {
00085 wfMaxlagError( $host, $lag, $maxLag );
00086 return false;
00087 } else {
00088 return true;
00089 }
00090 }
00091
00100 function checkInitialQueries( $title, $action ) {
00101 global $wgOut, $wgRequest, $wgContLang;
00102 if( $wgRequest->getVal( 'printable' ) === 'yes' ) {
00103 $wgOut->setPrintable();
00104 }
00105 $ret = NULL;
00106 if( $curid = $wgRequest->getInt( 'curid' ) ) {
00107 # URLs like this are generated by RC, because rc_title isn't always accurate
00108 $ret = Title::newFromID( $curid );
00109 } elseif( '' == $title && 'delete' != $action ) {
00110 $ret = Title::newMainPage();
00111 } else {
00112 $ret = Title::newFromURL( $title );
00113
00114
00115 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
00116 $wgContLang->findVariantLink( $title, $ret );
00117 }
00118 # For non-special titles, check for implicit titles
00119 if( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
00120
00121 $oldid = $wgRequest->getInt( 'oldid' );
00122 $oldid = $oldid ? $oldid : $wgRequest->getInt( 'diff' );
00123
00124 if( $oldid ) {
00125 $rev = Revision::newFromId( $oldid );
00126 $ret = $rev ? $rev->getTitle() : $ret;
00127 }
00128 }
00129 return $ret;
00130 }
00131
00139 function preliminaryChecks( &$title, &$output, $request ) {
00140 if( $request->getCheck( 'search' ) ) {
00141
00142
00143
00144
00145
00146 $title = SpecialPage::getTitleFor( 'Search' );
00147 }
00148 # If the user is not logged in, the Namespace:title of the article must be in
00149 # the Read array in order for the user to see it. (We have to check here to
00150 # catch special pages etc. We check again in Article::view())
00151 if( !is_null( $title ) && !$title->userCanRead() ) {
00152 $output->loginToUse();
00153 $output->output();
00154 $output->disable();
00155 return false;
00156 }
00157 return true;
00158 }
00159
00174 function initializeSpecialCases( &$title, &$output, $request ) {
00175 wfProfileIn( __METHOD__ );
00176
00177 $action = $this->getVal( 'Action' );
00178 if( is_null($title) || $title->getDBkey() == '' ) {
00179 $title = SpecialPage::getTitleFor( 'Badtitle' );
00180 # Die now before we mess up $wgArticle and the skin stops working
00181 throw new ErrorPageError( 'badtitle', 'badtitletext' );
00182 } else if( $title->getInterwiki() != '' ) {
00183 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
00184 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
00185 } else {
00186 $url = $title->getFullURL();
00187 }
00188
00189 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
00190 $output->redirect( $url );
00191 } else {
00192 $title = SpecialPage::getTitleFor( 'Badtitle' );
00193 throw new ErrorPageError( 'badtitle', 'badtitletext' );
00194 }
00195 } else if( $action == 'view' && !$request->wasPosted() &&
00196 ( !isset($this->GET['title']) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
00197 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
00198 {
00199 $targetUrl = $title->getFullURL();
00200
00201 if( $targetUrl == $request->getFullRequestURL() ) {
00202 $message = "Redirect loop detected!\n\n" .
00203 "This means the wiki got confused about what page was " .
00204 "requested; this sometimes happens when moving a wiki " .
00205 "to a new server or changing the server configuration.\n\n";
00206
00207 if( $this->getVal( 'UsePathInfo' ) ) {
00208 $message .= "The wiki is trying to interpret the page " .
00209 "title from the URL path portion (PATH_INFO), which " .
00210 "sometimes fails depending on the web server. Try " .
00211 "setting \"\$wgUsePathInfo = false;\" in your " .
00212 "LocalSettings.php, or check that \$wgArticlePath " .
00213 "is correct.";
00214 } else {
00215 $message .= "Your web server was detected as possibly not " .
00216 "supporting URL path components (PATH_INFO) correctly; " .
00217 "check your LocalSettings.php for a customized " .
00218 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
00219 "to true.";
00220 }
00221 wfHttpError( 500, "Internal error", $message );
00222 return false;
00223 } else {
00224 $output->setSquidMaxage( 1200 );
00225 $output->redirect( $targetUrl, '301' );
00226 }
00227 } else if( NS_SPECIAL == $title->getNamespace() ) {
00228
00229 SpecialPage::executePath( $title );
00230 } else {
00231
00232 wfProfileOut( __METHOD__ );
00233 return false;
00234 }
00235
00236 wfProfileOut( __METHOD__ );
00237 return true;
00238 }
00239
00246 static function articleFromTitle( &$title ) {
00247 if( NS_MEDIA == $title->getNamespace() ) {
00248
00249 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
00250 }
00251
00252 $article = null;
00253 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
00254 if( $article ) {
00255 return $article;
00256 }
00257
00258 switch( $title->getNamespace() ) {
00259 case NS_FILE:
00260 return new ImagePage( $title );
00261 case NS_CATEGORY:
00262 return new CategoryPage( $title );
00263 default:
00264 return new Article( $title );
00265 }
00266 }
00267
00276 function initializeArticle( &$title, $request ) {
00277 wfProfileIn( __METHOD__ );
00278
00279 $action = $this->getVal( 'action', 'view' );
00280 $article = self::articleFromTitle( $title );
00281 # NS_MEDIAWIKI has no redirects.
00282 # It is also used for CSS/JS, so performance matters here...
00283 if( $title->getNamespace() == NS_MEDIAWIKI ) {
00284 wfProfileOut( __METHOD__ );
00285 return $article;
00286 }
00287
00288
00289 $file = ($title->getNamespace() == NS_FILE) ? $article->getFile() : null;
00290 if( ( $action == 'view' || $action == 'render' )
00291 && !$request->getVal( 'oldid' ) &&
00292 $request->getVal( 'redirect' ) != 'no' &&
00293
00294 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
00295 {
00296 # Give extensions a change to ignore/handle redirects as needed
00297 $ignoreRedirect = $target = false;
00298
00299 $dbr = wfGetDB( DB_SLAVE );
00300 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
00301
00302 wfRunHooks( 'InitializeArticleMaybeRedirect',
00303 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
00304
00305
00306 if( !$ignoreRedirect && $article->isRedirect() ) {
00307 # Is the target already set by an extension?
00308 $target = $target ? $target : $article->followRedirect();
00309 if( is_string( $target ) ) {
00310 if( !$this->getVal( 'DisableHardRedirects' ) ) {
00311
00312 return $target;
00313 }
00314 }
00315 if( is_object($target) ) {
00316
00317 $rarticle = self::articleFromTitle( $target );
00318 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
00319 if( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
00320 $rarticle->setRedirectedFrom( $title );
00321 $article = $rarticle;
00322 $title = $target;
00323 }
00324 }
00325 } else {
00326 $title = $article->getTitle();
00327 }
00328 }
00329 wfProfileOut( __METHOD__ );
00330 return $article;
00331 }
00332
00339 function finalCleanup( &$deferredUpdates, &$output ) {
00340 wfProfileIn( __METHOD__ );
00341 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
00342 $factory = wfGetLBFactory();
00343 $factory->commitMasterChanges();
00344 # Output everything!
00345 $output->output();
00346 # Do any deferred jobs
00347 $this->doUpdates( $deferredUpdates );
00348 $this->doJobs();
00349 # Commit and close up!
00350 $factory->shutdown();
00351 wfProfileOut( __METHOD__ );
00352 }
00353
00362 function doUpdates( &$updates ) {
00363 wfProfileIn( __METHOD__ );
00364
00365 if (!$updates) {
00366 wfProfileOut( __METHOD__ );
00367 return;
00368 }
00369
00370 $dbw = wfGetDB( DB_MASTER );
00371 foreach( $updates as $up ) {
00372 $up->doUpdate();
00373
00374 # Commit after every update to prevent lock contention
00375 if( $dbw->trxLevel() ) {
00376 $dbw->commit();
00377 }
00378 }
00379 wfProfileOut( __METHOD__ );
00380 }
00381
00385 function doJobs() {
00386 $jobRunRate = $this->getVal( 'JobRunRate' );
00387
00388 if( $jobRunRate <= 0 || wfReadOnly() ) {
00389 return;
00390 }
00391 if( $jobRunRate < 1 ) {
00392 $max = mt_getrandmax();
00393 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
00394 return;
00395 }
00396 $n = 1;
00397 } else {
00398 $n = intval( $jobRunRate );
00399 }
00400
00401 while ( $n-- && false != ( $job = Job::pop() ) ) {
00402 $output = $job->toString() . "\n";
00403 $t = -wfTime();
00404 $success = $job->run();
00405 $t += wfTime();
00406 $t = round( $t*1000 );
00407 if( !$success ) {
00408 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
00409 } else {
00410 $output .= "Success, Time: $t ms\n";
00411 }
00412 wfDebugLog( 'jobqueue', $output );
00413 }
00414 }
00415
00419 function restInPeace() {
00420 wfLogProfilingData();
00421 wfDebug( "Request ended normally\n" );
00422 }
00423
00433 function performAction( &$output, &$article, &$title, &$user, &$request ) {
00434 wfProfileIn( __METHOD__ );
00435
00436 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
00437 wfProfileOut( __METHOD__ );
00438 return;
00439 }
00440
00441 $action = $this->getVal( 'Action' );
00442 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
00443
00444 $action = 'nosuchaction';
00445 }
00446
00447 switch( $action ) {
00448 case 'view':
00449 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
00450 $article->view();
00451 break;
00452 case 'raw':
00453 wfProfileIn( __METHOD__.'-raw' );
00454 $raw = new RawPage( $article );
00455 $raw->view();
00456 wfProfileOut( __METHOD__.'-raw' );
00457 break;
00458 case 'watch':
00459 case 'unwatch':
00460 case 'delete':
00461 case 'revert':
00462 case 'rollback':
00463 case 'protect':
00464 case 'unprotect':
00465 case 'info':
00466 case 'markpatrolled':
00467 case 'render':
00468 case 'deletetrackback':
00469 case 'purge':
00470 $article->$action();
00471 break;
00472 case 'print':
00473 $article->view();
00474 break;
00475 case 'dublincore':
00476 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
00477 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
00478 } else {
00479 $rdf = new DublinCoreRdf( $article );
00480 $rdf->show();
00481 }
00482 break;
00483 case 'creativecommons':
00484 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
00485 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
00486 } else {
00487 $rdf = new CreativeCommonsRdf( $article );
00488 $rdf->show();
00489 }
00490 break;
00491 case 'credits':
00492 Credits::showPage( $article );
00493 break;
00494 case 'submit':
00495 if( session_id() == '' ) {
00496
00497 wfSetupSession();
00498 }
00499
00500 case 'edit':
00501 case 'editredlink':
00502 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
00503 $internal = $request->getVal( 'internaledit' );
00504 $external = $request->getVal( 'externaledit' );
00505 $section = $request->getVal( 'section' );
00506 $oldid = $request->getVal( 'oldid' );
00507 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
00508 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
00509 $editor = new EditPage( $article );
00510 $editor->submit();
00511 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
00512 $mode = $request->getVal( 'mode' );
00513 $extedit = new ExternalEdit( $article, $mode );
00514 $extedit->edit();
00515 }
00516 }
00517 break;
00518 case 'history':
00519 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
00520 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
00521 }
00522 $history = new PageHistory( $article );
00523 $history->history();
00524 break;
00525 default:
00526 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
00527 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
00528 }
00529 }
00530 wfProfileOut( __METHOD__ );
00531
00532 }
00533
00534 };