From cf3cd572b0410e6db7255e3373c8ec8230c4af5c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 21 May 2014 11:11:47 +0200 Subject: [PATCH 1/6] Add a method to get the values for multiple users to OC\Preferences --- lib/private/preferences.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/private/preferences.php b/lib/private/preferences.php index e6d9f28b1d..c0000804aa 100644 --- a/lib/private/preferences.php +++ b/lib/private/preferences.php @@ -205,6 +205,41 @@ class Preferences { } } + /** + * Gets the preference for an array of users + * @param string $app + * @param string $key + * @param array $users + * @return array Mapped values: userid => value + */ + public function getValueForUsers($app, $key, $users) { + if (empty($users) || !is_array($users)) return array(); + + $chunked_users = array_chunk($users, 50, true); + $placeholders_50 = implode(',', array_fill(0, 50, '?')); + + $userValues = array(); + foreach ($chunked_users as $chunk) { + $queryParams = $chunk; + array_unshift($queryParams, $key); + array_unshift($queryParams, $app); + + $placeholders = (sizeof($chunk) == 50) ? $placeholders_50 : implode(',', array_fill(0, sizeof($users), '?')); + + $query = 'SELECT `userid`, `configvalue` ' + . ' FROM `*PREFIX*preferences` ' + . ' WHERE `appid` = ? AND `configkey` = ?' + . ' AND `userid` IN (' . $placeholders . ')'; + $result = $this->conn->executeQuery($query, $queryParams); + + while ($row = $result->fetch()) { + $userValues[$row['userid']] = $row['configvalue']; + } + } + + return $userValues; + } + /** * Deletes a key * @param string $user user From 457a4aa4255799836bcd2138509323d567114d1b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 21 May 2014 11:34:26 +0200 Subject: [PATCH 2/6] Add test case for getValueForUsers() --- tests/lib/preferences.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/lib/preferences.php b/tests/lib/preferences.php index f1f6ed0800..2f15f47ea1 100644 --- a/tests/lib/preferences.php +++ b/tests/lib/preferences.php @@ -184,6 +184,25 @@ class Test_Preferences_Object extends PHPUnit_Framework_TestCase { $preferences->setValue('grg', 'bar', 'foo', 'v2'); } + public function testGetUserValues() + { + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*preferences` VALUES(?, ?, ?, ?)'); + $query->execute(array('SomeUser', 'testGetUserValues', 'somekey', 'somevalue')); + $query->execute(array('AnotherUser', 'testGetUserValues', 'somekey', 'someothervalue')); + $query->execute(array('AUser', 'testGetUserValues', 'somekey', 'somevalue')); + + $preferences = new OC\Preferences(\OC_DB::getConnection()); + $values = $preferences->getValueForUsers('testGetUserValues', 'somekey', array('SomeUser', 'AnotherUser', 'NoValueSet')); + + $this->assertEquals(2, sizeof($values)); + + $this->assertArrayHasKey('SomeUser', $values); + $this->assertEquals('somevalue', $values['SomeUser']); + + $this->assertArrayHasKey('AnotherUser', $values); + $this->assertEquals('someothervalue', $values['AnotherUser']); + } + public function testDeleteKey() { $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); From 4fbfe077fb611bea2581556c52c068a3163c73b3 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 21 May 2014 13:10:23 +0200 Subject: [PATCH 3/6] Move return to new line --- lib/private/preferences.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/private/preferences.php b/lib/private/preferences.php index c0000804aa..62ef908655 100644 --- a/lib/private/preferences.php +++ b/lib/private/preferences.php @@ -213,7 +213,9 @@ class Preferences { * @return array Mapped values: userid => value */ public function getValueForUsers($app, $key, $users) { - if (empty($users) || !is_array($users)) return array(); + if (empty($users) || !is_array($users)) { + return array(); + } $chunked_users = array_chunk($users, 50, true); $placeholders_50 = implode(',', array_fill(0, 50, '?')); From 66f94273c65b6e6b4b41927337467bd3c0aaacc0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 21 May 2014 13:17:29 +0200 Subject: [PATCH 4/6] Add a lot of users for testing --- tests/lib/preferences.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/lib/preferences.php b/tests/lib/preferences.php index 2f15f47ea1..96228fcca1 100644 --- a/tests/lib/preferences.php +++ b/tests/lib/preferences.php @@ -192,8 +192,22 @@ class Test_Preferences_Object extends PHPUnit_Framework_TestCase { $query->execute(array('AUser', 'testGetUserValues', 'somekey', 'somevalue')); $preferences = new OC\Preferences(\OC_DB::getConnection()); - $values = $preferences->getValueForUsers('testGetUserValues', 'somekey', array('SomeUser', 'AnotherUser', 'NoValueSet')); + $users = array('SomeUser', 'AnotherUser', 'NoValueSet'); + $values = $preferences->getValueForUsers('testGetUserValues', 'somekey', $users); + $this->assertUserValues($values); + + // Add a lot of users so the array is chunked + for ($i = 1; $i <= 75; $i++) { + array_unshift($users, 'NoValueBefore#' . $i); + array_push($users, 'NoValueAfter#' . $i); + } + + $values = $preferences->getValueForUsers('testGetUserValues', 'somekey', $users); + $this->assertUserValues($values); + } + + protected function assertUserValues($values) { $this->assertEquals(2, sizeof($values)); $this->assertArrayHasKey('SomeUser', $values); From e20fb37038ab78c67701cae0086b78b409364147 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 21 May 2014 14:35:59 +0200 Subject: [PATCH 5/6] Correctly create list of placeholders --- lib/private/preferences.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/preferences.php b/lib/private/preferences.php index 62ef908655..a4bfc650d0 100644 --- a/lib/private/preferences.php +++ b/lib/private/preferences.php @@ -226,7 +226,7 @@ class Preferences { array_unshift($queryParams, $key); array_unshift($queryParams, $app); - $placeholders = (sizeof($chunk) == 50) ? $placeholders_50 : implode(',', array_fill(0, sizeof($users), '?')); + $placeholders = (sizeof($chunk) == 50) ? $placeholders_50 : implode(',', array_fill(0, sizeof($chunk), '?')); $query = 'SELECT `userid`, `configvalue` ' . ' FROM `*PREFIX*preferences` ' From d621a6fe5e571f3a273d0db301e3d5e14ed10763 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 21 May 2014 16:00:21 +0200 Subject: [PATCH 6/6] Clean the database after the test --- tests/lib/preferences.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/lib/preferences.php b/tests/lib/preferences.php index 96228fcca1..499be914fb 100644 --- a/tests/lib/preferences.php +++ b/tests/lib/preferences.php @@ -205,6 +205,10 @@ class Test_Preferences_Object extends PHPUnit_Framework_TestCase { $values = $preferences->getValueForUsers('testGetUserValues', 'somekey', $users); $this->assertUserValues($values); + + // Clean DB after the test + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*preferences` WHERE `appid` = ?'); + $query->execute(array('testGetUserValues')); } protected function assertUserValues($values) {