r67185 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67184‎ | r67185 | r67186 >
Date:14:28, 1 June 2010
Author:daniel
Status:ok (Comments)
Tags:
Comment:
allow parser/extensions to control for how long a ParserOutput gets cached. Introduced updateCacheExpiry(), Cleaned up use of getCacheTime() for marking uncacheable objects.
Modified paths:
  • /trunk/phase3/includes/Article.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)
  • /trunk/phase3/includes/parser/ParserCache.php (modified) (history)
  • /trunk/phase3/includes/parser/ParserOutput.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/parser/Parser.php
@@ -4787,7 +4787,8 @@
47884788 */
47894789 function disableCache() {
47904790 wfDebug( "Parser output marked as uncacheable.\n" );
4791 - $this->mOutput->mCacheTime = -1;
 4791+ $this->mOutput->setCacheTime( -1 ); // old style, for compatibility
 4792+ $this->mOutput->setCacheExpiry( 0 ); // new style, for consistency
47924793 }
47934794
47944795 /**#@+
Index: trunk/phase3/includes/parser/ParserOutput.php
@@ -11,6 +11,7 @@
1212 $mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
1313 $mTitleText, # title text of the chosen language variant
1414 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
 15+ $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
1516 $mVersion = Parser::VERSION, # Compatibility check
1617 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
1718 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
@@ -64,12 +65,66 @@
6566 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
6667 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
6768 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
68 - function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
 69+
 70+ /** setCacheTime() sets the timestamp expressing when the page has been rendered.
 71+ * This doesn not control expiry, see updateCacheExpiry() for that!
 72+ */
 73+ function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
6974 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
7075 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
7176 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
7277 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
7378
 79+
 80+ /** Sets the number of seconds after which this object should expire.
 81+ * This value is used with the ParserCache.
 82+ * If called with a value greater than the value provided at any previous call,
 83+ * the new call has no effect. The value returned by getCacheExpiry is smaller
 84+ * or equal to the smallest number that was provided as an argument to
 85+ * updateCacheExpiry().
 86+ */
 87+ function updateCacheExpiry( $seconds ) {
 88+ $seconds = (int)$seconds;
 89+
 90+ if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds )
 91+ $this->mCacheExpiry = $seconds;
 92+
 93+ // hack: set old-style marker for uncacheable entries.
 94+ if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 )
 95+ $this->mCacheTime = -1;
 96+ }
 97+
 98+ /** Returns the number of seconds after which this object should expire.
 99+ * This method is used by ParserCache to determine how long the ParserOutput can be cached.
 100+ * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
 101+ * The value returned by getCacheExpiry is smaller or equal to the smallest number
 102+ * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
 103+ * value of $wgParserCacheExpireTime.
 104+ */
 105+ function getCacheExpiry() {
 106+ global $wgParserCacheExpireTime;
 107+
 108+ if ( $this->mCacheTime < 0 ) return 0; // old-style marker for "not cachable"
 109+
 110+ $expire = $this->mCacheExpiry;
 111+
 112+ if ( $expire === null )
 113+ $expire = $wgParserCacheExpireTime;
 114+ else
 115+ $expire = min( $expire, $wgParserCacheExpireTime );
 116+
 117+ if( $this->containsOldMagic() ) { //compatibility hack
 118+ $expire = min( $expire, 3600 ); # 1 hour
 119+ }
 120+
 121+ if ( $expire <= 0 ) return 0; // not cachable
 122+ else return $expire;
 123+ }
 124+
 125+ function isCacheable() {
 126+ return $this->getCacheExpiry() > 0;
 127+ }
 128+
74129 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
75130 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
76131 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
@@ -174,9 +229,10 @@
175230 */
176231 public function expired( $touched ) {
177232 global $wgCacheEpoch;
178 - return $this->getCacheTime() == -1 || // parser says it's uncacheable
 233+ return !$this->isCacheable() || // parser says it's uncacheable
179234 $this->getCacheTime() < $touched ||
180235 $this->getCacheTime() <= $wgCacheEpoch ||
 236+ $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
181237 !isset( $this->mVersion ) ||
182238 version_compare( $this->mVersion, Parser::VERSION, "lt" );
183239 }
Index: trunk/phase3/includes/parser/ParserCache.php
@@ -96,11 +96,10 @@
9797 }
9898
9999 function save( $parserOutput, $article, $popts ){
100 - global $wgParserCacheExpireTime;
101100 $key = $this->getKey( $article, $popts );
 101+ $expire = $parserOutput->getCacheExpiry();
102102
103 - if( $parserOutput->getCacheTime() != -1 ) {
104 -
 103+ if( $expire > 0 ) {
105104 $now = wfTimestampNow();
106105 $parserOutput->setCacheTime( $now );
107106
@@ -110,11 +109,6 @@
111110 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
112111 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
113112
114 - if( $parserOutput->containsOldMagic() ){
115 - $expire = 3600; # 1 hour
116 - } else {
117 - $expire = $wgParserCacheExpireTime;
118 - }
119113 $this->mMemc->set( $key, $parserOutput, $expire );
120114
121115 } else {
Index: trunk/phase3/includes/Article.php
@@ -4457,7 +4457,7 @@
44584458 $this->mTitle->getPrefixedDBkey() ) );
44594459 }
44604460
4461 - if ( $wgEnableParserCache && $cache && $this && $this->mParserOutput->getCacheTime() != -1 ) {
 4461+ if ( $wgEnableParserCache && $cache && $this && !$this->mParserOutput->isCacheable() ) {
44624462 $parserCache = ParserCache::singleton();
44634463 $parserCache->save( $this->mParserOutput, $this, $parserOptions );
44644464 }
@@ -4465,7 +4465,7 @@
44664466 // Make sure file cache is not used on uncacheable content.
44674467 // Output that has magic words in it can still use the parser cache
44684468 // (if enabled), though it will generally expire sooner.
4469 - if ( $this->mParserOutput->getCacheTime() == -1 || $this->mParserOutput->containsOldMagic() ) {
 4469+ if ( !$this->mParserOutput->isCacheable() || $this->mParserOutput->containsOldMagic() ) {
44704470 $wgUseFileCache = false;
44714471 }
44724472
Index: trunk/phase3/includes/OutputPage.php
@@ -1050,7 +1050,7 @@
10511051 $popts, true, true, $this->mRevisionId
10521052 );
10531053 $popts->setTidy( false );
1054 - if ( $cache && $article && $parserOutput->getCacheTime() != -1 ) {
 1054+ if ( $cache && $article && !$parserOutput->isCacheable() ) {
10551055 $parserCache = ParserCache::singleton();
10561056 $parserCache->save( $parserOutput, $article, $popts );
10571057 }
@@ -1078,7 +1078,7 @@
10791079 $this->mHideNewSectionLink = $parserOutput->getHideNewSection();
10801080
10811081 $this->mParseWarnings = $parserOutput->getWarnings();
1082 - if ( $parserOutput->getCacheTime() == -1 ) {
 1082+ if ( !$parserOutput->isCacheable() ) {
10831083 $this->enableClientCache( false );
10841084 }
10851085 $this->mNoGallery = $parserOutput->getNoGallery();

Follow-up revisions

RevisionCommit summaryAuthorDate
r67197Fix fatal in r67185catrope19:40, 1 June 2010
r67823* Fix for r67185: cache the page if caching is allowed, not the opposite :)...ialex15:37, 10 June 2010
r68895Revert r68844, depends on r67185werdna17:57, 2 July 2010
r70767It should only be saved in the ParserCache if it IS cacheable....platonides15:28, 9 August 2010

Comments

#Comment by Raymond (talk | contribs)   19:19, 1 June 2010

Seen on translatewiki:

PHP Fatal error: Call to undefined method ParserOutput::setCacheExpiry() in /www/w/includes/parser/Parser.php on line 4791

#Comment by Duesentrieb (talk | contribs)   09:27, 9 June 2010

fixed by catrope in r67197

#Comment by Duesentrieb (talk | contribs)   13:33, 15 July 2010

needed by DataTransclusion extension

#Comment by Platonides (talk | contribs)   17:47, 17 October 2010

I have reviewed it and (with r67197, r67823, r70767 fixes) found it ok.

Keeping as new so at least another one looks at it.

#Comment by 😂 (talk | contribs)   18:22, 17 October 2010

Could use some braces on the if/else blocks, but otherwise looks good to me with the followups.

Status & tagging log