Demon has uploaded a new change for review. https://gerrit.wikimedia.org/r/58230
Change subject: Added a PoolCounterWorkViaCallback convenience class. ...................................................................... Added a PoolCounterWorkViaCallback convenience class. Change-Id: I1cbb2cc8a43eea2d28f38cfd2d3a4908dc7d0da9 --- M includes/AutoLoader.php M includes/PoolCounter.php 2 files changed, 72 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/30/58230/1 diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 84f5caa..31c7e09 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -194,6 +194,7 @@ 'PoolCounter' => 'includes/PoolCounter.php', 'PoolCounter_Stub' => 'includes/PoolCounter.php', 'PoolCounterWork' => 'includes/PoolCounter.php', + 'PoolCounterWorkViaCallback' => 'includes/PoolCounter.php', 'PoolWorkArticleView' => 'includes/WikiPage.php', 'Preferences' => 'includes/Preferences.php', 'PreferencesForm' => 'includes/Preferences.php', diff --git a/includes/PoolCounter.php b/includes/PoolCounter.php index 2ebef04..01a2047 100644 --- a/includes/PoolCounter.php +++ b/includes/PoolCounter.php @@ -142,7 +142,7 @@ } /** - * Handy class for dealing with PoolCounters using class members instead of callbacks. + * Class for dealing with PoolCounters using class members */ abstract class PoolCounterWork { protected $cacheable = false; //Does this override getCachedWork() ? @@ -244,3 +244,73 @@ $this->poolCounter = PoolCounter::factory( $type, $key ); } } + +/** + * Convenience class for dealing with PoolCounters using callbacks + * @since 1.22 + */ +class PoolCounterWorkViaCallback extends PoolCounterWork { + /** @var callable */ + protected $doWork; + /** @var callable|null */ + protected $doCachedWork; + /** @var callable|null */ + protected $fallback; + /** @var callable|null */ + protected $error; + + /** + * Build a PoolCounterWork class from a type, key, and callback map. + * + * The callback map must at least have a callback for the 'doWork' method. + * Additionally, callbacks can be provided for the 'doCachedWork', 'fallback', + * and 'error' methods. Methods without callbacks will be no-ops that return false. + * If a 'doCachedWork' callback is provided, then execute() may wait for any prior + * process in the pool to finish and reuse its cached result. + * + * @param string $type + * @param string $key + * @param array $callbacks Map of callbacks + * @throws MWException + */ + public function __construct( $type, $key, array $callbacks ) { + parent::__construct( $type, $key ); + foreach ( array( 'doWork', 'doCachedWork', 'fallback', 'error' ) as $name ) { + if ( isset( $callbacks[$name] ) ) { + if ( !is_callable( $callbacks[$name] ) ) { + throw new MWException( "Invalid callback provided for '$name' function." ); + } + $this->$name = $callbacks[$name]; + } + } + if ( !isset( $this->doWork ) ) { + throw new MWException( "No callback provided for 'doWork' function." ); + } + $this->cacheable = isset( $this->doCachedWork ); + } + + public function doWork() { + return call_user_func_array( $this->doWork, array() ); + } + + public function getCachedWork() { + if ( $this->doCachedWork ) { + return call_user_func_array( $this->doCachedWork, array() ); + } + return false; + } + + function fallback() { + if ( $this->fallback ) { + return call_user_func_array( $this->fallback, array() ); + } + return false; + } + + function error( $status ) { + if ( $this->error ) { + return call_user_func_array( $this->error, array( $status ) ); + } + return false; + } +} -- To view, visit https://gerrit.wikimedia.org/r/58230 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1cbb2cc8a43eea2d28f38cfd2d3a4908dc7d0da9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: wmf/1.21wmf12 Gerrit-Owner: Demon <ch...@wikimedia.org> Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits