5b0972eac78407ae439434ccc52d9ac8ebb7964f
3 * Helper class for the index.php entry point.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * The MediaWiki class is the helper class for the index.php entry point.
26 * @internal documentation reviewed 15 Mar 2010
31 * TODO: fold $output, etc, into this
36 public function request( WebRequest
$x = null ){
37 $old = $this->context
->getRequest();
38 $this->context
->setRequest( $x );
42 public function output( OutputPage
$x = null ){
43 $old = $this->context
->getOutput();
44 $this->context
->setOutput( $x );
48 public function __construct( RequestContext
$context = null ) {
50 $context = RequestContext
::getMain();
53 $this->context
= $context;
54 $this->context
->setTitle( $this->parseTitle() );
58 * Parse the request to get the Title object
60 * @return Title object to be $wgTitle
62 private function parseTitle() {
65 $request = $this->context
->getRequest();
66 $curid = $request->getInt( 'curid' );
67 $title = $request->getVal( 'title' );
69 if ( $request->getCheck( 'search' ) ) {
70 // Compatibility with old search URLs which didn't use Special:Search
71 // Just check for presence here, so blank requests still
72 // show the search page when using ugly URLs (bug 8054).
73 $ret = SpecialPage
::getTitleFor( 'Search' );
75 // URLs like this are generated by RC, because rc_title isn't always accurate
76 $ret = Title
::newFromID( $curid );
77 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
78 $ret = Title
::newMainPage();
80 $ret = Title
::newFromURL( $title );
81 // check variant links so that interwiki links don't have to worry
82 // about the possible different language variants
83 if ( count( $wgContLang->getVariants() ) > 1
84 && !is_null( $ret ) && $ret->getArticleID() == 0 )
86 $wgContLang->findVariantLink( $title, $ret );
89 // For non-special titles, check for implicit titles
90 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
91 // We can have urls with just ?diff=,?oldid= or even just ?diff=
92 $oldid = $request->getInt( 'oldid' );
93 $oldid = $oldid ?
$oldid : $request->getInt( 'diff' );
94 // Allow oldid to override a changed or missing title
96 $rev = Revision
::newFromId( $oldid );
97 $ret = $rev ?
$rev->getTitle() : $ret;
101 if ( $ret === null ||
( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ) {
108 * Get the Title object that we'll be acting on, as specified in the WebRequest
111 public function getTitle(){
112 if( $this->context
->getTitle() === null ){
113 $this->context
->setTitle( $this->parseTitle() );
115 return $this->context
->getTitle();
119 * Performs the request.
122 * - local interwiki redirects
129 public function performRequest() {
130 global $wgServer, $wgUsePathInfo;
132 wfProfileIn( __METHOD__
);
134 $request = $this->context
->getRequest();
135 $title = $this->context
->getTitle();
136 $output = $this->context
->getOutput();
137 $user = $this->context
->getUser();
139 if ( $request->getVal( 'printable' ) === 'yes' ) {
140 $output->setPrintable();
143 $pageView = false; // was an article or special page viewed?
145 wfRunHooks( 'BeforeInitialize',
146 array( &$title, null, &$output, &$user, $request, $this ) );
148 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
149 if ( $title instanceof BadTitle
) {
150 throw new ErrorPageError( 'badtitle', 'badtitletext' );
151 // If the user is not logged in, the Namespace:title of the article must be in
152 // the Read array in order for the user to see it. (We have to check here to
153 // catch special pages etc. We check again in Article::view())
154 } elseif ( !$title->userCanRead() ) {
155 $output->loginToUse();
156 // Interwiki redirects
157 } elseif ( $title->getInterwiki() != '' ) {
158 $rdfrom = $request->getVal( 'rdfrom' );
160 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
162 $query = $request->getValues();
163 unset( $query['title'] );
164 $url = $title->getFullURL( $query );
166 // Check for a redirect loop
167 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url )
168 && $title->isLocal() )
170 // 301 so google et al report the target as the actual url.
171 $output->redirect( $url, 301 );
173 $this->context
->setTitle( new BadTitle
);
174 wfProfileOut( __METHOD__
);
175 throw new ErrorPageError( 'badtitle', 'badtitletext' );
177 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
178 } elseif ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted()
179 && ( $request->getVal( 'title' ) === null ||
180 $title->getPrefixedDBKey() != $request->getVal( 'title' ) )
181 && !count( $request->getValueNames( array( 'action', 'title' ) ) ) )
183 if ( $title->getNamespace() == NS_SPECIAL
) {
184 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
186 $title = SpecialPage
::getTitleFor( $name, $subpage );
189 $targetUrl = $title->getFullURL();
190 // Redirect to canonical url, make it a 301 to allow caching
191 if ( $targetUrl == $request->getFullRequestURL() ) {
192 $message = "Redirect loop detected!\n\n" .
193 "This means the wiki got confused about what page was " .
194 "requested; this sometimes happens when moving a wiki " .
195 "to a new server or changing the server configuration.\n\n";
197 if ( $wgUsePathInfo ) {
198 $message .= "The wiki is trying to interpret the page " .
199 "title from the URL path portion (PATH_INFO), which " .
200 "sometimes fails depending on the web server. Try " .
201 "setting \"\$wgUsePathInfo = false;\" in your " .
202 "LocalSettings.php, or check that \$wgArticlePath " .
205 $message .= "Your web server was detected as possibly not " .
206 "supporting URL path components (PATH_INFO) correctly; " .
207 "check your LocalSettings.php for a customized " .
208 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
211 wfHttpError( 500, "Internal error", $message );
213 $output->setSquidMaxage( 1200 );
214 $output->redirect( $targetUrl, '301' );
217 } elseif ( NS_SPECIAL
== $title->getNamespace() ) {
219 // Actions that need to be made when we have a special pages
220 SpecialPageFactory
::executePath( $title, $this->context
);
222 // ...otherwise treat it as an article view. The article
223 // may be a redirect to another article or URL.
224 $article = $this->initializeArticle();
225 if ( is_object( $article ) ) {
228 * $wgArticle is deprecated, do not use it. This will possibly be removed
229 * entirely in 1.20 or 1.21
230 * @deprecated since 1.19
233 $wgArticle = $article;
235 $this->performAction( $article );
236 } elseif ( is_string( $article ) ) {
237 $output->redirect( $article );
239 wfProfileOut( __METHOD__
);
240 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
245 // Promote user to any groups they meet the criteria for
246 $user->addAutopromoteOnceGroups( 'onView' );
249 wfProfileOut( __METHOD__
);
253 * Create an Article object of the appropriate class for the given page.
255 * @deprecated in 1.19; use Article::newFromTitle() instead
256 * @param $title Title
257 * @param $context RequestContext
258 * @return Article object
260 public static function articleFromTitle( $title, RequestContext
$context ) {
261 return Article
::newFromTitle( $title, $context );
265 * Returns the action that will be executed, not necessarily the one passed
266 * passed through the "action" parameter. Actions disabled in
267 * $wgDisabledActions will be replaced by "nosuchaction"
269 * @return String: action
271 public function getAction() {
272 global $wgDisabledActions;
274 $request = $this->context
->getRequest();
275 $action = $request->getVal( 'action', 'view' );
277 // Check for disabled actions
278 if ( in_array( $action, $wgDisabledActions ) ) {
279 $action = 'nosuchaction';
280 } elseif ( $action === 'historysubmit' ) {
281 // Workaround for bug #20966: inability of IE to provide an action dependent
282 // on which submit button is clicked.
283 if ( $request->getBool( 'revisiondelete' ) ) {
284 $action = 'revisiondelete';
288 } elseif ( $action == 'editredlink' ) {
292 // Write back the executed action
293 $request->setVal( 'action', $action );
298 * Initialize the main Article object for "standard" actions (view, etc)
299 * Create an Article object for the page, following redirects if needed.
301 * @return mixed an Article, or a string to redirect to another URL
303 private function initializeArticle() {
304 global $wgDisableHardRedirects;
306 wfProfileIn( __METHOD__
);
308 $request = $this->context
->getRequest();
309 $title = $this->context
->getTitle();
311 $action = $request->getVal( 'action', 'view' );
312 $article = Article
::newFromTitle( $title, $this->context
);
313 // NS_MEDIAWIKI has no redirects.
314 // It is also used for CSS/JS, so performance matters here...
315 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
316 wfProfileOut( __METHOD__
);
319 // Namespace might change when using redirects
320 // Check for redirects ...
321 $file = ( $title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
322 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
323 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
324 !$request->getVal( 'diff' ) && // ... and not when showing diff
325 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
326 // ... and the article is not a non-redirect image page with associated file
327 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
329 // Give extensions a change to ignore/handle redirects as needed
330 $ignoreRedirect = $target = false;
332 wfRunHooks( 'InitializeArticleMaybeRedirect',
333 array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
335 // Follow redirects only for... redirects.
336 // If $target is set, then a hook wanted to redirect.
337 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
338 // Is the target already set by an extension?
339 $target = $target ?
$target : $article->followRedirect();
340 if ( is_string( $target ) ) {
341 if ( !$wgDisableHardRedirects ) {
342 // we'll need to redirect
343 wfProfileOut( __METHOD__
);
347 if ( is_object( $target ) ) {
348 // Rewrite environment to redirected article
349 $rarticle = Article
::newFromTitle( $target, $this->context
);
350 $rarticle->loadPageData();
351 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
352 $rarticle->setRedirectedFrom( $title );
353 $article = $rarticle;
354 $this->context
->setTitle( $target );
358 $this->context
->setTitle( $article->getTitle() );
361 wfProfileOut( __METHOD__
);
366 * Cleaning up request by doing deferred updates, DB transaction, and the output
368 public function finalCleanup() {
369 wfProfileIn( __METHOD__
);
370 // Now commit any transactions, so that unreported errors after
371 // output() don't roll back the whole DB transaction
372 $factory = wfGetLBFactory();
373 $factory->commitMasterChanges();
374 // Output everything!
375 $this->context
->getOutput()->output();
376 // Do any deferred jobs
377 wfDoUpdates( 'commit' );
379 wfProfileOut( __METHOD__
);
383 * Do a job from the job queue
385 private function doJobs() {
386 global $wgJobRunRate;
388 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
391 if ( $wgJobRunRate < 1 ) {
392 $max = mt_getrandmax();
393 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
398 $n = intval( $wgJobRunRate );
401 while ( $n-- && false != ( $job = Job
::pop() ) ) {
402 $output = $job->toString() . "\n";
404 $success = $job->run();
406 $t = round( $t * 1000 );
408 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
410 $output .= "Success, Time: $t ms\n";
412 wfDebugLog( 'jobqueue', $output );
417 * Ends this task peacefully
419 public function restInPeace() {
420 MessageCache
::logMessages();
421 wfLogProfilingData();
422 // Commit and close up!
423 $factory = wfGetLBFactory();
424 $factory->commitMasterChanges();
425 $factory->shutdown();
426 wfDebug( "Request ended normally\n" );
430 * Perform one of the "standard" actions
432 * @param $article Article
434 private function performAction( Page
$article ) {
435 global $wgSquidMaxage, $wgUseExternalEditor;
437 wfProfileIn( __METHOD__
);
439 $request = $this->context
->getRequest();
440 $output = $this->context
->getOutput();
441 $title = $this->context
->getTitle();
442 $user = $this->context
->getUser();
444 if ( !wfRunHooks( 'MediaWikiPerformAction',
445 array( $output, $article, $title, $user, $request, $this ) ) )
447 wfProfileOut( __METHOD__
);
451 $act = $this->getAction();
453 $action = Action
::factory( $act, $article );
454 if( $action instanceof Action
){
456 wfProfileOut( __METHOD__
);
462 $output->setSquidMaxage( $wgSquidMaxage );
465 case 'raw': // includes JS/CSS
466 wfProfileIn( __METHOD__
. '-raw' );
467 $raw = new RawPage( $article );
469 wfProfileOut( __METHOD__
. '-raw' );
478 if ( session_id() == '' ) {
479 // Send a cookie so anons get talk message notifications
484 if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
485 $internal = $request->getVal( 'internaledit' );
486 $external = $request->getVal( 'externaledit' );
487 $section = $request->getVal( 'section' );
488 $oldid = $request->getVal( 'oldid' );
489 if ( !$wgUseExternalEditor ||
$act == 'submit' ||
$internal ||
490 $section ||
$oldid ||
491 ( !$user->getOption( 'externaleditor' ) && !$external ) )
493 $editor = new EditPage( $article );
495 } elseif ( $wgUseExternalEditor
496 && ( $external ||
$user->getOption( 'externaleditor' ) ) )
498 $mode = $request->getVal( 'mode' );
499 $extedit = new ExternalEdit( $article, $mode );
505 if ( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
506 $output->setSquidMaxage( $wgSquidMaxage );
508 $history = new HistoryPage( $article );
512 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
513 $request->setVal( 'action', 'nosuchaction' );
514 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
517 wfProfileOut( __METHOD__
);
521 * Run the current MediaWiki instance
522 * index.php just calls this
526 $this->checkMaxLag( true );
528 $this->restInPeace();
529 } catch ( Exception
$e ) {
530 MWExceptionHandler
::handle( $e );
535 * Checks if the request should abort due to a lagged server,
536 * for given maxlag parameter.
538 * @param boolean $abort True if this class should abort the
539 * script execution. False to return the result as a boolean.
540 * @return boolean True if we passed the check, false if we surpass the maxlag
542 function checkMaxLag( $abort ) {
543 global $wgShowHostnames;
545 wfProfileIn( __METHOD__
);
546 $maxLag = $this->context
->getRequest()->getVal( 'maxlag' );
547 if ( !is_null( $maxLag ) ) {
548 $lb = wfGetLB(); // foo()->bar() is not supported in PHP4
549 list( $host, $lag ) = $lb->getMaxLag();
550 if ( $lag > $maxLag ) {
552 header( 'HTTP/1.1 503 Service Unavailable' );
553 header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
554 header( 'X-Database-Lag: ' . intval( $lag ) );
555 header( 'Content-Type: text/plain' );
556 if( $wgShowHostnames ) {
557 echo "Waiting for $host: $lag seconds lagged\n";
559 echo "Waiting for a database server: $lag seconds lagged\n";
563 wfProfileOut( __METHOD__
);
570 wfProfileOut( __METHOD__
);
575 global $wgUseFileCache, $wgTitle, $wgUseAjax;
577 wfProfileIn( __METHOD__
);
579 # Set title from request parameters
580 $wgTitle = $this->getTitle();
581 $action = $this->getAction();
583 # Send Ajax requests to the Ajax dispatcher.
584 if ( $wgUseAjax && $action == 'ajax' ) {
585 $dispatcher = new AjaxDispatcher();
586 $dispatcher->performAction();
587 wfProfileOut( __METHOD__
);
591 if ( $wgUseFileCache && $wgTitle->getNamespace() != NS_SPECIAL
) {
592 wfProfileIn( 'main-try-filecache' );
593 // Raw pages should handle cache control on their own,
594 // even when using file cache. This reduces hits from clients.
595 if ( HTMLFileCache
::useFileCache() ) {
596 /* Try low-level file cache hit */
597 $cache = new HTMLFileCache( $wgTitle, $action );
598 if ( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
599 /* Check incoming headers to see if client has this cached */
600 $timestamp = $cache->fileCacheTime();
601 if ( !$this->context
->getOutput()->checkLastModified( $timestamp ) ) {
602 $cache->loadFromFileCache();
604 # Do any stats increment/watchlist stuff
605 $article = Article
::newFromTitle( $wgTitle, $this->context
);
606 $article->viewUpdates();
607 # Tell OutputPage that output is taken care of
608 $this->context
->getOutput()->disable();
609 wfProfileOut( 'main-try-filecache' );
610 wfProfileOut( __METHOD__
);
614 wfProfileOut( 'main-try-filecache' );
617 $this->performRequest();
618 $this->finalCleanup();
620 wfProfileOut( __METHOD__
);