r85271 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85270‎ | r85271 | r85272 >
Date:19:39, 3 April 2011
Author:happy-melon
Status:ok
Tags:
Comment:
Store the WebRequest and OutputPage in the MediaWiki class, don't pass the global variables in to each function separately.
Modified paths:
  • /trunk/phase3/includes/Wiki.php (modified) (history)
  • /trunk/phase3/index.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Wiki.php
@@ -15,6 +15,20 @@
1616 private $params = array();
1717
1818 /**
 19+ * The request object
 20+ * @var WebRequest
 21+ */
 22+ private $request;
 23+
 24+ /**
 25+ * Output to deliver content to
 26+ * FIXME: most stuff in the codebase doesn't actually write to this, it'll write to
 27+ * $wgOut instead. So in practice this has to be $wgOut;
 28+ * @var OutputPage
 29+ */
 30+ private $output;
 31+
 32+ /**
1933 * Stores key/value pairs to circumvent global variables
2034 * Note that keys are case-insensitive!
2135 *
@@ -42,47 +56,65 @@
4357 return $default;
4458 }
4559
 60+ public function request( WebRequest &$x = null ){
 61+ return wfSetVar( $this->request, $x );
 62+ }
 63+
 64+ public function output( OutputPage &$x = null ){
 65+ return wfSetVar( $this->output, $x );
 66+ }
 67+
 68+ public function __construct( WebRequest &$request, /*OutputPage*/ &$output ){
 69+ $this->request =& $request;
 70+ $this->output =& $output;
 71+ }
 72+
4673 /**
4774 * Initialization of ... everything
4875 * Performs the request too
4976 *
5077 * @param $title Title ($wgTitle)
5178 * @param $article Article
52 - * @param $output OutputPage
5379 * @param $user User
54 - * @param $request WebRequest
5580 */
56 - public function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) {
 81+ public function performRequestForTitle( &$title, &$article, &$user ) {
5782 wfProfileIn( __METHOD__ );
5883
59 - $output->setTitle( $title );
60 - if ( $request->getVal( 'printable' ) === 'yes' ) {
61 - $output->setPrintable();
 84+ $this->output->setTitle( $title );
 85+ if ( $this->request->getVal( 'printable' ) === 'yes' ) {
 86+ $this->output->setPrintable();
6287 }
6388
64 - wfRunHooks( 'BeforeInitialize', array( &$title, &$article, &$output, &$user, $request, $this ) );
 89+ wfRunHooks( 'BeforeInitialize', array(
 90+ &$title,
 91+ &$article,
 92+ &$this->output,
 93+ &$user,
 94+ $this->request,
 95+ $this
 96+ ) );
6597
6698 // If the user is not logged in, the Namespace:title of the article must be in
6799 // the Read array in order for the user to see it. (We have to check here to
68100 // catch special pages etc. We check again in Article::view())
69101 if ( !is_null( $title ) && !$title->userCanRead() ) {
70 - $output->loginToUse();
71 - $this->finalCleanup( $output );
72 - $output->disable();
 102+ $this->output->loginToUse();
 103+ $this->finalCleanup();
 104+ $this->output->disable();
73105 wfProfileOut( __METHOD__ );
74106 return false;
75107 }
76108
77109 // Call handleSpecialCases() to deal with all special requests...
78 - if ( !$this->handleSpecialCases( $title, $output, $request ) ) {
 110+ if ( !$this->handleSpecialCases( $title ) ) {
79111 // ...otherwise treat it as an article view. The article
80112 // may be a redirect to another article or URL.
81 - $new_article = $this->initializeArticle( $title, $output, $request );
 113+ $new_article = $this->initializeArticle( $title );
82114 if ( is_object( $new_article ) ) {
83115 $article = $new_article;
84 - $this->performAction( $output, $article, $title, $user, $request );
 116+ $this->performAction( $article, $title, $user );
85117 } elseif ( is_string( $new_article ) ) {
86 - $output->redirect( $new_article );
 118+ $this->output->redirect( $new_article );
87119 } else {
88120 wfProfileOut( __METHOD__ );
89121 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
@@ -95,16 +127,15 @@
96128 * Checks some initial queries
97129 * FIXME: rename to parseTitle() ?
98130 *
99 - * @param $request WebRequest
100131 * @return Title object to be $wgTitle
101132 */
102 - /* private */ function checkInitialQueries( WebRequest $request ) {
 133+ /* private */ function checkInitialQueries() {
103134 global $wgContLang;
104135
105 - $curid = $request->getInt( 'curid' );
106 - $title = $request->getVal( 'title' );
 136+ $curid = $this->request->getInt( 'curid' );
 137+ $title = $this->request->getVal( 'title' );
107138
108 - if ( $request->getCheck( 'search' ) ) {
 139+ if ( $this->request->getCheck( 'search' ) ) {
109140 // Compatibility with old search URLs which didn't use Special:Search
110141 // Just check for presence here, so blank requests still
111142 // show the search page when using ugly URLs (bug 8054).
@@ -112,7 +143,7 @@
113144 } elseif ( $curid ) {
114145 // URLs like this are generated by RC, because rc_title isn't always accurate
115146 $ret = Title::newFromID( $curid );
116 - } elseif ( $title == '' && $this->getAction( $request ) != 'delete' ) {
 147+ } elseif ( $title == '' && $this->getAction() != 'delete' ) {
117148 $ret = Title::newMainPage();
118149 } else {
119150 $ret = Title::newFromURL( $title );
@@ -124,8 +155,8 @@
125156 // For non-special titles, check for implicit titles
126157 if ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
127158 // We can have urls with just ?diff=,?oldid= or even just ?diff=
128 - $oldid = $request->getInt( 'oldid' );
129 - $oldid = $oldid ? $oldid : $request->getInt( 'diff' );
 159+ $oldid = $this->request->getInt( 'oldid' );
 160+ $oldid = $oldid ? $oldid : $this->request->getInt( 'diff' );
130161 // Allow oldid to override a changed or missing title
131162 if ( $oldid ) {
132163 $rev = Revision::newFromId( $oldid );
@@ -143,44 +174,42 @@
144175 * - special pages
145176 *
146177 * @param $title Title
147 - * @param $output OutputPage
148 - * @param $request WebRequest
149178 * @return bool true if the request is already executed
150179 */
151 - private function handleSpecialCases( &$title, &$output, $request ) {
 180+ private function handleSpecialCases( &$title ) {
152181 wfProfileIn( __METHOD__ );
153182
154183 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
155184 if ( is_null( $title ) || ( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
156185 $title = SpecialPage::getTitleFor( 'Badtitle' );
157 - $output->setTitle( $title ); // bug 21456
 186+ $this->output->setTitle( $title ); // bug 21456
158187 // Die now before we mess up $wgArticle and the skin stops working
159188 throw new ErrorPageError( 'badtitle', 'badtitletext' );
160189
161190 // Interwiki redirects
162191 } else if ( $title->getInterwiki() != '' ) {
163 - $rdfrom = $request->getVal( 'rdfrom' );
 192+ $rdfrom = $this->request->getVal( 'rdfrom' );
164193 if ( $rdfrom ) {
165194 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
166195 } else {
167 - $query = $request->getValues();
 196+ $query = $this->request->getValues();
168197 unset( $query['title'] );
169198 $url = $title->getFullURL( $query );
170199 }
171200 /* Check for a redirect loop */
172201 if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $title->isLocal() ) {
173202 // 301 so google et al report the target as the actual url.
174 - $output->redirect( $url, 301 );
 203+ $this->output->redirect( $url, 301 );
175204 } else {
176205 $title = SpecialPage::getTitleFor( 'Badtitle' );
177 - $output->setTitle( $title ); // bug 21456
 206+ $this->output->setTitle( $title ); // bug 21456
178207 wfProfileOut( __METHOD__ );
179208 throw new ErrorPageError( 'badtitle', 'badtitletext' );
180209 }
181210 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
182 - } else if ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted()
183 - && ( $request->getVal( 'title' ) === null || $title->getPrefixedDBKey() != $request->getVal( 'title' ) )
184 - && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) )
 211+ } else if ( $this->request->getVal( 'action', 'view' ) == 'view' && !$this->request->wasPosted()
 212+ && ( $this->request->getVal( 'title' ) === null || $title->getPrefixedDBKey() != $this->request->getVal( 'title' ) )
 213+ && !count( array_diff( array_keys( $this->request->getValues() ), array( 'action', 'title' ) ) ) )
185214 {
186215 if ( $title->getNamespace() == NS_SPECIAL ) {
187216 list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
@@ -190,7 +219,7 @@
191220 }
192221 $targetUrl = $title->getFullURL();
193222 // Redirect to canonical url, make it a 301 to allow caching
194 - if ( $targetUrl == $request->getFullRequestURL() ) {
 223+ if ( $targetUrl == $this->request->getFullRequestURL() ) {
195224 $message = "Redirect loop detected!\n\n" .
196225 "This means the wiki got confused about what page was " .
197226 "requested; this sometimes happens when moving a wiki " .
@@ -214,8 +243,8 @@
215244 wfProfileOut( __METHOD__ );
216245 return false;
217246 } else {
218 - $output->setSquidMaxage( 1200 );
219 - $output->redirect( $targetUrl, '301' );
 247+ $this->output->setSquidMaxage( 1200 );
 248+ $this->output->redirect( $targetUrl, '301' );
220249 }
221250 // Special pages
222251 } else if ( NS_SPECIAL == $title->getNamespace() ) {
@@ -264,13 +293,12 @@
265294 * passed through the "action" parameter. Actions disabled in
266295 * $wgDisabledActions will be replaced by "nosuchaction"
267296 *
268 - * @param $request WebRequest
269297 * @return String: action
270298 */
271 - public function getAction( WebRequest $request ) {
 299+ public function getAction() {
272300 global $wgDisabledActions;
273301
274 - $action = $request->getVal( 'action', 'view' );
 302+ $action = $this->request->getVal( 'action', 'view' );
275303
276304 // Check for disabled actions
277305 if ( in_array( $action, $wgDisabledActions ) ) {
@@ -280,9 +308,9 @@
281309 // Workaround for bug #20966: inability of IE to provide an action dependent
282310 // on which submit button is clicked.
283311 if ( $action === 'historysubmit' ) {
284 - if ( $request->getBool( 'revisiondelete' ) ) {
 312+ if ( $this->request->getBool( 'revisiondelete' ) ) {
285313 return 'revisiondelete';
286 - } elseif ( $request->getBool( 'revisionmove' ) ) {
 314+ } elseif ( $this->request->getBool( 'revisionmove' ) ) {
287315 return 'revisionmove';
288316 } else {
289317 return 'view';
@@ -299,14 +327,12 @@
300328 * Create an Article object for the page, following redirects if needed.
301329 *
302330 * @param $title Title ($wgTitle)
303 - * @param $output OutputPage ($wgOut)
304 - * @param $request WebRequest ($wgRequest)
305331 * @return mixed an Article, or a string to redirect to another URL
306332 */
307 - private function initializeArticle( &$title, &$output, $request ) {
 333+ private function initializeArticle( &$title ) {
308334 wfProfileIn( __METHOD__ );
309335
310 - $action = $request->getVal( 'action', 'view' );
 336+ $action = $this->request->getVal( 'action', 'view' );
311337 $article = self::articleFromTitle( $title );
312338 // NS_MEDIAWIKI has no redirects.
313339 // It is also used for CSS/JS, so performance matters here...
@@ -318,9 +344,9 @@
319345 // Check for redirects ...
320346 $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
321347 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
322 - && !$request->getVal( 'oldid' ) && // ... and are not old revisions
323 - !$request->getVal( 'diff' ) && // ... and not when showing diff
324 - $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
 348+ && !$this->request->getVal( 'oldid' ) && // ... and are not old revisions
 349+ !$this->request->getVal( 'diff' ) && // ... and not when showing diff
 350+ $this->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
325351 // ... and the article is not a non-redirect image page with associated file
326352 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
327353 {
@@ -328,7 +354,7 @@
329355 $ignoreRedirect = $target = false;
330356
331357 wfRunHooks( 'InitializeArticleMaybeRedirect',
332 - array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
 358+ array( &$title, &$this->request, &$ignoreRedirect, &$target, &$article ) );
333359
334360 // Follow redirects only for... redirects.
335361 // If $target is set, then a hook wanted to redirect.
@@ -350,7 +376,7 @@
351377 $rarticle->setRedirectedFrom( $title );
352378 $article = $rarticle;
353379 $title = $target;
354 - $output->setTitle( $title );
 380+ $this->output->setTitle( $title );
355381 }
356382 }
357383 } else {
@@ -363,17 +389,15 @@
364390
365391 /**
366392 * Cleaning up request by doing deferred updates, DB transaction, and the output
367 - *
368 - * @param $output OutputPage
369393 */
370 - public function finalCleanup( &$output ) {
 394+ public function finalCleanup() {
371395 wfProfileIn( __METHOD__ );
372396 // Now commit any transactions, so that unreported errors after
373397 // output() don't roll back the whole DB transaction
374398 $factory = wfGetLBFactory();
375399 $factory->commitMasterChanges();
376400 // Output everything!
377 - $output->output();
 401+ $this->output->output();
378402 // Do any deferred jobs
379403 wfDoUpdates( 'commit' );
380404 // Close the session so that jobs don't access the current session
@@ -432,25 +456,23 @@
433457 /**
434458 * Perform one of the "standard" actions
435459 *
436 - * @param $output OutputPage
437460 * @param $article Article
438461 * @param $title Title
439462 * @param $user User
440 - * @param $request WebRequest
441463 */
442 - private function performAction( &$output, &$article, &$title, &$user, &$request ) {
 464+ private function performAction( &$article, &$title, &$user ) {
443465 wfProfileIn( __METHOD__ );
444466
445 - if ( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
 467+ if ( !wfRunHooks( 'MediaWikiPerformAction', array( $this->output, $article, $title, $user, $this->request, $this ) ) ) {
446468 wfProfileOut( __METHOD__ );
447469 return;
448470 }
449471
450 - $action = $this->getAction( $request );
 472+ $action = $this->getAction();
451473
452474 switch( $action ) {
453475 case 'view':
454 - $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
 476+ $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
455477 $article->view();
456478 break;
457479 case 'raw': // includes JS/CSS
@@ -503,24 +525,24 @@
504526 /* Continue... */
505527 case 'edit':
506528 if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
507 - $internal = $request->getVal( 'internaledit' );
508 - $external = $request->getVal( 'externaledit' );
509 - $section = $request->getVal( 'section' );
510 - $oldid = $request->getVal( 'oldid' );
 529+ $internal = $this->request->getVal( 'internaledit' );
 530+ $external = $this->request->getVal( 'externaledit' );
 531+ $section = $this->request->getVal( 'section' );
 532+ $oldid = $this->request->getVal( 'oldid' );
511533 if ( !$this->getVal( 'UseExternalEditor' ) || $action == 'submit' || $internal ||
512534 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
513535 $editor = new EditPage( $article );
514536 $editor->submit();
515537 } elseif ( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
516 - $mode = $request->getVal( 'mode' );
 538+ $mode = $this->request->getVal( 'mode' );
517539 $extedit = new ExternalEdit( $article, $mode );
518540 $extedit->edit();
519541 }
520542 }
521543 break;
522544 case 'history':
523 - if ( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
524 - $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
 545+ if ( $this->request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
 546+ $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
525547 }
526548 $history = new HistoryPage( $article );
527549 $history->history();
@@ -537,7 +559,7 @@
538560 break;
539561 default:
540562 if ( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
541 - $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
 563+ $this->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
542564 }
543565 }
544566 wfProfileOut( __METHOD__ );
Index: trunk/phase3/index.php
@@ -44,7 +44,7 @@
4545 wfProfileIn( 'index.php-setup' );
4646
4747 # Initialize MediaWiki base class
48 -$mediaWiki = new MediaWiki();
 48+$mediaWiki = new MediaWiki( $wgRequest, $wgOut );
4949
5050 $maxLag = $wgRequest->getVal( 'maxlag' );
5151 if ( !is_null( $maxLag ) ) {
@@ -56,7 +56,7 @@
5757 }
5858
5959 # Set title from request parameters
60 -$wgTitle = $mediaWiki->checkInitialQueries( $wgRequest );
 60+$wgTitle = $mediaWiki->checkInitialQueries();
6161 $action = $wgRequest->getVal( 'action', 'view' );
6262
6363 wfProfileOut( 'index.php-setup' );
@@ -88,7 +88,7 @@
8989 # Tell $wgOut that output is taken care of
9090 $wgOut->disable();
9191 wfProfileOut( 'index.php-filecache' );
92 - $mediaWiki->finalCleanup( $wgOut );
 92+ $mediaWiki->finalCleanup();
9393 wfProfileOut( 'index.php' );
9494 $mediaWiki->restInPeace();
9595 exit;
@@ -106,8 +106,8 @@
107107 $mediaWiki->setVal( 'UseExternalEditor', $wgUseExternalEditor );
108108 $mediaWiki->setVal( 'UsePathInfo', $wgUsePathInfo );
109109
110 -$mediaWiki->performRequestForTitle( $wgTitle, $wgArticle, $wgOut, $wgUser, $wgRequest );
111 -$mediaWiki->finalCleanup( $wgOut );
 110+$mediaWiki->performRequestForTitle( $wgTitle, $wgArticle, $wgUser );
 111+$mediaWiki->finalCleanup();
112112
113113 wfProfileOut( 'index.php' );
114114

Status & tagging log