3 * MediaWiki is the to-be base class for this whole project
5 * @internal documentation reviewed 15 Mar 2010
10 * Array of options which may or may not be used
11 * FIXME: this seems currently to be a messy halfway-house between globals
12 * and a config object. Pick one and run with it
15 private $params = array();
18 * Stores key/value pairs to circumvent global variables
19 * Note that keys are case-insensitive!
21 * @param $key String: key to store
22 * @param $value Mixed: value to put for the key
24 public function setVal( $key, &$value ) {
25 $key = strtolower( $key );
26 $this->params
[$key] =& $value;
30 * Retrieves key/value pairs to circumvent global variables
31 * Note that keys are case-insensitive!
33 * @param $key String: key to get
34 * @param $default string default value, defaults to empty string
35 * @return $default Mixed: default value if if the key doesn't exist
37 public function getVal( $key, $default = '' ) {
38 $key = strtolower( $key );
39 if ( isset( $this->params
[$key] ) ) {
40 return $this->params
[$key];
46 * Initialization of ... everything
47 * Performs the request too
49 * @param $title Title ($wgTitle)
50 * @param $article Article
51 * @param $output OutputPage
53 * @param $request WebRequest
55 public function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) {
56 wfProfileIn( __METHOD__
);
58 $output->setTitle( $title );
59 if ( $request->getVal( 'printable' ) === 'yes' ) {
60 $output->setPrintable();
63 wfRunHooks( 'BeforeInitialize', array( &$title, &$article, &$output, &$user, $request, $this ) );
65 // If the user is not logged in, the Namespace:title of the article must be in
66 // the Read array in order for the user to see it. (We have to check here to
67 // catch special pages etc. We check again in Article::view())
68 if ( !is_null( $title ) && !$title->userCanRead() ) {
69 $output->loginToUse();
70 $this->finalCleanup( $output );
72 wfProfileOut( __METHOD__
);
76 // Call handleSpecialCases() to deal with all special requests...
77 if ( !$this->handleSpecialCases( $title, $output, $request ) ) {
78 // ...otherwise treat it as an article view. The article
79 // may be a redirect to another article or URL.
80 $new_article = $this->initializeArticle( $title, $output, $request );
81 if ( is_object( $new_article ) ) {
82 $article = $new_article;
83 $this->performAction( $output, $article, $title, $user, $request );
84 } elseif ( is_string( $new_article ) ) {
85 $output->redirect( $new_article );
87 wfProfileOut( __METHOD__
);
88 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
91 wfProfileOut( __METHOD__
);
95 * Checks some initial queries
96 * FIXME: rename to parseTitle() ?
98 * @param $request WebRequest
99 * @return Title object to be $wgTitle
101 /* private */ function checkInitialQueries( WebRequest
$request ) {
104 $curid = $request->getInt( 'curid' );
105 $title = $request->getVal( 'title' );
107 if ( $request->getCheck( 'search' ) ) {
108 // Compatibility with old search URLs which didn't use Special:Search
109 // Just check for presence here, so blank requests still
110 // show the search page when using ugly URLs (bug 8054).
111 $ret = SpecialPage
::getTitleFor( 'Search' );
112 } elseif ( $curid ) {
113 // URLs like this are generated by RC, because rc_title isn't always accurate
114 $ret = Title
::newFromID( $curid );
115 } elseif ( $title == '' && $this->getAction( $request ) != 'delete' ) {
116 $ret = Title
::newMainPage();
118 $ret = Title
::newFromURL( $title );
119 // check variant links so that interwiki links don't have to worry
120 // about the possible different language variants
121 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
122 $wgContLang->findVariantLink( $title, $ret );
124 // For non-special titles, check for implicit titles
125 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
126 // We can have urls with just ?diff=,?oldid= or even just ?diff=
127 $oldid = $request->getInt( 'oldid' );
128 $oldid = $oldid ?
$oldid : $request->getInt( 'diff' );
129 // Allow oldid to override a changed or missing title
131 $rev = Revision
::newFromId( $oldid );
132 $ret = $rev ?
$rev->getTitle() : $ret;
139 * Initialize some special cases:
141 * - local interwiki redirects
145 * @param $title Title
146 * @param $output OutputPage
147 * @param $request WebRequest
148 * @return bool true if the request is already executed
150 private function handleSpecialCases( &$title, &$output, $request ) {
151 wfProfileIn( __METHOD__
);
153 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
154 if ( is_null( $title ) ||
( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
155 $title = SpecialPage
::getTitleFor( 'Badtitle' );
156 $output->setTitle( $title ); // bug 21456
157 // Die now before we mess up $wgArticle and the skin stops working
158 throw new ErrorPageError( 'badtitle', 'badtitletext' );
160 // Interwiki redirects
161 } else if ( $title->getInterwiki() != '' ) {
162 $rdfrom = $request->getVal( 'rdfrom' );
164 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
166 $query = $request->getValues();
167 unset( $query['title'] );
168 $url = $title->getFullURL( $query );
170 /* Check for a redirect loop */
171 if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $title->isLocal() ) {
172 // 301 so google et al report the target as the actual url.
173 $output->redirect( $url, 301 );
175 $title = SpecialPage
::getTitleFor( 'Badtitle' );
176 $output->setTitle( $title ); // bug 21456
177 wfProfileOut( __METHOD__
);
178 throw new ErrorPageError( 'badtitle', 'badtitletext' );
180 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
181 } else if ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted()
182 && ( $request->getVal( 'title' ) === null ||
$title->getPrefixedDBKey() != $request->getVal( 'title' ) )
183 && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
185 if ( $title->getNamespace() == NS_SPECIAL
) {
186 list( $name, $subpage ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBkey() );
188 $title = SpecialPage
::getTitleFor( $name, $subpage );
191 $targetUrl = $title->getFullURL();
192 // Redirect to canonical url, make it a 301 to allow caching
193 if ( $targetUrl == $request->getFullRequestURL() ) {
194 $message = "Redirect loop detected!\n\n" .
195 "This means the wiki got confused about what page was " .
196 "requested; this sometimes happens when moving a wiki " .
197 "to a new server or changing the server configuration.\n\n";
199 if ( $this->getVal( 'UsePathInfo' ) ) {
200 $message .= "The wiki is trying to interpret the page " .
201 "title from the URL path portion (PATH_INFO), which " .
202 "sometimes fails depending on the web server. Try " .
203 "setting \"\$wgUsePathInfo = false;\" in your " .
204 "LocalSettings.php, or check that \$wgArticlePath " .
207 $message .= "Your web server was detected as possibly not " .
208 "supporting URL path components (PATH_INFO) correctly; " .
209 "check your LocalSettings.php for a customized " .
210 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
213 wfHttpError( 500, "Internal error", $message );
214 wfProfileOut( __METHOD__
);
217 $output->setSquidMaxage( 1200 );
218 $output->redirect( $targetUrl, '301' );
221 } else if ( NS_SPECIAL
== $title->getNamespace() ) {
222 /* actions that need to be made when we have a special pages */
223 SpecialPage
::executePath( $title );
225 /* No match to special cases */
226 wfProfileOut( __METHOD__
);
229 /* Did match a special case */
230 wfProfileOut( __METHOD__
);
235 * Create an Article object of the appropriate class for the given page.
237 * @param $title Title
238 * @return Article object
240 public static function articleFromTitle( &$title ) {
241 if ( NS_MEDIA
== $title->getNamespace() ) {
242 // FIXME: where should this go?
243 $title = Title
::makeTitle( NS_FILE
, $title->getDBkey() );
247 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
252 switch( $title->getNamespace() ) {
254 return new ImagePage( $title );
256 return new CategoryPage( $title );
258 return new Article( $title );
263 * Returns the action that will be executed, not necesserly the one passed
264 * passed through the "action" parameter. Actions disabled in
265 * $wgDisabledActions will be replaced by "nosuchaction"
267 * @param $request WebRequest
268 * @return String: action
270 public function getAction( WebRequest
$request ) {
271 global $wgDisabledActions;
273 $action = $request->getVal( 'action', 'view' );
275 // Check for disabled actions
276 if ( in_array( $action, $wgDisabledActions ) ) {
277 return 'nosuchaction';
280 // Workaround for bug #20966: inability of IE to provide an action dependent
281 // on which submit button is clicked.
282 if ( $action === 'historysubmit' ) {
283 if ( $request->getBool( 'revisiondelete' ) ) {
284 return 'revisiondelete';
285 } elseif ( $request->getBool( 'revisionmove' ) ) {
286 return 'revisionmove';
290 } elseif ( $action == 'editredlink' ) {
298 * Initialize the object to be known as $wgArticle for "standard" actions
299 * Create an Article object for the page, following redirects if needed.
301 * @param $title Title ($wgTitle)
302 * @param $output OutputPage ($wgOut)
303 * @param $request WebRequest ($wgRequest)
304 * @return mixed an Article, or a string to redirect to another URL
306 private function initializeArticle( &$title, &$output, $request ) {
307 wfProfileIn( __METHOD__
);
309 $action = $request->getVal( 'action', 'view' );
310 $article = self
::articleFromTitle( $title );
311 // NS_MEDIAWIKI has no redirects.
312 // It is also used for CSS/JS, so performance matters here...
313 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
314 wfProfileOut( __METHOD__
);
317 // Namespace might change when using redirects
318 // Check for redirects ...
319 $file = ( $title->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
320 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
321 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
322 !$request->getVal( 'diff' ) && // ... and not when showing diff
323 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
324 // ... and the article is not a non-redirect image page with associated file
325 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
327 // Give extensions a change to ignore/handle redirects as needed
328 $ignoreRedirect = $target = false;
330 wfRunHooks( 'InitializeArticleMaybeRedirect',
331 array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
333 // Follow redirects only for... redirects.
334 // If $target is set, then a hook wanted to redirect.
335 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
336 // Is the target already set by an extension?
337 $target = $target ?
$target : $article->followRedirect();
338 if ( is_string( $target ) ) {
339 if ( !$this->getVal( 'DisableHardRedirects' ) ) {
340 // we'll need to redirect
341 wfProfileOut( __METHOD__
);
345 if ( is_object( $target ) ) {
346 // Rewrite environment to redirected article
347 $rarticle = self
::articleFromTitle( $target );
348 $rarticle->loadPageData();
349 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
350 $rarticle->setRedirectedFrom( $title );
351 $article = $rarticle;
353 $output->setTitle( $title );
357 $title = $article->getTitle();
360 wfProfileOut( __METHOD__
);
365 * Cleaning up request by doing deferred updates, DB transaction, and the output
367 * @param $output OutputPage
369 public function finalCleanup( &$output ) {
370 wfProfileIn( __METHOD__
);
371 // Now commit any transactions, so that unreported errors after
372 // output() don't roll back the whole DB transaction
373 $factory = wfGetLBFactory();
374 $factory->commitMasterChanges();
375 // Output everything!
377 // Do any deferred jobs
378 wfDoUpdates( 'commit' );
379 // Close the session so that jobs don't access the current session
380 session_write_close();
382 wfProfileOut( __METHOD__
);
386 * Do a job from the job queue
388 private function doJobs() {
389 global $wgJobRunRate;
391 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
394 if ( $wgJobRunRate < 1 ) {
395 $max = mt_getrandmax();
396 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
401 $n = intval( $wgJobRunRate );
404 while ( $n-- && false != ( $job = Job
::pop() ) ) {
405 $output = $job->toString() . "\n";
407 $success = $job->run();
409 $t = round( $t * 1000 );
411 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
413 $output .= "Success, Time: $t ms\n";
415 wfDebugLog( 'jobqueue', $output );
420 * Ends this task peacefully
422 public function restInPeace() {
423 MessageCache
::logMessages();
424 wfLogProfilingData();
425 // Commit and close up!
426 $factory = wfGetLBFactory();
427 $factory->commitMasterChanges();
428 $factory->shutdown();
429 wfDebug( "Request ended normally\n" );
433 * Perform one of the "standard" actions
435 * @param $output OutputPage
436 * @param $article Article
437 * @param $title Title
439 * @param $request WebRequest
441 private function performAction( &$output, &$article, &$title, &$user, &$request ) {
442 wfProfileIn( __METHOD__
);
444 if ( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
445 wfProfileOut( __METHOD__
);
449 $action = $this->getAction( $request );
453 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
456 case 'raw': // includes JS/CSS
457 wfProfileIn( __METHOD__
. '-raw' );
458 $raw = new RawPage( $article );
460 wfProfileOut( __METHOD__
. '-raw' );
470 case 'markpatrolled':
472 case 'deletetrackback':
480 if ( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
481 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
483 $rdf = new DublinCoreRdf( $article );
487 case 'creativecommons':
488 if ( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
489 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
491 $rdf = new CreativeCommonsRdf( $article );
496 Credits
::showPage( $article );
499 if ( session_id() == '' ) {
500 /* Send a cookie so anons get talk message notifications */
505 if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
506 $internal = $request->getVal( 'internaledit' );
507 $external = $request->getVal( 'externaledit' );
508 $section = $request->getVal( 'section' );
509 $oldid = $request->getVal( 'oldid' );
510 if ( !$this->getVal( 'UseExternalEditor' ) ||
$action == 'submit' ||
$internal ||
511 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
512 $editor = new EditPage( $article );
514 } elseif ( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
515 $mode = $request->getVal( 'mode' );
516 $extedit = new ExternalEdit( $article, $mode );
522 if ( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
523 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
525 $history = new HistoryPage( $article );
528 case 'revisiondelete':
529 // For show/hide submission from history page
530 $special = SpecialPage
::getPage( 'Revisiondelete' );
531 $special->execute( '' );
534 // For revision move submission from history page
535 $special = SpecialPage
::getPage( 'RevisionMove' );
536 $special->execute( '' );
539 if ( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
540 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
543 wfProfileOut( __METHOD__
);