From 5320f0835ef99b68b8f174a8e1fcaefe3c80491f Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 29 Sep 2016 01:16:05 -0700 Subject: [PATCH] Use wikimedia/scoped-callback The ScopedCallback class was moved into a separate library. This updates all callers to use the namespaced version, and provides a backwards-compatibility class wrapper under the old name. Bug: T146258 Change-Id: I2dd0a66fe2f510f26bdfef6b0a975c1beb3fd93c Depends-On: Iea0c40bdd7776372ccf72db8f088a2abaa4d3721 --- autoload.php | 2 +- composer.json | 1 + includes/GlobalFunctions.php | 1 + includes/PageProps.php | 1 + includes/compat/ScopedCallback.php | 29 +++++++ includes/libs/ScopedCallback.php | 77 ------------------- includes/profiler/SectionProfiler.php | 1 + .../libs/objectcache/BagOStuffTest.php | 11 ++- tests/phpunit/includes/session/TestUtils.php | 2 +- 9 files changed, 42 insertions(+), 83 deletions(-) create mode 100644 includes/compat/ScopedCallback.php delete mode 100644 includes/libs/ScopedCallback.php diff --git a/autoload.php b/autoload.php index 8e019b8272..3b428701ac 100644 --- a/autoload.php +++ b/autoload.php @@ -1235,7 +1235,7 @@ $wgAutoloadLocalClasses = [ 'SamplingStatsdClient' => __DIR__ . '/includes/libs/stats/SamplingStatsdClient.php', 'Sanitizer' => __DIR__ . '/includes/Sanitizer.php', 'SavepointPostgres' => __DIR__ . '/includes/libs/rdbms/database/utils/SavepointPostgres.php', - 'ScopedCallback' => __DIR__ . '/includes/libs/ScopedCallback.php', + 'ScopedCallback' => __DIR__ . '/includes/compat/ScopedCallback.php', 'ScopedLock' => __DIR__ . '/includes/libs/lockmanager/ScopedLock.php', 'SearchApi' => __DIR__ . '/includes/api/SearchApi.php', 'SearchDatabase' => __DIR__ . '/includes/search/SearchDatabase.php', diff --git a/composer.json b/composer.json index 9424a73bff..c827c0f100 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ "wikimedia/php-session-serializer": "1.0.3", "wikimedia/relpath": "1.0.3", "wikimedia/running-stat": "1.1.0", + "wikimedia/scoped-callback": "1.0.0", "wikimedia/utfnormal": "1.0.3", "wikimedia/wrappedstring": "2.2.0", "zordius/lightncandy": "0.23" diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 311cf25ecc..2b6088edeb 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -27,6 +27,7 @@ if ( !defined( 'MEDIAWIKI' ) ) { use Liuggio\StatsdClient\Sender\SocketSender; use MediaWiki\Logger\LoggerFactory; use MediaWiki\Session\SessionManager; +use Wikimedia\ScopedCallback; // Hide compatibility functions from Doxygen /// @cond diff --git a/includes/PageProps.php b/includes/PageProps.php index ed06935c2c..d09e1fbf9d 100644 --- a/includes/PageProps.php +++ b/includes/PageProps.php @@ -19,6 +19,7 @@ * * @file */ +use Wikimedia\ScopedCallback; /** * Gives access to properties of a page. diff --git a/includes/compat/ScopedCallback.php b/includes/compat/ScopedCallback.php new file mode 100644 index 0000000000..4fd4bc7989 --- /dev/null +++ b/includes/compat/ScopedCallback.php @@ -0,0 +1,29 @@ +callback = $callback; - $this->params = $params; - } - - /** - * Trigger a scoped callback and destroy it. - * This is the same is just setting it to null. - * - * @param ScopedCallback $sc - */ - public static function consume( ScopedCallback &$sc = null ) { - $sc = null; - } - - /** - * Destroy a scoped callback without triggering it - * - * @param ScopedCallback $sc - */ - public static function cancel( ScopedCallback &$sc = null ) { - if ( $sc ) { - $sc->callback = null; - } - $sc = null; - } - - /** - * Trigger the callback when this leaves scope - */ - function __destruct() { - if ( $this->callback !== null ) { - call_user_func_array( $this->callback, $this->params ); - } - } -} diff --git a/includes/profiler/SectionProfiler.php b/includes/profiler/SectionProfiler.php index 65ac6e6141..bbe139b0ae 100644 --- a/includes/profiler/SectionProfiler.php +++ b/includes/profiler/SectionProfiler.php @@ -21,6 +21,7 @@ * @ingroup Profiler * @author Aaron Schulz */ +use Wikimedia\ScopedCallback; /** * Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle diff --git a/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php b/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php index 92fb95418c..a1afa77726 100644 --- a/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php +++ b/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php @@ -1,4 +1,7 @@ * @group BagOStuff @@ -251,20 +254,20 @@ class BagOStuffTest extends MediaWikiTestCase { $value1 = $this->cache->getScopedLock( $key, 0 ); $value2 = $this->cache->getScopedLock( $key, 0 ); - $this->assertType( 'ScopedCallback', $value1, 'First call returned lock' ); + $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' ); $this->assertNull( $value2, 'Duplicate call returned no lock' ); unset( $value1 ); $value3 = $this->cache->getScopedLock( $key, 0 ); - $this->assertType( 'ScopedCallback', $value3, 'Lock returned callback after release' ); + $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' ); unset( $value3 ); $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' ); $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' ); - $this->assertType( 'ScopedCallback', $value1, 'First reentrant call returned lock' ); - $this->assertType( 'ScopedCallback', $value1, 'Second reentrant call returned lock' ); + $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' ); + $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' ); } /** diff --git a/tests/phpunit/includes/session/TestUtils.php b/tests/phpunit/includes/session/TestUtils.php index f1dc9e994b..0cc8ebf134 100644 --- a/tests/phpunit/includes/session/TestUtils.php +++ b/tests/phpunit/includes/session/TestUtils.php @@ -12,7 +12,7 @@ class TestUtils { /** * Override the singleton for unit testing * @param SessionManager|null $manager - * @return \\ScopedCallback|null + * @return \\Wikimedia\ScopedCallback|null */ public static function setSessionManagerSingleton( SessionManager $manager = null ) { session_write_close(); -- 2.20.1