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