]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/History.php
namespacing: \Shaarli\FileUtils
[github/shaarli/Shaarli.git] / application / History.php
index f93b03568235c1f71ecc7d9d86b21e0a56fab34b..a58466528050e2efb4aee4dbccb59f3629e42041 100644 (file)
@@ -1,4 +1,8 @@
 <?php
+namespace Shaarli;
+
+use DateTime;
+use Exception;
 
 /**
  * Class History
@@ -16,6 +20,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 +46,11 @@ class History
      */
     const SETTINGS = 'SETTINGS';
 
+    /**
+     * @var string Action key: a bulk import has been processed.
+     */
+    const IMPORT = 'IMPORT';
+
     /**
      * @var string History file path.
      */
@@ -60,7 +70,7 @@ class History
      * History constructor.
      *
      * @param string $historyFilePath History file path.
-     * @param int    $retentionTime   History content rentention time in seconds.
+     * @param int    $retentionTime   History content retention time in seconds.
      *
      * @throws Exception if something goes wrong.
      */
@@ -121,6 +131,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.
      *
@@ -135,7 +155,7 @@ class History
 
         $item = [
             'event' => $status,
-            'datetime' => (new DateTime())->format(DateTime::ATOM),
+            'datetime' => new DateTime(),
             'id' => $id !== null ? $id : '',
         ];
         $this->history = array_merge([$item], $this->history);
@@ -150,12 +170,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'));
         }
     }
 
@@ -166,7 +186,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'));
         }
     }
 
@@ -175,9 +195,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]);
             }
         }