3 * MediaWiki is the to-be base class for this whole project
5 * @internal documentation reviewed 15 Mar 2010
7 class MediaWiki
extends ContextSource
{
9 public function request( WebRequest
$x = null ){
10 return wfSetVar( $this->getRequest(), $x );
13 public function output( OutputPage
$x = null ){
14 return wfSetVar( $this->getOutput(), $x );
17 public function __construct( RequestContext
$context ){
18 $this->setContext( $context );
19 $this->getContext()->setTitle( $this->parseTitle() );
23 * Parse $request to get the Title object
25 * @return Title object to be $wgTitle
27 private function parseTitle() {
30 $curid = $this->getRequest()->getInt( 'curid' );
31 $title = $this->getRequest()->getVal( 'title' );
33 if ( $this->getRequest()->getCheck( 'search' ) ) {
34 // Compatibility with old search URLs which didn't use Special:Search
35 // Just check for presence here, so blank requests still
36 // show the search page when using ugly URLs (bug 8054).
37 $ret = SpecialPage
::getTitleFor( 'Search' );
39 // URLs like this are generated by RC, because rc_title isn't always accurate
40 $ret = Title
::newFromID( $curid );
41 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
42 $ret = Title
::newMainPage();
44 $ret = Title
::newFromURL( $title );
45 // check variant links so that interwiki links don't have to worry
46 // about the possible different language variants
47 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
48 $wgContLang->findVariantLink( $title, $ret );
51 // For non-special titles, check for implicit titles
52 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
53 // We can have urls with just ?diff=,?oldid= or even just ?diff=
54 $oldid = $this->getRequest()->getInt( 'oldid' );
55 $oldid = $oldid ?
$oldid : $this->getRequest()->getInt( 'diff' );
56 // Allow oldid to override a changed or missing title
58 $rev = Revision
::newFromId( $oldid );
59 $ret = $rev ?
$rev->getTitle() : $ret;
63 if( $ret === null ||
( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
70 * Get the Title object that we'll be acting on, as specified in the WebRequest
73 public function getTitle(){
74 if( $this->getContext()->getTitle() === null ){
75 $this->getContext()->setTitle( $this->parseTitle() );
77 return $this->getContext()->getTitle();
81 * Performs the request.
84 * - local interwiki redirects
89 * @return Article object
91 public function performRequest() {
92 global $wgServer, $wgUsePathInfo;
94 wfProfileIn( __METHOD__
);
96 if ( $this->getRequest()->getVal( 'printable' ) === 'yes' ) {
97 $this->getOutput()->setPrintable();
100 wfRunHooks( 'BeforeInitialize', array(
109 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
110 if ( $this->getTitle() instanceof BadTitle
) {
111 throw new ErrorPageError( 'badtitle', 'badtitletext' );
112 // If the user is not logged in, the Namespace:title of the article must be in
113 // the Read array in order for the user to see it. (We have to check here to
114 // catch special pages etc. We check again in Article::view())
115 } else if ( !$this->getTitle()->userCanRead() ) {
116 $this->getOutput()->loginToUse();
117 // Interwiki redirects
118 } else if ( $this->getTitle()->getInterwiki() != '' ) {
119 $rdfrom = $this->getRequest()->getVal( 'rdfrom' );
121 $url = $this->getTitle()->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
123 $query = $this->getRequest()->getValues();
124 unset( $query['title'] );
125 $url = $this->getTitle()->getFullURL( $query );
127 // Check for a redirect loop
128 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $this->getTitle()->isLocal() ) {
129 // 301 so google et al report the target as the actual url.
130 $this->getOutput()->redirect( $url, 301 );
132 $this->getContext()->setTitle( new BadTitle
);
133 wfProfileOut( __METHOD__
);
134 throw new ErrorPageError( 'badtitle', 'badtitletext' );
136 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
137 } else if ( $this->getRequest()->getVal( 'action', 'view' ) == 'view' && !$this->getRequest()->wasPosted()
138 && ( $this->getRequest()->getVal( 'title' ) === null ||
$this->getTitle()->getPrefixedDBKey() != $this->getRequest()->getVal( 'title' ) )
139 && !count( array_diff( array_keys( $this->getRequest()->getValues() ), array( 'action', 'title' ) ) ) )
141 if ( $this->getTitle()->getNamespace() == NS_SPECIAL
) {
142 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $this->getTitle()->getDBkey() );
144 $this->getContext()->setTitle( SpecialPage
::getTitleFor( $name, $subpage ) );
147 $targetUrl = $this->getTitle()->getFullURL();
148 // Redirect to canonical url, make it a 301 to allow caching
149 if ( $targetUrl == $this->getRequest()->getFullRequestURL() ) {
150 $message = "Redirect loop detected!\n\n" .
151 "This means the wiki got confused about what page was " .
152 "requested; this sometimes happens when moving a wiki " .
153 "to a new server or changing the server configuration.\n\n";
155 if ( $wgUsePathInfo ) {
156 $message .= "The wiki is trying to interpret the page " .
157 "title from the URL path portion (PATH_INFO), which " .
158 "sometimes fails depending on the web server. Try " .
159 "setting \"\$wgUsePathInfo = false;\" in your " .
160 "LocalSettings.php, or check that \$wgArticlePath " .
163 $message .= "Your web server was detected as possibly not " .
164 "supporting URL path components (PATH_INFO) correctly; " .
165 "check your LocalSettings.php for a customized " .
166 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
169 wfHttpError( 500, "Internal error", $message );
171 $this->getOutput()->setSquidMaxage( 1200 );
172 $this->getOutput()->redirect( $targetUrl, '301' );
175 } else if ( NS_SPECIAL
== $this->getTitle()->getNamespace() ) {
176 // actions that need to be made when we have a special pages
177 SpecialPageFactory
::executePath( $this->getTitle(), $this->getContext() );
179 // ...otherwise treat it as an article view. The article
180 // may be a redirect to another article or URL.
181 $article = $this->initializeArticle();
182 if ( is_object( $article ) ) {
183 $this->performAction( $article );
184 wfProfileOut( __METHOD__
);
186 } elseif ( is_string( $article ) ) {
187 $this->getOutput()->redirect( $article );
189 wfProfileOut( __METHOD__
);
190 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
193 wfProfileOut( __METHOD__
);
197 * Create an Article object of the appropriate class for the given page.
199 * @deprecated in 1.19; use Article::newFromTitle() instead
200 * @param $title Title
201 * @param $context RequestContext
202 * @return Article object
204 public static function articleFromTitle( $title, RequestContext
$context ) {
205 return Article
::newFromTitle( $title, $context );
209 * Returns the action that will be executed, not necesserly the one passed
210 * passed through the "action" parameter. Actions disabled in
211 * $wgDisabledActions will be replaced by "nosuchaction"
213 * @return String: action
215 public function getAction() {
216 global $wgDisabledActions;
218 $action = $this->getRequest()->getVal( 'action', 'view' );
220 // Check for disabled actions
221 if ( in_array( $action, $wgDisabledActions ) ) {
222 return 'nosuchaction';
225 // Workaround for bug #20966: inability of IE to provide an action dependent
226 // on which submit button is clicked.
227 if ( $action === 'historysubmit' ) {
228 if ( $this->getRequest()->getBool( 'revisiondelete' ) ) {
229 return 'revisiondelete';
233 } elseif ( $action == 'editredlink' ) {
241 * Initialize the main Article object for "standard" actions (view, etc)
242 * Create an Article object for the page, following redirects if needed.
244 * @return mixed an Article, or a string to redirect to another URL
246 private function initializeArticle() {
247 global $wgDisableHardRedirects;
249 wfProfileIn( __METHOD__
);
251 $action = $this->getRequest()->getVal( 'action', 'view' );
252 $article = Article
::newFromTitle( $this->getTitle(), $this->getContext() );
253 // NS_MEDIAWIKI has no redirects.
254 // It is also used for CSS/JS, so performance matters here...
255 if ( $this->getTitle()->getNamespace() == NS_MEDIAWIKI
) {
256 wfProfileOut( __METHOD__
);
259 // Namespace might change when using redirects
260 // Check for redirects ...
261 $file = ( $this->getTitle()->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
262 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
263 && !$this->getRequest()->getVal( 'oldid' ) && // ... and are not old revisions
264 !$this->getRequest()->getVal( 'diff' ) && // ... and not when showing diff
265 $this->getRequest()->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
266 // ... and the article is not a non-redirect image page with associated file
267 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
269 // Give extensions a change to ignore/handle redirects as needed
270 $ignoreRedirect = $target = false;
272 wfRunHooks( 'InitializeArticleMaybeRedirect',
273 array( $this->getTitle(), $this->getRequest(), &$ignoreRedirect, &$target, &$article ) );
275 // Follow redirects only for... redirects.
276 // If $target is set, then a hook wanted to redirect.
277 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
278 // Is the target already set by an extension?
279 $target = $target ?
$target : $article->followRedirect();
280 if ( is_string( $target ) ) {
281 if ( !$wgDisableHardRedirects ) {
282 // we'll need to redirect
283 wfProfileOut( __METHOD__
);
287 if ( is_object( $target ) ) {
288 // Rewrite environment to redirected article
289 $rarticle = Article
::newFromTitle( $target, $this->getContext() );
290 $rarticle->loadPageData();
291 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
292 $rarticle->setRedirectedFrom( $this->getTitle() );
293 $article = $rarticle;
294 $this->getContext()->setTitle( $target );
298 $this->getContext()->setTitle( $article->getTitle() );
301 wfProfileOut( __METHOD__
);
306 * Cleaning up request by doing deferred updates, DB transaction, and the output
308 public function finalCleanup() {
309 wfProfileIn( __METHOD__
);
310 // Now commit any transactions, so that unreported errors after
311 // output() don't roll back the whole DB transaction
312 $factory = wfGetLBFactory();
313 $factory->commitMasterChanges();
314 // Output everything!
315 $this->getOutput()->output();
316 // Do any deferred jobs
317 wfDoUpdates( 'commit' );
320 wfProfileOut( __METHOD__
);
324 * Do a job from the job queue
326 private function doJobs() {
327 global $wgJobRunRate;
329 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
332 if ( $wgJobRunRate < 1 ) {
333 $max = mt_getrandmax();
334 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
339 $n = intval( $wgJobRunRate );
342 // Close the session so that jobs don't access the current session
343 $this->shutdownLBFactory();
344 session_write_close();
346 while ( $n-- && false != ( $job = Job
::pop() ) ) {
347 $output = $job->toString() . "\n";
349 $success = $job->run();
351 $t = round( $t * 1000 );
353 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
355 $output .= "Success, Time: $t ms\n";
357 wfDebugLog( 'jobqueue', $output );
362 * Ends this task peacefully
364 public function restInPeace() {
365 MessageCache
::logMessages();
366 wfLogProfilingData();
367 $this->shutdownLBFactory();
368 wfDebug( "Request ended normally\n" );
372 * Commit pending master changes, shutdown the current loadbalancer
373 * factory and destroys the factory instance.
375 private function shutdownLBFactory() {
376 // Commit and close up!
377 $factory = LBFactory
::singleton();
378 $factory->commitMasterChanges();
379 $factory->shutdown();
380 LBFactory
::destroyInstance();
384 * Perform one of the "standard" actions
386 * @param $article Article
388 private function performAction( $article ) {
389 global $wgSquidMaxage, $wgUseExternalEditor;
391 wfProfileIn( __METHOD__
);
393 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
394 $this->getOutput(), $article, $this->getTitle(),
395 $this->getUser(), $this->getRequest(), $this ) ) )
397 wfProfileOut( __METHOD__
);
401 $act = $this->getAction();
403 $action = Action
::factory( $act, $article );
404 if( $action instanceof Action
){
406 wfProfileOut( __METHOD__
);
412 $this->getOutput()->setSquidMaxage( $wgSquidMaxage );
415 case 'raw': // includes JS/CSS
416 wfProfileIn( __METHOD__
. '-raw' );
417 $raw = new RawPage( $article );
419 wfProfileOut( __METHOD__
. '-raw' );
427 case 'markpatrolled':
429 case 'deletetrackback':
433 if ( session_id() == '' ) {
434 // Send a cookie so anons get talk message notifications
439 if ( wfRunHooks( 'CustomEditor', array( $article, $this->getUser() ) ) ) {
440 $internal = $this->getRequest()->getVal( 'internaledit' );
441 $external = $this->getRequest()->getVal( 'externaledit' );
442 $section = $this->getRequest()->getVal( 'section' );
443 $oldid = $this->getRequest()->getVal( 'oldid' );
444 if ( !$wgUseExternalEditor ||
$act == 'submit' ||
$internal ||
445 $section ||
$oldid ||
( !$this->getUser()->getOption( 'externaleditor' ) && !$external ) ) {
446 $editor = new EditPage( $article );
448 } elseif ( $wgUseExternalEditor && ( $external ||
$this->getUser()->getOption( 'externaleditor' ) ) ) {
449 $mode = $this->getRequest()->getVal( 'mode' );
450 $extedit = new ExternalEdit( $article, $mode );
456 if ( $this->getRequest()->getFullRequestURL() == $this->getTitle()->getInternalURL( 'action=history' ) ) {
457 $this->getOutput()->setSquidMaxage( $wgSquidMaxage );
459 $history = new HistoryPage( $article );
462 case 'revisiondelete':
463 // For show/hide submission from history page
464 $special = SpecialPageFactory
::getPage( 'Revisiondelete' );
465 $special->execute( '' );
468 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
469 $this->getOutput()->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
472 wfProfileOut( __METHOD__
);