2015-03-12 00:43:02 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Data storage for links.
|
|
|
|
*
|
|
|
|
* This object behaves like an associative array.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* $myLinks = new LinkDB();
|
2016-11-28 15:30:17 +01:00
|
|
|
* echo $myLinks[350]['title'];
|
2015-03-12 00:43:02 +01:00
|
|
|
* foreach ($myLinks as $link)
|
|
|
|
* echo $link['title'].' at url '.$link['url'].'; description:'.$link['description'];
|
|
|
|
*
|
|
|
|
* Available keys:
|
2016-11-28 15:30:17 +01:00
|
|
|
* - id: primary key, incremental integer identifier (persistent)
|
2015-03-12 00:43:02 +01:00
|
|
|
* - description: description of the entry
|
2016-11-28 15:30:17 +01:00
|
|
|
* - created: creation date of this entry, DateTime object.
|
|
|
|
* - updated: last modification date of this entry, DateTime object.
|
2015-03-12 00:43:02 +01:00
|
|
|
* - private: Is this link private? 0=no, other value=yes
|
|
|
|
* - tags: tags attached to this entry (separated by spaces)
|
|
|
|
* - title Title of the link
|
2015-12-22 10:24:31 +01:00
|
|
|
* - url URL of the link. Used for displayable links (no redirector, relative, etc.).
|
|
|
|
* Can be absolute or relative.
|
2015-03-12 00:43:02 +01:00
|
|
|
* Relative URLs are permalinks (e.g.'?m-ukcw')
|
2015-12-22 10:24:31 +01:00
|
|
|
* - real_url Absolute processed URL.
|
2016-11-28 18:24:15 +01:00
|
|
|
* - shorturl Permalink smallhash
|
2015-03-12 00:43:02 +01:00
|
|
|
*
|
|
|
|
* Implements 3 interfaces:
|
|
|
|
* - ArrayAccess: behaves like an associative array;
|
|
|
|
* - Countable: there is a count() method;
|
|
|
|
* - Iterator: usable in foreach () loops.
|
2016-11-28 15:30:17 +01:00
|
|
|
*
|
|
|
|
* ID mechanism:
|
|
|
|
* ArrayAccess is implemented in a way that will allow to access a link
|
|
|
|
* with the unique identifier ID directly with $link[ID].
|
|
|
|
* Note that it's not the real key of the link array attribute.
|
|
|
|
* This mechanism is in place to have persistent link IDs,
|
|
|
|
* even though the internal array is reordered by date.
|
|
|
|
* Example:
|
|
|
|
* - DB: link #1 (2010-01-01) link #2 (2016-01-01)
|
|
|
|
* - Order: #2 #1
|
|
|
|
* - Import links containing: link #3 (2013-01-01)
|
|
|
|
* - New DB: link #1 (2010-01-01) link #2 (2016-01-01) link #3 (2013-01-01)
|
|
|
|
* - Real order: #2 #3 #1
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
|
|
|
class LinkDB implements Iterator, Countable, ArrayAccess
|
|
|
|
{
|
2015-06-24 23:01:21 +02:00
|
|
|
// Links are stored as a PHP serialized string
|
2016-10-20 21:10:56 +02:00
|
|
|
private $datastore;
|
2015-06-24 23:01:21 +02:00
|
|
|
|
2016-02-17 22:46:50 +01:00
|
|
|
// Link date storage format
|
|
|
|
const LINK_DATE_FORMAT = 'Ymd_His';
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
// List of links (associative array)
|
|
|
|
// - key: link date (e.g. "20110823_124546"),
|
|
|
|
// - value: associative array (keys: title, description...)
|
2016-10-20 21:10:56 +02:00
|
|
|
private $links;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
2016-11-28 15:30:17 +01:00
|
|
|
// List of all recorded URLs (key=url, value=link offset)
|
|
|
|
// for fast reserve search (url-->link offset)
|
2016-10-20 21:10:56 +02:00
|
|
|
private $urls;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
2016-11-28 15:30:17 +01:00
|
|
|
/**
|
|
|
|
* @var array List of all links IDS mapped with their array offset.
|
|
|
|
* Map: id->offset.
|
|
|
|
*/
|
|
|
|
protected $ids;
|
|
|
|
|
|
|
|
// List of offset keys (for the Iterator interface implementation)
|
2016-10-20 21:10:56 +02:00
|
|
|
private $keys;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
2016-10-20 21:10:56 +02:00
|
|
|
// Position in the $this->keys array (for the Iterator interface)
|
|
|
|
private $position;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
|
|
|
// Is the user logged in? (used to filter private links)
|
2016-10-20 21:10:56 +02:00
|
|
|
private $loggedIn;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
2015-06-23 22:34:07 +02:00
|
|
|
// Hide public links
|
2016-10-20 21:10:56 +02:00
|
|
|
private $hidePublicLinks;
|
2015-06-23 22:34:07 +02:00
|
|
|
|
2015-09-18 13:26:36 +02:00
|
|
|
// link redirector set in user settings.
|
2016-10-20 21:10:56 +02:00
|
|
|
private $redirector;
|
2015-09-18 13:26:36 +02:00
|
|
|
|
2016-03-24 19:40:12 +01:00
|
|
|
/**
|
|
|
|
* Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* anonym.to needs clean URL while dereferer.org needs urlencoded URL.
|
|
|
|
*
|
|
|
|
* @var boolean $redirectorEncode parameter: true or false
|
|
|
|
*/
|
|
|
|
private $redirectorEncode;
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
|
|
|
* Creates a new LinkDB
|
|
|
|
*
|
|
|
|
* Checks if the datastore exists; else, attempts to create a dummy one.
|
|
|
|
*
|
2016-03-24 19:40:12 +01:00
|
|
|
* @param string $datastore datastore file path.
|
|
|
|
* @param boolean $isLoggedIn is the user logged in?
|
|
|
|
* @param boolean $hidePublicLinks if true all links are private.
|
|
|
|
* @param string $redirector link redirector set in user settings.
|
|
|
|
* @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
2016-10-20 21:24:39 +02:00
|
|
|
public function __construct(
|
2016-03-24 19:40:12 +01:00
|
|
|
$datastore,
|
|
|
|
$isLoggedIn,
|
|
|
|
$hidePublicLinks,
|
|
|
|
$redirector = '',
|
|
|
|
$redirectorEncode = true
|
|
|
|
)
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
$this->datastore = $datastore;
|
|
|
|
$this->loggedIn = $isLoggedIn;
|
|
|
|
$this->hidePublicLinks = $hidePublicLinks;
|
|
|
|
$this->redirector = $redirector;
|
2016-03-24 19:40:12 +01:00
|
|
|
$this->redirectorEncode = $redirectorEncode === true;
|
2016-10-20 21:19:51 +02:00
|
|
|
$this->check();
|
|
|
|
$this->read();
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Countable - Counts elements of an object
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
return count($this->links);
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArrayAccess - Assigns a value to the specified offset
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
|
|
|
// TODO: use exceptions instead of "die"
|
2016-10-20 21:10:56 +02:00
|
|
|
if (!$this->loggedIn) {
|
2015-03-12 00:43:02 +01:00
|
|
|
die('You are not authorized to add a link.');
|
|
|
|
}
|
2016-11-28 15:30:17 +01:00
|
|
|
if (!isset($value['id']) || empty($value['url'])) {
|
|
|
|
die('Internal Error: A link should always have an id and URL.');
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
2017-04-05 19:09:25 +02:00
|
|
|
if (($offset !== null && ! is_int($offset)) || ! is_int($value['id'])) {
|
2016-11-28 15:30:17 +01:00
|
|
|
die('You must specify an integer as a key.');
|
|
|
|
}
|
2017-04-05 19:09:25 +02:00
|
|
|
if ($offset !== null && $offset !== $value['id']) {
|
2016-11-28 15:30:17 +01:00
|
|
|
die('Array offset and link ID must be equal.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the link exists, we reuse the real offset, otherwise new entry
|
|
|
|
$existing = $this->getLinkOffset($offset);
|
|
|
|
if ($existing !== null) {
|
|
|
|
$offset = $existing;
|
|
|
|
} else {
|
|
|
|
$offset = count($this->links);
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
2016-10-20 21:10:56 +02:00
|
|
|
$this->links[$offset] = $value;
|
2016-11-28 15:30:17 +01:00
|
|
|
$this->urls[$value['url']] = $offset;
|
|
|
|
$this->ids[$value['id']] = $offset;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArrayAccess - Whether or not an offset exists
|
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
2016-11-28 15:30:17 +01:00
|
|
|
return array_key_exists($this->getLinkOffset($offset), $this->links);
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArrayAccess - Unsets an offset
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
if (!$this->loggedIn) {
|
2015-03-12 00:43:02 +01:00
|
|
|
// TODO: raise an exception
|
|
|
|
die('You are not authorized to delete a link.');
|
|
|
|
}
|
2016-11-28 15:30:17 +01:00
|
|
|
$realOffset = $this->getLinkOffset($offset);
|
|
|
|
$url = $this->links[$realOffset]['url'];
|
2016-10-20 21:10:56 +02:00
|
|
|
unset($this->urls[$url]);
|
2016-11-28 15:30:17 +01:00
|
|
|
unset($this->ids[$realOffset]);
|
|
|
|
unset($this->links[$realOffset]);
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArrayAccess - Returns the value at specified offset
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
2016-11-28 15:30:17 +01:00
|
|
|
$realOffset = $this->getLinkOffset($offset);
|
|
|
|
return isset($this->links[$realOffset]) ? $this->links[$realOffset] : null;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator - Returns the current element
|
|
|
|
*/
|
2016-10-20 21:24:39 +02:00
|
|
|
public function current()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-11-28 15:30:17 +01:00
|
|
|
return $this[$this->keys[$this->position]];
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator - Returns the key of the current element
|
|
|
|
*/
|
2016-10-20 21:24:39 +02:00
|
|
|
public function key()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
return $this->keys[$this->position];
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator - Moves forward to next element
|
|
|
|
*/
|
2016-10-20 21:24:39 +02:00
|
|
|
public function next()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
++$this->position;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator - Rewinds the Iterator to the first element
|
|
|
|
*
|
|
|
|
* Entries are sorted by date (latest first)
|
|
|
|
*/
|
2016-10-20 21:24:39 +02:00
|
|
|
public function rewind()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-11-28 15:30:17 +01:00
|
|
|
$this->keys = array_keys($this->ids);
|
2016-10-20 21:10:56 +02:00
|
|
|
$this->position = 0;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator - Checks if current position is valid
|
|
|
|
*/
|
2016-10-20 21:24:39 +02:00
|
|
|
public function valid()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
return isset($this->keys[$this->position]);
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the DB directory and file exist
|
|
|
|
*
|
|
|
|
* If no DB file is found, creates a dummy DB.
|
|
|
|
*/
|
2016-10-20 21:19:51 +02:00
|
|
|
private function check()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
if (file_exists($this->datastore)) {
|
2015-03-12 00:43:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a dummy database for example
|
2016-10-20 21:10:56 +02:00
|
|
|
$this->links = array();
|
2015-03-12 00:43:02 +01:00
|
|
|
$link = array(
|
2016-11-28 15:30:17 +01:00
|
|
|
'id' => 1,
|
2015-06-23 20:54:43 +02:00
|
|
|
'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone',
|
|
|
|
'url'=>'https://github.com/shaarli/Shaarli/wiki',
|
|
|
|
'description'=>'Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login.
|
|
|
|
|
|
|
|
To learn how to use Shaarli, consult the link "Help/documentation" at the bottom of this page.
|
|
|
|
|
|
|
|
You use the community supported version of the original Shaarli project, by Sebastien Sauvage.',
|
2015-03-12 00:43:02 +01:00
|
|
|
'private'=>0,
|
2016-11-28 15:30:17 +01:00
|
|
|
'created'=> new DateTime(),
|
2015-03-12 00:43:02 +01:00
|
|
|
'tags'=>'opensource software'
|
|
|
|
);
|
2016-11-28 18:24:15 +01:00
|
|
|
$link['shorturl'] = link_small_hash($link['created'], $link['id']);
|
2016-11-28 15:30:17 +01:00
|
|
|
$this->links[1] = $link;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
|
|
|
$link = array(
|
2016-11-28 15:30:17 +01:00
|
|
|
'id' => 0,
|
2015-03-12 00:43:02 +01:00
|
|
|
'title'=>'My secret stuff... - Pastebin.com',
|
|
|
|
'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
|
2015-06-23 20:54:43 +02:00
|
|
|
'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.',
|
2015-03-12 00:43:02 +01:00
|
|
|
'private'=>1,
|
2016-11-28 15:30:17 +01:00
|
|
|
'created'=> new DateTime('1 minute ago'),
|
2016-11-28 18:24:15 +01:00
|
|
|
'tags'=>'secretstuff',
|
2015-03-12 00:43:02 +01:00
|
|
|
);
|
2016-11-28 18:24:15 +01:00
|
|
|
$link['shorturl'] = link_small_hash($link['created'], $link['id']);
|
2016-11-28 15:30:17 +01:00
|
|
|
$this->links[0] = $link;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
|
|
|
// Write database to disk
|
2016-10-20 21:19:51 +02:00
|
|
|
$this->write();
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads database from disk to memory
|
|
|
|
*/
|
2016-10-20 21:19:51 +02:00
|
|
|
private function read()
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2015-06-23 14:57:54 +02:00
|
|
|
// Public links are hidden and user not logged in => nothing to show
|
2016-10-20 21:10:56 +02:00
|
|
|
if ($this->hidePublicLinks && !$this->loggedIn) {
|
|
|
|
$this->links = array();
|
2015-06-23 14:57:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-16 12:30:18 +01:00
|
|
|
$this->links = FileUtils::readFlatDB($this->datastore, []);
|
2015-03-12 00:43:02 +01:00
|
|
|
|
2016-11-28 15:30:17 +01:00
|
|
|
$toremove = array();
|
|
|
|
foreach ($this->links as $key => &$link) {
|
|
|
|
if (! $this->loggedIn && $link['private'] != 0) {
|
|
|
|
// Transition for not upgraded databases.
|
|
|
|
$toremove[] = $key;
|
|
|
|
continue;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
2016-01-21 20:18:44 +01:00
|
|
|
|
2016-01-12 19:50:48 +01:00
|
|
|
// Sanitize data fields.
|
2015-09-18 13:26:36 +02:00
|
|
|
sanitizeLink($link);
|
2016-01-21 20:18:44 +01:00
|
|
|
|
|
|
|
// Remove private tags if the user is not logged in.
|
2016-10-20 21:10:56 +02:00
|
|
|
if (! $this->loggedIn) {
|
2016-08-02 10:34:21 +02:00
|
|
|
$link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
|
2016-01-21 20:18:44 +01:00
|
|
|
}
|
|
|
|
|
2015-09-18 13:26:36 +02:00
|
|
|
// Do not use the redirector for internal links (Shaarli note URL starting with a '?').
|
2016-10-20 21:10:56 +02:00
|
|
|
if (!empty($this->redirector) && !startsWith($link['url'], '?')) {
|
|
|
|
$link['real_url'] = $this->redirector;
|
2016-03-24 19:40:12 +01:00
|
|
|
if ($this->redirectorEncode) {
|
|
|
|
$link['real_url'] .= urlencode(unescape($link['url']));
|
|
|
|
} else {
|
|
|
|
$link['real_url'] .= $link['url'];
|
|
|
|
}
|
2015-09-18 13:26:36 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$link['real_url'] = $link['url'];
|
|
|
|
}
|
2016-11-28 15:30:17 +01:00
|
|
|
|
|
|
|
// To be able to load links before running the update, and prepare the update
|
|
|
|
if (! isset($link['created'])) {
|
|
|
|
$link['id'] = $link['linkdate'];
|
2016-11-28 18:24:15 +01:00
|
|
|
$link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']);
|
2016-11-28 15:30:17 +01:00
|
|
|
if (! empty($link['updated'])) {
|
2016-11-28 18:24:15 +01:00
|
|
|
$link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']);
|
2016-11-28 15:30:17 +01:00
|
|
|
}
|
2016-11-28 18:24:15 +01:00
|
|
|
$link['shorturl'] = smallHash($link['linkdate']);
|
2016-11-28 15:30:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If user is not logged in, filter private links.
|
|
|
|
foreach ($toremove as $offset) {
|
|
|
|
unset($this->links[$offset]);
|
2015-06-11 13:53:27 +02:00
|
|
|
}
|
2016-11-28 15:30:17 +01:00
|
|
|
|
|
|
|
$this->reorder();
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 22:49:58 +01:00
|
|
|
/**
|
|
|
|
* Saves the database from memory to disk
|
|
|
|
*
|
|
|
|
* @throws IOException the datastore is not writable
|
|
|
|
*/
|
2016-10-20 21:19:51 +02:00
|
|
|
private function write()
|
2015-11-11 22:49:58 +01:00
|
|
|
{
|
2017-01-16 12:30:18 +01:00
|
|
|
FileUtils::writeFlatDB($this->datastore, $this->links);
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
|
|
|
* Saves the database from memory to disk
|
2015-07-09 22:14:39 +02:00
|
|
|
*
|
|
|
|
* @param string $pageCacheDir page cache directory
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
2016-10-20 21:19:51 +02:00
|
|
|
public function save($pageCacheDir)
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
if (!$this->loggedIn) {
|
2015-03-12 00:43:02 +01:00
|
|
|
// TODO: raise an Exception instead
|
|
|
|
die('You are not authorized to change the database.');
|
|
|
|
}
|
2015-11-11 22:49:58 +01:00
|
|
|
|
2016-10-20 21:19:51 +02:00
|
|
|
$this->write();
|
2015-11-11 22:49:58 +01:00
|
|
|
|
2015-07-09 22:14:39 +02:00
|
|
|
invalidateCaches($pageCacheDir);
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the link for a given URL, or False if it does not exist.
|
2015-09-02 13:55:39 +02:00
|
|
|
*
|
|
|
|
* @param string $url URL to search for
|
|
|
|
*
|
|
|
|
* @return mixed the existing link if it exists, else 'false'
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
|
|
|
public function getLinkFromUrl($url)
|
|
|
|
{
|
2016-10-20 21:10:56 +02:00
|
|
|
if (isset($this->urls[$url])) {
|
|
|
|
return $this->links[$this->urls[$url]];
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-21 21:40:49 +01:00
|
|
|
* Returns the shaare corresponding to a smallHash.
|
2015-03-12 00:43:02 +01:00
|
|
|
*
|
2016-03-21 21:40:49 +01:00
|
|
|
* @param string $request QUERY_STRING server parameter.
|
|
|
|
*
|
|
|
|
* @return array $filtered array containing permalink data.
|
|
|
|
*
|
|
|
|
* @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
|
|
|
|
*/
|
|
|
|
public function filterHash($request)
|
|
|
|
{
|
|
|
|
$request = substr($request, 0, 6);
|
2016-10-20 21:10:56 +02:00
|
|
|
$linkFilter = new LinkFilter($this->links);
|
2016-03-21 21:40:49 +01:00
|
|
|
return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of articles for a given day.
|
|
|
|
*
|
|
|
|
* @param string $request day to filter. Format: YYYYMMDD.
|
|
|
|
*
|
|
|
|
* @return array list of shaare found.
|
|
|
|
*/
|
|
|
|
public function filterDay($request) {
|
2016-10-20 21:10:56 +02:00
|
|
|
$linkFilter = new LinkFilter($this->links);
|
2016-03-21 21:40:49 +01:00
|
|
|
return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter links according to search parameters.
|
|
|
|
*
|
|
|
|
* @param array $filterRequest Search request content. Supported keys:
|
|
|
|
* - searchtags: list of tags
|
|
|
|
* - searchterm: term search
|
2015-12-27 10:08:20 +01:00
|
|
|
* @param bool $casesensitive Optional: Perform case sensitive filter
|
2017-01-16 13:57:11 +01:00
|
|
|
* @param string $visibility return only all/private/public links
|
2017-06-01 17:55:26 +02:00
|
|
|
* @param string $untaggedonly return only untagged links
|
2015-03-12 00:43:02 +01:00
|
|
|
*
|
2016-03-21 21:40:49 +01:00
|
|
|
* @return array filtered links, all links if no suitable filter was provided.
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
2017-06-01 17:55:26 +02:00
|
|
|
public function filterSearch($filterRequest = array(), $casesensitive = false, $visibility = 'all', $untaggedonly = false)
|
2016-01-20 22:51:56 +01:00
|
|
|
{
|
2016-03-21 21:40:49 +01:00
|
|
|
// Filter link database according to parameters.
|
2017-04-01 12:17:37 +02:00
|
|
|
$searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
|
|
|
|
$searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
|
2016-03-21 21:40:49 +01:00
|
|
|
|
2017-04-01 12:17:37 +02:00
|
|
|
// Search tags + fullsearch - blank string parameter will return all links.
|
2017-06-01 17:55:26 +02:00
|
|
|
$type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT; // == "vuotext"
|
2017-04-01 12:17:37 +02:00
|
|
|
$request = [$searchtags, $searchterm];
|
2016-03-21 21:40:49 +01:00
|
|
|
|
2016-11-28 15:30:17 +01:00
|
|
|
$linkFilter = new LinkFilter($this);
|
2017-06-01 17:55:26 +02:00
|
|
|
return $linkFilter->filter($type, $request, $casesensitive, $visibility, $untaggedonly);
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-18 20:28:11 +02:00
|
|
|
* Returns the list tags appearing in the links with the given tags
|
|
|
|
* @param $filteringTags: tags selecting the links to consider
|
|
|
|
* @param $visibility: process only all/private/public links
|
|
|
|
* @return: a tag=>linksCount array
|
2015-03-12 00:43:02 +01:00
|
|
|
*/
|
2017-05-18 20:28:11 +02:00
|
|
|
public function linksCountPerTag($filteringTags = [], $visibility = 'all')
|
2015-03-12 00:43:02 +01:00
|
|
|
{
|
2017-05-18 20:28:11 +02:00
|
|
|
$links = empty($filteringTags) ? $this->links : $this->filterSearch(['searchtags' => $filteringTags], false, $visibility);
|
2015-03-12 00:43:02 +01:00
|
|
|
$tags = array();
|
2016-04-14 17:59:37 +02:00
|
|
|
$caseMapping = array();
|
2017-05-18 20:28:11 +02:00
|
|
|
foreach ($links as $link) {
|
2016-05-19 20:33:45 +02:00
|
|
|
foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) {
|
2016-04-14 17:59:37 +02:00
|
|
|
if (empty($tag)) {
|
|
|
|
continue;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
2016-04-14 17:59:37 +02:00
|
|
|
// The first case found will be displayed.
|
|
|
|
if (!isset($caseMapping[strtolower($tag)])) {
|
|
|
|
$caseMapping[strtolower($tag)] = $tag;
|
|
|
|
$tags[$caseMapping[strtolower($tag)]] = 0;
|
|
|
|
}
|
|
|
|
$tags[$caseMapping[strtolower($tag)]]++;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sort tags by usage (most used tag first)
|
|
|
|
arsort($tags);
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:10:00 +02:00
|
|
|
/**
|
|
|
|
* Rename or delete a tag across all links.
|
|
|
|
*
|
|
|
|
* @param string $from Tag to rename
|
|
|
|
* @param string $to New tag. If none is provided, the from tag will be deleted
|
|
|
|
*
|
|
|
|
* @return array|bool List of altered links or false on error
|
|
|
|
*/
|
|
|
|
public function renameTag($from, $to)
|
|
|
|
{
|
|
|
|
if (empty($from)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$delete = empty($to);
|
|
|
|
// True for case-sensitive tag search.
|
|
|
|
$linksToAlter = $this->filterSearch(['searchtags' => $from], true);
|
|
|
|
foreach($linksToAlter as $key => &$value)
|
|
|
|
{
|
|
|
|
$tags = preg_split('/\s+/', trim($value['tags']));
|
|
|
|
if (($pos = array_search($from, $tags)) !== false) {
|
|
|
|
if ($delete) {
|
|
|
|
unset($tags[$pos]); // Remove tag.
|
|
|
|
} else {
|
|
|
|
$tags[$pos] = trim($to);
|
|
|
|
}
|
|
|
|
$value['tags'] = trim(implode(' ', array_unique($tags)));
|
|
|
|
$this[$value['id']] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $linksToAlter;
|
|
|
|
}
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
/**
|
|
|
|
* Returns the list of days containing articles (oldest first)
|
|
|
|
* Output: An array containing days (in format YYYYMMDD).
|
|
|
|
*/
|
|
|
|
public function days()
|
|
|
|
{
|
|
|
|
$linkDays = array();
|
2016-11-28 15:30:17 +01:00
|
|
|
foreach ($this->links as $link) {
|
|
|
|
$linkDays[$link['created']->format('Ymd')] = 0;
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|
|
|
|
$linkDays = array_keys($linkDays);
|
|
|
|
sort($linkDays);
|
2016-01-12 19:50:48 +01:00
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
return $linkDays;
|
|
|
|
}
|
2016-11-28 15:30:17 +01: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')
|
|
|
|
{
|
|
|
|
$order = $order === 'ASC' ? -1 : 1;
|
|
|
|
// Reorder array by dates.
|
|
|
|
usort($this->links, function($a, $b) use ($order) {
|
|
|
|
return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->urls = array();
|
|
|
|
$this->ids = array();
|
|
|
|
foreach ($this->links as $key => $link) {
|
|
|
|
$this->urls[$link['url']] = $key;
|
|
|
|
$this->ids[$link['id']] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next key for link creation.
|
|
|
|
* E.g. If the last ID is 597, the next will be 598.
|
|
|
|
*
|
|
|
|
* @return int next ID.
|
|
|
|
*/
|
|
|
|
public function getNextId()
|
|
|
|
{
|
|
|
|
if (!empty($this->ids)) {
|
|
|
|
return max(array_keys($this->ids)) + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a link offset in links array from its unique ID.
|
|
|
|
*
|
|
|
|
* @param int $id Persistent ID of a link.
|
|
|
|
*
|
2016-11-28 18:24:15 +01:00
|
|
|
* @return int Real offset in local array, or null if doesn't exist.
|
2016-11-28 15:30:17 +01:00
|
|
|
*/
|
|
|
|
protected function getLinkOffset($id)
|
|
|
|
{
|
|
|
|
if (isset($this->ids[$id])) {
|
|
|
|
return $this->ids[$id];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-03-12 00:43:02 +01:00
|
|
|
}
|