Aude has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/254681

Change subject: Consolidate watchlist / recent changes query hook handling
......................................................................

Consolidate watchlist / recent changes query hook handling

Previously the watchlist and recent changes hooks were doing
somewhat different things, with the watchlist doing too much
and too complicated.

All the recent changes hook did was add a where condition
(rc_type != RC_EXTERNAL) when we want Wikibase changes hidden.
This is what we should do as well for the watchlist, and
if Wikibase changes are to be displayed then we don't need
to interfere with what core does.

This patch also fixes the issue of watchlist showing more
than the last change for a page, if the "Expand watchlist to show
all changes, not just the most recent" option is turned off
in preferences.

Some of the code and the dependencies needed for this hook
and the ChangesListSpecialPageFilters hook are the same
so they are handled here in the same hook handler class.

Bug: T64798
Bug: T50047
Change-Id: Ib660a016c075c51660b18cea777a4431537ecb69
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.php
D client/includes/Hooks/ChangesListSpecialPageFilterHandler.php
A client/includes/Hooks/ChangesListSpecialPageHooksHandler.php
D client/includes/Hooks/SpecialWatchlistQueryHandler.php
D client/includes/recentchanges/RecentChangesFilterOptions.php
R client/tests/phpunit/includes/Hooks/ChangesListSpecialPageHooksHandlerTest.php
D client/tests/phpunit/includes/Hooks/SpecialWatchlistQueryHandlerTest.php
D client/tests/phpunit/includes/recentchanges/RecentChangesFilterOptionsTest.php
9 files changed, 369 insertions(+), 698 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/81/254681/1

diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index c085481..015fbdd 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -21,10 +21,8 @@
 use Wikibase\Client\Hooks\BeforePageDisplayHandler;
 use Wikibase\Client\Hooks\DeletePageNoticeCreator;
 use Wikibase\Client\Hooks\InfoActionHookHandler;
-use Wikibase\Client\Hooks\SpecialWatchlistQueryHandler;
 use Wikibase\Client\RecentChanges\ChangeLineFormatter;
 use Wikibase\Client\RecentChanges\ExternalChangeFactory;
-use Wikibase\Client\RecentChanges\RecentChangesFilterOptions;
 use Wikibase\Client\RepoItemLinkGenerator;
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\Entity\EntityId;
@@ -88,33 +86,6 @@
                if ( $engine == 'lua' && $allowDataTransclusion === true ) {
                        $extraLibraries['mw.wikibase'] = 
'Wikibase\Client\DataAccess\Scribunto\Scribunto_LuaWikibaseLibrary';
                        $extraLibraries['mw.wikibase.entity'] = 
'Wikibase\Client\DataAccess\Scribunto\Scribunto_LuaWikibaseEntityLibrary';
-               }
-
-               return true;
-       }
-
-       /**
-        * Hook for modifying the query for fetching recent changes
-        * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/SpecialRecentChangesQuery
-        *
-        * @since 0.2
-        *
-        * @param array &$conds
-        * @param string[] &$tables
-        * @param array &$join_conds
-        * @param FormOptions $opts
-        * @param array &$query_options
-        * @param string[] &$fields
-        *
-        * @return bool
-        */
-       public static function onSpecialRecentChangesQuery( array &$conds, 
array &$tables,
-               array &$join_conds, FormOptions $opts, array &$query_options, 
array &$fields
-       ) {
-               $rcFilterOpts = new RecentChangesFilterOptions( $opts );
-
-               if ( $rcFilterOpts->showWikibaseEdits() === false ) {
-                       $conds[] = 'rc_type != ' . RC_EXTERNAL;
                }
 
                return true;
@@ -211,38 +182,6 @@
                if ( is_string( $formattedComment ) ) {
                        $comment = $formatter->wrapAutoComment( $pre, 
$formattedComment, $post );
                }
-       }
-
-       /**
-        * Modifies watchlist query to include external changes
-        * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/SpecialWatchlistQuery
-        *
-        * @since 0.2
-        *
-        * @param array &$conds
-        * @param array &$tables
-        * @param array &$join_conds
-        * @param array &$fields
-        * @param FormOptions|array|null $opts MediaWiki 1.22 used an array and 
MobileFrontend still does.
-        *
-        * @return bool
-        */
-       public static function onSpecialWatchlistQuery(
-               array &$conds,
-               array &$tables,
-               array &$join_conds,
-               array &$fields,
-               $opts = null
-       ) {
-               $db = wfGetDB( DB_SLAVE );
-               $settings = WikibaseClient::getDefaultInstance()->getSettings();
-               $showExternalChanges = $settings->getSetting( 
'showExternalRecentChanges' );
-
-               $handler = new SpecialWatchlistQueryHandler( 
$GLOBALS['wgUser'], $db, $showExternalChanges );
-
-               $conds = $handler->addWikibaseConditions( 
$GLOBALS['wgRequest'], $conds, $opts );
-
-               return true;
        }
 
        /**
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 0140d0a..ef34602 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -127,9 +127,8 @@
        $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';
+       $wgHooks['ChangesListSpecialPageFilters'][] = 
'\Wikibase\Client\Hooks\ChangesListSpecialPageHooksHandler::onChangesListSpecialPageFilters';
+       $wgHooks['ChangesListSpecialPageQuery'][] = 
'\Wikibase\Client\Hooks\ChangesListSpecialPageHooksHandler::onChangesListSpecialPageQuery';
 
        // update hooks
        $wgHooks['LoadExtensionSchemaUpdates'][] = 
'\Wikibase\Client\Usage\Sql\SqlUsageTrackerSchemaUpdater::onSchemaUpdate';
diff --git a/client/includes/Hooks/ChangesListSpecialPageFilterHandler.php 
b/client/includes/Hooks/ChangesListSpecialPageFilterHandler.php
deleted file mode 100644
index 04bbc18..0000000
--- a/client/includes/Hooks/ChangesListSpecialPageFilterHandler.php
+++ /dev/null
@@ -1,166 +0,0 @@
-<?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 bool
-        */
-       private $showExternalChanges;
-
-       /**
-        * @param WebRequest $request
-        * @param User $user
-        * @param string $pageName
-        * @param bool $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
-        */
-       private 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 static 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() ) {
-                       $filterName = $this->getFilterName();
-
-                       $filters[$filterName] = array(
-                               'msg' => 'wikibase-rc-hide-wikidata',
-                               'default' => $this->getToggleDefault()
-                       );
-               }
-       }
-
-       /**
-        * @return bool
-        */
-       private function shouldAddFilter() {
-               return $this->showExternalChanges && 
!$this->isEnhancedChangesEnabled();
-       }
-
-       /**
-        * @return bool
-        */
-       private function getToggleDefault() {
-               if ( $this->request->getVal( 'action' ) === 'submit' ) {
-                       return $this->request->getBool( $this->getFilterName() 
);
-               }
-
-               // if preference enabled, then Wikibase edits are included by 
default and
-               // the toggle default value needs to be the inverse to hide 
them, and vice versa.
-               return !$this->hasShowWikibaseEditsPrefEnabled();
-       }
-
-       /**
-        * @return bool
-        */
-       private function hasShowWikibaseEditsPrefEnabled() {
-               return (bool)$this->user->getOption( $this->getOptionName() );
-       }
-
-       /**
-        * @return bool
-        */
-       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/ChangesListSpecialPageHooksHandler.php 
b/client/includes/Hooks/ChangesListSpecialPageHooksHandler.php
new file mode 100644
index 0000000..8132b56
--- /dev/null
+++ b/client/includes/Hooks/ChangesListSpecialPageHooksHandler.php
@@ -0,0 +1,264 @@
+<?php
+
+namespace Wikibase\Client\Hooks;
+
+use ChangesListSpecialPage;
+use FormOptions;
+use IContextSource;
+use RequestContext;
+use User;
+use WebRequest;
+use Wikibase\Client\WikibaseClient;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < aude.w...@gmail.com >
+ */
+class ChangesListSpecialPageHooksHandler {
+
+       /**
+        * @var WebRequest
+        */
+       private $request;
+
+       /**
+        * @var User
+        */
+       private $user;
+
+       /**
+        * @var string
+        */
+       private $pageName;
+
+       /**
+        * @var bool
+        */
+       private $showExternalChanges;
+
+       /**
+        * @var ChangesListSpecialPageHooksHandler
+        */
+       private static $instance = null;
+
+       /**
+        * @param WebRequest $request
+        * @param User $user
+        * @param string $pageName
+        * @param bool $showExternalChanges
+        */
+       public function __construct(
+               WebRequest $request,
+               User $user,
+               $pageName,
+               $showExternalChanges
+       ) {
+               $this->request = $request;
+               $this->user = $user;
+               $this->pageName = $pageName;
+               $this->showExternalChanges = $showExternalChanges;
+       }
+
+       /**
+        * @param IContextSource $context
+        * @param string $specialPageName
+        *
+        * @return ChangesListSpecialPageHooksHandler
+        */
+       private static function newFromGlobalState(
+               IContextSource $context,
+               $specialPageName
+       ) {
+               $settings = WikibaseClient::getDefaultInstance()->getSettings();
+
+               return new self(
+                       $context->getRequest(),
+                       $context->getUser(),
+                       $specialPageName,
+                       $settings->getSetting( 'showExternalRecentChanges' )
+               );
+       }
+
+       /**
+        * @param IContextSource $context
+        * @param string $specialPageName
+        *
+        * @return ChangesListSpecialPageHooksHandler
+        */
+       private static function getInstance(
+               IContextSource $context,
+               $specialPageName
+       ) {
+               if ( self::$instance === null ) {
+                       self::$instance = self::newFromGlobalState( $context, 
$specialPageName );
+               }
+
+               return self::$instance;
+       }
+
+       /**
+        * 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 static function onChangesListSpecialPageFilters(
+               ChangesListSpecialPage $specialPage,
+               array &$filters
+       ) {
+               $hookHandler = self::getInstance(
+                       $specialPage->getContext(),
+                       $specialPage->getName()
+               );
+
+               $hookHandler->addFilterIfEnabled( $filters );
+
+               return true;
+       }
+
+
+       /**
+        * Modifies watchlist and recent changes query to include external 
changes
+        * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/ChangesListSpecialPageQuery
+        *
+        * @param string $specialPageName
+        * @param array &$tables
+        * @param array &$fields
+        * @param array &$conds
+        * @param array &$query_options
+        * @param array &$join_conds
+        * @param FormOptions $opts
+        *
+        * @return bool
+        */
+       public function onChangesListSpecialPageQuery(
+               $specialPageName,
+               array &$tables,
+               array &$fields,
+               array &$conds,
+               array &$query_options,
+               array &$join_conds,
+               FormOptions $opts
+       ) {
+               $hookHandler = self::getInstance(
+                       RequestContext::getMain(),
+                       $specialPageName
+               );
+
+               $conds = $hookHandler->addWikibaseConditions( $conds, $opts );
+
+               return true;
+       }
+
+       /**
+        * @param array &$filters
+        */
+       public function addFilterIfEnabled( array &$filters ) {
+               if ( $this->hasWikibaseChangesEnabled() ) {
+                       $filterName = $this->getFilterName();
+
+                       // the toggle needs to be the inverse to invoke the 
inverse display status.
+                       // e.g. if Wikibase changes currently hidden, then when 
the user
+                       // clicks the toggle, then Wikibase changes are 
displayed.
+                       $filters[$filterName] = array(
+                               'msg' => 'wikibase-rc-hide-wikidata',
+                               'default' => 
!$this->hasWikibaseChangesDisplayed()
+                       );
+               }
+       }
+
+       /**
+        * @param array $conds
+        * @param FormOptions
+        *
+        * @return array
+        */
+       public function addWikibaseConditions( array $conds, FormOptions $opts 
) {
+               // do not include wikibase changes for activated enhanced 
watchlist
+               // since we do not support that format yet
+               if ( $this->shouldHideWikibaseChanges( $opts ) ) {
+                       $conds[] = 'rc_type != ' . RC_EXTERNAL;
+               }
+
+               return $conds;
+       }
+
+       /**
+        * @param FormOptions $opts
+        *
+        * @return boolean
+        */
+       private function shouldHideWikibaseChanges( FormOptions $opts ) {
+               if ( !$this->hasWikibaseChangesEnabled() ) {
+                       return true;
+               }
+
+               $filterName = $this->getFilterName();
+
+               return $opts->offsetExists( $filterName ) && $opts->getValue( 
$filterName ) === true;
+       }
+
+       /**
+        * @return bool
+        */
+       private function hasWikibaseChangesEnabled() {
+               return $this->showExternalChanges && 
!$this->isEnhancedChangesEnabled();
+       }
+
+       /**
+        * @return bool
+        */
+       private function hasWikibaseChangesDisplayed() {
+               if ( $this->request->getVal( 'action' ) === 'submit' ) {
+                       return !$this->request->getBool( $this->getFilterName() 
);
+               }
+
+               // if preference enabled, then Wikibase edits are included by 
default and
+               // the toggle default value needs to be the inverse to hide 
them, and vice versa.
+               return $this->hasShowWikibaseEditsPrefEnabled();
+       }
+
+       /**
+        * @return bool
+        */
+       private function hasShowWikibaseEditsPrefEnabled() {
+               return (bool)$this->user->getOption( $this->getOptionName() );
+       }
+
+       /**
+        * @return bool
+        */
+       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/SpecialWatchlistQueryHandler.php 
b/client/includes/Hooks/SpecialWatchlistQueryHandler.php
deleted file mode 100644
index 2dd5270..0000000
--- a/client/includes/Hooks/SpecialWatchlistQueryHandler.php
+++ /dev/null
@@ -1,175 +0,0 @@
-<?php
-
-namespace Wikibase\Client\Hooks;
-
-use DatabaseBase;
-use FormOptions;
-use User;
-use WebRequest;
-
-/**
- * @since 0.5
- *
- * @licence GNU GPL v2+
- * @author Katie Filbert < aude.w...@gmail.com >
- */
-class SpecialWatchlistQueryHandler {
-
-       /**
-        * @var User
-        */
-       private $user;
-
-       /**
-        * @var DatabaseBase
-        */
-       private $db;
-
-       /**
-        * @var boolean
-        */
-       private $showExternalChanges;
-
-       /**
-        * @var string
-        */
-       private $rcTypeLogCondition;
-
-       /**
-        * @param User $user
-        * @param DatabaseBase $db
-        * @param boolean $showExternalChanges
-        */
-       public function __construct( User $user, DatabaseBase $db, 
$showExternalChanges ) {
-               $this->user = $user;
-               $this->db = $db;
-               $this->showExternalChanges = $showExternalChanges;
-       }
-
-       /**
-        * @param WebRequest $request
-        * @param array $conds
-        * @param FormOptions|array|null $opts MediaWiki 1.22 used an array and 
MobileFrontend still does.
-        *
-        * @return array
-        */
-       public function addWikibaseConditions( WebRequest $request, array 
$conds, $opts = null ) {
-               // do not include wikibase changes for activated enhanced 
watchlist
-               // since we do not support that format yet
-               if ( $this->shouldHideWikibaseChanges( $request, $opts ) ) {
-                       $newConds = $this->makeHideWikibaseConds( $conds );
-               } else {
-                       $newConds = $this->makeShowWikibaseConds( $conds );
-               }
-
-               return $newConds;
-       }
-
-       /**
-        * @param WebRequest $request
-        * @param FormOptions|array|null $opts MediaWiki 1.22 used an array and 
MobileFrontend still does.
-        *
-        * @return boolean
-        */
-       private function shouldHideWikibaseChanges( WebRequest $request, $opts 
= null ) {
-               if ( !$this->showExternalChanges || 
$this->isEnhancedChangesEnabled( $request ) === true ) {
-                       return true;
-               }
-
-               if ( !$opts || $opts->getValue( 'hideWikibase' ) === true ) {
-                       return true;
-               }
-
-               return false;
-       }
-
-       /**
-        * @param array $conds
-        *
-        * @return array
-        */
-       private function makeHideWikibaseConds( array $conds ) {
-               $conds[] = $this->getHideRcExternalCond();
-
-               return $conds;
-       }
-
-       /**
-        * @param array $conds
-        *
-        * @return array
-        */
-       private function makeShowWikibaseConds( array $conds ) {
-               $newConds = array();
-
-               foreach ( $conds as $key => $cond ) {
-                       if ( $this->isRcTypeLogCondition( $cond ) ) {
-                               $newConds[$key] = 
$this->makeShowLogAndWikibaseType();
-                       } else {
-                               $newConds[$key] = $cond;
-                       }
-               }
-
-               return $newConds;
-       }
-
-       /**
-        * @param string $cond
-        *
-        * @return bool
-        */
-       private function isRcTypeLogCondition( $cond ) {
-               return $cond === $this->getRcTypeLogCondition();
-       }
-
-       /**
-        * @return string
-        */
-       private function getRcTypeLogCondition() {
-               if ( !isset( $this->rcTypeLogCondition ) ) {
-                       $this->rcTypeLogCondition = 
$this->makeLatestOrTypesCond( array( RC_LOG ) );
-               }
-
-               return $this->rcTypeLogCondition;
-       }
-
-       /**
-        * @return string
-        */
-       private function makeShowLogAndWikibaseType() {
-               return $this->makeLatestOrTypesCond( array( RC_LOG, RC_EXTERNAL 
) );
-       }
-
-       /**
-        * @param array $types
-        *
-        * @return string
-        */
-       private function makeLatestOrTypesCond( array $types ) {
-               $where = array(
-                       'rc_this_oldid=page_latest',
-                       'rc_type' => $types
-               );
-
-               $cond = $this->db->makeList( $where, LIST_OR );
-
-               return $cond;
-       }
-
-       /**
-        * @return string
-        */
-       private function getHideRcExternalCond() {
-               return 'rc_type != ' . RC_EXTERNAL;
-       }
-
-       /**
-        * @param WebRequest $request
-        *
-        * @return bool
-        */
-       private function isEnhancedChangesEnabled( WebRequest $request ) {
-               return $request->getBool( 'enhanced', $this->user->getOption( 
'usenewrc' ) ) === true;
-       }
-
-}
diff --git a/client/includes/recentchanges/RecentChangesFilterOptions.php 
b/client/includes/recentchanges/RecentChangesFilterOptions.php
deleted file mode 100644
index 67eb337..0000000
--- a/client/includes/recentchanges/RecentChangesFilterOptions.php
+++ /dev/null
@@ -1,80 +0,0 @@
-<?php
-
-namespace Wikibase\Client\RecentChanges;
-
-use FormOptions;
-use Wikibase\Client\WikibaseClient;
-
-/**
- * @since 0.4
- *
- * @licence GNU GPL v2+
- * @author Katie Filbert < aude.w...@gmail.com >
- */
-class RecentChangesFilterOptions {
-
-       private $opts;
-
-       public function __construct( FormOptions $opts ) {
-               $this->opts = $opts;
-       }
-
-       /**
-        * Is Wikibase recent changes feature disabled?
-        *
-        * @return bool
-        */
-       private function isDisabled() {
-               $rcSetting = WikibaseClient::getDefaultInstance()->getSettings()
-                       ->getSetting( 'showExternalRecentChanges' );
-
-               // sanity check for the setting
-               if ( !is_bool( $rcSetting ) ) {
-                       $rcSetting = false;
-               }
-
-               return !$rcSetting;
-       }
-
-       /**
-        * Is hidewikidata filter selected?
-        *
-        * @return bool
-        */
-       private function hideWikibase() {
-               // @TODO: Remve naming inconsistency (hideWikibase <> 
hideWikidata)
-               if ( isset( $this->opts['hidewikidata'] ) && 
$this->opts['hidewikidata'] === true ) {
-                       return true;
-               }
-               return false;
-       }
-
-       /**
-        * Is the enhanced changes format used?
-        *
-        * @note this is temporary and we will support enhanced changes in the 
near future
-        *
-        * @return bool
-        */
-       private function isEnhancedChanges() {
-               // @todo evil globals, though the recent changes and watchlist 
query hooks
-               // so nor provide access to context
-               global $wgRequest, $wgUser;
-               return $wgRequest->getBool( 'enhanced', $wgUser->getOption( 
'usenewrc' ) );
-       }
-
-       /**
-        * Do we show wikibase edits in recent changes?
-        *
-        * @since 0.4
-        *
-        * @return bool
-        */
-       public function showWikibaseEdits() {
-               if ( $this->isDisabled() || $this->hideWikibase() || 
$this->isEnhancedChanges() ) {
-                       return false;
-               }
-               return true;
-       }
-
-}
diff --git 
a/client/tests/phpunit/includes/Hooks/ChangesListSpecialPageFilterHandlerTest.php
 
b/client/tests/phpunit/includes/Hooks/ChangesListSpecialPageHooksHandlerTest.php
similarity index 66%
rename from 
client/tests/phpunit/includes/Hooks/ChangesListSpecialPageFilterHandlerTest.php
rename to 
client/tests/phpunit/includes/Hooks/ChangesListSpecialPageHooksHandlerTest.php
index a5e9f2c..0b89624 100644
--- 
a/client/tests/phpunit/includes/Hooks/ChangesListSpecialPageFilterHandlerTest.php
+++ 
b/client/tests/phpunit/includes/Hooks/ChangesListSpecialPageHooksHandlerTest.php
@@ -3,29 +3,116 @@
 namespace Wikibase\Client\Tests\Hooks;
 
 use FauxRequest;
-use Wikibase\Client\Hooks\ChangesListSpecialPageFilterHandler;
+use FormOptions;
+use SpecialPageFactory;
+use Wikibase\Client\Hooks\ChangesListSpecialPageHooksHandler;
 
 /**
- * @covers Wikibase\Client\Hooks\ChangesListSpecialPageFilterHandler
+ * @covers Wikibase\Client\Hooks\ChangesListSpecialPageHooksHandler
  *
  * @group WikibaseClientHooks
  * @group WikibaseClient
  * @group Wikibase
  */
-class ChangesListSpecialPageFilterHandlerTest extends 
\PHPUnit_Framework_TestCase {
+class ChangesListSpecialPageHooksHandlerTest extends 
\PHPUnit_Framework_TestCase {
+
+       public function testOnChangesListSpecialPageFilters() {
+               $user = $this->getUser(
+                       array(
+                               array( 'usernewrc' => 0 )
+                       )
+               );
+
+               $specialPage = SpecialPageFactory::getPage( 'Recentchanges' );
+               $specialPage->getContext()->setUser( $user );
+
+               $filters = array();
+
+               
ChangesListSpecialPageHooksHandler::onChangesListSpecialPageFilters(
+                       $specialPage,
+                       $filters
+               );
+
+               $expected = array(
+                       'hidewikidata' => array(
+                       'msg' => 'wikibase-rc-hide-wikidata',
+                               'default' => true
+                       )
+               );
+
+               $this->assertSame( $expected, $filters );
+       }
+
+       public function testOnChangesListSpecialPageQuery() {
+               $tables = array( 'recentchanges' );
+               $fields = array( 'rc_id' );
+               $conds = array();
+               $query_options = array();
+               $join_conds = array();
+
+               $opts = new FormOptions();
+
+               
ChangesListSpecialPageHooksHandler::onChangesListSpecialPageQuery(
+                       'RecentChanges',
+                       $tables,
+                       $fields,
+                       $conds,
+                       $query_options,
+                       $join_conds,
+                       $opts
+               );
+
+               // this is just a sanity check
+               $this->assertInternalType( 'array', $conds );
+       }
+
+       public function testAddWikibaseConditions_wikibaseEditsDisplayed() {
+               $hookHandler = new ChangesListSpecialPageHooksHandler(
+                       $this->getRequest( array() ),
+                       $this->getUser( array( 'usenewrc' => 0, 
'wlshowwikibase' => 1 ) ),
+                       'Watchlist',
+                       true
+               );
+
+               $opts = new FormOptions();
+               $opts->add( 'wlshowwikibase', true );
+
+               $conds = array();
+               $hookHandler->addWikibaseConditions( $conds, $opts );
+
+               $this->assertEquals( array(), $conds );
+       }
+
+       public function testAddWikibaseConditions_wikibaseEditsHidden() {
+               $hookHandler = new ChangesListSpecialPageHooksHandler(
+                       $this->getRequest( array() ),
+                       $this->getUser( array( 'usenewrc' => 1 ) ),
+                       'Watchlist',
+                       true
+               );
+
+
+//             $opts = new FormOptions();
+//             $opts->add( 'wlshowwikibase', true );
+
+               $conds = array();
+               $hookHandler->addWikibaseConditions( $conds, $opts );
+
+               $this->assertEquals( array( 'rc_type != ' . RC_EXTERNAL ), 
$conds );
+       }
 
        /**
         * @dataProvider filterNotAddedWhenUsingEnhancedChangesProvider
         */
        public function testFilterNotAddedWhenUsingEnhancedChanges(
                array $requestParams,
-               array $userPreferences,
+               array $userOptions,
                $pageName,
                $message
        ) {
-               $hookHandler = new ChangesListSpecialPageFilterHandler(
+               $hookHandler = new ChangesListSpecialPageHooksHandler(
                        $this->getRequest( $requestParams ),
-                       $this->getUser( $userPreferences ),
+                       $this->getUser( $userOptions ),
                        $pageName,
                        true
                );
@@ -82,14 +169,14 @@
         */
        public function testFilter_withoutShowWikibaseEditsByDefaultPreference(
                array $requestParams,
-               array $userPreferences,
+               array $userOptions,
                $expectedFilterName,
                $expectedToggleDefault,
                $specialPageName
        ) {
-               $hookHandler = new ChangesListSpecialPageFilterHandler(
+               $hookHandler = new ChangesListSpecialPageHooksHandler(
                        $this->getRequest( $requestParams ),
-                       $this->getUser( $userPreferences ),
+                       $this->getUser( $userOptions ),
                        $specialPageName,
                        true
                );
@@ -159,14 +246,14 @@
         */
        public function testFilter_withShowWikibaseEditsByDefaultPreference(
                array $requestParams,
-               array $userPreferences,
+               array $userOptions,
                $expectedFilterName,
                $expectedToggleDefault,
                $specialPageName
        ) {
-               $hookHandler = new ChangesListSpecialPageFilterHandler(
+               $hookHandler = new ChangesListSpecialPageHooksHandler(
                        $this->getRequest( $requestParams ),
-                       $this->getUser( $userPreferences ),
+                       $this->getUser( $userOptions ),
                        $specialPageName,
                        true
                );
@@ -228,7 +315,7 @@
         * @dataProvider 
filterNotAddedWhenExternalRecentChangesDisabledProvider() {
         */
        public function testFilterNotAddedWhenExternalRecentChangesDisabled( 
$specialPageName ) {
-               $hookHandler = new ChangesListSpecialPageFilterHandler(
+               $hookHandler = new ChangesListSpecialPageHooksHandler(
                        $this->getRequest( array() ),
                        $this->getUser( array( 'usenewrc' => 0 ) ),
                        $specialPageName,
@@ -253,15 +340,15 @@
                return new FauxRequest( $requestParams );
        }
 
-       private function getUser( array $options ) {
+       private function getUser( array $userOptions ) {
                $user = $this->getMockBuilder( 'User' )
                        ->disableOriginalConstructor()
                        ->getMock();
 
                $user->expects( $this->any() )
                        ->method( 'getOption' )
-                       ->will( $this->returnCallback( function( $optionName ) 
use ( $options ) {
-                               foreach ( $options as $key => $value ) {
+                       ->will( $this->returnCallback( function( $optionName ) 
use ( $userOptions ) {
+                               foreach ( $userOptions as $key => $value ) {
                                        if ( $optionName === $key ) {
                                                return $value;
                                        }
diff --git 
a/client/tests/phpunit/includes/Hooks/SpecialWatchlistQueryHandlerTest.php 
b/client/tests/phpunit/includes/Hooks/SpecialWatchlistQueryHandlerTest.php
deleted file mode 100644
index e611f88..0000000
--- a/client/tests/phpunit/includes/Hooks/SpecialWatchlistQueryHandlerTest.php
+++ /dev/null
@@ -1,102 +0,0 @@
-<?php
-
-namespace Wikibase\Client\Tests\Hooks;
-
-use FauxRequest;
-use FormOptions;
-use User;
-use Wikibase\Client\Hooks\SpecialWatchlistQueryHandler;
-
-/**
- * @covers Wikibase\Client\Hooks\SpecialWatchlistQueryHandler
- *
- * @group WikibaseClient
- * @group HookHandler
- * @group Wikibase
- *
- * @licence GNU GPL v2+
- * @author Katie Filbert < aude.w...@gmail.com >
- */
-class SpecialWatchlistQueryHandlerTest extends \PHPUnit_Framework_TestCase {
-
-       /**
-        * @dataProvider addWikibaseConditionsProvider
-        */
-       public function testAddWikibaseConditions(
-               array $expected,
-               array $conds,
-               $showExternalChanges,
-               $enhanced,
-               $hideWikibase,
-               $message
-       ) {
-               $user = $this->getUser( $enhanced );
-
-               $database = $this->getDatabase();
-               $hookHandler = new SpecialWatchlistQueryHandler( $user, 
$database, $showExternalChanges );
-
-               $opts = new FormOptions();
-               $opts->add( 'hideWikibase', $hideWikibase );
-
-               $newConds = $hookHandler->addWikibaseConditions( new 
FauxRequest(), $conds, $opts );
-
-               $this->assertEquals( $expected, $newConds, $message );
-       }
-
-       public function addWikibaseConditionsProvider() {
-               $conds = array( "(rc_this_oldid=page_latest) OR rc_type = '3'" 
);
-
-               $expectedHideConds = array_merge( $conds, array( 'rc_type != 5' 
) );
-               $expectedShowConds = array( "(rc_this_oldid=page_latest) OR 
rc_type IN (3,5)" );
-
-               return array(
-                       array( $expectedHideConds, $conds, true, true, true, 
'enhanced, hide wikibase opt' ),
-                       array( $expectedHideConds, $conds, true, true, false, 
'enhanced, no hide wikibase opt' ),
-                       array( $expectedHideConds, $conds, true, false, true, 
'not enhanced, hide wikibase opt' ),
-                       array( $expectedShowConds, $conds, true, false, false, 
'not enhanced, show wikibase opt' ),
-                       array( $expectedHideConds, $conds, false, false, false, 
'changes integration not enabled' )
-               );
-       }
-
-       /**
-        * @param bool $enhanced
-        *
-        * @return User
-        */
-       private function getUser( $enhanced ) {
-               $user = $this->getMockBuilder( 'User' )
-                       ->disableOriginalConstructor()
-                       ->getMock();
-
-               $user->expects( $this->any() )
-                       ->method( 'getOption' )
-                       ->with( 'usenewrc' )
-                       ->will( $this->returnCallback( function() use ( 
$enhanced ) {
-                               return $enhanced;
-                       } ) );
-
-               return $user;
-       }
-
-       private function getDatabase() {
-               $database = $this->getMockBuilder( 'DatabaseMysql' )
-                       ->disableOriginalConstructor()
-                       ->getMock();
-
-               $database->expects( $this->any() )
-                       ->method( 'makeList' )
-                       ->will( $this->returnCallback( function( $conds ) {
-                               if ( array_key_exists( 'rc_type', $conds ) ) {
-                                       if ( $conds['rc_type'] === array( 3 ) ) 
{
-                                               return 
"(rc_this_oldid=page_latest) OR rc_type = '3'";
-                                       } else {
-                                               return 
'(rc_this_oldid=page_latest) OR rc_type IN (3,5)';
-                                       }
-                               }
-                               return '';
-                       } ) );
-
-               return $database;
-       }
-
-}
diff --git 
a/client/tests/phpunit/includes/recentchanges/RecentChangesFilterOptionsTest.php
 
b/client/tests/phpunit/includes/recentchanges/RecentChangesFilterOptionsTest.php
deleted file mode 100644
index e67765e..0000000
--- 
a/client/tests/phpunit/includes/recentchanges/RecentChangesFilterOptionsTest.php
+++ /dev/null
@@ -1,95 +0,0 @@
-<?php
-
-namespace Wikibase\Client\Tests\RecentChanges;
-
-use FormOptions;
-use User;
-use Wikibase\Client\RecentChanges\RecentChangesFilterOptions;
-use Wikibase\Client\WikibaseClient;
-
-/**
- * @covers Wikibase\Client\RecentChanges\RecentChangesFilterOptions
- *
- * @group WikibaseClient
- * @group Test
- * @group Wikibase
- * @group Database
- *
- * @licence GNU GPL v2+
- * @author Marius Hoch < h...@online.de >
- */
-class RecentChangesFilterOptionsTest extends \MediaWikiTestCase {
-
-       protected function setUp() {
-               parent::setUp();
-
-               $user = User::newFromName( 'RecentChangesFilterOptionsTest' );
-               $this->setMwGlobals( 'wgUser', $user );
-       }
-
-       /**
-        * @dataProvider provideShowWikibaseEdits
-        *
-        * @param bool $expected
-        * @param bool $showExternalRecentChanges
-        * @param bool $hideWikibase
-        * @param bool $useNewRc
-        */
-       public function testShowWikibaseEdits(
-               $expected,
-               $showExternalRecentChanges,
-               $hideWikibase,
-               $useNewRc
-       ) {
-               global $wgUser; // Set by setUp
-
-               $settings = WikibaseClient::getDefaultInstance()->getSettings();
-               $oldShowExternal = $settings->getSetting( 
'showExternalRecentChanges' );
-               $wgUser->setOption( 'usenewrc', $useNewRc );
-
-               $settings->setSetting( 'showExternalRecentChanges', 
$showExternalRecentChanges );
-               $opts = new FormOptions();
-               $opts->add( 'hidewikidata', $hideWikibase );
-
-               $recentChangesFilterOptions = new RecentChangesFilterOptions( 
$opts );
-               $this->assertSame( $expected, 
$recentChangesFilterOptions->showWikibaseEdits() );
-
-               $settings->setSetting( 'showExternalRecentChanges', 
$oldShowExternal );
-       }
-
-       public function provideShowWikibaseEdits() {
-               return array(
-                       'Wikibase shown' => array(
-                               'expected' => true,
-                               'showExternalRecentChanges' => true,
-                               'hideWikibase' => false,
-                               'useNewRc' => false
-                       ),
-                       'hidewikidata set' => array(
-                               'expected' => false,
-                               'showExternalRecentChanges' => true,
-                               'hideWikibase' => true,
-                               'useNewRc' => false
-                       ),
-                       'showExternalRecentChanges is false' => array(
-                               'expected' => false,
-                               'showExternalRecentChanges' => false,
-                               'hideWikibase' => false,
-                               'useNewRc' => false
-                       ),
-                       'hidewikidata set and showExternalRecentChanges are 
false' => array(
-                               'expected' => false,
-                               'showExternalRecentChanges' => false,
-                               'hideWikibase' => true,
-                               'useNewRc' => false
-                       ),
-                       'usenewrc user option true' => array(
-                               'expected' => false,
-                               'showExternalRecentChanges' => true,
-                               'hideWikibase' => false,
-                               'useNewRc' => true
-                       ),
-               );
-       }
-
-}

-- 
To view, visit https://gerrit.wikimedia.org/r/254681
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib660a016c075c51660b18cea777a4431537ecb69
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

Reply via email to