]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Entry: add archived_at property and updateArchived method
authorSébastien Viande <sviande@gmail.com>
Wed, 11 Apr 2018 09:42:52 +0000 (11:42 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 21 Sep 2018 08:33:33 +0000 (10:33 +0200)
13 files changed:
app/DoctrineMigrations/Version20180405182455.php [new file with mode: 0755]
src/Wallabag/ApiBundle/Controller/EntryRestController.php
src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/ImportBundle/Import/BrowserImport.php
src/Wallabag/ImportBundle/Import/InstapaperImport.php
src/Wallabag/ImportBundle/Import/PinboardImport.php
src/Wallabag/ImportBundle/Import/PocketImport.php
src/Wallabag/ImportBundle/Import/ReadabilityImport.php
src/Wallabag/ImportBundle/Import/WallabagImport.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php

diff --git a/app/DoctrineMigrations/Version20180405182455.php b/app/DoctrineMigrations/Version20180405182455.php
new file mode 100755 (executable)
index 0000000..71879c0
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+namespace Application\Migrations;
+
+use Doctrine\DBAL\Migrations\AbstractMigration;
+use Doctrine\DBAL\Schema\Schema;
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Add archived_at column and set its value to updated_at for is_archived entries.
+ */
+class Version20180405182455 extends AbstractMigration implements ContainerAwareInterface
+{
+    /**
+     * @var ContainerInterface
+     */
+    private $container;
+
+    public function setContainer(ContainerInterface $container = null)
+    {
+        $this->container = $container;
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        $entryTable = $schema->getTable($this->getTable('entry'));
+
+        $this->skipIf($entryTable->hasColumn('archived_at'), 'It seems that you already played this migration.');
+
+        $entryTable->addColumn('archived_at', 'datetime', [
+            'notnull' => false,
+        ]);
+    }
+
+    public function postUp(Schema $schema)
+    {
+        $entryTable = $schema->getTable($this->getTable('entry'));
+        $this->skipIf(!$entryTable->hasColumn('archived_at'), 'Unable to add archived_at colum');
+
+        $this->connection->executeQuery(
+            'UPDATE ' . $this->getTable('entry') . ' SET archived_at = updated_at WHERE is_archived = :is_archived',
+            [
+                'is_archived' => true,
+            ]
+        );
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function down(Schema $schema)
+    {
+        $entryTable = $schema->getTable($this->getTable('entry'));
+
+        $this->skipIf(!$entryTable->hasColumn('archived_at'), 'It seems that you already played this migration.');
+
+        $entryTable->dropColumn('archived_at');
+    }
+
+    private function getTable($tableName)
+    {
+        return $this->container->getParameter('database_table_prefix') . $tableName;
+    }
+}
index 0b4e74a0f1d69d332d710300d2471a69132a6d24..dc63b98dee8836c76574695bf3204e863844456b 100644 (file)
@@ -358,7 +358,7 @@ class EntryRestController extends WallabagRestController
         }
 
         if (null !== $data['isArchived']) {
-            $entry->setArchived((bool) $data['isArchived']);
+            $entry->updateArchived((bool) $data['isArchived']);
         }
 
         if (null !== $data['isStarred']) {
@@ -474,7 +474,7 @@ class EntryRestController extends WallabagRestController
         }
 
         if (null !== $data['isArchived']) {
-            $entry->setArchived((bool) $data['isArchived']);
+            $entry->updateArchived((bool) $data['isArchived']);
         }
 
         if (null !== $data['isStarred']) {
index 0e1510a29bf450f7c8e9e6067fb7c6ee2af84012..62fb5fa68b9a630a0f1a8ec9acdd9595438059c1 100644 (file)
@@ -98,7 +98,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
         $entry6->setMimetype('text/html');
         $entry6->setTitle('test title entry6');
         $entry6->setContent('This is my content /o/');
-        $entry6->setArchived(true);
+        $entry6->updateArchived(true);
         $entry6->setLanguage('de');
         $entry6->addTag($this->getReference('bar-tag'));
 
index 2b1f2e050799c24495611d2ba75212e37944f4d5..b3cfdc4a4c26dd13f979cb0782ba8a96592d453c 100644 (file)
@@ -86,6 +86,15 @@ class Entry
      */
     private $isArchived = false;
 
+    /**
+     * @var \DateTime
+     *
+     * @ORM\Column(name="archived_at", type="datetime", nullable=true)
+     *
+     * @Groups({"entries_for_user", "export_all"})
+     */
+    private $archivedAt = null;
+
     /**
      * @var bool
      *
@@ -335,6 +344,44 @@ class Entry
         return $this;
     }
 
+    /**
+     * update isArchived and archive_at fields.
+     *
+     * @param bool $isArchived
+     *
+     * @return Entry
+     */
+    public function updateArchived($isArchived = false)
+    {
+        $this->setArchived($isArchived);
+        $this->setArchivedAt(null);
+        if ($this->isArchived()) {
+            $this->setArchivedAt(new \DateTime());
+        }
+
+        return $this;
+    }
+
+    /**
+     * @return \DateTime|null
+     */
+    public function getArchivedAt()
+    {
+        return $this->archivedAt;
+    }
+
+    /**
+     * @param \DateTime|null $archivedAt
+     *
+     * @return Entry
+     */
+    public function setArchivedAt($archivedAt = null)
+    {
+        $this->archivedAt = $archivedAt;
+
+        return $this;
+    }
+
     /**
      * Get isArchived.
      *
@@ -357,7 +404,7 @@ class Entry
 
     public function toggleArchive()
     {
-        $this->isArchived = $this->isArchived() ^ 1;
+        $this->updateArchived($this->isArchived() ^ 1);
 
         return $this;
     }
index 225f1791f5bfe6b6fdc04bba4a2d0abf9cc7c600..614386cb2804f451f022c48181480b7c1ee13db1 100644 (file)
@@ -133,7 +133,7 @@ abstract class BrowserImport extends AbstractImport
             );
         }
 
-        $entry->setArchived($data['is_archived']);
+        $entry->updateArchived($data['is_archived']);
 
         if (!empty($data['created_at'])) {
             $dt = new \DateTime();
index e4f0970c0c3f41a531a9d548c46fdc175946e92c..e113ba00824a75c07f07f142fea311a415a07c34 100644 (file)
@@ -135,7 +135,7 @@ class InstapaperImport extends AbstractImport
             );
         }
 
-        $entry->setArchived($importedEntry['is_archived']);
+        $entry->updateArchived($importedEntry['is_archived']);
         $entry->setStarred($importedEntry['is_starred']);
 
         $this->em->persist($entry);
index 110b046422ae3c25a7f5be34f59c12e4d11938b2..9a5e8cb6c2cca497d2e5b9a7ee8b0e2b0b3009d2 100644 (file)
@@ -119,7 +119,7 @@ class PinboardImport extends AbstractImport
             );
         }
 
-        $entry->setArchived($data['is_archived']);
+        $entry->updateArchived($data['is_archived']);
         $entry->setStarred($data['is_starred']);
         $entry->setCreatedAt(new \DateTime($data['created_at']));
 
index c1b35b7ef028a0e261d9a072e867246eca4816eb..4b1ad1d7565bc0b3a9dfbb537a7c1262ab57adad 100644 (file)
@@ -194,7 +194,7 @@ class PocketImport extends AbstractImport
         $this->fetchContent($entry, $url);
 
         // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
-        $entry->setArchived(1 === $importedEntry['status'] || $this->markAsRead);
+        $entry->updateArchived(1 === $importedEntry['status'] || $this->markAsRead);
 
         // 0 or 1 - 1 If the item is starred
         $entry->setStarred(1 === $importedEntry['favorite']);
index 002b27f46b5b2ace08be9f89ad402672496a9c8a..d677758211e039d757efd874f2141120499700a4 100644 (file)
@@ -111,7 +111,7 @@ class ReadabilityImport extends AbstractImport
         // update entry with content (in case fetching failed, the given entry will be return)
         $this->fetchContent($entry, $data['url'], $data);
 
-        $entry->setArchived($data['is_archived']);
+        $entry->updateArchived($data['is_archived']);
         $entry->setStarred($data['is_starred']);
         $entry->setCreatedAt(new \DateTime($data['created_at']));
 
index c64ccd64d196095ce8b9e3f913c4f856075f0fa3..916137f17e4e1487fc5890b33d9b2955336ff3ed 100644 (file)
@@ -122,7 +122,7 @@ abstract class WallabagImport extends AbstractImport
             $entry->setPreviewPicture($importedEntry['preview_picture']);
         }
 
-        $entry->setArchived($data['is_archived']);
+        $entry->updateArchived($data['is_archived']);
         $entry->setStarred($data['is_starred']);
 
         if (!empty($data['created_at'])) {
index 58b617f3d5bc9519d1e50fe165875b931a037ae4..6b26376dbfb4a7709392a29e3d1dede3e3f3bc12 100644 (file)
@@ -438,6 +438,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertSame(0, $content['is_archived']);
         $this->assertSame(0, $content['is_starred']);
         $this->assertNull($content['starred_at']);
+        $this->assertNull($content['archived_at']);
         $this->assertSame('New title for my article', $content['title']);
         $this->assertSame(1, $content['user_id']);
         $this->assertCount(2, $content['tags']);
@@ -533,6 +534,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertSame(1, $content['is_archived']);
         $this->assertSame(1, $content['is_starred']);
         $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
+        $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['archived_at']))->getTimestamp());
         $this->assertSame(1, $content['user_id']);
     }
 
index e07c57dd34e7544e100420779bd31c15f184b908..d709f4ebd2439596acfe7ea461204317737f56e9 100644 (file)
@@ -849,7 +849,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $entryArchived->setContent('Youhou');
         $entryArchived->setTitle('Youhou');
         $entryArchived->addTag($tagArchived);
-        $entryArchived->setArchived(true);
+        $entryArchived->updateArchived(true);
         $em->persist($entryArchived);
 
         $annotationArchived = new Annotation($user);
index bf0068b4b1e0f6941afc5bc3b517699a1860890c..0ac119d86f2a2546db948f5d153da14281401f0d 100644 (file)
@@ -621,7 +621,7 @@ class EntryControllerTest extends WallabagCoreTestCase
         $content->setMimetype('text/html');
         $content->setTitle('test title entry');
         $content->setContent('This is my content /o/');
-        $content->setArchived(true);
+        $content->updateArchived(true);
         $content->setLanguage('fr');
 
         $em->persist($content);
@@ -774,7 +774,7 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $entry = new Entry($this->getLoggedInUser());
         $entry->setUrl($this->url);
-        $entry->setArchived(false);
+        $entry->updateArchived(false);
         $this->getEntityManager()->persist($entry);
         $this->getEntityManager()->flush();
 
@@ -1245,7 +1245,7 @@ class EntryControllerTest extends WallabagCoreTestCase
         $entry = new Entry($this->getLoggedInUser());
         $entry->setUrl('http://0.0.0.0/foo/baz/qux');
         $entry->setTitle('Le manège');
-        $entry->setArchived(true);
+        $entry->updateArchived(true);
         $this->getEntityManager()->persist($entry);
         $this->getEntityManager()->flush();
 
@@ -1275,7 +1275,7 @@ class EntryControllerTest extends WallabagCoreTestCase
         $entry = new Entry($this->getLoggedInUser());
         $entry->setUrl('http://domain/qux');
         $entry->setTitle('Le manège');
-        $entry->setArchived(true);
+        $entry->updateArchived(true);
         $this->getEntityManager()->persist($entry);
         $this->getEntityManager()->flush();