r85240 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85239‎ | r85240 | r85241 >
Date:10:41, 3 April 2011
Author:dantman
Status:resolved (Comments)
Tags:
Comment:
Implement the RequestContext class. Some credit to IAlex, ;) other credit for me and that plethora of bugs and hicoughs I had to deal with in impelenting it.
http://www.mediawiki.org/wiki/Requests_for_comment/Context_object (it's little different though)
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/Setup.php (modified) (history)
  • /trunk/phase3/includes/SpecialPage.php (modified) (history)
  • /trunk/phase3/includes/StubObject.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialPage.php
@@ -74,20 +74,10 @@
7575 */
7676 var $mAddedRedirectParams = array();
7777 /**
78 - * Current request
79 - * @var WebRequest
 78+ * Current request context
 79+ * @var RequestContext
8080 */
81 - protected $mRequest;
82 - /**
83 - * Current output page
84 - * @var OutputPage
85 - */
86 - protected $mOutput;
87 - /**
88 - * Full title including $par
89 - * @var Title
90 - */
91 - protected $mFullTitle;
 81+ protected $mContext;
9282
9383 /**
9484 * List of special pages, followed by parameters.
@@ -556,7 +546,7 @@
557547 }
558548
559549 # Page exists, set the context
560 - $page->setContext( $wgRequest, $wgOut );
 550+ $page->setContext( $wgOut->getContext() );
561551
562552 # Check for redirect
563553 if ( !$including ) {
@@ -629,9 +619,10 @@
630620
631621 $oldTitle = $wgTitle;
632622 $oldOut = $wgOut;
633 - $wgOut = new OutputPage;
634 - $wgOut->setTitle( $title );
635 - $wgOut->setUser( $wgUser ); # for now, there may be a better idea in the future
 623+
 624+ $context = new RequestContext;
 625+ $context->setTitle( $title );
 626+ $wgOut = $context->getOutput();
636627
637628 $ret = SpecialPage::executePath( $title, true );
638629 if ( $ret === true ) {
@@ -888,11 +879,9 @@
889880 * This may be overridden by subclasses.
890881 */
891882 function execute( $par ) {
892 - global $wgUser;
893 -
894883 $this->setHeaders();
895884
896 - if ( $this->userCanExecute( $wgUser ) ) {
 885+ if ( $this->userCanExecute( $this->getUser() ) ) {
897886 $func = $this->mFunction;
898887 // only load file if the function does not exist
899888 if(!is_callable($func) and $this->mFile) {
@@ -993,28 +982,36 @@
994983 /**
995984 * Sets the context this SpecialPage is executed in
996985 *
997 - * @param $request WebRequest
998 - * @param $output OutputPage
 986+ * @param $context RequestContext
 987+ * @since 1.18
999988 */
1000 - protected function setContext( $request, $output ) {
1001 - $this->mRequest = $request;
1002 - $this->mOutput = $output;
1003 - $this->mFullTitle = $output->getTitle();
 989+ protected function setContext( $context ) {
 990+ $this->mContext = $context;
1004991 }
1005992
1006993 /**
 994+ * Gets the context this SpecialPage is executed in
 995+ *
 996+ * @return RequestContext
 997+ * @since 1.18
 998+ */
 999+ public function getContext() {
 1000+ if ( $this->mContext instanceof RequestContext ) {
 1001+ return $this->mContext;
 1002+ } else {
 1003+ wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
 1004+ return RequestContext::getMain();
 1005+ }
 1006+ }
 1007+
 1008+ /**
10071009 * Get the WebRequest being used for this instance
10081010 *
10091011 * @return WebRequest
10101012 * @since 1.18
10111013 */
10121014 public function getRequest() {
1013 - if ( !isset($this->mRequest) ) {
1014 - wfDebug( __METHOD__ . " called and \$mRequest is null. Return \$wgRequest for sanity\n" );
1015 - global $wgRequest;
1016 - return $wgRequest;
1017 - }
1018 - return $this->mRequest;
 1015+ return $this->getContext()->getRequest();
10191016 }
10201017
10211018 /**
@@ -1024,12 +1021,7 @@
10251022 * @since 1.18
10261023 */
10271024 public function getOutput() {
1028 - if ( !isset($this->mOutput) ) {
1029 - wfDebug( __METHOD__ . " called and \$mOutput is null. Return \$wgOut for sanity\n" );
1030 - global $wgOut;
1031 - return $wgOut;
1032 - }
1033 - return $this->mOutput;
 1025+ return $this->getContext()->getOutput();
10341026 }
10351027
10361028 /**
@@ -1039,7 +1031,7 @@
10401032 * @since 1.18
10411033 */
10421034 public function getUser() {
1043 - return $this->getOutput()->getUser();
 1035+ return $this->getContext()->getUser();
10441036 }
10451037
10461038 /**
@@ -1049,17 +1041,27 @@
10501042 * @since 1.18
10511043 */
10521044 public function getSkin() {
1053 - return $this->getOutput()->getSkin();
 1045+ return $this->getContext()->getSkin();
10541046 }
10551047
10561048 /**
 1049+ * Return the full title, including $par
 1050+ *
 1051+ * @return Title
 1052+ * @since 1.18
 1053+ */
 1054+ public function getFullTitle() {
 1055+ return $this->getContext()->getTitle();
 1056+ }
 1057+
 1058+ /**
10571059 * Wrapper around wfMessage that sets the current context. Currently this
10581060 * is only the title.
10591061 *
10601062 * @see wfMessage
10611063 */
10621064 public function msg( /* $args */ ) {
1063 - return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->mFullTitle );
 1065+ return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
10641066 }
10651067 }
10661068
Index: trunk/phase3/includes/StubObject.php
@@ -131,39 +131,44 @@
132132 }
133133
134134 /**
135 - * Stub object for the user language. It depends of the user preferences and
136 - * "uselang" parameter that can be passed to index.php. This object have to be
137 - * in $wgLang global.
 135+ * Stub object for the $wg globals replaced by RequestContext
138136 */
139 -class StubUserLang extends StubObject {
140 -
141 - function __construct() {
142 - parent::__construct( 'wgLang' );
 137+class StubRequestContext extends StubObject {
 138+
 139+ private $method = null;
 140+
 141+ function __construct( $global, $method ) {
 142+ parent::__construct( $global, 'RequestContext' );
 143+ $this->method = $method;
143144 }
144 -
 145+
145146 function __call( $name, $args ) {
146147 return $this->_call( $name, $args );
147148 }
 149+
 150+ function __get( $name ) {
 151+ // __get doesn't seam to play nice with _unstub
 152+ return RequestContext::getMain()->{$this->method}()->{$name};
 153+ }
148154
149 - function _newObject() {
150 - global $wgLanguageCode, $wgRequest, $wgUser, $wgContLang;
151 - $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
152 - // BCP 47 - letter case MUST NOT carry meaning
153 - $code = strtolower( $code );
 155+ function __set( $name, $val ) {
 156+ // __set doesn't seam to play nice with _unstub
 157+ RequestContext::getMain()->{$this->method}()->{$name} = $val;
 158+ }
154159
155 - # Validate $code
156 - if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
157 - wfDebug( "Invalid user language code\n" );
158 - $code = $wgLanguageCode;
159 - }
 160+ function __isset( $name ) {
 161+ // __isset doesn't seam to play nice with _unstub
 162+ return isset( RequestContext::getMain()->{$this->method}()->{$name} );
 163+ }
160164
161 - wfRunHooks( 'UserGetLanguageObject', array( $wgUser, &$code ) );
 165+ function __unset( $name ) {
 166+ // __unset doesn't seam to play nice with _unstub
 167+ unset( RequestContext::getMain()->{$this->method}()->{$name} );
 168+ }
162169
163 - if( $code === $wgLanguageCode ) {
164 - return $wgContLang;
165 - } else {
166 - $obj = Language::factory( $code );
167 - return $obj;
168 - }
 170+ function _newObject() {
 171+ return RequestContext::getMain()->{$this->method}();
169172 }
 173+
170174 }
 175+
Index: trunk/phase3/includes/OutputPage.php
@@ -752,52 +752,53 @@
753753 }
754754
755755 /**
756 - * Set the Title object to use
 756+ * Set the RequestContext used in this instance
757757 *
758 - * @param $t Title object
 758+ * @param RequestContext $context
759759 */
760 - public function setTitle( $t ) {
761 - $this->mTitle = $t;
 760+ public function setContext( RequestContext $context ) {
 761+ $this->mContext = $context;
762762 }
763763
764764 /**
765 - * Get the Title object used in this instance
 765+ * Get the RequestContext used in this instance
766766 *
767 - * @return Title
 767+ * @return RequestContext
768768 */
769 - public function getTitle() {
770 - if ( $this->mTitle instanceof Title ) {
771 - return $this->mTitle;
772 - } else {
773 - wfDebug( __METHOD__ . " called and \$mTitle is null. Return \$wgTitle for sanity\n" );
774 - global $wgTitle;
775 - return $wgTitle;
 769+ public function getContext() {
 770+ if ( !isset($this->mContext) ) {
 771+ wfDebug( __METHOD__ . " called and \$mContext is null. Using RequestContext::getMain(); for sanity\n" );
 772+ $this->mContext = RequestContext::getMain();
776773 }
 774+ return $this->mContext;
777775 }
778776
779777 /**
780 - * Set the User object to use
 778+ * Set the Title object to use
781779 *
782 - * @param $u User object
783 - * @since 1.18
 780+ * @param $t Title object
784781 */
785 - public function setUser( $u ) {
786 - $this->mUser = $u;
 782+ public function setTitle( $t ) {
 783+ $this->getContext()->setTitle($t);
787784 }
788785
789786 /**
 787+ * Get the Title object used in this instance
 788+ *
 789+ * @return Title
 790+ */
 791+ public function getTitle() {
 792+ return $this->getContext()->getTitle();
 793+ }
 794+
 795+ /**
790796 * Get the User object used in this instance
791797 *
792798 * @return User
793799 * @since 1.18
794800 */
795801 public function getUser() {
796 - if ( !isset($this->mUser) ) {
797 - wfDebug( __METHOD__ . " called and \$mUser is null. Return \$wgUser for sanity\n" );
798 - global $wgUser;
799 - return $wgUser;
800 - }
801 - return $this->mUser;
 802+ return $this->getContext()->getUser();
802803 }
803804
804805 /**
@@ -807,9 +808,7 @@
808809 * @since 1.18
809810 */
810811 public function getSkin() {
811 - // For now we'll just proxy to the user. In the future a saner location for
812 - // organizing what skin to use may be chosen
813 - return $this->getUser()->getSkin();
 812+ return $this->getContext()->getSkin();
814813 }
815814
816815 /**
@@ -2628,7 +2627,7 @@
26292628 // Add user JS if enabled
26302629 if ( $wgAllowUserJs && $this->getUser()->isLoggedIn() ) {
26312630 $action = $wgRequest->getVal( 'action', 'view' );
2632 - if( $this->mTitle && $this->mTitle->isJsSubpage() && $sk->userCanPreview( $action ) ) {
 2631+ if( $this->getTitle() && $this->getTitle()->isJsSubpage() && $sk->userCanPreview( $action ) ) {
26332632 # XXX: additional security check/prompt?
26342633 $scripts .= Html::inlineScript( "\n" . $wgRequest->getText( 'wpTextbox1' ) . "\n" ) . "\n";
26352634 } else {
Index: trunk/phase3/includes/AutoLoader.php
@@ -197,6 +197,7 @@
198198 'RegexlikeReplacer' => 'includes/StringUtils.php',
199199 'ReplacementArray' => 'includes/StringUtils.php',
200200 'Replacer' => 'includes/StringUtils.php',
 201+ 'RequestContext' => 'includes/RequestContext.php',
201202 'ResourceLoader' => 'includes/resourceloader/ResourceLoader.php',
202203 'ResourceLoaderContext' => 'includes/resourceloader/ResourceLoaderContext.php',
203204 'ResourceLoaderModule' => 'includes/resourceloader/ResourceLoaderModule.php',
@@ -231,8 +232,8 @@
232233 'SquidPurgeClientPool' => 'includes/SquidPurgeClient.php',
233234 'Status' => 'includes/Status.php',
234235 'StubContLang' => 'includes/StubObject.php',
235 - 'StubUserLang' => 'includes/StubObject.php',
236236 'StubObject' => 'includes/StubObject.php',
 237+ 'StubRequestContext' => 'includes/StubObject.php',
237238 'StringUtils' => 'includes/StringUtils.php',
238239 'TablePager' => 'includes/Pager.php',
239240 'TitleDependency' => 'includes/CacheDependency.php',
Index: trunk/phase3/includes/Setup.php
@@ -365,17 +365,17 @@
366366
367367 // Now that variant lists may be available...
368368 $wgRequest->interpolateTitle();
369 -$wgUser = $wgCommandLineMode ? new User : User::newFromSession();
 369+$wgUser = new StubRequestContext( 'wgUser', 'getUser' );
370370
371371 /**
372372 * @var Language
373373 */
374 -$wgLang = new StubUserLang;
 374+$wgLang = new StubRequestContext( 'wgLang', 'getLang' );
375375
376376 /**
377377 * @var OutputPage
378378 */
379 -$wgOut = new StubObject( 'wgOut', 'OutputPage' );
 379+$wgOut = new StubRequestContext( 'wgOut', 'getOutput' );
380380
381381 /**
382382 * @var Parser

Follow-up revisions

RevisionCommit summaryAuthorDate
r85242Followup r85240; Commit the additional file that was part of that change. ;) ...dantman11:09, 3 April 2011
r85250Continue with r85240; Move getSkin from User to RequestContext, do it without...dantman12:46, 3 April 2011
r85278Follow-up to r85240:...happy-melon20:40, 3 April 2011
r85285Follow-up r85278, r85240:...happy-melon21:32, 3 April 2011

Comments

#Comment by Nikerabbit (talk | contribs)   18:05, 3 April 2011

[03-Apr-2011 17:55:00] PHP Catchable fatal error: Argument 1 passed to Title::isNamespaceProtected() must be an instance of User, instance of StubRequestContext given, called in /www/w/includes/Title.php on line 1362 and defined in /www/w/includes/Title.php on line 1151

#Comment by Dantman (talk | contribs)   23:53, 3 April 2011

I believe Happy-melon fixed that in r85278.

#Comment by Nikerabbit (talk | contribs)   20:39, 3 April 2011

[03-Apr-2011 20:27:40] PHP Catchable fatal error: Argument 1 passed to RequestContext::setTitle() must be an instance of Title, null given, called in /www/w/includes/OutputPage.php on line 792 and defined in /www/w/includes/RequestContext.php on line 45

#Comment by Reedy (talk | contribs)   21:16, 3 April 2011

[Sun Apr 03 22:12:55 2011] [error] [client 82.24.53.123] PHP Fatal error: Call to private method OutputPage::getContext() from context 'SpecialPage' in /var/www/designlc/w/includes/SpecialPage.php on line 549 has already been noted and is awaiting a fix?

#Comment by Dantman (talk | contribs)   21:49, 3 April 2011

That was caused by happy-melon in r85278.

#Comment by Happy-melon (talk | contribs)   21:50, 3 April 2011

And fixed in r85285.

Status & tagging log