PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Bookmark/classes/Bookmark.php

https://gitlab.com/lnxw37/gnu-social
PHP | 300 lines | 185 code | 36 blank | 79 comment | 24 complexity | 11d8c95bddecb3406f7846802d13f6d2 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0
  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('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * For storing the fact that a notice is a bookmark
  34. *
  35. * @category Bookmark
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  39. * @link http://status.net/
  40. *
  41. * @see DB_DataObject
  42. */
  43. class Bookmark extends Managed_DataObject
  44. {
  45. public $__table = 'bookmark'; // table name
  46. public $id; // char(36) primary_key not_null
  47. public $profile_id; // int(4) not_null
  48. public $url; // varchar(191) not_null not 255 because utf8mb4 takes more space
  49. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  50. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  51. public $description; // text
  52. public $created; // datetime
  53. public static function schemaDef()
  54. {
  55. return array(
  56. 'fields' => array(
  57. 'id' => array('type' => 'char',
  58. 'length' => 36,
  59. 'not null' => true),
  60. 'profile_id' => array('type' => 'int', 'not null' => true),
  61. 'uri' => array('type' => 'varchar',
  62. 'length' => 191,
  63. 'not null' => true),
  64. 'url' => array('type' => 'varchar',
  65. 'length' => 191,
  66. 'not null' => true),
  67. 'title' => array('type' => 'varchar', 'length' => 191),
  68. 'description' => array('type' => 'text'),
  69. 'created' => array('type' => 'datetime', 'not null' => true),
  70. ),
  71. 'primary key' => array('id'),
  72. 'unique keys' => array(
  73. 'bookmark_uri_key' => array('uri'),
  74. ),
  75. 'foreign keys' => array(
  76. 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id'))
  77. ),
  78. 'indexes' => array('bookmark_created_idx' => array('created'),
  79. 'bookmark_url_idx' => array('url'),
  80. 'bookmark_profile_id_idx' => array('profile_id'),
  81. ),
  82. );
  83. }
  84. /**
  85. * Get a bookmark based on a notice
  86. *
  87. * @param Notice $notice Notice to check for
  88. *
  89. * @return Bookmark found bookmark or null
  90. */
  91. static function getByNotice($notice)
  92. {
  93. return self::getKV('uri', $notice->uri);
  94. }
  95. /**
  96. * Get the bookmark that a user made for an URL
  97. *
  98. * @param Profile $profile Profile to check for
  99. * @param string $url URL to check for
  100. *
  101. * @return Bookmark bookmark found or null
  102. */
  103. static function getByURL($profile, $url)
  104. {
  105. $nb = new Bookmark();
  106. $nb->profile_id = $profile->id;
  107. $nb->url = $url;
  108. if ($nb->find(true)) {
  109. return $nb;
  110. } else {
  111. return null;
  112. }
  113. }
  114. /**
  115. * Save a new notice bookmark
  116. *
  117. * @param Profile $profile To save the bookmark for
  118. * @param string $title Title of the bookmark
  119. * @param string $url URL of the bookmark
  120. * @param mixed $rawtags array of tags or string
  121. * @param string $description Description of the bookmark
  122. * @param array $options Options for the Notice::saveNew()
  123. *
  124. * @return Notice saved notice
  125. */
  126. static function saveNew($profile, $title, $url, $rawtags, $description,
  127. $options=null)
  128. {
  129. if (!common_valid_http_url($url)) {
  130. throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
  131. }
  132. $nb = self::getByURL($profile, $url);
  133. if (!empty($nb)) {
  134. // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
  135. throw new ClientException(_m('Bookmark already exists.'));
  136. }
  137. if (empty($options)) {
  138. $options = array();
  139. }
  140. if (array_key_exists('uri', $options)) {
  141. $other = Bookmark::getKV('uri', $options['uri']);
  142. if (!empty($other)) {
  143. // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
  144. throw new ClientException(_m('Bookmark already exists.'));
  145. }
  146. }
  147. if (is_string($rawtags)) {
  148. if (empty($rawtags)) {
  149. $rawtags = array();
  150. } else {
  151. $rawtags = preg_split('/[\s,]+/', $rawtags);
  152. }
  153. }
  154. $nb = new Bookmark();
  155. $nb->id = UUID::gen();
  156. $nb->profile_id = $profile->id;
  157. $nb->url = $url;
  158. $nb->title = $title;
  159. $nb->description = $description;
  160. if (array_key_exists('created', $options)) {
  161. $nb->created = $options['created'];
  162. } else {
  163. $nb->created = common_sql_now();
  164. }
  165. if (array_key_exists('uri', $options)) {
  166. $nb->uri = $options['uri'];
  167. } else {
  168. // FIXME: hacks to work around router bugs in
  169. // queue daemons
  170. $r = Router::get();
  171. $path = $r->build('showbookmark',
  172. array('id' => $nb->id));
  173. if (empty($path)) {
  174. $nb->uri = common_path('bookmark/'.$nb->id, false, false);
  175. } else {
  176. $nb->uri = common_local_url('showbookmark',
  177. array('id' => $nb->id),
  178. null,
  179. null,
  180. false);
  181. }
  182. }
  183. $nb->insert();
  184. $tags = array();
  185. $replies = array();
  186. // filter "for:nickname" tags
  187. foreach ($rawtags as $tag) {
  188. if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
  189. // skip if done by caller
  190. if (!array_key_exists('replies', $options)) {
  191. $nickname = mb_substr($tag, 4);
  192. $other = common_relative_profile($profile,
  193. $nickname);
  194. if (!empty($other)) {
  195. $replies[] = $other->getUri();
  196. }
  197. }
  198. } else {
  199. $tags[] = common_canonical_tag($tag);
  200. }
  201. }
  202. $hashtags = array();
  203. $taglinks = array();
  204. foreach ($tags as $tag) {
  205. $hashtags[] = '#'.$tag;
  206. $attrs = array('href' => Notice_tag::url($tag),
  207. 'rel' => $tag,
  208. 'class' => 'tag');
  209. $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
  210. }
  211. // Use user's preferences for short URLs, if possible
  212. try {
  213. $user = User::getKV('id', $profile->id);
  214. $shortUrl = File_redirection::makeShort($url,
  215. empty($user) ? null : $user);
  216. } catch (Exception $e) {
  217. // Don't let this stop us.
  218. $shortUrl = $url;
  219. }
  220. // TRANS: Bookmark content.
  221. // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
  222. // TRANS: %4$s is space separated list of hash tags.
  223. $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
  224. $title,
  225. $shortUrl,
  226. $description,
  227. implode(' ', $hashtags));
  228. // TRANS: Rendered bookmark content.
  229. // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
  230. // TRANS: %4$s is space separated list of hash tags.
  231. $rendered = sprintf(_m('<span class="xfolkentry">'.
  232. '<a class="taggedlink" href="%1$s">%2$s</a> '.
  233. '<span class="description">%3$s</span> '.
  234. '<span class="meta">%4$s</span>'.
  235. '</span>'),
  236. htmlspecialchars($url),
  237. htmlspecialchars($title),
  238. htmlspecialchars($description),
  239. implode(' ', $taglinks));
  240. $options = array_merge(array('urls' => array($url),
  241. 'rendered' => $rendered,
  242. 'tags' => $tags,
  243. 'replies' => $replies,
  244. 'object_type' => ActivityObject::BOOKMARK),
  245. $options);
  246. if (!array_key_exists('uri', $options)) {
  247. $options['uri'] = $nb->uri;
  248. }
  249. try {
  250. $saved = Notice::saveNew($profile->id,
  251. $content,
  252. array_key_exists('source', $options) ?
  253. $options['source'] : 'web',
  254. $options);
  255. } catch (Exception $e) {
  256. $nb->delete();
  257. throw $e;
  258. }
  259. if (empty($saved)) {
  260. $nb->delete();
  261. }
  262. return $saved;
  263. }
  264. }