r78201 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78200‎ | r78201 | r78202 >
Date:18:17, 10 December 2010
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
* (bug 19006) {{REVISIONUSER}} no longer acts like {{CURRENTUSER}} in some cases
* Removed other usages of $wgUser in Parser stuff, as stated in doc
* Added mechanism to get an User object in Parser, based either on the object given in Paser::preSaveTransform() or the ParserOptions otherwise
Modified paths:
  • /trunk/phase3/includes/parser/CoreParserFunctions.php (modified) (history)
  • /trunk/phase3/includes/parser/LinkHolderArray.php (modified) (history)
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)
  • /trunk/phase3/includes/parser/ParserOptions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/parser/ParserOptions.php
@@ -40,7 +40,7 @@
4141 var $mThumbSize; # Thumb size preferred by the user.
4242 var $mCleanSignatures; #
4343
44 - var $mUser; # Stored user object, just used to initialise the skin
 44+ var $mUser; # Stored user object
4545 var $mIsPreview; # Parsing the page for a "preview" operation
4646 var $mIsSectionPreview; # Parsing the page for a "preview" operation on a single section
4747 var $mIsPrintable; # Parsing the printable version of the page
@@ -80,6 +80,7 @@
8181 function getIsSectionPreview() { return $this->mIsSectionPreview; }
8282 function getIsPrintable() { $this->accessedOptions['printable'] = true;
8383 return $this->mIsPrintable; }
 84+ function getUser() { return $this->mUser; }
8485
8586 function getSkin( $title = null ) {
8687 if ( !isset( $this->mSkin ) ) {
Index: trunk/phase3/includes/parser/LinkHolderArray.php
@@ -105,9 +105,8 @@
106106 * Get the stub threshold
107107 */
108108 function getStubThreshold() {
109 - global $wgUser;
110109 if ( !isset( $this->stubThreshold ) ) {
111 - $this->stubThreshold = $wgUser->getStubThreshold();
 110+ $this->stubThreshold = $this->parent->getUser()->getStubThreshold();
112111 }
113112 return $this->stubThreshold;
114113 }
Index: trunk/phase3/includes/parser/Parser.php
@@ -103,6 +103,7 @@
104104 var $mTplExpandCache; # empty-frame expansion cache
105105 var $mTplRedirCache, $mTplDomCache, $mHeadings, $mDoubleUnderscores;
106106 var $mExpensiveFunctionCount; # number of expensive parser function calls
 107+ var $mUser; # User object; only used when doing pre-save transform
107108
108109 # Temporary
109110 # These are variables reset at least once per parse regardless of $clearState
@@ -110,8 +111,10 @@
111112 var $mTitle; # Title context, used for self-link rendering and similar things
112113 var $mOutputType; # Output type, one of the OT_xxx constants
113114 var $ot; # Shortcut alias, see setOutputType()
 115+ var $mRevisionObject; # The revision object of the specified revision ID
114116 var $mRevisionId; # ID to display in {{REVISIONID}} tags
115117 var $mRevisionTimestamp; # The timestamp of the specified revision ID
 118+ var $mRevisionUser; # Userto display in {{REVISIONUSER}} tag
116119 var $mRevIdForTs; # The revision ID which was used to fetch the timestamp
117120
118121 /**
@@ -197,8 +200,10 @@
198201 $this->mInPre = false;
199202 $this->mLinkHolders = new LinkHolderArray( $this );
200203 $this->mLinkID = 0;
201 - $this->mRevisionTimestamp = $this->mRevisionId = null;
 204+ $this->mRevisionObject = $this->mRevisionTimestamp =
 205+ $this->mRevisionId = $this->mRevisionUser = null;
202206 $this->mVarCache = array();
 207+ $this->mUser = null;
203208
204209 /**
205210 * Prefix for temporary replacement strings for the multipass parser.
@@ -271,10 +276,14 @@
272277 $this->setTitle( $title ); # Page title has to be set for the pre-processor
273278
274279 $oldRevisionId = $this->mRevisionId;
 280+ $oldRevisionObject = $this->mRevisionObject;
275281 $oldRevisionTimestamp = $this->mRevisionTimestamp;
 282+ $oldRevisionUser = $this->mRevisionUser;
276283 if ( $revid !== null ) {
277284 $this->mRevisionId = $revid;
 285+ $this->mRevisionObject = null;
278286 $this->mRevisionTimestamp = null;
 287+ $this->mRevisionUser = null;
279288 }
280289 $this->setOutputType( self::OT_HTML );
281290 wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState ) );
@@ -422,7 +431,9 @@
423432 $this->mOutput->setText( $text );
424433
425434 $this->mRevisionId = $oldRevisionId;
 435+ $this->mRevisionObject = $oldRevisionObject;
426436 $this->mRevisionTimestamp = $oldRevisionTimestamp;
 437+ $this->mRevisionUser = $oldRevisionUser;
427438 wfProfileOut( $fname );
428439 wfProfileOut( __METHOD__ );
429440
@@ -499,6 +510,16 @@
500511 }
501512
502513 /**
 514+ * Set the current user.
 515+ * Should only be used when doing pre-save transform.
 516+ *
 517+ * @param $user Mixed: User object or null (to reset)
 518+ */
 519+ function setUser( $user ) {
 520+ $this->mUser = $user;
 521+ }
 522+
 523+ /**
503524 * Accessor for mUniqPrefix.
504525 *
505526 * @return String
@@ -622,6 +643,18 @@
623644 }
624645
625646 /**
 647+ * Get a User object either from $this->mUser, if set, or from the
 648+ * ParserOptions object otherwise
 649+ *
 650+ * @return User object
 651+ */
 652+ function getUser() {
 653+ if( !is_null( $this->mUser ) )
 654+ return $this->mUser;
 655+ return $this->mOptions->getUser();
 656+ }
 657+
 658+ /**
626659 * Get a preprocessor object
627660 *
628661 * @return Preprocessor instance
@@ -1842,7 +1875,7 @@
18431876
18441877 if ( $ns == NS_CATEGORY ) {
18451878 wfProfileIn( __METHOD__."-category" );
1846 - $s = rtrim( $s . "\n" ); # bug 87
 1879+ $s = preg_replace( "/(\s*\n)+\s*$/D", '', $s ); # bug 87
18471880
18481881 if ( $wasblank ) {
18491882 $sortkey = $this->getDefaultSort();
@@ -1858,7 +1891,7 @@
18591892 * Strip the whitespace Category links produce, see bug 87
18601893 * @todo We might want to use trim($tmp, "\n") here.
18611894 */
1862 - $s .= trim( $prefix . $trail, "\n" ) == '' ? '': $prefix . $trail;
 1895+ $s .= trim( $prefix . $trail, "\n" ) == '' ? '' : $prefix . $trail;
18631896
18641897 wfProfileOut( __METHOD__."-category" );
18651898 continue;
@@ -3995,6 +4028,7 @@
39964029 $options->resetUsage();
39974030 $this->mOptions = $options;
39984031 $this->setTitle( $title );
 4032+ $this->setUser( $user );
39994033 $this->setOutputType( self::OT_WIKI );
40004034
40014035 if ( $clearState ) {
@@ -4007,6 +4041,9 @@
40084042 $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
40094043 $text = $this->pstPass2( $text, $user );
40104044 $text = $this->mStripState->unstripBoth( $text );
 4045+
 4046+ $this->setUser( null ); #Reset
 4047+
40114048 return $text;
40124049 }
40134050
@@ -4941,31 +4978,43 @@
49424979 }
49434980
49444981 /**
 4982+ * Get the revision object for $this->mRevisionId
 4983+ *
 4984+ * @return either a Revision object or null
 4985+ */
 4986+ protected function getRevisionObject() {
 4987+ if ( !is_null( $this->mRevisionObject ) )
 4988+ return $this->mRevisionObject;
 4989+ if ( is_null( $this->mRevisionId ) )
 4990+ return null;
 4991+
 4992+ $this->mRevisionObject = Revision::newFromId( $this->mRevisionId );
 4993+ return $this->mRevisionObject;
 4994+ }
 4995+
 4996+ /**
49454997 * Get the timestamp associated with the current revision, adjusted for
49464998 * the default server-local timestamp
49474999 */
49485000 function getRevisionTimestamp() {
49495001 if ( is_null( $this->mRevisionTimestamp ) ) {
49505002 wfProfileIn( __METHOD__ );
4951 - global $wgContLang;
4952 - $dbr = wfGetDB( DB_SLAVE );
4953 - $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
4954 - array( 'rev_id' => $this->mRevisionId ), __METHOD__ );
49555003
4956 - # Normalize timestamp to internal MW format for timezone processing.
4957 - # This has the added side-effect of replacing a null value with
4958 - # the current time, which gives us more sensible behavior for
4959 - # previews.
4960 - $timestamp = wfTimestamp( TS_MW, $timestamp );
 5004+ $revObject = $this->getRevisionObject();
 5005+ $timestamp = $revObject ? $revObject->getTimestamp() : false;
49615006
4962 - # The cryptic '' timezone parameter tells to use the site-default
4963 - # timezone offset instead of the user settings.
4964 - #
4965 - # Since this value will be saved into the parser cache, served
4966 - # to other users, and potentially even used inside links and such,
4967 - # it needs to be consistent for all visitors.
4968 - $this->mRevisionTimestamp = $wgContLang->userAdjust( $timestamp, '' );
 5007+ if( $timestamp !== false ) {
 5008+ global $wgContLang;
49695009
 5010+ # The cryptic '' timezone parameter tells to use the site-default
 5011+ # timezone offset instead of the user settings.
 5012+ #
 5013+ # Since this value will be saved into the parser cache, served
 5014+ # to other users, and potentially even used inside links and such,
 5015+ # it needs to be consistent for all visitors.
 5016+ $this->mRevisionTimestamp = $wgContLang->userAdjust( $timestamp, '' );
 5017+ }
 5018+
49705019 wfProfileOut( __METHOD__ );
49715020 }
49725021 return $this->mRevisionTimestamp;
@@ -4977,16 +5026,18 @@
49785027 * @return String: user name
49795028 */
49805029 function getRevisionUser() {
4981 - # if this template is subst: the revision id will be blank,
4982 - # so just use the current user's name
4983 - if ( $this->mRevisionId ) {
4984 - $revision = Revision::newFromId( $this->mRevisionId );
4985 - $revuser = $revision->getUserText();
4986 - } else {
4987 - global $wgUser;
4988 - $revuser = $wgUser->getName();
 5030+ if( is_null( $this->mRevisionUser ) ) {
 5031+ $revObject = $this->getRevisionObject();
 5032+
 5033+ # if this template is subst: the revision id will be blank,
 5034+ # so just use the current user's name
 5035+ if( $revObject ) {
 5036+ $this->mRevisionUser = $revObject->getUserText();
 5037+ } elseif( $this->ot['wiki'] || $this->mOptions->getIsPreview() ) {
 5038+ $this->mRevisionUser = $this->getUser()->getName();
 5039+ }
49895040 }
4990 - return $revuser;
 5041+ return $this->mRevisionUser;
49915042 }
49925043
49935044 /**
Index: trunk/phase3/includes/parser/CoreParserFunctions.php
@@ -244,13 +244,12 @@
245245 if ( is_object( $title ) && $title->getNamespace() == NS_USER )
246246 $user = $title->getText();
247247
248 - // check parameter, or use $wgUser if in interface message
 248+ // check parameter, or use the ParserOptions if in interface message
249249 $user = User::newFromName( $user );
250250 if ( $user ) {
251251 $gender = $user->getOption( 'gender' );
252252 } elseif ( $parser->getOptions()->getInterfaceMessage() ) {
253 - global $wgUser;
254 - $gender = $wgUser->getOption( 'gender' );
 253+ $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
255254 }
256255 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
257256 wfProfileOut( __METHOD__ );

Follow-up revisions

RevisionCommit summaryAuthorDate
r78202Fixes for r78201:...ialex18:23, 10 December 2010
r78205Whoops, forgot to commit this with r78201ialex19:40, 10 December 2010
r82511Per Platonides; follow-up r78201: add "stubthreshold" to ParserOptions; also ...ialex15:03, 20 February 2011
r89375typo from r78201krinkle01:04, 3 June 2011
r100610* (bug 31921) Fix for r78201: magic words REVISIONDAY, REVISIONMONTH and REVI...ialex16:14, 24 October 2011

Comments

#Comment by Reedy (talk | contribs)   18:18, 10 December 2010

All your one line if's should really be braced

#Comment by Nikerabbit (talk | contribs)   20:04, 10 December 2010

Were the changes for example in function getRevisionTimestamp() intended for this commit?

#Comment by IAlex (talk | contribs)   17:46, 11 December 2010

Yes, to use the common Revision object instead of doing a separate database query.

#Comment by Platonides (talk | contribs)   22:25, 9 February 2011

ParserOptions need getStubThreshold() and getGender() methods.

getStubThreshold() would provide a proper fix for r81765.

Article parserOptions should probably have getGender() disabled (ie. returning ). At least in MiserMode.

#Comment by Platonides (talk | contribs)   23:28, 9 February 2011

getGender() doesn't affect the caching key, since it only uses $wgUser in interface messages.

#Comment by IAlex (talk | contribs)   15:06, 20 February 2011

Added stub threshold in r82511; but I'm not convinced that gender would be useful.

#Comment by Nikerabbit (talk | contribs)   15:28, 20 February 2011

There is also a bug report open having gender work in page gontent, too busy to check out the number right now.

#Comment by IAlex (talk | contribs)   15:50, 20 February 2011

That's bug 27364.

#Comment by Platonides (talk | contribs)   15:53, 20 February 2011

No, it's not (currently) needed for getGender(). I was confused on my first message.

#Comment by Aaron Schulz (talk | contribs)   22:55, 30 June 2011

What is still needed here?

#Comment by IAlex (talk | contribs)   08:04, 1 July 2011

I don't think there's anything left.

Status & tagging log