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 */
14 function MediaWiki () {
19 * Stores key/value pairs to circumvent global variables
20 * Note that keys are case-insensitive!
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 function getVal( $key, $default = '' ) {
32 $key = strtolower( $key );
33 if( isset( $this->params
[$key] ) ) {
34 return $this->params
[$key];
40 * Initialization of ... everything
41 @return Article either the object to become $wgArticle, or NULL
43 function initialize ( &$title, &$output, &$user, $request) {
44 wfProfileIn( 'MediaWiki::initialize' );
45 $this->preliminaryChecks ( $title, $output, $request ) ;
47 if ( !$this->initializeSpecialCases( $title, $output, $request ) ) {
48 $article = $this->initializeArticle( $title, $request );
49 if( is_object( $article ) ) {
50 $this->performAction( $output, $article, $title, $user, $request );
51 } elseif( is_string( $article ) ) {
52 $output->redirect( $article );
54 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
57 wfProfileOut( 'MediaWiki::initialize' );
62 * Checks some initial queries
63 * Note that $title here is *not* a Title object, but a string!
65 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
66 if ($request->getVal( 'printable' ) == 'yes') {
67 $output->setPrintable();
73 if ( '' == $title && 'delete' != $action ) {
74 $ret = Title
::newFromText( wfMsgForContent( 'mainpage' ) );
75 } elseif ( $curid = $request->getInt( 'curid' ) ) {
76 # URLs like this are generated by RC, because rc_title isn't always accurate
77 $ret = Title
::newFromID( $curid );
79 $ret = Title
::newFromURL( $title );
80 /* check variant links so that interwiki links don't have to worry about
81 the possible different language variants
83 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
84 $lang->findVariantLink( $title, $ret );
91 * Checks for search query and anon-cannot-read case
93 function preliminaryChecks ( &$title, &$output, $request ) {
95 # Debug statement for user levels
98 $search = $request->getText( 'search' );
99 if( !is_null( $search ) && $search !== '' ) {
100 // Compatibility with old search URLs which didn't use Special:Search
101 // Do this above the read whitelist check for security...
102 $title = SpecialPage
::getTitleFor( 'Search' );
104 $this->setVal( 'Search', $search );
106 # If the user is not logged in, the Namespace:title of the article must be in
107 # the Read array in order for the user to see it. (We have to check here to
108 # catch special pages etc. We check again in Article::view())
109 if ( !is_null( $title ) && !$title->userCanRead() ) {
110 $output->loginToUse();
118 * Initialize the object to be known as $wgArticle for special cases
120 function initializeSpecialCases ( &$title, &$output, $request ) {
122 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
124 $search = $this->getVal('Search');
125 $action = $this->getVal('Action');
126 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
127 require_once( 'includes/SpecialSearch.php' );
128 $title = SpecialPage
::getTitleFor( 'Search' );
130 } else if( !$title or $title->getDBkey() == '' ) {
131 $title = SpecialPage
::getTitleFor( 'Badtitle' );
132 # Die now before we mess up $wgArticle and the skin stops working
133 throw new ErrorPageError( 'badtitle', 'badtitletext' );
134 } else if ( $title->getInterwiki() != '' ) {
135 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
136 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
138 $url = $title->getFullURL();
140 /* Check for a redirect loop */
141 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
142 $output->redirect( $url );
144 $title = SpecialPage
::getTitleFor( 'Badtitle' );
145 throw new ErrorPageError( 'badtitle', 'badtitletext' );
147 } else if ( ( $action == 'view' ) &&
148 (!isset( $this->GET
['title'] ) ||
$title->getPrefixedDBKey() != $this->GET
['title'] ) &&
149 !count( array_diff( array_keys( $this->GET
), array( 'action', 'title' ) ) ) )
151 /* Redirect to canonical url, make it a 301 to allow caching */
152 $output->setSquidMaxage( 1200 );
153 $output->redirect( $title->getFullURL(), '301');
154 } else if ( NS_SPECIAL
== $title->getNamespace() ) {
155 /* actions that need to be made when we have a special pages */
156 SpecialPage
::executePath( $title );
158 /* No match to special cases */
159 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
162 /* Did match a special case */
163 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
168 * Create an Article object of the appropriate class for the given page.
169 * @param Title $title
172 function articleFromTitle( $title ) {
174 wfRunHooks('ArticleFromTitle', array( &$title, &$article ) );
179 if( NS_MEDIA
== $title->getNamespace() ) {
180 // FIXME: where should this go?
181 $title = Title
::makeTitle( NS_IMAGE
, $title->getDBkey() );
184 switch( $title->getNamespace() ) {
186 return new ImagePage( $title );
188 return new CategoryPage( $title );
190 return new Article( $title );
195 * Initialize the object to be known as $wgArticle for "standard" actions
196 * Create an Article object for the page, following redirects if needed.
197 * @param Title $title
198 * @param Request $request
199 * @param string $action
200 * @return mixed an Article, or a string to redirect to another URL
202 function initializeArticle( $title, $request ) {
204 wfProfileIn( 'MediaWiki::initializeArticle' );
206 $action = $this->getVal('Action');
207 $article = $this->articleFromTitle( $title );
209 // Namespace might change when using redirects
210 if( $action == 'view' && !$request->getVal( 'oldid' ) &&
211 $request->getVal( 'redirect' ) != 'no' ) {
213 $dbr =& wfGetDB(DB_SLAVE
);
214 $article->loadPageData($article->pageDataFromTitle($dbr, $title));
216 /* Follow redirects only for... redirects */
217 if ($article->mIsRedirect
) {
218 $target = $article->followRedirect();
219 if( is_string( $target ) ) {
220 global $wgDisableHardRedirects;
221 if( !$wgDisableHardRedirects ) {
222 // we'll need to redirect
226 if( is_object( $target ) ) {
227 /* Rewrite environment to redirected article */
228 $rarticle = $this->articleFromTitle($target);
229 $rarticle->loadPageData($rarticle->pageDataFromTitle($dbr,$target));
230 if ($rarticle->mTitle
->mArticleID
) {
231 $article = $rarticle;
233 $article->setRedirectedFrom( $title );
239 $wgTitle = $article->mTitle
;
242 wfProfileOut( 'MediaWiki::initializeArticle' );
247 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
249 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
250 wfProfileIn( 'MediaWiki::finalCleanup' );
251 $this->doUpdates( $deferredUpdates );
253 $loadBalancer->saveMasterPos();
254 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
255 $loadBalancer->commitAll();
257 wfProfileOut( 'MediaWiki::finalCleanup' );
261 * Deferred updates aren't really deferred anymore. It's important to report errors to the
262 * user, and that means doing this before OutputPage::output(). Note that for page saves,
263 * the client will wait until the script exits anyway before following the redirect.
265 function doUpdates ( &$updates ) {
266 wfProfileIn( 'MediaWiki::doUpdates' );
267 $dbw =& wfGetDB( DB_MASTER
);
268 foreach( $updates as $up ) {
271 # Commit after every update to prevent lock contention
272 if ( $dbw->trxLevel() ) {
276 wfProfileOut( 'MediaWiki::doUpdates' );
280 * Do a job from the job queue
283 global $wgJobRunRate;
285 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
288 if ( $wgJobRunRate < 1 ) {
289 $max = mt_getrandmax();
290 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
295 $n = intval( $wgJobRunRate );
298 while ( $n-- && false != ($job = Job
::pop())) {
299 $output = $job->toString() . "\n";
301 $success = $job->run();
303 $t = round( $t*1000 );
305 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
307 $output .= "Success, Time: $t ms\n";
309 wfDebugLog( 'jobqueue', $output );
314 * Ends this task peacefully
316 function restInPeace ( &$loadBalancer ) {
317 wfLogProfilingData();
318 $loadBalancer->closeAll();
319 wfDebug( "Request ended normally\n" );
323 * Perform one of the "standard" actions
325 function performAction( &$output, &$article, &$title, &$user, &$request ) {
327 wfProfileIn( 'MediaWiki::performAction' );
329 $action = $this->getVal('Action');
330 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
331 /* No such action; this will switch to the default case */
332 $action = 'nosuchaction';
337 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
348 case 'markpatrolled':
350 case 'deletetrackback':
358 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
359 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
361 require_once( 'includes/Metadata.php' );
362 wfDublinCoreRdf( $article );
365 case 'creativecommons':
366 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
367 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
369 require_once( 'includes/Metadata.php' );
370 wfCreativeCommonsRdf( $article );
374 require_once( 'includes/Credits.php' );
375 showCreditsPage( $article );
378 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
379 /* Send a cookie so anons get talk message notifications */
380 User
::SetupSession();
384 $internal = $request->getVal( 'internaledit' );
385 $external = $request->getVal( 'externaledit' );
386 $section = $request->getVal( 'section' );
387 $oldid = $request->getVal( 'oldid' );
388 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
389 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
390 $editor = new EditPage( $article );
392 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
393 $mode = $request->getVal( 'mode' );
394 $extedit = new ExternalEdit( $article, $mode );
399 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
400 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
402 $history = new PageHistory( $article );
406 $raw = new RawPage( $article );
410 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
411 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
414 wfProfileOut( 'MediaWiki::performAction' );
419 }; /* End of class MediaWiki */