3 * MediaWiki is the to-be base class for this whole project
5 * @internal documentation reviewed 15 Mar 2010
10 * TODO: fold $output, etc, into this
15 public function request( WebRequest
$x = null ){
16 return wfSetVar( $this->context
->request
, $x );
19 public function output( OutputPage
$x = null ){
20 return wfSetVar( $this->context
->output
, $x );
23 public function __construct( RequestContext
$context ){
24 $this->context
= $context;
25 $this->context
->setTitle( $this->parseTitle() );
29 * Parse $request to get the Title object
31 * @return Title object to be $wgTitle
33 private function parseTitle() {
36 $request = $this->context
->getRequest();
37 $curid = $request->getInt( 'curid' );
38 $title = $request->getVal( 'title' );
40 if ( $request->getCheck( 'search' ) ) {
41 // Compatibility with old search URLs which didn't use Special:Search
42 // Just check for presence here, so blank requests still
43 // show the search page when using ugly URLs (bug 8054).
44 $ret = SpecialPage
::getTitleFor( 'Search' );
46 // URLs like this are generated by RC, because rc_title isn't always accurate
47 $ret = Title
::newFromID( $curid );
48 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
49 $ret = Title
::newMainPage();
51 $ret = Title
::newFromURL( $title );
52 // check variant links so that interwiki links don't have to worry
53 // about the possible different language variants
54 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
55 $wgContLang->findVariantLink( $title, $ret );
58 // For non-special titles, check for implicit titles
59 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
60 // We can have urls with just ?diff=,?oldid= or even just ?diff=
61 $oldid = $request->getInt( 'oldid' );
62 $oldid = $oldid ?
$oldid : $request->getInt( 'diff' );
63 // Allow oldid to override a changed or missing title
65 $rev = Revision
::newFromId( $oldid );
66 $ret = $rev ?
$rev->getTitle() : $ret;
70 if( $ret === null ||
( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
77 * Get the Title object that we'll be acting on, as specified in the WebRequest
80 public function getTitle(){
81 if( $this->context
->title
=== null ){
82 $this->context
->title
= $this->parseTitle();
84 return $this->context
->title
;
88 * Performs the request.
91 * - local interwiki redirects
96 * @return Article object
98 public function performRequest() {
99 global $wgServer, $wgUsePathInfo;
101 wfProfileIn( __METHOD__
);
103 if ( $this->context
->request
->getVal( 'printable' ) === 'yes' ) {
104 $this->context
->output
->setPrintable();
107 wfRunHooks( 'BeforeInitialize', array(
108 &$this->context
->title
,
110 &$this->context
->output
,
111 &$this->context
->user
,
112 $this->context
->request
,
116 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
117 if ( $this->context
->title
instanceof BadTitle
) {
118 throw new ErrorPageError( 'badtitle', 'badtitletext' );
119 // If the user is not logged in, the Namespace:title of the article must be in
120 // the Read array in order for the user to see it. (We have to check here to
121 // catch special pages etc. We check again in Article::view())
122 } else if ( !$this->context
->title
->userCanRead() ) {
123 $this->context
->output
->loginToUse();
124 // Interwiki redirects
125 } else if ( $this->context
->title
->getInterwiki() != '' ) {
126 $rdfrom = $this->context
->request
->getVal( 'rdfrom' );
128 $url = $this->context
->title
->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
130 $query = $this->context
->request
->getValues();
131 unset( $query['title'] );
132 $url = $this->context
->title
->getFullURL( $query );
134 // Check for a redirect loop
135 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $this->context
->title
->isLocal() ) {
136 // 301 so google et al report the target as the actual url.
137 $this->context
->output
->redirect( $url, 301 );
139 $this->context
->title
= new BadTitle
;
140 wfProfileOut( __METHOD__
);
141 throw new ErrorPageError( 'badtitle', 'badtitletext' );
143 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
144 } else if ( $this->context
->request
->getVal( 'action', 'view' ) == 'view' && !$this->context
->request
->wasPosted()
145 && ( $this->context
->request
->getVal( 'title' ) === null ||
$this->context
->title
->getPrefixedDBKey() != $this->context
->request
->getVal( 'title' ) )
146 && !count( array_diff( array_keys( $this->context
->request
->getValues() ), array( 'action', 'title' ) ) ) )
148 if ( $this->context
->title
->getNamespace() == NS_SPECIAL
) {
149 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $this->context
->title
->getDBkey() );
151 $this->context
->title
= SpecialPage
::getTitleFor( $name, $subpage );
154 $targetUrl = $this->context
->title
->getFullURL();
155 // Redirect to canonical url, make it a 301 to allow caching
156 if ( $targetUrl == $this->context
->request
->getFullRequestURL() ) {
157 $message = "Redirect loop detected!\n\n" .
158 "This means the wiki got confused about what page was " .
159 "requested; this sometimes happens when moving a wiki " .
160 "to a new server or changing the server configuration.\n\n";
162 if ( $wgUsePathInfo ) {
163 $message .= "The wiki is trying to interpret the page " .
164 "title from the URL path portion (PATH_INFO), which " .
165 "sometimes fails depending on the web server. Try " .
166 "setting \"\$wgUsePathInfo = false;\" in your " .
167 "LocalSettings.php, or check that \$wgArticlePath " .
170 $message .= "Your web server was detected as possibly not " .
171 "supporting URL path components (PATH_INFO) correctly; " .
172 "check your LocalSettings.php for a customized " .
173 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
176 wfHttpError( 500, "Internal error", $message );
178 $this->context
->output
->setSquidMaxage( 1200 );
179 $this->context
->output
->redirect( $targetUrl, '301' );
182 } else if ( NS_SPECIAL
== $this->context
->title
->getNamespace() ) {
183 // actions that need to be made when we have a special pages
184 SpecialPageFactory
::executePath( $this->context
->title
, $this->context
);
186 // ...otherwise treat it as an article view. The article
187 // may be a redirect to another article or URL.
188 $article = $this->initializeArticle();
189 if ( is_object( $article ) ) {
190 $this->performAction( $article );
191 wfProfileOut( __METHOD__
);
193 } elseif ( is_string( $article ) ) {
194 $this->context
->output
->redirect( $article );
196 wfProfileOut( __METHOD__
);
197 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
200 wfProfileOut( __METHOD__
);
204 * Create an Article object of the appropriate class for the given page.
206 * @deprecated in 1.19; use Article::newFromTitle() instead
207 * @param $title Title
208 * @param $context RequestContext
209 * @return Article object
211 public static function articleFromTitle( $title, RequestContext
$context ) {
212 return Article
::newFromTitle( $title, $context );
216 * Returns the action that will be executed, not necesserly the one passed
217 * passed through the "action" parameter. Actions disabled in
218 * $wgDisabledActions will be replaced by "nosuchaction"
220 * @return String: action
222 public function getAction() {
223 global $wgDisabledActions;
225 $action = $this->context
->request
->getVal( 'action', 'view' );
227 // Check for disabled actions
228 if ( in_array( $action, $wgDisabledActions ) ) {
229 return 'nosuchaction';
232 // Workaround for bug #20966: inability of IE to provide an action dependent
233 // on which submit button is clicked.
234 if ( $action === 'historysubmit' ) {
235 if ( $this->context
->request
->getBool( 'revisiondelete' ) ) {
236 return 'revisiondelete';
240 } elseif ( $action == 'editredlink' ) {
248 * Initialize the main Article object for "standard" actions (view, etc)
249 * Create an Article object for the page, following redirects if needed.
251 * @return mixed an Article, or a string to redirect to another URL
253 private function initializeArticle() {
254 global $wgDisableHardRedirects;
256 wfProfileIn( __METHOD__
);
258 $action = $this->context
->request
->getVal( 'action', 'view' );
259 $article = Article
::newFromTitle( $this->context
->title
, $this->context
);
260 // NS_MEDIAWIKI has no redirects.
261 // It is also used for CSS/JS, so performance matters here...
262 if ( $this->context
->title
->getNamespace() == NS_MEDIAWIKI
) {
263 wfProfileOut( __METHOD__
);
266 // Namespace might change when using redirects
267 // Check for redirects ...
268 $file = ( $this->context
->title
->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
269 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
270 && !$this->context
->request
->getVal( 'oldid' ) && // ... and are not old revisions
271 !$this->context
->request
->getVal( 'diff' ) && // ... and not when showing diff
272 $this->context
->request
->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
273 // ... and the article is not a non-redirect image page with associated file
274 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
276 // Give extensions a change to ignore/handle redirects as needed
277 $ignoreRedirect = $target = false;
279 wfRunHooks( 'InitializeArticleMaybeRedirect',
280 array( &$this->context
->title
, &$this->context
->request
, &$ignoreRedirect, &$target, &$article ) );
282 // Follow redirects only for... redirects.
283 // If $target is set, then a hook wanted to redirect.
284 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
285 // Is the target already set by an extension?
286 $target = $target ?
$target : $article->followRedirect();
287 if ( is_string( $target ) ) {
288 if ( !$wgDisableHardRedirects ) {
289 // we'll need to redirect
290 wfProfileOut( __METHOD__
);
294 if ( is_object( $target ) ) {
295 // Rewrite environment to redirected article
296 $rarticle = Article
::newFromTitle( $target, $this->context
);
297 $rarticle->loadPageData();
298 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
299 $rarticle->setRedirectedFrom( $this->context
->title
);
300 $article = $rarticle;
301 $this->context
->title
= $target;
305 $this->context
->title
= $article->getTitle();
308 wfProfileOut( __METHOD__
);
313 * Cleaning up request by doing deferred updates, DB transaction, and the output
315 public function finalCleanup() {
316 wfProfileIn( __METHOD__
);
317 // Now commit any transactions, so that unreported errors after
318 // output() don't roll back the whole DB transaction
319 $factory = wfGetLBFactory();
320 $factory->commitMasterChanges();
321 // Output everything!
322 $this->context
->output
->output();
323 // Do any deferred jobs
324 wfDoUpdates( 'commit' );
327 wfProfileOut( __METHOD__
);
331 * Do a job from the job queue
333 private function doJobs() {
334 global $wgJobRunRate;
336 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
339 if ( $wgJobRunRate < 1 ) {
340 $max = mt_getrandmax();
341 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
346 $n = intval( $wgJobRunRate );
349 // Close the session so that jobs don't access the current session
350 $this->shutdownLBFactory();
351 session_write_close();
353 while ( $n-- && false != ( $job = Job
::pop() ) ) {
354 $output = $job->toString() . "\n";
356 $success = $job->run();
358 $t = round( $t * 1000 );
360 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
362 $output .= "Success, Time: $t ms\n";
364 wfDebugLog( 'jobqueue', $output );
369 * Ends this task peacefully
371 public function restInPeace() {
372 MessageCache
::logMessages();
373 wfLogProfilingData();
374 $this->shutdownLBFactory();
375 wfDebug( "Request ended normally\n" );
379 * Commit pending master changes, shutdown the current loadbalancer
380 * factory and destroys the factory instance.
382 private function shutdownLBFactory() {
383 // Commit and close up!
384 $factory = LBFactory
::singleton();
385 $factory->commitMasterChanges();
386 $factory->shutdown();
387 LBFactory
::destroyInstance();
391 * Perform one of the "standard" actions
393 * @param $article Article
395 private function performAction( $article ) {
396 global $wgSquidMaxage, $wgUseExternalEditor;
398 wfProfileIn( __METHOD__
);
400 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
401 $this->context
->output
, $article, $this->context
->title
,
402 $this->context
->user
, $this->context
->request
, $this ) ) )
404 wfProfileOut( __METHOD__
);
408 $act = $this->getAction();
410 $action = Action
::factory( $act, $article );
411 if( $action instanceof Action
){
413 wfProfileOut( __METHOD__
);
419 $this->context
->output
->setSquidMaxage( $wgSquidMaxage );
422 case 'raw': // includes JS/CSS
423 wfProfileIn( __METHOD__
. '-raw' );
424 $raw = new RawPage( $article );
426 wfProfileOut( __METHOD__
. '-raw' );
438 if ( session_id() == '' ) {
439 // Send a cookie so anons get talk message notifications
444 if ( wfRunHooks( 'CustomEditor', array( $article, $this->context
->user
) ) ) {
445 $internal = $this->context
->request
->getVal( 'internaledit' );
446 $external = $this->context
->request
->getVal( 'externaledit' );
447 $section = $this->context
->request
->getVal( 'section' );
448 $oldid = $this->context
->request
->getVal( 'oldid' );
449 if ( !$wgUseExternalEditor ||
$act == 'submit' ||
$internal ||
450 $section ||
$oldid ||
( !$this->context
->user
->getOption( 'externaleditor' ) && !$external ) ) {
451 $editor = new EditPage( $article );
453 } elseif ( $wgUseExternalEditor && ( $external ||
$this->context
->user
->getOption( 'externaleditor' ) ) ) {
454 $mode = $this->context
->request
->getVal( 'mode' );
455 $extedit = new ExternalEdit( $article, $mode );
461 if ( $this->context
->request
->getFullRequestURL() == $this->context
->title
->getInternalURL( 'action=history' ) ) {
462 $this->context
->output
->setSquidMaxage( $wgSquidMaxage );
464 $history = new HistoryPage( $article );
468 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
469 $this->context
->output
->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
472 wfProfileOut( __METHOD__
);