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
47 * @param $article Article
48 * @param $output OutputPage
50 * @param $request WebRequest
52 function initialize( &$title, &$article, &$output, &$user, $request ) {
53 wfProfileIn( __METHOD__
);
54 $this->preliminaryChecks( $title, $output, $request ) ;
55 if ( !$this->initializeSpecialCases( $title, $output, $request ) ) {
56 $new_article = $this->initializeArticle( $title, $request );
57 if( is_object( $new_article ) ) {
58 $article = $new_article;
59 $this->performAction( $output, $article, $title, $user, $request );
60 } elseif( is_string( $new_article ) ) {
61 $output->redirect( $new_article );
63 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
66 wfProfileOut( __METHOD__
);
70 * Check if the maximum lag of database slaves is higher that $maxLag, and
71 * if it's the case, output an error message
73 * @param $maxLag int: maximum lag allowed for the request, as supplied by
75 * @return bool true if the request can continue
77 function checkMaxLag( $maxLag ) {
78 list( $host, $lag ) = wfGetLB()->getMaxLag();
79 if ( $lag > $maxLag ) {
80 wfMaxlagError( $host, $lag, $maxLag );
89 * Checks some initial queries
90 * Note that $title here is *not* a Title object, but a string!
92 * @param $title String
93 * @param $action String
94 * @return Title object to be $wgTitle
96 function checkInitialQueries( $title, $action ) {
97 global $wgOut, $wgRequest, $wgContLang;
98 if( $wgRequest->getVal( 'printable' ) == 'yes' ){
99 $wgOut->setPrintable();
104 if ( '' == $title && 'delete' != $action ) {
105 $ret = Title
::newMainPage();
106 } elseif ( $curid = $wgRequest->getInt( 'curid' ) ) {
107 # URLs like this are generated by RC, because rc_title isn't always accurate
108 $ret = Title
::newFromID( $curid );
110 $ret = Title
::newFromURL( $title );
111 // check variant links so that interwiki links don't have to worry
112 // about the possible different language variants
113 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
114 $wgContLang->findVariantLink( $title, $ret );
117 if ( ( $oldid = $wgRequest->getInt( 'oldid' ) )
118 && ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) ) {
119 // Allow oldid to override a changed or missing title.
120 $rev = Revision
::newFromId( $oldid );
122 $ret = $rev->getTitle();
129 * Checks for search query and anon-cannot-read case
131 * @param $title Title
132 * @param $output OutputPage
133 * @param $request WebRequest
135 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' );
146 # If the user is not logged in, the Namespace:title of the article must be in
147 # the Read array in order for the user to see it. (We have to check here to
148 # catch special pages etc. We check again in Article::view())
149 if ( !is_null( $title ) && !$title->userCanRead() ) {
150 $output->loginToUse();
158 * Initialize some special cases:
160 * - local interwiki redirects
164 * @param $title Title
165 * @param $output OutputPage
166 * @param $request WebRequest
167 * @return bool true if the request is already executed
169 function initializeSpecialCases( &$title, &$output, $request ) {
170 wfProfileIn( __METHOD__
);
172 $action = $this->getVal( 'Action' );
173 if( !$title ||
$title->getDBkey() == '' ) {
174 $title = SpecialPage
::getTitleFor( 'Badtitle' );
175 # Die now before we mess up $wgArticle and the skin stops working
176 throw new ErrorPageError( 'badtitle', 'badtitletext' );
177 } else if ( $title->getInterwiki() != '' ) {
178 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
179 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
181 $url = $title->getFullURL();
183 /* Check for a redirect loop */
184 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
185 $output->redirect( $url );
187 $title = SpecialPage
::getTitleFor( 'Badtitle' );
188 throw new ErrorPageError( 'badtitle', 'badtitletext' );
190 } else if ( ( $action == 'view' ) && !$request->wasPosted() &&
191 (!isset( $this->GET
['title'] ) ||
$title->getPrefixedDBKey() != $this->GET
['title'] ) &&
192 !count( array_diff( array_keys( $this->GET
), array( 'action', 'title' ) ) ) )
194 $targetUrl = $title->getFullURL();
195 // Redirect to canonical url, make it a 301 to allow caching
196 if( $targetUrl == $request->getFullRequestURL() ) {
197 $message = "Redirect loop detected!\n\n" .
198 "This means the wiki got confused about what page was " .
199 "requested; this sometimes happens when moving a wiki " .
200 "to a new server or changing the server configuration.\n\n";
202 if( $this->getVal( 'UsePathInfo' ) ) {
203 $message .= "The wiki is trying to interpret the page " .
204 "title from the URL path portion (PATH_INFO), which " .
205 "sometimes fails depending on the web server. Try " .
206 "setting \"\$wgUsePathInfo = false;\" in your " .
207 "LocalSettings.php, or check that \$wgArticlePath " .
210 $message .= "Your web server was detected as possibly not " .
211 "supporting URL path components (PATH_INFO) correctly; " .
212 "check your LocalSettings.php for a customized " .
213 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
216 wfHttpError( 500, "Internal error", $message );
219 $output->setSquidMaxage( 1200 );
220 $output->redirect( $targetUrl, '301' );
222 } else if ( NS_SPECIAL
== $title->getNamespace() ) {
223 /* actions that need to be made when we have a special pages */
224 SpecialPage
::executePath( $title );
226 /* No match to special cases */
227 wfProfileOut( __METHOD__
);
230 /* Did match a special case */
231 wfProfileOut( __METHOD__
);
236 * Create an Article object of the appropriate class for the given page.
238 * @param $title Title
239 * @return Article object
241 static function articleFromTitle( &$title ) {
242 if( NS_MEDIA
== $title->getNamespace() ) {
243 // FIXME: where should this go?
244 $title = Title
::makeTitle( NS_IMAGE
, $title->getDBkey() );
248 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
253 switch( $title->getNamespace() ) {
255 return new ImagePage( $title );
257 return new CategoryPage( $title );
259 return new Article( $title );
264 * Initialize the object to be known as $wgArticle for "standard" actions
265 * Create an Article object for the page, following redirects if needed.
267 * @param $title Title
268 * @param $request WebRequest
269 * @return mixed an Article, or a string to redirect to another URL
271 function initializeArticle( &$title, $request ) {
272 wfProfileIn( __METHOD__
);
274 $action = $this->getVal( 'action' );
275 $article = self
::articleFromTitle( $title );
277 wfDebug("Article: ".$title->getPrefixedText()."\n");
279 // Namespace might change when using redirects
280 // Check for redirects ...
281 $file = $title->getNamespace() == NS_IMAGE ?
$article->getFile() : null;
282 if( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
283 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
284 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
285 // ... and the article is not an image page with associated file
286 !( is_object( $file ) && $file->exists() &&
287 !$file->getRedirected() ) ) { // ... unless it is really an image redirect
289 $dbr = wfGetDB( DB_SLAVE
);
290 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
292 // Follow redirects only for... redirects
293 if( $article->isRedirect() ) {
294 $target = $article->followRedirect();
295 if( is_string( $target ) ) {
296 if( !$this->getVal( 'DisableHardRedirects' ) ) {
297 // we'll need to redirect
302 if( is_object( $target ) ) {
303 // Rewrite environment to redirected article
304 $rarticle = self
::articleFromTitle( $target );
305 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
306 if ( $rarticle->getTitle()->exists() ||
307 ( is_object( $file ) &&
308 !$file->isLocal() ) ) {
309 $rarticle->setRedirectedFrom( $title );
310 $article = $rarticle;
315 $title = $article->getTitle();
318 wfProfileOut( __METHOD__
);
323 * Cleaning up by doing deferred updates, calling LBFactory and doing the output
325 * @param $deferredUpdates array of updates to do
326 * @param $output OutputPage
328 function finalCleanup ( &$deferredUpdates, &$output ) {
329 wfProfileIn( __METHOD__
);
330 $this->doUpdates( $deferredUpdates );
332 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
333 $factory = wfGetLBFactory();
334 $factory->shutdown();
336 wfProfileOut( __METHOD__
);
340 * Deferred updates aren't really deferred anymore. It's important to report
341 * errors to the user, and that means doing this before OutputPage::output().
342 * Note that for page saves, the client will wait until the script exits
343 * anyway before following the redirect.
345 * @param $updates array of objects that hold an update to do
347 function doUpdates( &$updates ) {
348 wfProfileIn( __METHOD__
);
349 /* No need to get master connections in case of empty updates array */
351 wfProfileOut( __METHOD__
);
355 $dbw = wfGetDB( DB_MASTER
);
356 foreach( $updates as $up ) {
359 # Commit after every update to prevent lock contention
360 if ( $dbw->trxLevel() ) {
364 wfProfileOut( __METHOD__
);
368 * Do a job from the job queue
371 $jobRunRate = $this->getVal( 'JobRunRate' );
373 if ( $jobRunRate <= 0 ||
wfReadOnly() ) {
376 if ( $jobRunRate < 1 ) {
377 $max = mt_getrandmax();
378 if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
383 $n = intval( $jobRunRate );
386 while ( $n-- && false != ( $job = Job
::pop() ) ) {
387 $output = $job->toString() . "\n";
389 $success = $job->run();
391 $t = round( $t*1000 );
393 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
395 $output .= "Success, Time: $t ms\n";
397 wfDebugLog( 'jobqueue', $output );
402 * Ends this task peacefully
404 function restInPeace() {
405 wfLogProfilingData();
406 wfDebug( "Request ended normally\n" );
410 * Perform one of the "standard" actions
412 * @param $output OutputPage
413 * @param $article Article
414 * @param $title Title
416 * @param $request WebRequest
418 function performAction( &$output, &$article, &$title, &$user, &$request ) {
419 wfProfileIn( __METHOD__
);
421 if ( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request ) ) ) {
422 wfProfileOut( __METHOD__
);
426 $action = $this->getVal( 'Action' );
427 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
428 /* No such action; this will switch to the default case */
429 $action = 'nosuchaction';
434 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
445 case 'markpatrolled':
447 case 'deletetrackback':
455 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
456 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
458 require_once( 'includes/Metadata.php' );
459 wfDublinCoreRdf( $article );
462 case 'creativecommons':
463 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
464 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
466 require_once( 'includes/Metadata.php' );
467 wfCreativeCommonsRdf( $article );
471 require_once( 'includes/Credits.php' );
472 showCreditsPage( $article );
475 if( session_id() == '' ) {
476 /* Send a cookie so anons get talk message notifications */
482 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
483 $internal = $request->getVal( 'internaledit' );
484 $external = $request->getVal( 'externaledit' );
485 $section = $request->getVal( 'section' );
486 $oldid = $request->getVal( 'oldid' );
487 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
488 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
489 $editor = new EditPage( $article );
491 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
492 $mode = $request->getVal( 'mode' );
493 $extedit = new ExternalEdit( $article, $mode );
499 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
500 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
502 $history = new PageHistory( $article );
506 $raw = new RawPage( $article );
510 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
511 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
514 wfProfileOut( __METHOD__
);
518 }; /* End of class MediaWiki */