3 * MediaWiki is the to-be base class for this whole project
5 * @internal documentation reviewed 15 Mar 2010
11 * Stores key/value pairs to circumvent global variables
12 * Note that keys are case-insensitive!
14 * @param $key String: key to store
15 * @param $value Mixed: value to put for the key
17 function setVal( $key, &$value ) {
18 $key = strtolower( $key );
19 $this->params
[$key] =& $value;
23 * Retrieves key/value pairs to circumvent global variables
24 * Note that keys are case-insensitive!
26 * @param $key String: key to get
27 * @param $default string default value, defaults to empty string
28 * @return $default Mixed: default value if if the key doesn't exist
30 function getVal( $key, $default = '' ) {
31 $key = strtolower( $key );
32 if( isset( $this->params
[$key] ) ) {
33 return $this->params
[$key];
39 * Initialization of ... everything
40 * Performs the request too
42 * @param $title Title ($wgTitle)
43 * @param $article Article
44 * @param $output OutputPage
46 * @param $request WebRequest
48 function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) {
49 wfProfileIn( __METHOD__
);
51 $output->setTitle( $title );
53 wfRunHooks( 'BeforeInitialize', array( &$title, &$article, &$output, &$user, $request, $this ) );
55 if( !$this->preliminaryChecks( $title, $output ) ) {
56 wfProfileOut( __METHOD__
);
59 // Call handleSpecialCases() to deal with all special requests...
60 if( !$this->handleSpecialCases( $title, $output, $request ) ) {
61 // ...otherwise treat it as an article view. The article
62 // may be a redirect to another article or URL.
63 $new_article = $this->initializeArticle( $title, $output, $request );
64 if( is_object( $new_article ) ) {
65 $article = $new_article;
66 $this->performAction( $output, $article, $title, $user, $request );
67 } elseif( is_string( $new_article ) ) {
68 $output->redirect( $new_article );
70 wfProfileOut( __METHOD__
);
71 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
74 wfProfileOut( __METHOD__
);
78 * Check if the maximum lag of database slaves is higher that $maxLag, and
79 * if it's the case, output an error message
81 * @param $maxLag int: maximum lag allowed for the request, as supplied by
83 * @return bool true if the request can continue
85 function checkMaxLag( $maxLag ) {
86 list( $host, $lag ) = wfGetLB()->getMaxLag();
87 if( $lag > $maxLag ) {
88 wfMaxlagError( $host, $lag, $maxLag );
96 * Checks some initial queries
97 * Note that $title here is *not* a Title object, but a string!
99 * @param $title String
100 * @param $action String
101 * @return Title object to be $wgTitle
103 function checkInitialQueries( $title, $action ) {
104 global $wgOut, $wgRequest, $wgContLang;
105 if( $wgRequest->getVal( 'printable' ) === 'yes' ) {
106 $wgOut->setPrintable();
109 $curid = $wgRequest->getInt( 'curid' );
110 if( $wgRequest->getCheck( 'search' ) ) {
111 // Compatibility with old search URLs which didn't use Special:Search
112 // Just check for presence here, so blank requests still
113 // show the search page when using ugly URLs (bug 8054).
114 $ret = SpecialPage
::getTitleFor( 'Search' );
116 // URLs like this are generated by RC, because rc_title isn't always accurate
117 $ret = Title
::newFromID( $curid );
118 } elseif( $title == '' && $action != 'delete' ) {
119 $ret = Title
::newMainPage();
121 $ret = Title
::newFromURL( $title );
122 // check variant links so that interwiki links don't have to worry
123 // about the possible different language variants
124 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
125 $wgContLang->findVariantLink( $title, $ret );
127 // For non-special titles, check for implicit titles
128 if( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
129 // We can have urls with just ?diff=,?oldid= or even just ?diff=
130 $oldid = $wgRequest->getInt( 'oldid' );
131 $oldid = $oldid ?
$oldid : $wgRequest->getInt( 'diff' );
132 // Allow oldid to override a changed or missing title
134 $rev = Revision
::newFromId( $oldid );
135 $ret = $rev ?
$rev->getTitle() : $ret;
142 * Checks for anon-cannot-read case
144 * @param $title Title
145 * @param $output OutputPage
146 * @return boolean true if successful
148 function preliminaryChecks( &$title, &$output ) {
149 // If the user is not logged in, the Namespace:title of the article must be in
150 // the Read array in order for the user to see it. (We have to check here to
151 // catch special pages etc. We check again in Article::view())
152 if( !is_null( $title ) && !$title->userCanRead() ) {
153 $output->loginToUse();
154 $this->finalCleanup( $output );
162 * Initialize some special cases:
164 * - local interwiki redirects
168 * @param $title Title
169 * @param $output OutputPage
170 * @param $request WebRequest
171 * @return bool true if the request is already executed
173 function handleSpecialCases( &$title, &$output, $request ) {
174 wfProfileIn( __METHOD__
);
176 $action = $this->getVal( 'Action' );
178 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
179 if( is_null($title) ||
( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
180 $title = SpecialPage
::getTitleFor( 'Badtitle' );
181 $output->setTitle( $title ); // bug 21456
182 // Die now before we mess up $wgArticle and the skin stops working
183 throw new ErrorPageError( 'badtitle', 'badtitletext' );
185 // Interwiki redirects
186 } else if( $title->getInterwiki() != '' ) {
187 $rdfrom = $request->getVal( 'rdfrom' );
189 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
191 $query = $request->getValues();
192 unset( $query['title'] );
193 $url = $title->getFullURL( $query );
195 /* Check for a redirect loop */
196 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
197 $output->redirect( $url );
199 $title = SpecialPage
::getTitleFor( 'Badtitle' );
200 $output->setTitle( $title ); // bug 21456
201 wfProfileOut( __METHOD__
);
202 throw new ErrorPageError( 'badtitle', 'badtitletext' );
204 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
205 } else if ( $action == 'view' && !$request->wasPosted()
206 && ( $request->getVal( 'title' ) === null ||
$title->getPrefixedDBKey() != $request->getText( 'title' ) )
207 && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
209 if ( $title->getNamespace() == NS_SPECIAL
) {
210 list( $name, $subpage ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBkey() );
212 $title = SpecialPage
::getTitleFor( $name, $subpage );
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 $output OutputPage
362 function finalCleanup( &$output ) {
363 wfProfileIn( __METHOD__
);
364 // Now commit any transactions, so that unreported errors after
365 // output() don't roll back the whole DB transaction
366 $factory = wfGetLBFactory();
367 $factory->commitMasterChanges();
368 // Output everything!
370 // Do any deferred jobs
371 wfDoUpdates( 'commit' );
372 // Close the session so that jobs don't access the current session
373 session_write_close();
375 wfProfileOut( __METHOD__
);
379 * Do a job from the job queue
382 global $wgJobRunRate;
384 if( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
387 if( $wgJobRunRate < 1 ) {
388 $max = mt_getrandmax();
389 if( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
394 $n = intval( $wgJobRunRate );
397 while ( $n-- && false != ( $job = Job
::pop() ) ) {
398 $output = $job->toString() . "\n";
400 $success = $job->run();
402 $t = round( $t*1000 );
404 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
406 $output .= "Success, Time: $t ms\n";
408 wfDebugLog( 'jobqueue', $output );
413 * Ends this task peacefully
415 function restInPeace() {
416 MessageCache
::logMessages();
417 wfLogProfilingData();
418 // Commit and close up!
419 $factory = wfGetLBFactory();
420 $factory->commitMasterChanges();
421 $factory->shutdown();
422 wfDebug( "Request ended normally\n" );
426 * Perform one of the "standard" actions
428 * @param $output OutputPage
429 * @param $article Article
430 * @param $title Title
432 * @param $request WebRequest
434 function performAction( &$output, &$article, &$title, &$user, &$request ) {
435 wfProfileIn( __METHOD__
);
437 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
438 wfProfileOut( __METHOD__
);
442 $action = $this->getVal( 'Action' );
443 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
444 /* No such action; this will switch to the default case */
445 $action = 'nosuchaction';
448 // Workaround for bug #20966: inability of IE to provide an action dependent
449 // on which submit button is clicked.
450 if ( $action === 'historysubmit' ) {
451 if ( $request->getBool( 'revisiondelete' ) ) {
452 $action = 'revisiondelete';
453 } elseif ( $request->getBool( 'revisionmove' ) ) {
454 $action = 'revisionmove';
462 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
465 case 'raw': // includes JS/CSS
466 wfProfileIn( __METHOD__
.'-raw' );
467 $raw = new RawPage( $article );
469 wfProfileOut( __METHOD__
.'-raw' );
479 case 'markpatrolled':
481 case 'deletetrackback':
489 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
490 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
492 $rdf = new DublinCoreRdf( $article );
496 case 'creativecommons':
497 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
498 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
500 $rdf = new CreativeCommonsRdf( $article );
505 Credits
::showPage( $article );
508 if( session_id() == '' ) {
509 /* Send a cookie so anons get talk message notifications */
515 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
516 $internal = $request->getVal( 'internaledit' );
517 $external = $request->getVal( 'externaledit' );
518 $section = $request->getVal( 'section' );
519 $oldid = $request->getVal( 'oldid' );
520 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
521 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
522 $editor = new EditPage( $article );
524 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
525 $mode = $request->getVal( 'mode' );
526 $extedit = new ExternalEdit( $article, $mode );
532 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
533 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
535 $history = new HistoryPage( $article );
538 case 'revisiondelete':
539 // For show/hide submission from history page
540 $special = SpecialPage
::getPage( 'Revisiondelete' );
541 $special->execute( '' );
544 // For revision move submission from history page
545 $special = SpecialPage
::getPage( 'RevisionMove' );
546 $special->execute( '' );
549 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
550 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
553 wfProfileOut( __METHOD__
);