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 // No valid variant in URL (if the main-language has multi-variants), to ensure
214 // anonymous access would always be redirect to a URL with 'variant' parameter
215 ( !$request->getVal( 'variant' ) && $wgContLang->hasVariants() && !$wgUser->isLoggedIn() ) ) &&
216 !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
218 if( !$wgUser->isLoggedIn() ) {
219 $pref = $wgContLang->getPreferredVariant( false, $fromHeader = true );
220 $targetUrl = $title->getFullURL( '', $variant = $pref );
223 $targetUrl = $title->getFullURL();
224 // Redirect to canonical url, make it a 301 to allow caching
225 if( $targetUrl == $request->getFullRequestURL() ) {
226 $message = "Redirect loop detected!\n\n" .
227 "This means the wiki got confused about what page was " .
228 "requested; this sometimes happens when moving a wiki " .
229 "to a new server or changing the server configuration.\n\n";
231 if( $this->getVal( 'UsePathInfo' ) ) {
232 $message .= "The wiki is trying to interpret the page " .
233 "title from the URL path portion (PATH_INFO), which " .
234 "sometimes fails depending on the web server. Try " .
235 "setting \"\$wgUsePathInfo = false;\" in your " .
236 "LocalSettings.php, or check that \$wgArticlePath " .
239 $message .= "Your web server was detected as possibly not " .
240 "supporting URL path components (PATH_INFO) correctly; " .
241 "check your LocalSettings.php for a customized " .
242 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
245 wfHttpError( 500, "Internal error", $message );
246 wfProfileOut( __METHOD__
);
249 $output->setSquidMaxage( 1200 );
250 $output->redirect( $targetUrl, '301' );
253 } else if( NS_SPECIAL
== $title->getNamespace() ) {
254 /* actions that need to be made when we have a special pages */
255 SpecialPage
::executePath( $title );
257 /* No match to special cases */
258 wfProfileOut( __METHOD__
);
261 /* Did match a special case */
262 wfProfileOut( __METHOD__
);
267 * Create an Article object of the appropriate class for the given page.
269 * @param $title Title
270 * @return Article object
272 static function articleFromTitle( &$title ) {
273 if( NS_MEDIA
== $title->getNamespace() ) {
274 // FIXME: where should this go?
275 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
279 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
284 switch( $title->getNamespace() ) {
286 return new ImagePage( $title );
288 return new CategoryPage( $title );
290 return new Article( $title );
295 * Initialize the object to be known as $wgArticle for "standard" actions
296 * Create an Article object for the page, following redirects if needed.
298 * @param $title Title ($wgTitle)
299 * @param $output OutputPage ($wgOut)
300 * @param $request WebRequest ($wgRequest)
301 * @return mixed an Article, or a string to redirect to another URL
303 function initializeArticle( &$title, &$output, $request ) {
304 wfProfileIn( __METHOD__
);
306 $action = $this->getVal( 'action', 'view' );
307 $article = self
::articleFromTitle( $title );
308 // NS_MEDIAWIKI has no redirects.
309 // It is also used for CSS/JS, so performance matters here...
310 if( $title->getNamespace() == NS_MEDIAWIKI
) {
311 wfProfileOut( __METHOD__
);
314 // Namespace might change when using redirects
315 // Check for redirects ...
316 $file = ($title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
317 if( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
318 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
319 !$request->getVal( 'diff' ) && // ... and not when showing diff
320 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
321 // ... and the article is not a non-redirect image page with associated file
322 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
324 // Give extensions a change to ignore/handle redirects as needed
325 $ignoreRedirect = $target = false;
327 $dbr = wfGetDB( DB_SLAVE
);
328 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
330 wfRunHooks( 'InitializeArticleMaybeRedirect',
331 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
333 // Follow redirects only for... redirects.
334 // If $target is set, then a hook wanted to redirect.
335 if( !$ignoreRedirect && ($target ||
$article->isRedirect()) ) {
336 // Is the target already set by an extension?
337 $target = $target ?
$target : $article->followRedirect();
338 if( is_string( $target ) ) {
339 if( !$this->getVal( 'DisableHardRedirects' ) ) {
340 // we'll need to redirect
341 wfProfileOut( __METHOD__
);
345 if( is_object($target) ) {
346 // Rewrite environment to redirected article
347 $rarticle = self
::articleFromTitle( $target );
348 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
349 if( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
350 $rarticle->setRedirectedFrom( $title );
351 $article = $rarticle;
353 $output->setTitle( $title );
357 $title = $article->getTitle();
360 wfProfileOut( __METHOD__
);
365 * Cleaning up request by doing:
366 ** deferred updates, DB transaction, and the output
368 * @param $deferredUpdates array of updates to do
369 * @param $output OutputPage
371 function finalCleanup( &$deferredUpdates, &$output ) {
372 wfProfileIn( __METHOD__
);
373 // Now commit any transactions, so that unreported errors after
374 // output() don't roll back the whole DB transaction
375 $factory = wfGetLBFactory();
376 $factory->commitMasterChanges();
377 // Output everything!
379 // Do any deferred jobs
380 $this->doUpdates( $deferredUpdates );
381 // Close the session so that jobs don't access the current session
382 session_write_close();
384 wfProfileOut( __METHOD__
);
388 * Deferred updates aren't really deferred anymore. It's important to report
389 * errors to the user, and that means doing this before OutputPage::output().
390 * Note that for page saves, the client will wait until the script exits
391 * anyway before following the redirect.
393 * @param $updates array of objects that hold an update to do
395 function doUpdates( &$updates ) {
396 wfProfileIn( __METHOD__
);
397 /* No need to get master connections in case of empty updates array */
399 wfProfileOut( __METHOD__
);
403 $dbw = wfGetDB( DB_MASTER
);
404 foreach( $updates as $up ) {
407 // Commit after every update to prevent lock contention
408 if( $dbw->trxLevel() ) {
412 wfProfileOut( __METHOD__
);
416 * Do a job from the job queue
419 $jobRunRate = $this->getVal( 'JobRunRate' );
421 if( $jobRunRate <= 0 ||
wfReadOnly() ) {
424 if( $jobRunRate < 1 ) {
425 $max = mt_getrandmax();
426 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
431 $n = intval( $jobRunRate );
434 while ( $n-- && false != ( $job = Job
::pop() ) ) {
435 $output = $job->toString() . "\n";
437 $success = $job->run();
439 $t = round( $t*1000 );
441 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
443 $output .= "Success, Time: $t ms\n";
445 wfDebugLog( 'jobqueue', $output );
450 * Ends this task peacefully
452 function restInPeace() {
453 MessageCache
::logMessages();
454 wfLogProfilingData();
455 // Commit and close up!
456 $factory = wfGetLBFactory();
457 $factory->commitMasterChanges();
458 $factory->shutdown();
459 wfDebug( "Request ended normally\n" );
463 * Perform one of the "standard" actions
465 * @param $output OutputPage
466 * @param $article Article
467 * @param $title Title
469 * @param $request WebRequest
471 function performAction( &$output, &$article, &$title, &$user, &$request ) {
472 wfProfileIn( __METHOD__
);
474 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
475 wfProfileOut( __METHOD__
);
479 $action = $this->getVal( 'Action' );
480 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
481 /* No such action; this will switch to the default case */
482 $action = 'nosuchaction';
485 // Workaround for bug #20966: inability of IE to provide an action dependent
486 // on which submit button is clicked.
487 if ( $action === 'historysubmit' ) {
488 if ( $request->getBool( 'revisiondelete' ) ) {
489 $action = 'revisiondelete';
490 } elseif ( $request->getBool( 'revisionmove' ) ) {
491 $action = 'revisionmove';
499 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
502 case 'raw': // includes JS/CSS
503 wfProfileIn( __METHOD__
.'-raw' );
504 $raw = new RawPage( $article );
506 wfProfileOut( __METHOD__
.'-raw' );
516 case 'markpatrolled':
518 case 'deletetrackback':
526 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
527 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
529 $rdf = new DublinCoreRdf( $article );
533 case 'creativecommons':
534 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
535 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
537 $rdf = new CreativeCommonsRdf( $article );
542 Credits
::showPage( $article );
545 if( session_id() == '' ) {
546 /* Send a cookie so anons get talk message notifications */
552 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
553 $internal = $request->getVal( 'internaledit' );
554 $external = $request->getVal( 'externaledit' );
555 $section = $request->getVal( 'section' );
556 $oldid = $request->getVal( 'oldid' );
557 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
558 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
559 $editor = new EditPage( $article );
561 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
562 $mode = $request->getVal( 'mode' );
563 $extedit = new ExternalEdit( $article, $mode );
569 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
570 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
572 $history = new HistoryPage( $article );
575 case 'revisiondelete':
576 // For show/hide submission from history page
577 $special = SpecialPage
::getPage( 'Revisiondelete' );
578 $special->execute( '' );
581 // For revision move submission from history page
582 $special = SpecialPage
::getPage( 'RevisionMove' );
583 $special->execute( '' );
586 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
587 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
590 wfProfileOut( __METHOD__
);