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, $wgShowHostnames;
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 if( $wgShowHostnames ) {
68 echo "Waiting for $host: $lag seconds lagged\n";
70 echo "Waiting for a database server: $lag seconds lagged\n";
80 * Checks some initial queries
81 * Note that $title here is *not* a Title object, but a string!
83 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
84 if ($request->getVal( 'printable' ) == 'yes') {
85 $output->setPrintable();
91 if ( '' == $title && 'delete' != $action ) {
92 $ret = Title
::newMainPage();
93 } elseif ( $curid = $request->getInt( 'curid' ) ) {
94 # URLs like this are generated by RC, because rc_title isn't always accurate
95 $ret = Title
::newFromID( $curid );
97 $ret = Title
::newFromURL( $title );
98 /* check variant links so that interwiki links don't have to worry about
99 the possible different language variants
101 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
102 $lang->findVariantLink( $title, $ret );
105 if ( ( $oldid = $request->getInt( 'oldid' ) )
106 && ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) ) {
107 // Allow oldid to override a changed or missing title.
108 $rev = Revision
::newFromId( $oldid );
110 $ret = $rev->getTitle();
117 * Checks for search query and anon-cannot-read case
119 function preliminaryChecks ( &$title, &$output, $request ) {
121 if( $request->getCheck( 'search' ) ) {
122 // Compatibility with old search URLs which didn't use Special:Search
123 // Just check for presence here, so blank requests still
124 // show the search page when using ugly URLs (bug 8054).
126 // Do this above the read whitelist check for security...
127 $title = SpecialPage
::getTitleFor( 'Search' );
130 # If the user is not logged in, the Namespace:title of the article must be in
131 # the Read array in order for the user to see it. (We have to check here to
132 # catch special pages etc. We check again in Article::view())
133 if ( !is_null( $title ) && !$title->userCanRead() ) {
134 $output->loginToUse();
142 * Initialize the object to be known as $wgArticle for special cases
144 function initializeSpecialCases ( &$title, &$output, $request ) {
146 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
148 $action = $this->getVal('Action');
149 if( !$title or $title->getDBkey() == '' ) {
150 $title = SpecialPage
::getTitleFor( 'Badtitle' );
151 # Die now before we mess up $wgArticle and the skin stops working
152 throw new ErrorPageError( 'badtitle', 'badtitletext' );
153 } else if ( $title->getInterwiki() != '' ) {
154 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
155 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
157 $url = $title->getFullURL();
159 /* Check for a redirect loop */
160 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
161 $output->redirect( $url );
163 $title = SpecialPage
::getTitleFor( 'Badtitle' );
164 throw new ErrorPageError( 'badtitle', 'badtitletext' );
166 } else if ( ( $action == 'view' ) && !$wgRequest->wasPosted() &&
167 (!isset( $this->GET
['title'] ) ||
$title->getPrefixedDBKey() != $this->GET
['title'] ) &&
168 !count( array_diff( array_keys( $this->GET
), array( 'action', 'title' ) ) ) )
170 $targetUrl = $title->getFullURL();
171 // Redirect to canonical url, make it a 301 to allow caching
172 global $wgUsePathInfo;
173 if( $targetUrl == $wgRequest->getFullRequestURL() ) {
174 $message = "Redirect loop detected!\n\n" .
175 "This means the wiki got confused about what page was " .
176 "requested; this sometimes happens when moving a wiki " .
177 "to a new server or changing the server configuration.\n\n";
179 if( $wgUsePathInfo ) {
180 $message .= "The wiki is trying to interpret the page " .
181 "title from the URL path portion (PATH_INFO), which " .
182 "sometimes fails depending on the web server. Try " .
183 "setting \"\$wgUsePathInfo = false;\" in your " .
184 "LocalSettings.php, or check that \$wgArticlePath " .
187 $message .= "Your web server was detected as possibly not " .
188 "supporting URL path components (PATH_INFO) correctly; " .
189 "check your LocalSettings.php for a customized " .
190 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
193 wfHttpError( 500, "Internal error", $message );
196 $output->setSquidMaxage( 1200 );
197 $output->redirect( $targetUrl, '301');
199 } else if ( NS_SPECIAL
== $title->getNamespace() ) {
200 /* actions that need to be made when we have a special pages */
201 SpecialPage
::executePath( $title );
203 /* No match to special cases */
204 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
207 /* Did match a special case */
208 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
213 * Create an Article object of the appropriate class for the given page.
214 * @param Title $title
217 static function articleFromTitle( $title ) {
219 wfRunHooks('ArticleFromTitle', array( &$title, &$article ) );
224 if( NS_MEDIA
== $title->getNamespace() ) {
225 // FIXME: where should this go?
226 $title = Title
::makeTitle( NS_IMAGE
, $title->getDBkey() );
229 switch( $title->getNamespace() ) {
231 return new ImagePage( $title );
233 return new CategoryPage( $title );
235 return new Article( $title );
240 * Initialize the object to be known as $wgArticle for "standard" actions
241 * Create an Article object for the page, following redirects if needed.
242 * @param Title $title
243 * @param Request $request
244 * @param string $action
245 * @return mixed an Article, or a string to redirect to another URL
247 function initializeArticle( $title, $request ) {
249 wfProfileIn( 'MediaWiki::initializeArticle' );
251 $action = $this->getVal('Action');
252 $article = $this->articleFromTitle( $title );
254 // Namespace might change when using redirects
255 if( $action == 'view' && !$request->getVal( 'oldid' ) &&
256 $request->getVal( 'redirect' ) != 'no' ) {
258 $dbr = wfGetDB(DB_SLAVE
);
259 $article->loadPageData($article->pageDataFromTitle($dbr, $title));
261 /* Follow redirects only for... redirects */
262 if ($article->mIsRedirect
) {
263 $target = $article->followRedirect();
264 if( is_string( $target ) ) {
265 global $wgDisableHardRedirects;
266 if( !$wgDisableHardRedirects ) {
267 // we'll need to redirect
271 if( is_object( $target ) ) {
272 /* Rewrite environment to redirected article */
273 $rarticle = $this->articleFromTitle($target);
274 $rarticle->loadPageData($rarticle->pageDataFromTitle($dbr,$target));
275 if ($rarticle->mTitle
->mArticleID
) {
276 $article = $rarticle;
278 $article->setRedirectedFrom( $title );
284 $wgTitle = $article->mTitle
;
287 wfProfileOut( 'MediaWiki::initializeArticle' );
292 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
294 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
295 wfProfileIn( 'MediaWiki::finalCleanup' );
296 $this->doUpdates( $deferredUpdates );
298 $loadBalancer->saveMasterPos();
299 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
300 $loadBalancer->commitAll();
302 wfProfileOut( 'MediaWiki::finalCleanup' );
306 * Deferred updates aren't really deferred anymore. It's important to report errors to the
307 * user, and that means doing this before OutputPage::output(). Note that for page saves,
308 * the client will wait until the script exits anyway before following the redirect.
310 function doUpdates ( &$updates ) {
311 wfProfileIn( 'MediaWiki::doUpdates' );
312 $dbw = wfGetDB( DB_MASTER
);
313 foreach( $updates as $up ) {
316 # Commit after every update to prevent lock contention
317 if ( $dbw->trxLevel() ) {
321 wfProfileOut( 'MediaWiki::doUpdates' );
325 * Do a job from the job queue
328 global $wgJobRunRate;
330 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
333 if ( $wgJobRunRate < 1 ) {
334 $max = mt_getrandmax();
335 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
340 $n = intval( $wgJobRunRate );
343 while ( $n-- && false != ($job = Job
::pop())) {
344 $output = $job->toString() . "\n";
346 $success = $job->run();
348 $t = round( $t*1000 );
350 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
352 $output .= "Success, Time: $t ms\n";
354 wfDebugLog( 'jobqueue', $output );
359 * Ends this task peacefully
361 function restInPeace ( &$loadBalancer ) {
362 wfLogProfilingData();
363 $loadBalancer->closeAll();
364 wfDebug( "Request ended normally\n" );
368 * Perform one of the "standard" actions
370 function performAction( &$output, &$article, &$title, &$user, &$request ) {
372 wfProfileIn( 'MediaWiki::performAction' );
374 $action = $this->getVal('Action');
375 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
376 /* No such action; this will switch to the default case */
377 $action = 'nosuchaction';
382 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
393 case 'markpatrolled':
395 case 'deletetrackback':
403 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
404 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
406 require_once( 'includes/Metadata.php' );
407 wfDublinCoreRdf( $article );
410 case 'creativecommons':
411 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
412 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
414 require_once( 'includes/Metadata.php' );
415 wfCreativeCommonsRdf( $article );
419 require_once( 'includes/Credits.php' );
420 showCreditsPage( $article );
423 if( session_id() == '' ) {
424 /* Send a cookie so anons get talk message notifications */
429 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
430 $internal = $request->getVal( 'internaledit' );
431 $external = $request->getVal( 'externaledit' );
432 $section = $request->getVal( 'section' );
433 $oldid = $request->getVal( 'oldid' );
434 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
435 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
436 $editor = new EditPage( $article );
438 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
439 $mode = $request->getVal( 'mode' );
440 $extedit = new ExternalEdit( $article, $mode );
447 if( $wgRequest->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
448 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
450 $history = new PageHistory( $article );
454 $raw = new RawPage( $article );
458 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
459 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
462 wfProfileOut( 'MediaWiki::performAction' );
466 }; /* End of class MediaWiki */