Profile_tag.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * Table Definition for profile_tag
  4. */
  5. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  6. class Profile_tag extends Managed_DataObject
  7. {
  8. ###START_AUTOCODE
  9. /* the code below is auto generated do not remove the above tag */
  10. public $__table = 'profile_tag'; // table name
  11. public $tagger; // int(4) primary_key not_null
  12. public $tagged; // int(4) primary_key not_null
  13. public $tag; // varchar(64) primary_key not_null
  14. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  15. /* the code above is auto generated do not remove the tag below */
  16. ###END_AUTOCODE
  17. public static function schemaDef()
  18. {
  19. return array(
  20. 'fields' => array(
  21. 'tagger' => array('type' => 'int', 'not null' => true, 'description' => 'user making the tag'),
  22. 'tagged' => array('type' => 'int', 'not null' => true, 'description' => 'profile tagged'),
  23. 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this notice'),
  24. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date the tag was added'),
  25. ),
  26. 'primary key' => array('tagger', 'tagged', 'tag'),
  27. 'foreign keys' => array(
  28. 'profile_tag_tagger_fkey' => array('profile', array('tagger' => 'id')),
  29. 'profile_tag_tagged_fkey' => array('profile', array('tagged' => 'id')),
  30. 'profile_tag_tag_fkey' => array('profile_list', array('tag' => 'tag')),
  31. ),
  32. 'indexes' => array(
  33. 'profile_tag_modified_idx' => array('modified'),
  34. 'profile_tag_tagger_tag_idx' => array('tagger', 'tag'),
  35. 'profile_tag_tagged_idx' => array('tagged'),
  36. ),
  37. );
  38. }
  39. function links()
  40. {
  41. return array('tagger,tag' => 'profile_list:tagger,tag');
  42. }
  43. function getMeta()
  44. {
  45. return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag));
  46. }
  47. static function getTags($tagger, $tagged, $auth_user=null) {
  48. $profile_list = new Profile_list();
  49. $include_priv = 1;
  50. if (!($auth_user instanceof User ||
  51. $auth_user instanceof Profile) ||
  52. ($auth_user->id !== $tagger)) {
  53. $profile_list->private = false;
  54. $include_priv = 0;
  55. }
  56. $key = sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $include_priv);
  57. $tags = Profile_list::getCached($key);
  58. if ($tags !== false) {
  59. return $tags;
  60. }
  61. $qry = 'select profile_list.* from profile_list left join '.
  62. 'profile_tag on (profile_list.tag = profile_tag.tag and '.
  63. 'profile_list.tagger = profile_tag.tagger) where '.
  64. 'profile_tag.tagger = %d and profile_tag.tagged = %d ';
  65. $qry = sprintf($qry, $tagger, $tagged);
  66. if (!$include_priv) {
  67. $qry .= ' and profile_list.private = 0';
  68. }
  69. $profile_list->query($qry);
  70. Profile_list::setCache($key, $profile_list);
  71. return $profile_list;
  72. }
  73. static function getTagsArray($tagger, $tagged, $auth_user_id=null)
  74. {
  75. $ptag = new Profile_tag();
  76. $qry = sprintf('select profile_tag.tag '.
  77. 'from profile_tag join profile_list '.
  78. ' on (profile_tag.tagger = profile_list.tagger ' .
  79. ' and profile_tag.tag = profile_list.tag) ' .
  80. 'where profile_tag.tagger = %d ' .
  81. 'and profile_tag.tagged = %d ',
  82. $tagger, $tagged);
  83. if ($auth_user_id != $tagger) {
  84. $qry .= 'and profile_list.private = 0';
  85. }
  86. $tags = array();
  87. $ptag->query($qry);
  88. while ($ptag->fetch()) {
  89. $tags[] = $ptag->tag;
  90. }
  91. return $tags;
  92. }
  93. static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
  94. $newtags = array_unique($newtags);
  95. $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
  96. $ptag = new Profile_tag();
  97. // Delete stuff that's in old and not in new
  98. $to_delete = array_diff($oldtags, $newtags);
  99. // Insert stuff that's in new and not in old
  100. $to_insert = array_diff($newtags, $oldtags);
  101. foreach ($to_delete as $deltag) {
  102. self::unTag($tagger, $tagged, $deltag);
  103. }
  104. foreach ($to_insert as $instag) {
  105. $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
  106. self::setTag($tagger, $tagged, $instag, null, $private);
  107. }
  108. return true;
  109. }
  110. # set a single tag
  111. static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
  112. $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
  113. 'tagged' => $tagged,
  114. 'tag' => $tag));
  115. # if tag already exists, return it
  116. if(!empty($ptag)) {
  117. return $ptag;
  118. }
  119. $tagger_profile = Profile::getKV('id', $tagger);
  120. $tagged_profile = Profile::getKV('id', $tagged);
  121. if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
  122. if (!$tagger_profile->canTag($tagged_profile)) {
  123. // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged.
  124. throw new ClientException(_('You cannot tag this user.'));
  125. return false;
  126. }
  127. $tags = new Profile_list();
  128. $tags->tagger = $tagger;
  129. $count = (int) $tags->count('distinct tag');
  130. if ($count >= common_config('peopletag', 'maxtags')) {
  131. // TRANS: Client exception thrown trying to set more tags than allowed.
  132. throw new ClientException(sprintf(_('You already have created %d or more tags ' .
  133. 'which is the maximum allowed number of tags. ' .
  134. 'Try using or deleting some existing tags.'),
  135. common_config('peopletag', 'maxtags')));
  136. return false;
  137. }
  138. $plist = new Profile_list();
  139. $plist->query('BEGIN');
  140. $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
  141. if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
  142. // TRANS: Client exception thrown when trying to add more people than allowed to a list.
  143. throw new ClientException(sprintf(_('You already have %1$d or more people in list %2$s, ' .
  144. 'which is the maximum allowed number. ' .
  145. 'Try unlisting others first.'),
  146. common_config('peopletag', 'maxpeople'), $tag));
  147. return false;
  148. }
  149. $newtag = new Profile_tag();
  150. $newtag->tagger = $tagger;
  151. $newtag->tagged = $tagged;
  152. $newtag->tag = $tag;
  153. $result = $newtag->insert();
  154. if (!$result) {
  155. common_log_db_error($newtag, 'INSERT', __FILE__);
  156. return false;
  157. }
  158. try {
  159. $plist->query('COMMIT');
  160. Event::handle('EndTagProfile', array($newtag));
  161. } catch (Exception $e) {
  162. $newtag->delete();
  163. $profile_list->delete();
  164. throw $e;
  165. return false;
  166. }
  167. $profile_list->taggedCount(true);
  168. self::blowCaches($tagger, $tagged);
  169. }
  170. return $newtag;
  171. }
  172. static function unTag($tagger, $tagged, $tag) {
  173. $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
  174. 'tagged' => $tagged,
  175. 'tag' => $tag));
  176. if (!$ptag) {
  177. return true;
  178. }
  179. if (Event::handle('StartUntagProfile', array($ptag))) {
  180. $orig = clone($ptag);
  181. $result = $ptag->delete();
  182. if (!$result) {
  183. common_log_db_error($this, 'DELETE', __FILE__);
  184. return false;
  185. }
  186. Event::handle('EndUntagProfile', array($orig));
  187. if ($result) {
  188. $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
  189. if (!empty($profile_list)) {
  190. $profile_list->taggedCount(true);
  191. }
  192. self::blowCaches($tagger, $tagged);
  193. return true;
  194. }
  195. return false;
  196. }
  197. }
  198. // @fixme: move this to Profile_list?
  199. static function cleanup($profile_list) {
  200. $ptag = new Profile_tag();
  201. $ptag->tagger = $profile_list->tagger;
  202. $ptag->tag = $profile_list->tag;
  203. $ptag->find();
  204. while($ptag->fetch()) {
  205. if (Event::handle('StartUntagProfile', array($ptag))) {
  206. $orig = clone($ptag);
  207. $result = $ptag->delete();
  208. if (!$result) {
  209. common_log_db_error($this, 'DELETE', __FILE__);
  210. }
  211. Event::handle('EndUntagProfile', array($orig));
  212. }
  213. }
  214. }
  215. // move a tag!
  216. static function moveTag($orig, $new) {
  217. $tags = new Profile_tag();
  218. $qry = 'UPDATE profile_tag SET ' .
  219. 'tag = "%s", tagger = "%s" ' .
  220. 'WHERE tag = "%s" ' .
  221. 'AND tagger = "%s"';
  222. $result = $tags->query(sprintf($qry,
  223. $tags->escape($new->tag),
  224. $tags->escape($new->tagger),
  225. $tags->escape($orig->tag),
  226. $tags->escape($orig->tagger)));
  227. if ($result === false) {
  228. common_log_db_error($tags, 'UPDATE', __FILE__);
  229. throw new Exception('Could not move Profile_tag, see db log for details.');
  230. }
  231. return $result;
  232. }
  233. static function blowCaches($tagger, $tagged) {
  234. foreach (array(0, 1) as $perm) {
  235. self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
  236. }
  237. return true;
  238. }
  239. // Return profiles with a given tag
  240. static function getTagged($tagger, $tag) {
  241. $profile = new Profile();
  242. $profile->query('SELECT profile.* ' .
  243. 'FROM profile JOIN profile_tag ' .
  244. 'ON profile.id = profile_tag.tagged ' .
  245. 'WHERE profile_tag.tagger = ' . $profile->escape($tagger) . ' ' .
  246. 'AND profile_tag.tag = "' . $profile->escape($tag) . '" ');
  247. $tagged = array();
  248. while ($profile->fetch()) {
  249. $tagged[] = clone($profile);
  250. }
  251. return true;
  252. }
  253. function insert()
  254. {
  255. $result = parent::insert();
  256. if ($result) {
  257. self::blow('profile_list:tagged_count:%d:%s',
  258. $this->tagger,
  259. $this->tag);
  260. }
  261. return $result;
  262. }
  263. function delete($useWhere=false)
  264. {
  265. $result = parent::delete($useWhere);
  266. if ($result !== false) {
  267. self::blow('profile_list:tagged_count:%d:%s',
  268. $this->tagger,
  269. $this->tag);
  270. }
  271. return $result;
  272. }
  273. }