3 use Shaarli\Bookmark\Bookmark
;
4 use Shaarli\Bookmark\BookmarkArray
;
7 * Populates a reference datastore to test Bookmark
11 public static $NB_LINKS_TOTAL = 11;
13 private $bookmarks = array();
14 private $_publicCount = 0;
15 private $_privateCount = 0;
20 * Populates the test DB with reference data
22 * @param bool $isLegacy Use links as array instead of Bookmark object
24 public function __construct($isLegacy = false)
26 $this->isLegacy
= $isLegacy;
27 if (! $this->isLegacy
) {
28 $this->bookmarks
= new BookmarkArray();
34 'This is an older pinned link',
36 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100309_101010'),
47 'This is a pinned link',
49 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121207_152312'),
58 'Link title: @website',
60 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
62 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'),
70 'Note: I have a big ID but an old date',
72 'Used to test bookmarks reordering.',
74 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
80 'PSR-2: Coding Style Guide',
81 'http://www.php-fig.org/psr/psr-2/',
82 'This guide extends and expands on PSR-1, the basic coding standard.',
84 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_152312'),
85 'coding-style standards quality assurance'
90 'Free as in Freedom 2.0 @website',
91 'https://static.fsf.org/nosvn/faif-2.0.pdf',
92 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
94 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114633'),
95 'free gnu software stallman -exclude stuff hashtag',
96 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20160803_093033')
102 'http://mediagoblin.org/',
103 'A free software media publishing platform #hashtagOther',
105 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130614_184135'),
106 'gnu media web .hidden hashtag',
107 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20130615_184230'),
113 'w3c-markup-validator',
114 'https://dvcs.w3.org/hg/markup-validator/summary',
115 'Mercurial repository for the W3C Validator #private',
117 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20141125_084734'),
118 'css html w3c web Mercurial'
123 'UserFriendly - Web Designer',
124 'http://ars.userfriendly.org/cartoons/?id=20121206',
125 'Naming conventions... #private',
127 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_142300'),
133 'UserFriendly - Samba',
134 'http://ars.userfriendly.org/cartoons/?id=20010306',
137 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_172539'),
144 'http://geek-and-poke.com/',
147 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_182539'),
148 'dev cartoon tag1 tag2 tag3 tag4 '
155 protected function addLink(
171 'description' => $description,
172 'private' => $private,
175 'updated' => $updated,
176 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(Bookmark
::LINK_DATE_FORMAT
) . $id),
179 if (! $this->isLegacy
) {
180 $bookmark = new Bookmark();
181 $this->bookmarks
[$id] = $bookmark->fromArray($link);
183 $this->bookmarks
[$id] = $link;
187 $this->_privateCount++
;
190 $this->_publicCount++
;
194 * Writes data to the datastore
196 public function write($filename)
201 '<?php /* '.base64_encode(gzdeflate(serialize($this->bookmarks
))).' */ ?>'
206 * Reorder links by creation date (newest first).
208 * @param string $order ASC|DESC
210 public function reorder($order = 'DESC')
212 if (! $this->isLegacy
) {
213 $this->bookmarks
->reorder($order);
215 $order = $order === 'ASC' ? -1 : 1;
216 // backward compatibility: ignore reorder if the the `created` field doesn't exist
217 if (! isset(array_values($this->bookmarks
)[0]['created'])) {
221 usort($this->bookmarks
, function ($a, $b) use ($order) {
222 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
223 return $a['sticky'] ? -1 : 1;
226 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
232 * Returns the number of bookmarks in the reference data
234 public function countLinks()
236 return $this->_publicCount +
$this->_privateCount
;
240 * Returns the number of public bookmarks in the reference data
242 public function countPublicLinks()
244 return $this->_publicCount
;
248 * Returns the number of private bookmarks in the reference data
250 public function countPrivateLinks()
252 return $this->_privateCount
;
256 * Returns the number of bookmarks without tag
258 public function countUntaggedLinks()
261 foreach ($this->bookmarks
as $link) {
262 if (! $this->isLegacy
) {
263 if (empty($link->getTags())) {
267 if (empty($link['tags'])) {
275 public function getLinks()
278 return $this->bookmarks
;
282 * Setter to override link creation.
284 * @param array $links List of bookmarks.
286 public function setLinks($links)
288 $this->bookmarks
= $links;