]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/utils/ReferenceLinkDB.php
3 * Populates a reference datastore to test LinkDB
7 public static $NB_LINKS_TOTAL = 9;
9 private $_links = array();
10 private $_publicCount = 0;
11 private $_privateCount = 0;
14 * Populates the test DB with reference data
16 public function __construct()
20 'Link title: @website',
22 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
24 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114651'),
32 'Note: I have a big ID but an old date',
34 'Used to test links reordering.',
36 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20100310_101010'),
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.',
46 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_152312'),
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',
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')
64 'http://mediagoblin.org/',
65 'A free software media publishing platform #hashtagOther',
67 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
68 'gnu media web .hidden hashtag',
69 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
75 'w3c-markup-validator',
76 'https://dvcs.w3.org/hg/markup-validator/summary',
77 'Mercurial repository for the W3C Validator #private',
79 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20141125_084734'),
80 'css html w3c web Mercurial'
85 'UserFriendly - Web Designer',
86 'http://ars.userfriendly.org/cartoons/?id=20121206',
87 'Naming conventions... #private',
89 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_142300'),
95 'UserFriendly - Samba',
96 'http://ars.userfriendly.org/cartoons/?id=20010306',
99 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_172539'),
106 'http://geek-and-poke.com/',
109 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_182539'),
110 'dev cartoon tag1 tag2 tag3 tag4 '
117 protected function addLink($id, $title, $url, $description, $private, $date, $tags, $updated = '', $shorturl = '')
123 'description' => $description,
124 'private' => $private,
127 'updated' => $updated,
128 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB
::LINK_DATE_FORMAT
) . $id),
130 $this->_links
[$id] = $link;
133 $this->_privateCount++
;
136 $this->_publicCount++
;
140 * Writes data to the datastore
142 public function write($filename)
147 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links
))).' */ ?>'
152 * Reorder links by creation date (newest first).
154 * Also update the urls and ids mapping arrays.
156 * @param string $order ASC|DESC
158 public function reorder($order = 'DESC')
160 // backward compatibility: ignore reorder if the the `created` field doesn't exist
161 if (! isset(array_values($this->_links
)[0]['created'])) {
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;
173 * Returns the number of links in the reference data
175 public function countLinks()
177 return $this->_publicCount +
$this->_privateCount
;
181 * Returns the number of public links in the reference data
183 public function countPublicLinks()
185 return $this->_publicCount
;
189 * Returns the number of private links in the reference data
191 public function countPrivateLinks()
193 return $this->_privateCount
;
197 * Returns the number of links without tag
199 public function countUntaggedLinks()
202 foreach ($this->_links
as $link) {
203 if (empty($link['tags'])) {
210 public function getLinks()
213 return $this->_links
;
217 * Setter to override link creation.
219 * @param array $links List of links.
221 public function setLinks($links)
223 $this->_links
= $links;