Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / Wiki.php
blob601a7227d3cb489b865c93655d7ef037b16b285f
1 <?php
2 /**
3 * MediaWiki is the to-be base class for this whole project
4 */
6 class MediaWiki {
8 var $GET; /* Stores the $_GET variables at time of creation, can be changed */
9 var $params = array();
11 /** Constructor. It just save the $_GET variable */
12 function __construct() {
13 $this->GET = $_GET;
16 /**
17 * Stores key/value pairs to circumvent global variables
18 * Note that keys are case-insensitive!
20 function setVal( $key, &$value ) {
21 $key = strtolower( $key );
22 $this->params[$key] =& $value;
25 /**
26 * Retrieves key/value pairs to circumvent global variables
27 * Note that keys are case-insensitive!
29 function getVal( $key, $default = '' ) {
30 $key = strtolower( $key );
31 if( isset( $this->params[$key] ) ) {
32 return $this->params[$key];
34 return $default;
37 /**
38 * Initialization of ... everything
39 @return Article either the object to become $wgArticle, or NULL
41 function initialize ( &$title, &$output, &$user, $request) {
42 wfProfileIn( 'MediaWiki::initialize' );
43 $this->preliminaryChecks ( $title, $output, $request ) ;
44 $article = NULL;
45 if ( !$this->initializeSpecialCases( $title, $output, $request ) ) {
46 $article = $this->initializeArticle( $title, $request );
47 if( is_object( $article ) ) {
48 $this->performAction( $output, $article, $title, $user, $request );
49 } elseif( is_string( $article ) ) {
50 $output->redirect( $article );
51 } else {
52 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
55 wfProfileOut( 'MediaWiki::initialize' );
56 return $article;
59 function checkMaxLag( $maxLag ) {
60 global $wgLoadBalancer;
61 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
62 if ( $lag > $maxLag ) {
63 header( 'HTTP/1.1 503 Service Unavailable' );
64 header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
65 header( 'X-Database-Lag: ' . intval( $lag ) );
66 header( 'Content-Type: text/plain' );
67 echo "Waiting for $host: $lag seconds lagged\n";
68 return false;
69 } else {
70 return true;
75 /**
76 * Checks some initial queries
77 * Note that $title here is *not* a Title object, but a string!
79 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
80 if ($request->getVal( 'printable' ) == 'yes') {
81 $output->setPrintable();
84 $ret = NULL ;
87 if ( '' == $title && 'delete' != $action ) {
88 $ret = Title::newMainPage();
89 } elseif ( $curid = $request->getInt( 'curid' ) ) {
90 # URLs like this are generated by RC, because rc_title isn't always accurate
91 $ret = Title::newFromID( $curid );
92 } else {
93 $ret = Title::newFromURL( $title );
94 /* check variant links so that interwiki links don't have to worry about
95 the possible different language variants
97 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
98 $lang->findVariantLink( $title, $ret );
101 return $ret ;
105 * Checks for search query and anon-cannot-read case
107 function preliminaryChecks ( &$title, &$output, $request ) {
109 # Debug statement for user levels
110 // print_r($wgUser);
112 $search = $request->getText( 'search' );
113 if( !is_null( $search ) && $search !== '' ) {
114 // Compatibility with old search URLs which didn't use Special:Search
115 // Do this above the read whitelist check for security...
116 $title = SpecialPage::getTitleFor( 'Search' );
118 $this->setVal( 'Search', $search );
120 # If the user is not logged in, the Namespace:title of the article must be in
121 # the Read array in order for the user to see it. (We have to check here to
122 # catch special pages etc. We check again in Article::view())
123 if ( !is_null( $title ) && !$title->userCanRead() ) {
124 $output->loginToUse();
125 $output->output();
126 exit;
132 * Initialize the object to be known as $wgArticle for special cases
134 function initializeSpecialCases ( &$title, &$output, $request ) {
135 global $wgRequest;
136 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
138 $search = $this->getVal('Search');
139 $action = $this->getVal('Action');
140 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
141 require_once( 'includes/SpecialSearch.php' );
142 $title = SpecialPage::getTitleFor( 'Search' );
143 wfSpecialSearch();
144 } else if( !$title or $title->getDBkey() == '' ) {
145 $title = SpecialPage::getTitleFor( 'Badtitle' );
146 # Die now before we mess up $wgArticle and the skin stops working
147 throw new ErrorPageError( 'badtitle', 'badtitletext' );
148 } else if ( $title->getInterwiki() != '' ) {
149 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
150 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
151 } else {
152 $url = $title->getFullURL();
154 /* Check for a redirect loop */
155 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
156 $output->redirect( $url );
157 } else {
158 $title = SpecialPage::getTitleFor( 'Badtitle' );
159 throw new ErrorPageError( 'badtitle', 'badtitletext' );
161 } else if ( ( $action == 'view' ) && !$wgRequest->wasPosted() &&
162 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
163 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
165 $targetUrl = $title->getFullURL();
166 // Redirect to canonical url, make it a 301 to allow caching
167 global $wgUsePathInfo;
168 if( $targetUrl == $wgRequest->getFullRequestURL() ) {
169 $message = "Redirect loop detected!\n\n" .
170 "This means the wiki got confused about what page was " .
171 "requested; this sometimes happens when moving a wiki " .
172 "to a new server or changing the server configuration.\n\n";
174 if( $wgUsePathInfo ) {
175 $message .= "The wiki is trying to interpret the page " .
176 "title from the URL path portion (PATH_INFO), which " .
177 "sometimes fails depending on the web server. Try " .
178 "setting \"\$wgUsePathInfo = false;\" in your " .
179 "LocalSettings.php, or check that \$wgArticlePath " .
180 "is correct.";
181 } else {
182 $message .= "Your web server was detected as possibly not " .
183 "supporting URL path components (PATH_INFO) correctly; " .
184 "check your LocalSettings.php for a customized " .
185 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
186 "to true.";
188 wfHttpError( 500, "Internal error", $message );
189 return false;
190 } else {
191 $output->setSquidMaxage( 1200 );
192 $output->redirect( $targetUrl, '301');
194 } else if ( NS_SPECIAL == $title->getNamespace() ) {
195 /* actions that need to be made when we have a special pages */
196 SpecialPage::executePath( $title );
197 } else {
198 /* No match to special cases */
199 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
200 return false;
202 /* Did match a special case */
203 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
204 return true;
208 * Create an Article object of the appropriate class for the given page.
209 * @param Title $title
210 * @return Article
212 function articleFromTitle( $title ) {
213 $article = null;
214 wfRunHooks('ArticleFromTitle', array( &$title, &$article ) );
215 if ( $article ) {
216 return $article;
219 if( NS_MEDIA == $title->getNamespace() ) {
220 // FIXME: where should this go?
221 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
224 switch( $title->getNamespace() ) {
225 case NS_IMAGE:
226 return new ImagePage( $title );
227 case NS_CATEGORY:
228 return new CategoryPage( $title );
229 default:
230 return new Article( $title );
235 * Initialize the object to be known as $wgArticle for "standard" actions
236 * Create an Article object for the page, following redirects if needed.
237 * @param Title $title
238 * @param Request $request
239 * @param string $action
240 * @return mixed an Article, or a string to redirect to another URL
242 function initializeArticle( $title, $request ) {
243 global $wgTitle;
244 wfProfileIn( 'MediaWiki::initializeArticle' );
246 $action = $this->getVal('Action');
247 $article = $this->articleFromTitle( $title );
249 // Namespace might change when using redirects
250 if( $action == 'view' && !$request->getVal( 'oldid' ) &&
251 $request->getVal( 'redirect' ) != 'no' ) {
253 $dbr = wfGetDB(DB_SLAVE);
254 $article->loadPageData($article->pageDataFromTitle($dbr, $title));
256 /* Follow redirects only for... redirects */
257 if ($article->mIsRedirect) {
258 $target = $article->followRedirect();
259 if( is_string( $target ) ) {
260 global $wgDisableHardRedirects;
261 if( !$wgDisableHardRedirects ) {
262 // we'll need to redirect
263 return $target;
266 if( is_object( $target ) ) {
267 /* Rewrite environment to redirected article */
268 $rarticle = $this->articleFromTitle($target);
269 $rarticle->loadPageData($rarticle->pageDataFromTitle($dbr,$target));
270 if ($rarticle->mTitle->mArticleID) {
271 $article = $rarticle;
272 $wgTitle = $target;
273 $article->setRedirectedFrom( $title );
274 } else {
275 $wgTitle = $title;
278 } else {
279 $wgTitle = $article->mTitle;
282 wfProfileOut( 'MediaWiki::initializeArticle' );
283 return $article;
287 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
289 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
290 wfProfileIn( 'MediaWiki::finalCleanup' );
291 $this->doUpdates( $deferredUpdates );
292 $this->doJobs();
293 $loadBalancer->saveMasterPos();
294 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
295 $loadBalancer->commitAll();
296 $output->output();
297 wfProfileOut( 'MediaWiki::finalCleanup' );
301 * Deferred updates aren't really deferred anymore. It's important to report errors to the
302 * user, and that means doing this before OutputPage::output(). Note that for page saves,
303 * the client will wait until the script exits anyway before following the redirect.
305 function doUpdates ( &$updates ) {
306 wfProfileIn( 'MediaWiki::doUpdates' );
307 $dbw = wfGetDB( DB_MASTER );
308 foreach( $updates as $up ) {
309 $up->doUpdate();
311 # Commit after every update to prevent lock contention
312 if ( $dbw->trxLevel() ) {
313 $dbw->commit();
316 wfProfileOut( 'MediaWiki::doUpdates' );
320 * Do a job from the job queue
322 function doJobs() {
323 global $wgJobRunRate;
325 if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
326 return;
328 if ( $wgJobRunRate < 1 ) {
329 $max = mt_getrandmax();
330 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
331 return;
333 $n = 1;
334 } else {
335 $n = intval( $wgJobRunRate );
338 while ( $n-- && false != ($job = Job::pop())) {
339 $output = $job->toString() . "\n";
340 $t = -wfTime();
341 $success = $job->run();
342 $t += wfTime();
343 $t = round( $t*1000 );
344 if ( !$success ) {
345 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
346 } else {
347 $output .= "Success, Time: $t ms\n";
349 wfDebugLog( 'jobqueue', $output );
354 * Ends this task peacefully
356 function restInPeace ( &$loadBalancer ) {
357 wfLogProfilingData();
358 $loadBalancer->closeAll();
359 wfDebug( "Request ended normally\n" );
363 * Perform one of the "standard" actions
365 function performAction( &$output, &$article, &$title, &$user, &$request ) {
367 wfProfileIn( 'MediaWiki::performAction' );
369 $action = $this->getVal('Action');
370 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
371 /* No such action; this will switch to the default case */
372 $action = 'nosuchaction';
375 switch( $action ) {
376 case 'view':
377 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
378 $article->view();
379 break;
380 case 'watch':
381 case 'unwatch':
382 case 'delete':
383 case 'revert':
384 case 'rollback':
385 case 'protect':
386 case 'unprotect':
387 case 'info':
388 case 'markpatrolled':
389 case 'render':
390 case 'deletetrackback':
391 case 'purge':
392 $article->$action();
393 break;
394 case 'print':
395 $article->view();
396 break;
397 case 'dublincore':
398 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
399 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
400 } else {
401 require_once( 'includes/Metadata.php' );
402 wfDublinCoreRdf( $article );
404 break;
405 case 'creativecommons':
406 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
407 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
408 } else {
409 require_once( 'includes/Metadata.php' );
410 wfCreativeCommonsRdf( $article );
412 break;
413 case 'credits':
414 require_once( 'includes/Credits.php' );
415 showCreditsPage( $article );
416 break;
417 case 'submit':
418 if( session_id() == '' ) {
419 /* Send a cookie so anons get talk message notifications */
420 wfSetupSession();
422 /* Continue... */
423 case 'edit':
424 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
425 $internal = $request->getVal( 'internaledit' );
426 $external = $request->getVal( 'externaledit' );
427 $section = $request->getVal( 'section' );
428 $oldid = $request->getVal( 'oldid' );
429 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
430 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
431 $editor = new EditPage( $article );
432 $editor->submit();
433 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
434 $mode = $request->getVal( 'mode' );
435 $extedit = new ExternalEdit( $article, $mode );
436 $extedit->edit();
439 break;
440 case 'history':
441 global $wgRequest;
442 if( $wgRequest->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
443 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
445 $history = new PageHistory( $article );
446 $history->history();
447 break;
448 case 'raw':
449 $raw = new RawPage( $article );
450 $raw->view();
451 break;
452 default:
453 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
454 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
457 wfProfileOut( 'MediaWiki::performAction' );
461 }; /* End of class MediaWiki */