X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FHistory.php;h=d230f39de71bf0286158240341e41d73336125c5;hb=8fbc29de0252c462884605d4f691b6746e524fc9;hp=c06067df80ca454bf0b8d3647e2f2d2b2cc3b35f;hpb=4306b184c4471825f916d895b047ed03fdf58985;p=github%2Fshaarli%2FShaarli.git diff --git a/application/History.php b/application/History.php index c06067df..d230f39d 100644 --- a/application/History.php +++ b/application/History.php @@ -1,5 +1,12 @@ 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(); } @@ -77,31 +99,31 @@ class History /** * Add Event: new link. * - * @param array $link Link data. + * @param Bookmark $link Link data. */ public function addLink($link) { - $this->addEvent(self::CREATED, $link['id']); + $this->addEvent(self::CREATED, $link->getId()); } /** * Add Event: update existing link. * - * @param array $link Link data. + * @param Bookmark $link Link data. */ public function updateLink($link) { - $this->addEvent(self::UPDATED, $link['id']); + $this->addEvent(self::UPDATED, $link->getId()); } /** * Add Event: delete existing link. * - * @param array $link Link data. + * @param Bookmark $link Link data. */ public function deleteLink($link) { - $this->addEvent(self::DELETED, $link['id']); + $this->addEvent(self::DELETED, $link->getId()); } /** @@ -112,6 +134,16 @@ class History $this->addEvent(self::SETTINGS); } + /** + * Add Event: bulk import. + * + * Note: we don't store bookmarks 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. * @@ -120,9 +152,13 @@ class History */ 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); @@ -137,12 +173,12 @@ class History */ protected function check() { - if (! is_file($this->historyFilePath)) { + if (!is_file($this->historyFilePath)) { FileUtils::writeFlatDB($this->historyFilePath, []); } - if (! is_writable($this->historyFilePath)) { - throw new Exception('History file isn\'t readable or writable'); + if (!is_writable($this->historyFilePath)) { + throw new Exception(t('History file isn\'t readable or writable')); } } @@ -153,7 +189,7 @@ class History { $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')); } } @@ -162,9 +198,9 @@ class History */ protected function write() { - $comparaison = new DateTime('-'. $this->retentionTime . ' seconds'); + $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]); } } @@ -178,6 +214,10 @@ class History */ public function getHistory() { + if ($this->history === null) { + $this->initialize(); + } + return $this->history; } }