3 * MediaWiki is the to-be base class for this whole project
8 var $GET; /* Stores the $_GET variables at time of creation, can be changed */
11 /** Constructor. It just save the $_GET variable */
12 function __construct() {
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;
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];
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 ) ;
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 );
52 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
55 wfProfileOut( 'MediaWiki::initialize' );
59 function checkMaxLag( $maxLag ) {
60 global $wgLoadBalancer;
61 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
62 if ( $lag > $maxLag ) {
63 wfMaxlagError( $host, $lag, $maxLag );
72 * Checks some initial queries
73 * Note that $title here is *not* a Title object, but a string!
75 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
76 if ($request->getVal( 'printable' ) == 'yes') {
77 $output->setPrintable();
83 if ( '' == $title && 'delete' != $action ) {
84 $ret = Title
::newMainPage();
85 } elseif ( $curid = $request->getInt( 'curid' ) ) {
86 # URLs like this are generated by RC, because rc_title isn't always accurate
87 $ret = Title
::newFromID( $curid );
89 $ret = Title
::newFromURL( $title );
90 /* check variant links so that interwiki links don't have to worry about
91 the possible different language variants
93 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
94 $lang->findVariantLink( $title, $ret );
97 if ( ( $oldid = $request->getInt( 'oldid' ) )
98 && ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) ) {
99 // Allow oldid to override a changed or missing title.
100 $rev = Revision
::newFromId( $oldid );
102 $ret = $rev->getTitle();
109 * Checks for search query and anon-cannot-read case
111 function preliminaryChecks ( &$title, &$output, $request ) {
113 if( $request->getCheck( 'search' ) ) {
114 // Compatibility with old search URLs which didn't use Special:Search
115 // Just check for presence here, so blank requests still
116 // show the search page when using ugly URLs (bug 8054).
118 // Do this above the read whitelist check for security...
119 $title = SpecialPage
::getTitleFor( 'Search' );
122 # If the user is not logged in, the Namespace:title of the article must be in
123 # the Read array in order for the user to see it. (We have to check here to
124 # catch special pages etc. We check again in Article::view())
125 if ( !is_null( $title ) && !$title->userCanRead() ) {
126 $output->loginToUse();
134 * Initialize the object to be known as $wgArticle for special cases
136 function initializeSpecialCases ( &$title, &$output, $request ) {
138 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
140 $action = $this->getVal('Action');
141 if( !$title or $title->getDBkey() == '' ) {
142 $title = SpecialPage
::getTitleFor( 'Badtitle' );
143 # Die now before we mess up $wgArticle and the skin stops working
144 throw new ErrorPageError( 'badtitle', 'badtitletext' );
145 } else if ( $title->getInterwiki() != '' ) {
146 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
147 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
149 $url = $title->getFullURL();
151 /* Check for a redirect loop */
152 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
153 $output->redirect( $url );
155 $title = SpecialPage
::getTitleFor( 'Badtitle' );
156 throw new ErrorPageError( 'badtitle', 'badtitletext' );
158 } else if ( ( $action == 'view' ) && !$wgRequest->wasPosted() &&
159 (!isset( $this->GET
['title'] ) ||
$title->getPrefixedDBKey() != $this->GET
['title'] ) &&
160 !count( array_diff( array_keys( $this->GET
), array( 'action', 'title' ) ) ) )
162 $targetUrl = $title->getFullURL();
163 // Redirect to canonical url, make it a 301 to allow caching
164 global $wgUsePathInfo;
165 if( $targetUrl == $wgRequest->getFullRequestURL() ) {
166 $message = "Redirect loop detected!\n\n" .
167 "This means the wiki got confused about what page was " .
168 "requested; this sometimes happens when moving a wiki " .
169 "to a new server or changing the server configuration.\n\n";
171 if( $wgUsePathInfo ) {
172 $message .= "The wiki is trying to interpret the page " .
173 "title from the URL path portion (PATH_INFO), which " .
174 "sometimes fails depending on the web server. Try " .
175 "setting \"\$wgUsePathInfo = false;\" in your " .
176 "LocalSettings.php, or check that \$wgArticlePath " .
179 $message .= "Your web server was detected as possibly not " .
180 "supporting URL path components (PATH_INFO) correctly; " .
181 "check your LocalSettings.php for a customized " .
182 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
185 wfHttpError( 500, "Internal error", $message );
188 $output->setSquidMaxage( 1200 );
189 $output->redirect( $targetUrl, '301');
191 } else if ( NS_SPECIAL
== $title->getNamespace() ) {
192 /* actions that need to be made when we have a special pages */
193 SpecialPage
::executePath( $title );
195 /* No match to special cases */
196 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
199 /* Did match a special case */
200 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
205 * Create an Article object of the appropriate class for the given page.
206 * @param Title $title
209 static function articleFromTitle( $title ) {
211 wfRunHooks('ArticleFromTitle', array( &$title, &$article ) );
216 if( NS_MEDIA
== $title->getNamespace() ) {
217 // FIXME: where should this go?
218 $title = Title
::makeTitle( NS_IMAGE
, $title->getDBkey() );
221 switch( $title->getNamespace() ) {
223 return new ImagePage( $title );
225 return new CategoryPage( $title );
227 return new Article( $title );
232 * Initialize the object to be known as $wgArticle for "standard" actions
233 * Create an Article object for the page, following redirects if needed.
234 * @param Title $title
235 * @param Request $request
236 * @param string $action
237 * @return mixed an Article, or a string to redirect to another URL
239 function initializeArticle( $title, $request ) {
241 wfProfileIn( 'MediaWiki::initializeArticle' );
243 $action = $this->getVal('Action');
244 $article = $this->articleFromTitle( $title );
246 // Namespace might change when using redirects
247 if( ( $action == 'view' ||
$action == 'render' ) && !$request->getVal( 'oldid' ) &&
248 $request->getVal( 'redirect' ) != 'no' ) {
250 $dbr = wfGetDB(DB_SLAVE
);
251 $article->loadPageData($article->pageDataFromTitle($dbr, $title));
253 /* Follow redirects only for... redirects */
254 if ($article->mIsRedirect
) {
255 $target = $article->followRedirect();
256 if( is_string( $target ) ) {
257 global $wgDisableHardRedirects;
258 if( !$wgDisableHardRedirects ) {
259 // we'll need to redirect
263 if( is_object( $target ) ) {
264 /* Rewrite environment to redirected article */
265 $rarticle = $this->articleFromTitle($target);
266 $rarticle->loadPageData($rarticle->pageDataFromTitle($dbr,$target));
267 if ($rarticle->mTitle
->mArticleID
) {
268 $article = $rarticle;
270 $article->setRedirectedFrom( $title );
276 $wgTitle = $article->mTitle
;
279 wfProfileOut( 'MediaWiki::initializeArticle' );
284 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
286 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
287 wfProfileIn( 'MediaWiki::finalCleanup' );
288 $this->doUpdates( $deferredUpdates );
290 $loadBalancer->saveMasterPos();
291 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
292 $loadBalancer->commitAll();
294 wfProfileOut( 'MediaWiki::finalCleanup' );
298 * Deferred updates aren't really deferred anymore. It's important to report errors to the
299 * user, and that means doing this before OutputPage::output(). Note that for page saves,
300 * the client will wait until the script exits anyway before following the redirect.
302 function doUpdates ( &$updates ) {
303 wfProfileIn( 'MediaWiki::doUpdates' );
304 $dbw = wfGetDB( DB_MASTER
);
305 foreach( $updates as $up ) {
308 # Commit after every update to prevent lock contention
309 if ( $dbw->trxLevel() ) {
313 wfProfileOut( 'MediaWiki::doUpdates' );
317 * Do a job from the job queue
320 global $wgJobRunRate;
322 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
325 if ( $wgJobRunRate < 1 ) {
326 $max = mt_getrandmax();
327 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
332 $n = intval( $wgJobRunRate );
335 while ( $n-- && false != ($job = Job
::pop())) {
336 $output = $job->toString() . "\n";
338 $success = $job->run();
340 $t = round( $t*1000 );
342 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
344 $output .= "Success, Time: $t ms\n";
346 wfDebugLog( 'jobqueue', $output );
351 * Ends this task peacefully
353 function restInPeace ( &$loadBalancer ) {
354 wfLogProfilingData();
355 $loadBalancer->closeAll();
356 wfDebug( "Request ended normally\n" );
360 * Perform one of the "standard" actions
362 function performAction( &$output, &$article, &$title, &$user, &$request ) {
364 wfProfileIn( 'MediaWiki::performAction' );
366 if ( !wfRunHooks('MediaWikiPerformAction', array($output, $article, $title, $user, $request)) ) {
367 wfProfileOut( 'MediaWiki::performAction' );
371 $action = $this->getVal('Action');
372 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
373 /* No such action; this will switch to the default case */
374 $action = 'nosuchaction';
379 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
390 case 'markpatrolled':
392 case 'deletetrackback':
400 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
401 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
403 require_once( 'includes/Metadata.php' );
404 wfDublinCoreRdf( $article );
407 case 'creativecommons':
408 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
409 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
411 require_once( 'includes/Metadata.php' );
412 wfCreativeCommonsRdf( $article );
416 require_once( 'includes/Credits.php' );
417 showCreditsPage( $article );
420 if( session_id() == '' ) {
421 /* Send a cookie so anons get talk message notifications */
426 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
427 $internal = $request->getVal( 'internaledit' );
428 $external = $request->getVal( 'externaledit' );
429 $section = $request->getVal( 'section' );
430 $oldid = $request->getVal( 'oldid' );
431 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
432 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
433 $editor = new EditPage( $article );
435 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
436 $mode = $request->getVal( 'mode' );
437 $extedit = new ExternalEdit( $article, $mode );
444 if( $wgRequest->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
445 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
447 $history = new PageHistory( $article );
451 $raw = new RawPage( $article );
455 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
456 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
459 wfProfileOut( 'MediaWiki::performAction' );
463 }; /* End of class MediaWiki */