Bookmark.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /**
  3. * Data class to mark notices as bookmarks
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2009, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * For storing the fact that a notice is a bookmark
  32. *
  33. * @category Bookmark
  34. * @package StatusNet
  35. * @author Evan Prodromou <evan@status.net>
  36. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  37. * @link http://status.net/
  38. *
  39. * @see DB_DataObject
  40. */
  41. class Bookmark extends Managed_DataObject
  42. {
  43. public $__table = 'bookmark'; // table name
  44. public $id; // char(36) primary_key not_null
  45. public $profile_id; // int(4) not_null
  46. public $url; // varchar(191) not_null not 255 because utf8mb4 takes more space
  47. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  48. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  49. public $description; // text
  50. public $created; // datetime
  51. public static function schemaDef()
  52. {
  53. return array(
  54. 'fields' => array(
  55. 'id' => array('type' => 'char',
  56. 'length' => 36,
  57. 'not null' => true),
  58. 'profile_id' => array('type' => 'int', 'not null' => true),
  59. 'uri' => array('type' => 'varchar',
  60. 'length' => 191,
  61. 'not null' => true),
  62. 'url' => array('type' => 'varchar',
  63. 'length' => 191,
  64. 'not null' => true),
  65. 'title' => array('type' => 'varchar', 'length' => 191),
  66. 'description' => array('type' => 'text'),
  67. 'created' => array('type' => 'datetime', 'not null' => true),
  68. ),
  69. 'primary key' => array('uri'),
  70. 'unique keys' => array(
  71. 'bookmark_id_key' => array('id'),
  72. ),
  73. 'foreign keys' => array(
  74. 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  75. 'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')),
  76. ),
  77. 'indexes' => array('bookmark_created_idx' => array('created'),
  78. 'bookmark_url_idx' => array('url'),
  79. 'bookmark_profile_id_idx' => array('profile_id'),
  80. ),
  81. );
  82. }
  83. /**
  84. * Get a bookmark based on a notice
  85. *
  86. * @param Notice $stored Notice activity which represents the Bookmark
  87. *
  88. * @return Bookmark The found bookmark object.
  89. * @throws NoResultException When you don't find it after all.
  90. */
  91. static public function fromStored(Notice $stored)
  92. {
  93. return self::getByPK(array('uri' => $stored->getUri()));
  94. }
  95. public function getStored()
  96. {
  97. return Notice::getByKeys(array('uri' => $this->getUri()));
  98. }
  99. public function getDescription()
  100. {
  101. return $this->description;
  102. }
  103. public function getTitle()
  104. {
  105. return $this->title;
  106. }
  107. public function getUri()
  108. {
  109. return $this->uri;
  110. }
  111. public function getUrl()
  112. {
  113. if (empty($this->url)) {
  114. throw new InvalidUrlException($this->url);
  115. }
  116. return $this->url;
  117. }
  118. /**
  119. * Get the bookmark that a user made for an URL
  120. *
  121. * @param Profile $profile Profile to check for
  122. * @param string $url URL to check for
  123. *
  124. * @return Bookmark bookmark found or null
  125. */
  126. static function getByURL(Profile $profile, $url)
  127. {
  128. $nb = new Bookmark();
  129. $nb->profile_id = $profile->getID();
  130. $nb->url = $url;
  131. if (!$nb->find(true)) {
  132. throw new NoResultException($nb);
  133. }
  134. return $nb;
  135. }
  136. /**
  137. * Store a Bookmark object
  138. *
  139. * @param Profile $profile To save the bookmark for
  140. * @param string $title Title of the bookmark
  141. * @param string $url URL of the bookmark
  142. * @param string $description Description of the bookmark
  143. *
  144. * @return Bookmark the Bookmark object
  145. */
  146. static function saveActivityObject(ActivityObject $actobj, Notice $stored)
  147. {
  148. $url = null;
  149. // each extra element is array('tagname', array('attr'=>'val', ...), 'content')
  150. foreach ($actobj->extra as $extra) {
  151. if ($extra[1]['rel'] !== 'related') {
  152. continue;
  153. }
  154. if ($url===null && strlen($extra[1]['href'])>0) {
  155. $url = $extra[1]['href'];
  156. } elseif ($url !== null) {
  157. // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
  158. throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
  159. }
  160. }
  161. if (is_null($url)) {
  162. // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
  163. throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
  164. }
  165. if (!strlen($actobj->title)) {
  166. throw new ClientException(_m('You must provide a non-empty title.'));
  167. }
  168. if (!common_valid_http_url($url)) {
  169. throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
  170. }
  171. try {
  172. $object = self::getByURL($stored->getProfile(), $url);
  173. throw new ClientException(_m('You have already bookmarked this URL.'));
  174. } catch (NoResultException $e) {
  175. // Alright, so then we have to create it.
  176. }
  177. $nb = new Bookmark();
  178. $nb->id = UUID::gen();
  179. $nb->uri = $stored->getUri();
  180. $nb->profile_id = $stored->getProfile()->getID();
  181. $nb->title = $actobj->title;
  182. $nb->url = $url;
  183. $nb->description = $actobj->summary;
  184. $nb->created = $stored->created;
  185. $result = $nb->insert();
  186. if ($result === false) {
  187. throw new ServerException('Could not insert Bookmark into database!');
  188. }
  189. return $nb;
  190. }
  191. public function asActivityObject()
  192. {
  193. $stored = $this->getStored();
  194. $object = new ActivityObject();
  195. $object->id = $this->getUri();
  196. $object->type = ActivityObject::BOOKMARK;
  197. $object->title = $this->getTitle();
  198. $object->summary = $this->getDescription();
  199. $object->link = $stored->getUrl();
  200. $object->content = $stored->getRendered();
  201. // Attributes of the URL
  202. $attachments = $stored->attachments();
  203. if (count($attachments) != 1) {
  204. // TRANS: Server exception thrown when a bookmark has multiple attachments.
  205. throw new ServerException(_m('Bookmark notice with the '.
  206. 'wrong number of attachments.'));
  207. }
  208. $bookmarkedurl = $attachments[0];
  209. $attrs = array('rel' => 'related',
  210. 'href' => $bookmarkedurl->getUrl());
  211. if (!strlen($bookmarkedurl->title)) {
  212. $attrs['title'] = $bookmarkedurl->title;
  213. }
  214. $object->extra[] = array('link', $attrs, null);
  215. // Attributes of the thumbnail, if any
  216. try {
  217. $thumbnail = $bookmarkedurl->getThumbnail();
  218. $tattrs = array('rel' => 'preview',
  219. 'href' => $thumbnail->getUrl());
  220. if (!empty($thumbnail->width)) {
  221. $tattrs['media:width'] = $thumbnail->width;
  222. }
  223. if (!empty($thumbnail->height)) {
  224. $tattrs['media:height'] = $thumbnail->height;
  225. }
  226. $object->extra[] = array('link', $tattrs, null);
  227. } catch (UnsupportedMediaException $e) {
  228. // No image thumbnail metadata available
  229. }
  230. return $object;
  231. }
  232. /**
  233. * Save a new notice bookmark
  234. *
  235. * @param Profile $profile To save the bookmark for
  236. * @param string $title Title of the bookmark
  237. * @param string $url URL of the bookmark
  238. * @param array $rawtags array of tags
  239. * @param string $description Description of the bookmark
  240. * @param array $options Options for the Notice::saveNew()
  241. *
  242. * @return Notice saved notice
  243. */
  244. static function addNew(Profile $actor, $title, $url, array $rawtags, $description, array $options=array())
  245. {
  246. $act = new Activity();
  247. $act->verb = ActivityVerb::POST;
  248. $act->time = time();
  249. $act->actor = $actor->asActivityObject();
  250. $actobj = new ActivityObject();
  251. $actobj->type = ActivityObject::BOOKMARK;
  252. $actobj->title = $title;
  253. $actobj->summary = $description;
  254. $actobj->extra[] = array('link', array('rel'=>'related', 'href'=>$url), null);
  255. $act->objects[] = $actobj;
  256. $act->enclosures[] = $url;
  257. $tags = array();
  258. $replies = array();
  259. // filter "for:nickname" tags
  260. foreach ($rawtags as $tag) {
  261. if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
  262. // skip if done by caller
  263. if (!array_key_exists('replies', $options)) {
  264. $nickname = mb_substr($tag, 4);
  265. $other = common_relative_profile($actor, $nickname);
  266. if (!empty($other)) {
  267. $replies[] = $other->getUri();
  268. }
  269. }
  270. } else {
  271. $tags[] = common_canonical_tag($tag);
  272. }
  273. }
  274. $hashtags = array();
  275. $taglinks = array();
  276. foreach ($tags as $tag) {
  277. $hashtags[] = '#'.$tag;
  278. $attrs = array('href' => Notice_tag::url($tag),
  279. 'rel' => $tag,
  280. 'class' => 'tag');
  281. $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
  282. }
  283. // Use user's preferences for short URLs, if possible
  284. // FIXME: Should be possible to with the Profile object...
  285. try {
  286. $user = $actor->getUser();
  287. $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
  288. } catch (Exception $e) {
  289. // Don't let this stop us.
  290. $shortUrl = $url;
  291. }
  292. // TRANS: Rendered bookmark content.
  293. // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
  294. // TRANS: %4$s is space separated list of hash tags.
  295. $actobj->content = sprintf(_m('<span class="xfolkentry">'.
  296. '<a class="taggedlink" href="%1$s">%2$s</a> '.
  297. '<span class="description">%3$s</span> '.
  298. '<span class="meta">%4$s</span>'.
  299. '</span>'),
  300. htmlspecialchars($url),
  301. htmlspecialchars($title),
  302. htmlspecialchars($description),
  303. implode(' ', $taglinks));
  304. foreach ($tags as $term) {
  305. $catEl = new AtomCategory();
  306. $catEl->term = $term;
  307. $activity->categories[] = $catEl;
  308. }
  309. $options = array_merge(array('urls' => array($url),
  310. 'rendered' => $rendered,
  311. 'tags' => $tags,
  312. 'replies' => $replies,
  313. 'object_type' => ActivityObject::BOOKMARK),
  314. $options);
  315. return Notice::saveActivity($act, $actor, $options);
  316. }
  317. }