3 * MediaWiki is the to-be base class for this whole project
7 var $GET; /* Stores the $_GET variables at time of creation, can be changed */
10 /** Constructor. It just save the $_GET variable */
11 function __construct() {
16 * Stores key/value pairs to circumvent global variables
17 * Note that keys are case-insensitive!
19 * @param $key String: key to store
20 * @param $value Mixed: value to put for the key
22 function setVal( $key, &$value ) {
23 $key = strtolower( $key );
24 $this->params[$key] =& $value;
28 * Retrieves key/value pairs to circumvent global variables
29 * Note that keys are case-insensitive!
31 * @param $key String: key to get
32 * @param $default Mixed: default value if if the key doesn't exist
34 function getVal( $key, $default = '' ) {
35 $key = strtolower( $key );
36 if( isset( $this->params[$key] ) ) {
37 return $this->params[$key];
43 * Initialization of ... everything
44 * Performs the request too
45 * FIXME: why is this crap called "initialize" when it performs everything?
47 * @param $title Title ($wgTitle)
48 * @param $article Article
49 * @param $output OutputPage
51 * @param $request WebRequest
53 function initialize( &$title, &$article, &$output, &$user, $request ) {
54 wfProfileIn( __METHOD__ );
55 if( !$this->preliminaryChecks( $title, $output, $request ) ) {
56 wfProfileOut( __METHOD__ );
59 if( !$this->initializeSpecialCases( $title, $output, $request ) ) {
60 $new_article = $this->initializeArticle( $title, $request );
61 if( is_object( $new_article ) ) {
62 $article = $new_article;
63 $this->performAction( $output, $article, $title, $user, $request );
64 } elseif( is_string( $new_article ) ) {
65 $output->redirect( $new_article );
67 wfProfileOut( __METHOD__ );
68 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
71 wfProfileOut( __METHOD__ );
75 * Check if the maximum lag of database slaves is higher that $maxLag, and
76 * if it's the case, output an error message
78 * @param $maxLag int: maximum lag allowed for the request, as supplied by
80 * @return bool true if the request can continue
82 function checkMaxLag( $maxLag ) {
83 list( $host, $lag ) = wfGetLB()->getMaxLag();
84 if( $lag > $maxLag ) {
85 wfMaxlagError( $host, $lag, $maxLag );
93 * Checks some initial queries
94 * Note that $title here is *not* a Title object, but a string!
96 * @param $title String
97 * @param $action String
98 * @return Title object to be $wgTitle
100 function checkInitialQueries( $title, $action ) {
101 global $wgOut, $wgRequest, $wgContLang;
102 if( $wgRequest->getVal( 'printable' ) === 'yes' ) {
103 $wgOut->setPrintable();
106 if( $curid = $wgRequest->getInt( 'curid' ) ) {
107 # URLs like this are generated by RC, because rc_title isn't always accurate
108 $ret = Title::newFromID( $curid );
109 } elseif( '' == $title && 'delete' != $action ) {
110 $ret = Title::newMainPage();
112 $ret = Title::newFromURL( $title );
113 // check variant links so that interwiki links don't have to worry
114 // about the possible different language variants
115 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
116 $wgContLang->findVariantLink( $title, $ret );
118 if( ( $oldid = $wgRequest->getInt( 'oldid' ) )
119 && ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) ) {
120 // Allow oldid to override a changed or missing title.
121 $rev = Revision::newFromId( $oldid );
123 $ret = $rev->getTitle();
130 * Checks for search query and anon-cannot-read case
132 * @param $title Title
133 * @param $output OutputPage
134 * @param $request WebRequest
136 function preliminaryChecks( &$title, &$output, $request ) {
137 if( $request->getCheck( 'search' ) ) {
138 // Compatibility with old search URLs which didn't use Special:Search
139 // Just check for presence here, so blank requests still
140 // show the search page when using ugly URLs (bug 8054).
142 // Do this above the read whitelist check for security...
143 $title = SpecialPage::getTitleFor( 'Search' );
145 # If the user is not logged in, the Namespace:title of the article must be in
146 # the Read array in order for the user to see it. (We have to check here to
147 # catch special pages etc. We check again in Article::view())
148 if( !is_null( $title ) && !$title->userCanRead() ) {
149 $output->loginToUse();
158 * Initialize some special cases:
160 * - local interwiki redirects
164 * FIXME: why is this crap called "initialize" when it performs everything?
166 * @param $title Title
167 * @param $output OutputPage
168 * @param $request WebRequest
169 * @return bool true if the request is already executed
171 function initializeSpecialCases( &$title, &$output, $request ) {
172 wfProfileIn( __METHOD__ );
174 $action = $this->getVal( 'Action' );
175 if( is_null($title) || $title->getDBkey() == '' ) {
176 $title = SpecialPage::getTitleFor( 'Badtitle' );
177 # Die now before we mess up $wgArticle and the skin stops working
178 throw new ErrorPageError( 'badtitle', 'badtitletext' );
179 } else if( $title->getInterwiki() != '' ) {
180 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
181 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
183 $url = $title->getFullURL();
185 /* Check for a redirect loop */
186 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
187 $output->redirect( $url );
189 $title = SpecialPage::getTitleFor( 'Badtitle' );
190 throw new ErrorPageError( 'badtitle', 'badtitletext' );
192 } else if( $action == 'view' && !$request->wasPosted() &&
193 ( !isset($this->GET['title']) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
194 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
196 $targetUrl = $title->getFullURL();
197 // Redirect to canonical url, make it a 301 to allow caching
198 if( $targetUrl == $request->getFullRequestURL() ) {
199 $message = "Redirect loop detected!\n\n" .
200 "This means the wiki got confused about what page was " .
201 "requested; this sometimes happens when moving a wiki " .
202 "to a new server or changing the server configuration.\n\n";
204 if( $this->getVal( 'UsePathInfo' ) ) {
205 $message .= "The wiki is trying to interpret the page " .
206 "title from the URL path portion (PATH_INFO), which " .
207 "sometimes fails depending on the web server. Try " .
208 "setting \"\$wgUsePathInfo = false;\" in your " .
209 "LocalSettings.php, or check that \$wgArticlePath " .
212 $message .= "Your web server was detected as possibly not " .
213 "supporting URL path components (PATH_INFO) correctly; " .
214 "check your LocalSettings.php for a customized " .
215 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
218 wfHttpError( 500, "Internal error", $message );
221 $output->setSquidMaxage( 1200 );
222 $output->redirect( $targetUrl, '301' );
224 } else if( NS_SPECIAL == $title->getNamespace() ) {
225 /* actions that need to be made when we have a special pages */
226 SpecialPage::executePath( $title );
228 /* No match to special cases */
229 wfProfileOut( __METHOD__ );
232 /* Did match a special case */
233 wfProfileOut( __METHOD__ );
238 * Create an Article object of the appropriate class for the given page.
240 * @param $title Title
241 * @return Article object
243 static function articleFromTitle( &$title ) {
244 if( NS_MEDIA == $title->getNamespace() ) {
245 // FIXME: where should this go?
246 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
250 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
255 switch( $title->getNamespace() ) {
257 return new ImagePage( $title );
259 return new CategoryPage( $title );
261 return new Article( $title );
266 * Initialize the object to be known as $wgArticle for "standard" actions
267 * Create an Article object for the page, following redirects if needed.
269 * @param $title Title ($wgTitle)
270 * @param $request WebRequest
271 * @return mixed an Article, or a string to redirect to another URL
273 function initializeArticle( &$title, $request ) {
274 wfProfileIn( __METHOD__ );
276 $action = $this->getVal( 'action', 'view' );
277 $article = self::articleFromTitle( $title );
278 # NS_MEDIAWIKI has no redirects.
279 # It is also used for CSS/JS, so performance matters here...
280 if( $title->getNamespace() == NS_MEDIAWIKI ) {
281 wfProfileOut( __METHOD__ );
284 // Namespace might change when using redirects
285 // Check for redirects ...
286 $file = ($title->getNamespace() == NS_FILE) ? $article->getFile() : null;
287 if( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
288 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
289 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
290 // ... and the article is not a non-redirect image page with associated file
291 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
293 # Give extensions a change to ignore/handle redirects as needed
294 $ignoreRedirect = $target = false;
296 $dbr = wfGetDB( DB_SLAVE );
297 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
299 wfRunHooks( 'InitializeArticleMaybeRedirect',
300 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
302 // Follow redirects only for... redirects
303 if( !$ignoreRedirect && $article->isRedirect() ) {
304 # Is the target already set by an extension?
305 $target = $target ? $target : $article->followRedirect();
306 if( is_string( $target ) ) {
307 if( !$this->getVal( 'DisableHardRedirects' ) ) {
308 // we'll need to redirect
312 if( is_object($target) ) {
313 // Rewrite environment to redirected article
314 $rarticle = self::articleFromTitle( $target );
315 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
316 if( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
317 $rarticle->setRedirectedFrom( $title );
318 $article = $rarticle;
323 $title = $article->getTitle();
326 wfProfileOut( __METHOD__ );
331 * Cleaning up by doing deferred updates, calling LBFactory and doing the output
333 * @param $deferredUpdates array of updates to do
334 * @param $output OutputPage
336 function finalCleanup( &$deferredUpdates, &$output ) {
337 wfProfileIn( __METHOD__ );
338 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
339 $factory = wfGetLBFactory();
340 $factory->commitMasterChanges();
343 # Do any deferred jobs
344 $this->doUpdates( $deferredUpdates );
346 # Commit and close up!
347 $factory->shutdown();
348 wfProfileOut( __METHOD__ );
352 * Deferred updates aren't really deferred anymore. It's important to report
353 * errors to the user, and that means doing this before OutputPage::output().
354 * Note that for page saves, the client will wait until the script exits
355 * anyway before following the redirect.
357 * @param $updates array of objects that hold an update to do
359 function doUpdates( &$updates ) {
360 wfProfileIn( __METHOD__ );
361 /* No need to get master connections in case of empty updates array */
363 wfProfileOut( __METHOD__ );
367 $dbw = wfGetDB( DB_MASTER );
368 foreach( $updates as $up ) {
371 # Commit after every update to prevent lock contention
372 if( $dbw->trxLevel() ) {
376 wfProfileOut( __METHOD__ );
380 * Do a job from the job queue
383 $jobRunRate = $this->getVal( 'JobRunRate' );
385 if( $jobRunRate <= 0 || wfReadOnly() ) {
388 if( $jobRunRate < 1 ) {
389 $max = mt_getrandmax();
390 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
395 $n = intval( $jobRunRate );
398 while ( $n-- && false != ( $job = Job::pop() ) ) {
399 $output = $job->toString() . "\n";
401 $success = $job->run();
403 $t = round( $t*1000 );
405 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
407 $output .= "Success, Time: $t ms\n";
409 wfDebugLog( 'jobqueue', $output );
414 * Ends this task peacefully
416 function restInPeace() {
417 wfLogProfilingData();
418 wfDebug( "Request ended normally\n" );
422 * Perform one of the "standard" actions
424 * @param $output OutputPage
425 * @param $article Article
426 * @param $title Title
428 * @param $request WebRequest
430 function performAction( &$output, &$article, &$title, &$user, &$request ) {
431 wfProfileIn( __METHOD__ );
433 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
434 wfProfileOut( __METHOD__ );
438 $action = $this->getVal( 'Action' );
439 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
440 /* No such action; this will switch to the default case */
441 $action = 'nosuchaction';
446 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
449 case 'raw': // includes JS/CSS
450 $raw = new RawPage( $article );
461 case 'markpatrolled':
463 case 'deletetrackback':
471 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
472 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
474 $rdf = new DublinCoreRdf( $article );
478 case 'creativecommons':
479 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
480 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
482 $rdf = new CreativeCommonsRdf( $article );
487 Credits::showPage( $article );
490 if( session_id() == '' ) {
491 /* Send a cookie so anons get talk message notifications */
497 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
498 $internal = $request->getVal( 'internaledit' );
499 $external = $request->getVal( 'externaledit' );
500 $section = $request->getVal( 'section' );
501 $oldid = $request->getVal( 'oldid' );
502 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
503 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
504 $editor = new EditPage( $article );
506 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
507 $mode = $request->getVal( 'mode' );
508 $extedit = new ExternalEdit( $article, $mode );
514 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
515 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
517 $history = new PageHistory( $article );
521 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
522 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
525 wfProfileOut( __METHOD__ );
529 }; /* End of class MediaWiki */