]>
Commit | Line | Data |
---|---|---|
ca74886f | 1 | <?php |
f24896b2 V |
2 | |
3 | use Shaarli\Bookmark\LinkDB; | |
4 | ||
ca74886f V |
5 | /** |
6 | * Populates a reference datastore to test LinkDB | |
7 | */ | |
8 | class ReferenceLinkDB | |
9 | { | |
4154c25b | 10 | public static $NB_LINKS_TOTAL = 11; |
89baf23d | 11 | |
07b6fa75 V |
12 | private $_links = array(); |
13 | private $_publicCount = 0; | |
14 | private $_privateCount = 0; | |
ca74886f V |
15 | |
16 | /** | |
17 | * Populates the test DB with reference data | |
18 | */ | |
735ed4a9 | 19 | public function __construct() |
ca74886f | 20 | { |
4154c25b A |
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 | ||
89baf23d | 47 | $this->addLink( |
c3dfd899 | 48 | 41, |
89baf23d A |
49 | 'Link title: @website', |
50 | '?WDWyig', | |
9ccca401 | 51 | 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', |
89baf23d | 52 | 0, |
d592daea | 53 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), |
c3dfd899 A |
54 | 'sTuff', |
55 | null, | |
56 | 'WDWyig' | |
89baf23d A |
57 | ); |
58 | ||
ca74886f | 59 | $this->addLink( |
c3dfd899 A |
60 | 42, |
61 | 'Note: I have a big ID but an old date', | |
62 | '?WDWyig', | |
63 | 'Used to test links reordering.', | |
64 | 0, | |
d592daea | 65 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'), |
c3dfd899 A |
66 | 'ut' |
67 | ); | |
68 | ||
7d86f40b A |
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 | ||
c3dfd899 A |
79 | $this->addLink( |
80 | 8, | |
522b278b | 81 | 'Free as in Freedom 2.0 @website', |
ca74886f | 82 | 'https://static.fsf.org/nosvn/faif-2.0.pdf', |
9ccca401 | 83 | 'Richard Stallman and the Free Software Revolution. Read this. #hashtag', |
ca74886f | 84 | 0, |
d592daea | 85 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), |
c6d876bb | 86 | 'free gnu software stallman -exclude stuff hashtag', |
d592daea | 87 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033') |
ca74886f V |
88 | ); |
89 | ||
90 | $this->addLink( | |
c3dfd899 | 91 | 7, |
ca74886f V |
92 | 'MediaGoblin', |
93 | 'http://mediagoblin.org/', | |
9ccca401 | 94 | 'A free software media publishing platform #hashtagOther', |
ca74886f | 95 | 0, |
d592daea | 96 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'), |
c3dfd899 | 97 | 'gnu media web .hidden hashtag', |
68016e37 | 98 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130615_184230'), |
c3dfd899 | 99 | 'IuWvgA' |
ca74886f V |
100 | ); |
101 | ||
102 | $this->addLink( | |
c3dfd899 | 103 | 6, |
ca74886f V |
104 | 'w3c-markup-validator', |
105 | 'https://dvcs.w3.org/hg/markup-validator/summary', | |
9ccca401 | 106 | 'Mercurial repository for the W3C Validator #private', |
ca74886f | 107 | 1, |
d592daea | 108 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'), |
ca74886f V |
109 | 'css html w3c web Mercurial' |
110 | ); | |
111 | ||
112 | $this->addLink( | |
c3dfd899 | 113 | 4, |
ca74886f V |
114 | 'UserFriendly - Web Designer', |
115 | 'http://ars.userfriendly.org/cartoons/?id=20121206', | |
9ccca401 | 116 | 'Naming conventions... #private', |
ca74886f | 117 | 0, |
d592daea | 118 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), |
ca74886f V |
119 | 'dev cartoon web' |
120 | ); | |
121 | ||
122 | $this->addLink( | |
c3dfd899 | 123 | 1, |
ca74886f V |
124 | 'UserFriendly - Samba', |
125 | 'http://ars.userfriendly.org/cartoons/?id=20010306', | |
126 | 'Tropical printing', | |
127 | 0, | |
d592daea | 128 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), |
ca74886f V |
129 | 'samba cartoon web' |
130 | ); | |
131 | ||
132 | $this->addLink( | |
c3dfd899 | 133 | 0, |
ca74886f V |
134 | 'Geek and Poke', |
135 | 'http://geek-and-poke.com/', | |
136 | '', | |
137 | 1, | |
d592daea | 138 | DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), |
9866b408 | 139 | 'dev cartoon tag1 tag2 tag3 tag4 ' |
ca74886f V |
140 | ); |
141 | } | |
142 | ||
143 | /** | |
144 | * Adds a new link | |
145 | */ | |
4154c25b A |
146 | protected function addLink( |
147 | $id, | |
148 | $title, | |
149 | $url, | |
150 | $description, | |
151 | $private, | |
152 | $date, | |
153 | $tags, | |
154 | $updated = '', | |
155 | $shorturl = '', | |
067c2dd8 V |
156 | $pinned = false |
157 | ) { | |
ca74886f | 158 | $link = array( |
c3dfd899 | 159 | 'id' => $id, |
ca74886f V |
160 | 'title' => $title, |
161 | 'url' => $url, | |
162 | 'description' => $description, | |
163 | 'private' => $private, | |
ca74886f | 164 | 'tags' => $tags, |
c3dfd899 | 165 | 'created' => $date, |
c6d876bb | 166 | 'updated' => $updated, |
d592daea | 167 | 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id), |
4154c25b | 168 | 'sticky' => $pinned |
ca74886f | 169 | ); |
c3dfd899 | 170 | $this->_links[$id] = $link; |
ca74886f V |
171 | |
172 | if ($private) { | |
07b6fa75 | 173 | $this->_privateCount++; |
ca74886f V |
174 | return; |
175 | } | |
07b6fa75 | 176 | $this->_publicCount++; |
ca74886f V |
177 | } |
178 | ||
179 | /** | |
180 | * Writes data to the datastore | |
181 | */ | |
9c8752a2 | 182 | public function write($filename) |
ca74886f | 183 | { |
9ec0a611 | 184 | $this->reorder(); |
ca74886f V |
185 | file_put_contents( |
186 | $filename, | |
07b6fa75 | 187 | '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' |
ca74886f V |
188 | ); |
189 | } | |
190 | ||
9ec0a611 A |
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. | |
067c2dd8 | 207 | usort($this->_links, function ($a, $b) use ($order) { |
4154c25b A |
208 | if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) { |
209 | return $a['sticky'] ? -1 : 1; | |
210 | } | |
211 | ||
9ec0a611 A |
212 | return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; |
213 | }); | |
214 | } | |
215 | ||
ca74886f V |
216 | /** |
217 | * Returns the number of links in the reference data | |
218 | */ | |
219 | public function countLinks() | |
220 | { | |
07b6fa75 | 221 | return $this->_publicCount + $this->_privateCount; |
ca74886f V |
222 | } |
223 | ||
224 | /** | |
225 | * Returns the number of public links in the reference data | |
226 | */ | |
227 | public function countPublicLinks() | |
228 | { | |
07b6fa75 | 229 | return $this->_publicCount; |
ca74886f V |
230 | } |
231 | ||
232 | /** | |
233 | * Returns the number of private links in the reference data | |
234 | */ | |
235 | public function countPrivateLinks() | |
236 | { | |
07b6fa75 | 237 | return $this->_privateCount; |
ca74886f | 238 | } |
822bffce | 239 | |
7d86f40b A |
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 | ||
822bffce A |
254 | public function getLinks() |
255 | { | |
9ec0a611 | 256 | $this->reorder(); |
822bffce A |
257 | return $this->_links; |
258 | } | |
c3dfd899 A |
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 | } | |
ca74886f | 269 | } |