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