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