Profile_tag.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Table Definition for profile_tag
  19. */
  20. class Profile_tag extends Managed_DataObject
  21. {
  22. public $__table = 'profile_tag'; // table name
  23. public $tagger; // int(4) primary_key not_null
  24. public $tagged; // int(4) primary_key not_null
  25. public $tag; // varchar(64) primary_key not_null
  26. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  27. public static function schemaDef()
  28. {
  29. return array(
  30. 'fields' => array(
  31. 'tagger' => array('type' => 'int', 'not null' => true, 'description' => 'user making the tag'),
  32. 'tagged' => array('type' => 'int', 'not null' => true, 'description' => 'profile tagged'),
  33. 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this notice'),
  34. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date the tag was added'),
  35. ),
  36. 'primary key' => array('tagger', 'tagged', 'tag'),
  37. 'foreign keys' => array(
  38. 'profile_tag_tagger_fkey' => array('profile', array('tagger' => 'id')),
  39. 'profile_tag_tagged_fkey' => array('profile', array('tagged' => 'id')),
  40. ),
  41. 'indexes' => array(
  42. 'profile_tag_modified_idx' => array('modified'),
  43. 'profile_tag_tagger_tag_idx' => array('tagger', 'tag'),
  44. 'profile_tag_tagged_idx' => array('tagged'),
  45. ),
  46. );
  47. }
  48. public function links()
  49. {
  50. return array('tagger,tag' => 'profile_list:tagger,tag');
  51. }
  52. public function getMeta()
  53. {
  54. return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag));
  55. }
  56. public static function getSelfTagsArray(Profile $target)
  57. {
  58. return self::getTagsArray($target->getID(), $target->getID(), $target);
  59. }
  60. public static function setSelfTags(Profile $target, array $newtags, array $privacy = [])
  61. {
  62. return self::setTags($target->getID(), $target->getID(), $newtags, $privacy);
  63. }
  64. public static function getTags($tagger, $tagged, $auth_user = null)
  65. {
  66. $profile_list = new Profile_list();
  67. $include_priv = 1;
  68. if (!($auth_user instanceof User ||
  69. $auth_user instanceof Profile) ||
  70. ($auth_user->id !== $tagger)) {
  71. $profile_list->private = false;
  72. $include_priv = 0;
  73. }
  74. $key = sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $include_priv);
  75. $tags = Profile_list::getCached($key);
  76. if ($tags !== false) {
  77. return $tags;
  78. }
  79. $qry = 'select profile_list.* from profile_list left join '.
  80. 'profile_tag on (profile_list.tag = profile_tag.tag and '.
  81. 'profile_list.tagger = profile_tag.tagger) where '.
  82. 'profile_tag.tagger = %d and profile_tag.tagged = %d ';
  83. $qry = sprintf($qry, $tagger, $tagged);
  84. if (!$include_priv) {
  85. $qry .= ' AND profile_list.private IS NOT TRUE';
  86. }
  87. $profile_list->query($qry);
  88. Profile_list::setCache($key, $profile_list);
  89. return $profile_list;
  90. }
  91. public static function getTagsArray($tagger, $tagged, Profile $scoped = null)
  92. {
  93. $ptag = new Profile_tag();
  94. $qry = sprintf(
  95. 'SELECT profile_tag.tag '.
  96. 'FROM profile_tag INNER JOIN profile_list '.
  97. ' ON (profile_tag.tagger = profile_list.tagger ' .
  98. ' and profile_tag.tag = profile_list.tag) ' .
  99. 'WHERE profile_tag.tagger = %d ' .
  100. 'AND profile_tag.tagged = %d ',
  101. $tagger,
  102. $tagged
  103. );
  104. if (!$scoped instanceof Profile || $scoped->getID() !== $tagger) {
  105. $qry .= 'AND profile_list.private IS NOT TRUE';
  106. }
  107. $tags = array();
  108. $ptag->query($qry);
  109. while ($ptag->fetch()) {
  110. $tags[] = $ptag->tag;
  111. }
  112. return $tags;
  113. }
  114. public static function setTags($tagger, $tagged, array $newtags, array $privacy = [])
  115. {
  116. $newtags = array_unique($newtags);
  117. $oldtags = self::getTagsArray($tagger, $tagged, Profile::getByID($tagger));
  118. $ptag = new Profile_tag();
  119. // Delete stuff that's in old and not in new
  120. $to_delete = array_diff($oldtags, $newtags);
  121. // Insert stuff that's in new and not in old
  122. $to_insert = array_diff($newtags, $oldtags);
  123. foreach ($to_delete as $deltag) {
  124. self::unTag($tagger, $tagged, $deltag);
  125. }
  126. foreach ($to_insert as $instag) {
  127. $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
  128. self::setTag($tagger, $tagged, $instag, null, $private);
  129. }
  130. return true;
  131. }
  132. # set a single tag
  133. public static function setTag($tagger, $tagged, $tag, $desc=null, $private = false)
  134. {
  135. $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
  136. 'tagged' => $tagged,
  137. 'tag' => $tag));
  138. # if tag already exists, return it
  139. if ($ptag instanceof Profile_tag) {
  140. return $ptag;
  141. }
  142. $tagger_profile = Profile::getByID($tagger);
  143. $tagged_profile = Profile::getByID($tagged);
  144. if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
  145. if (!$tagger_profile->canTag($tagged_profile)) {
  146. // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged.
  147. throw new ClientException(_('You cannot tag this user.'));
  148. }
  149. $tags = new Profile_list();
  150. $tags->tagger = $tagger;
  151. $count = (int) $tags->count('distinct tag');
  152. if ($count >= common_config('peopletag', 'maxtags')) {
  153. // TRANS: Client exception thrown trying to set more tags than allowed.
  154. throw new ClientException(sprintf(
  155. _('You already have created %d or more tags ' .
  156. 'which is the maximum allowed number of tags. ' .
  157. 'Try using or deleting some existing tags.'),
  158. common_config('peopletag', 'maxtags')
  159. ));
  160. }
  161. $plist = new Profile_list();
  162. $plist->query('START TRANSACTION');
  163. $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
  164. if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
  165. // TRANS: Client exception thrown when trying to add more people than allowed to a list.
  166. throw new ClientException(sprintf(
  167. _('You already have %1$d or more people in list %2$s, ' .
  168. 'which is the maximum allowed number. ' .
  169. 'Try unlisting others first.'),
  170. common_config('peopletag', 'maxpeople'),
  171. $tag
  172. ));
  173. }
  174. $newtag = new Profile_tag();
  175. $newtag->tagger = $tagger;
  176. $newtag->tagged = $tagged;
  177. $newtag->tag = $tag;
  178. $result = $newtag->insert();
  179. if (!$result) {
  180. common_log_db_error($newtag, 'INSERT', __FILE__);
  181. $plist->query('ROLLBACK');
  182. return false;
  183. }
  184. try {
  185. $plist->query('COMMIT');
  186. Event::handle('EndTagProfile', array($newtag));
  187. } catch (Exception $e) {
  188. $newtag->delete();
  189. $profile_list->delete();
  190. throw $e;
  191. }
  192. $profile_list->taggedCount(true);
  193. self::blowCaches($tagger, $tagged);
  194. }
  195. return $newtag;
  196. }
  197. public static function unTag($tagger, $tagged, $tag)
  198. {
  199. $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
  200. 'tagged' => $tagged,
  201. 'tag' => $tag));
  202. if (!$ptag) {
  203. return true;
  204. }
  205. if (Event::handle('StartUntagProfile', array($ptag))) {
  206. $orig = clone($ptag);
  207. $result = $ptag->delete();
  208. if ($result === false) {
  209. common_log_db_error($this, 'DELETE', __FILE__);
  210. return false;
  211. }
  212. Event::handle('EndUntagProfile', array($orig));
  213. $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
  214. if (!empty($profile_list)) {
  215. $profile_list->taggedCount(true);
  216. }
  217. self::blowCaches($tagger, $tagged);
  218. return true;
  219. }
  220. }
  221. // @fixme: move this to Profile_list?
  222. public static function cleanup($profile_list)
  223. {
  224. $ptag = new Profile_tag();
  225. $ptag->tagger = $profile_list->tagger;
  226. $ptag->tag = $profile_list->tag;
  227. $ptag->find();
  228. while ($ptag->fetch()) {
  229. if (Event::handle('StartUntagProfile', array($ptag))) {
  230. $orig = clone($ptag);
  231. $result = $ptag->delete();
  232. if (!$result) {
  233. common_log_db_error($this, 'DELETE', __FILE__);
  234. }
  235. Event::handle('EndUntagProfile', array($orig));
  236. }
  237. }
  238. }
  239. // move a tag!
  240. public static function moveTag($orig, $new)
  241. {
  242. $tags = new Profile_tag();
  243. $result = $tags->query(sprintf(
  244. <<<'END'
  245. UPDATE profile_tag
  246. SET tag = %1$s, tagger = %2$s, modified = CURRENT_TIMESTAMP
  247. WHERE tag = %3$s AND tagger = %4$s
  248. END,
  249. $tags->_quote($new->tag),
  250. $tags->_quote($new->tagger),
  251. $tags->_quote($orig->tag),
  252. $tags->_quote($orig->tagger)
  253. ));
  254. if ($result === false) {
  255. common_log_db_error($tags, 'UPDATE', __FILE__);
  256. throw new Exception('Could not move Profile_tag, see db log for details.');
  257. }
  258. return $result;
  259. }
  260. public static function blowCaches($tagger, $tagged)
  261. {
  262. foreach (array(0, 1) as $perm) {
  263. self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
  264. }
  265. return true;
  266. }
  267. // Return profiles with a given tag
  268. public static function getTagged($tagger, $tag)
  269. {
  270. $profile = new Profile();
  271. $profile->query('SELECT profile.* ' .
  272. 'FROM profile JOIN profile_tag ' .
  273. 'ON profile.id = profile_tag.tagged ' .
  274. 'WHERE profile_tag.tagger = ' . $profile->escape($tagger) . ' ' .
  275. "AND profile_tag.tag = '" . $profile->escape($tag) . "' ");
  276. $tagged = [];
  277. while ($profile->fetch()) {
  278. $tagged[] = clone($profile);
  279. }
  280. return true;
  281. }
  282. public function insert()
  283. {
  284. $result = parent::insert();
  285. if ($result) {
  286. self::blow(
  287. 'profile_list:tagged_count:%d:%s',
  288. $this->tagger,
  289. $this->tag
  290. );
  291. }
  292. return $result;
  293. }
  294. public function delete($useWhere = false)
  295. {
  296. $result = parent::delete($useWhere);
  297. if ($result !== false) {
  298. self::blow(
  299. 'profile_list:tagged_count:%d:%s',
  300. $this->tagger,
  301. $this->tag
  302. );
  303. }
  304. return $result;
  305. }
  306. }