2015-03-12 00:43:02 +01:00
|
|
|
<?php
|
2018-12-03 01:10:39 +01:00
|
|
|
|
2021-04-05 11:00:28 +02:00
|
|
|
namespace Shaarli\Tests\Utils;
|
|
|
|
|
|
|
|
use DateTime;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\Bookmark\Bookmark;
|
|
|
|
use Shaarli\Bookmark\BookmarkArray;
|
2018-12-03 01:10:39 +01:00
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Populates a reference datastore to test Bookmark
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
|
|
|
class ReferenceLinkDB
|
|
|
|
{
|
2018-05-22 22:44:38 +02:00
|
|
|
public static $NB_LINKS_TOTAL = 11;
|
2016-03-12 17:54:56 +01:00
|
|
|
|
2021-04-05 11:00:28 +02:00
|
|
|
protected $bookmarks = [];
|
|
|
|
protected $publicCount = 0;
|
|
|
|
protected $privateCount = 0;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
2021-04-05 11:00:28 +02:00
|
|
|
protected $isLegacy;
|
2020-01-17 21:34:12 +01:00
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
|
|
|
* Populates the test DB with reference data
|
2020-01-17 21:34:12 +01:00
|
|
|
*
|
|
|
|
* @param bool $isLegacy Use links as array instead of Bookmark object
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
2020-01-17 21:34:12 +01:00
|
|
|
public function __construct($isLegacy = false)
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
$this->isLegacy = $isLegacy;
|
|
|
|
if (! $this->isLegacy) {
|
|
|
|
$this->bookmarks = new BookmarkArray();
|
|
|
|
}
|
2018-05-22 22:44:38 +02:00
|
|
|
$this->addLink(
|
|
|
|
11,
|
|
|
|
'Pined older',
|
2020-07-28 22:24:41 +02:00
|
|
|
'/shaare/PCRizQ',
|
2018-05-22 22:44:38 +02:00
|
|
|
'This is an older pinned link',
|
|
|
|
0,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100309_101010'),
|
2018-05-22 22:44:38 +02:00
|
|
|
'',
|
|
|
|
null,
|
|
|
|
'PCRizQ',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addLink(
|
|
|
|
10,
|
|
|
|
'Pined',
|
2020-07-28 22:24:41 +02:00
|
|
|
'/shaare/0gCTjQ',
|
2018-05-22 22:44:38 +02:00
|
|
|
'This is a pinned link',
|
|
|
|
0,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121207_152312'),
|
2018-05-22 22:44:38 +02:00
|
|
|
'',
|
|
|
|
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',
|
2020-07-28 22:24:41 +02:00
|
|
|
'/shaare/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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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',
|
2020-07-28 22:24:41 +02:00
|
|
|
'/shaare/WDWyig',
|
2020-01-17 21:34:12 +01:00
|
|
|
'Used to test bookmarks reordering.',
|
2016-11-28 16:17:25 +01:00
|
|
|
0,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'),
|
2020-10-12 12:23:57 +02:00
|
|
|
'coding-style standards quality assurance'
|
2017-04-01 12:17:37 +02:00
|
|
|
);
|
|
|
|
|
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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'),
|
2016-08-03 09:45:28 +02:00
|
|
|
'free gnu software stallman -exclude stuff hashtag',
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
|
2016-11-28 16:17:25 +01:00
|
|
|
'gnu media web .hidden hashtag',
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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,
|
2020-01-17 21:34:12 +01:00
|
|
|
DateTime::createFromFormat(Bookmark::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
|
|
|
|
) {
|
2021-04-05 09:39:34 +02:00
|
|
|
$link = [
|
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,
|
2020-01-17 21:34:12 +01:00
|
|
|
'shorturl' => $shorturl ? $shorturl : smallHash($date->format(Bookmark::LINK_DATE_FORMAT) . $id),
|
2018-05-22 22:44:38 +02:00
|
|
|
'sticky' => $pinned
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2020-01-17 21:34:12 +01:00
|
|
|
if (! $this->isLegacy) {
|
|
|
|
$bookmark = new Bookmark();
|
|
|
|
$this->bookmarks[$id] = $bookmark->fromArray($link);
|
|
|
|
} else {
|
|
|
|
$this->bookmarks[$id] = $link;
|
|
|
|
}
|
2015-03-12 00:43:02 +01:00
|
|
|
|
|
|
|
if ($private) {
|
2021-04-05 11:00:28 +02:00
|
|
|
$this->privateCount++;
|
2015-03-12 00:43:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-04-05 11:00:28 +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,
|
2021-04-05 09:39:34 +02:00
|
|
|
'<?php /* ' . base64_encode(gzdeflate(serialize($this->bookmarks))) . ' */ ?>'
|
2015-03-12 00:43:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-02 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Reorder links by creation date (newest first).
|
|
|
|
*
|
|
|
|
* @param string $order ASC|DESC
|
|
|
|
*/
|
|
|
|
public function reorder($order = 'DESC')
|
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
if (! $this->isLegacy) {
|
|
|
|
$this->bookmarks->reorder($order);
|
|
|
|
} else {
|
|
|
|
$order = $order === 'ASC' ? -1 : 1;
|
|
|
|
// backward compatibility: ignore reorder if the the `created` field doesn't exist
|
|
|
|
if (! isset(array_values($this->bookmarks)[0]['created'])) {
|
|
|
|
return;
|
2018-05-22 22:44:38 +02:00
|
|
|
}
|
|
|
|
|
2020-01-17 21:34:12 +01:00
|
|
|
usort($this->bookmarks, function ($a, $b) use ($order) {
|
|
|
|
if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
|
|
|
|
return $a['sticky'] ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
|
|
|
|
});
|
|
|
|
}
|
2017-09-02 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Returns the number of bookmarks in the reference data
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
|
|
|
public function countLinks()
|
|
|
|
{
|
2021-04-05 11:00:28 +02:00
|
|
|
return $this->publicCount + $this->privateCount;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Returns the number of public bookmarks in the reference data
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
|
|
|
public function countPublicLinks()
|
|
|
|
{
|
2021-04-05 11:00:28 +02:00
|
|
|
return $this->publicCount;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Returns the number of private bookmarks in the reference data
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
|
|
|
public function countPrivateLinks()
|
|
|
|
{
|
2021-04-05 11:00:28 +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
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Returns the number of bookmarks without tag
|
2017-04-01 12:17:37 +02:00
|
|
|
*/
|
|
|
|
public function countUntaggedLinks()
|
|
|
|
{
|
|
|
|
$cpt = 0;
|
2020-01-17 21:34:12 +01:00
|
|
|
foreach ($this->bookmarks as $link) {
|
|
|
|
if (! $this->isLegacy) {
|
|
|
|
if (empty($link->getTags())) {
|
|
|
|
++$cpt;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (empty($link['tags'])) {
|
|
|
|
++$cpt;
|
|
|
|
}
|
2017-04-01 12:17:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $cpt;
|
|
|
|
}
|
|
|
|
|
2015-12-27 10:08:20 +01:00
|
|
|
public function getLinks()
|
|
|
|
{
|
2017-09-02 15:10:44 +02:00
|
|
|
$this->reorder();
|
2020-01-17 21:34:12 +01:00
|
|
|
return $this->bookmarks;
|
2015-12-27 10:08:20 +01:00
|
|
|
}
|
2016-11-28 16:17:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter to override link creation.
|
|
|
|
*
|
2020-01-17 21:34:12 +01:00
|
|
|
* @param array $links List of bookmarks.
|
2016-11-28 16:17:25 +01:00
|
|
|
*/
|
|
|
|
public function setLinks($links)
|
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
$this->bookmarks = $links;
|
2016-11-28 16:17:25 +01:00
|
|
|
}
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|