dafae673fbf28e4d31dc8e63023fe55c4a3aa967
3 * MediaWiki is the to-be base class for this whole project
5 * @internal documentation reviewed 15 Mar 2010
10 * TODO: fold $output, etc, into this
15 public function request( WebRequest
$x = null ){
16 $this->context
->setRequest( $x );
17 return $this->context
->getRequest();
20 public function output( OutputPage
$x = null ){
21 $this->context
->setOutput( $x );
22 return $this->context
->getOutput();
25 public function __construct( RequestContext
$context = null ) {
27 $context = RequestContext
::getMain();
30 $this->context
= $context;
31 $this->context
->setTitle( $this->parseTitle() );
35 * Parse $request to get the Title object
37 * @return Title object to be $wgTitle
39 private function parseTitle() {
42 $request = $this->context
->getRequest();
43 $curid = $request->getInt( 'curid' );
44 $title = $request->getVal( 'title' );
46 if ( $request->getCheck( 'search' ) ) {
47 // Compatibility with old search URLs which didn't use Special:Search
48 // Just check for presence here, so blank requests still
49 // show the search page when using ugly URLs (bug 8054).
50 $ret = SpecialPage
::getTitleFor( 'Search' );
52 // URLs like this are generated by RC, because rc_title isn't always accurate
53 $ret = Title
::newFromID( $curid );
54 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
55 $ret = Title
::newMainPage();
57 $ret = Title
::newFromURL( $title );
58 // check variant links so that interwiki links don't have to worry
59 // about the possible different language variants
60 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
61 $wgContLang->findVariantLink( $title, $ret );
64 // For non-special titles, check for implicit titles
65 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
66 // We can have urls with just ?diff=,?oldid= or even just ?diff=
67 $oldid = $request->getInt( 'oldid' );
68 $oldid = $oldid ?
$oldid : $request->getInt( 'diff' );
69 // Allow oldid to override a changed or missing title
71 $rev = Revision
::newFromId( $oldid );
72 $ret = $rev ?
$rev->getTitle() : $ret;
76 if( $ret === null ||
( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
83 * Get the Title object that we'll be acting on, as specified in the WebRequest
86 public function getTitle(){
87 if( $this->context
->getTitle() === null ){
88 $this->context
->setTitle( $this->parseTitle() );
90 return $this->context
->getTitle();
94 * Performs the request.
97 * - local interwiki redirects
102 * @return Article object
104 public function performRequest() {
105 global $wgServer, $wgUsePathInfo;
107 wfProfileIn( __METHOD__
);
109 $request = $this->context
->getRequest();
110 $title = $this->context
->getTitle();
111 $output = $this->context
->getOutput();
112 $user = $this->context
->getUser();
114 if ( $request->getVal( 'printable' ) === 'yes' ) {
115 $output->setPrintable();
118 wfRunHooks( 'BeforeInitialize', array(
127 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
128 if ( $title instanceof BadTitle
) {
129 throw new ErrorPageError( 'badtitle', 'badtitletext' );
130 // If the user is not logged in, the Namespace:title of the article must be in
131 // the Read array in order for the user to see it. (We have to check here to
132 // catch special pages etc. We check again in Article::view())
133 } elseif ( !$title->userCanRead() ) {
134 $output->loginToUse();
135 // Interwiki redirects
136 } elseif ( $title->getInterwiki() != '' ) {
137 $rdfrom = $request->getVal( 'rdfrom' );
139 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
141 $query = $request->getValues();
142 unset( $query['title'] );
143 $url = $title->getFullURL( $query );
145 // Check for a redirect loop
146 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $title->isLocal() ) {
147 // 301 so google et al report the target as the actual url.
148 $output->redirect( $url, 301 );
150 $this->context
->setTitle( new BadTitle
);
151 wfProfileOut( __METHOD__
);
152 throw new ErrorPageError( 'badtitle', 'badtitletext' );
154 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
155 } elseif ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted()
156 && ( $request->getVal( 'title' ) === null ||
$title->getPrefixedDBKey() != $request->getVal( 'title' ) )
157 && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
159 if ( $title->getNamespace() == NS_SPECIAL
) {
160 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
162 $title = SpecialPage
::getTitleFor( $name, $subpage );
165 $targetUrl = $title->getFullURL();
166 // Redirect to canonical url, make it a 301 to allow caching
167 if ( $targetUrl == $request->getFullRequestURL() ) {
168 $message = "Redirect loop detected!\n\n" .
169 "This means the wiki got confused about what page was " .
170 "requested; this sometimes happens when moving a wiki " .
171 "to a new server or changing the server configuration.\n\n";
173 if ( $wgUsePathInfo ) {
174 $message .= "The wiki is trying to interpret the page " .
175 "title from the URL path portion (PATH_INFO), which " .
176 "sometimes fails depending on the web server. Try " .
177 "setting \"\$wgUsePathInfo = false;\" in your " .
178 "LocalSettings.php, or check that \$wgArticlePath " .
181 $message .= "Your web server was detected as possibly not " .
182 "supporting URL path components (PATH_INFO) correctly; " .
183 "check your LocalSettings.php for a customized " .
184 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
187 wfHttpError( 500, "Internal error", $message );
189 $output->setSquidMaxage( 1200 );
190 $output->redirect( $targetUrl, '301' );
193 } elseif ( NS_SPECIAL
== $title->getNamespace() ) {
194 // actions that need to be made when we have a special pages
195 SpecialPageFactory
::executePath( $title, $this->context
);
197 // ...otherwise treat it as an article view. The article
198 // may be a redirect to another article or URL.
199 $article = $this->initializeArticle();
200 if ( is_object( $article ) ) {
202 * $wgArticle is deprecated, do not use it. This will possibly be removed
203 * entirely in 1.20 or 1.21
204 * @deprecated since 1.19
207 $wgArticle = $article;
209 $this->performAction( $article );
210 wfProfileOut( __METHOD__
);
212 } elseif ( is_string( $article ) ) {
213 $output->redirect( $article );
215 wfProfileOut( __METHOD__
);
216 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
219 wfProfileOut( __METHOD__
);
223 * Create an Article object of the appropriate class for the given page.
225 * @deprecated in 1.19; use Article::newFromTitle() instead
226 * @param $title Title
227 * @param $context RequestContext
228 * @return Article object
230 public static function articleFromTitle( $title, RequestContext
$context ) {
231 return Article
::newFromTitle( $title, $context );
235 * Returns the action that will be executed, not necesserly the one passed
236 * passed through the "action" parameter. Actions disabled in
237 * $wgDisabledActions will be replaced by "nosuchaction"
239 * @return String: action
241 public function getAction() {
242 global $wgDisabledActions;
244 $request = $this->context
->getRequest();
245 $action = $request->getVal( 'action', 'view' );
247 // Check for disabled actions
248 if ( in_array( $action, $wgDisabledActions ) ) {
249 return 'nosuchaction';
252 // Workaround for bug #20966: inability of IE to provide an action dependent
253 // on which submit button is clicked.
254 if ( $action === 'historysubmit' ) {
255 if ( $request->getBool( 'revisiondelete' ) ) {
256 return 'revisiondelete';
260 } elseif ( $action == 'editredlink' ) {
268 * Initialize the main Article object for "standard" actions (view, etc)
269 * Create an Article object for the page, following redirects if needed.
271 * @return mixed an Article, or a string to redirect to another URL
273 private function initializeArticle() {
274 global $wgDisableHardRedirects;
276 wfProfileIn( __METHOD__
);
278 $request = $this->context
->getRequest();
279 $title = $this->context
->getTitle();
281 $action = $request->getVal( 'action', 'view' );
282 $article = Article
::newFromTitle( $title, $this->context
);
283 // NS_MEDIAWIKI has no redirects.
284 // It is also used for CSS/JS, so performance matters here...
285 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
286 wfProfileOut( __METHOD__
);
289 // Namespace might change when using redirects
290 // Check for redirects ...
291 $file = ( $title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
292 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
293 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
294 !$request->getVal( 'diff' ) && // ... and not when showing diff
295 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
296 // ... and the article is not a non-redirect image page with associated file
297 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
299 // Give extensions a change to ignore/handle redirects as needed
300 $ignoreRedirect = $target = false;
302 wfRunHooks( 'InitializeArticleMaybeRedirect',
303 array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
305 // Follow redirects only for... redirects.
306 // If $target is set, then a hook wanted to redirect.
307 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
308 // Is the target already set by an extension?
309 $target = $target ?
$target : $article->followRedirect();
310 if ( is_string( $target ) ) {
311 if ( !$wgDisableHardRedirects ) {
312 // we'll need to redirect
313 wfProfileOut( __METHOD__
);
317 if ( is_object( $target ) ) {
318 // Rewrite environment to redirected article
319 $rarticle = Article
::newFromTitle( $target, $this->context
);
320 $rarticle->loadPageData();
321 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
322 $rarticle->setRedirectedFrom( $title );
323 $article = $rarticle;
324 $this->context
->setTitle( $target );
328 $this->context
->setTitle( $article->getTitle() );
331 wfProfileOut( __METHOD__
);
336 * Cleaning up request by doing deferred updates, DB transaction, and the output
338 public function finalCleanup() {
339 wfProfileIn( __METHOD__
);
340 // Now commit any transactions, so that unreported errors after
341 // output() don't roll back the whole DB transaction
342 $factory = wfGetLBFactory();
343 $factory->commitMasterChanges();
344 // Output everything!
345 $this->context
->getOutput()->output();
346 // Do any deferred jobs
347 wfDoUpdates( 'commit' );
349 wfProfileOut( __METHOD__
);
353 * Do a job from the job queue
355 private function doJobs() {
356 global $wgJobRunRate;
358 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
361 if ( $wgJobRunRate < 1 ) {
362 $max = mt_getrandmax();
363 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
368 $n = intval( $wgJobRunRate );
371 while ( $n-- && false != ( $job = Job
::pop() ) ) {
372 $output = $job->toString() . "\n";
374 $success = $job->run();
376 $t = round( $t * 1000 );
378 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
380 $output .= "Success, Time: $t ms\n";
382 wfDebugLog( 'jobqueue', $output );
387 * Ends this task peacefully
389 public function restInPeace() {
390 MessageCache
::logMessages();
391 wfLogProfilingData();
392 // Commit and close up!
393 $factory = wfGetLBFactory();
394 $factory->commitMasterChanges();
395 $factory->shutdown();
396 wfDebug( "Request ended normally\n" );
400 * Perform one of the "standard" actions
402 * @param $article Article
404 private function performAction( $article ) {
405 global $wgSquidMaxage, $wgUseExternalEditor;
407 wfProfileIn( __METHOD__
);
409 $request = $this->context
->getRequest();
410 $output = $this->context
->getOutput();
411 $title = $this->context
->getTitle();
412 $user = $this->context
->getUser();
414 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
415 $output, $article, $title,
416 $user, $request, $this ) ) )
418 wfProfileOut( __METHOD__
);
422 $act = $this->getAction();
424 $action = Action
::factory( $act, $article );
425 if( $action instanceof Action
){
427 wfProfileOut( __METHOD__
);
433 $output->setSquidMaxage( $wgSquidMaxage );
436 case 'raw': // includes JS/CSS
437 wfProfileIn( __METHOD__
. '-raw' );
438 $raw = new RawPage( $article );
440 wfProfileOut( __METHOD__
. '-raw' );
452 if ( session_id() == '' ) {
453 // Send a cookie so anons get talk message notifications
458 if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
459 $internal = $request->getVal( 'internaledit' );
460 $external = $request->getVal( 'externaledit' );
461 $section = $request->getVal( 'section' );
462 $oldid = $request->getVal( 'oldid' );
463 if ( !$wgUseExternalEditor ||
$act == 'submit' ||
$internal ||
464 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
465 $editor = new EditPage( $article );
467 } elseif ( $wgUseExternalEditor && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
468 $mode = $request->getVal( 'mode' );
469 $extedit = new ExternalEdit( $article, $mode );
475 if ( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
476 $output->setSquidMaxage( $wgSquidMaxage );
478 $history = new HistoryPage( $article );
482 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
483 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
486 wfProfileOut( __METHOD__
);
490 * Run the current MediaWiki instance
491 * index.php just calls this
495 $this->checkMaxLag( true );
497 $this->finalCleanup();
498 $this->restInPeace();
499 } catch ( Exception
$e ) {
500 MWExceptionHandler
::handle( $e );
505 * Checks if the request should abort due to a lagged server,
506 * for given maxlag parameter.
508 * @param boolean $abort True if this class should abort the
509 * script execution. False to return the result as a boolean.
510 * @return boolean True if we passed the check, false if we surpass the maxlag
512 function checkMaxLag( $abort ) {
513 global $wgShowHostnames;
515 wfProfileIn( __METHOD__
);
516 $maxLag = $this->context
->getRequest()->getVal( 'maxlag' );
517 if ( !is_null( $maxLag ) ) {
518 $lb = wfGetLB(); // foo()->bar() is not supported in PHP4
519 list( $host, $lag ) = $lb->getMaxLag();
520 if ( $lag > $maxLag ) {
522 header( 'HTTP/1.1 503 Service Unavailable' );
523 header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
524 header( 'X-Database-Lag: ' . intval( $lag ) );
525 header( 'Content-Type: text/plain' );
526 if( $wgShowHostnames ) {
527 echo "Waiting for $host: $lag seconds lagged\n";
529 echo "Waiting for a database server: $lag seconds lagged\n";
533 wfProfileOut( __METHOD__
);
540 wfProfileOut( __METHOD__
);
545 global $wgUseFileCache, $wgTitle, $wgUseAjax;
547 wfProfileIn( __METHOD__
);
549 # Set title from request parameters
550 $wgTitle = $this->getTitle();
551 $action = $this->context
->getRequest()->getVal( 'action', 'view' );
553 # Send Ajax requests to the Ajax dispatcher.
554 if ( $wgUseAjax && $action == 'ajax' ) {
555 $dispatcher = new AjaxDispatcher();
556 $dispatcher->performAction();
557 wfProfileOut( __METHOD__
);
561 if ( $wgUseFileCache && $wgTitle !== null ) {
562 wfProfileIn( 'main-try-filecache' );
563 // Raw pages should handle cache control on their own,
564 // even when using file cache. This reduces hits from clients.
565 if ( $action != 'raw' && HTMLFileCache
::useFileCache() ) {
566 /* Try low-level file cache hit */
567 $cache = new HTMLFileCache( $wgTitle, $action );
568 if ( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
569 /* Check incoming headers to see if client has this cached */
570 if ( !$this->context
->getOutput()->checkLastModified( $cache->fileCacheTime() ) ) {
571 $cache->loadFromFileCache();
573 # Do any stats increment/watchlist stuff
574 $article = Article
::newFromTitle( $wgTitle, $this->context
);
575 $article->viewUpdates();
576 # Tell OutputPage that output is taken care of
577 $this->context
->getOutput()->disable();
578 wfProfileOut( 'main-try-filecache' );
579 wfProfileOut( __METHOD__
);
583 wfProfileOut( 'main-try-filecache' );
586 $this->performRequest();
588 wfProfileOut( __METHOD__
);