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 $old = $this->context
->getRequest();
17 $this->context
->setRequest( $x );
21 public function output( OutputPage
$x = null ){
22 $old = $this->context
->getOutput();
23 $this->context
->setOutput( $x );
27 public function __construct( RequestContext
$context = null ) {
29 $context = RequestContext
::getMain();
32 $this->context
= $context;
33 $this->context
->setTitle( $this->parseTitle() );
37 * Parse $request to get the Title object
39 * @return Title object to be $wgTitle
41 private function parseTitle() {
44 $request = $this->context
->getRequest();
45 $curid = $request->getInt( 'curid' );
46 $title = $request->getVal( 'title' );
48 if ( $request->getCheck( 'search' ) ) {
49 // Compatibility with old search URLs which didn't use Special:Search
50 // Just check for presence here, so blank requests still
51 // show the search page when using ugly URLs (bug 8054).
52 $ret = SpecialPage
::getTitleFor( 'Search' );
54 // URLs like this are generated by RC, because rc_title isn't always accurate
55 $ret = Title
::newFromID( $curid );
56 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
57 $ret = Title
::newMainPage();
59 $ret = Title
::newFromURL( $title );
60 // check variant links so that interwiki links don't have to worry
61 // about the possible different language variants
62 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
63 $wgContLang->findVariantLink( $title, $ret );
66 // For non-special titles, check for implicit titles
67 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
68 // We can have urls with just ?diff=,?oldid= or even just ?diff=
69 $oldid = $request->getInt( 'oldid' );
70 $oldid = $oldid ?
$oldid : $request->getInt( 'diff' );
71 // Allow oldid to override a changed or missing title
73 $rev = Revision
::newFromId( $oldid );
74 $ret = $rev ?
$rev->getTitle() : $ret;
78 if( $ret === null ||
( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
85 * Get the Title object that we'll be acting on, as specified in the WebRequest
88 public function getTitle(){
89 if( $this->context
->getTitle() === null ){
90 $this->context
->setTitle( $this->parseTitle() );
92 return $this->context
->getTitle();
96 * Performs the request.
99 * - local interwiki redirects
106 public function performRequest() {
107 global $wgServer, $wgUsePathInfo;
109 wfProfileIn( __METHOD__
);
111 $request = $this->context
->getRequest();
112 $title = $this->context
->getTitle();
113 $output = $this->context
->getOutput();
114 $user = $this->context
->getUser();
116 if ( $request->getVal( 'printable' ) === 'yes' ) {
117 $output->setPrintable();
120 $pageView = false; // was an article or special page viewed?
122 wfRunHooks( 'BeforeInitialize',
123 array( &$title, null, &$output, &$user, $request, $this ) );
125 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
126 if ( $title instanceof BadTitle
) {
127 throw new ErrorPageError( 'badtitle', 'badtitletext' );
128 // If the user is not logged in, the Namespace:title of the article must be in
129 // the Read array in order for the user to see it. (We have to check here to
130 // catch special pages etc. We check again in Article::view())
131 } elseif ( !$title->userCanRead() ) {
132 $output->loginToUse();
133 // Interwiki redirects
134 } elseif ( $title->getInterwiki() != '' ) {
135 $rdfrom = $request->getVal( 'rdfrom' );
137 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
139 $query = $request->getValues();
140 unset( $query['title'] );
141 $url = $title->getFullURL( $query );
143 // Check for a redirect loop
144 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $title->isLocal() ) {
145 // 301 so google et al report the target as the actual url.
146 $output->redirect( $url, 301 );
148 $this->context
->setTitle( new BadTitle
);
149 wfProfileOut( __METHOD__
);
150 throw new ErrorPageError( 'badtitle', 'badtitletext' );
152 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
153 } elseif ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted()
154 && ( $request->getVal( 'title' ) === null ||
$title->getPrefixedDBKey() != $request->getVal( 'title' ) )
155 && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
157 if ( $title->getNamespace() == NS_SPECIAL
) {
158 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
160 $title = SpecialPage
::getTitleFor( $name, $subpage );
163 $targetUrl = $title->getFullURL();
164 // Redirect to canonical url, make it a 301 to allow caching
165 if ( $targetUrl == $request->getFullRequestURL() ) {
166 $message = "Redirect loop detected!\n\n" .
167 "This means the wiki got confused about what page was " .
168 "requested; this sometimes happens when moving a wiki " .
169 "to a new server or changing the server configuration.\n\n";
171 if ( $wgUsePathInfo ) {
172 $message .= "The wiki is trying to interpret the page " .
173 "title from the URL path portion (PATH_INFO), which " .
174 "sometimes fails depending on the web server. Try " .
175 "setting \"\$wgUsePathInfo = false;\" in your " .
176 "LocalSettings.php, or check that \$wgArticlePath " .
179 $message .= "Your web server was detected as possibly not " .
180 "supporting URL path components (PATH_INFO) correctly; " .
181 "check your LocalSettings.php for a customized " .
182 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
185 wfHttpError( 500, "Internal error", $message );
187 $output->setSquidMaxage( 1200 );
188 $output->redirect( $targetUrl, '301' );
191 } elseif ( NS_SPECIAL
== $title->getNamespace() ) {
193 // Actions that need to be made when we have a special pages
194 SpecialPageFactory
::executePath( $title, $this->context
);
196 // ...otherwise treat it as an article view. The article
197 // may be a redirect to another article or URL.
198 $article = $this->initializeArticle();
199 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 } elseif ( is_string( $article ) ) {
211 $output->redirect( $article );
213 wfProfileOut( __METHOD__
);
214 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
219 // Promote user to any groups they meet the criteria for
220 $user->addAutopromoteOnceGroups( 'onView' );
223 wfProfileOut( __METHOD__
);
227 * Create an Article object of the appropriate class for the given page.
229 * @deprecated in 1.19; use Article::newFromTitle() instead
230 * @param $title Title
231 * @param $context RequestContext
232 * @return Article object
234 public static function articleFromTitle( $title, RequestContext
$context ) {
235 return Article
::newFromTitle( $title, $context );
239 * Returns the action that will be executed, not necesserly the one passed
240 * passed through the "action" parameter. Actions disabled in
241 * $wgDisabledActions will be replaced by "nosuchaction"
243 * @return String: action
245 public function getAction() {
246 global $wgDisabledActions;
248 $request = $this->context
->getRequest();
249 $action = $request->getVal( 'action', 'view' );
251 // Check for disabled actions
252 if ( in_array( $action, $wgDisabledActions ) ) {
253 return 'nosuchaction';
256 // Workaround for bug #20966: inability of IE to provide an action dependent
257 // on which submit button is clicked.
258 if ( $action === 'historysubmit' ) {
259 if ( $request->getBool( 'revisiondelete' ) ) {
260 return 'revisiondelete';
264 } elseif ( $action == 'editredlink' ) {
272 * Initialize the main Article object for "standard" actions (view, etc)
273 * Create an Article object for the page, following redirects if needed.
275 * @return mixed an Article, or a string to redirect to another URL
277 private function initializeArticle() {
278 global $wgDisableHardRedirects;
280 wfProfileIn( __METHOD__
);
282 $request = $this->context
->getRequest();
283 $title = $this->context
->getTitle();
285 $action = $request->getVal( 'action', 'view' );
286 $article = Article
::newFromTitle( $title, $this->context
);
287 // NS_MEDIAWIKI has no redirects.
288 // It is also used for CSS/JS, so performance matters here...
289 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
290 wfProfileOut( __METHOD__
);
293 // Namespace might change when using redirects
294 // Check for redirects ...
295 $file = ( $title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
296 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
297 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
298 !$request->getVal( 'diff' ) && // ... and not when showing diff
299 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
300 // ... and the article is not a non-redirect image page with associated file
301 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
303 // Give extensions a change to ignore/handle redirects as needed
304 $ignoreRedirect = $target = false;
306 wfRunHooks( 'InitializeArticleMaybeRedirect',
307 array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
309 // Follow redirects only for... redirects.
310 // If $target is set, then a hook wanted to redirect.
311 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
312 // Is the target already set by an extension?
313 $target = $target ?
$target : $article->followRedirect();
314 if ( is_string( $target ) ) {
315 if ( !$wgDisableHardRedirects ) {
316 // we'll need to redirect
317 wfProfileOut( __METHOD__
);
321 if ( is_object( $target ) ) {
322 // Rewrite environment to redirected article
323 $rarticle = Article
::newFromTitle( $target, $this->context
);
324 $rarticle->loadPageData();
325 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
326 $rarticle->setRedirectedFrom( $title );
327 $article = $rarticle;
328 $this->context
->setTitle( $target );
332 $this->context
->setTitle( $article->getTitle() );
335 wfProfileOut( __METHOD__
);
340 * Cleaning up request by doing deferred updates, DB transaction, and the output
342 public function finalCleanup() {
343 wfProfileIn( __METHOD__
);
344 // Now commit any transactions, so that unreported errors after
345 // output() don't roll back the whole DB transaction
346 $factory = wfGetLBFactory();
347 $factory->commitMasterChanges();
348 // Output everything!
349 $this->context
->getOutput()->output();
350 // Do any deferred jobs
351 wfDoUpdates( 'commit' );
353 wfProfileOut( __METHOD__
);
357 * Do a job from the job queue
359 private function doJobs() {
360 global $wgJobRunRate;
362 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
365 if ( $wgJobRunRate < 1 ) {
366 $max = mt_getrandmax();
367 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
372 $n = intval( $wgJobRunRate );
375 while ( $n-- && false != ( $job = Job
::pop() ) ) {
376 $output = $job->toString() . "\n";
378 $success = $job->run();
380 $t = round( $t * 1000 );
382 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
384 $output .= "Success, Time: $t ms\n";
386 wfDebugLog( 'jobqueue', $output );
391 * Ends this task peacefully
393 public function restInPeace() {
394 MessageCache
::logMessages();
395 wfLogProfilingData();
396 // Commit and close up!
397 $factory = wfGetLBFactory();
398 $factory->commitMasterChanges();
399 $factory->shutdown();
400 wfDebug( "Request ended normally\n" );
404 * Perform one of the "standard" actions
406 * @param $article Article
408 private function performAction( Article
$article ) {
409 global $wgSquidMaxage, $wgUseExternalEditor;
411 wfProfileIn( __METHOD__
);
413 $request = $this->context
->getRequest();
414 $output = $this->context
->getOutput();
415 $title = $this->context
->getTitle();
416 $user = $this->context
->getUser();
418 if ( !wfRunHooks( 'MediaWikiPerformAction',
419 array( $output, $article, $title, $user, $request, $this ) ) )
421 wfProfileOut( __METHOD__
);
425 $act = $this->getAction();
427 $action = Action
::factory( $act, $article );
428 if( $action instanceof Action
){
430 wfProfileOut( __METHOD__
);
436 $output->setSquidMaxage( $wgSquidMaxage );
439 case 'raw': // includes JS/CSS
440 wfProfileIn( __METHOD__
. '-raw' );
441 $raw = new RawPage( $article );
443 wfProfileOut( __METHOD__
. '-raw' );
454 if ( session_id() == '' ) {
455 // Send a cookie so anons get talk message notifications
460 if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
461 $internal = $request->getVal( 'internaledit' );
462 $external = $request->getVal( 'externaledit' );
463 $section = $request->getVal( 'section' );
464 $oldid = $request->getVal( 'oldid' );
465 if ( !$wgUseExternalEditor ||
$act == 'submit' ||
$internal ||
466 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
467 $editor = new EditPage( $article );
469 } elseif ( $wgUseExternalEditor && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
470 $mode = $request->getVal( 'mode' );
471 $extedit = new ExternalEdit( $article, $mode );
477 if ( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
478 $output->setSquidMaxage( $wgSquidMaxage );
480 $history = new HistoryPage( $article );
484 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
485 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
488 wfProfileOut( __METHOD__
);
492 * Run the current MediaWiki instance
493 * index.php just calls this
497 $this->checkMaxLag( true );
499 $this->restInPeace();
500 } catch ( Exception
$e ) {
501 MWExceptionHandler
::handle( $e );
506 * Checks if the request should abort due to a lagged server,
507 * for given maxlag parameter.
509 * @param boolean $abort True if this class should abort the
510 * script execution. False to return the result as a boolean.
511 * @return boolean True if we passed the check, false if we surpass the maxlag
513 function checkMaxLag( $abort ) {
514 global $wgShowHostnames;
516 wfProfileIn( __METHOD__
);
517 $maxLag = $this->context
->getRequest()->getVal( 'maxlag' );
518 if ( !is_null( $maxLag ) ) {
519 $lb = wfGetLB(); // foo()->bar() is not supported in PHP4
520 list( $host, $lag ) = $lb->getMaxLag();
521 if ( $lag > $maxLag ) {
523 header( 'HTTP/1.1 503 Service Unavailable' );
524 header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
525 header( 'X-Database-Lag: ' . intval( $lag ) );
526 header( 'Content-Type: text/plain' );
527 if( $wgShowHostnames ) {
528 echo "Waiting for $host: $lag seconds lagged\n";
530 echo "Waiting for a database server: $lag seconds lagged\n";
534 wfProfileOut( __METHOD__
);
541 wfProfileOut( __METHOD__
);
546 global $wgUseFileCache, $wgTitle, $wgUseAjax;
548 wfProfileIn( __METHOD__
);
550 # Set title from request parameters
551 $wgTitle = $this->getTitle();
552 $action = $this->getAction();
554 # Send Ajax requests to the Ajax dispatcher.
555 if ( $wgUseAjax && $action == 'ajax' ) {
556 $dispatcher = new AjaxDispatcher();
557 $dispatcher->performAction();
558 wfProfileOut( __METHOD__
);
562 if ( $wgUseFileCache && $wgTitle->getNamespace() != NS_SPECIAL
) {
563 wfProfileIn( 'main-try-filecache' );
564 // Raw pages should handle cache control on their own,
565 // even when using file cache. This reduces hits from clients.
566 if ( HTMLFileCache
::useFileCache() ) {
567 /* Try low-level file cache hit */
568 $cache = new HTMLFileCache( $wgTitle, $action );
569 if ( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
570 /* Check incoming headers to see if client has this cached */
571 if ( !$this->context
->getOutput()->checkLastModified( $cache->fileCacheTime() ) ) {
572 $cache->loadFromFileCache();
574 # Do any stats increment/watchlist stuff
575 $article = Article
::newFromTitle( $wgTitle, $this->context
);
576 $article->viewUpdates();
577 # Tell OutputPage that output is taken care of
578 $this->context
->getOutput()->disable();
579 wfProfileOut( 'main-try-filecache' );
580 wfProfileOut( __METHOD__
);
584 wfProfileOut( 'main-try-filecache' );
587 $this->performRequest();
588 $this->finalCleanup();
590 wfProfileOut( __METHOD__
);