]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/History.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / application / History.php
index 116b9264019c2a667e19f2d1e47aee0d5ab0b362..d230f39de71bf0286158240341e41d73336125c5 100644 (file)
@@ -1,5 +1,12 @@
 <?php
 
+namespace Shaarli;
+
+use DateTime;
+use Exception;
+use Shaarli\Bookmark\Bookmark;
+use Shaarli\Helper\FileUtils;
+
 /**
  * Class History
  *
@@ -16,6 +23,7 @@
  *   - UPDATED: link updated
  *   - DELETED: link deleted
  *   - SETTINGS: the settings have been updated through the UI.
+ *   - IMPORT: bulk bookmarks import
  *
  * Note: new events are put at the beginning of the file and history array.
  */
@@ -24,22 +32,27 @@ class History
     /**
      * @var string Action key: a new link has been created.
      */
-    const CREATED = 'CREATED';
+    public const CREATED = 'CREATED';
 
     /**
      * @var string Action key: a link has been updated.
      */
-    const UPDATED = 'UPDATED';
+    public const UPDATED = 'UPDATED';
 
     /**
      * @var string Action key: a link has been deleted.
      */
-    const DELETED = 'DELETED';
+    public const DELETED = 'DELETED';
 
     /**
      * @var string Action key: settings have been updated.
      */
-    const SETTINGS = 'SETTINGS';
+    public const SETTINGS = 'SETTINGS';
+
+    /**
+     * @var string Action key: a bulk import has been processed.
+     */
+    public const IMPORT = 'IMPORT';
 
     /**
      * @var string History file path.
@@ -60,7 +73,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.
      */
@@ -86,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());
     }
 
     /**
@@ -121,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.
      *
@@ -150,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'));
         }
     }
 
@@ -166,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'));
         }
     }
 
@@ -175,7 +198,7 @@ class History
      */
     protected function write()
     {
-        $comparaison = new DateTime('-'. $this->retentionTime . ' seconds');
+        $comparaison = new DateTime('-' . $this->retentionTime . ' seconds');
         foreach ($this->history as $key => $value) {
             if ($value['datetime'] < $comparaison) {
                 unset($this->history[$key]);