2017-01-16 12:31:08 +01:00
|
|
|
<?php
|
2018-12-02 23:24:58 +01:00
|
|
|
namespace Shaarli;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use Exception;
|
|
|
|
use FileUtils;
|
2017-01-16 12:31:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class History
|
|
|
|
*
|
|
|
|
* Handle the history file tracing events in Shaarli.
|
|
|
|
* The history is stored as JSON in a file set by 'resource.history' setting.
|
|
|
|
*
|
|
|
|
* Available data:
|
|
|
|
* - event: event key
|
|
|
|
* - datetime: event date, in ISO8601 format.
|
|
|
|
* - id: event item identifier (currently only link IDs).
|
|
|
|
*
|
|
|
|
* Available event keys:
|
|
|
|
* - CREATED: new link
|
|
|
|
* - UPDATED: link updated
|
|
|
|
* - DELETED: link deleted
|
|
|
|
* - SETTINGS: the settings have been updated through the UI.
|
2017-10-07 16:40:16 +02:00
|
|
|
* - IMPORT: bulk links import
|
2017-01-16 12:31:08 +01:00
|
|
|
*
|
|
|
|
* Note: new events are put at the beginning of the file and history array.
|
|
|
|
*/
|
|
|
|
class History
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string Action key: a new link has been created.
|
|
|
|
*/
|
|
|
|
const CREATED = 'CREATED';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Action key: a link has been updated.
|
|
|
|
*/
|
|
|
|
const UPDATED = 'UPDATED';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Action key: a link has been deleted.
|
|
|
|
*/
|
|
|
|
const DELETED = 'DELETED';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Action key: settings have been updated.
|
|
|
|
*/
|
|
|
|
const SETTINGS = 'SETTINGS';
|
|
|
|
|
2017-10-07 16:40:16 +02:00
|
|
|
/**
|
|
|
|
* @var string Action key: a bulk import has been processed.
|
|
|
|
*/
|
|
|
|
const IMPORT = 'IMPORT';
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
/**
|
|
|
|
* @var string History file path.
|
|
|
|
*/
|
|
|
|
protected $historyFilePath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array History data.
|
|
|
|
*/
|
|
|
|
protected $history;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int History retention time in seconds (1 month).
|
|
|
|
*/
|
|
|
|
protected $retentionTime = 2678400;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History constructor.
|
|
|
|
*
|
|
|
|
* @param string $historyFilePath History file path.
|
|
|
|
* @param int $retentionTime History content rentention time in seconds.
|
|
|
|
*
|
|
|
|
* @throws Exception if something goes wrong.
|
|
|
|
*/
|
|
|
|
public function __construct($historyFilePath, $retentionTime = null)
|
|
|
|
{
|
|
|
|
$this->historyFilePath = $historyFilePath;
|
|
|
|
if ($retentionTime !== null) {
|
|
|
|
$this->retentionTime = $retentionTime;
|
|
|
|
}
|
2017-01-16 12:50:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize: read history file.
|
|
|
|
*
|
|
|
|
* Allow lazy loading (don't read the file if it isn't necessary).
|
|
|
|
*/
|
|
|
|
protected function initialize()
|
|
|
|
{
|
2017-01-16 12:31:08 +01:00
|
|
|
$this->check();
|
|
|
|
$this->read();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Event: new link.
|
|
|
|
*
|
|
|
|
* @param array $link Link data.
|
|
|
|
*/
|
|
|
|
public function addLink($link)
|
|
|
|
{
|
|
|
|
$this->addEvent(self::CREATED, $link['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Event: update existing link.
|
|
|
|
*
|
|
|
|
* @param array $link Link data.
|
|
|
|
*/
|
|
|
|
public function updateLink($link)
|
|
|
|
{
|
|
|
|
$this->addEvent(self::UPDATED, $link['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Event: delete existing link.
|
|
|
|
*
|
|
|
|
* @param array $link Link data.
|
|
|
|
*/
|
|
|
|
public function deleteLink($link)
|
|
|
|
{
|
|
|
|
$this->addEvent(self::DELETED, $link['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Event: settings updated.
|
|
|
|
*/
|
|
|
|
public function updateSettings()
|
|
|
|
{
|
|
|
|
$this->addEvent(self::SETTINGS);
|
|
|
|
}
|
|
|
|
|
2017-10-07 16:40:16 +02:00
|
|
|
/**
|
|
|
|
* Add Event: bulk import.
|
|
|
|
*
|
|
|
|
* Note: we don't store links add/update one by one since it can have a huge impact on performances.
|
|
|
|
*/
|
|
|
|
public function importLinks()
|
|
|
|
{
|
|
|
|
$this->addEvent(self::IMPORT);
|
|
|
|
}
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
/**
|
|
|
|
* Save a new event and write it in the history file.
|
|
|
|
*
|
|
|
|
* @param string $status Event key, should be defined as constant.
|
2018-12-02 23:24:58 +01:00
|
|
|
* @param mixed $id Event item identifier (e.g. link ID).
|
2017-01-16 12:31:08 +01:00
|
|
|
*/
|
|
|
|
protected function addEvent($status, $id = null)
|
|
|
|
{
|
2017-01-16 12:50:36 +01:00
|
|
|
if ($this->history === null) {
|
|
|
|
$this->initialize();
|
|
|
|
}
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
$item = [
|
|
|
|
'event' => $status,
|
2017-05-07 16:50:20 +02:00
|
|
|
'datetime' => new DateTime(),
|
2017-01-16 12:31:08 +01:00
|
|
|
'id' => $id !== null ? $id : '',
|
|
|
|
];
|
|
|
|
$this->history = array_merge([$item], $this->history);
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the history file is writable.
|
|
|
|
* Create the file if it doesn't exist.
|
|
|
|
*
|
|
|
|
* @throws Exception if it isn't writable.
|
|
|
|
*/
|
|
|
|
protected function check()
|
|
|
|
{
|
2018-12-02 23:24:58 +01:00
|
|
|
if (!is_file($this->historyFilePath)) {
|
2017-01-16 12:31:08 +01:00
|
|
|
FileUtils::writeFlatDB($this->historyFilePath, []);
|
|
|
|
}
|
|
|
|
|
2018-12-02 23:24:58 +01:00
|
|
|
if (!is_writable($this->historyFilePath)) {
|
2017-05-09 18:12:15 +02:00
|
|
|
throw new Exception(t('History file isn\'t readable or writable'));
|
2017-01-16 12:31:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read JSON history file.
|
|
|
|
*/
|
|
|
|
protected function read()
|
|
|
|
{
|
|
|
|
$this->history = FileUtils::readFlatDB($this->historyFilePath, []);
|
|
|
|
if ($this->history === false) {
|
2017-05-09 18:12:15 +02:00
|
|
|
throw new Exception(t('Could not parse history file'));
|
2017-01-16 12:31:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write JSON history file and delete old entries.
|
|
|
|
*/
|
|
|
|
protected function write()
|
|
|
|
{
|
2018-12-02 23:24:58 +01:00
|
|
|
$comparaison = new DateTime('-' . $this->retentionTime . ' seconds');
|
2017-01-16 12:31:08 +01:00
|
|
|
foreach ($this->history as $key => $value) {
|
2017-05-07 16:50:20 +02:00
|
|
|
if ($value['datetime'] < $comparaison) {
|
2017-01-16 12:31:08 +01:00
|
|
|
unset($this->history[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FileUtils::writeFlatDB($this->historyFilePath, array_values($this->history));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the History.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHistory()
|
|
|
|
{
|
2017-01-16 12:50:36 +01:00
|
|
|
if ($this->history === null) {
|
|
|
|
$this->initialize();
|
|
|
|
}
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
return $this->history;
|
|
|
|
}
|
|
|
|
}
|