]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/utils/ReferenceLinkDB.php
namespacing: \Shaarli\Bookmark\LinkDB
[github/shaarli/Shaarli.git] / tests / utils / ReferenceLinkDB.php
1 <?php
2
3 use Shaarli\Bookmark\LinkDB;
4
5 /**
6 * Populates a reference datastore to test LinkDB
7 */
8 class ReferenceLinkDB
9 {
10 public static $NB_LINKS_TOTAL = 11;
11
12 private $_links = array();
13 private $_publicCount = 0;
14 private $_privateCount = 0;
15
16 /**
17 * Populates the test DB with reference data
18 */
19 public function __construct()
20 {
21 $this->addLink(
22 11,
23 'Pined older',
24 '?PCRizQ',
25 'This is an older pinned link',
26 0,
27 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100309_101010'),
28 '',
29 null,
30 'PCRizQ',
31 true
32 );
33
34 $this->addLink(
35 10,
36 'Pined',
37 '?0gCTjQ',
38 'This is a pinned link',
39 0,
40 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121207_152312'),
41 '',
42 null,
43 '0gCTjQ',
44 true
45 );
46
47 $this->addLink(
48 41,
49 'Link title: @website',
50 '?WDWyig',
51 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
52 0,
53 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'),
54 'sTuff',
55 null,
56 'WDWyig'
57 );
58
59 $this->addLink(
60 42,
61 'Note: I have a big ID but an old date',
62 '?WDWyig',
63 'Used to test links reordering.',
64 0,
65 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'),
66 'ut'
67 );
68
69 $this->addLink(
70 9,
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.',
74 0,
75 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_152312'),
76 ''
77 );
78
79 $this->addLink(
80 8,
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',
84 0,
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')
88 );
89
90 $this->addLink(
91 7,
92 'MediaGoblin',
93 'http://mediagoblin.org/',
94 'A free software media publishing platform #hashtagOther',
95 0,
96 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
97 'gnu media web .hidden hashtag',
98 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
99 'IuWvgA'
100 );
101
102 $this->addLink(
103 6,
104 'w3c-markup-validator',
105 'https://dvcs.w3.org/hg/markup-validator/summary',
106 'Mercurial repository for the W3C Validator #private',
107 1,
108 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'),
109 'css html w3c web Mercurial'
110 );
111
112 $this->addLink(
113 4,
114 'UserFriendly - Web Designer',
115 'http://ars.userfriendly.org/cartoons/?id=20121206',
116 'Naming conventions... #private',
117 0,
118 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
119 'dev cartoon web'
120 );
121
122 $this->addLink(
123 1,
124 'UserFriendly - Samba',
125 'http://ars.userfriendly.org/cartoons/?id=20010306',
126 'Tropical printing',
127 0,
128 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
129 'samba cartoon web'
130 );
131
132 $this->addLink(
133 0,
134 'Geek and Poke',
135 'http://geek-and-poke.com/',
136 '',
137 1,
138 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
139 'dev cartoon tag1 tag2 tag3 tag4 '
140 );
141 }
142
143 /**
144 * Adds a new link
145 */
146 protected function addLink(
147 $id,
148 $title,
149 $url,
150 $description,
151 $private,
152 $date,
153 $tags,
154 $updated = '',
155 $shorturl = '',
156 $pinned = false
157 ) {
158 $link = array(
159 'id' => $id,
160 'title' => $title,
161 'url' => $url,
162 'description' => $description,
163 'private' => $private,
164 'tags' => $tags,
165 'created' => $date,
166 'updated' => $updated,
167 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id),
168 'sticky' => $pinned
169 );
170 $this->_links[$id] = $link;
171
172 if ($private) {
173 $this->_privateCount++;
174 return;
175 }
176 $this->_publicCount++;
177 }
178
179 /**
180 * Writes data to the datastore
181 */
182 public function write($filename)
183 {
184 $this->reorder();
185 file_put_contents(
186 $filename,
187 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
188 );
189 }
190
191 /**
192 * Reorder links by creation date (newest first).
193 *
194 * Also update the urls and ids mapping arrays.
195 *
196 * @param string $order ASC|DESC
197 */
198 public function reorder($order = 'DESC')
199 {
200 // backward compatibility: ignore reorder if the the `created` field doesn't exist
201 if (! isset(array_values($this->_links)[0]['created'])) {
202 return;
203 }
204
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;
210 }
211
212 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
213 });
214 }
215
216 /**
217 * Returns the number of links in the reference data
218 */
219 public function countLinks()
220 {
221 return $this->_publicCount + $this->_privateCount;
222 }
223
224 /**
225 * Returns the number of public links in the reference data
226 */
227 public function countPublicLinks()
228 {
229 return $this->_publicCount;
230 }
231
232 /**
233 * Returns the number of private links in the reference data
234 */
235 public function countPrivateLinks()
236 {
237 return $this->_privateCount;
238 }
239
240 /**
241 * Returns the number of links without tag
242 */
243 public function countUntaggedLinks()
244 {
245 $cpt = 0;
246 foreach ($this->_links as $link) {
247 if (empty($link['tags'])) {
248 ++$cpt;
249 }
250 }
251 return $cpt;
252 }
253
254 public function getLinks()
255 {
256 $this->reorder();
257 return $this->_links;
258 }
259
260 /**
261 * Setter to override link creation.
262 *
263 * @param array $links List of links.
264 */
265 public function setLinks($links)
266 {
267 $this->_links = $links;
268 }
269 }