https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114028

Revision: 114028
Author:   jeroendedauw
Date:     2012-03-16 18:45:27 +0000 (Fri, 16 Mar 2012)
Log Message:
-----------
adding abstract page to not have to duplicate caching code to much

Modified Paths:
--------------
    trunk/extensions/EducationProgram/EducationProgram.php

Added Paths:
-----------
    trunk/extensions/EducationProgram/specials/SpecialCachedPage.php

Modified: trunk/extensions/EducationProgram/EducationProgram.php
===================================================================
--- trunk/extensions/EducationProgram/EducationProgram.php      2012-03-16 
18:33:43 UTC (rev 114027)
+++ trunk/extensions/EducationProgram/EducationProgram.php      2012-03-16 
18:45:27 UTC (rev 114028)
@@ -144,6 +144,7 @@
 $wgAutoloadClasses['SpecialCAProfile']                                 = 
dirname( __FILE__ ) . '/specials/SpecialCAProfile.php';
 $wgAutoloadClasses['SpecialAmbassadorProfile']                 = dirname( 
__FILE__ ) . '/specials/SpecialAmbassadorProfile.php';
 $wgAutoloadClasses['SpecialStudentActivity']           = dirname( __FILE__ ) . 
'/specials/SpecialStudentActivity.php';
+$wgAutoloadClasses['SpecialCachedPage']                        = dirname( 
__FILE__ ) . '/specials/SpecialCachedPage.php';
 
 // Special pages
 $wgSpecialPages['MyCourses']                                           = 
'SpecialMyCourses';

Added: trunk/extensions/EducationProgram/specials/SpecialCachedPage.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialCachedPage.php            
                (rev 0)
+++ trunk/extensions/EducationProgram/specials/SpecialCachedPage.php    
2012-03-16 18:45:27 UTC (rev 114028)
@@ -0,0 +1,110 @@
+<?php
+
+/**
+ * Abstract special page class with scaffolding for caching the HTML output.
+ *
+ * @since 0.1
+ *
+ * @file SpecialCachedPage.php
+ * @ingroup EducationProgram
+ *
+ * @licence GNU GPL v3 or later
+ * @author Jeroen De Dauw < [email protected] >
+ */
+abstract class SpecialCachedPage extends SpecialPage {
+
+       /**
+        * The time to live for the cache, in seconds.
+        *
+        * @since 0.1
+        * @var integer
+        */
+       protected $cacheExpiry = 3600;
+
+       /**
+        * Main method.
+        *
+        * @since 0.1
+        *
+        * @param string $subPage
+        */
+       public function execute( $subPage ) {
+               parent::execute( $subPage );
+
+               $cache = wfGetCache( CACHE_ANYTHING );
+               $cacheKey = $this->getCacheKey();
+               $cachedHTML = $cache->get( $cacheKey );
+
+               $out = $this->getOutput();
+
+               if ( $this->getRequest()->getText( 'action' ) !== 'purge' && 
is_string( $cachedHTML ) ) {
+                       $html = $cachedHTML;
+               }
+               else {
+                       $this->displayCachedContent();
+
+                       $html = $out->getHTML();
+                       $cache->set( $cacheKey, $html, $this->cacheExpiry );
+               }
+
+               $out->clearHTML();
+
+               $this->displayBeforeCached();
+               $out->addHTML( $html );
+               $this->displayAfterCached();
+       }
+
+       /**
+        * Sets the time to live for the cache, in seconds.
+        *
+        * @since 0.1
+        *
+        * @param integer $cacheExpiry
+        */
+       protected function setExpirey( $cacheExpiry ) {
+               $this->cacheExpiry = $cacheExpiry;
+       }
+
+       /**
+        * Returns the cache key to use to cache this page's HTML output.
+        * Is constructed from the special page name and language code.
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getCacheKey() {
+               return wfMemcKey( $this->mName, $this->getLanguage()->getCode() 
);
+       }
+
+       /**
+        * Display the cached content. Everything added to the output here
+        * will be cached.
+        *
+        * @since 0.1
+        */
+       protected function displayCachedContent() {
+
+       }
+
+       /**
+        * Display non-cached content that will be added to the final output
+        * before the cached HTML.
+        *
+        * @since 0.1
+        */
+       protected function displayBeforeCached() {
+
+       }
+
+       /**
+        * Display non-cached content that will be added to the final output
+        * after the cached HTML.
+        *
+        * @since 0.1
+        */
+       protected function displayAfterCached() {
+
+       }
+
+}


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to