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__
);
56 $output->setTitle( $title );
58 if( !$this->preliminaryChecks( $title, $output, $request ) ) {
59 wfProfileOut( __METHOD__
);
62 if( !$this->initializeSpecialCases( $title, $output, $request ) ) {
63 $new_article = $this->initializeArticle( $title, $output, $request );
64 if( is_object( $new_article ) ) {
65 $article = $new_article;
66 $this->performAction( $output, $article, $title, $user, $request );
67 } elseif( is_string( $new_article ) ) {
68 $output->redirect( $new_article );
70 wfProfileOut( __METHOD__
);
71 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
74 wfProfileOut( __METHOD__
);
78 * Check if the maximum lag of database slaves is higher that $maxLag, and
79 * if it's the case, output an error message
81 * @param $maxLag int: maximum lag allowed for the request, as supplied by
83 * @return bool true if the request can continue
85 function checkMaxLag( $maxLag ) {
86 list( $host, $lag ) = wfGetLB()->getMaxLag();
87 if( $lag > $maxLag ) {
88 wfMaxlagError( $host, $lag, $maxLag );
96 * Checks some initial queries
97 * Note that $title here is *not* a Title object, but a string!
99 * @param $title String
100 * @param $action String
101 * @return Title object to be $wgTitle
103 function checkInitialQueries( $title, $action ) {
104 global $wgOut, $wgRequest, $wgContLang;
105 if( $wgRequest->getVal( 'printable' ) === 'yes' ) {
106 $wgOut->setPrintable();
109 if( $curid = $wgRequest->getInt( 'curid' ) ) {
110 # URLs like this are generated by RC, because rc_title isn't always accurate
111 $ret = Title
::newFromID( $curid );
112 } elseif( '' == $title && 'delete' != $action ) {
113 $ret = Title
::newMainPage();
115 $ret = Title
::newFromURL( $title );
116 // check variant links so that interwiki links don't have to worry
117 // about the possible different language variants
118 if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
119 $wgContLang->findVariantLink( $title, $ret );
121 # For non-special titles, check for implicit titles
122 if( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
123 // We can have urls with just ?diff=,?oldid= or even just ?diff=
124 $oldid = $wgRequest->getInt( 'oldid' );
125 $oldid = $oldid ?
$oldid : $wgRequest->getInt( 'diff' );
126 // Allow oldid to override a changed or missing title
128 $rev = Revision
::newFromId( $oldid );
129 $ret = $rev ?
$rev->getTitle() : $ret;
136 * Checks for search query and anon-cannot-read case
138 * @param $title Title
139 * @param $output OutputPage
140 * @param $request WebRequest
142 function preliminaryChecks( &$title, &$output, $request ) {
143 if( $request->getCheck( 'search' ) ) {
144 // Compatibility with old search URLs which didn't use Special:Search
145 // Just check for presence here, so blank requests still
146 // show the search page when using ugly URLs (bug 8054).
148 // Do this above the read whitelist check for security...
149 $title = SpecialPage
::getTitleFor( 'Search' );
151 # If the user is not logged in, the Namespace:title of the article must be in
152 # the Read array in order for the user to see it. (We have to check here to
153 # catch special pages etc. We check again in Article::view())
154 if( !is_null( $title ) && !$title->userCanRead() ) {
155 global $wgDeferredUpdateList;
156 $output->loginToUse();
157 $this->finalCleanup( $wgDeferredUpdateList, $output );
165 * Initialize some special cases:
167 * - local interwiki redirects
171 * FIXME: why is this crap called "initialize" when it performs everything?
173 * @param $title Title
174 * @param $output OutputPage
175 * @param $request WebRequest
176 * @return bool true if the request is already executed
178 function initializeSpecialCases( &$title, &$output, $request ) {
179 wfProfileIn( __METHOD__
);
181 $action = $this->getVal( 'Action' );
182 if( is_null($title) ||
$title->getDBkey() == '' ) {
183 $title = SpecialPage
::getTitleFor( 'Badtitle' );
184 # Die now before we mess up $wgArticle and the skin stops working
185 throw new ErrorPageError( 'badtitle', 'badtitletext' );
186 } else if( $title->getInterwiki() != '' ) {
187 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
188 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
190 $url = $title->getFullURL();
192 /* Check for a redirect loop */
193 if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
194 $output->redirect( $url );
196 $title = SpecialPage
::getTitleFor( 'Badtitle' );
197 throw new ErrorPageError( 'badtitle', 'badtitletext' );
199 } else if( $action == 'view' && !$request->wasPosted() &&
200 ( !isset($this->GET
['title']) ||
$title->getPrefixedDBKey() != $this->GET
['title'] ) &&
201 !count( array_diff( array_keys( $this->GET
), array( 'action', 'title' ) ) ) )
203 $targetUrl = $title->getFullURL();
204 // Redirect to canonical url, make it a 301 to allow caching
205 if( $targetUrl == $request->getFullRequestURL() ) {
206 $message = "Redirect loop detected!\n\n" .
207 "This means the wiki got confused about what page was " .
208 "requested; this sometimes happens when moving a wiki " .
209 "to a new server or changing the server configuration.\n\n";
211 if( $this->getVal( 'UsePathInfo' ) ) {
212 $message .= "The wiki is trying to interpret the page " .
213 "title from the URL path portion (PATH_INFO), which " .
214 "sometimes fails depending on the web server. Try " .
215 "setting \"\$wgUsePathInfo = false;\" in your " .
216 "LocalSettings.php, or check that \$wgArticlePath " .
219 $message .= "Your web server was detected as possibly not " .
220 "supporting URL path components (PATH_INFO) correctly; " .
221 "check your LocalSettings.php for a customized " .
222 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
225 wfHttpError( 500, "Internal error", $message );
228 $output->setSquidMaxage( 1200 );
229 $output->redirect( $targetUrl, '301' );
231 } else if( NS_SPECIAL
== $title->getNamespace() ) {
232 /* actions that need to be made when we have a special pages */
233 SpecialPage
::executePath( $title );
235 /* No match to special cases */
236 wfProfileOut( __METHOD__
);
239 /* Did match a special case */
240 wfProfileOut( __METHOD__
);
245 * Create an Article object of the appropriate class for the given page.
247 * @param $title Title
248 * @return Article object
250 static function articleFromTitle( &$title ) {
251 if( NS_MEDIA
== $title->getNamespace() ) {
252 // FIXME: where should this go?
253 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
257 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
262 switch( $title->getNamespace() ) {
264 return new ImagePage( $title );
266 return new CategoryPage( $title );
268 return new Article( $title );
273 * Initialize the object to be known as $wgArticle for "standard" actions
274 * Create an Article object for the page, following redirects if needed.
276 * @param $title Title ($wgTitle)
277 * @param $output OutputPage ($wgOut)
278 * @param $request WebRequest ($wgRequest)
279 * @return mixed an Article, or a string to redirect to another URL
281 function initializeArticle( &$title, &$output, $request ) {
282 wfProfileIn( __METHOD__
);
284 $action = $this->getVal( 'action', 'view' );
285 $article = self
::articleFromTitle( $title );
286 # NS_MEDIAWIKI has no redirects.
287 # It is also used for CSS/JS, so performance matters here...
288 if( $title->getNamespace() == NS_MEDIAWIKI
) {
289 wfProfileOut( __METHOD__
);
292 // Namespace might change when using redirects
293 // Check for redirects ...
294 $file = ($title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
295 if( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
296 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
297 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
298 // ... and the article is not a non-redirect image page with associated file
299 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
301 # Give extensions a change to ignore/handle redirects as needed
302 $ignoreRedirect = $target = false;
304 $dbr = wfGetDB( DB_SLAVE
);
305 $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
307 wfRunHooks( 'InitializeArticleMaybeRedirect',
308 array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
310 // Follow redirects only for... redirects
311 if( !$ignoreRedirect && $article->isRedirect() ) {
312 # Is the target already set by an extension?
313 $target = $target ?
$target : $article->followRedirect();
314 if( is_string( $target ) ) {
315 if( !$this->getVal( 'DisableHardRedirects' ) ) {
316 // we'll need to redirect
320 if( is_object($target) ) {
321 // Rewrite environment to redirected article
322 $rarticle = self
::articleFromTitle( $target );
323 $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
324 if( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
325 $rarticle->setRedirectedFrom( $title );
326 $article = $rarticle;
328 $output->setTitle( $title );
332 $title = $article->getTitle();
335 wfProfileOut( __METHOD__
);
340 * Cleaning up request by doing:
341 ** deferred updates, DB transaction, and the output
343 * @param $deferredUpdates array of updates to do
344 * @param $output OutputPage
346 function finalCleanup( &$deferredUpdates, &$output ) {
347 wfProfileIn( __METHOD__
);
348 # Now commit any transactions, so that unreported errors after
349 # output() don't roll back the whole DB transaction
350 $factory = wfGetLBFactory();
351 $factory->commitMasterChanges();
354 # Do any deferred jobs
355 $this->doUpdates( $deferredUpdates );
357 wfProfileOut( __METHOD__
);
361 * Deferred updates aren't really deferred anymore. It's important to report
362 * errors to the user, and that means doing this before OutputPage::output().
363 * Note that for page saves, the client will wait until the script exits
364 * anyway before following the redirect.
366 * @param $updates array of objects that hold an update to do
368 function doUpdates( &$updates ) {
369 wfProfileIn( __METHOD__
);
370 /* No need to get master connections in case of empty updates array */
372 wfProfileOut( __METHOD__
);
376 $dbw = wfGetDB( DB_MASTER
);
377 foreach( $updates as $up ) {
380 # Commit after every update to prevent lock contention
381 if( $dbw->trxLevel() ) {
385 wfProfileOut( __METHOD__
);
389 * Do a job from the job queue
392 $jobRunRate = $this->getVal( 'JobRunRate' );
394 if( $jobRunRate <= 0 ||
wfReadOnly() ) {
397 if( $jobRunRate < 1 ) {
398 $max = mt_getrandmax();
399 if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
404 $n = intval( $jobRunRate );
407 while ( $n-- && false != ( $job = Job
::pop() ) ) {
408 $output = $job->toString() . "\n";
410 $success = $job->run();
412 $t = round( $t*1000 );
414 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
416 $output .= "Success, Time: $t ms\n";
418 wfDebugLog( 'jobqueue', $output );
423 * Ends this task peacefully
425 function restInPeace() {
426 wfLogProfilingData();
427 # Commit and close up!
428 $factory = wfGetLBFactory();
429 $factory->commitMasterChanges();
430 $factory->shutdown();
431 wfDebug( "Request ended normally\n" );
435 * Perform one of the "standard" actions
437 * @param $output OutputPage
438 * @param $article Article
439 * @param $title Title
441 * @param $request WebRequest
443 function performAction( &$output, &$article, &$title, &$user, &$request ) {
444 wfProfileIn( __METHOD__
);
446 if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
447 wfProfileOut( __METHOD__
);
451 $action = $this->getVal( 'Action' );
452 if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
453 /* No such action; this will switch to the default case */
454 $action = 'nosuchaction';
459 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
462 case 'raw': // includes JS/CSS
463 wfProfileIn( __METHOD__
.'-raw' );
464 $raw = new RawPage( $article );
466 wfProfileOut( __METHOD__
.'-raw' );
476 case 'markpatrolled':
478 case 'deletetrackback':
486 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
487 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
489 $rdf = new DublinCoreRdf( $article );
493 case 'creativecommons':
494 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
495 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
497 $rdf = new CreativeCommonsRdf( $article );
502 Credits
::showPage( $article );
505 if( session_id() == '' ) {
506 /* Send a cookie so anons get talk message notifications */
512 if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
513 $internal = $request->getVal( 'internaledit' );
514 $external = $request->getVal( 'externaledit' );
515 $section = $request->getVal( 'section' );
516 $oldid = $request->getVal( 'oldid' );
517 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
518 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
519 $editor = new EditPage( $article );
521 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
522 $mode = $request->getVal( 'mode' );
523 $extedit = new ExternalEdit( $article, $mode );
529 if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
530 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
532 $history = new PageHistory( $article );
536 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
537 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
540 wfProfileOut( __METHOD__
);
544 }; /* End of class MediaWiki */