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