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, $request ) ) {
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 if( $curid = $wgRequest->getInt( 'curid' ) ) {
113 // URLs like this are generated by RC, because rc_title isn't always accurate
114 $ret = Title
::newFromID( $curid );
115 } elseif( $title == '' && $action != 'delete' ) {
116 $ret = Title
::newMainPage();
118 $ret = Title
::newFromURL( $title );
119 // check variant links so that interwiki links don't have to worry
120 // about the possible different language variants
121 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
122 $wgContLang->findVariantLink( $title, $ret );
124 // For non-special titles, check for implicit titles
125 if( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
126 // We can have urls with just ?diff=,?oldid= or even just ?diff=
127 $oldid = $wgRequest->getInt( 'oldid' );
128 $oldid = $oldid ?
$oldid : $wgRequest->getInt( 'diff' );
129 // Allow oldid to override a changed or missing title
131 $rev = Revision
::newFromId( $oldid );
132 $ret = $rev ?
$rev->getTitle() : $ret;
139 * Checks for search query and anon-cannot-read case
141 * @param $title Title
142 * @param $output OutputPage
143 * @param $request WebRequest
144 * @return boolean true if successful
146 function preliminaryChecks( &$title, &$output, $request ) {
147 if( $request->getCheck( 'search' ) ) {
148 // Compatibility with old search URLs which didn't use Special:Search
149 // Just check for presence here, so blank requests still
150 // show the search page when using ugly URLs (bug 8054).
152 // Do this above the read whitelist check for security...
153 $title = SpecialPage
::getTitleFor( 'Search' );
155 // If the user is not logged in, the Namespace:title of the article must be in
156 // the Read array in order for the user to see it. (We have to check here to
157 // catch special pages etc. We check again in Article::view())
158 if( !is_null( $title ) && !$title->userCanRead() ) {
159 global $wgDeferredUpdateList;
160 $output->loginToUse();
161 $this->finalCleanup( $wgDeferredUpdateList, $output );
169 * Initialize some special cases:
171 * - local interwiki redirects
175 * @param $title Title
176 * @param $output OutputPage
177 * @param $request WebRequest
178 * @return bool true if the request is already executed
180 function handleSpecialCases( &$title, &$output, $request ) {
181 wfProfileIn( __METHOD__
);
182 global $wgContLang, $wgUser;
183 $action = $this->getVal( 'Action' );
185 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
186 if( is_null($title) ||
( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
187 $title = SpecialPage
::getTitleFor( 'Badtitle' );
188 $output->setTitle( $title ); // bug 21456
189 // Die now before we mess up $wgArticle and the skin stops working
190 throw new ErrorPageError( 'badtitle', 'badtitletext' );
192 // Interwiki redirects
193 } else if( $title->getInterwiki() != '' ) {
194 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
195 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
197 $query = $request->getValues();
198 unset( $query['title'] );
199 $url = $title->getFullURL( $query );
201 /* Check for a redirect loop */
202 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
203 $output->redirect( $url );
205 $title = SpecialPage
::getTitleFor( 'Badtitle' );
206 $output->setTitle( $title ); // bug 21456
207 wfProfileOut( __METHOD__
);
208 throw new ErrorPageError( 'badtitle', 'badtitletext' );
210 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
211 } else if( $action == 'view' && !$request->wasPosted()
212 && ( ( !$request->getVal( 'title' ) ||
$title->getPrefixedDBKey() != $request->getText( 'title' ) ) )
213 && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
215 $targetUrl = $title->getFullURL();
216 // Redirect to canonical url, make it a 301 to allow caching
217 if( $targetUrl == $request->getFullRequestURL() ) {
218 $message = "Redirect loop detected!\n\n" .
219 "This means the wiki got confused about what page was " .
220 "requested; this sometimes happens when moving a wiki " .
221 "to a new server or changing the server configuration.\n\n";
223 if( $this->getVal( 'UsePathInfo' ) ) {
224 $message .= "The wiki is trying to interpret the page " .
225 "title from the URL path portion (PATH_INFO), which " .
226 "sometimes fails depending on the web server. Try " .
227 "setting \"\$wgUsePathInfo = false;\" in your " .
228 "LocalSettings.php, or check that \$wgArticlePath " .
231 $message .= "Your web server was detected as possibly not " .
232 "supporting URL path components (PATH_INFO) correctly; " .
233 "check your LocalSettings.php for a customized " .
234 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
237 wfHttpError( 500, "Internal error", $message );
238 wfProfileOut( __METHOD__
);
241 $output->setSquidMaxage( 1200 );
242 $output->redirect( $targetUrl, '301' );
245 } else if( NS_SPECIAL
== $title->getNamespace() ) {
246 /* actions that need to be made when we have a special pages */
247 SpecialPage
::executePath( $title );
249 /* No match to special cases */
250 wfProfileOut( __METHOD__
);
253 /* Did match a special case */
254 wfProfileOut( __METHOD__
);
259 * Create an Article object of the appropriate class for the given page.
261 * @param $title Title
262 * @return Article object
264 static function articleFromTitle( &$title ) {
265 if( NS_MEDIA
== $title->getNamespace() ) {
266 // FIXME: where should this go?
267 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
271 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
276 switch( $title->getNamespace() ) {
278 return new ImagePage( $title );
280 return new CategoryPage( $title );
282 return new Article( $title );
287 * Initialize the object to be known as $wgArticle for "standard" actions
288 * Create an Article object for the page, following redirects if needed.
290 * @param $title Title ($wgTitle)
291 * @param $output OutputPage ($wgOut)
292 * @param $request WebRequest ($wgRequest)
293 * @return mixed an Article, or a string to redirect to another URL
295 function initializeArticle( &$title, &$output, $request ) {
296 wfProfileIn( __METHOD__
);
298 $action = $this->getVal( 'action', 'view' );
299 $article = self
::articleFromTitle( $title );
300 // NS_MEDIAWIKI has no redirects.
301 // It is also used for CSS/JS, so performance matters here...
302 if( $title->getNamespace() == NS_MEDIAWIKI
) {
303 wfProfileOut( __METHOD__
);
306 // Namespace might change when using redirects
307 // Check for redirects ...
308 $file = ($title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
309 if( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
310 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
311 !$request->getVal( 'diff' ) && // ... and not when showing diff
312 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
313 // ... and the article is not a non-redirect image page with associated file
314 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
316 // Give extensions a change to ignore/handle redirects as needed
317 $ignoreRedirect = $target = false;
319 $dbr = wfGetDB( DB_SLAVE
);
320 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
322 wfRunHooks( 'InitializeArticleMaybeRedirect',
323 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
325 // Follow redirects only for... redirects.
326 // If $target is set, then a hook wanted to redirect.
327 if( !$ignoreRedirect && ($target ||
$article->isRedirect()) ) {
328 // Is the target already set by an extension?
329 $target = $target ?
$target : $article->followRedirect();
330 if( is_string( $target ) ) {
331 if( !$this->getVal( 'DisableHardRedirects' ) ) {
332 // we'll need to redirect
333 wfProfileOut( __METHOD__
);
337 if( is_object($target) ) {
338 // Rewrite environment to redirected article
339 $rarticle = self
::articleFromTitle( $target );
340 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
341 if( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
342 $rarticle->setRedirectedFrom( $title );
343 $article = $rarticle;
345 $output->setTitle( $title );
349 $title = $article->getTitle();
352 wfProfileOut( __METHOD__
);
357 * Cleaning up request by doing:
358 ** deferred updates, DB transaction, and the output
360 * @param $deferredUpdates array of updates to do
361 * @param $output OutputPage
363 function finalCleanup( &$deferredUpdates, &$output ) {
364 wfProfileIn( __METHOD__
);
365 // Now commit any transactions, so that unreported errors after
366 // output() don't roll back the whole DB transaction
367 $factory = wfGetLBFactory();
368 $factory->commitMasterChanges();
369 // Output everything!
371 // Do any deferred jobs
372 $this->doUpdates( $deferredUpdates );
373 // Close the session so that jobs don't access the current session
374 session_write_close();
376 wfProfileOut( __METHOD__
);
380 * Deferred updates aren't really deferred anymore. It's important to report
381 * errors to the user, and that means doing this before OutputPage::output().
382 * Note that for page saves, the client will wait until the script exits
383 * anyway before following the redirect.
385 * @param $updates array of objects that hold an update to do
387 function doUpdates( &$updates ) {
388 wfProfileIn( __METHOD__
);
389 /* No need to get master connections in case of empty updates array */
391 wfProfileOut( __METHOD__
);
395 $dbw = wfGetDB( DB_MASTER
);
396 foreach( $updates as $up ) {
399 // Commit after every update to prevent lock contention
400 if( $dbw->trxLevel() ) {
404 wfProfileOut( __METHOD__
);
408 * Do a job from the job queue
411 $jobRunRate = $this->getVal( 'JobRunRate' );
413 if( $jobRunRate <= 0 ||
wfReadOnly() ) {
416 if( $jobRunRate < 1 ) {
417 $max = mt_getrandmax();
418 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
423 $n = intval( $jobRunRate );
426 while ( $n-- && false != ( $job = Job
::pop() ) ) {
427 $output = $job->toString() . "\n";
429 $success = $job->run();
431 $t = round( $t*1000 );
433 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
435 $output .= "Success, Time: $t ms\n";
437 wfDebugLog( 'jobqueue', $output );
442 * Ends this task peacefully
444 function restInPeace() {
445 MessageCache
::logMessages();
446 wfLogProfilingData();
447 // Commit and close up!
448 $factory = wfGetLBFactory();
449 $factory->commitMasterChanges();
450 $factory->shutdown();
451 wfDebug( "Request ended normally\n" );
455 * Perform one of the "standard" actions
457 * @param $output OutputPage
458 * @param $article Article
459 * @param $title Title
461 * @param $request WebRequest
463 function performAction( &$output, &$article, &$title, &$user, &$request ) {
464 wfProfileIn( __METHOD__
);
466 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
467 wfProfileOut( __METHOD__
);
471 $action = $this->getVal( 'Action' );
472 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
473 /* No such action; this will switch to the default case */
474 $action = 'nosuchaction';
477 // Workaround for bug #20966: inability of IE to provide an action dependent
478 // on which submit button is clicked.
479 if ( $action === 'historysubmit' ) {
480 if ( $request->getBool( 'revisiondelete' ) ) {
481 $action = 'revisiondelete';
482 } elseif ( $request->getBool( 'revisionmove' ) ) {
483 $action = 'revisionmove';
491 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
494 case 'raw': // includes JS/CSS
495 wfProfileIn( __METHOD__
.'-raw' );
496 $raw = new RawPage( $article );
498 wfProfileOut( __METHOD__
.'-raw' );
508 case 'markpatrolled':
510 case 'deletetrackback':
518 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
519 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
521 $rdf = new DublinCoreRdf( $article );
525 case 'creativecommons':
526 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
527 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
529 $rdf = new CreativeCommonsRdf( $article );
534 Credits
::showPage( $article );
537 if( session_id() == '' ) {
538 /* Send a cookie so anons get talk message notifications */
544 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
545 $internal = $request->getVal( 'internaledit' );
546 $external = $request->getVal( 'externaledit' );
547 $section = $request->getVal( 'section' );
548 $oldid = $request->getVal( 'oldid' );
549 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
550 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
551 $editor = new EditPage( $article );
553 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
554 $mode = $request->getVal( 'mode' );
555 $extedit = new ExternalEdit( $article, $mode );
561 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
562 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
564 $history = new HistoryPage( $article );
567 case 'revisiondelete':
568 // For show/hide submission from history page
569 $special = SpecialPage
::getPage( 'Revisiondelete' );
570 $special->execute( '' );
573 // For revision move submission from history page
574 $special = SpecialPage
::getPage( 'RevisionMove' );
575 $special->execute( '' );
578 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
579 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
582 wfProfileOut( __METHOD__
);