]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - utils/ReferenceLinkDB.php
3 * Populates a reference datastore to test LinkDB
7 public static $NB_LINKS_TOTAL = 11;
9 private $_links = array();
10 private $_publicCount = 0;
11 private $_privateCount = 0;
14 * Populates the test DB with reference data
16 public function __construct()
22 'This is an older pinned link',
24 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20100309_101010'),
35 'This is a pinned link',
37 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121207_152312'),
46 'Link title: @website',
48 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
50 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114651'),
58 'Note: I have a big ID but an old date',
60 'Used to test links reordering.',
62 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20100310_101010'),
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.',
72 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_152312'),
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',
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')
90 'http://mediagoblin.org/',
91 'A free software media publishing platform #hashtagOther',
93 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
94 'gnu media web .hidden hashtag',
95 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
101 'w3c-markup-validator',
102 'https://dvcs.w3.org/hg/markup-validator/summary',
103 'Mercurial repository for the W3C Validator #private',
105 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20141125_084734'),
106 'css html w3c web Mercurial'
111 'UserFriendly - Web Designer',
112 'http://ars.userfriendly.org/cartoons/?id=20121206',
113 'Naming conventions... #private',
115 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_142300'),
121 'UserFriendly - Samba',
122 'http://ars.userfriendly.org/cartoons/?id=20010306',
125 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_172539'),
132 'http://geek-and-poke.com/',
135 DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_182539'),
136 'dev cartoon tag1 tag2 tag3 tag4 '
143 protected function addLink(
159 'description' => $description,
160 'private' => $private,
163 'updated' => $updated,
164 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB
::LINK_DATE_FORMAT
) . $id),
167 $this->_links
[$id] = $link;
170 $this->_privateCount++
;
173 $this->_publicCount++
;
177 * Writes data to the datastore
179 public function write($filename)
184 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links
))).' */ ?>'
189 * Reorder links by creation date (newest first).
191 * Also update the urls and ids mapping arrays.
193 * @param string $order ASC|DESC
195 public function reorder($order = 'DESC')
197 // backward compatibility: ignore reorder if the the `created` field doesn't exist
198 if (! isset(array_values($this->_links
)[0]['created'])) {
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;
209 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
214 * Returns the number of links in the reference data
216 public function countLinks()
218 return $this->_publicCount +
$this->_privateCount
;
222 * Returns the number of public links in the reference data
224 public function countPublicLinks()
226 return $this->_publicCount
;
230 * Returns the number of private links in the reference data
232 public function countPrivateLinks()
234 return $this->_privateCount
;
238 * Returns the number of links without tag
240 public function countUntaggedLinks()
243 foreach ($this->_links
as $link) {
244 if (empty($link['tags'])) {
251 public function getLinks()
254 return $this->_links
;
258 * Setter to override link creation.
260 * @param array $links List of links.
262 public function setLinks($links)
264 $this->_links
= $links;