X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FProfile_tag.php;h=8034e68ccf728cd73adba3c80874e41029b1e535;hb=956e053da6e7efb27f00b4d4195a832a8c298a45;hp=d9b094182f1dcb7e11b343d0a2f232e00e4fc8d6;hpb=9ca3c3d1c31ff2b30ecd7bbc2ec9ec3722173f7f;p=quix0rs-gnu-social.git diff --git a/classes/Profile_tag.php b/classes/Profile_tag.php index d9b094182f..8034e68ccf 100644 --- a/classes/Profile_tag.php +++ b/classes/Profile_tag.php @@ -2,26 +2,15 @@ /** * Table Definition for profile_tag */ -require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; class Profile_tag extends Managed_DataObject { - ###START_AUTOCODE - /* the code below is auto generated do not remove the above tag */ - public $__table = 'profile_tag'; // table name public $tagger; // int(4) primary_key not_null public $tagged; // int(4) primary_key not_null public $tag; // varchar(64) primary_key not_null public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP - /* Static get */ - function staticGet($k,$v=null) - { return Memcached_DataObject::staticGet('Profile_tag',$k,$v); } - - /* the code above is auto generated do not remove the tag below */ - ###END_AUTOCODE - public static function schemaDef() { return array( @@ -46,10 +35,6 @@ class Profile_tag extends Managed_DataObject ); } - function pkeyGet($kv) { - return Memcached_DataObject::pkeyGet('Profile_tag', $kv); - } - function links() { return array('tagger,tag' => 'profile_list:tagger,tag'); @@ -60,6 +45,16 @@ class Profile_tag extends Managed_DataObject return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag)); } + static function getSelfTagsArray(Profile $target) + { + return self::getTagsArray($target->getID(), $target->getID(), $target); + } + + static function setSelfTags(Profile $target, array $newtags, array $privacy=array()) + { + return self::setTags($target->getID(), $target->getID(), $newtags, $privacy); + } + static function getTags($tagger, $tagged, $auth_user=null) { $profile_list = new Profile_list(); @@ -79,54 +74,54 @@ class Profile_tag extends Managed_DataObject return $tags; } - $profile_tag = new Profile_tag(); - $profile_list->tagger = $tagger; - $profile_tag->tagged = $tagged; + $qry = 'select profile_list.* from profile_list left join '. + 'profile_tag on (profile_list.tag = profile_tag.tag and '. + 'profile_list.tagger = profile_tag.tagger) where '. + 'profile_tag.tagger = %d and profile_tag.tagged = %d '; + $qry = sprintf($qry, $tagger, $tagged); - $profile_list->selectAdd(); + if (!$include_priv) { + $qry .= ' and profile_list.private = 0'; + } - // only fetch id, tag, mainpage and - // private hoping this will be faster - $profile_list->selectAdd('profile_list.id, ' . - 'profile_list.tag, ' . - 'profile_list.mainpage, ' . - 'profile_list.private'); - $profile_list->joinAdd($profile_tag); - $profile_list->find(); + $profile_list->query($qry); Profile_list::setCache($key, $profile_list); return $profile_list; } - static function getTagsArray($tagger, $tagged, $auth_user_id=null) + static function getTagsArray($tagger, $tagged, Profile $scoped=null) { $ptag = new Profile_tag(); - $ptag->tagger = $tagger; - $ptag->tagged = $tagged; - - if ($tagger != $auth_user_id) { - $list = new Profile_list(); - $list->private = false; - $ptag->joinAdd($list); - $ptag->selectAdd(); - $ptag->selectAdd('profile_tag.tag'); + + $qry = sprintf('select profile_tag.tag '. + 'from profile_tag join profile_list '. + ' on (profile_tag.tagger = profile_list.tagger ' . + ' and profile_tag.tag = profile_list.tag) ' . + 'where profile_tag.tagger = %d ' . + 'and profile_tag.tagged = %d ', + $tagger, $tagged); + + if (!$scoped instanceof Profile || $scoped->getID() !== $tagger) { + $qry .= 'and profile_list.private = 0'; } $tags = array(); - $ptag->find(); + + $ptag->query($qry); + while ($ptag->fetch()) { $tags[] = $ptag->tag; } - $ptag->free(); return $tags; } - static function setTags($tagger, $tagged, $newtags, $privacy=array()) { + static function setTags($tagger, $tagged, array $newtags, array $privacy=array()) { $newtags = array_unique($newtags); - $oldtags = self::getTagsArray($tagger, $tagged, $tagger); + $oldtags = self::getTagsArray($tagger, $tagged, Profile::getByID($tagger)); $ptag = new Profile_tag(); @@ -157,19 +152,18 @@ class Profile_tag extends Managed_DataObject 'tag' => $tag)); # if tag already exists, return it - if(!empty($ptag)) { + if ($ptag instanceof Profile_tag) { return $ptag; } - $tagger_profile = Profile::staticGet('id', $tagger); - $tagged_profile = Profile::staticGet('id', $tagged); + $tagger_profile = Profile::getByID($tagger); + $tagged_profile = Profile::getByID($tagged); if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) { if (!$tagger_profile->canTag($tagged_profile)) { // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged. throw new ClientException(_('You cannot tag this user.')); - return false; } $tags = new Profile_list(); @@ -182,7 +176,6 @@ class Profile_tag extends Managed_DataObject 'which is the maximum allowed number of tags. ' . 'Try using or deleting some existing tags.'), common_config('peopletag', 'maxtags'))); - return false; } $plist = new Profile_list(); @@ -193,10 +186,9 @@ class Profile_tag extends Managed_DataObject if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) { // TRANS: Client exception thrown when trying to add more people than allowed to a list. throw new ClientException(sprintf(_('You already have %1$d or more people in list %2$s, ' . - 'which is the maximum allowed number.' . + 'which is the maximum allowed number. ' . 'Try unlisting others first.'), common_config('peopletag', 'maxpeople'), $tag)); - return false; } $newtag = new Profile_tag(); @@ -207,9 +199,9 @@ class Profile_tag extends Managed_DataObject $result = $newtag->insert(); - if (!$result) { common_log_db_error($newtag, 'INSERT', __FILE__); + $plist->query('ROLLBACK'); return false; } @@ -220,7 +212,6 @@ class Profile_tag extends Managed_DataObject $newtag->delete(); $profile_list->delete(); throw $e; - return false; } $profile_list->taggedCount(true); @@ -241,20 +232,17 @@ class Profile_tag extends Managed_DataObject if (Event::handle('StartUntagProfile', array($ptag))) { $orig = clone($ptag); $result = $ptag->delete(); - if (!$result) { + if ($result === false) { common_log_db_error($this, 'DELETE', __FILE__); return false; } Event::handle('EndUntagProfile', array($orig)); - if ($result) { - $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger)); - if (!empty($profile_list)) { - $profile_list->taggedCount(true); - } - self::blowCaches($tagger, $tagged); - return true; + $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger)); + if (!empty($profile_list)) { + $profile_list->taggedCount(true); } - return false; + self::blowCaches($tagger, $tagged); + return true; } } @@ -284,14 +272,17 @@ class Profile_tag extends Managed_DataObject 'tag = "%s", tagger = "%s" ' . 'WHERE tag = "%s" ' . 'AND tagger = "%s"'; - $result = $tags->query(sprintf($qry, $new->tag, $new->tagger, - $orig->tag, $orig->tagger)); + $result = $tags->query(sprintf($qry, + $tags->escape($new->tag), + $tags->escape($new->tagger), + $tags->escape($orig->tag), + $tags->escape($orig->tagger))); - if (!$result) { + if ($result === false) { common_log_db_error($tags, 'UPDATE', __FILE__); - return false; + throw new Exception('Could not move Profile_tag, see db log for details.'); } - return true; + return $result; } static function blowCaches($tagger, $tagged) { @@ -307,8 +298,8 @@ class Profile_tag extends Managed_DataObject $profile->query('SELECT profile.* ' . 'FROM profile JOIN profile_tag ' . 'ON profile.id = profile_tag.tagged ' . - 'WHERE profile_tag.tagger = ' . $tagger . ' ' . - 'AND profile_tag.tag = "' . $tag . '" '); + 'WHERE profile_tag.tagger = ' . $profile->escape($tagger) . ' ' . + 'AND profile_tag.tag = "' . $profile->escape($tag) . '" '); $tagged = array(); while ($profile->fetch()) { $tagged[] = clone($profile); @@ -327,10 +318,10 @@ class Profile_tag extends Managed_DataObject return $result; } - function delete() + function delete($useWhere=false) { - $result = parent::delete(); - if ($result) { + $result = parent::delete($useWhere); + if ($result !== false) { self::blow('profile_list:tagged_count:%d:%s', $this->tagger, $this->tag);