Aude has uploaded a new change for review.
https://gerrit.wikimedia.org/r/119767
Change subject: Fix watchlist hook query in the client
......................................................................
Fix watchlist hook query in the client
In fixing this, a new hook handler is added and more tests.
The problem was that core was changed to add quotes around
the RC type values, and then wikibase was not matching the
where condition in a consistent way.
Here, we use makeList() which adds quotes, and compare the
where conditions, to modify the rc_type condition to include
Wikibase "external" changes if watchlist is to show them.
We exclude external rc_type if watchlist is to hide them.
Further improvement in future patches could change
the code to make more use of FormOptions and share code
with recent changes options handling. (see bug 62798)
Bug: 62149
Change-Id: I672bedc673736d36a29161cfe116b5df021ff5b8
(cherry picked from commit b10dbab9fa0cfb86c254288ae6b8148d4db7f238)
---
M client/WikibaseClient.hooks.php
A client/includes/hooks/SpecialWatchlistQueryHandler.php
A client/tests/phpunit/includes/hooks/SpecialWatchlistQueryHandlerTest.php
3 files changed, 245 insertions(+), 29 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/67/119767/1
diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index db4b34a..21cd3a5 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -28,6 +28,7 @@
use UnexpectedValueException;
use User;
use Wikibase\Client\Hooks\InfoActionHookHandler;
+use Wikibase\Client\Hooks\SpecialWatchlistQueryHandler;
use Wikibase\Client\MovePageNotice;
use Wikibase\Client\Hooks\OtherProjectsSidebarGenerator;
use Wikibase\Client\WikibaseClient;
@@ -255,7 +256,9 @@
wfProfileIn( __METHOD__ );
- if ( $rc->getAttribute( 'rc_type' ) == RC_EXTERNAL ) {
+ $type = $rc->getAttribute( 'rc_type' );
+
+ if ( $type == RC_EXTERNAL ) {
$wikibaseClient = WikibaseClient::getDefaultInstance();
$changeFactory = new ExternalChangeFactory(
$wikibaseClient->getSettings()->getSetting(
'repoSiteId' )
@@ -309,36 +312,11 @@
public static function onSpecialWatchlistQuery( array &$conds, array
&$tables,
array &$join_conds, array &$fields, $opts = array()
) {
- global $wgRequest, $wgUser;
+ $db = wfGetDB( DB_SLAVE );
+ $handler = new SpecialWatchlistQueryHandler(
$GLOBALS['wgUser'], $db );
- wfProfileIn( __METHOD__ );
+ $conds = $handler->addWikibaseConditions(
$GLOBALS['wgRequest'], $conds, $opts );
- if (
- // Don't act on activated enhanced watchlist
- $wgRequest->getBool( 'enhanced', $wgUser->getOption(
'usenewrc' ) ) === false &&
- // Or in case the user disabled it
- $opts['hideWikibase'] === 0
- ) {
- $dbr = wfGetDB( DB_SLAVE );
-
- $newConds = array();
- foreach( $conds as $k => $v ) {
- if ( $v === 'rc_this_oldid=page_latest OR
rc_type=3' ) {
- $where = array(
- 'rc_this_oldid=page_latest',
- 'rc_type' => array( 3, 5 )
- );
- $newConds[$k] = $dbr->makeList( $where,
LIST_OR );
- } else {
- $newConds[$k] = $v;
- }
- }
- $conds = $newConds;
- } else {
- $conds[] = 'rc_type != 5';
- }
-
- wfProfileOut( __METHOD__ );
return true;
}
diff --git a/client/includes/hooks/SpecialWatchlistQueryHandler.php
b/client/includes/hooks/SpecialWatchlistQueryHandler.php
new file mode 100644
index 0000000..745ea83
--- /dev/null
+++ b/client/includes/hooks/SpecialWatchlistQueryHandler.php
@@ -0,0 +1,147 @@
+<?php
+
+namespace Wikibase\Client\Hooks;
+
+use DatabaseBase;
+use User;
+use WebRequest;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ */
+class SpecialWatchlistQueryHandler {
+
+ /**
+ * @var User
+ */
+ private $user;
+
+ /**
+ * @var DatabaseBase
+ */
+ private $db;
+
+ /**
+ * @var string
+ */
+ private $rcTypeLogCondition;
+
+ /**
+ * @param User $user
+ * @param DatabaseBase $db
+ */
+ public function __construct( User $user, DatabaseBase $db ) {
+ $this->user = $user;
+ $this->db = $db;
+ }
+
+ /**
+ * @param WebRequest $request
+ * @param array $conds
+ * @param FormOptions $opts
+ *
+ * @return array
+ */
+ public function addWikibaseConditions( WebRequest $request, array
$conds, $opts ) {
+ $hideWikibase = $opts->getValue( 'hideWikibase');
+
+ // do not include wikibase changes for activated enhanced
watchlist
+ // since we do not support that format yet
+ if ( $this->isEnhancedChangesEnabled( $request ) === true ||
$hideWikibase === true ) {
+ $newConds = $this->makeHideWikibaseConds( $conds );
+ } else {
+ $newConds = $this->makeShowWikibaseConds( $conds );
+ }
+
+ return $newConds;
+ }
+
+ /**
+ * @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;
+ }
+
+ /**
+ * @return boolean
+ */
+ 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
+ */
+ private function isEnhancedChangesEnabled( WebRequest $request ) {
+ return $request->getBool( 'enhanced', $this->user->getOption(
'usenewrc' ) ) === true;
+ }
+
+}
diff --git
a/client/tests/phpunit/includes/hooks/SpecialWatchlistQueryHandlerTest.php
b/client/tests/phpunit/includes/hooks/SpecialWatchlistQueryHandlerTest.php
new file mode 100644
index 0000000..49a4a86
--- /dev/null
+++ b/client/tests/phpunit/includes/hooks/SpecialWatchlistQueryHandlerTest.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Wikibase\Test;
+
+use FauxRequest;
+use FormOptions;
+use Wikibase\Client\Hooks\SpecialWatchlistQueryHandler;
+
+/**
+ * @covers Wikibase\Client\Hooks\SpecialWatchlistQueryHandler
+ *
+ * @group WikibaseClient
+ * @group HookHandler
+ * @group Wikibase
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ */
+class SpecialWatchlistQueryHandlerTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider addWikibaseConditionsProvider
+ */
+ public function testAddWikibaseConditions( $expected, $conds,
$enhanced, $hideWikibase,
+ $message
+ ) {
+ $user = $this->getUser( $enhanced );
+
+ $database = $this->getDatabase();
+ $hookHandler = new SpecialWatchlistQueryHandler( $user,
$database );
+
+ $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,
'enhanced, hide wikibase opt' ),
+ array( $expectedHideConds, $conds, true, false,
'enhanced, no hide wikibase opt' ),
+ array( $expectedHideConds, $conds, false, true, 'not
enhanced, hide wikibase opt' ),
+ array( $expectedShowConds, $conds, false, false, 'not
enhanced, show wikibase opt' )
+ );
+ }
+
+ /**
+ * @param boolean $enhanced
+ */
+ 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 $database;
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/119767
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I672bedc673736d36a29161cfe116b5df021ff5b8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: mw1.23-wmf19
Gerrit-Owner: Aude <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits