* - UPDATED: link updated
* - DELETED: link deleted
* - SETTINGS: the settings have been updated through the UI.
+ * - IMPORT: bulk links import
*
* Note: new events are put at the beginning of the file and history array.
*/
*/
const SETTINGS = 'SETTINGS';
+ /**
+ * @var string Action key: a bulk import has been processed.
+ */
+ const IMPORT = 'IMPORT';
+
/**
* @var string History file path.
*/
if ($retentionTime !== null) {
$this->retentionTime = $retentionTime;
}
+ }
+
+ /**
+ * Initialize: read history file.
+ *
+ * Allow lazy loading (don't read the file if it isn't necessary).
+ */
+ protected function initialize()
+ {
$this->check();
$this->read();
}
$this->addEvent(self::SETTINGS);
}
+ /**
+ * 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);
+ }
+
/**
* Save a new event and write it in the history file.
*
*/
protected function addEvent($status, $id = null)
{
+ if ($this->history === null) {
+ $this->initialize();
+ }
+
$item = [
'event' => $status,
- 'datetime' => (new DateTime())->format(DateTime::ATOM),
+ 'datetime' => new DateTime(),
'id' => $id !== null ? $id : '',
];
$this->history = array_merge([$item], $this->history);
}
if (! is_writable($this->historyFilePath)) {
- throw new Exception('History file isn\'t readable or writable');
+ throw new Exception(t('History file isn\'t readable or writable'));
}
}
{
$this->history = FileUtils::readFlatDB($this->historyFilePath, []);
if ($this->history === false) {
- throw new Exception('Could not parse history file');
+ throw new Exception(t('Could not parse history file'));
}
}
{
$comparaison = new DateTime('-'. $this->retentionTime . ' seconds');
foreach ($this->history as $key => $value) {
- if (DateTime::createFromFormat(DateTime::ATOM, $value['datetime']) < $comparaison) {
+ if ($value['datetime'] < $comparaison) {
unset($this->history[$key]);
}
}
*/
public function getHistory()
{
+ if ($this->history === null) {
+ $this->initialize();
+ }
+
return $this->history;
}
}