CacheTime.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Parser cache specific expiry check.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Parser
  22. */
  23. /**
  24. * Parser cache specific expiry check.
  25. *
  26. * @ingroup Parser
  27. */
  28. class CacheTime {
  29. /** @var array|bool ParserOptions which have been taken into account to
  30. * produce output or false if not available.
  31. */
  32. public $mUsedOptions;
  33. # Compatibility check
  34. public $mVersion = Parser::VERSION;
  35. # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
  36. public $mCacheTime = '';
  37. # Seconds after which the object should expire, use 0 for uncacheable. Used in ParserCache.
  38. public $mCacheExpiry = null;
  39. # Revision ID that was parsed
  40. public $mCacheRevisionId = null;
  41. /**
  42. * @return string TS_MW timestamp
  43. */
  44. public function getCacheTime() {
  45. return wfTimestamp( TS_MW, $this->mCacheTime );
  46. }
  47. /**
  48. * setCacheTime() sets the timestamp expressing when the page has been rendered.
  49. * This does not control expiry, see updateCacheExpiry() for that!
  50. * @param string $t TS_MW timestamp
  51. * @return string
  52. */
  53. public function setCacheTime( $t ) {
  54. return wfSetVar( $this->mCacheTime, $t );
  55. }
  56. /**
  57. * @since 1.23
  58. * @return int|null Revision id, if any was set
  59. */
  60. public function getCacheRevisionId() {
  61. return $this->mCacheRevisionId;
  62. }
  63. /**
  64. * @since 1.23
  65. * @param int $id Revision id
  66. */
  67. public function setCacheRevisionId( $id ) {
  68. $this->mCacheRevisionId = $id;
  69. }
  70. /**
  71. * Sets the number of seconds after which this object should expire.
  72. *
  73. * This value is used with the ParserCache.
  74. * If called with a value greater than the value provided at any previous call,
  75. * the new call has no effect. The value returned by getCacheExpiry is smaller
  76. * or equal to the smallest number that was provided as an argument to
  77. * updateCacheExpiry().
  78. *
  79. * Avoid using 0 if at all possible. Consider JavaScript for highly dynamic content.
  80. *
  81. * @param int $seconds
  82. */
  83. public function updateCacheExpiry( $seconds ) {
  84. $seconds = (int)$seconds;
  85. if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
  86. $this->mCacheExpiry = $seconds;
  87. }
  88. }
  89. /**
  90. * Returns the number of seconds after which this object should expire.
  91. * This method is used by ParserCache to determine how long the ParserOutput can be cached.
  92. * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
  93. * The value returned by getCacheExpiry is smaller or equal to the smallest number
  94. * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
  95. * value of $wgParserCacheExpireTime.
  96. * @return int|mixed|null
  97. */
  98. public function getCacheExpiry() {
  99. global $wgParserCacheExpireTime;
  100. if ( $this->mCacheTime < 0 ) {
  101. return 0;
  102. } // old-style marker for "not cacheable"
  103. $expire = $this->mCacheExpiry;
  104. if ( $expire === null ) {
  105. $expire = $wgParserCacheExpireTime;
  106. } else {
  107. $expire = min( $expire, $wgParserCacheExpireTime );
  108. }
  109. if ( $expire <= 0 ) {
  110. return 0; // not cacheable
  111. } else {
  112. return $expire;
  113. }
  114. }
  115. /**
  116. * @return bool
  117. */
  118. public function isCacheable() {
  119. return $this->getCacheExpiry() > 0;
  120. }
  121. /**
  122. * Return true if this cached output object predates the global or
  123. * per-article cache invalidation timestamps, or if it comes from
  124. * an incompatible older version.
  125. *
  126. * @param string $touched The affected article's last touched timestamp
  127. * @return bool
  128. */
  129. public function expired( $touched ) {
  130. global $wgCacheEpoch;
  131. return !$this->isCacheable() // parser says it's uncacheable
  132. || $this->getCacheTime() < $touched
  133. || $this->getCacheTime() <= $wgCacheEpoch
  134. || $this->getCacheTime() <
  135. wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) // expiry period has passed
  136. || !isset( $this->mVersion )
  137. || version_compare( $this->mVersion, Parser::VERSION, "lt" );
  138. }
  139. /**
  140. * Return true if this cached output object is for a different revision of
  141. * the page.
  142. *
  143. * @todo We always return false if $this->getCacheRevisionId() is null;
  144. * this prevents invalidating the whole parser cache when this change is
  145. * deployed. Someday that should probably be changed.
  146. *
  147. * @since 1.23
  148. * @param int $id The affected article's current revision id
  149. * @return bool
  150. */
  151. public function isDifferentRevision( $id ) {
  152. $cached = $this->getCacheRevisionId();
  153. return $cached !== null && $id !== $cached;
  154. }
  155. }