Compare commits

...

31 Commits

Author SHA1 Message Date
Christian Weiske
a14192b372 re-fix isAdmin() sql optimization 2010-02-20 11:07:35 +01:00
Christian Weiske
1fd4d4d9fc sql optimization: reduce by 9 sql queries when logged in and looking at bookmarks 2010-02-20 10:57:52 +01:00
Christian Weiske
10c6863b9e add SemanticScuttle_Service_Bookmark::bookmarksExist() method to check for existance of multiple bookmarks at once for future sql optimization 2010-02-20 10:50:29 +01:00
Christian Weiske
f968870c83 reformat query 2010-02-20 09:54:46 +01:00
Christian Weiske
9a2f4df186 fix tests after last commit 2010-02-20 09:53:39 +01:00
Christian Weiske
e851f195ec save another query 2010-02-20 09:39:37 +01:00
Christian Weiske
686e9269ac save another query 2010-02-20 09:35:04 +01:00
Christian Weiske
e904ae29bc reformat _getuser and add docblock 2010-02-20 09:20:51 +01:00
Christian Weiske
41a57d2c81 header docblock 2010-02-20 09:06:01 +01:00
Christian Weiske
65a24bf243 docblock 2010-02-20 09:04:50 +01:00
Christian Weiske
3b3d427483 remove end date condition since it seems unneccessary to me 2010-02-20 08:55:21 +01:00
Christian Weiske
8bb4c7d318 new config option to disable "SET NAMES UTF8" sql call 2010-02-18 11:10:03 +01:00
Christian Weiske
4408598f07 sql optimization: 9 queries less! 2010-02-18 10:51:01 +01:00
Christian Weiske
254449cb9c make isAdmin accept a user name, too 2010-02-18 10:48:57 +01:00
Christian Weiske
f0e57da5f8 sql optimization: fetch all tags at once 2010-02-18 00:06:20 +01:00
Christian Weiske
55839da10a add new method for better sql performance: Bookmark2Tag::getTagsForBookmarks() 2010-02-17 23:55:21 +01:00
Christian Weiske
08b88d9586 reformat and CS 2010-02-17 23:42:35 +01:00
Christian Weiske
486cf8b172 make tests better 2010-02-17 23:42:24 +01:00
Christian Weiske
179e40c36b add bookmark2tagtest to alltests 2010-02-17 19:20:50 +01:00
Christian Weiske
da245bee56 add test for Bookmark2Tag::getTagsForBookmark() 2010-02-17 19:17:35 +01:00
Christian Weiske
a951a40f98 test getBookmarks() with tag loading functionality 2010-02-17 19:06:58 +01:00
Christian Weiske
57cafb6d79 first sql optimization: fetch countOthers() for all bookmarks at once instead of each single 2010-02-17 08:30:58 +01:00
Christian Weiske
aca27618d0 replace some strange foreach constructs with the proper variant 2010-02-17 08:14:39 +01:00
Christian Weiske
414818318c make countOthers() accept an array of addresses 2010-02-17 08:02:25 +01:00
Christian Weiske
094564a9a9 reformat countOthers() 2010-02-16 22:47:16 +01:00
Christian Weiske
35496e95ce test complex combination of watches, publics and private bookmarks with countOthers() 2010-02-16 22:41:37 +01:00
Christian Weiske
38f9812f37 allow setting of status during addBookmark in tests 2010-02-16 22:41:04 +01:00
Christian Weiske
d4b7d32c77 multiple tests for SemanticScuttle_Service_Bookmark::countOthers() 2010-02-16 22:12:59 +01:00
Christian Weiske
0160d2847b allow adding of certain addresses 2010-02-16 22:08:19 +01:00
Christian Weiske
152bfa736b do not return numbers < 0 2010-02-16 22:07:44 +01:00
Christian Weiske
b89b4acbf5 do not return numbers < 0 2010-02-16 22:07:31 +01:00
14 changed files with 952 additions and 70 deletions

View File

@ -180,6 +180,15 @@ $dbname = 'scuttle';
*/
$tableprefix = 'sc_';
/*
* If the database needs to be switched to UTF8
* manually or not. If true, a "SET NAMES UTF8" query
* will be sent at the beginning. If you need performance,
* save this query and set it in your mysql server options.
*
* @var boolean
*/
$dbneedssetnames = true;
/***************************************************

View File

@ -216,9 +216,19 @@ if($currenttag!= '') {
<ol <?php echo ($start > 0 ? ' start="'. ++$start .'"' : ''); ?>
id="bookmarks">
<?php
foreach(array_keys($bookmarks) as $key) {
$row =& $bookmarks[$key];
<?php
$addresses = array();
foreach ($bookmarks as $key => &$row) {
$addresses[$row['bId']] = $row['bAddress'];
}
$otherCounts = $bookmarkservice->countOthers($addresses);
if ($userservice->isLoggedOn()) {
$existence = $bookmarkservice->bookmarksExist(
$addresses, $currentUser->getId()
);
}
foreach ($bookmarks as $key => &$row) {
switch ($row['bStatus']) {
case 0:
$access = '';
@ -234,9 +244,7 @@ if($currenttag!= '') {
$cats = '';
$tagsForCopy = '';
$tags = $row['tags'];
foreach(array_keys($tags) as $key) {
$tag =& $tags[$key];
foreach ($tags as $tkey => &$tag) {
$cats .= '<a href="'. sprintf($cat_url, filter($row['username'], 'url'), filter($tag, 'url')) .'" rel="tag">'. filter($tag) .'</a>, ';
$tagsForCopy.= $tag.',';
}
@ -264,7 +272,7 @@ if($currenttag!= '') {
// Udders!
if (!isset($hash)) {
$others = $bookmarkservice->countOthers($row['bAddress']);
$others = $otherCounts[$row['bAddress']];
$ostart = '<a href="'. createURL('history', $row['bHash']) .'">';
$oend = '</a>';
switch ($others) {
@ -281,7 +289,7 @@ if($currenttag!= '') {
// Copy link
if ($userservice->isLoggedOn()
&& ($currentUser->getId() != $row['uId'])
&& !$bookmarkservice->bookmarkExists($row['bAddress'], $currentUser->getId())
&& !$existence[$row['bAddress']]
) {
$copy .= ' - <a href="'
. createURL('bookmarks', $currentUser->getUsername() .'?action=add&amp;copyOf='. $row['bId'])
@ -304,7 +312,7 @@ if($currenttag!= '') {
}
// Admin specific design
if($userservice->isAdmin($row['uId'])) {
if ($userservice->isAdmin($row['username'])) {
$adminBgClass = 'class="adminBackground"';
$adminStar = ' <img src="'. ROOT .'images/logo_24.gif" width="12px" title="'. T_('This bookmark is certified by an admin user.') .'" />';
} else {

View File

@ -1,10 +1,17 @@
ChangeLog for SemantiScuttle
============================
0.97.0 - 2010-?????
-------------------
- Many SQL optimizations
- New config option to skip "SET NAMES UTF8" call: $dbneedssetnames
0.96.1 - 2010-02-09
-------------------
- Fix bug #2948410: API is broken in 0.96.0
0.96.0 - 2010-02-08
-------------------
- Fix bug #2843523: ArtViper thumbnail license change

View File

@ -158,7 +158,7 @@ class SemanticScuttle_Model_User
// Look for value only if not already set
if(!isset($this->isAdmin)) {
$us = SemanticScuttle_Service_Factory::get('User');
$this->isAdmin = $us->isAdmin($this->id);
$this->isAdmin = $us->isAdmin($this->username);
}
return $this->isAdmin;
}

View File

@ -284,15 +284,18 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
}
$userservice = SemanticScuttle_Service_Factory::get('User');
$user = $userservice->getCurrentUser();
$user = $userservice->getCurrentObjectUser();
if ($user === null) {
return false;
}
//user has to be either admin, or owner
if ($GLOBALS['adminsCanModifyBookmarksFromOtherUsers']
&& $userservice->isAdmin($user)
&& $userservice->isAdmin($user->username)
) {
return true;
} else {
return ($bookmark['uId'] == $user['uId']);
return ($bookmark['uId'] == $user->id);
}
}
@ -309,7 +312,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
* @return boolean True when the bookmark with the given URL
* exists for the user, false if not.
*/
function bookmarkExists($address = false, $uid = null)
public function bookmarkExists($address = false, $uid = null)
{
if (!$address) {
return false;
@ -322,7 +325,9 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
$crit['uId'] = $uid;
}
$sql = 'SELECT COUNT(*) as "0" FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $this->db->sql_build_array('SELECT', $crit);
$sql = 'SELECT COUNT(*) as "0" FROM '
. $GLOBALS['tableprefix'] . 'bookmarks'
. ' WHERE '. $this->db->sql_build_array('SELECT', $crit);
if (!($dbresult = $this->db->sql_query($sql))) {
message_die(
@ -341,6 +346,60 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
/**
* Checks if the given addresses exist
*
* @param array $addresses Array of addresses
* @param integer $uid User ID the addresses shall belong to
*
* @return array Array with addresses as keys, true/false for existence
* as value
*/
public function bookmarksExist($addresses, $uid = null)
{
if (count($addresses) == 0) {
return array();
}
$hashes = array();
$sql = '(1';
foreach ($addresses as $key => $address) {
$hash = md5($this->normalize($address));
$hashes[$hash] = $address;
$sql .= ' OR bHash = "'
. $this->db->sql_escape($hash)
. '"';
}
$sql .= ')';
if ($uid !== null) {
$sql .= ' AND uId = ' . intval($uid);
}
$sql = 'SELECT bHash, COUNT(*) as "count" FROM '
. $this->getTableName()
. ' WHERE ' . $sql
. ' GROUP BY bHash';
if (!($dbresult = $this->db->sql_query($sql))) {
message_die(
GENERAL_ERROR, 'Could not get bookmark counts', '',
__LINE__, __FILE__, $sql, $this->db
);
}
$existence = array_combine(
$addresses,
array_fill(0, count($addresses), false)
);
while ($row = $this->db->sql_fetchrow($dbresult)) {
$existence[$hashes[$row['bHash']]] = $row['count'] > 0;
}
return $existence;
}
/**
* Adds a bookmark to the database.
*
@ -804,10 +863,17 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
$total = $row['total'];
$this->db->sql_freeresult($totalresult);
$bookmarks = array();
while ($row = & $this->db->sql_fetchrow($dbresult)) {
$row['tags'] = $b2tservice->getTagsForBookmark(intval($row['bId']));
$bookmarks[] = $row;
$bookmarks = array();
$bookmarkids = array();
while ($row = $this->db->sql_fetchrow($dbresult)) {
$bookmarks[] = $row;
$bookmarkids[] = $row['bId'];
}
if (count($bookmarkids)) {
$tags = $b2tservice->getTagsForBookmarks($bookmarkids);
foreach ($bookmarks as &$bookmark) {
$bookmark['tags'] = $tags[$bookmark['bId']];
}
}
$this->db->sql_freeresult($dbresult);
@ -889,36 +955,88 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
function countOthers($address)
/**
* Counts the number of bookmarks that have the same address
* as the given address.
*
* @internal
* We do support fetching counts for multiple addresses at once
* because that allows us to reduce the number of queries
* we need in the web interface when displaying i.e.
* 10 bookmarks - only one SQL query is needed then.
*
* @param string|array $addresses Address/URL to look for, string
* of one address or array with
* multiple ones
*
* @return integer Number of bookmarks minus one that have the address.
* In case $addresses was an array, key-value array
* with key being the address, value said number of
* bookmarks
*/
public function countOthers($addresses)
{
if (!$address) {
if (!$addresses) {
return false;
}
$bArray = is_array($addresses);
$userservice = SemanticScuttle_Service_Factory :: get('User');
$sId = $userservice->getCurrentUserId();
$us = SemanticScuttle_Service_Factory::get('User');
$sId = (int)$us->getCurrentUserId();
if ($userservice->isLoggedOn()) {
// All public bookmarks, user's own bookmarks and any shared with user
$privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
$watchnames = $userservice->getWatchNames($sId, true);
foreach($watchnames as $watchuser) {
$privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
if ($us->isLoggedOn()) {
//All public bookmarks, user's own bookmarks
// and any shared with our user
$privacy = ' AND ((B.bStatus = 0) OR (B.uId = ' . $sId . ')';
$watchnames = $us->getWatchNames($sId, true);
foreach ($watchnames as $watchuser) {
$privacy .= ' OR (U.username = "'
. $this->db->sql_escape($watchuser)
. '" AND B.bStatus = 1)';
}
$privacy .= ')';
} else {
// Just public bookmarks
//Just public bookmarks
$privacy = ' AND B.bStatus = 0';
}
$sql = 'SELECT COUNT(*) FROM '. $userservice->getTableName() .' AS U, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE U.'. $userservice->getFieldName('primary') .' = B.uId AND B.bHash = "'. md5($address) .'"'. $privacy;
if (!($dbresult = & $this->db->sql_query($sql))) {
message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
$addressesSql = ' AND (0';
foreach ((array)$addresses as $address) {
$addressesSql .= ' OR B.bHash = "'
. $this->db->sql_escape(md5($address))
. '"';
}
$addressesSql .= ')';
$sql = 'SELECT B.bAddress, COUNT(*) as count FROM '
. $us->getTableName() . ' AS U'
. ', '. $GLOBALS['tableprefix'] . 'bookmarks AS B'
. ' WHERE U.'. $us->getFieldName('primary') .' = B.uId'
. $addressesSql
. $privacy
. ' GROUP BY B.bHash';
if (!($dbresult = $this->db->sql_query($sql))) {
message_die(
GENERAL_ERROR, 'Could not get other count',
'', __LINE__, __FILE__, $sql, $this->db
);
}
$output = $this->db->sql_fetchfield(0, 0) - 1;
//be sure we also list urls in our array
// that are not found in the database
$counts = array_combine(
(array)$addresses,
array_fill(0, count((array)$addresses), 0)
);
while ($row = $this->db->sql_fetchrow($dbresult)) {
$counts[$row['bAddress']]
= $row['count'] > 0 ? $row['count'] - 1 : 0;
}
$this->db->sql_freeresult($dbresult);
return $output;
return $bArray ? $counts : reset($counts);
}

View File

@ -266,27 +266,95 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService
return true;
}
function &getTagsForBookmark($bookmarkid) {
/**
* Retrieves all tags for a given bookmark except system tags.
*
* @param integer $bookmarkid ID of the bookmark
*
* @return array Array of tags
*/
public function getTagsForBookmark($bookmarkid)
{
if (!is_numeric($bookmarkid)) {
message_die(GENERAL_ERROR, 'Could not get tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query);
message_die(
GENERAL_ERROR, 'Could not get tags (invalid bookmarkid)',
'', __LINE__, __FILE__, $query
);
return false;
}
$query = 'SELECT tag FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND LEFT(tag, 7) <> "system:" ORDER BY id ASC';
$query = 'SELECT tag FROM ' . $this->getTableName()
. ' WHERE bId = ' . intval($bookmarkid)
. ' AND LEFT(tag, 7) <> "system:"'
. ' ORDER BY id ASC';
if (!($dbresult =& $this->db->sql_query($query))) {
message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db);
if (!($dbresult = $this->db->sql_query($query))) {
message_die(
GENERAL_ERROR, 'Could not get tags',
'', __LINE__, __FILE__, $query, $this->db
);
return false;
}
$tags = array();
while ($row =& $this->db->sql_fetchrow($dbresult)) {
while ($row = $this->db->sql_fetchrow($dbresult)) {
$tags[] = $row['tag'];
}
$this->db->sql_freeresult($dbresult);
return $tags;
}
/**
* Retrieves all tags for an array of bookmark IDs
*
* @param array $bookmarkids Array of bookmark IDs
*
* @return array Array of tag arrays. Key is bookmark ID.
*/
public function getTagsForBookmarks($bookmarkids)
{
if (!is_array($bookmarkids)) {
message_die(
GENERAL_ERROR, 'Could not get tags (invalid bookmarkids)',
'', __LINE__, __FILE__, $query
);
return false;
} else if (count($bookmarkids) == 0) {
return array();
}
$sql = '';
foreach ($bookmarkids as $bookmarkid) {
$sql .= ' OR bId = ' . intval($bookmarkid);
}
$query = 'SELECT tag, bId FROM ' . $this->getTableName()
. ' WHERE (1' . $sql . ')'
. ' AND LEFT(tag, 7) <> "system:"'
. ' ORDER BY id, bId ASC';
if (!($dbresult = $this->db->sql_query($query))) {
message_die(
GENERAL_ERROR, 'Could not get tags',
'', __LINE__, __FILE__, $query, $this->db
);
return false;
}
$tags = array_combine(
$bookmarkids,
array_fill(0, count($bookmarkids), array())
);
while ($row = $this->db->sql_fetchrow($dbresult)) {
$tags[$row['bId']][] = $row['tag'];
}
$this->db->sql_freeresult($dbresult);
return $tags;
}
function &getTags($userid = NULL) {
$userservice =SemanticScuttle_Service_Factory::get('User');
$logged_on_user = $userservice->getCurrentUserId();

View File

@ -113,7 +113,7 @@ class SemanticScuttle_Service_Factory
protected static function loadDb()
{
global $dbhost, $dbuser, $dbpass, $dbname,
$dbport, $dbpersist, $dbtype;
$dbport, $dbpersist, $dbtype, $dbneedssetnames;
if (self::$db !== null) {
return;
@ -130,7 +130,9 @@ class SemanticScuttle_Service_Factory
self::$db
);
}
$db->sql_query('SET NAMES UTF8');
$dbneedssetnames && $db->sql_query('SET NAMES UTF8');
self::$db = $db;
}

View File

@ -76,15 +76,28 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
$this->updateSessionStability();
}
function _getuser($fieldname, $value) {
$query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"';
/**
* Fetches the desired user row from database, specified by column and value
*
* @param string $fieldname Name of database column to identify user
* @param string $value Value of $fieldname
*
* @return array Database row or boolean false
*/
protected function _getuser($fieldname, $value)
{
$query = 'SELECT * FROM '. $this->getTableName()
. ' WHERE ' . $fieldname . ' = "' . $this->db->sql_escape($value) . '"';
if (! ($dbresult =& $this->db->sql_query($query)) ) {
message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
if (!($dbresult = $this->db->sql_query($query)) ) {
message_die(
GENERAL_ERROR, 'Could not get user',
'', __LINE__, __FILE__, $query, $this->db
);
return false;
}
$row =& $this->db->sql_fetchrow($dbresult);
$row = $this->db->sql_fetchrow($dbresult);
$this->db->sql_freeresult($dbresult);
if ($row) {
return $row;
@ -305,9 +318,14 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
/**
* Checks if the given user is an administrator.
* Uses global admin_users property containing admin
* user names
* user names.
*
* @param integer|array $user User ID or user row from DB
* Passing the user id makes this function load the user
* from database. For efficiency reasons, try to pass
* the user name or database row.
*
* @param integer|array|string $user User ID or user row from DB
* or user name
*
* @return boolean True if the user is admin
*/
@ -315,10 +333,13 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
{
if (is_numeric($user)) {
$user = $this->getUser($user);
$user = $user['username'];
} else if (is_array($user)) {
$user = $user['username'];
}
if (isset($GLOBALS['admin_users'])
&& in_array($user['username'], $GLOBALS['admin_users'])
&& in_array($user, $GLOBALS['admin_users'])
) {
return true;
} else {
@ -386,6 +407,7 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
}
//reload user object
$this->getCurrentUser(true);
$this->getCurrentObjectUser(true);
}

View File

@ -1,6 +1,19 @@
<?php
/* Define functions used into the application */
/**
* Defines some commonly used functions.
*
* SemanticScuttle - your social bookmark manager.
*
* PHP version 5.
*
* @category Bookmarking
* @package SemanticScuttle
* @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
* @author Christian Weiske <cweiske@cweiske.de>
* @author Eric Dane <ericdane@users.sourceforge.net>
* @license GPL http://www.gnu.org/licenses/gpl.html
* @link http://sourceforge.net/projects/semanticscuttle
*/
// Converts tags:
// - direction = out: convert spaces to underscores;
@ -35,10 +48,23 @@ function filter($data, $type = NULL) {
return $data;
}
function getPerPageCount($userObject = null) {
/**
* Returns the number of bookmarks that shall be displayed on one page.
*
* @param SemanticScuttle_Model_User $userObject Object of the current user
*
* @return integer Number of bookmarks per page
*
* @uses $defaultPerPage
* @uses $defaultPerPageForAdmins
*/
function getPerPageCount($userObject = null)
{
global $defaultPerPage, $defaultPerPageForAdmins;
if(isset($defaultPerPageForAdmins) && $userObject != null && $userObject->isAdmin()) {
if (isset($defaultPerPageForAdmins)
&& $userObject != null && $userObject->isAdmin()
) {
return $defaultPerPageForAdmins;
} else {
return $defaultPerPage;

View File

@ -56,6 +56,7 @@ class AllTests extends PHPUnit_Framework_TestSuite
$suite = new AllTests();
$tdir = dirname(__FILE__);
$suite->addTestFile($tdir . '/BookmarkTest.php');
$suite->addTestFile($tdir . '/Bookmark2TagTest.php');
$suite->addTestFile($tdir . '/Tag2TagTest.php');
$suite->addTestFile($tdir . '/TagsCacheTest.php');
$suite->addTestFile($tdir . '/CommonDescriptionTest.php');

207
tests/Bookmark2TagTest.php Normal file
View File

@ -0,0 +1,207 @@
<?php
/**
* SemanticScuttle - your social bookmark manager.
*
* PHP version 5.
*
* @category Bookmarking
* @package SemanticScuttle
* @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
* @author Christian Weiske <cweiske@cweiske.de>
* @author Eric Dane <ericdane@users.sourceforge.net>
* @license GPL http://www.gnu.org/licenses/gpl.html
* @link http://sourceforge.net/projects/semanticscuttle
*/
require_once 'prepare.php';
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Bookmark2TagTest::main');
}
/**
* Unit tests for the SemanticScuttle bookmark-tag combination service.
*
* @category Bookmarking
* @package SemanticScuttle
* @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
* @author Christian Weiske <cweiske@cweiske.de>
* @author Eric Dane <ericdane@users.sourceforge.net>
* @license GPL http://www.gnu.org/licenses/gpl.html
* @link http://sourceforge.net/projects/semanticscuttle
*/
class Bookmark2TagTest extends TestBase
{
protected $us;
protected $bs;
protected $ts;
protected $tts;
/**
* Used to run this test class standalone
*
* @return void
*/
public static function main()
{
require_once 'PHPUnit/TextUI/TestRunner.php';
PHPUnit_TextUI_TestRunner::run(
new PHPUnit_Framework_TestSuite(__CLASS__)
);
}
protected function setUp()
{
$this->us = SemanticScuttle_Service_Factory::get('User');
$this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
$this->bs->deleteAll();
$this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag');
$this->b2ts->deleteAll();
$this->tts = SemanticScuttle_Service_Factory::get('Tag2Tag');
$this->tts->deleteAll();
$this->tsts = SemanticScuttle_Service_Factory::get('TagStat');
$this->tsts->deleteAll();
$this->vs = SemanticScuttle_Service_Factory::get('Vote');
$this->vs->deleteAll();
}
/**
* Test getTagsForBookmark() when the bookmark has no tags
*
* @return void
*/
public function testGetTagsForBookmarkNone()
{
$this->addBookmark(null, null, 0, array('forz', 'barz'));
$bid = $this->addBookmark(null, null, 0, array());
$this->assertEquals(
array(),
$this->b2ts->getTagsForBookmark($bid)
);
}
/**
* Test getTagsForBookmark() when the bookmark has one tag
*
* @return void
*/
public function testGetTagsForBookmarkOne()
{
$this->addBookmark(null, null, 0, array('forz', 'barz'));
$bid = $this->addBookmark(null, null, 0, array());
$this->b2ts->attachTags($bid, array('foo'));
$this->assertEquals(
array('foo'),
$this->b2ts->getTagsForBookmark($bid)
);
}
/**
* Test getTagsForBookmark() when the bookmark has three tags
*
* @return void
*/
public function testGetTagsForBookmarkThree()
{
$this->addBookmark(null, null, 0, array('forz', 'barz'));
$bid = $this->addBookmark(null, null, 0, array());
$this->b2ts->attachTags($bid, array('foo', 'bar', 'fuu'));
$tags = $this->b2ts->getTagsForBookmark($bid);
$this->assertType('array', $tags);
$this->assertContains('foo', $tags);
$this->assertContains('bar', $tags);
$this->assertContains('fuu', $tags);
}
/**
* Test getTagsForBookmarks() when no bookmarks have tags.
*
* @return void
*/
public function testGetTagsForBookmarksNone()
{
$bid1 = $this->addBookmark(null, null, 0, array());
$bid2 = $this->addBookmark(null, null, 0, array());
$alltags = $this->b2ts->getTagsForBookmarks(
array($bid1, $bid2)
);
$this->assertType('array', $alltags);
$this->assertEquals(2, count($alltags));
$this->assertType('array', $alltags[$bid1]);
$this->assertType('array', $alltags[$bid2]);
$this->assertEquals(0, count($alltags[$bid1]));
$this->assertEquals(0, count($alltags[$bid2]));
}
/**
* Test getTagsForBookmarks() when most bookmarks have tags.
*
* @return void
*/
public function testGetTagsForBookmarksMost()
{
$bid1 = $this->addBookmark(null, null, 0, array());
$this->b2ts->attachTags($bid1, array('foo', 'bar', 'fuu'));
$bid2 = $this->addBookmark(null, null, 0, array());
$this->b2ts->attachTags($bid2, array('foo', 'bar2', 'fuu2'));
$bid3 = $this->addBookmark(null, null, 0, array());
$this->b2ts->attachTags($bid3, array('foo', 'bar2', 'fuu3'));
$bid4 = $this->addBookmark(null, null, 0, array());
//no tags
$alltags = $this->b2ts->getTagsForBookmarks(
array($bid1, $bid2, $bid3, $bid4)
);
$this->assertType('array', $alltags);
foreach ($alltags as $bid => $btags) {
$this->assertType('array', $btags);
if ($bid == $bid1) {
$this->assertEquals(3, count($btags));
$this->assertContains('foo', $btags);
$this->assertContains('bar', $btags);
$this->assertContains('fuu', $btags);
} else if ($bid == $bid2) {
$this->assertEquals(3, count($btags));
$this->assertContains('foo', $btags);
$this->assertContains('bar2', $btags);
$this->assertContains('fuu2', $btags);
} else if ($bid == $bid3) {
$this->assertEquals(3, count($btags));
$this->assertContains('foo', $btags);
$this->assertContains('bar2', $btags);
$this->assertContains('fuu3', $btags);
} else if ($bid == $bid4) {
$this->assertEquals(0, count($btags));
} else {
$this->assertTrue(false, 'Unknown bookmark id');
}
}
}
}
if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') {
Bookmark2TagTest::main();
}
?>

View File

@ -252,6 +252,128 @@ class BookmarkTest extends TestBase
/**
* Tests if bookmarksExist() returns true when a bookmark
* exists
*
* @return void
*/
public function testBookmarksExistTrueSingle()
{
$bid = $this->addBookmark();
$bookmark = $this->bs->getBookmark($bid);
$ret = $this->bs->bookmarksExist(array($bookmark['bAddress']));
$this->assertType('array', $ret);
$this->assertEquals(1, count($ret));
$this->assertTrue($ret[$bookmark['bAddress']]);
}
/**
* Tests if bookmarksExist() returns true when all bookmarks
* exist
*
* @return void
*/
public function testBookmarksExistTrueMultiple()
{
$bid = $this->addBookmark();
$bookmark = $this->bs->getBookmark($bid);
$bid2 = $this->addBookmark();
$bookmark2 = $this->bs->getBookmark($bid2);
$ret = $this->bs->bookmarksExist(
array(
$bookmark['bAddress'],
$bookmark2['bAddress']
)
);
$this->assertType('array', $ret);
$this->assertEquals(2, count($ret));
$this->assertTrue($ret[$bookmark['bAddress']]);
$this->assertTrue($ret[$bookmark2['bAddress']]);
}
/**
* Tests if bookmarksExist() returns false when a bookmark
* does not exist
*
* @return void
*/
public function testBookmarksExistFalseSingle()
{
$ret = $this->bs->bookmarksExist(array('does-not-exist'));
$this->assertType('array', $ret);
$this->assertEquals(1, count($ret));
$this->assertFalse($ret['does-not-exist']);
}
/**
* Tests if bookmarksExist() returns false when all bookmarks
* do not exist
*
* @return void
*/
public function testBookmarksExistFalseMultiple()
{
$bms = array(
'does-not-exist',
'does-not-exist-2',
'does-not-exist-3',
);
$ret = $this->bs->bookmarksExist($bms);
$this->assertType('array', $ret);
$this->assertEquals(3, count($ret));
$this->assertFalse($ret['does-not-exist']);
$this->assertFalse($ret['does-not-exist-2']);
$this->assertFalse($ret['does-not-exist-3']);
}
/**
* Tests if bookmarksExist() returns true when some bookmarks
* exist.
*
* @return void
*/
public function testBookmarksExistSome()
{
$bid = $this->addBookmark();
$bookmark = $this->bs->getBookmark($bid);
$bid2 = $this->addBookmark();
$bookmark2 = $this->bs->getBookmark($bid2);
$ret = $this->bs->bookmarksExist(
array(
$bookmark['bAddress'],
'does-not-exist',
$bookmark2['bAddress'],
'does-not-exist-2',
'does-not-exist-3'
)
);
$this->assertType('array', $ret);
$this->assertEquals(5, count($ret));
$this->assertTrue($ret[$bookmark['bAddress']]);
$this->assertTrue($ret[$bookmark2['bAddress']]);
$this->assertFalse($ret['does-not-exist']);
$this->assertFalse($ret['does-not-exist-2']);
$this->assertFalse($ret['does-not-exist-3']);
}
/**
* Test if countBookmarks() works with no bookmarks
*
@ -331,6 +453,40 @@ class BookmarkTest extends TestBase
/**
* Check tag loading functionality of getBookmarks()
*
* @return void
*/
public function testGetBookmarksIncludeTags()
{
$uid = $this->addUser();
$bid = $this->addBookmark($uid);
$this->b2ts->attachTags($bid, array('foo', 'bar'));
$bid2 = $this->addBookmark($uid);
$this->b2ts->attachTags($bid2, array('fuu', 'baz'));
$bms = $this->bs->getBookmarks();
$this->assertEquals(2, count($bms['bookmarks']));
$this->assertEquals(2, $bms['total']);
foreach ($bms['bookmarks'] as $bm) {
$this->assertArrayHasKey('tags', $bm);
$this->assertType('array', $bm['tags']);
if ($bm['bId'] == $bid) {
$this->assertContains('foo', $bm['tags']);
$this->assertContains('bar', $bm['tags']);
} else if ($bm['bId'] == $bid2) {
$this->assertContains('fuu', $bm['tags']);
$this->assertContains('baz', $bm['tags']);
} else {
$this->assertTrue(false, 'Unknown bookmark id');
}
}
}
/**
* Test if deleting a bookmark works.
*
@ -778,6 +934,251 @@ class BookmarkTest extends TestBase
$this->assertEquals('newShortNambb', $bm['bShort']);
}
/**
* Test what countOther() returns when the address does not exist
*
* @return void
*/
public function testCountOthersAddressDoesNotExist()
{
$this->assertEquals(0, $this->bs->countOthers('http://example.org'));
}
/**
* Test what countOther() returns when nobody else has the same bookmark
*
* @return void
*/
public function testCountOthersNone()
{
$uid = $this->addUser();
$address = 'http://example.org';
$this->addBookmark($uid, $address);
$this->assertEquals(0, $this->bs->countOthers($address));
}
/**
* Test what countOther() returns when the address exists only once
* and multiple bookmarks are in the database.
*
* @return void
*/
public function testCountOthersMultipleNone()
{
$uid = $this->addUser();
$address = 'http://example.org';
$this->addBookmark($uid, $address);
$this->addBookmark($uid);
$this->addBookmark($uid);
$this->assertEquals(0, $this->bs->countOthers($address));
}
/**
* Test what countOther() returns when the address exists only once
* and the same user and other users have other bookmarks
*
* @return void
*/
public function testCountOthersMultipleUsersNone()
{
$uid = $this->addUser();
$uid2 = $this->addUser();
$address = 'http://example.org';
$this->addBookmark($uid, $address);
$this->addBookmark($uid);
$this->addBookmark($uid2);
$this->assertEquals(0, $this->bs->countOthers($address));
}
/**
* Test what countOther() returns when the address exists two
* times in the database.
*
* @return void
*/
public function testCountOthersOne()
{
$uid = $this->addUser();
$uid2 = $this->addUser();
$address = 'http://example.org';
$this->addBookmark($uid, $address);
$this->addBookmark($uid2, $address);
$this->assertEquals(1, $this->bs->countOthers($address));
}
/**
* Test what countOther() returns when the address exists four
* times in the database.
*
* @return void
*/
public function testCountOthersThree()
{
$uid = $this->addUser();
$address = 'http://example.org';
$this->addBookmark($uid, $address);
$this->addBookmark(null, $address);
$this->addBookmark(null, $address);
$this->addBookmark(null, $address);
$this->assertEquals(3, $this->bs->countOthers($address));
}
/**
* Test what countOther() returns when the user is logged in
* and friends (people on the watchlist) have bookmarked
* and shared the same address.
*
* @return void
*/
public function testCountOthersWatchlist()
{
$uid = $this->addUser();
$address = 'http://example.org';
//log user in
$this->us->setCurrentUserId($uid);
//setup users
$otherPublic1 = $this->addUser();
$otherPublic2 = $this->addUser();
$otherShared1 = $this->addUser();
$otherPrivate1 = $this->addUser();
$friendPublic1 = $this->addUser();
$friendShared1 = $this->addUser();
$friendShared2 = $this->addUser();
$friendPrivate1 = $this->addUser();
$friendSharing1 = $this->addUser();
//setup watchlists
$us = SemanticScuttle_Service_Factory::get('User');
$this->us->setCurrentUserId($friendPublic1);
$us->setWatchStatus($uid);
$this->us->setCurrentUserId($friendShared1);
$us->setWatchStatus($uid);
$this->us->setCurrentUserId($friendShared2);
$us->setWatchStatus($uid);
$this->us->setCurrentUserId($friendPrivate1);
$us->setWatchStatus($uid);
//back to login of main user
$this->us->setCurrentUserId($uid);
$us->setWatchStatus($friendSharing1);
//add bookmarks
$this->addBookmark($uid, $address, 0);
$this->addBookmark($otherPublic1, $address, 0);
$this->addBookmark($otherPublic2, $address, 0);
$this->addBookmark($otherShared1, $address, 1);
$this->addBookmark($otherPrivate1, $address, 2);
$this->addBookmark($friendPublic1, $address, 0);
$this->addBookmark($friendShared1, $address, 1);
$this->addBookmark($friendShared2, $address, 1);
$this->addBookmark($friendPrivate1, $address, 2);
//this user is on our watchlist, but we not on his
$this->addBookmark($friendSharing1, $address, 1);
//2 public
//1 public (friend)
//2 shared
//-> 5
$this->assertEquals(5, $this->bs->countOthers($address));
}
/**
* Test what countOther() returns when multiple addresses are
* passed to it and none of them exists.
*
* @return void
*/
public function testCountOthersArrayNone()
{
$this->assertEquals(
array('1' => 0, '2' => 0, '3' => 0),
$this->bs->countOthers(array('1', '2', '3'))
);
}
/**
* Test what countOther() returns when multiple addresses are
* passed to it and only one of them exists.
*
* @return void
*/
public function testCountOthersArrayOneNone()
{
$uid = $this->addUser();
$uid2 = $this->addUser();
$address1 = 'http://example.org/1';
$address2 = 'http://example.org/2';
$this->addBookmark($uid, $address1);
$this->addBookmark($uid, $address2);
$this->addBookmark($uid2, $address1);
$this->assertEquals(
array(
$address1 => 1,
$address2 => 0
),
$this->bs->countOthers(
array($address1, $address2)
)
);
}
/**
* Test what countOther() returns when multiple addresses are passed
* to it and both of them exist with different numbers for each.
*
* @return void
*/
public function testCountOthersArrayTwoOne()
{
$uid = $this->addUser();
$uid2 = $this->addUser();
$uid3 = $this->addUser();
$address1 = 'http://example.org/1';
$address2 = 'http://example.org/2';
$this->addBookmark($uid, $address1);
$this->addBookmark($uid, $address2);
$this->addBookmark($uid2, $address1);
$this->addBookmark($uid2, $address2);
$this->addBookmark($uid3, $address1);
$this->assertEquals(
array(
$address1 => 2,
$address2 => 1
),
$this->bs->countOthers(
array($address1, $address2)
)
);
}
}

View File

@ -29,25 +29,41 @@ class TestBase extends PHPUnit_Framework_TestCase
/**
* Create a new bookmark.
*
* @param integer $user User ID the bookmark shall belong
* @param integer $user User ID the bookmark shall belong
* @param string $address Bookmark address to use
* @param integer $status Bookmark visibility
* @param array $tags Array of tags to attach. If "null" is given,
* it will automatically be "unittest"
*
* @return integer ID of bookmark
*
* @see SemanticScuttle_Service_Bookmark::addBookmark()
*/
protected function addBookmark($user = null)
{
protected function addBookmark(
$user = null, $address = null, $status = 0,
$tags = null
) {
if ($user === null) {
$user = $this->addUser();
}
if ($tags === null) {
$tags = array('unittest');
}
$bs = SemanticScuttle_Service_Factory::get('Bookmark');
$rand = rand();
if ($address === null) {
$address = 'http://example.org/' . $rand;
}
$bid = $bs->addBookmark(
'http://example.org/' . $rand,
$address,
'unittest bookmark #' . $rand,
'description',
null,
0,
array('unittest'),
$status,
$tags,
null, null, false, false,
$user
);

View File

@ -67,19 +67,16 @@ if (intval(GET_PAGE) > 1) {
$start = 0;
}
$dtend = date('Y-m-d H:i:s', strtotime('tomorrow'));
/*$dtstart = date('Y-m-d H:i:s', strtotime($dtend .' -'. $defaultRecentDays .' days'));*/
$tplVars['page'] = $page;
$tplVars['start'] = $start;
$tplVars['page'] = $page;
$tplVars['start'] = $start;
$tplVars['popCount'] = 30;
$tplVars['sidebar_blocks'] = $GLOBALS["index_sidebar_blocks"];
$tplVars['range'] = 'all';
$tplVars['range'] = 'all';
$tplVars['pagetitle'] = T_('Store, share and tag your favourite links');
$tplVars['subtitle'] = T_('All Bookmarks');
$tplVars['subtitle'] = T_('All Bookmarks');
$tplVars['bookmarkCount'] = $start + 1;
$bookmarks =& $bookmarkservice->getBookmarks($start, $perpage, NULL, NULL, NULL, getSortOrder(), NULL, 0, $dtend);
$bookmarks = $bookmarkservice->getBookmarks($start, $perpage, NULL, NULL, NULL, getSortOrder(), NULL, 0, NULL);
$tplVars['total'] = $bookmarks['total'];
$tplVars['bookmarks'] =& $bookmarks['bookmarks'];