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();
18 * TODO: fold $output, etc, into this
24 * Stores key/value pairs to circumvent global variables
25 * Note that keys are case-insensitive!
27 * @param $key String: key to store
28 * @param $value Mixed: value to put for the key
30 public function setVal( $key, &$value ) {
31 $key = strtolower( $key );
32 $this->params
[$key] =& $value;
36 * Retrieves key/value pairs to circumvent global variables
37 * Note that keys are case-insensitive!
39 * @param $key String: key to get
40 * @param $default string default value, defaults to empty string
41 * @return $default Mixed: default value if if the key doesn't exist
43 public function getVal( $key, $default = '' ) {
44 $key = strtolower( $key );
45 if ( isset( $this->params
[$key] ) ) {
46 return $this->params
[$key];
51 public function request( WebRequest
$x = null ){
52 return wfSetVar( $this->context
->request
, $x );
55 public function output( OutputPage
$x = null ){
56 return wfSetVar( $this->context
->output
, $x );
59 public function __construct( RequestContext
$context ){
60 $this->context
= $context;
61 $this->context
->setTitle( $this->parseTitle() );
65 * Initialization of ... everything
66 * Performs the request too
68 * @param $article Article
70 public function performRequestForTitle( &$article ) {
71 wfProfileIn( __METHOD__
);
73 if ( $this->context
->request
->getVal( 'printable' ) === 'yes' ) {
74 $this->context
->output
->setPrintable();
77 wfRunHooks( 'BeforeInitialize', array(
78 &$this->context
->title
,
80 &$this->context
->output
,
81 &$this->context
->user
,
82 $this->context
->request
,
86 // If the user is not logged in, the Namespace:title of the article must be in
87 // the Read array in order for the user to see it. (We have to check here to
88 // catch special pages etc. We check again in Article::view())
89 if ( !is_null( $this->context
->title
) && !$this->context
->title
->userCanRead() ) {
90 $this->context
->output
->loginToUse();
91 $this->finalCleanup();
92 $this->context
->output
->disable();
93 wfProfileOut( __METHOD__
);
97 // Call handleSpecialCases() to deal with all special requests...
98 if ( !$this->handleSpecialCases() ) {
99 // ...otherwise treat it as an article view. The article
100 // may be a redirect to another article or URL.
101 $new_article = $this->initializeArticle();
102 if ( is_object( $new_article ) ) {
103 $article = $new_article;
104 $this->performAction( $article );
105 } elseif ( is_string( $new_article ) ) {
106 $this->context
->output
->redirect( $new_article );
108 wfProfileOut( __METHOD__
);
109 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
112 wfProfileOut( __METHOD__
);
116 * Parse $request to get the Title object
118 * @return Title object to be $wgTitle
120 private function parseTitle() {
123 $curid = $this->context
->request
->getInt( 'curid' );
124 $title = $this->context
->request
->getVal( 'title' );
126 if ( $this->context
->request
->getCheck( 'search' ) ) {
127 // Compatibility with old search URLs which didn't use Special:Search
128 // Just check for presence here, so blank requests still
129 // show the search page when using ugly URLs (bug 8054).
130 $ret = SpecialPage
::getTitleFor( 'Search' );
131 } elseif ( $curid ) {
132 // URLs like this are generated by RC, because rc_title isn't always accurate
133 $ret = Title
::newFromID( $curid );
134 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
135 $ret = Title
::newMainPage();
137 $ret = Title
::newFromURL( $title );
138 // check variant links so that interwiki links don't have to worry
139 // about the possible different language variants
140 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
141 $wgContLang->findVariantLink( $title, $ret );
144 // For non-special titles, check for implicit titles
145 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
146 // We can have urls with just ?diff=,?oldid= or even just ?diff=
147 $oldid = $this->context
->request
->getInt( 'oldid' );
148 $oldid = $oldid ?
$oldid : $this->context
->request
->getInt( 'diff' );
149 // Allow oldid to override a changed or missing title
151 $rev = Revision
::newFromId( $oldid );
152 $ret = $rev ?
$rev->getTitle() : $ret;
156 if( $ret === null ||
( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
163 * Get the Title object that we'll be acting on, as specified in the WebRequest
166 public function getTitle(){
167 if( $this->context
->title
=== null ){
168 $this->context
->title
= $this->parseTitle();
170 return $this->context
->title
;
174 * Initialize some special cases:
176 * - local interwiki redirects
180 * @return bool true if the request is already executed
182 private function handleSpecialCases() {
183 wfProfileIn( __METHOD__
);
185 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
186 if ( $this->context
->title
instanceof BadTitle
) {
187 // Die now before we mess up $wgArticle and the skin stops working
188 throw new ErrorPageError( 'badtitle', 'badtitletext' );
190 // Interwiki redirects
191 } else if ( $this->context
->title
->getInterwiki() != '' ) {
192 $rdfrom = $this->context
->request
->getVal( 'rdfrom' );
194 $url = $this->context
->title
->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
196 $query = $this->context
->request
->getValues();
197 unset( $query['title'] );
198 $url = $this->context
->title
->getFullURL( $query );
200 // Check for a redirect loop
201 if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $this->context
->title
->isLocal() ) {
202 // 301 so google et al report the target as the actual url.
203 $this->context
->output
->redirect( $url, 301 );
205 $this->context
->title
= new BadTitle
;
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->context
->request
->getVal( 'action', 'view' ) == 'view' && !$this->context
->request
->wasPosted()
211 && ( $this->context
->request
->getVal( 'title' ) === null ||
$this->context
->title
->getPrefixedDBKey() != $this->context
->request
->getVal( 'title' ) )
212 && !count( array_diff( array_keys( $this->context
->request
->getValues() ), array( 'action', 'title' ) ) ) )
214 if ( $this->context
->title
->getNamespace() == NS_SPECIAL
) {
215 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $this->context
->title
->getDBkey() );
217 $this->context
->title
= SpecialPage
::getTitleFor( $name, $subpage );
220 $targetUrl = $this->context
->title
->getFullURL();
221 // Redirect to canonical url, make it a 301 to allow caching
222 if ( $targetUrl == $this->context
->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->context
->output
->setSquidMaxage( 1200 );
247 $this->context
->output
->redirect( $targetUrl, '301' );
250 } else if ( NS_SPECIAL
== $this->context
->title
->getNamespace() ) {
251 // actions that need to be made when we have a special pages
252 SpecialPageFactory
::executePath( $this->context
->title
, $this->context
);
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 * @param $context RequestContext
268 * @return Article object
270 public static function articleFromTitle( $title, RequestContext
$context ) {
271 if ( NS_MEDIA
== $title->getNamespace() ) {
272 // FIXME: where should this go?
273 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
277 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
282 switch( $title->getNamespace() ) {
284 $page = new ImagePage( $title );
287 $page = new CategoryPage( $title );
290 $page = new Article( $title );
292 $page->setContext( $context );
297 * Returns the action that will be executed, not necesserly the one passed
298 * passed through the "action" parameter. Actions disabled in
299 * $wgDisabledActions will be replaced by "nosuchaction"
301 * @return String: action
303 public function getAction() {
304 global $wgDisabledActions;
306 $action = $this->context
->request
->getVal( 'action', 'view' );
308 // Check for disabled actions
309 if ( in_array( $action, $wgDisabledActions ) ) {
310 return 'nosuchaction';
313 // Workaround for bug #20966: inability of IE to provide an action dependent
314 // on which submit button is clicked.
315 if ( $action === 'historysubmit' ) {
316 if ( $this->context
->request
->getBool( 'revisiondelete' ) ) {
317 return 'revisiondelete';
318 } elseif ( $this->context
->request
->getBool( 'revisionmove' ) ) {
319 return 'revisionmove';
323 } elseif ( $action == 'editredlink' ) {
331 * Initialize the object to be known as $wgArticle for "standard" actions
332 * Create an Article object for the page, following redirects if needed.
334 * @return mixed an Article, or a string to redirect to another URL
336 private function initializeArticle() {
337 wfProfileIn( __METHOD__
);
339 $action = $this->context
->request
->getVal( 'action', 'view' );
340 $article = self
::articleFromTitle( $this->context
->title
, $this->context
);
341 // NS_MEDIAWIKI has no redirects.
342 // It is also used for CSS/JS, so performance matters here...
343 if ( $this->context
->title
->getNamespace() == NS_MEDIAWIKI
) {
344 wfProfileOut( __METHOD__
);
347 // Namespace might change when using redirects
348 // Check for redirects ...
349 $file = ( $this->context
->title
->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
350 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
351 && !$this->context
->request
->getVal( 'oldid' ) && // ... and are not old revisions
352 !$this->context
->request
->getVal( 'diff' ) && // ... and not when showing diff
353 $this->context
->request
->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
354 // ... and the article is not a non-redirect image page with associated file
355 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
357 // Give extensions a change to ignore/handle redirects as needed
358 $ignoreRedirect = $target = false;
360 wfRunHooks( 'InitializeArticleMaybeRedirect',
361 array( &$this->context
->title
, &$this->context
->request
, &$ignoreRedirect, &$target, &$article ) );
363 // Follow redirects only for... redirects.
364 // If $target is set, then a hook wanted to redirect.
365 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
366 // Is the target already set by an extension?
367 $target = $target ?
$target : $article->followRedirect();
368 if ( is_string( $target ) ) {
369 if ( !$this->getVal( 'DisableHardRedirects' ) ) {
370 // we'll need to redirect
371 wfProfileOut( __METHOD__
);
375 if ( is_object( $target ) ) {
376 // Rewrite environment to redirected article
377 $rarticle = self
::articleFromTitle( $target, $this->context
);
378 $rarticle->loadPageData();
379 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
380 $rarticle->setRedirectedFrom( $this->context
->title
);
381 $article = $rarticle;
382 $this->context
->title
= $target;
386 $this->context
->title
= $article->getTitle();
389 wfProfileOut( __METHOD__
);
394 * Cleaning up request by doing deferred updates, DB transaction, and the output
396 public function finalCleanup() {
397 wfProfileIn( __METHOD__
);
398 // Now commit any transactions, so that unreported errors after
399 // output() don't roll back the whole DB transaction
400 $factory = wfGetLBFactory();
401 $factory->commitMasterChanges();
402 // Output everything!
403 $this->context
->output
->output();
404 // Do any deferred jobs
405 wfDoUpdates( 'commit' );
406 // Close the session so that jobs don't access the current session
407 session_write_close();
409 wfProfileOut( __METHOD__
);
413 * Do a job from the job queue
415 private function doJobs() {
416 global $wgJobRunRate;
418 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
421 if ( $wgJobRunRate < 1 ) {
422 $max = mt_getrandmax();
423 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
428 $n = intval( $wgJobRunRate );
431 while ( $n-- && false != ( $job = Job
::pop() ) ) {
432 $output = $job->toString() . "\n";
434 $success = $job->run();
436 $t = round( $t * 1000 );
438 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
440 $output .= "Success, Time: $t ms\n";
442 wfDebugLog( 'jobqueue', $output );
447 * Ends this task peacefully
449 public function restInPeace() {
450 MessageCache
::logMessages();
451 wfLogProfilingData();
452 // Commit and close up!
453 $factory = wfGetLBFactory();
454 $factory->commitMasterChanges();
455 $factory->shutdown();
456 wfDebug( "Request ended normally\n" );
460 * Perform one of the "standard" actions
462 * @param $article Article
464 private function performAction( $article ) {
465 wfProfileIn( __METHOD__
);
467 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
468 $this->context
->output
, $article, $this->context
->title
,
469 $this->context
->user
, $this->context
->request
, $this ) ) )
471 wfProfileOut( __METHOD__
);
475 $act = $this->getAction();
477 $action = Action
::factory( $this->getAction(), $article );
478 if( $action instanceof Action
){
480 wfProfileOut( __METHOD__
);
486 $this->context
->output
->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
489 case 'raw': // includes JS/CSS
490 wfProfileIn( __METHOD__
. '-raw' );
491 $raw = new RawPage( $article );
493 wfProfileOut( __METHOD__
. '-raw' );
501 case 'markpatrolled':
503 case 'deletetrackback':
507 if ( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
508 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
510 $rdf = new DublinCoreRdf( $article );
514 case 'creativecommons':
515 if ( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
516 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
518 $rdf = new CreativeCommonsRdf( $article );
523 if ( session_id() == '' ) {
524 // Send a cookie so anons get talk message notifications
529 if ( wfRunHooks( 'CustomEditor', array( $article, $this->context
->user
) ) ) {
530 $internal = $this->context
->request
->getVal( 'internaledit' );
531 $external = $this->context
->request
->getVal( 'externaledit' );
532 $section = $this->context
->request
->getVal( 'section' );
533 $oldid = $this->context
->request
->getVal( 'oldid' );
534 if ( !$this->getVal( 'UseExternalEditor' ) ||
$act == 'submit' ||
$internal ||
535 $section ||
$oldid ||
( !$this->context
->user
->getOption( 'externaleditor' ) && !$external ) ) {
536 $editor = new EditPage( $article );
538 } elseif ( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$this->context
->user
->getOption( 'externaleditor' ) ) ) {
539 $mode = $this->context
->request
->getVal( 'mode' );
540 $extedit = new ExternalEdit( $article, $mode );
546 if ( $this->context
->request
->getFullRequestURL() == $this->context
->title
->getInternalURL( 'action=history' ) ) {
547 $this->context
->output
->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
549 $history = new HistoryPage( $article );
552 case 'revisiondelete':
553 // For show/hide submission from history page
554 $special = SpecialPageFactory
::getPage( 'Revisiondelete' );
555 $special->execute( '' );
558 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
559 $this->context
->output
->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
562 wfProfileOut( __METHOD__
);