https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114163
Revision: 114163
Author: jeroendedauw
Date: 2012-03-19 18:40:22 +0000 (Mon, 19 Mar 2012)
Log Message:
-----------
work on caching of special:institutions
Modified Paths:
--------------
trunk/extensions/EducationProgram/compat/SpecialCachedPage.php
trunk/extensions/EducationProgram/includes/EPOrg.php
trunk/extensions/EducationProgram/specials/SpecialEPPage.php
trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php
trunk/extensions/EducationProgram/specials/SpecialInstitutions.php
trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php
Modified: trunk/extensions/EducationProgram/compat/SpecialCachedPage.php
===================================================================
--- trunk/extensions/EducationProgram/compat/SpecialCachedPage.php
2012-03-19 18:28:47 UTC (rev 114162)
+++ trunk/extensions/EducationProgram/compat/SpecialCachedPage.php
2012-03-19 18:40:22 UTC (rev 114163)
@@ -2,15 +2,11 @@
/**
* Abstract special page class with scaffolding for caching the HTML output.
+ * This copy is kept for compatibility with MW < 1.20.
+ * As of 1.20, this class can be found at
includes/specials/SpecialCachedPage.php
*
- * To enable the caching functionality, the cacheExpiry field should be set
- * in the constructor.
+ * TODO: uncomment when done w/ dev (double declaration makes PhpStorm mad :)
*
- * To add HTML that should be cached, use addCachedHTML like this:
- * $this->addCachedHTML( array( $this, 'displayCachedContent' ) );
- *
- * After adding the last HTML that should be cached, call $this->saveCache();
- *
* @since 0.1
*
* @file SpecialCachedPage.php
@@ -19,241 +15,241 @@
* @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 or a unix timestamp
indicating the point of expiry.
- *
- * @since 0.1
- * @var integer|null
- */
- protected $cacheExpiry = null;
-
- /**
- * List of HTML chunks to be cached (if !hasCached) or that where
cashed (of hasCached).
- * If no cached already, then the newly computed chunks are added here,
- * if it as cached already, chunks are removed from this list as they
are needed.
- *
- * @since 0.1
- * @var array
- */
- protected $cachedChunks;
-
- /**
- * Indicates if the to be cached content was already cached.
- * Null if this information is not available yet.
- *
- * @since 0.1
- * @var boolean|null
- */
- protected $hasCached = null;
-
- /**
- * Main method.
- *
- * @since 0.1
- *
- * @param string|null $subPage
- */
- public function execute( $subPage ) {
- if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
- $this->hasCached = false;
- }
-
- if ( !is_null( $this->cacheExpiry ) ) {
- $this->initCaching();
-
- if ( $this->hasCached === true ) {
- $this->getOutput()->setSubtitle(
$this->getCachedNotice( $subPage ) );
- }
- }
- }
-
- /**
- * Returns a message that notifies the user he/she is looking at
- * a cached version of the page, including a refresh link.
- *
- * @since 0.1
- *
- * @param string|null $subPage
- *
- * @return string
- */
- protected function getCachedNotice( $subPage ) {
- $refreshArgs = $this->getRequest()->getQueryValues();
- unset( $refreshArgs['title'] );
- $refreshArgs['action'] = 'purge';
-
- $refreshLink = Linker::link(
- $this->getTitle( $subPage ),
- $this->msg( 'cachedspecial-refresh-now' )->escaped(),
- array(),
- $refreshArgs
- );
-
- if ( $this->cacheExpiry < 86400 * 3650 ) {
- $message = $this->msg(
- 'cachedspecial-viewing-cached-ttl',
- $this->getDurationText( $this->cacheExpiry )
- )->escaped();
- }
- else {
- $message = $this->msg(
- 'cachedspecial-viewing-cached-ts'
- )->escaped();
- }
-
- return $message . ' ' . $refreshLink;
- }
-
- /**
- * Returns a message with the time to live of the cache.
- * Takes care of compatibility with MW < 1.20, in which
Language::formatDuration was introduced.
- *
- * @since 0.1
- *
- * @param integer $seconds
- * @param array $chosenIntervals
- *
- * @return string
- */
- protected function getDurationText( $seconds, array $chosenIntervals =
array( 'years', 'days', 'hours', 'minutes', 'seconds' ) ) {
- if ( method_exists( $this->getLanguage(), 'formatDuration' ) ) {
- return $this->getLanguage()->formatDuration( $seconds,
$chosenIntervals );
- }
- else {
- $intervals = array(
- 'years' => 31557600, // 86400 * 365.25
- 'weeks' => 604800,
- 'days' => 86400,
- 'hours' => 3600,
- 'minutes' => 60,
- 'seconds' => 1,
- );
-
- if ( !empty( $chosenIntervals ) ) {
- $intervals = array_intersect_key( $intervals,
array_flip( $chosenIntervals ) );
- }
-
- $segments = array();
-
- foreach ( $intervals as $name => $length ) {
- $value = floor( $seconds / $length );
-
- if ( $value > 0 || ( $name == 'seconds' &&
empty( $segments ) ) ) {
- $seconds -= $value * $length;
- $segments[] = $this->msg( 'duration-' .
$name, array( $value ) )->escaped();
- }
- }
-
- return $this->getLanguage()->listToText( $segments );
- }
- }
-
- /**
- * Initializes the caching if not already done so.
- * Should be called before any of the caching functionality is used.
- *
- * @since 0.1
- */
- protected function initCaching() {
- if ( is_null( $this->hasCached ) ) {
- $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
) {
- $this->initCaching();
-
- if ( $this->hasCached ) {
- $html = '';
-
- if ( is_null( $key ) ) {
- $itemKey = array_keys( 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(
$this->cachedChunks );
- }
- }
- 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->getOutput()->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 === false && !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 class SpecialCachedPage extends SpecialPage {
+//
+// /**
+// * The time to live for the cache, in seconds or a unix timestamp
indicating the point of expiry.
+// *
+// * @since 0.1
+// * @var integer|null
+// */
+// protected $cacheExpiry = null;
+//
+// /**
+// * List of HTML chunks to be cached (if !hasCached) or that where
cashed (of hasCached).
+// * If no cached already, then the newly computed chunks are added here,
+// * if it as cached already, chunks are removed from this list as they
are needed.
+// *
+// * @since 0.1
+// * @var array
+// */
+// protected $cachedChunks;
+//
+// /**
+// * Indicates if the to be cached content was already cached.
+// * Null if this information is not available yet.
+// *
+// * @since 0.1
+// * @var boolean|null
+// */
+// protected $hasCached = null;
+//
+// /**
+// * Main method.
+// *
+// * @since 0.1
+// *
+// * @param string|null $subPage
+// */
+// public function execute( $subPage ) {
+// if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
+// $this->hasCached = false;
+// }
+//
+// if ( !is_null( $this->cacheExpiry ) ) {
+// $this->initCaching();
+//
+// if ( $this->hasCached === true ) {
+// $this->getOutput()->setSubtitle(
$this->getCachedNotice( $subPage ) );
+// }
+// }
+// }
+//
+// /**
+// * Returns a message that notifies the user he/she is looking at
+// * a cached version of the page, including a refresh link.
+// *
+// * @since 0.1
+// *
+// * @param string|null $subPage
+// *
+// * @return string
+// */
+// protected function getCachedNotice( $subPage ) {
+// $refreshArgs = $this->getRequest()->getQueryValues();
+// unset( $refreshArgs['title'] );
+// $refreshArgs['action'] = 'purge';
+//
+// $refreshLink = Linker::link(
+// $this->getTitle( $subPage ),
+// $this->msg( 'cachedspecial-refresh-now' )->escaped(),
+// array(),
+// $refreshArgs
+// );
+//
+// if ( $this->cacheExpiry < 86400 * 3650 ) {
+// $message = $this->msg(
+// 'cachedspecial-viewing-cached-ttl',
+// $this->getDurationText( $this->cacheExpiry )
+// )->escaped();
+// }
+// else {
+// $message = $this->msg(
+// 'cachedspecial-viewing-cached-ts'
+// )->escaped();
+// }
+//
+// return $message . ' ' . $refreshLink;
+// }
+//
+// /**
+// * Returns a message with the time to live of the cache.
+// * Takes care of compatibility with MW < 1.20, in which
Language::formatDuration was introduced.
+// *
+// * @since 0.1
+// *
+// * @param integer $seconds
+// * @param array $chosenIntervals
+// *
+// * @return string
+// */
+// protected function getDurationText( $seconds, array $chosenIntervals =
array( 'years', 'days', 'hours', 'minutes', 'seconds' ) ) {
+// if ( method_exists( $this->getLanguage(), 'formatDuration' ) ) {
+// return $this->getLanguage()->formatDuration( $seconds,
$chosenIntervals );
+// }
+// else {
+// $intervals = array(
+// 'years' => 31557600, // 86400 * 365.25
+// 'weeks' => 604800,
+// 'days' => 86400,
+// 'hours' => 3600,
+// 'minutes' => 60,
+// 'seconds' => 1,
+// );
+//
+// if ( !empty( $chosenIntervals ) ) {
+// $intervals = array_intersect_key( $intervals,
array_flip( $chosenIntervals ) );
+// }
+//
+// $segments = array();
+//
+// foreach ( $intervals as $name => $length ) {
+// $value = floor( $seconds / $length );
+//
+// if ( $value > 0 || ( $name == 'seconds' &&
empty( $segments ) ) ) {
+// $seconds -= $value * $length;
+// $segments[] = $this->msg( 'duration-' .
$name, array( $value ) )->escaped();
+// }
+// }
+//
+// return $this->getLanguage()->listToText( $segments );
+// }
+// }
+//
+// /**
+// * Initializes the caching if not already done so.
+// * Should be called before any of the caching functionality is used.
+// *
+// * @since 0.1
+// */
+// protected function initCaching() {
+// if ( is_null( $this->hasCached ) ) {
+// $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
) {
+// $this->initCaching();
+//
+// if ( $this->hasCached ) {
+// $html = '';
+//
+// if ( is_null( $key ) ) {
+// $itemKey = array_keys( 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(
$this->cachedChunks );
+// }
+// }
+// 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->getOutput()->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 === false && !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()
);
+// }
+//
+//}
Modified: trunk/extensions/EducationProgram/includes/EPOrg.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPOrg.php 2012-03-19
18:28:47 UTC (rev 114162)
+++ trunk/extensions/EducationProgram/includes/EPOrg.php 2012-03-19
18:40:22 UTC (rev 114163)
@@ -135,7 +135,7 @@
}
/**
- * Adds a control to add a new org to the provided context.
+ * Returns thr HTML for a control to add a new org to the provided
context.
* Adittional arguments can be provided to set the default values for
the control fields.
*
* @since 0.1
@@ -143,40 +143,34 @@
* @param IContextSource $context
* @param array $args
*
- * @return boolean
+ * @return string
*/
- public static function displayAddNewControl( IContextSource $context,
array $args = array() ) {
- if ( !$context->getUser()->isAllowed( 'ep-org' ) ) {
- return false;
- }
-
- $out = $context->getOutput();
+ public static function getAddNewControl( IContextSource $context, array
$args = array() ) {
+ $html = '';
- $out->addModules( 'ep.addorg' );
-
- $out->addHTML( Html::openElement(
+ $html .= Html::openElement(
'form',
array(
'method' => 'post',
'action' => EPOrgs::singleton()->getTitleFor(
'NAME_PLACEHOLDER' )->getLocalURL( array( 'action' => 'edit' ) ),
)
- ) );
+ );
- $out->addHTML( '<fieldset>' );
+ $html .= '<fieldset>';
- $out->addHTML( '<legend>' . wfMsgHtml( 'ep-institutions-addnew'
) . '</legend>' );
+ $html .= '<legend>' . wfMsgHtml( 'ep-institutions-addnew' ) .
'</legend>';
- $out->addElement( 'p', array(), wfMsg(
'ep-institutions-namedoc' ) );
+ $html .= Html::element( 'p', array(), wfMsg(
'ep-institutions-namedoc' ) );
- $out->addHTML( Xml::inputLabel(
+ $html .= Xml::inputLabel(
wfMsg( 'ep-institutions-newname' ),
'newname',
'newname',
false,
array_key_exists( 'name', $args ) ? $args['name'] :
false
- ) );
+ );
- $out->addHTML( ' ' . Html::input(
+ $html .= ' ' . Html::input(
'addneworg',
wfMsg( 'ep-institutions-add' ),
'submit',
@@ -184,38 +178,39 @@
'disabled' => 'disabled',
'class' => 'ep-org-add',
)
- ) );
+ );
- $out->addHTML( Html::hidden( 'isnew', 1 ) );
+ $html .= Html::hidden( 'isnew', 1 );
- $out->addHTML( '</fieldset></form>' );
+ $html .= '</fieldset></form>';
- return true;
+ return $html;
}
/**
- * Display a pager with courses.
+ * Returns the HTML for a pager with institutions.
*
* @since 0.1
*
* @param IContextSource $context
* @param array $conditions
+ *
+ * @return string
*/
- public static function displayPager( IContextSource $context, array
$conditions = array() ) {
+ public static function getPager( IContextSource $context, array
$conditions = array() ) {
$pager = new EPOrgPager( $context, $conditions );
if ( $pager->getNumRows() ) {
- $context->getOutput()->addHTML(
+ return
$pager->getFilterControl() .
$pager->getNavigationBar() .
$pager->getBody() .
$pager->getNavigationBar() .
- $pager->getMultipleItemControl()
- );
+ $pager->getMultipleItemControl();
}
else {
- $context->getOutput()->addHTML(
$pager->getFilterControl( true ) );
- $context->getOutput()->addWikiMsg(
'ep-institutions-noresults' );
+ return $pager->getFilterControl( true ) .
+ $context->msg( 'ep-institutions-noresults' );
}
}
Modified: trunk/extensions/EducationProgram/specials/SpecialEPPage.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialEPPage.php
2012-03-19 18:28:47 UTC (rev 114162)
+++ trunk/extensions/EducationProgram/specials/SpecialEPPage.php
2012-03-19 18:40:22 UTC (rev 114163)
@@ -54,7 +54,7 @@
* @return boolean
*/
public function execute( $subPage ) {
- parent::execute( $subPage );
+ // parent::execute( $subPage );
$subPage = is_null( $subPage ) ? '' : $subPage;
$this->subPage = trim( str_replace( '_', ' ', $subPage ) );
Modified: trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php
2012-03-19 18:28:47 UTC (rev 114162)
+++ trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php
2012-03-19 18:40:22 UTC (rev 114163)
@@ -19,7 +19,6 @@
* @since 0.1
*/
public function __construct() {
- $this->cacheExpiry = 3600;
parent::__construct( 'EducationProgram' );
}
@@ -33,6 +32,8 @@
public function execute( $subPage ) {
parent::execute( $subPage );
+ $this->startCache( 3600 );
+
$this->displayNavigation();
$this->addCachedHTML( array( $this, 'displaySummaryTable' ) );
Modified: trunk/extensions/EducationProgram/specials/SpecialInstitutions.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialInstitutions.php
2012-03-19 18:28:47 UTC (rev 114162)
+++ trunk/extensions/EducationProgram/specials/SpecialInstitutions.php
2012-03-19 18:40:22 UTC (rev 114163)
@@ -31,16 +31,40 @@
* @param string|null $subPage
*/
public function execute( $subPage ) {
+
parent::execute( $subPage );
if ( $this->subPage === '' ) {
+ $this->startCache( 3600, $this->getUser()->isAnon() );
+
$this->displayNavigation();
- EPOrg::displayAddNewControl( $this->getContext() );
- EPOrg::displayPager( $this->getContext() );
+
+ if ( $this->getUser()->isAllowed( 'ep-org' ) ) {
+ $this->getOutput()->addModules( 'ep.addorg' );
+ $this->addCachedHTML(
'EPOrg::getAddNewControl', array( $this->getContext() ) );
+ }
+
+ $this->addCachedHTML( 'EPOrg::getPager', array(
$this->getContext() ) );
+
+ $this->saveCache();
}
else {
$this->getOutput()->redirect( Title::newFromText(
$this->subPage, EP_NS_INSTITUTION )->getLocalURL() );
}
}
+ /**
+ * @see SpecialCachedPage::getCacheKey
+ * @return array
+ */
+ protected function getCacheKey() {
+ $values = $this->getRequest()->getValues();
+
+ if ( array_key_exists( 'action', $values ) && $values['action']
=== 'purge' ) {
+ unset( $values['action'] );
+ }
+
+ return array_merge( $values, parent::getCacheKey() );
+ }
+
}
Modified: trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php
2012-03-19 18:28:47 UTC (rev 114162)
+++ trunk/extensions/EducationProgram/specials/SpecialStudentActivity.php
2012-03-19 18:40:22 UTC (rev 114163)
@@ -19,7 +19,6 @@
* @since 0.1
*/
public function __construct() {
- $this->cacheExpiry = 180;
parent::__construct( 'StudentActivity' );
}
@@ -33,6 +32,8 @@
public function execute( $subPage ) {
parent::execute( $subPage );
+ $this->startCache( 180 );
+
$this->getOutput()->addModules( 'ep.studentactivity' );
$this->displayNavigation();
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs