From 5c480c9babcd20c8701fc5e3f651a1e9082d4e8f Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 23 Mar 2011 18:52:01 +0100 Subject: format getPopularTags and add docblock --- src/SemanticScuttle/Service/Bookmark2Tag.php | 75 +++++++++++++++++++++------- 1 file changed, 56 insertions(+), 19 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 4d2c969..e3997dd 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -477,35 +477,70 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); } - // $users can be {NULL, an id, an array of id} - function &getPopularTags($user = NULL, $limit = 30, $logged_on_user = NULL, $days = NULL) { + /** + * The the most popular tags and their usage count + * + * @param mixed $user Integer user ID or array of user IDs to limit tag + * finding to + * @param integer $limit Number of tags to return + * @param integer $logged_on_user ID of the user that's currently logged in. + * If the logged in user equals the $user to find + * tags for, tags of private bookmarks are + * returned. + * @param integer $days Bookmarks have to be changed in the last X days + * if their tags shall count* + * + * @return array Array of found tags. Each tag entry is an array with two keys, + * 'tag' (tag name) and 'bCount'. + */ + public function getPopularTags( + $user = null, $limit = 30, $logged_on_user = null, $days = null + ) { // Only count the tags that are visible to the current user. - if (($user != $logged_on_user) || is_null($user) || ($user === false)) - $privacy = ' AND B.bStatus = 0'; - else - $privacy = ''; + if (($user != $logged_on_user) || is_null($user) || ($user === false)) { + $privacy = ' AND B.bStatus = 0'; + } else { + $privacy = ''; + } - if (is_null($days) || !is_int($days)) - $span = ''; - else - $span = ' AND B.bDatetime > "'. date('Y-m-d H:i:s', time() - (86400 * $days)) .'"'; + if (is_null($days) || !is_int($days)) { + $span = ''; + } else { + $span = ' AND B.bDatetime > "' + . date('Y-m-d H:i:s', time() - (86400 * $days)) + . '"'; + } + + $query = 'SELECT' + . ' T.tag, COUNT(T.bId) AS bCount' + . ' FROM ' + . $this->getTableName() . ' AS T' + . ', ' . $GLOBALS['tableprefix'] . 'bookmarks AS B' + . ' WHERE'; - $query = 'SELECT T.tag, COUNT(T.bId) AS bCount FROM '. $this->getTableName() .' AS T, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE '; if (is_null($user) || ($user === false)) { - $query .= 'B.bId = T.bId AND B.bStatus = 0'; - } elseif(is_array($user)) { + $query .= ' B.bId = T.bId AND B.bStatus = 0'; + } else if (is_array($user)) { $query .= ' (1 = 0'; //tricks - foreach($user as $u) { - $query .= ' OR B.uId = '. $this->db->sql_escape($u) .' AND B.bId = T.bId'; + foreach ($user as $u) { + $query .= ' OR B.uId = ' . $this->db->sql_escape($u) + . ' AND B.bId = T.bId'; } $query .= ' )'; } else { - $query .= 'B.uId = '. $this->db->sql_escape($user) .' AND B.bId = T.bId'. $privacy; + $query .= ' B.uId = ' . $this->db->sql_escape($user) + . ' AND B.bId = T.bId' . $privacy; } - $query .= $span .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag'; - if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) { - message_die(GENERAL_ERROR, 'Could not get popular tags', '', __LINE__, __FILE__, $query, $this->db); + $query .= $span . ' AND LEFT(T.tag, 7) <> "system:"' + . ' GROUP BY T.tag' + . ' ORDER BY bCount DESC, tag'; + + if (!($dbresult = $this->db->sql_query_limit($query, $limit))) { + message_die( + GENERAL_ERROR, 'Could not get popular tags', + '', __LINE__, __FILE__, $query, $this->db + ); return false; } @@ -514,6 +549,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $output; } + + function hasTag($bookmarkid, $tag) { $query = 'SELECT COUNT(*) AS tCount FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND tag ="'. $this->db->sql_escape($tag) .'"'; -- cgit v1.2.3-54-g00ecf From cb28cd3371e215646765f6d1d06263a54a5526f1 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 23 Mar 2011 19:11:38 +0100 Subject: unittest for getAdminTags as well as CS and docblock fixes on it --- src/SemanticScuttle/Service/Bookmark2Tag.php | 30 +++++++++++++++++++++++++--- tests/Bookmark2TagTest.php | 20 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index e3997dd..d367b62 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -454,15 +454,37 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $output; } - function &getAdminTags($limit = 30, $logged_on_user = NULL, $days = NULL) { + + + /** + * Returns the tags used by admin users + * + * @param integer $limit Number of tags to return + * @param integer $logged_on_user ID of the user that's currently logged in. + * If the logged in user equals the $user to find + * tags for, tags of private bookmarks are + * returned. + * @param integer $days Bookmarks have to be changed in the last X days + * if their tags shall count* + * + * @return array Array of found tags. Each tag entry is an array with two keys, + * 'tag' (tag name) and 'bCount'. + * + * @see getPopularTags() + */ + public function getAdminTags( + $limit = 30, $logged_on_user = null, $days = null + ) { // look for admin ids - $userservice = SemanticScuttle_Service_Factory :: get('User'); - $adminIds = $userservice->getAdminIds(); + $userservice = SemanticScuttle_Service_Factory::get('User'); + $adminIds = $userservice->getAdminIds(); // ask for their tags return $this->getPopularTags($adminIds, $limit, $logged_on_user, $days); } + + function &getContactTags($user, $limit = 30, $logged_on_user = NULL, $days = NULL) { // look for contact ids $userservice = SemanticScuttle_Service_Factory :: get('User'); @@ -477,6 +499,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); } + + /** * The the most popular tags and their usage count * diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index a83a826..d85cf73 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -56,6 +56,7 @@ class Bookmark2TagTest extends TestBase protected function setUp() { $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->us->deleteAll(); $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); $this->bs->deleteAll(); $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag'); @@ -447,6 +448,25 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'two', 'bCount' => '1'), $arTags); $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags); } + + + public function testGetAdminTags() + { + $admin1 = $this->addUser('admin1'); + $admin2 = $this->addUser('admin2'); + $user1 = $this->addUser(); + $this->addBookmark($admin1, null, 0, array('admintag', 'admintag1')); + $this->addBookmark($admin2, null, 0, array('admintag', 'admintag2')); + $this->addBookmark($user1, null, 0, array('usertag')); + + $GLOBALS['admin_users'] = array('admin1', 'admin2'); + + $arTags = $this->b2ts->getAdminTags(4); + $this->assertEquals(3, count($arTags)); + $this->assertContains(array('tag' => 'admintag', 'bCount' => '2'), $arTags); + $this->assertContains(array('tag' => 'admintag1', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'admintag2', 'bCount' => '1'), $arTags); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { -- cgit v1.2.3-54-g00ecf From 637e276ac7dc0e9acdbb79ed68415c1770ce7a3f Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 08:36:54 +0100 Subject: unittest for Bookmark2Tag::getContactTags, CS and docblock --- src/SemanticScuttle/Service/Bookmark2Tag.php | 31 ++++++++++++-- tests/Bookmark2TagTest.php | 62 +++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 10 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index d367b62..478b47e 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -485,13 +485,33 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService - function &getContactTags($user, $limit = 30, $logged_on_user = NULL, $days = NULL) { + + /** + * Returns the tags used by users that are part of the user's watchlist, + * and the current user's own tags. + * + * @param integer $user ID of the user to get the watchlist from + * @param integer $limit Number of tags to return + * @param integer $logged_on_user ID of the user that's currently logged in. + * If set, that user is added to the list of + * people to get the tags from + * @param integer $days Bookmarks have to be changed in the last X days + * if their tags shall count* + * + * @return array Array of found tags. Each tag entry is an array with two keys, + * 'tag' (tag name) and 'bCount'. + * + * @see getPopularTags() + */ + public function getContactTags( + $user, $limit = 30, $logged_on_user = null, $days = null + ) { // look for contact ids - $userservice = SemanticScuttle_Service_Factory :: get('User'); + $userservice = SemanticScuttle_Service_Factory::get('User'); $contacts = $userservice->getWatchlist($user); - // add the user (to show him/her also his/her tags) - if(!is_null($logged_on_user)) { + // add the user (to show him also his own tags) + if (!is_null($logged_on_user)) { $contacts[] = $logged_on_user; } @@ -516,6 +536,9 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. + * + * @see getAdminTags() + * @see getContactTags() */ public function getPopularTags( $user = null, $limit = 30, $logged_on_user = null, $days = null diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index d85cf73..ad64bf6 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -74,7 +74,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmark() when the bookmark has no tags * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkNone() { @@ -92,7 +92,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmark() when the bookmark has one tag * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkOne() { @@ -109,9 +109,9 @@ class Bookmark2TagTest extends TestBase /** - * Test getTagsForBookmark() when the bookmark has thr tags + * Test getTagsForBookmark() when the bookmark has three tags * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkThr() { @@ -132,7 +132,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmarks() when no bookmarks have tags. * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks */ public function testGetTagsForBookmarksNone() { @@ -155,7 +155,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmarks() when most bookmarks have tags. * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks */ public function testGetTagsForBookmarksMost() { @@ -450,6 +450,9 @@ class Bookmark2TagTest extends TestBase } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getAdminTags + */ public function testGetAdminTags() { $admin1 = $this->addUser('admin1'); @@ -467,6 +470,53 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'admintag1', 'bCount' => '1'), $arTags); $this->assertContains(array('tag' => 'admintag2', 'bCount' => '1'), $arTags); } + + + + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags + */ + public function testGetContactTagsWatchlistOnly() + { + $user1 = $this->addUser(); + $user2 = $this->addUser(); + $user3 = $this->addUser(); + $this->us->setCurrentUserId($user1); + $this->us->setWatchStatus($user2); + //user1 watches user2 now + + $this->addBookmark($user1, null, 0, array('usertag', 'usertag1')); + $this->addBookmark($user2, null, 0, array('usertag', 'usertag2')); + $this->addBookmark($user3, null, 0, array('usertag', 'usertag3')); + + $arTags = $this->b2ts->getContactTags($user1, 10); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags); + } + + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags + */ + public function testGetContactTagsIncludingUser() + { + $user1 = $this->addUser(); + $user2 = $this->addUser(); + $user3 = $this->addUser(); + $this->us->setCurrentUserId($user1); + $this->us->setWatchStatus($user2); + //user1 watches user2 now + + $this->addBookmark($user1, null, 0, array('usertag', 'usertag1')); + $this->addBookmark($user2, null, 0, array('usertag', 'usertag2')); + $this->addBookmark($user3, null, 0, array('usertag', 'usertag3')); + + $arTags = $this->b2ts->getContactTags($user1, 10, $user1); + $this->assertEquals(3, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '2'), $arTags); + $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { -- cgit v1.2.3-54-g00ecf From 0f481a99ea1dea8ec993c37f0613a48f97463e6b Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 08:45:03 +0100 Subject: fix docblock --- src/SemanticScuttle/Service/Bookmark2Tag.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 478b47e..83a8aed 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -465,7 +465,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * tags for, tags of private bookmarks are * returned. * @param integer $days Bookmarks have to be changed in the last X days - * if their tags shall count* + * if their tags shall count * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -496,7 +496,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * If set, that user is added to the list of * people to get the tags from * @param integer $days Bookmarks have to be changed in the last X days - * if their tags shall count* + * if their tags shall count * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -532,7 +532,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * tags for, tags of private bookmarks are * returned. * @param integer $days Bookmarks have to be changed in the last X days - * if their tags shall count* + * if their tags shall count * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. -- cgit v1.2.3-54-g00ecf From 3f57237b88688f956add02bc18cd4642082fb526 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 08:50:18 +0100 Subject: make getPopularTags a tiny bit easier --- src/SemanticScuttle/Service/Bookmark2Tag.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 83a8aed..b69cfd4 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -550,14 +550,6 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService $privacy = ''; } - if (is_null($days) || !is_int($days)) { - $span = ''; - } else { - $span = ' AND B.bDatetime > "' - . date('Y-m-d H:i:s', time() - (86400 * $days)) - . '"'; - } - $query = 'SELECT' . ' T.tag, COUNT(T.bId) AS bCount' . ' FROM ' @@ -579,7 +571,13 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService . ' AND B.bId = T.bId' . $privacy; } - $query .= $span . ' AND LEFT(T.tag, 7) <> "system:"' + if (is_int($days)) { + $query .= ' AND B.bDatetime > "' + . date('Y-m-d H:i:s', time() - (86400 * $days)) + . '"'; + } + + $query .= ' AND LEFT(T.tag, 7) <> "system:"' . ' GROUP BY T.tag' . ' ORDER BY bCount DESC, tag'; -- cgit v1.2.3-54-g00ecf From 30f84b3ca3242f00926f30c08ece4405be8b61fc Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 19:07:50 +0100 Subject: fix privacy protection issue when fetching tags of several users --- src/SemanticScuttle/Service/Bookmark2Tag.php | 2 +- tests/Bookmark2TagTest.php | 69 ++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 21 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index b69cfd4..21ea0d5 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -565,7 +565,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService $query .= ' OR B.uId = ' . $this->db->sql_escape($u) . ' AND B.bId = T.bId'; } - $query .= ' )'; + $query .= ' )' . $privacy; } else { $query .= ' B.uId = ' . $this->db->sql_escape($user) . ' AND B.bId = T.bId' . $privacy; diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index ad64bf6..e2020dc 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -37,6 +37,26 @@ class Bookmark2TagTest extends TestBase protected $tts; + /** + * Create a bookmark. Like addBookmark(), just with other paramter order + * to make some tests in that class easier to write. + * + * @param integer $user User ID the bookmark shall belong + * @param array $tags Array of tags to attach. If "null" is given, + * it will automatically be "unittest" + * @param string $date strtotime-compatible string + * @param string $title Bookmark title + * + * @return integer ID of bookmark + */ + protected function addTagBookmark($user, $tags, $date = null, $title = null) + { + return $this->addBookmark( + $user, null, 0, $tags, $title, $date + ); + } + + /** * Used to run this test class standalone @@ -207,25 +227,6 @@ class Bookmark2TagTest extends TestBase } - /** - * Create a bookmark - * - * @param integer $user User ID the bookmark shall belong - * @param array $tags Array of tags to attach. If "null" is given, - * it will automatically be "unittest" - * @param string $date strtotime-compatible string - * @param string $title Bookmark title - * - * @return integer ID of bookmark - */ - protected function addTagBookmark($user, $tags, $date = null, $title = null) - { - return $this->addBookmark( - $user, null, 0, $tags, $title, $date - ); - } - - /** * Fetch the most popular tags in descending order @@ -398,10 +399,12 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags); } + + /** * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags */ - public function testGetPopularTagsPublicOnly() + public function testGetPopularTagsPublicOnlyNoUser() { $user1 = $this->addUser(); $this->addBookmark($user1, null, 0, array('one')); @@ -416,7 +419,13 @@ class Bookmark2TagTest extends TestBase ), $arTags ); + } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsPublicOnlySingleUser() + { $user1 = $this->addUser(); $this->addBookmark($user1, null, 0, array('one')); $this->addBookmark($user1, null, 1, array('one', 'two')); @@ -432,6 +441,26 @@ class Bookmark2TagTest extends TestBase ); } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsPublicOnlySeveralUsers() + { + $user1 = $this->addUser(); + $user2 = $this->addUser(); + $this->addBookmark($user1, null, 0, array('one')); + $this->addBookmark($user1, null, 1, array('one', 'two')); + $this->addBookmark($user1, null, 2, array('thr')); + $this->addBookmark($user2, null, 0, array('fou')); + $this->addBookmark($user2, null, 1, array('fiv')); + $this->addBookmark($user2, null, 2, array('six')); + + $arTags = $this->b2ts->getPopularTags(array($user1, $user2)); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'one', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'fou', 'bCount' => '1'), $arTags); + } + /** * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags */ -- cgit v1.2.3-54-g00ecf From d761abb05e28ef4f345acef739e3b371b462f9fe Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 19:11:06 +0100 Subject: docblock for deleteAll --- src/SemanticScuttle/Service/Bookmark2Tag.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 21ea0d5..beb4185 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -674,7 +674,15 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $output; } - function deleteAll() { + + + /** + * Deletes all tags in bookmarks2tags + * + * @return void + */ + public function deleteAll() + { $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; $this->db->sql_query($query); } -- cgit v1.2.3-54-g00ecf From e667feb0ca9ff30a063149a2ce20b3398585dd4f Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 25 Mar 2011 07:44:07 +0100 Subject: beginsWith-parameter for getPopulartags, getContactTags and getAdminTags --- src/SemanticScuttle/Service/Bookmark2Tag.php | 25 ++++++++--- tests/Bookmark2TagTest.php | 64 ++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index beb4185..1dc0ffe 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -466,6 +466,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * returned. * @param integer $days Bookmarks have to be changed in the last X days * if their tags shall count + * @param string $beginsWith The tag name shall begin with that string * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -473,14 +474,16 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * @see getPopularTags() */ public function getAdminTags( - $limit = 30, $logged_on_user = null, $days = null + $limit = 30, $logged_on_user = null, $days = null, $beginsWith = null ) { // look for admin ids $userservice = SemanticScuttle_Service_Factory::get('User'); $adminIds = $userservice->getAdminIds(); // ask for their tags - return $this->getPopularTags($adminIds, $limit, $logged_on_user, $days); + return $this->getPopularTags( + $adminIds, $limit, $logged_on_user, $days, $beginsWith + ); } @@ -497,6 +500,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * people to get the tags from * @param integer $days Bookmarks have to be changed in the last X days * if their tags shall count + * @param string $beginsWith The tag name shall begin with that string * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -504,7 +508,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * @see getPopularTags() */ public function getContactTags( - $user, $limit = 30, $logged_on_user = null, $days = null + $user, $limit = 30, $logged_on_user = null, $days = null, + $beginsWith = null ) { // look for contact ids $userservice = SemanticScuttle_Service_Factory::get('User'); @@ -516,7 +521,9 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService } // ask for their tags - return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); + return $this->getPopularTags( + $contacts, $limit, $logged_on_user, $days, $beginsWith + ); } @@ -533,6 +540,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * returned. * @param integer $days Bookmarks have to be changed in the last X days * if their tags shall count + * @param string $beginsWith The tag name shall begin with that string * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -541,7 +549,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * @see getContactTags() */ public function getPopularTags( - $user = null, $limit = 30, $logged_on_user = null, $days = null + $user = null, $limit = 30, $logged_on_user = null, $days = null, + $beginsWith = null ) { // Only count the tags that are visible to the current user. if (($user != $logged_on_user) || is_null($user) || ($user === false)) { @@ -577,6 +586,12 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService . '"'; } + if (!is_null($beginsWith)) { + $query .= ' AND T.tag LIKE \'' + . $this->db->sql_escape($beginsWith) + . '%\''; + } + $query .= ' AND LEFT(T.tag, 7) <> "system:"' . ' GROUP BY T.tag' . ' ORDER BY bCount DESC, tag'; diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index e2020dc..ffd83c3 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -334,6 +334,31 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'thr', 'bCount' => '2'), $arTags); } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsBeginsWith() + { + $user = $this->addUser(); + $this->addTagBookmark($user, array('one', 'two')); + $this->addTagBookmark($user, array('one', 'thr')); + $this->addTagBookmark($user, array('one', 'two')); + $this->addTagBookmark($user, array('one', 'thr')); + + $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 'o'); + $this->assertEquals(1, count($arTags)); + $this->assertContains(array('tag' => 'one', 'bCount' => '4'), $arTags); + + $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 'tw'); + $this->assertEquals(1, count($arTags)); + $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags); + + $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 't'); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags); + $this->assertContains(array('tag' => 'thr', 'bCount' => '2'), $arTags); + } + /** @@ -500,6 +525,23 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'admintag2', 'bCount' => '1'), $arTags); } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getAdminTags + */ + public function testGetAdminTagsBeginsWith() + { + $admin1 = $this->addUser('admin1'); + $this->addBookmark($admin1, null, 0, array('admintag', 'admintag1')); + $this->addBookmark($admin1, null, 0, array('tester', 'testos')); + + $GLOBALS['admin_users'] = array('admin1'); + + $arTags = $this->b2ts->getAdminTags(4, null, null, 'test'); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'tester', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'testos', 'bCount' => '1'), $arTags); + } + /** @@ -546,6 +588,28 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags); } + + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags + */ + public function testGetContactTagsBeginsWith() + { + $user1 = $this->addUser(); + $this->addBookmark($user1, null, 0, array('usertag', 'usertag1')); + $this->addBookmark($user1, null, 0, array('usable', 'undefined')); + $this->addBookmark($user1, null, 0, array('fußbad', 'usable')); + + $arTags = $this->b2ts->getContactTags($user1, 10, $user1, null, 'user'); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); + + $arTags = $this->b2ts->getContactTags($user1, 10, $user1, null, 'us'); + $this->assertEquals(3, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usable', 'bCount' => '2'), $arTags); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { -- cgit v1.2.3-54-g00ecf From d6e99db40dc88de1782099b30941075ebc8dfa97 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 25 Mar 2011 08:00:32 +0100 Subject: do not generate invalid SQL when called with a not-so valid array --- src/SemanticScuttle/Service/Bookmark2Tag.php | 6 ++++-- tests/Bookmark2TagTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'src/SemanticScuttle/Service/Bookmark2Tag.php') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 1dc0ffe..a10cb61 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -571,8 +571,10 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService } else if (is_array($user)) { $query .= ' (1 = 0'; //tricks foreach ($user as $u) { - $query .= ' OR B.uId = ' . $this->db->sql_escape($u) - . ' AND B.bId = T.bId'; + if (is_numeric($u)) { + $query .= ' OR B.uId = ' . $this->db->sql_escape($u) + . ' AND B.bId = T.bId'; + } } $query .= ' )' . $privacy; } else { diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index ffd83c3..fff4222 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -426,6 +426,23 @@ class Bookmark2TagTest extends TestBase + /** + * This may happen when the method is called with a problematic user array. + * In that case we may not generate invalid SQL or so. + * + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsUserArrayWithNull() + { + $user1 = $this->addUser(); + $this->addTagBookmark($user1, array('one')); + + $arTags = $this->b2ts->getPopularTags(array(null)); + $this->assertEquals(0, count($arTags)); + } + + + /** * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags */ -- cgit v1.2.3-54-g00ecf