]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ImportBundle/Import/WallabagV1Import.php
Fix `findOneByUrl` side effect in tests
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / WallabagV1Import.php
index 7b0126748e28d4afe8d5c88717241bea25d6fb63..6f8feaf36b2924c0bc2ea717103bfa59689ef5d7 100644 (file)
@@ -47,7 +47,15 @@ class WallabagV1Import implements ImportInterface
      */
     public function getName()
     {
-        return 'Wallabag v1';
+        return 'wallabag v1';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getUrl()
+    {
+        return 'import_wallabag_v1';
     }
 
     /**
@@ -55,7 +63,7 @@ class WallabagV1Import implements ImportInterface
      */
     public function getDescription()
     {
-        return 'This importer will import all your wallabag v1 articles.';
+        return 'This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.';
     }
 
     /**
@@ -75,7 +83,13 @@ class WallabagV1Import implements ImportInterface
             return false;
         }
 
-        $this->parseEntries(json_decode(file_get_contents($this->filepath), true));
+        $data = json_decode(file_get_contents($this->filepath), true);
+
+        if (empty($data)) {
+            return false;
+        }
+
+        $this->parseEntries($data);
 
         return true;
     }
@@ -108,10 +122,12 @@ class WallabagV1Import implements ImportInterface
      */
     private function parseEntries($entries)
     {
+        $i = 1;
+
         foreach ($entries as $importedEntry) {
             $existingEntry = $this->em
                 ->getRepository('WallabagCoreBundle:Entry')
-                ->existByUrlAndUserId($importedEntry['url'], $this->user->getId());
+                ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
 
             if (false !== $existingEntry) {
                 ++$this->skippedEntries;
@@ -130,6 +146,12 @@ class WallabagV1Import implements ImportInterface
 
             $this->em->persist($entry);
             ++$this->importedEntries;
+
+            // flush every 20 entries
+            if (($i % 20) === 0) {
+                $this->em->flush();
+            }
+            ++$i;
         }
 
         $this->em->flush();