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 # For non-special titles, check for implicit titles
119 if( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
120 // We can have urls with just ?diff=,?oldid= or even just ?diff=
121 $oldid = $wgRequest->getInt( 'oldid' );
122 $oldid = $oldid ?
$oldid : $wgRequest->getInt( 'diff' );
123 // Allow oldid to override a changed or missing title
125 $rev = Revision
::newFromId( $oldid );
126 $ret = $rev ?
$rev->getTitle() : $ret;
133 * Checks for search query and anon-cannot-read case
135 * @param $title Title
136 * @param $output OutputPage
137 * @param $request WebRequest
139 function preliminaryChecks( &$title, &$output, $request ) {
140 if( $request->getCheck( 'search' ) ) {
141 // Compatibility with old search URLs which didn't use Special:Search
142 // Just check for presence here, so blank requests still
143 // show the search page when using ugly URLs (bug 8054).
145 // Do this above the read whitelist check for security...
146 $title = SpecialPage
::getTitleFor( 'Search' );
148 # If the user is not logged in, the Namespace:title of the article must be in
149 # the Read array in order for the user to see it. (We have to check here to
150 # catch special pages etc. We check again in Article::view())
151 if( !is_null( $title ) && !$title->userCanRead() ) {
152 $output->loginToUse();
161 * Initialize some special cases:
163 * - local interwiki redirects
167 * FIXME: why is this crap called "initialize" when it performs everything?
169 * @param $title Title
170 * @param $output OutputPage
171 * @param $request WebRequest
172 * @return bool true if the request is already executed
174 function initializeSpecialCases( &$title, &$output, $request ) {
175 wfProfileIn( __METHOD__
);
177 $action = $this->getVal( 'Action' );
178 if( is_null($title) ||
$title->getDBkey() == '' ) {
179 $title = SpecialPage
::getTitleFor( 'Badtitle' );
180 # Die now before we mess up $wgArticle and the skin stops working
181 throw new ErrorPageError( 'badtitle', 'badtitletext' );
182 } else if( $title->getInterwiki() != '' ) {
183 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
184 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
186 $url = $title->getFullURL();
188 /* Check for a redirect loop */
189 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
190 $output->redirect( $url );
192 $title = SpecialPage
::getTitleFor( 'Badtitle' );
193 throw new ErrorPageError( 'badtitle', 'badtitletext' );
195 } else if( $action == 'view' && !$request->wasPosted() &&
196 ( !isset($this->GET
['title']) ||
$title->getPrefixedDBKey() != $this->GET
['title'] ) &&
197 !count( array_diff( array_keys( $this->GET
), array( 'action', 'title' ) ) ) )
199 $targetUrl = $title->getFullURL();
200 // Redirect to canonical url, make it a 301 to allow caching
201 if( $targetUrl == $request->getFullRequestURL() ) {
202 $message = "Redirect loop detected!\n\n" .
203 "This means the wiki got confused about what page was " .
204 "requested; this sometimes happens when moving a wiki " .
205 "to a new server or changing the server configuration.\n\n";
207 if( $this->getVal( 'UsePathInfo' ) ) {
208 $message .= "The wiki is trying to interpret the page " .
209 "title from the URL path portion (PATH_INFO), which " .
210 "sometimes fails depending on the web server. Try " .
211 "setting \"\$wgUsePathInfo = false;\" in your " .
212 "LocalSettings.php, or check that \$wgArticlePath " .
215 $message .= "Your web server was detected as possibly not " .
216 "supporting URL path components (PATH_INFO) correctly; " .
217 "check your LocalSettings.php for a customized " .
218 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
221 wfHttpError( 500, "Internal error", $message );
224 $output->setSquidMaxage( 1200 );
225 $output->redirect( $targetUrl, '301' );
227 } else if( NS_SPECIAL
== $title->getNamespace() ) {
228 /* actions that need to be made when we have a special pages */
229 SpecialPage
::executePath( $title );
231 /* No match to special cases */
232 wfProfileOut( __METHOD__
);
235 /* Did match a special case */
236 wfProfileOut( __METHOD__
);
241 * Create an Article object of the appropriate class for the given page.
243 * @param $title Title
244 * @return Article object
246 static function articleFromTitle( &$title ) {
247 if( NS_MEDIA
== $title->getNamespace() ) {
248 // FIXME: where should this go?
249 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
253 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
258 switch( $title->getNamespace() ) {
260 return new ImagePage( $title );
262 return new CategoryPage( $title );
264 return new Article( $title );
269 * Initialize the object to be known as $wgArticle for "standard" actions
270 * Create an Article object for the page, following redirects if needed.
272 * @param $title Title ($wgTitle)
273 * @param $request WebRequest
274 * @return mixed an Article, or a string to redirect to another URL
276 function initializeArticle( &$title, $request ) {
277 wfProfileIn( __METHOD__
);
279 $action = $this->getVal( 'action', 'view' );
280 $article = self
::articleFromTitle( $title );
281 # NS_MEDIAWIKI has no redirects.
282 # It is also used for CSS/JS, so performance matters here...
283 if( $title->getNamespace() == NS_MEDIAWIKI
) {
284 wfProfileOut( __METHOD__
);
287 // Namespace might change when using redirects
288 // Check for redirects ...
289 $file = ($title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
290 if( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
291 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
292 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
293 // ... and the article is not a non-redirect image page with associated file
294 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
296 # Give extensions a change to ignore/handle redirects as needed
297 $ignoreRedirect = $target = false;
299 $dbr = wfGetDB( DB_SLAVE
);
300 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
302 wfRunHooks( 'InitializeArticleMaybeRedirect',
303 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
305 // Follow redirects only for... redirects
306 if( !$ignoreRedirect && $article->isRedirect() ) {
307 # Is the target already set by an extension?
308 $target = $target ?
$target : $article->followRedirect();
309 if( is_string( $target ) ) {
310 if( !$this->getVal( 'DisableHardRedirects' ) ) {
311 // we'll need to redirect
315 if( is_object($target) ) {
316 // Rewrite environment to redirected article
317 $rarticle = self
::articleFromTitle( $target );
318 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
319 if( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
320 $rarticle->setRedirectedFrom( $title );
321 $article = $rarticle;
326 $title = $article->getTitle();
329 wfProfileOut( __METHOD__
);
334 * Cleaning up by doing deferred updates, calling LBFactory and doing the output
336 * @param $deferredUpdates array of updates to do
337 * @param $output OutputPage
339 function finalCleanup( &$deferredUpdates, &$output ) {
340 wfProfileIn( __METHOD__
);
341 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
342 $factory = wfGetLBFactory();
343 $factory->commitMasterChanges();
346 # Do any deferred jobs
347 $this->doUpdates( $deferredUpdates );
349 # Commit and close up!
350 $factory->shutdown();
351 wfProfileOut( __METHOD__
);
355 * Deferred updates aren't really deferred anymore. It's important to report
356 * errors to the user, and that means doing this before OutputPage::output().
357 * Note that for page saves, the client will wait until the script exits
358 * anyway before following the redirect.
360 * @param $updates array of objects that hold an update to do
362 function doUpdates( &$updates ) {
363 wfProfileIn( __METHOD__
);
364 /* No need to get master connections in case of empty updates array */
366 wfProfileOut( __METHOD__
);
370 $dbw = wfGetDB( DB_MASTER
);
371 foreach( $updates as $up ) {
374 # Commit after every update to prevent lock contention
375 if( $dbw->trxLevel() ) {
379 wfProfileOut( __METHOD__
);
383 * Do a job from the job queue
386 $jobRunRate = $this->getVal( 'JobRunRate' );
388 if( $jobRunRate <= 0 ||
wfReadOnly() ) {
391 if( $jobRunRate < 1 ) {
392 $max = mt_getrandmax();
393 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
398 $n = intval( $jobRunRate );
401 while ( $n-- && false != ( $job = Job
::pop() ) ) {
402 $output = $job->toString() . "\n";
404 $success = $job->run();
406 $t = round( $t*1000 );
408 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
410 $output .= "Success, Time: $t ms\n";
412 wfDebugLog( 'jobqueue', $output );
417 * Ends this task peacefully
419 function restInPeace() {
420 wfLogProfilingData();
421 wfDebug( "Request ended normally\n" );
425 * Perform one of the "standard" actions
427 * @param $output OutputPage
428 * @param $article Article
429 * @param $title Title
431 * @param $request WebRequest
433 function performAction( &$output, &$article, &$title, &$user, &$request ) {
434 wfProfileIn( __METHOD__
);
436 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
437 wfProfileOut( __METHOD__
);
441 $action = $this->getVal( 'Action' );
442 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
443 /* No such action; this will switch to the default case */
444 $action = 'nosuchaction';
449 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
452 case 'raw': // includes JS/CSS
453 $raw = new RawPage( $article );
464 case 'markpatrolled':
466 case 'deletetrackback':
474 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
475 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
477 $rdf = new DublinCoreRdf( $article );
481 case 'creativecommons':
482 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
483 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
485 $rdf = new CreativeCommonsRdf( $article );
490 Credits
::showPage( $article );
493 if( session_id() == '' ) {
494 /* Send a cookie so anons get talk message notifications */
500 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
501 $internal = $request->getVal( 'internaledit' );
502 $external = $request->getVal( 'externaledit' );
503 $section = $request->getVal( 'section' );
504 $oldid = $request->getVal( 'oldid' );
505 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
506 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
507 $editor = new EditPage( $article );
509 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
510 $mode = $request->getVal( 'mode' );
511 $extedit = new ExternalEdit( $article, $mode );
517 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
518 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
520 $history = new PageHistory( $article );
524 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
525 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
528 wfProfileOut( __METHOD__
);
532 }; /* End of class MediaWiki */