r85288 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85287‎ | r85288 | r85289 >
Date:22:09, 3 April 2011
Author:happy-melon
Status:reverted (Comments)
Tags:
Comment:
Implement magic accessors for RequestContext variables: you can now just call $context->request->stuff(), and that is internally mapped to the get accessor. Rename the private variables to the old $mName syntax: I know that that's "discouraged in new code", but in this case it stops over-clever IDEs highlighting the magic accesses as potential visibility errors and sticking big error tags on them.
Modified paths:
  • /trunk/phase3/includes/RequestContext.php (modified) (history)
  • /trunk/phase3/includes/Wiki.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/RequestContext.php
@@ -1,19 +1,19 @@
22 <?php
33 /**
44 * Group all the pieces relevant to the context of a request into one instance
5 - *
 5+ *
66 * @author IAlex
77 * @author Daniel Friesen
88 * @file
99 */
1010
1111 class RequestContext {
12 - private $request; /// WebRequest object
13 - private $title; /// Title object
14 - private $output; /// OutputPage object
15 - private $user; /// User object
16 - private $lang; /// Language object
17 - private $skin; /// Skin object
 12+ private $mRequest; // / WebRequest object
 13+ private $mTitle; // / Title object
 14+ private $mOutput; // / OutputPage object
 15+ private $mUser; // / User object
 16+ private $mLang; // / Language object
 17+ private $mSkin; // / Skin object
1818
1919 /**
2020 * Set the WebRequest object
@@ -21,7 +21,7 @@
2222 * @param $r WebRequest object
2323 */
2424 public function setRequest( WebRequest $r ) {
25 - $this->request = $r;
 25+ $this->mRequest = $r;
2626 }
2727
2828 /**
@@ -30,11 +30,11 @@
3131 * @return WebRequest
3232 */
3333 public function getRequest() {
34 - if ( !isset($this->request) ) {
 34+ if ( !isset( $this->mRequest ) ) {
3535 global $wgRequest; # fallback to $wg till we can improve this
36 - $this->request = $wgRequest;
 36+ $this->mRequest = $wgRequest;
3737 }
38 - return $this->request;
 38+ return $this->mRequest;
3939 }
4040
4141 /**
@@ -43,7 +43,7 @@
4444 * @param $t Title object
4545 */
4646 public function setTitle( Title $t ) {
47 - $this->title = $t;
 47+ $this->mTitle = $t;
4848 }
4949
5050 /**
@@ -52,11 +52,11 @@
5353 * @return Title
5454 */
5555 public function getTitle() {
56 - if ( !isset($this->title) ) {
 56+ if ( !isset( $this->mTitle ) ) {
5757 global $wgTitle; # fallback to $wg till we can improve this
58 - $this->title = $wgTitle;
 58+ $this->mTitle = $wgTitle;
5959 }
60 - return $this->title;
 60+ return $this->mTitle;
6161 }
6262
6363 /**
@@ -64,8 +64,8 @@
6565 *
6666 * @param $u OutputPage
6767 */
68 - public function setOutput( OutputPage $u ) {
69 - $this->output = $u;
 68+ public function setOutput( OutputPage $o ) {
 69+ $this->mOutput = $o;
7070 }
7171
7272 /**
@@ -74,11 +74,11 @@
7575 * @return OutputPage object
7676 */
7777 public function getOutput() {
78 - if ( !isset($this->output) ) {
79 - $this->output = new OutputPage;
80 - $this->output->setContext( $this );
 78+ if ( !isset( $this->mOutput ) ) {
 79+ $this->mOutput = new OutputPage;
 80+ $this->mOutput->setContext( $this );
8181 }
82 - return $this->output;
 82+ return $this->mOutput;
8383 }
8484
8585 /**
@@ -87,7 +87,7 @@
8888 * @param $u User
8989 */
9090 public function setUser( User $u ) {
91 - $this->user = $u;
 91+ $this->mUser = $u;
9292 }
9393
9494 /**
@@ -96,11 +96,13 @@
9797 * @return User
9898 */
9999 public function getUser() {
100 - if ( !isset($this->user) ) {
 100+ if ( !isset( $this->mUser ) ) {
101101 global $wgCommandLineMode;
102 - $this->user = $wgCommandLineMode ? new User : User::newFromSession( $this->getRequest() );
 102+ $this->mUser = $wgCommandLineMode
 103+ ? new User
 104+ : User::newFromSession( $this->getRequest() );
103105 }
104 - return $this->user;
 106+ return $this->mUser;
105107 }
106108
107109 /**
@@ -109,28 +111,31 @@
110112 * @return Language
111113 */
112114 public function getLang() {
113 - if ( !isset($this->lang) ) {
 115+ if ( !isset( $this->mLang ) ) {
114116 global $wgLanguageCode, $wgContLang;
115 - $code = $this->getRequest()->getVal( 'uselang', $this->getUser()->getOption( 'language' ) );
 117+ $code = $this->getRequest()->getVal(
 118+ 'uselang',
 119+ $this->getUser()->getOption( 'language' )
 120+ );
116121 // BCP 47 - letter case MUST NOT carry meaning
117122 $code = strtolower( $code );
118123
119124 # Validate $code
120 - if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
 125+ if ( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
121126 wfDebug( "Invalid user language code\n" );
122127 $code = $wgLanguageCode;
123128 }
124129
125130 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
126131
127 - if( $code === $wgLanguageCode ) {
128 - $this->lang = $wgContLang;
 132+ if ( $code === $wgLanguageCode ) {
 133+ $this->mLang = $wgContLang;
129134 } else {
130135 $obj = Language::factory( $code );
131 - $this->lang = $obj;
 136+ $this->mLang = $obj;
132137 }
133138 }
134 - return $this->lang;
 139+ return $this->mLang;
135140 }
136141
137142 /**
@@ -139,11 +144,11 @@
140145 * @return Skin
141146 */
142147 public function getSkin() {
143 - if ( !isset($this->skin) ) {
 148+ if ( !isset( $this->mSkin ) ) {
144149 wfProfileIn( __METHOD__ . '-createskin' );
145 -
 150+
146151 global $wgHiddenPrefs;
147 - if( !in_array( 'skin', $wgHiddenPrefs ) ) {
 152+ if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
148153 # get the user skin
149154 $userSkin = $this->getUser()->getOption( 'skin' );
150155 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
@@ -153,11 +158,11 @@
154159 $userSkin = $wgDefaultSkin;
155160 }
156161
157 - $this->skin = Skin::newFromKey( $userSkin );
158 - $this->skin->setContext( $this );
 162+ $this->mSkin = Skin::newFromKey( $userSkin );
 163+ $this->mSkin->setContext( $this );
159164 wfProfileOut( __METHOD__ . '-createskin' );
160165 }
161 - return $this->skin;
 166+ return $this->mSkin;
162167 }
163168
164169 /** Helpful methods **/
@@ -182,10 +187,31 @@
183188 */
184189 public static function getMain() {
185190 static $instance = null;
186 - if ( !isset($instance) ) {
 191+ if ( !isset( $instance ) ) {
187192 $instance = new self;
188193 }
189194 return $instance;
190195 }
 196+
 197+ /**
 198+ * Make these C#-style accessors, so you can do $context->user->getName() which is
 199+ * internally mapped to $context->__get('user')->getName() which is mapped to
 200+ * $context->getUser()->getName()
 201+ */
 202+ public function __get( $name ) {
 203+ if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
 204+ $fname = 'get' . ucfirst( $name );
 205+ return $this->$fname();
 206+ }
 207+ trigger_error( "Undefined property {$name}", E_NOTICE );
 208+ }
 209+
 210+ public function __set( $name, $value ) {
 211+ if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
 212+ $fname = 'set' . ucfirst( $name );
 213+ return $this->$fname( $value );
 214+ }
 215+ trigger_error( "Undefined property {$name}", E_NOTICE );
 216+ }
191217 }
192218
Index: trunk/phase3/includes/Wiki.php
@@ -15,26 +15,6 @@
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 - /**
33 - * The Title we'll be working on
34 - * @var Title
35 - */
36 - private $title;
37 -
38 - /**
3919 * TODO: fold $output, etc, into this
4020 * @var RequestContext
4121 */
@@ -69,22 +49,18 @@
7050 }
7151
7252 public function request( WebRequest &$x = null ){
73 - return wfSetVar( $this->request, $x );
 53+ return wfSetVar( $this->context->request, $x );
7454 }
7555
7656 public function output( OutputPage &$x = null ){
77 - return wfSetVar( $this->output, $x );
 57+ return wfSetVar( $this->context->output, $x );
7858 }
7959
8060 public function __construct( WebRequest &$request, /*OutputPage*/ &$output ){
81 - $this->request =& $request;
82 - $this->output =& $output;
83 - $this->title = $this->parseTitle();
84 -
8561 $this->context = new RequestContext();
86 - $this->context->setRequest( $this->request );
87 - $this->context->setOutput( $this->output );
88 - $this->context->setTitle( $this->title );
 62+ $this->context->setRequest( $request );
 63+ $this->context->setOutput( $output );
 64+ $this->context->setTitle( $this->parseTitle() );
8965 }
9066
9167 /**
@@ -97,27 +73,27 @@
9874 public function performRequestForTitle( &$article, &$user ) {
9975 wfProfileIn( __METHOD__ );
10076
101 - $this->output->setTitle( $this->title );
102 - if ( $this->request->getVal( 'printable' ) === 'yes' ) {
103 - $this->output->setPrintable();
 77+ $this->context->output->setTitle( $this->context->title );
 78+ if ( $this->context->request->getVal( 'printable' ) === 'yes' ) {
 79+ $this->context->output->setPrintable();
10480 }
10581
10682 wfRunHooks( 'BeforeInitialize', array(
107 - &$this->title,
 83+ &$this->context->title,
10884 &$article,
109 - &$this->output,
 85+ &$this->context->output,
11086 &$user,
111 - $this->request,
 87+ $this->context->request,
11288 $this
11389 ) );
11490
11591 // If the user is not logged in, the Namespace:title of the article must be in
11692 // the Read array in order for the user to see it. (We have to check here to
11793 // catch special pages etc. We check again in Article::view())
118 - if ( !is_null( $this->title ) && !$this->title->userCanRead() ) {
119 - $this->output->loginToUse();
 94+ if ( !is_null( $this->context->title ) && !$this->context->title->userCanRead() ) {
 95+ $this->context->output->loginToUse();
12096 $this->finalCleanup();
121 - $this->output->disable();
 97+ $this->context->output->disable();
12298 wfProfileOut( __METHOD__ );
12399 return false;
124100 }
@@ -131,7 +107,7 @@
132108 $article = $new_article;
133109 $this->performAction( $article, $user );
134110 } elseif ( is_string( $new_article ) ) {
135 - $this->output->redirect( $new_article );
 111+ $this->context->output->redirect( $new_article );
136112 } else {
137113 wfProfileOut( __METHOD__ );
138114 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
@@ -148,10 +124,10 @@
149125 private function parseTitle() {
150126 global $wgContLang;
151127
152 - $curid = $this->request->getInt( 'curid' );
153 - $title = $this->request->getVal( 'title' );
 128+ $curid = $this->context->request->getInt( 'curid' );
 129+ $title = $this->context->request->getVal( 'title' );
154130
155 - if ( $this->request->getCheck( 'search' ) ) {
 131+ if ( $this->context->request->getCheck( 'search' ) ) {
156132 // Compatibility with old search URLs which didn't use Special:Search
157133 // Just check for presence here, so blank requests still
158134 // show the search page when using ugly URLs (bug 8054).
@@ -171,8 +147,8 @@
172148 // For non-special titles, check for implicit titles
173149 if ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
174150 // We can have urls with just ?diff=,?oldid= or even just ?diff=
175 - $oldid = $this->request->getInt( 'oldid' );
176 - $oldid = $oldid ? $oldid : $this->request->getInt( 'diff' );
 151+ $oldid = $this->context->request->getInt( 'oldid' );
 152+ $oldid = $oldid ? $oldid : $this->context->request->getInt( 'diff' );
177153 // Allow oldid to override a changed or missing title
178154 if ( $oldid ) {
179155 $rev = Revision::newFromId( $oldid );
@@ -187,10 +163,10 @@
188164 * @return Title
189165 */
190166 public function getTitle(){
191 - if( $this->title === null ){
192 - $this->title = $this->parseTitle();
 167+ if( $this->context->title === null ){
 168+ $this->context->title = $this->parseTitle();
193169 }
194 - return $this->title;
 170+ return $this->context->title;
195171 }
196172
197173 /**
@@ -206,46 +182,46 @@
207183 wfProfileIn( __METHOD__ );
208184
209185 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
210 - if ( is_null( $this->title ) || ( ( $this->title->getDBkey() == '' ) && ( $this->title->getInterwiki() == '' ) ) ) {
211 - $this->title = SpecialPage::getTitleFor( 'Badtitle' );
212 - $this->output->setTitle( $this->title ); // bug 21456
 186+ if ( is_null( $this->context->title ) || ( ( $this->context->title->getDBkey() == '' ) && ( $this->context->title->getInterwiki() == '' ) ) ) {
 187+ $this->context->title = SpecialPage::getTitleFor( 'Badtitle' );
 188+ $this->context->output->setTitle( $this->context->title ); // bug 21456
213189 // Die now before we mess up $wgArticle and the skin stops working
214190 throw new ErrorPageError( 'badtitle', 'badtitletext' );
215191
216192 // Interwiki redirects
217 - } else if ( $this->title->getInterwiki() != '' ) {
218 - $rdfrom = $this->request->getVal( 'rdfrom' );
 193+ } else if ( $this->context->title->getInterwiki() != '' ) {
 194+ $rdfrom = $this->context->request->getVal( 'rdfrom' );
219195 if ( $rdfrom ) {
220 - $url = $this->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
 196+ $url = $this->context->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
221197 } else {
222 - $query = $this->request->getValues();
 198+ $query = $this->context->request->getValues();
223199 unset( $query['title'] );
224 - $url = $this->title->getFullURL( $query );
 200+ $url = $this->context->title->getFullURL( $query );
225201 }
226202 /* Check for a redirect loop */
227 - if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $this->title->isLocal() ) {
 203+ if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $this->context->title->isLocal() ) {
228204 // 301 so google et al report the target as the actual url.
229 - $this->output->redirect( $url, 301 );
 205+ $this->context->output->redirect( $url, 301 );
230206 } else {
231 - $this->title = SpecialPage::getTitleFor( 'Badtitle' );
232 - $this->output->setTitle( $this->title ); // bug 21456
 207+ $this->context->title = SpecialPage::getTitleFor( 'Badtitle' );
 208+ $this->context->output->setTitle( $this->context->title ); // bug 21456
233209 wfProfileOut( __METHOD__ );
234210 throw new ErrorPageError( 'badtitle', 'badtitletext' );
235211 }
236212 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
237 - } else if ( $this->request->getVal( 'action', 'view' ) == 'view' && !$this->request->wasPosted()
238 - && ( $this->request->getVal( 'title' ) === null || $this->title->getPrefixedDBKey() != $this->request->getVal( 'title' ) )
239 - && !count( array_diff( array_keys( $this->request->getValues() ), array( 'action', 'title' ) ) ) )
 213+ } else if ( $this->context->request->getVal( 'action', 'view' ) == 'view' && !$this->context->request->wasPosted()
 214+ && ( $this->context->request->getVal( 'title' ) === null || $this->context->title->getPrefixedDBKey() != $this->context->request->getVal( 'title' ) )
 215+ && !count( array_diff( array_keys( $this->context->request->getValues() ), array( 'action', 'title' ) ) ) )
240216 {
241 - if ( $this->title->getNamespace() == NS_SPECIAL ) {
242 - list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $this->title->getDBkey() );
 217+ if ( $this->context->title->getNamespace() == NS_SPECIAL ) {
 218+ list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $this->context->title->getDBkey() );
243219 if ( $name ) {
244 - $this->title = SpecialPage::getTitleFor( $name, $subpage );
 220+ $this->context->title = SpecialPage::getTitleFor( $name, $subpage );
245221 }
246222 }
247 - $targetUrl = $this->title->getFullURL();
 223+ $targetUrl = $this->context->title->getFullURL();
248224 // Redirect to canonical url, make it a 301 to allow caching
249 - if ( $targetUrl == $this->request->getFullRequestURL() ) {
 225+ if ( $targetUrl == $this->context->request->getFullRequestURL() ) {
250226 $message = "Redirect loop detected!\n\n" .
251227 "This means the wiki got confused about what page was " .
252228 "requested; this sometimes happens when moving a wiki " .
@@ -269,13 +245,13 @@
270246 wfProfileOut( __METHOD__ );
271247 return false;
272248 } else {
273 - $this->output->setSquidMaxage( 1200 );
274 - $this->output->redirect( $targetUrl, '301' );
 249+ $this->context->output->setSquidMaxage( 1200 );
 250+ $this->context->output->redirect( $targetUrl, '301' );
275251 }
276252 // Special pages
277 - } else if ( NS_SPECIAL == $this->title->getNamespace() ) {
 253+ } else if ( NS_SPECIAL == $this->context->title->getNamespace() ) {
278254 /* actions that need to be made when we have a special pages */
279 - SpecialPage::executePath( $this->title, $this->context );
 255+ SpecialPage::executePath( $this->context->title, $this->context );
280256 } else {
281257 /* No match to special cases */
282258 wfProfileOut( __METHOD__ );
@@ -324,7 +300,7 @@
325301 public function getAction() {
326302 global $wgDisabledActions;
327303
328 - $action = $this->request->getVal( 'action', 'view' );
 304+ $action = $this->context->request->getVal( 'action', 'view' );
329305
330306 // Check for disabled actions
331307 if ( in_array( $action, $wgDisabledActions ) ) {
@@ -334,9 +310,9 @@
335311 // Workaround for bug #20966: inability of IE to provide an action dependent
336312 // on which submit button is clicked.
337313 if ( $action === 'historysubmit' ) {
338 - if ( $this->request->getBool( 'revisiondelete' ) ) {
 314+ if ( $this->context->request->getBool( 'revisiondelete' ) ) {
339315 return 'revisiondelete';
340 - } elseif ( $this->request->getBool( 'revisionmove' ) ) {
 316+ } elseif ( $this->context->request->getBool( 'revisionmove' ) ) {
341317 return 'revisionmove';
342318 } else {
343319 return 'view';
@@ -357,21 +333,21 @@
358334 private function initializeArticle() {
359335 wfProfileIn( __METHOD__ );
360336
361 - $action = $this->request->getVal( 'action', 'view' );
362 - $article = self::articleFromTitle( $this->title );
 337+ $action = $this->context->request->getVal( 'action', 'view' );
 338+ $article = self::articleFromTitle( $this->context->title );
363339 // NS_MEDIAWIKI has no redirects.
364340 // It is also used for CSS/JS, so performance matters here...
365 - if ( $this->title->getNamespace() == NS_MEDIAWIKI ) {
 341+ if ( $this->context->title->getNamespace() == NS_MEDIAWIKI ) {
366342 wfProfileOut( __METHOD__ );
367343 return $article;
368344 }
369345 // Namespace might change when using redirects
370346 // Check for redirects ...
371 - $file = ( $this->title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
 347+ $file = ( $this->context->title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
372348 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
373 - && !$this->request->getVal( 'oldid' ) && // ... and are not old revisions
374 - !$this->request->getVal( 'diff' ) && // ... and not when showing diff
375 - $this->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
 349+ && !$this->context->request->getVal( 'oldid' ) && // ... and are not old revisions
 350+ !$this->context->request->getVal( 'diff' ) && // ... and not when showing diff
 351+ $this->context->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
376352 // ... and the article is not a non-redirect image page with associated file
377353 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
378354 {
@@ -379,7 +355,7 @@
380356 $ignoreRedirect = $target = false;
381357
382358 wfRunHooks( 'InitializeArticleMaybeRedirect',
383 - array( &$this->title, &$this->request, &$ignoreRedirect, &$target, &$article ) );
 359+ array( &$this->context->title, &$this->context->request, &$ignoreRedirect, &$target, &$article ) );
384360
385361 // Follow redirects only for... redirects.
386362 // If $target is set, then a hook wanted to redirect.
@@ -398,14 +374,14 @@
399375 $rarticle = self::articleFromTitle( $target );
400376 $rarticle->loadPageData();
401377 if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
402 - $rarticle->setRedirectedFrom( $this->title );
 378+ $rarticle->setRedirectedFrom( $this->context->title );
403379 $article = $rarticle;
404 - $this->title = $target;
405 - $this->output->setTitle( $this->title );
 380+ $this->context->title = $target;
 381+ $this->context->output->setTitle( $this->context->title );
406382 }
407383 }
408384 } else {
409 - $this->title = $article->getTitle();
 385+ $this->context->title = $article->getTitle();
410386 }
411387 }
412388 wfProfileOut( __METHOD__ );
@@ -422,7 +398,7 @@
423399 $factory = wfGetLBFactory();
424400 $factory->commitMasterChanges();
425401 // Output everything!
426 - $this->output->output();
 402+ $this->context->output->output();
427403 // Do any deferred jobs
428404 wfDoUpdates( 'commit' );
429405 // Close the session so that jobs don't access the current session
@@ -488,8 +464,8 @@
489465 wfProfileIn( __METHOD__ );
490466
491467 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
492 - $this->output, $article, $this->title,
493 - $user, $this->request, $this ) ) )
 468+ $this->context->output, $article, $this->context->title,
 469+ $user, $this->context->request, $this ) ) )
494470 {
495471 wfProfileOut( __METHOD__ );
496472 return;
@@ -499,7 +475,7 @@
500476
501477 switch( $action ) {
502478 case 'view':
503 - $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
 479+ $this->context->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
504480 $article->view();
505481 break;
506482 case 'raw': // includes JS/CSS
@@ -552,24 +528,24 @@
553529 /* Continue... */
554530 case 'edit':
555531 if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
556 - $internal = $this->request->getVal( 'internaledit' );
557 - $external = $this->request->getVal( 'externaledit' );
558 - $section = $this->request->getVal( 'section' );
559 - $oldid = $this->request->getVal( 'oldid' );
 532+ $internal = $this->context->request->getVal( 'internaledit' );
 533+ $external = $this->context->request->getVal( 'externaledit' );
 534+ $section = $this->context->request->getVal( 'section' );
 535+ $oldid = $this->context->request->getVal( 'oldid' );
560536 if ( !$this->getVal( 'UseExternalEditor' ) || $action == 'submit' || $internal ||
561537 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
562538 $editor = new EditPage( $article );
563539 $editor->submit();
564540 } elseif ( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
565 - $mode = $this->request->getVal( 'mode' );
 541+ $mode = $this->context->request->getVal( 'mode' );
566542 $extedit = new ExternalEdit( $article, $mode );
567543 $extedit->edit();
568544 }
569545 }
570546 break;
571547 case 'history':
572 - if ( $this->request->getFullRequestURL() == $this->title->getInternalURL( 'action=history' ) ) {
573 - $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
 548+ if ( $this->context->request->getFullRequestURL() == $this->context->title->getInternalURL( 'action=history' ) ) {
 549+ $this->context->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
574550 }
575551 $history = new HistoryPage( $article );
576552 $history->history();
@@ -586,7 +562,7 @@
587563 break;
588564 default:
589565 if ( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
590 - $this->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
 566+ $this->context->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
591567 }
592568 }
593569 wfProfileOut( __METHOD__ );

Follow-up revisions

RevisionCommit summaryAuthorDate
r89406Start unpicking r85288 (magic __get() accessor for RequestContext). Instead,...happy-melon10:54, 3 June 2011
r89408More unpicking of r85288. I think this is all of the magic method calls, but...happy-melon11:04, 3 June 2011
r90899Revert r85288 (magic accessors for RequestContext); much more trouble than th...happy-melon19:23, 27 June 2011

Comments

#Comment by Reedy (talk | contribs)   19:52, 1 June 2011

Got a big of a whinge here...

class RequestContext {

	/**
	 * @var WebRequest
	 */
	private $mRequest;
class MediaWiki {

	/**
	 * TODO: fold $output, etc, into this
	 * @var RequestContext
	 */
	private $context;

	public function request( WebRequest $x = null ){
		return wfSetVar( $this->context->request, $x );
	}

	public function output( OutputPage $x = null ){
		return wfSetVar( $this->context->output, $x );
	}


We've got the usage of $output and $mOutput inside and outside of the context, and it looks messy

Same for $request and $mRequest

#Comment by Happy-melon (talk | contribs)   23:14, 1 June 2011

I don't think anything's actually wrong with that code in terms of variable access/visibility/whatever. The magic methods just add an exotic twist to working out what the best naming convention is...

I'm actually not so sure about this approach any more; in r86872 I wanted to make RequestContext itself an IContextSource, and not being able to do so offended my sense of recursion... :D Plus it's not so good for type hinting. I think just abstracting this away by reimplementing r86872 would be preferable.

#Comment by Reedy (talk | contribs)   23:36, 1 June 2011

Just seemingly having 2 references to possibly multiple objects of the same things seems a bit daft/wrong :)

Which is more confusing. I was looking at index.php and noticed both seemed to be used at a low level above it (in the RequestContext and the MediaWiki classes)

If you want me to find it specifically, let me know, but it'd be nice to tidy it up

#Comment by Reedy (talk | contribs)   23:37, 1 June 2011

ie I'm not saying the outside member access isn't ok (it's fine!), just the duplicate usage of them smells bad :)

#Comment by Tim Starling (talk | contribs)   00:45, 3 June 2011

You can't use __get() in PHP.

I tried it once before, but I had to self-revert because PHP's implementation of it is so broken. The issue is the re-entry guard, which will randomly break your code due to subtle changes in initialisation order. If you try to re-enter __get(), even with a different $name, PHP will stop the function from being called and will return null instead, without even raising a warning. So if one of the get*() functions happens to depend on another lazy-initialised member of RequestContext, the result is subtle breakage dependent on initialisation order.

#Comment by Tim Starling (talk | contribs)   00:52, 3 June 2011

It looks like quite a complex revert, so maybe I will leave this for a day or two and hope someone will do it for me.

#Comment by Catrope (talk | contribs)   09:21, 3 June 2011

I never knew __get() had this problem, yet I've used it successfully in another project. The important thing is that you can't use the magic behavior internally: all your getters and everything that could be called by __get() must use the real accessors instead of __get() / __set() magic. I guess this just kind of seemed like an obvious good idea to me, so I never ran into this bug.

#Comment by Tim Starling (talk | contribs)   23:16, 3 June 2011

The get functions in this revision call half the codebase: User, Language, Title, etc. You can't guarantee that nobody is ever going to use RequestContext from one of those modules. If you tried to use it in places where it was safe, that would establish a convention which would then be applied to places where it is not safe. It's better to just remove the temptation.

#Comment by Happy-melon (talk | contribs)   19:24, 27 June 2011

I've reverted in r90899 what bits of this haven't already been overwritten by later changes.

Status & tagging log