2015-03-12 00:43:02 +01:00
|
|
|
<?php
|
2018-12-03 01:10:39 +01:00
|
|
|
|
|
|
|
use Shaarli\Bookmark\LinkDB;
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
|
|
|
* Populates a reference datastore to test LinkDB
|
|
|
|
*/
|
|
|
|
class ReferenceLinkDB
|
|
|
|
{
|
2018-05-22 22:44:38 +02:00
|
|
|
public static $NB_LINKS_TOTAL = 11;
|
2016-03-12 17:54:56 +01:00
|
|
|
|
2015-07-09 21:07:37 +02:00
|
|
|
private $_links = array();
|
|
|
|
private $_publicCount = 0;
|
|
|
|
private $_privateCount = 0;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates the test DB with reference data
|
|
|
|
*/
|
2016-10-20 21:24:39 +02:00
|
|
|
public function __construct()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2018-05-22 22:44:38 +02:00
|
|
|
$this->addLink(
|
|
|
|
11,
|
|
|
|
'Pined older',
|
|
|
|
'?PCRizQ',
|
|
|
|
'This is an older pinned link',
|
|
|
|
0,
|
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100309_101010'),
|
|
|
|
'',
|
|
|
|
null,
|
|
|
|
'PCRizQ',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addLink(
|
|
|
|
10,
|
|
|
|
'Pined',
|
|
|
|
'?0gCTjQ',
|
|
|
|
'This is a pinned link',
|
|
|
|
0,
|
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121207_152312'),
|
|
|
|
'',
|
|
|
|
null,
|
|
|
|
'0gCTjQ',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->addLink(
|
2016-11-28 16:17:25 +01:00
|
|
|
41,
|
2016-03-12 17:54:56 +01:00
|
|
|
'Link title: @website',
|
|
|
|
'?WDWyig',
|
2016-05-10 23:18:04 +02:00
|
|
|
'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
|
2016-03-12 17:54:56 +01:00
|
|
|
0,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'),
|
2016-11-28 16:17:25 +01:00
|
|
|
'sTuff',
|
|
|
|
null,
|
|
|
|
'WDWyig'
|
2016-03-12 17:54:56 +01:00
|
|
|
);
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
$this->addLink(
|
2016-11-28 16:17:25 +01:00
|
|
|
42,
|
|
|
|
'Note: I have a big ID but an old date',
|
|
|
|
'?WDWyig',
|
|
|
|
'Used to test links reordering.',
|
|
|
|
0,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'),
|
2016-11-28 16:17:25 +01:00
|
|
|
'ut'
|
|
|
|
);
|
|
|
|
|
2017-04-01 12:17:37 +02:00
|
|
|
$this->addLink(
|
|
|
|
9,
|
|
|
|
'PSR-2: Coding Style Guide',
|
|
|
|
'http://www.php-fig.org/psr/psr-2/',
|
|
|
|
'This guide extends and expands on PSR-1, the basic coding standard.',
|
|
|
|
0,
|
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_152312'),
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->addLink(
|
|
|
|
8,
|
2016-02-02 19:42:48 +01:00
|
|
|
'Free as in Freedom 2.0 @website',
|
2015-03-12 00:43:02 +01:00
|
|
|
'https://static.fsf.org/nosvn/faif-2.0.pdf',
|
2016-05-10 23:18:04 +02:00
|
|
|
'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
|
2015-03-12 00:43:02 +01:00
|
|
|
0,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'),
|
2016-08-03 09:45:28 +02:00
|
|
|
'free gnu software stallman -exclude stuff hashtag',
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')
|
2015-03-12 00:43:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->addLink(
|
2016-11-28 16:17:25 +01:00
|
|
|
7,
|
2015-03-12 00:43:02 +01:00
|
|
|
'MediaGoblin',
|
|
|
|
'http://mediagoblin.org/',
|
2016-05-10 23:18:04 +02:00
|
|
|
'A free software media publishing platform #hashtagOther',
|
2015-03-12 00:43:02 +01:00
|
|
|
0,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
|
2016-11-28 16:17:25 +01:00
|
|
|
'gnu media web .hidden hashtag',
|
2017-01-05 15:58:24 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
|
2016-11-28 16:17:25 +01:00
|
|
|
'IuWvgA'
|
2015-03-12 00:43:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->addLink(
|
2016-11-28 16:17:25 +01:00
|
|
|
6,
|
2015-03-12 00:43:02 +01:00
|
|
|
'w3c-markup-validator',
|
|
|
|
'https://dvcs.w3.org/hg/markup-validator/summary',
|
2016-05-10 23:18:04 +02:00
|
|
|
'Mercurial repository for the W3C Validator #private',
|
2015-03-12 00:43:02 +01:00
|
|
|
1,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'),
|
2015-03-12 00:43:02 +01:00
|
|
|
'css html w3c web Mercurial'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addLink(
|
2016-11-28 16:17:25 +01:00
|
|
|
4,
|
2015-03-12 00:43:02 +01:00
|
|
|
'UserFriendly - Web Designer',
|
|
|
|
'http://ars.userfriendly.org/cartoons/?id=20121206',
|
2016-05-10 23:18:04 +02:00
|
|
|
'Naming conventions... #private',
|
2015-03-12 00:43:02 +01:00
|
|
|
0,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
|
2015-03-12 00:43:02 +01:00
|
|
|
'dev cartoon web'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addLink(
|
2016-11-28 16:17:25 +01:00
|
|
|
1,
|
2015-03-12 00:43:02 +01:00
|
|
|
'UserFriendly - Samba',
|
|
|
|
'http://ars.userfriendly.org/cartoons/?id=20010306',
|
|
|
|
'Tropical printing',
|
|
|
|
0,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
|
2015-03-12 00:43:02 +01:00
|
|
|
'samba cartoon web'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addLink(
|
2016-11-28 16:17:25 +01:00
|
|
|
0,
|
2015-03-12 00:43:02 +01:00
|
|
|
'Geek and Poke',
|
|
|
|
'http://geek-and-poke.com/',
|
|
|
|
'',
|
|
|
|
1,
|
2016-11-28 18:24:15 +01:00
|
|
|
DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
|
2016-08-02 10:34:21 +02:00
|
|
|
'dev cartoon tag1 tag2 tag3 tag4 '
|
2015-03-12 00:43:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new link
|
|
|
|
*/
|
2018-05-22 22:44:38 +02:00
|
|
|
protected function addLink(
|
|
|
|
$id,
|
|
|
|
$title,
|
|
|
|
$url,
|
|
|
|
$description,
|
|
|
|
$private,
|
|
|
|
$date,
|
|
|
|
$tags,
|
|
|
|
$updated = '',
|
|
|
|
$shorturl = '',
|
2018-10-13 00:35:47 +02:00
|
|
|
$pinned = false
|
|
|
|
) {
|
2015-03-12 00:43:02 +01:00
|
|
|
$link = array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => $id,
|
2015-03-12 00:43:02 +01:00
|
|
|
'title' => $title,
|
|
|
|
'url' => $url,
|
|
|
|
'description' => $description,
|
|
|
|
'private' => $private,
|
|
|
|
'tags' => $tags,
|
2016-11-28 16:17:25 +01:00
|
|
|
'created' => $date,
|
2016-08-03 09:45:28 +02:00
|
|
|
'updated' => $updated,
|
2016-11-28 18:24:15 +01:00
|
|
|
'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id),
|
2018-05-22 22:44:38 +02:00
|
|
|
'sticky' => $pinned
|
2015-03-12 00:43:02 +01:00
|
|
|
);
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->_links[$id] = $link;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
|
|
|
if ($private) {
|
2015-07-09 21:07:37 +02:00
|
|
|
$this->_privateCount++;
|
2015-03-12 00:43:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-07-09 21:07:37 +02:00
|
|
|
$this->_publicCount++;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes data to the datastore
|
|
|
|
*/
|
2015-06-24 23:01:21 +02:00
|
|
|
public function write($filename)
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2017-09-02 15:10:44 +02:00
|
|
|
$this->reorder();
|
2015-03-12 00:43:02 +01:00
|
|
|
file_put_contents(
|
|
|
|
$filename,
|
2015-07-09 21:07:37 +02:00
|
|
|
'<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
|
2015-03-12 00:43:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-02 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Reorder links by creation date (newest first).
|
|
|
|
*
|
|
|
|
* Also update the urls and ids mapping arrays.
|
|
|
|
*
|
|
|
|
* @param string $order ASC|DESC
|
|
|
|
*/
|
|
|
|
public function reorder($order = 'DESC')
|
|
|
|
{
|
|
|
|
// backward compatibility: ignore reorder if the the `created` field doesn't exist
|
|
|
|
if (! isset(array_values($this->_links)[0]['created'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$order = $order === 'ASC' ? -1 : 1;
|
|
|
|
// Reorder array by dates.
|
2018-10-13 00:35:47 +02:00
|
|
|
usort($this->_links, function ($a, $b) use ($order) {
|
2018-05-22 22:44:38 +02:00
|
|
|
if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
|
|
|
|
return $a['sticky'] ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2017-09-02 15:10:44 +02:00
|
|
|
return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of links in the reference data
|
|
|
|
*/
|
|
|
|
public function countLinks()
|
|
|
|
{
|
2015-07-09 21:07:37 +02:00
|
|
|
return $this->_publicCount + $this->_privateCount;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of public links in the reference data
|
|
|
|
*/
|
|
|
|
public function countPublicLinks()
|
|
|
|
{
|
2015-07-09 21:07:37 +02:00
|
|
|
return $this->_publicCount;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of private links in the reference data
|
|
|
|
*/
|
|
|
|
public function countPrivateLinks()
|
|
|
|
{
|
2015-07-09 21:07:37 +02:00
|
|
|
return $this->_privateCount;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
2015-12-27 10:08:20 +01:00
|
|
|
|
2017-04-01 12:17:37 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of links without tag
|
|
|
|
*/
|
|
|
|
public function countUntaggedLinks()
|
|
|
|
{
|
|
|
|
$cpt = 0;
|
|
|
|
foreach ($this->_links as $link) {
|
|
|
|
if (empty($link['tags'])) {
|
|
|
|
++$cpt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $cpt;
|
|
|
|
}
|
|
|
|
|
2015-12-27 10:08:20 +01:00
|
|
|
public function getLinks()
|
|
|
|
{
|
2017-09-02 15:10:44 +02:00
|
|
|
$this->reorder();
|
2015-12-27 10:08:20 +01:00
|
|
|
return $this->_links;
|
|
|
|
}
|
2016-11-28 16:17:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter to override link creation.
|
|
|
|
*
|
|
|
|
* @param array $links List of links.
|
|
|
|
*/
|
|
|
|
public function setLinks($links)
|
|
|
|
{
|
|
|
|
$this->_links = $links;
|
|
|
|
}
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|