SpecialCachedPage.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Abstract special page class with scaffolding for caching HTML and other values
  4. * in a single blob.
  5. *
  6. * Before using any of the caching functionality, call startCache.
  7. * After the last call to either getCachedValue or addCachedHTML, call saveCache.
  8. *
  9. * To get a cached value or compute it, use getCachedValue like this:
  10. * $this->getCachedValue( $callback );
  11. *
  12. * To add HTML that should be cached, use addCachedHTML like this:
  13. * $this->addCachedHTML( $callback );
  14. *
  15. * The callback function is only called when needed, so do all your expensive
  16. * computations here. This function should returns the HTML to be cached.
  17. * It should not add anything to the PageOutput object!
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  32. * http://www.gnu.org/copyleft/gpl.html
  33. *
  34. * @file
  35. * @ingroup SpecialPage
  36. * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  37. * @since 1.20
  38. */
  39. abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
  40. /**
  41. * CacheHelper object to which we forward the non-SpecialPage specific caching work.
  42. * Initialized in startCache.
  43. *
  44. * @since 1.20
  45. * @var CacheHelper
  46. */
  47. protected $cacheHelper;
  48. /**
  49. * If the cache is enabled or not.
  50. *
  51. * @since 1.20
  52. * @var bool
  53. */
  54. protected $cacheEnabled = true;
  55. /**
  56. * Gets called after @see SpecialPage::execute.
  57. *
  58. * @since 1.20
  59. *
  60. * @param string|null $subPage
  61. */
  62. protected function afterExecute( $subPage ) {
  63. $this->saveCache();
  64. parent::afterExecute( $subPage );
  65. }
  66. /**
  67. * Sets if the cache should be enabled or not.
  68. *
  69. * @since 1.20
  70. * @param bool $cacheEnabled
  71. */
  72. public function setCacheEnabled( $cacheEnabled ) {
  73. $this->cacheHelper->setCacheEnabled( $cacheEnabled );
  74. }
  75. /**
  76. * Initializes the caching.
  77. * Should be called before the first time anything is added via addCachedHTML.
  78. *
  79. * @since 1.20
  80. *
  81. * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
  82. * @param bool|null $cacheEnabled Sets if the cache should be enabled or not.
  83. */
  84. public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
  85. if ( !isset( $this->cacheHelper ) ) {
  86. $this->cacheHelper = new CacheHelper();
  87. $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
  88. $this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] );
  89. $keyArgs = $this->getCacheKey();
  90. if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
  91. unset( $keyArgs['action'] );
  92. }
  93. $this->cacheHelper->setCacheKey( $keyArgs );
  94. if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
  95. $this->cacheHelper->rebuildOnDemand();
  96. }
  97. }
  98. $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
  99. }
  100. /**
  101. * Get a cached value if available or compute it if not and then cache it if possible.
  102. * The provided $computeFunction is only called when the computation needs to happen
  103. * and should return a result value. $args are arguments that will be passed to the
  104. * compute function when called.
  105. *
  106. * @since 1.20
  107. *
  108. * @param callable $computeFunction
  109. * @param array|mixed $args
  110. * @param string|null $key
  111. *
  112. * @return mixed
  113. */
  114. public function getCachedValue( $computeFunction, $args = [], $key = null ) {
  115. return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
  116. }
  117. /**
  118. * Add some HTML to be cached.
  119. * This is done by providing a callback function that should
  120. * return the HTML to be added. It will only be called if the
  121. * item is not in the cache yet or when the cache has been invalidated.
  122. *
  123. * @since 1.20
  124. *
  125. * @param callable $computeFunction
  126. * @param array $args
  127. * @param string|null $key
  128. */
  129. public function addCachedHTML( $computeFunction, $args = [], $key = null ) {
  130. $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue(
  131. $computeFunction,
  132. $args,
  133. $key
  134. ) );
  135. }
  136. /**
  137. * Saves the HTML to the cache in case it got recomputed.
  138. * Should be called after the last time anything is added via addCachedHTML.
  139. *
  140. * @since 1.20
  141. */
  142. public function saveCache() {
  143. if ( isset( $this->cacheHelper ) ) {
  144. $this->cacheHelper->saveCache();
  145. }
  146. }
  147. /**
  148. * Sets the time to live for the cache, in seconds or a unix timestamp
  149. * indicating the point of expiry.
  150. *
  151. * @since 1.20
  152. *
  153. * @param int $cacheExpiry
  154. */
  155. public function setExpiry( $cacheExpiry ) {
  156. $this->cacheHelper->setExpiry( $cacheExpiry );
  157. }
  158. /**
  159. * Returns the variables used to constructed the cache key in an array.
  160. *
  161. * @since 1.20
  162. *
  163. * @return array
  164. */
  165. protected function getCacheKey() {
  166. return [
  167. $this->mName,
  168. $this->getLanguage()->getCode()
  169. ];
  170. }
  171. /**
  172. * Gets called after the cache got initialized.
  173. *
  174. * @since 1.20
  175. *
  176. * @param bool $hasCached
  177. */
  178. public function onCacheInitialized( $hasCached ) {
  179. if ( $hasCached ) {
  180. $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
  181. }
  182. }
  183. }