]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/utils/ReferenceLinkDB.php
3 use Shaarli\Bookmark\LinkDB
;
6 * Populates a reference datastore to test LinkDB
10 public static $NB_LINKS_TOTAL = 11;
12 private $_links = array();
13 private $_publicCount = 0;
14 private $_privateCount = 0;
17 * Populates the test DB with reference data
19 public function __construct()
25 'This is an older pinned link',
27 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20100309_101010'),
38 'This is a pinned link',
40 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121207_152312'),
49 'Link title: @website',
51 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
53 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114651'),
61 'Note: I have a big ID but an old date',
63 'Used to test links reordering.',
65 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20100310_101010'),
71 'PSR-2: Coding Style Guide',
72 'http://www.php-fig.org/psr/psr-2/',
73 'This guide extends and expands on PSR-1, the basic coding standard.',
75 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_152312'),
81 'Free as in Freedom 2.0 @website',
82 'https://static.fsf.org/nosvn/faif-2.0.pdf',
83 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
85 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114633'),
86 'free gnu software stallman -exclude stuff hashtag',
87 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20160803_093033')
93 'http://mediagoblin.org/',
94 'A free software media publishing platform #hashtagOther',
96 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
97 'gnu media web .hidden hashtag',
98 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
104 'w3c-markup-validator',
105 'https://dvcs.w3.org/hg/markup-validator/summary',
106 'Mercurial repository for the W3C Validator #private',
108 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20141125_084734'),
109 'css html w3c web Mercurial'
114 'UserFriendly - Web Designer',
115 'http://ars.userfriendly.org/cartoons/?id=20121206',
116 'Naming conventions... #private',
118 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_142300'),
124 'UserFriendly - Samba',
125 'http://ars.userfriendly.org/cartoons/?id=20010306',
128 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_172539'),
135 'http://geek-and-poke.com/',
138 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_182539'),
139 'dev cartoon tag1 tag2 tag3 tag4 '
146 protected function addLink(
162 'description' => $description,
163 'private' => $private,
166 'updated' => $updated,
167 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB
::LINK_DATE_FORMAT
) . $id),
170 $this->_links
[$id] = $link;
173 $this->_privateCount++
;
176 $this->_publicCount++
;
180 * Writes data to the datastore
182 public function write($filename)
187 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links
))).' */ ?>'
192 * Reorder links by creation date (newest first).
194 * Also update the urls and ids mapping arrays.
196 * @param string $order ASC|DESC
198 public function reorder($order = 'DESC')
200 // backward compatibility: ignore reorder if the the `created` field doesn't exist
201 if (! isset(array_values($this->_links
)[0]['created'])) {
205 $order = $order === 'ASC' ? -1 : 1;
206 // Reorder array by dates.
207 usort($this->_links
, function ($a, $b) use ($order) {
208 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
209 return $a['sticky'] ? -1 : 1;
212 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
217 * Returns the number of links in the reference data
219 public function countLinks()
221 return $this->_publicCount +
$this->_privateCount
;
225 * Returns the number of public links in the reference data
227 public function countPublicLinks()
229 return $this->_publicCount
;
233 * Returns the number of private links in the reference data
235 public function countPrivateLinks()
237 return $this->_privateCount
;
241 * Returns the number of links without tag
243 public function countUntaggedLinks()
246 foreach ($this->_links
as $link) {
247 if (empty($link['tags'])) {
254 public function getLinks()
257 return $this->_links
;
261 * Setter to override link creation.
263 * @param array $links List of links.
265 public function setLinks($links)
267 $this->_links
= $links;