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 * Initialization of ... everything
30 * Performs the request too
32 * @param $article Article
34 public function performRequestForTitle( &$article ) {
35 wfProfileIn( __METHOD__
);
37 if ( $this->context
->request
->getVal( 'printable' ) === 'yes' ) {
38 $this->context
->output
->setPrintable();
41 wfRunHooks( 'BeforeInitialize', array(
42 &$this->context
->title
,
44 &$this->context
->output
,
45 &$this->context
->user
,
46 $this->context
->request
,
50 // If the user is not logged in, the Namespace:title of the article must be in
51 // the Read array in order for the user to see it. (We have to check here to
52 // catch special pages etc. We check again in Article::view())
53 if ( !is_null( $this->context
->title
) && !$this->context
->title
->userCanRead() ) {
54 $this->context
->output
->loginToUse();
55 $this->finalCleanup();
56 $this->context
->output
->disable();
57 wfProfileOut( __METHOD__
);
61 // Call handleSpecialCases() to deal with all special requests...
62 if ( !$this->handleSpecialCases() ) {
63 // ...otherwise treat it as an article view. The article
64 // may be a redirect to another article or URL.
65 $new_article = $this->initializeArticle();
66 if ( is_object( $new_article ) ) {
67 $article = $new_article;
68 $this->performAction( $article );
69 } elseif ( is_string( $new_article ) ) {
70 $this->context
->output
->redirect( $new_article );
72 wfProfileOut( __METHOD__
);
73 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
76 wfProfileOut( __METHOD__
);
80 * Parse $request to get the Title object
82 * @return Title object to be $wgTitle
84 private function parseTitle() {
87 $curid = $this->context
->request
->getInt( 'curid' );
88 $title = $this->context
->request
->getVal( 'title' );
90 if ( $this->context
->request
->getCheck( 'search' ) ) {
91 // Compatibility with old search URLs which didn't use Special:Search
92 // Just check for presence here, so blank requests still
93 // show the search page when using ugly URLs (bug 8054).
94 $ret = SpecialPage
::getTitleFor( 'Search' );
96 // URLs like this are generated by RC, because rc_title isn't always accurate
97 $ret = Title
::newFromID( $curid );
98 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
99 $ret = Title
::newMainPage();
101 $ret = Title
::newFromURL( $title );
102 // check variant links so that interwiki links don't have to worry
103 // about the possible different language variants
104 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
105 $wgContLang->findVariantLink( $title, $ret );
108 // For non-special titles, check for implicit titles
109 if ( is_null( $ret ) ||
$ret->getNamespace() != NS_SPECIAL
) {
110 // We can have urls with just ?diff=,?oldid= or even just ?diff=
111 $oldid = $this->context
->request
->getInt( 'oldid' );
112 $oldid = $oldid ?
$oldid : $this->context
->request
->getInt( 'diff' );
113 // Allow oldid to override a changed or missing title
115 $rev = Revision
::newFromId( $oldid );
116 $ret = $rev ?
$rev->getTitle() : $ret;
120 if( $ret === null ||
( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
127 * Get the Title object that we'll be acting on, as specified in the WebRequest
130 public function getTitle(){
131 if( $this->context
->title
=== null ){
132 $this->context
->title
= $this->parseTitle();
134 return $this->context
->title
;
138 * Initialize some special cases:
140 * - local interwiki redirects
144 * @return bool true if the request is already executed
146 private function handleSpecialCases() {
147 global $wgServer, $wgUsePathInfo;
149 wfProfileIn( __METHOD__
);
151 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
152 if ( $this->context
->title
instanceof BadTitle
) {
153 throw new ErrorPageError( 'badtitle', 'badtitletext' );
155 // Interwiki redirects
156 } else if ( $this->context
->title
->getInterwiki() != '' ) {
157 $rdfrom = $this->context
->request
->getVal( 'rdfrom' );
159 $url = $this->context
->title
->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
161 $query = $this->context
->request
->getValues();
162 unset( $query['title'] );
163 $url = $this->context
->title
->getFullURL( $query );
165 // Check for a redirect loop
166 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $this->context
->title
->isLocal() ) {
167 // 301 so google et al report the target as the actual url.
168 $this->context
->output
->redirect( $url, 301 );
170 $this->context
->title
= new BadTitle
;
171 wfProfileOut( __METHOD__
);
172 throw new ErrorPageError( 'badtitle', 'badtitletext' );
174 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
175 } else if ( $this->context
->request
->getVal( 'action', 'view' ) == 'view' && !$this->context
->request
->wasPosted()
176 && ( $this->context
->request
->getVal( 'title' ) === null ||
$this->context
->title
->getPrefixedDBKey() != $this->context
->request
->getVal( 'title' ) )
177 && !count( array_diff( array_keys( $this->context
->request
->getValues() ), array( 'action', 'title' ) ) ) )
179 if ( $this->context
->title
->getNamespace() == NS_SPECIAL
) {
180 list( $name, $subpage ) = SpecialPageFactory
::resolveAlias( $this->context
->title
->getDBkey() );
182 $this->context
->title
= SpecialPage
::getTitleFor( $name, $subpage );
185 $targetUrl = $this->context
->title
->getFullURL();
186 // Redirect to canonical url, make it a 301 to allow caching
187 if ( $targetUrl == $this->context
->request
->getFullRequestURL() ) {
188 $message = "Redirect loop detected!\n\n" .
189 "This means the wiki got confused about what page was " .
190 "requested; this sometimes happens when moving a wiki " .
191 "to a new server or changing the server configuration.\n\n";
193 if ( $wgUsePathInfo ) {
194 $message .= "The wiki is trying to interpret the page " .
195 "title from the URL path portion (PATH_INFO), which " .
196 "sometimes fails depending on the web server. Try " .
197 "setting \"\$wgUsePathInfo = false;\" in your " .
198 "LocalSettings.php, or check that \$wgArticlePath " .
201 $message .= "Your web server was detected as possibly not " .
202 "supporting URL path components (PATH_INFO) correctly; " .
203 "check your LocalSettings.php for a customized " .
204 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
207 wfHttpError( 500, "Internal error", $message );
208 wfProfileOut( __METHOD__
);
211 $this->context
->output
->setSquidMaxage( 1200 );
212 $this->context
->output
->redirect( $targetUrl, '301' );
215 } else if ( NS_SPECIAL
== $this->context
->title
->getNamespace() ) {
216 // actions that need to be made when we have a special pages
217 SpecialPageFactory
::executePath( $this->context
->title
, $this->context
);
219 // No match to special cases
220 wfProfileOut( __METHOD__
);
223 // Did match a special case
224 wfProfileOut( __METHOD__
);
229 * Create an Article object of the appropriate class for the given page.
231 * @deprecated in 1.19; use Article::newFromTitle() instead
232 * @param $title Title
233 * @param $context RequestContext
234 * @return Article object
236 public static function articleFromTitle( $title, RequestContext
$context ) {
237 return Article
::newFromTitle( $title, $context );
241 * Returns the action that will be executed, not necesserly the one passed
242 * passed through the "action" parameter. Actions disabled in
243 * $wgDisabledActions will be replaced by "nosuchaction"
245 * @return String: action
247 public function getAction() {
248 global $wgDisabledActions;
250 $action = $this->context
->request
->getVal( 'action', 'view' );
252 // Check for disabled actions
253 if ( in_array( $action, $wgDisabledActions ) ) {
254 return 'nosuchaction';
257 // Workaround for bug #20966: inability of IE to provide an action dependent
258 // on which submit button is clicked.
259 if ( $action === 'historysubmit' ) {
260 if ( $this->context
->request
->getBool( 'revisiondelete' ) ) {
261 return 'revisiondelete';
265 } elseif ( $action == 'editredlink' ) {
273 * Initialize the main Article object for "standard" actions (view, etc)
274 * Create an Article object for the page, following redirects if needed.
276 * @return mixed an Article, or a string to redirect to another URL
278 private function initializeArticle() {
279 global $wgDisableHardRedirects;
281 wfProfileIn( __METHOD__
);
283 $action = $this->context
->request
->getVal( 'action', 'view' );
284 $article = Article
::newFromTitle( $this->context
->title
, $this->context
);
285 // NS_MEDIAWIKI has no redirects.
286 // It is also used for CSS/JS, so performance matters here...
287 if ( $this->context
->title
->getNamespace() == NS_MEDIAWIKI
) {
288 wfProfileOut( __METHOD__
);
291 // Namespace might change when using redirects
292 // Check for redirects ...
293 $file = ( $this->context
->title
->getNamespace() == NS_FILE
) ?
$article->getFile() : null;
294 if ( ( $action == 'view' ||
$action == 'render' ) // ... for actions that show content
295 && !$this->context
->request
->getVal( 'oldid' ) && // ... and are not old revisions
296 !$this->context
->request
->getVal( 'diff' ) && // ... and not when showing diff
297 $this->context
->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 wfRunHooks( 'InitializeArticleMaybeRedirect',
305 array( &$this->context
->title
, &$this->context
->request
, &$ignoreRedirect, &$target, &$article ) );
307 // Follow redirects only for... redirects.
308 // If $target is set, then a hook wanted to redirect.
309 if ( !$ignoreRedirect && ( $target ||
$article->isRedirect() ) ) {
310 // Is the target already set by an extension?
311 $target = $target ?
$target : $article->followRedirect();
312 if ( is_string( $target ) ) {
313 if ( !$wgDisableHardRedirects ) {
314 // we'll need to redirect
315 wfProfileOut( __METHOD__
);
319 if ( is_object( $target ) ) {
320 // Rewrite environment to redirected article
321 $rarticle = Article
::newFromTitle( $target, $this->context
);
322 $rarticle->loadPageData();
323 if ( $rarticle->exists() ||
( is_object( $file ) && !$file->isLocal() ) ) {
324 $rarticle->setRedirectedFrom( $this->context
->title
);
325 $article = $rarticle;
326 $this->context
->title
= $target;
330 $this->context
->title
= $article->getTitle();
333 wfProfileOut( __METHOD__
);
338 * Cleaning up request by doing deferred updates, DB transaction, and the output
340 public function finalCleanup() {
341 wfProfileIn( __METHOD__
);
342 // Now commit any transactions, so that unreported errors after
343 // output() don't roll back the whole DB transaction
344 $factory = wfGetLBFactory();
345 $factory->commitMasterChanges();
346 // Output everything!
347 $this->context
->output
->output();
348 // Do any deferred jobs
349 wfDoUpdates( 'commit' );
350 // Close the session so that jobs don't access the current session
351 session_write_close();
353 wfProfileOut( __METHOD__
);
357 * Do a job from the job queue
359 private function doJobs() {
360 global $wgJobRunRate;
362 if ( $wgJobRunRate <= 0 ||
wfReadOnly() ) {
365 if ( $wgJobRunRate < 1 ) {
366 $max = mt_getrandmax();
367 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
372 $n = intval( $wgJobRunRate );
375 while ( $n-- && false != ( $job = Job
::pop() ) ) {
376 $output = $job->toString() . "\n";
378 $success = $job->run();
380 $t = round( $t * 1000 );
382 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
384 $output .= "Success, Time: $t ms\n";
386 wfDebugLog( 'jobqueue', $output );
391 * Ends this task peacefully
393 public function restInPeace() {
394 MessageCache
::logMessages();
395 wfLogProfilingData();
396 // Commit and close up!
397 $factory = wfGetLBFactory();
398 $factory->commitMasterChanges();
399 $factory->shutdown();
400 wfDebug( "Request ended normally\n" );
404 * Perform one of the "standard" actions
406 * @param $article Article
408 private function performAction( $article ) {
409 global $wgSquidMaxage, $wgUseExternalEditor,
410 $wgEnableDublinCoreRdf, $wgEnableCreativeCommonsRdf;
412 wfProfileIn( __METHOD__
);
414 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
415 $this->context
->output
, $article, $this->context
->title
,
416 $this->context
->user
, $this->context
->request
, $this ) ) )
418 wfProfileOut( __METHOD__
);
422 $act = $this->getAction();
424 $action = Action
::factory( $act, $article );
425 if( $action instanceof Action
){
427 wfProfileOut( __METHOD__
);
433 $this->context
->output
->setSquidMaxage( $wgSquidMaxage );
436 case 'raw': // includes JS/CSS
437 wfProfileIn( __METHOD__
. '-raw' );
438 $raw = new RawPage( $article );
440 wfProfileOut( __METHOD__
. '-raw' );
448 case 'markpatrolled':
450 case 'deletetrackback':
454 if ( !$wgEnableDublinCoreRdf ) {
455 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
457 $rdf = new DublinCoreRdf( $article );
461 case 'creativecommons':
462 if ( !$wgEnableCreativeCommonsRdf ) {
463 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
465 $rdf = new CreativeCommonsRdf( $article );
470 if ( session_id() == '' ) {
471 // Send a cookie so anons get talk message notifications
476 if ( wfRunHooks( 'CustomEditor', array( $article, $this->context
->user
) ) ) {
477 $internal = $this->context
->request
->getVal( 'internaledit' );
478 $external = $this->context
->request
->getVal( 'externaledit' );
479 $section = $this->context
->request
->getVal( 'section' );
480 $oldid = $this->context
->request
->getVal( 'oldid' );
481 if ( !$wgUseExternalEditor ||
$act == 'submit' ||
$internal ||
482 $section ||
$oldid ||
( !$this->context
->user
->getOption( 'externaleditor' ) && !$external ) ) {
483 $editor = new EditPage( $article );
485 } elseif ( $wgUseExternalEditor && ( $external ||
$this->context
->user
->getOption( 'externaleditor' ) ) ) {
486 $mode = $this->context
->request
->getVal( 'mode' );
487 $extedit = new ExternalEdit( $article, $mode );
493 if ( $this->context
->request
->getFullRequestURL() == $this->context
->title
->getInternalURL( 'action=history' ) ) {
494 $this->context
->output
->setSquidMaxage( $wgSquidMaxage );
496 $history = new HistoryPage( $article );
499 case 'revisiondelete':
500 // For show/hide submission from history page
501 $special = SpecialPageFactory
::getPage( 'Revisiondelete' );
502 $special->execute( '' );
505 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
506 $this->context
->output
->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
509 wfProfileOut( __METHOD__
);