723351dfcb07dc05c256df3028acfd556ed9cc65
3 * MediaWiki is the to-be base class for this whole project
5 * @internal documentation reviewed 15 Mar 2010
10 * Array of options which may or may not be used
11 * FIXME: this seems currently to be a messy halfway-house between globals
12 * and a config object. Pick one and run with it
15 private $params = array();
24 * Output to deliver content to
25 * FIXME: most stuff in the codebase doesn't actually write to this, it'll write to
26 * $wgOut instead. So in practice this has to be $wgOut;
32 * Stores key/value pairs to circumvent global variables
33 * Note that keys are case-insensitive!
35 * @param $key String: key to store
36 * @param $value Mixed: value to put for the key
38 public function setVal( $key, &$value ) {
39 $key = strtolower( $key );
40 $this->params
[$key] =& $value;
44 * Retrieves key/value pairs to circumvent global variables
45 * Note that keys are case-insensitive!
47 * @param $key String: key to get
48 * @param $default string default value, defaults to empty string
49 * @return $default Mixed: default value if if the key doesn't exist
51 public function getVal( $key, $default = '' ) {
52 $key = strtolower( $key );
53 if ( isset( $this->params
[$key] ) ) {
54 return $this->params
[$key];
59 public function request( WebRequest
&$x = null ){
60 return wfSetVar( $this->request
, $x );
63 public function output( OutputPage
&$x = null ){
64 return wfSetVar( $this->output
, $x );
67 public function __construct( WebRequest
&$request, /*OutputPage*/ &$output ){
68 $this->request
=& $request;
69 $this->output
=& $output;
73 * Initialization of ... everything
74 * Performs the request too
76 * @param $title Title ($wgTitle)
77 * @param $article Article
80 public function performRequestForTitle( &$title, &$article, &$user ) {
81 wfProfileIn( __METHOD__
);
83 $this->output
->setTitle( $title );
84 if ( $this->request
->getVal( 'printable' ) === 'yes' ) {
85 $this->output
->setPrintable();
88 wfRunHooks( 'BeforeInitialize', array(
97 // If the user is not logged in, the Namespace:title of the article must be in
98 // the Read array in order for the user to see it. (We have to check here to
99 // catch special pages etc. We check again in Article::view())
100 if ( !is_null( $title ) && !$title->userCanRead() ) {
101 $this->output
->loginToUse();
102 $this->finalCleanup();
103 $this->output
->disable();
104 wfProfileOut( __METHOD__
);
108 // Call handleSpecialCases() to deal with all special requests...
109 if ( !$this->handleSpecialCases( $title ) ) {
110 // ...otherwise treat it as an article view. The article
111 // may be a redirect to another article or URL.
112 $new_article = $this->initializeArticle( $title );
113 if ( is_object( $new_article ) ) {
114 $article = $new_article;
115 $this->performAction( $article, $title, $user );
116 } elseif ( is_string( $new_article ) ) {
117 $this->output
->redirect( $new_article );
119 wfProfileOut( __METHOD__
);
120 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
123 wfProfileOut( __METHOD__
);
127 * Checks some initial queries
128 * FIXME: rename to parseTitle() ?
130 * @return Title object to be $wgTitle
132 /* private */ function checkInitialQueries() {
135 $curid = $this->request
->getInt( 'curid' );
136 $title = $this->request
->getVal( 'title' );
138 if ( $this->request
->getCheck( 'search' ) ) {
139 // Compatibility with old search URLs which didn't use Special:Search
140 // Just check for presence here, so blank requests still
141 // show the search page when using ugly URLs (bug 8054).
142 $ret = SpecialPage
::getTitleFor( 'Search' );
143 } elseif ( $curid ) {
144 // URLs like this are generated by RC, because rc_title isn't always accurate
145 $ret = Title
::newFromID( $curid );
146 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
147 $ret = Title
::newMainPage();
149 $ret = Title
::newFromURL( $title );
150 // check variant links so that interwiki links don't have to worry
151 // about the possible different language variants
152 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
153 $wgContLang->findVariantLink( $title, $ret );
155 // For non-special titles, check for implicit titles
156 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
157 // We can have urls with just ?diff=,?oldid= or even just ?diff=
158 $oldid = $this->request
->getInt( 'oldid' );
159 $oldid = $oldid ?
$oldid : $this->request
->getInt( 'diff' );
160 // Allow oldid to override a changed or missing title
162 $rev = Revision
::newFromId( $oldid );
163 $ret = $rev ?
$rev->getTitle() : $ret;
170 * Initialize some special cases:
172 * - local interwiki redirects
176 * @param $title Title
177 * @return bool true if the request is already executed
179 private function handleSpecialCases( &$title ) {
180 wfProfileIn( __METHOD__
);
182 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
183 if ( is_null( $title ) ||
( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
184 $title = SpecialPage
::getTitleFor( 'Badtitle' );
185 $this->output
->setTitle( $title ); // bug 21456
186 // Die now before we mess up $wgArticle and the skin stops working
187 throw new ErrorPageError( 'badtitle', 'badtitletext' );
189 // Interwiki redirects
190 } else if ( $title->getInterwiki() != '' ) {
191 $rdfrom = $this->request
->getVal( 'rdfrom' );
193 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
195 $query = $this->request
->getValues();
196 unset( $query['title'] );
197 $url = $title->getFullURL( $query );
199 /* Check for a redirect loop */
200 if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $title->isLocal() ) {
201 // 301 so google et al report the target as the actual url.
202 $this->output
->redirect( $url, 301 );
204 $title = SpecialPage
::getTitleFor( 'Badtitle' );
205 $this->output
->setTitle( $title ); // bug 21456
206 wfProfileOut( __METHOD__
);
207 throw new ErrorPageError( 'badtitle', 'badtitletext' );
209 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
210 } else if ( $this->request
->getVal( 'action', 'view' ) == 'view' && !$this->request
->wasPosted()
211 && ( $this->request
->getVal( 'title' ) === null ||
$title->getPrefixedDBKey() != $this->request
->getVal( 'title' ) )
212 && !count( array_diff( array_keys( $this->request
->getValues() ), array( 'action', 'title' ) ) ) )
214 if ( $title->getNamespace() == NS_SPECIAL
) {
215 list( $name, $subpage ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBkey() );
217 $title = SpecialPage
::getTitleFor( $name, $subpage );
220 $targetUrl = $title->getFullURL();
221 // Redirect to canonical url, make it a 301 to allow caching
222 if ( $targetUrl == $this->request
->getFullRequestURL() ) {
223 $message = "Redirect loop detected!\n\n" .
224 "This means the wiki got confused about what page was " .
225 "requested; this sometimes happens when moving a wiki " .
226 "to a new server or changing the server configuration.\n\n";
228 if ( $this->getVal( 'UsePathInfo' ) ) {
229 $message .= "The wiki is trying to interpret the page " .
230 "title from the URL path portion (PATH_INFO), which " .
231 "sometimes fails depending on the web server. Try " .
232 "setting \"\$wgUsePathInfo = false;\" in your " .
233 "LocalSettings.php, or check that \$wgArticlePath " .
236 $message .= "Your web server was detected as possibly not " .
237 "supporting URL path components (PATH_INFO) correctly; " .
238 "check your LocalSettings.php for a customized " .
239 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
242 wfHttpError( 500, "Internal error", $message );
243 wfProfileOut( __METHOD__
);
246 $this->output
->setSquidMaxage( 1200 );
247 $this->output
->redirect( $targetUrl, '301' );
250 } else if ( NS_SPECIAL
== $title->getNamespace() ) {
251 /* actions that need to be made when we have a special pages */
252 SpecialPage
::executePath( $title );
254 /* No match to special cases */
255 wfProfileOut( __METHOD__
);
258 /* Did match a special case */
259 wfProfileOut( __METHOD__
);
264 * Create an Article object of the appropriate class for the given page.
266 * @param $title Title
267 * @return Article object
269 public static function articleFromTitle( &$title ) {
270 if ( NS_MEDIA
== $title->getNamespace() ) {
271 // FIXME: where should this go?
272 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
276 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
281 switch( $title->getNamespace() ) {
283 return new ImagePage( $title );
285 return new CategoryPage( $title );
287 return new Article( $title );
292 * Returns the action that will be executed, not necesserly the one passed
293 * passed through the "action" parameter. Actions disabled in
294 * $wgDisabledActions will be replaced by "nosuchaction"
296 * @return String: action
298 public function getAction() {
299 global $wgDisabledActions;
301 $action = $this->request
->getVal( 'action', 'view' );
303 // Check for disabled actions
304 if ( in_array( $action, $wgDisabledActions ) ) {
305 return 'nosuchaction';
308 // Workaround for bug #20966: inability of IE to provide an action dependent
309 // on which submit button is clicked.
310 if ( $action === 'historysubmit' ) {
311 if ( $this->request
->getBool( 'revisiondelete' ) ) {
312 return 'revisiondelete';
313 } elseif ( $this->request
->getBool( 'revisionmove' ) ) {
314 return 'revisionmove';
318 } elseif ( $action == 'editredlink' ) {
326 * Initialize the object to be known as $wgArticle for "standard" actions
327 * Create an Article object for the page, following redirects if needed.
329 * @param $title Title ($wgTitle)
330 * @return mixed an Article, or a string to redirect to another URL
332 private function initializeArticle( &$title ) {
333 wfProfileIn( __METHOD__
);
335 $action = $this->request
->getVal( 'action', 'view' );
336 $article = self
::articleFromTitle( $title );
337 // NS_MEDIAWIKI has no redirects.
338 // It is also used for CSS/JS, so performance matters here...
339 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
340 wfProfileOut( __METHOD__
);
343 // Namespace might change when using redirects
344 // Check for redirects ...
345 $file = ( $title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
346 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
347 && !$this->request
->getVal( 'oldid' ) && // ... and are not old revisions
348 !$this->request
->getVal( 'diff' ) && // ... and not when showing diff
349 $this->request
->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
350 // ... and the article is not a non-redirect image page with associated file
351 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
353 // Give extensions a change to ignore/handle redirects as needed
354 $ignoreRedirect = $target = false;
356 wfRunHooks( 'InitializeArticleMaybeRedirect',
357 array( &$title, &$this->request
, &$ignoreRedirect, &$target, &$article ) );
359 // Follow redirects only for... redirects.
360 // If $target is set, then a hook wanted to redirect.
361 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
362 // Is the target already set by an extension?
363 $target = $target ?
$target : $article->followRedirect();
364 if ( is_string( $target ) ) {
365 if ( !$this->getVal( 'DisableHardRedirects' ) ) {
366 // we'll need to redirect
367 wfProfileOut( __METHOD__
);
371 if ( is_object( $target ) ) {
372 // Rewrite environment to redirected article
373 $rarticle = self
::articleFromTitle( $target );
374 $rarticle->loadPageData();
375 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
376 $rarticle->setRedirectedFrom( $title );
377 $article = $rarticle;
379 $this->output
->setTitle( $title );
383 $title = $article->getTitle();
386 wfProfileOut( __METHOD__
);
391 * Cleaning up request by doing deferred updates, DB transaction, and the output
393 public function finalCleanup() {
394 wfProfileIn( __METHOD__
);
395 // Now commit any transactions, so that unreported errors after
396 // output() don't roll back the whole DB transaction
397 $factory = wfGetLBFactory();
398 $factory->commitMasterChanges();
399 // Output everything!
400 $this->output
->output();
401 // Do any deferred jobs
402 wfDoUpdates( 'commit' );
403 // Close the session so that jobs don't access the current session
404 session_write_close();
406 wfProfileOut( __METHOD__
);
410 * Do a job from the job queue
412 private function doJobs() {
413 global $wgJobRunRate;
415 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
418 if ( $wgJobRunRate < 1 ) {
419 $max = mt_getrandmax();
420 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
425 $n = intval( $wgJobRunRate );
428 while ( $n-- && false != ( $job = Job
::pop() ) ) {
429 $output = $job->toString() . "\n";
431 $success = $job->run();
433 $t = round( $t * 1000 );
435 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
437 $output .= "Success, Time: $t ms\n";
439 wfDebugLog( 'jobqueue', $output );
444 * Ends this task peacefully
446 public function restInPeace() {
447 MessageCache
::logMessages();
448 wfLogProfilingData();
449 // Commit and close up!
450 $factory = wfGetLBFactory();
451 $factory->commitMasterChanges();
452 $factory->shutdown();
453 wfDebug( "Request ended normally\n" );
457 * Perform one of the "standard" actions
459 * @param $article Article
460 * @param $title Title
463 private function performAction( &$article, &$title, &$user ) {
464 wfProfileIn( __METHOD__
);
466 if ( !wfRunHooks( 'MediaWikiPerformAction', array( $this->output
, $article, $title, $user, $this->request
, $this ) ) ) {
467 wfProfileOut( __METHOD__
);
471 $action = $this->getAction();
475 $this->output
->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
478 case 'raw': // includes JS/CSS
479 wfProfileIn( __METHOD__
. '-raw' );
480 $raw = new RawPage( $article );
482 wfProfileOut( __METHOD__
. '-raw' );
492 case 'markpatrolled':
494 case 'deletetrackback':
502 if ( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
503 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
505 $rdf = new DublinCoreRdf( $article );
509 case 'creativecommons':
510 if ( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
511 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
513 $rdf = new CreativeCommonsRdf( $article );
518 Credits
::showPage( $article );
521 if ( session_id() == '' ) {
522 /* Send a cookie so anons get talk message notifications */
527 if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
528 $internal = $this->request
->getVal( 'internaledit' );
529 $external = $this->request
->getVal( 'externaledit' );
530 $section = $this->request
->getVal( 'section' );
531 $oldid = $this->request
->getVal( 'oldid' );
532 if ( !$this->getVal( 'UseExternalEditor' ) ||
$action == 'submit' ||
$internal ||
533 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
534 $editor = new EditPage( $article );
536 } elseif ( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
537 $mode = $this->request
->getVal( 'mode' );
538 $extedit = new ExternalEdit( $article, $mode );
544 if ( $this->request
->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
545 $this->output
->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
547 $history = new HistoryPage( $article );
550 case 'revisiondelete':
551 // For show/hide submission from history page
552 $special = SpecialPage
::getPage( 'Revisiondelete' );
553 $special->execute( '' );
556 // For revision move submission from history page
557 $special = SpecialPage
::getPage( 'RevisionMove' );
558 $special->execute( '' );
561 if ( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
562 $this->output
->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
565 wfProfileOut( __METHOD__
);