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

Revision: 114044
Author:   jeroendedauw
Date:     2012-03-17 00:51:25 +0000 (Sat, 17 Mar 2012)
Log Message:
-----------
playing with some stuff to make partial caching of special pages easier

Modified Paths:
--------------
    trunk/extensions/EducationProgram/specials/SpecialCachedPage.php
    trunk/extensions/EducationProgram/specials/SpecialEPPage.php
    trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php

Modified: trunk/extensions/EducationProgram/specials/SpecialCachedPage.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialCachedPage.php    
2012-03-17 00:08:26 UTC (rev 114043)
+++ trunk/extensions/EducationProgram/specials/SpecialCachedPage.php    
2012-03-17 00:51:25 UTC (rev 114044)
@@ -1,5 +1,142 @@
 <?php
 
+class CachedOutput {
+
+       /**
+        * The time to live for the cache, in seconds or a unix timestamp 
indicating the point of expiry.
+        *
+        * @since 0.1
+        * @var integer
+        */
+       protected $cacheExpiry = 3600;
+
+       protected $cachedChunks;
+
+       protected $hasCached = null;
+
+       protected $out;
+
+       public function __construct( OutputPage $out ) {
+               $this->out = $out;
+       }
+
+       public function invalidateCache() {
+               $this->hasCached = false;
+       }
+
+       /**
+        * Initializes the caching.
+        * Should be called ONCE before any of the caching functionality is 
used,
+        * only when $this->hasCached is null.
+        *
+        * @since 0.1
+        */
+       protected function initCaching() {
+               $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( 
$this->getCacheKey() );
+
+               $this->hasCached = is_array( $cachedChunks );
+               $this->cachedChunks = $this->hasCached ? $cachedChunks : 
array();
+       }
+
+       /**
+        * Add some HTML to be cached.
+        * This is done by providing a callback function that should
+        * return the HTML to be added. It will only be called if the
+        * item is not in the cache yet or when the cache has been invalidated.
+        *
+        * @since 0.1
+        *
+        * @param {function} $callback
+        * @param array $args
+        * @param string|null $key
+        */
+       public function addCachedHTML( $callback, $args = array(), $key = null 
) {
+               if ( is_null( $this->hasCached ) ) {
+                       $this->initCaching();
+               }
+
+               if ( $this->hasCached ) {
+                       $html = '';
+
+                       if ( is_null( $key ) ) {
+                               $itemKey = array_flip( array_slice( 
$this->cachedChunks, 0, 1 ) );
+                               $itemKey = array_shift( $itemKey );
+
+                               if ( is_integer( $itemKey ) ) {
+                                       wfWarn( "Attempted to get item with 
non-numeric key while the next item in the queue has a key ($itemKey) in " . 
__METHOD__ );
+                               }
+                               elseif ( is_null( $itemKey ) ) {
+                                       wfWarn( "Attempted to get an item while 
the queue is empty in " . __METHOD__ );
+                               }
+                               else {
+                                       $html = array_shift( $key );
+                               }
+                       }
+                       else {
+                               if ( array_key_exists( $key, 
$this->cachedChunks ) ) {
+                                       $html = $this->cachedChunks[$key];
+                                       unset( $this->cachedChunks[$key] );
+                               }
+                               else {
+                                       wfWarn( "There is no item with key 
'$key' in this->cachedChunks in " . __METHOD__ );
+                               }
+                       }
+               }
+               else {
+                       $html = call_user_func_array( $callback, $args );
+
+                       if ( is_null( $key ) ) {
+                               $this->cachedChunks[] = $html;
+                       }
+                       else {
+                               $this->cachedChunks[$key] = $html;
+                       }
+               }
+
+               $this->addHTML( $html );
+       }
+
+       public function addHTML( $html ) {
+               $this->out->addHTML( $html );
+       }
+
+       /**
+        * Saves the HTML to the cache in case it got recomputed.
+        * Should be called after the last time anything is added via 
addCachedHTML.
+        *
+        * @since 0.1
+        */
+       public function saveCache() {
+               if ( !$this->hasCached && !empty( $this->cachedChunks ) ) {
+                       wfGetCache( CACHE_ANYTHING )->set( 
$this->getCacheKey(), $this->cachedChunks, $this->cacheExpiry );
+               }
+       }
+
+       /**
+        * Sets the time to live for the cache, in seconds or a unix timestamp 
indicating the point of expiry..
+        *
+        * @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() 
);
+       }
+
+}
+
 /**
  * Abstract special page class with scaffolding for caching the HTML output.
  *
@@ -14,13 +151,15 @@
 abstract class SpecialCachedPage extends SpecialPage {
 
        /**
-        * The time to live for the cache, in seconds.
+        * The time to live for the cache, in seconds or a unix timestamp 
indicating the point of expiry.
         *
         * @since 0.1
-        * @var integer
+        * @var integer|null
         */
-       protected $cacheExpiry = 3600;
+       protected $cacheExpiry = null;
 
+       protected $cachedOutput = null;
+
        /**
         * Main method.
         *
@@ -29,33 +168,35 @@
         * @param string $subPage
         */
        public function execute( $subPage ) {
-               parent::execute( $subPage );
+               //parent::execute( $subPage );
 
-               $cache = wfGetCache( CACHE_ANYTHING );
-               $cacheKey = $this->getCacheKey();
-               $cachedHTML = $cache->get( $cacheKey );
+               if ( !is_null( $this->cacheExpiry ) ) {
+                       $cache = wfGetCache( CACHE_ANYTHING );
+                       $cacheKey = $this->getCacheKey();
+                       $cachedHTML = $cache->get( $cacheKey );
 
-               $out = $this->getOutput();
+                       $out = $this->getOutput();
 
-               if ( $this->getRequest()->getText( 'action' ) !== 'purge' && 
is_string( $cachedHTML ) ) {
-                       $html = $cachedHTML;
-               }
-               else {
-                       $this->displayCachedContent();
+                       if ( $this->getRequest()->getText( 'action' ) !== 
'purge' && is_string( $cachedHTML ) ) {
+                               $html = $cachedHTML;
+                       }
+                       else {
+                               $this->displayCachedContent();
 
-                       $html = $out->getHTML();
-                       $cache->set( $cacheKey, $html, $this->cacheExpiry );
-               }
+                               $html = $out->getHTML();
+                               $cache->set( $cacheKey, $html, 
$this->cacheExpiry );
+                       }
 
-               $out->clearHTML();
+                       $out->clearHTML();
 
-               $this->displayBeforeCached();
-               $out->addHTML( $html );
-               $this->displayAfterCached();
+                       $this->displayBeforeCached();
+                       $out->addHTML( $html );
+                       $this->displayAfterCached();
+               }
        }
 
        /**
-        * Sets the time to live for the cache, in seconds.
+        * Sets the time to live for the cache, in seconds or a unix timestamp 
indicating the point of expiry..
         *
         * @since 0.1
         *

Modified: trunk/extensions/EducationProgram/specials/SpecialEPPage.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialEPPage.php        
2012-03-17 00:08:26 UTC (rev 114043)
+++ trunk/extensions/EducationProgram/specials/SpecialEPPage.php        
2012-03-17 00:51:25 UTC (rev 114044)
@@ -12,7 +12,7 @@
  * @licence GNU GPL v3 or later
  * @author Jeroen De Dauw < [email protected] >
  */
-abstract class SpecialEPPage extends SpecialPage {
+abstract class SpecialEPPage extends SpecialCachedPage {
 
        /**
         * The subpage, ie the part after Special:PageName/
@@ -54,6 +54,8 @@
         * @return boolean
         */
        public function execute( $subPage ) {
+               parent::execute( $subPage );
+
                $subPage = is_null( $subPage ) ? '' : $subPage;
                $this->subPage = trim( str_replace( '_', ' ', $subPage ) );
 

Modified: trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php       
2012-03-17 00:08:26 UTC (rev 114043)
+++ trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php       
2012-03-17 00:51:25 UTC (rev 114044)
@@ -20,6 +20,7 @@
         */
        public function __construct() {
                parent::__construct( 'StudentActivity' );
+               $this->cacheExpiry = 600;
        }
 
        /**
@@ -30,34 +31,40 @@
         * @param string $subPage
         */
        public function execute( $subPage ) {
-               parent::execute( $subPage );
+               //parent::execute( $subPage );
 
-               $cache = wfGetCache( CACHE_ANYTHING );
-               $cacheKey = wfMemcKey( get_class( $this ), 
$this->getLanguage()->getCode() );
-               $cachedHTML = $cache->get( $cacheKey );
+               if ( !is_null( $this->cacheExpiry ) ) {
+                       $cache = wfGetCache( CACHE_ANYTHING );
+                       $cacheKey = $this->getCacheKey();
+                       $cachedHTML = $cache->get( $cacheKey );
 
-               $out = $this->getOutput();
+                       $out = $this->getOutput();
 
-               if ( $this->getRequest()->getText( 'action' ) !== 'purge' && 
is_string( $cachedHTML ) ) {
-                       $html = $cachedHTML;
-               }
-               else {
-                       $conds = array( 'last_active > ' . wfGetDB( DB_SLAVE 
)->addQuotes(
-                               wfTimestamp( TS_MW, time() - ( EPSettings::get( 
'recentActivityLimit' ) ) )
-                       ) );
+                       if ( $this->getRequest()->getText( 'action' ) !== 
'purge' && is_string( $cachedHTML ) ) {
+                               $html = $cachedHTML;
+                       }
+                       else {
+                               $this->displayCachedContent();
 
-                       $this->displayStudentMeter( $conds );
-                       $this->displayPager( $conds );
+                               $html = $out->getHTML();
+                               $cache->set( $cacheKey, $html, 
$this->cacheExpiry );
+                       }
 
-                       $html = $out->getHTML();
-                       $cache->set( $cacheKey, $html, 3600 );
+                       $out->clearHTML();
+
+                       $this->displayBeforeCached();
+                       $out->addHTML( $html );
+                       $this->displayAfterCached();
                }
+       }
 
-               $out->clearHTML();
+       protected function displayCachedContent() {
+               $conds = array( 'last_active > ' . wfGetDB( DB_SLAVE 
)->addQuotes(
+                       wfTimestamp( TS_MW, time() - ( EPSettings::get( 
'recentActivityLimit' ) ) )
+               ) );
 
-               $this->displayNavigation();
-
-               $out->addHTML( $html );
+               $this->displayStudentMeter( $conds );
+               $this->displayPager( $conds );
        }
 
        public function displayPager( array $conds ) {


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

Reply via email to