]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/utils/ReferenceLinkDB.php
Empty tag search will look for not tagged links
[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 = 9;
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 41,
20 'Link title: @website',
21 '?WDWyig',
22 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
23 0,
24 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'),
25 'sTuff',
26 null,
27 'WDWyig'
28 );
29
30 $this->addLink(
31 42,
32 'Note: I have a big ID but an old date',
33 '?WDWyig',
34 'Used to test links reordering.',
35 0,
36 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'),
37 'ut'
38 );
39
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
50 $this->addLink(
51 8,
52 'Free as in Freedom 2.0 @website',
53 'https://static.fsf.org/nosvn/faif-2.0.pdf',
54 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
55 0,
56 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'),
57 'free gnu software stallman -exclude stuff hashtag',
58 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')
59 );
60
61 $this->addLink(
62 7,
63 'MediaGoblin',
64 'http://mediagoblin.org/',
65 'A free software media publishing platform #hashtagOther',
66 0,
67 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
68 'gnu media web .hidden hashtag',
69 null,
70 'IuWvgA'
71 );
72
73 $this->addLink(
74 6,
75 'w3c-markup-validator',
76 'https://dvcs.w3.org/hg/markup-validator/summary',
77 'Mercurial repository for the W3C Validator #private',
78 1,
79 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'),
80 'css html w3c web Mercurial'
81 );
82
83 $this->addLink(
84 4,
85 'UserFriendly - Web Designer',
86 'http://ars.userfriendly.org/cartoons/?id=20121206',
87 'Naming conventions... #private',
88 0,
89 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
90 'dev cartoon web'
91 );
92
93 $this->addLink(
94 1,
95 'UserFriendly - Samba',
96 'http://ars.userfriendly.org/cartoons/?id=20010306',
97 'Tropical printing',
98 0,
99 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
100 'samba cartoon web'
101 );
102
103 $this->addLink(
104 0,
105 'Geek and Poke',
106 'http://geek-and-poke.com/',
107 '',
108 1,
109 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
110 'dev cartoon tag1 tag2 tag3 tag4 '
111 );
112 }
113
114 /**
115 * Adds a new link
116 */
117 protected function addLink($id, $title, $url, $description, $private, $date, $tags, $updated = '', $shorturl = '')
118 {
119 $link = array(
120 'id' => $id,
121 'title' => $title,
122 'url' => $url,
123 'description' => $description,
124 'private' => $private,
125 'tags' => $tags,
126 'created' => $date,
127 'updated' => $updated,
128 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id),
129 );
130 $this->_links[$id] = $link;
131
132 if ($private) {
133 $this->_privateCount++;
134 return;
135 }
136 $this->_publicCount++;
137 }
138
139 /**
140 * Writes data to the datastore
141 */
142 public function write($filename)
143 {
144 file_put_contents(
145 $filename,
146 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
147 );
148 }
149
150 /**
151 * Returns the number of links in the reference data
152 */
153 public function countLinks()
154 {
155 return $this->_publicCount + $this->_privateCount;
156 }
157
158 /**
159 * Returns the number of public links in the reference data
160 */
161 public function countPublicLinks()
162 {
163 return $this->_publicCount;
164 }
165
166 /**
167 * Returns the number of private links in the reference data
168 */
169 public function countPrivateLinks()
170 {
171 return $this->_privateCount;
172 }
173
174 /**
175 * Returns the number of links without tag
176 */
177 public function countUntaggedLinks()
178 {
179 $cpt = 0;
180 foreach ($this->_links as $link) {
181 if (empty($link['tags'])) {
182 ++$cpt;
183 }
184 }
185 return $cpt;
186 }
187
188 public function getLinks()
189 {
190 return $this->_links;
191 }
192
193 /**
194 * Setter to override link creation.
195 *
196 * @param array $links List of links.
197 */
198 public function setLinks($links)
199 {
200 $this->_links = $links;
201 }
202 }