Index: trunk/extensions/EducationProgram/EducationProgram.php |
— | — | @@ -144,6 +144,7 @@ |
145 | 145 | $wgAutoloadClasses['SpecialCAProfile'] = dirname( __FILE__ ) . '/specials/SpecialCAProfile.php'; |
146 | 146 | $wgAutoloadClasses['SpecialAmbassadorProfile'] = dirname( __FILE__ ) . '/specials/SpecialAmbassadorProfile.php'; |
147 | 147 | $wgAutoloadClasses['SpecialStudentActivity'] = dirname( __FILE__ ) . '/specials/SpecialStudentActivity.php'; |
| 148 | +$wgAutoloadClasses['SpecialCachedPage'] = dirname( __FILE__ ) . '/specials/SpecialCachedPage.php'; |
148 | 149 | |
149 | 150 | // Special pages |
150 | 151 | $wgSpecialPages['MyCourses'] = 'SpecialMyCourses'; |
Index: trunk/extensions/EducationProgram/specials/SpecialCachedPage.php |
— | — | @@ -0,0 +1,110 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract special page class with scaffolding for caching the HTML output. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file SpecialCachedPage.php |
| 10 | + * @ingroup EducationProgram |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +abstract class SpecialCachedPage extends SpecialPage { |
| 16 | + |
| 17 | + /** |
| 18 | + * The time to live for the cache, in seconds. |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * @var integer |
| 22 | + */ |
| 23 | + protected $cacheExpiry = 3600; |
| 24 | + |
| 25 | + /** |
| 26 | + * Main method. |
| 27 | + * |
| 28 | + * @since 0.1 |
| 29 | + * |
| 30 | + * @param string $subPage |
| 31 | + */ |
| 32 | + public function execute( $subPage ) { |
| 33 | + parent::execute( $subPage ); |
| 34 | + |
| 35 | + $cache = wfGetCache( CACHE_ANYTHING ); |
| 36 | + $cacheKey = $this->getCacheKey(); |
| 37 | + $cachedHTML = $cache->get( $cacheKey ); |
| 38 | + |
| 39 | + $out = $this->getOutput(); |
| 40 | + |
| 41 | + if ( $this->getRequest()->getText( 'action' ) !== 'purge' && is_string( $cachedHTML ) ) { |
| 42 | + $html = $cachedHTML; |
| 43 | + } |
| 44 | + else { |
| 45 | + $this->displayCachedContent(); |
| 46 | + |
| 47 | + $html = $out->getHTML(); |
| 48 | + $cache->set( $cacheKey, $html, $this->cacheExpiry ); |
| 49 | + } |
| 50 | + |
| 51 | + $out->clearHTML(); |
| 52 | + |
| 53 | + $this->displayBeforeCached(); |
| 54 | + $out->addHTML( $html ); |
| 55 | + $this->displayAfterCached(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Sets the time to live for the cache, in seconds. |
| 60 | + * |
| 61 | + * @since 0.1 |
| 62 | + * |
| 63 | + * @param integer $cacheExpiry |
| 64 | + */ |
| 65 | + protected function setExpirey( $cacheExpiry ) { |
| 66 | + $this->cacheExpiry = $cacheExpiry; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the cache key to use to cache this page's HTML output. |
| 71 | + * Is constructed from the special page name and language code. |
| 72 | + * |
| 73 | + * @since 0.1 |
| 74 | + * |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + protected function getCacheKey() { |
| 78 | + return wfMemcKey( $this->mName, $this->getLanguage()->getCode() ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Display the cached content. Everything added to the output here |
| 83 | + * will be cached. |
| 84 | + * |
| 85 | + * @since 0.1 |
| 86 | + */ |
| 87 | + protected function displayCachedContent() { |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Display non-cached content that will be added to the final output |
| 93 | + * before the cached HTML. |
| 94 | + * |
| 95 | + * @since 0.1 |
| 96 | + */ |
| 97 | + protected function displayBeforeCached() { |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Display non-cached content that will be added to the final output |
| 103 | + * after the cached HTML. |
| 104 | + * |
| 105 | + * @since 0.1 |
| 106 | + */ |
| 107 | + protected function displayAfterCached() { |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | +} |