Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/252162
Change subject: Switch to use ChangesListSpecialPageFilters hook in client ...................................................................... Switch to use ChangesListSpecialPageFilters hook in client Not in scope of this patch is to rename the recent changes option name etc. While this might seem simple, it needs a migration strategy to convert existing preferences and b/w compat for the old filter name and should be done carefully in a separate patch. Change-Id: I66d345e4355accf44f93ce8c6d765c41cebbcc3b --- M client/WikibaseClient.hooks.php M client/WikibaseClient.php A client/includes/Hooks/ChangesListSpecialPageFilterHandler.php D client/includes/Hooks/ChangesPageWikibaseFilterHandler.php A client/tests/phpunit/includes/Hooks/ChangesListSpecialPageFilterHandlerTest.php D client/tests/phpunit/includes/Hooks/ChangesPageWikibaseFilterHandlerTest.php 6 files changed, 330 insertions(+), 299 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/62/252162/1 diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php index dbf0e26..9178629 100644 --- a/client/WikibaseClient.hooks.php +++ b/client/WikibaseClient.hooks.php @@ -439,54 +439,6 @@ } /** - * Adds a toggle for showing/hiding Wikidata entries in recent changes - * - * @param SpecialRecentChanges $special - * @param array &$filters - * - * @return bool - */ - public static function onSpecialRecentChangesFilters( SpecialRecentChanges $special, array &$filters ) { - $hookHandler = new ChangesPageWikibaseFilterHandler( - $special->getContext(), - WikibaseClient::getDefaultInstance()->getSettings()->getSetting( 'showExternalRecentChanges' ), - 'hidewikidata', - 'rcshowwikidata', - 'wikibase-rc-hide-wikidata' - ); - - // @fixme remove wikidata-specific stuff! - $filters = $hookHandler->addFilterIfEnabled( $filters ); - - return true; - } - - /** - * Modifies watchlist options to show a toggle for Wikibase changes - * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialWatchlistFilters - * - * @since 0.4 - * - * @param SpecialWatchlist $special - * @param array $filters - * - * @return bool - */ - public static function onSpecialWatchlistFilters( $special, &$filters ) { - $hookHandler = new ChangesPageWikibaseFilterHandler( - $special->getContext(), - WikibaseClient::getDefaultInstance()->getSettings()->getSetting( 'showExternalRecentChanges' ), - 'hideWikibase', - 'wlshowwikibase', - 'wikibase-rc-hide-wikidata' - ); - - $filters = $hookHandler->addFilterIfEnabled( $filters ); - - return true; - } - - /** * Adds a preference for showing or hiding Wikidata entries in recent changes * * @param User $user diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php index c57f995..2a57df9 100644 --- a/client/WikibaseClient.php +++ b/client/WikibaseClient.php @@ -111,14 +111,10 @@ $wgHooks['ParserGetVariableValueSwitch'][] = '\Wikibase\ClientHooks::onParserGetVariableValueSwitch'; $wgHooks['SkinTemplateOutputPageBeforeExec'][] = '\Wikibase\ClientHooks::onSkinTemplateOutputPageBeforeExec'; $wgHooks['SpecialMovepageAfterMove'][] = '\Wikibase\Client\Hooks\MovePageNotice::onSpecialMovepageAfterMove'; - $wgHooks['SpecialWatchlistQuery'][] = '\Wikibase\ClientHooks::onSpecialWatchlistQuery'; - $wgHooks['SpecialRecentChangesQuery'][] = '\Wikibase\ClientHooks::onSpecialRecentChangesQuery'; - $wgHooks['SpecialRecentChangesFilters'][] = '\Wikibase\ClientHooks::onSpecialRecentChangesFilters'; $wgHooks['GetPreferences'][] = '\Wikibase\ClientHooks::onGetPreferences'; $wgHooks['BeforePageDisplay'][] = '\Wikibase\ClientHooks::onBeforePageDisplay'; $wgHooks['BeforePageDisplay'][] = '\Wikibase\ClientHooks::onBeforePageDisplayAddJsConfig'; $wgHooks['ScribuntoExternalLibraries'][] = '\Wikibase\ClientHooks::onScribuntoExternalLibraries'; - $wgHooks['SpecialWatchlistFilters'][] = '\Wikibase\ClientHooks::onSpecialWatchlistFilters'; $wgHooks['InfoAction'][] = '\Wikibase\ClientHooks::onInfoAction'; $wgHooks['TitleMoveComplete'][] = '\Wikibase\Client\Hooks\UpdateRepoHookHandlers::onTitleMoveComplete'; $wgHooks['BaseTemplateAfterPortlet'][] = '\Wikibase\ClientHooks::onBaseTemplateAfterPortlet'; @@ -129,6 +125,11 @@ $wgHooks['ParserLimitReportPrepare'][] = '\Wikibase\Client\Hooks\ParserLimitHookHandlers::onParserLimitReportPrepare'; $wgHooks['FormatAutocomments'][] = '\Wikibase\ClientHooks::onFormat'; + // recent changes / watchlist hooks + $wgHooks['ChangesListSpecialPageFilters'][] = '\Wikibase\Client\Hooks\ChangesListSpecialPageFilterHandler::onChangesListSpecialPageFilters'; + $wgHooks['SpecialWatchlistQuery'][] = '\Wikibase\ClientHooks::onSpecialWatchlistQuery'; + $wgHooks['SpecialRecentChangesQuery'][] = '\Wikibase\ClientHooks::onSpecialRecentChangesQuery'; + // update hooks $wgHooks['LoadExtensionSchemaUpdates'][] = '\Wikibase\Client\Usage\Sql\SqlUsageTrackerSchemaUpdater::onSchemaUpdate'; diff --git a/client/includes/Hooks/ChangesListSpecialPageFilterHandler.php b/client/includes/Hooks/ChangesListSpecialPageFilterHandler.php new file mode 100644 index 0000000..09b4516 --- /dev/null +++ b/client/includes/Hooks/ChangesListSpecialPageFilterHandler.php @@ -0,0 +1,165 @@ +<?php + +namespace Wikibase\Client\Hooks; + +use ChangesListSpecialPage; +use User; +use WebRequest; +use Wikibase\Client\WikibaseClient; + +/** + * @since 0.5 + * + * @licence GNU GPL v2+ + * @author Katie Filbert < aude.w...@gmail.com > + */ +class ChangesListSpecialPageFilterHandler { + + /** + * @var WebRequest + */ + private $request; + + /** + * @var User + */ + private $user; + + /** + * @var string + */ + private $pageName; + + /** + * @var boolean + */ + private $showExternalChanges; + + /** + * @param WebRequest $request + * @param User $user + * @param string $pageName + * @param boolean $showExternalChanges + */ + public function __construct( + WebRequest $request, + User $user, + $pageName, + $showExternalChanges + ) { + $this->request = $request; + $this->user = $user; + $this->pageName = $pageName; + $this->showExternalChanges = $showExternalChanges; + } + + /** + * @param ChangesListSpecialPage $specialPage + * + * @return ChangesListSpecialPageFilterHandler + */ + public static function newFromGlobalState( + ChangesListSpecialPage $specialPage + ) { + $context = $specialPage->getContext(); + $settings = WikibaseClient::getDefaultInstance()->getSettings(); + + return new self( + $context->getRequest(), + $context->getUser(), + $specialPage->getName(), + $settings->getSetting( 'showExternalRecentChanges' ) + ); + } + + /** + * Modifies recent changes and watchlist options to show a toggle for Wikibase changes + * @see https://www.mediawiki.org/wiki/Manual:Hooks/ChangesListSpecialPageFilters + * + * @param ChangesListSpecialPage $specialPage + * @param array &$filters + * + * @return bool + */ + public function onChangesListSpecialPageFilters( + ChangesListSpecialPage $specialPage, + array &$filters + ) { + $hookHandler = self::newFromGlobalState( $specialPage ); + $hookHandler->addFilterIfEnabled( $filters ); + + return true; + } + + /** + * @param array &$filters + */ + public function addFilterIfEnabled( array &$filters ) { + if ( $this->shouldAddFilter() ) { + $toggleDefault = $this->getShowWikibaseEditsByDefaultSetting(); + $this->addFilter( $filters, $toggleDefault ); + } + } + + /** + * @return boolean + */ + private function shouldAddFilter() { + return $this->showExternalChanges && !$this->isEnhancedChangesEnabled(); + } + + /** + * @param array &$filters + * @param boolean $toggleDefault + */ + private function addFilter( array &$filters, $toggleDefault ) { + $filterName = $this->getFilterName(); + + $filters[$filterName] = array( + 'msg' => 'wikibase-rc-hide-wikidata', + 'default' => $toggleDefault + ); + + return $filters; + } + + /** + * @return boolean + */ + private function getShowWikibaseEditsByDefaultSetting() { + return !$this->user->getOption( $this->getOptionName() ); + } + + /** + * @return boolean + */ + private function isEnhancedChangesEnabled() { + $enhancedChangesUserOption = $this->user->getOption( 'usenewrc' ); + + return $this->request->getBool( 'enhanced', $enhancedChangesUserOption ); + } + + /** + * @return string + */ + private function getFilterName() { + if ( $this->pageName === 'Watchlist' ) { + return 'hideWikibase'; + } + + return 'hidewikidata'; + } + + /** + * @return string + */ + private function getOptionName() { + if ( $this->pageName === 'Watchlist' ) { + return 'wlshowwikibase'; + } + + return 'rcshowwikidata'; + } + + +} diff --git a/client/includes/Hooks/ChangesPageWikibaseFilterHandler.php b/client/includes/Hooks/ChangesPageWikibaseFilterHandler.php deleted file mode 100644 index 45f358b..0000000 --- a/client/includes/Hooks/ChangesPageWikibaseFilterHandler.php +++ /dev/null @@ -1,130 +0,0 @@ -<?php - -namespace Wikibase\Client\Hooks; - -use IContextSource; -use User; - -/** - * @since 0.5 - * - * @licence GNU GPL v2+ - * @author Katie Filbert < aude.w...@gmail.com > - */ -class ChangesPageWikibaseFilterHandler { - - /** - * @var IContextSource - */ - private $context; - - /** - * @var boolean - */ - private $showExternalChanges; - - /** - * @var string - */ - private $filterName; - - /** - * @var string - */ - private $optionName; - - /** - * @var string - */ - private $toggleMessageKey; - - /** - * @param IContextSource $context - * @param boolean $showExternalChanges - * @param string $filterName - name for Wikibase toggle in FormOptions - * @param string $optionName - user option name for showing Wikibase edits by default - * @param string $toggleMessageKey - */ - public function __construct( - IContextSource $context, - $showExternalChanges, - $filterName, - $optionName, - $toggleMessageKey - ) { - $this->context = $context; - $this->showExternalChanges = $showExternalChanges; - $this->filterName = $filterName; - $this->optionName = $optionName; - $this->toggleMessageKey = $toggleMessageKey; - } - - /** - * @param array $filters - * - * @return array - */ - public function addFilterIfEnabled( array $filters ) { - $user = $this->context->getUser(); - - if ( !$this->shouldAddFilter( $user ) ) { - return $filters; - } - - $toggleDefault = $this->showWikibaseEditsByDefault( $user ); - $filters = $this->addFilter( $filters, $toggleDefault ); - - return $filters; - } - - /** - * @param User $user - * - * @return boolean - */ - private function shouldAddFilter( User $user ) { - if ( $this->showExternalChanges && !$this->isEnhancedChangesEnabled( $user ) ) { - return true; - } - - return false; - } - - /** - * @param array $filters - * @param boolean $toggleDefault - * - * @return array - */ - private function addFilter( array $filters, $toggleDefault ) { - $filters["{$this->filterName}"] = array( - 'msg' => $this->toggleMessageKey, - 'default' => $toggleDefault - ); - - return $filters; - } - - /** - * @param User $user - * - * @return boolean - */ - private function showWikibaseEditsByDefault( User $user ) { - return !$user->getOption( $this->optionName ); - } - - /** - * @param User $user - * - * @return boolean - */ - private function isEnhancedChangesEnabled( User $user ) { - $enhancedChangesUserOption = $user->getOption( 'usenewrc' ); - - $isEnabled = $this->context->getRequest()->getBool( 'enhanced', $enhancedChangesUserOption ); - - return $isEnabled; - } - -} diff --git a/client/tests/phpunit/includes/Hooks/ChangesListSpecialPageFilterHandlerTest.php b/client/tests/phpunit/includes/Hooks/ChangesListSpecialPageFilterHandlerTest.php new file mode 100644 index 0000000..e942923 --- /dev/null +++ b/client/tests/phpunit/includes/Hooks/ChangesListSpecialPageFilterHandlerTest.php @@ -0,0 +1,160 @@ +<?php + +namespace Wikibase\Client\Tests\Hooks; + +use DerivativeContext; +use FauxRequest; +use RequestContext; +use Wikibase\Client\Hooks\ChangesListSpecialPageFilterHandler; + +/** + * @covers Wikibase\Client\Hooks\ChangesListSpecialPageFilterHandler + * + * @group WikibaseClientHooks + * @group WikibaseClient + * @group Wikibase + */ +class ChangesListSpecialPageFilterHandlerTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider filterNotAddedWhenUsingEnhancedChangesProvider + */ + public function testFilterNotAddedWhenUsingEnhancedChanges( + $enhancedChangesDefault, + $enhancedMode, + $pageName, + $message + ) { + $hookHandler = new ChangesListSpecialPageFilterHandler( + $this->getRequest( $enhancedMode ), + $this->getUser( $enhancedChangesDefault, false ), + $pageName, + true + ); + + $filters = array(); + $hookHandler->addFilterIfEnabled( $filters ); + + $this->assertEquals( array(), $filters, $message ); + } + + public function filterNotAddedWhenUsingEnhancedChangesProvider() { + return array( + array( true, true, 'Watchlist', 'enhanced default pref and using in Wathclist' ), + array( true, true, 'RecentChanges', 'enhanced default pref and using in RC' ), + array( false, true, 'Watchlist', 'enhanced not default but using in Watchlist' ), + array( false, true, 'RecentChanges', 'enhanced not default but using in RC' ) + ); + } + + /** + * @dataProvider changesPageProvider + */ + public function testFilterAddedWhenNotUsingEnhancedChanges( + $expectedFilterName, + $expectedFilterMessageKey, + $specialPageName + ) { + $hookHandler = new ChangesListSpecialPageFilterHandler( + $this->getRequest( false ), + $this->getUser( false, true ), + $specialPageName, + true + ); + + $filters = array(); + $hookHandler->addFilterIfEnabled( $filters ); + + $expected = array( + $expectedFilterName => array( + 'msg' => $expectedFilterMessageKey, + 'default' => false + ) + ); + + $this->assertEquals( $expected, $filters ); + } + + /** + * @dataProvider changesPageProvider + */ + public function testFilterAddedAndEnabledByDefault_WhenNotUsingEnhancedChanges( + $expectedFilterName, + $expectedFilterMessageKey, + $specialPageName + ) { + $hookHandler = new ChangesListSpecialPageFilterHandler( + $this->getRequest( false ), + $this->getUser( false, false ), + $specialPageName, + true + ); + + $filters = array(); + $hookHandler->addFilterIfEnabled( $filters ); + + $expected = array( + $expectedFilterName => array( + 'msg' => $expectedFilterMessageKey, + 'default' => true + ) + ); + + $this->assertEquals( $expected, $filters ); + } + + /** + * @dataProvider changesPageProvider + */ + public function testFilterNotAddedWhenExternalRecentChangesDisabled( + $expectedFilterName, + $expectedFilterMessageKey, + $specialPageName + ) { + $hookHandler = new ChangesListSpecialPageFilterHandler( + $this->getRequest( false ), + $this->getUser( false, false ), + $specialPageName, + false + ); + + $filters = array(); + $hookHandler->addFilterIfEnabled( $filters ); + + $this->assertEquals( array(), $filters ); + } + + public function changesPageProvider() { + return array( + array( 'hideWikibase', 'wikibase-rc-hide-wikidata', 'Watchlist' ), + array( 'hidewikidata', 'wikibase-rc-hide-wikidata', 'RecentChanges' ), + array( 'hidewikidata', 'wikibase-rc-hide-wikidata', 'RecentChangesLinked' ) + ); + } + + private function getRequest( $enhancedMode ) { + return new FauxRequest( array( 'enhanced' => $enhancedMode ) ); + } + + private function getUser( $enhancedChangesPref, $hideWikibaseEditsByDefault ) { + $user = $this->getMockBuilder( 'User' ) + ->disableOriginalConstructor() + ->getMock(); + + $user->expects( $this->any() ) + ->method( 'getOption' ) + ->will( $this->returnCallback( function( $optionName ) use ( + $enhancedChangesPref, + $hideWikibaseEditsByDefault + ) { + if ( $optionName === 'usenewrc' ) { + return $enhancedChangesPref; + } else { + return $hideWikibaseEditsByDefault; + } + } ) ); + + return $user; + } + +} diff --git a/client/tests/phpunit/includes/Hooks/ChangesPageWikibaseFilterHandlerTest.php b/client/tests/phpunit/includes/Hooks/ChangesPageWikibaseFilterHandlerTest.php deleted file mode 100644 index e557709..0000000 --- a/client/tests/phpunit/includes/Hooks/ChangesPageWikibaseFilterHandlerTest.php +++ /dev/null @@ -1,117 +0,0 @@ -<?php - -namespace Wikibase\Client\Tests\Hooks; - -use DerivativeContext; -use FauxRequest; -use RequestContext; -use Wikibase\Client\Hooks\ChangesPageWikibaseFilterHandler; - -/** - * @covers Wikibase\Client\Hooks\ChangesPageWikibaseFilterHandler - * - * @group WikibaseClientHooks - * @group WikibaseClient - * @group Wikibase - */ -class ChangesPageWikibaseFilterHandlerTest extends \PHPUnit_Framework_TestCase { - - /** - * @dataProvider filterNotAddedWhenUsingEnhancedChangesProvider - */ - public function testFilterNotAddedWhenUsingEnhancedChanges( - $enhancedChangesDefault, - $useEnhancedChanges - ) { - $context = $this->getContext( $enhancedChangesDefault, $useEnhancedChanges, false ); - $hookHandler = new ChangesPageWikibaseFilterHandler( $context, true, 'foo', 'bar', 'foobar' ); - - $filters = array(); - $filters = $hookHandler->addFilterIfEnabled( $filters ); - - $this->assertEquals( array(), $filters ); - } - - public function filterNotAddedWhenUsingEnhancedChangesProvider() { - return array( - array( true, true, 'enhanced changes default preference and using' ), - array( false, true, 'enhanced changes not default but using' ) - ); - } - - public function testFilterAddedWhenNotUsingEnhancedChanges() { - $context = $this->getContext( false, false, true ); - $hookHandler = new ChangesPageWikibaseFilterHandler( $context, true, 'foo', 'bar', 'foobar' ); - - $filters = array(); - $filters = $hookHandler->addFilterIfEnabled( $filters ); - - $expected = array( - 'foo' => array( - 'msg' => 'foobar', - 'default' => false - ) - ); - - $this->assertEquals( $expected, $filters ); - } - - public function testFilterAddedAndEnabledByDefault_WhenNotUsingEnhancedChanges() { - $context = $this->getContext( false, false, false ); - $hookHandler = new ChangesPageWikibaseFilterHandler( $context, true, 'foo', 'bar', 'foobar' ); - - $filters = array(); - $filters = $hookHandler->addFilterIfEnabled( $filters ); - - $expected = array( - 'foo' => array( - 'msg' => 'foobar', - 'default' => true - ) - ); - - $this->assertEquals( $expected, $filters ); - } - - public function testFilterNotAddedWhenExternalRecentChangesDisabled() { - $context = $this->getContext( false, false, false ); - $hookHandler = new ChangesPageWikibaseFilterHandler( $context, false, 'foo', 'bar', 'foobar' ); - - $filters = array(); - $filters = $hookHandler->addFilterIfEnabled( $filters ); - - $this->assertEquals( array(), $filters ); - } - - private function getContext( $enhancedChangesPref, $useEnhanced, $hideWikibaseEditsByDefault ) { - $context = new DerivativeContext( RequestContext::getMain() ); - $context->setUser( $this->getUser( $enhancedChangesPref, $hideWikibaseEditsByDefault ) ); - - $request = new FauxRequest( array( 'enhanced' => $useEnhanced ) ); - $context->setRequest( $request ); - - return $context; - } - - private function getUser( $enhancedChangesPref, $hideWikibaseEditsByDefault ) { - $user = $this->getMockBuilder( 'User' ) - ->disableOriginalConstructor() - ->getMock(); - - $user->expects( $this->any() ) - ->method( 'getOption' ) - ->will( $this->returnCallback( function( $optionName ) use ( - $enhancedChangesPref, - $hideWikibaseEditsByDefault - ) { - if ( $optionName === 'usenewrc' ) { - return $enhancedChangesPref; - } else { - return $hideWikibaseEditsByDefault; - } - } ) ); - - return $user; - } - -} -- To view, visit https://gerrit.wikimedia.org/r/252162 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I66d345e4355accf44f93ce8c6d765c41cebbcc3b Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits