X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FHistory.php;h=35ec016a9be27c374dec0e96cbcd0d27e20a119a;hb=12266213d098a53c5f005b9afcbbe62771fd580c;hp=c06067df80ca454bf0b8d3647e2f2d2b2cc3b35f;hpb=4306b184c4471825f916d895b047ed03fdf58985;p=github%2Fshaarli%2FShaarli.git diff --git a/application/History.php b/application/History.php index c06067df..35ec016a 100644 --- a/application/History.php +++ b/application/History.php @@ -16,6 +16,7 @@ * - 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. */ @@ -41,6 +42,11 @@ class History */ const SETTINGS = 'SETTINGS'; + /** + * @var string Action key: a bulk import has been processed. + */ + const IMPORT = 'IMPORT'; + /** * @var string History file path. */ @@ -70,6 +76,15 @@ class History 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(); } @@ -112,6 +127,16 @@ class History $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. * @@ -120,9 +145,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); @@ -142,7 +171,7 @@ class 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')); } } @@ -153,7 +182,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')); } } @@ -164,7 +193,7 @@ class History { $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 +207,10 @@ class History */ public function getHistory() { + if ($this->history === null) { + $this->initialize(); + } + return $this->history; } }