3 * MediaWiki is the to-be base class for this whole project
5 * @internal documentation reviewed 15 Mar 2010
11 function __construct() {}
14 * Stores key/value pairs to circumvent global variables
15 * Note that keys are case-insensitive!
17 * @param $key String: key to store
18 * @param $value Mixed: value to put for the key
20 function setVal( $key, &$value ) {
21 $key = strtolower( $key );
22 $this->params[$key] =& $value;
26 * Retrieves key/value pairs to circumvent global variables
27 * Note that keys are case-insensitive!
29 * @param $key String: key to get
30 * @param $default string default value, defaults to empty string
31 * @return $default Mixed: default value if if the key doesn't exist
33 function getVal( $key, $default = '' ) {
34 $key = strtolower( $key );
35 if( isset( $this->params[$key] ) ) {
36 return $this->params[$key];
42 * Initialization of ... everything
43 * Performs the request too
45 * @param $title Title ($wgTitle)
46 * @param $article Article
47 * @param $output OutputPage
49 * @param $request WebRequest
51 function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) {
52 wfProfileIn( __METHOD__ );
54 $output->setTitle( $title );
56 wfRunHooks( 'BeforeInitialize', array( &$title, &$article, &$output, &$user, $request, $this ) );
58 if( !$this->preliminaryChecks( $title, $output ) ) {
59 wfProfileOut( __METHOD__ );
62 // Call handleSpecialCases() to deal with all special requests...
63 if( !$this->handleSpecialCases( $title, $output, $request ) ) {
64 // ...otherwise treat it as an article view. The article
65 // may be a redirect to another article or URL.
66 $new_article = $this->initializeArticle( $title, $output, $request );
67 if( is_object( $new_article ) ) {
68 $article = $new_article;
69 $this->performAction( $output, $article, $title, $user, $request );
70 } elseif( is_string( $new_article ) ) {
71 $output->redirect( $new_article );
73 wfProfileOut( __METHOD__ );
74 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
77 wfProfileOut( __METHOD__ );
81 * Check if the maximum lag of database slaves is higher that $maxLag, and
82 * if it's the case, output an error message
84 * @param $maxLag int: maximum lag allowed for the request, as supplied by
86 * @return bool true if the request can continue
88 function checkMaxLag( $maxLag ) {
89 list( $host, $lag ) = wfGetLB()->getMaxLag();
90 if( $lag > $maxLag ) {
91 wfMaxlagError( $host, $lag, $maxLag );
99 * Checks some initial queries
100 * Note that $title here is *not* a Title object, but a string!
102 * @param $title String
103 * @param $action String
104 * @return Title object to be $wgTitle
106 function checkInitialQueries( $title, $action ) {
107 global $wgOut, $wgRequest, $wgContLang;
108 if( $wgRequest->getVal( 'printable' ) === 'yes' ) {
109 $wgOut->setPrintable();
112 $curid = $wgRequest->getInt( 'curid' );
113 if( $wgRequest->getCheck( 'search' ) ) {
114 // Compatibility with old search URLs which didn't use Special:Search
115 // Just check for presence here, so blank requests still
116 // show the search page when using ugly URLs (bug 8054).
117 $ret = SpecialPage::getTitleFor( 'Search' );
119 // URLs like this are generated by RC, because rc_title isn't always accurate
120 $ret = Title::newFromID( $curid );
121 } elseif( $title == '' && $action != 'delete' ) {
122 $ret = Title::newMainPage();
124 $ret = Title::newFromURL( $title );
125 // check variant links so that interwiki links don't have to worry
126 // about the possible different language variants
127 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
128 $wgContLang->findVariantLink( $title, $ret );
130 // For non-special titles, check for implicit titles
131 if( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
132 // We can have urls with just ?diff=,?oldid= or even just ?diff=
133 $oldid = $wgRequest->getInt( 'oldid' );
134 $oldid = $oldid ? $oldid : $wgRequest->getInt( 'diff' );
135 // Allow oldid to override a changed or missing title
137 $rev = Revision::newFromId( $oldid );
138 $ret = $rev ? $rev->getTitle() : $ret;
145 * Checks for anon-cannot-read case
147 * @param $title Title
148 * @param $output OutputPage
149 * @return boolean true if successful
151 function preliminaryChecks( &$title, &$output ) {
153 // If the user is not logged in, the Namespace:title of the article must be in
154 // the Read array in order for the user to see it. (We have to check here to
155 // catch special pages etc. We check again in Article::view())
156 if( !is_null( $title ) && !$title->userCanRead() ) {
157 // Bug 32276: allowing the skin to generate output with $wgTitle
158 // set to the input title would allow anonymous users to
159 // determine whether a page exists, potentially leaking private data. In fact, the
160 // curid and oldid request parameters would allow page titles to be enumerated even
161 // when they are not guessable. So we reset the title to Special:Badtitle before the
162 // permissions error is displayed.
163 $badtitle = SpecialPage::getTitleFor( 'Badtitle' );
164 $output->setTitle( $badtitle );
165 $wgTitle = $badtitle;
167 $output->loginToUse();
168 $this->finalCleanup( $output );
176 * Initialize some special cases:
178 * - local interwiki redirects
182 * @param $title Title
183 * @param $output OutputPage
184 * @param $request WebRequest
185 * @return bool true if the request is already executed
187 function handleSpecialCases( &$title, &$output, $request ) {
188 wfProfileIn( __METHOD__ );
190 $action = $this->getVal( 'Action' );
192 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
193 if( is_null($title) || ( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
194 $title = SpecialPage::getTitleFor( 'Badtitle' );
195 $output->setTitle( $title ); // bug 21456
196 // Die now before we mess up $wgArticle and the skin stops working
197 throw new ErrorPageError( 'badtitle', 'badtitletext' );
199 // Interwiki redirects
200 } else if( $title->getInterwiki() != '' ) {
201 $rdfrom = $request->getVal( 'rdfrom' );
203 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
205 $query = $request->getValues();
206 unset( $query['title'] );
207 $url = $title->getFullURL( $query );
209 /* Check for a redirect loop */
210 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
211 // 301 so google et al report the target as the actual url.
212 $output->redirect( $url, 301 );
214 $title = SpecialPage::getTitleFor( 'Badtitle' );
215 $output->setTitle( $title ); // bug 21456
216 wfProfileOut( __METHOD__ );
217 throw new ErrorPageError( 'badtitle', 'badtitletext' );
219 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
220 } else if ( $action == 'view' && !$request->wasPosted()
221 && ( $request->getVal( 'title' ) === null || $title->getPrefixedDBKey() != $request->getVal( 'title' ) )
222 && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
224 if ( $title->getNamespace() == NS_SPECIAL ) {
225 list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
227 $title = SpecialPage::getTitleFor( $name, $subpage );
230 $targetUrl = $title->getFullURL();
231 // Redirect to canonical url, make it a 301 to allow caching
232 if( $targetUrl == $request->getFullRequestURL() ) {
233 $message = "Redirect loop detected!\n\n" .
234 "This means the wiki got confused about what page was " .
235 "requested; this sometimes happens when moving a wiki " .
236 "to a new server or changing the server configuration.\n\n";
238 if( $this->getVal( 'UsePathInfo' ) ) {
239 $message .= "The wiki is trying to interpret the page " .
240 "title from the URL path portion (PATH_INFO), which " .
241 "sometimes fails depending on the web server. Try " .
242 "setting \"\$wgUsePathInfo = false;\" in your " .
243 "LocalSettings.php, or check that \$wgArticlePath " .
246 $message .= "Your web server was detected as possibly not " .
247 "supporting URL path components (PATH_INFO) correctly; " .
248 "check your LocalSettings.php for a customized " .
249 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
252 wfHttpError( 500, "Internal error", $message );
253 wfProfileOut( __METHOD__ );
256 $output->setSquidMaxage( 1200 );
257 $output->redirect( $targetUrl, '301' );
260 } else if( NS_SPECIAL == $title->getNamespace() ) {
261 /* actions that need to be made when we have a special pages */
262 SpecialPage::executePath( $title );
264 /* No match to special cases */
265 wfProfileOut( __METHOD__ );
268 /* Did match a special case */
269 wfProfileOut( __METHOD__ );
274 * Create an Article object of the appropriate class for the given page.
276 * @param $title Title
277 * @return Article object
279 static function articleFromTitle( &$title ) {
280 if( NS_MEDIA == $title->getNamespace() ) {
281 // FIXME: where should this go?
282 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
286 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
291 switch( $title->getNamespace() ) {
293 return new ImagePage( $title );
295 return new CategoryPage( $title );
297 return new Article( $title );
302 * Initialize the object to be known as $wgArticle for "standard" actions
303 * Create an Article object for the page, following redirects if needed.
305 * @param $title Title ($wgTitle)
306 * @param $output OutputPage ($wgOut)
307 * @param $request WebRequest ($wgRequest)
308 * @return mixed an Article, or a string to redirect to another URL
310 function initializeArticle( &$title, &$output, $request ) {
311 wfProfileIn( __METHOD__ );
313 $action = $this->getVal( 'action', 'view' );
314 $article = self::articleFromTitle( $title );
315 // NS_MEDIAWIKI has no redirects.
316 // It is also used for CSS/JS, so performance matters here...
317 if( $title->getNamespace() == NS_MEDIAWIKI ) {
318 wfProfileOut( __METHOD__ );
321 // Namespace might change when using redirects
322 // Check for redirects ...
323 $file = ($title->getNamespace() == NS_FILE) ? $article->getFile() : null;
324 if( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
325 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
326 !$request->getVal( 'diff' ) && // ... and not when showing diff
327 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
328 // ... and the article is not a non-redirect image page with associated file
329 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
331 // Give extensions a change to ignore/handle redirects as needed
332 $ignoreRedirect = $target = false;
334 $dbr = wfGetDB( DB_SLAVE );
335 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
337 wfRunHooks( 'InitializeArticleMaybeRedirect',
338 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
340 // Follow redirects only for... redirects.
341 // If $target is set, then a hook wanted to redirect.
342 if( !$ignoreRedirect && ($target || $article->isRedirect()) ) {
343 // Is the target already set by an extension?
344 $target = $target ? $target : $article->followRedirect();
345 if( is_string( $target ) ) {
346 if( !$this->getVal( 'DisableHardRedirects' ) ) {
347 // we'll need to redirect
348 wfProfileOut( __METHOD__ );
352 if( is_object($target) ) {
353 // Rewrite environment to redirected article
354 $rarticle = self::articleFromTitle( $target );
355 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
356 if( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
357 $rarticle->setRedirectedFrom( $title );
358 $article = $rarticle;
360 $output->setTitle( $title );
364 $title = $article->getTitle();
367 wfProfileOut( __METHOD__ );
372 * Cleaning up request by doing:
373 ** deferred updates, DB transaction, and the output
375 * @param $output OutputPage
377 function finalCleanup( &$output ) {
378 wfProfileIn( __METHOD__ );
379 // Now commit any transactions, so that unreported errors after
380 // output() don't roll back the whole DB transaction
381 $factory = wfGetLBFactory();
382 $factory->commitMasterChanges();
383 // Output everything!
385 // Do any deferred jobs
388 wfProfileOut( __METHOD__ );
392 * Do a job from the job queue
395 $jobRunRate = $this->getVal( 'JobRunRate' );
397 if( $jobRunRate <= 0 || wfReadOnly() ) {
400 if( $jobRunRate < 1 ) {
401 $max = mt_getrandmax();
402 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
407 $n = intval( $jobRunRate );
410 while ( $n-- && false != ( $job = Job::pop() ) ) {
411 $output = $job->toString() . "\n";
413 $success = $job->run();
415 $t = round( $t*1000 );
417 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
419 $output .= "Success, Time: $t ms\n";
421 wfDebugLog( 'jobqueue', $output );
426 * Ends this task peacefully
428 function restInPeace() {
429 wfLogProfilingData();
430 // Commit and close up!
431 $factory = wfGetLBFactory();
432 $factory->commitMasterChanges();
433 $factory->shutdown();
434 wfDebug( "Request ended normally\n" );
438 * Perform one of the "standard" actions
440 * @param $output OutputPage
441 * @param $article Article
442 * @param $title Title
444 * @param $request WebRequest
446 function performAction( &$output, &$article, &$title, &$user, &$request ) {
447 wfProfileIn( __METHOD__ );
449 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
450 wfProfileOut( __METHOD__ );
454 $action = $this->getVal( 'Action' );
455 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
456 /* No such action; this will switch to the default case */
457 $action = 'nosuchaction';
460 // Workaround for bug #20966: inability of IE to provide an action dependent
461 // on which submit button is clicked.
462 if ( $action === 'historysubmit' ) {
463 if ( $request->getBool( 'revisiondelete' ) ) {
464 $action = 'revisiondelete';
472 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
475 case 'raw': // includes JS/CSS
476 wfProfileIn( __METHOD__.'-raw' );
477 $raw = new RawPage( $article );
479 wfProfileOut( __METHOD__.'-raw' );
489 case 'markpatrolled':
491 case 'deletetrackback':
499 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
500 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
502 $rdf = new DublinCoreRdf( $article );
506 case 'creativecommons':
507 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
508 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
510 $rdf = new CreativeCommonsRdf( $article );
515 Credits::showPage( $article );
518 if( session_id() == '' ) {
519 /* Send a cookie so anons get talk message notifications */
525 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
526 $internal = $request->getVal( 'internaledit' );
527 $external = $request->getVal( 'externaledit' );
528 $section = $request->getVal( 'section' );
529 $oldid = $request->getVal( 'oldid' );
530 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
531 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
532 $editor = new EditPage( $article );
534 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
535 $mode = $request->getVal( 'mode' );
536 $extedit = new ExternalEdit( $article, $mode );
542 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
543 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
545 $history = new HistoryPage( $article );
548 case 'revisiondelete':
549 // For show/hide submission from history page
550 $special = SpecialPage::getPage( 'Revisiondelete' );
551 $special->execute( '' );
554 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
555 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
558 wfProfileOut( __METHOD__ );