]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ImportBundle/Import/BrowserImport.php
Merge pull request #3165 from wallabag/it-translation-update
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / BrowserImport.php
index 68fa8bf83877c98354f662613e56973a6912440c..71e65e5916f04e4216da71c46a2fd272285c2e79 100644 (file)
@@ -4,7 +4,7 @@ namespace Wallabag\ImportBundle\Import;
 
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\UserBundle\Entity\User;
-use Wallabag\CoreBundle\Helper\ContentProxy;
+use Wallabag\CoreBundle\Event\EntrySavedEvent;
 
 abstract class BrowserImport extends AbstractImport
 {
@@ -45,6 +45,8 @@ abstract class BrowserImport extends AbstractImport
         $data = json_decode(file_get_contents($this->filepath), true);
 
         if (empty($data)) {
+            $this->logger->error('Wallabag Browser: no entries in imported file');
+
             return false;
         }
 
@@ -79,6 +81,7 @@ abstract class BrowserImport extends AbstractImport
     protected function parseEntries($entries)
     {
         $i = 1;
+        $entryToBeFlushed = [];
 
         foreach ($entries as $importedEntry) {
             if ((array) $importedEntry !== $importedEntry) {
@@ -91,14 +94,29 @@ abstract class BrowserImport extends AbstractImport
                 continue;
             }
 
+            // @see AbstractImport
+            $entryToBeFlushed[] = $entry;
+
             // flush every 20 entries
             if (($i % 20) === 0) {
                 $this->em->flush();
+
+                foreach ($entryToBeFlushed as $entry) {
+                    $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
+                }
+
+                $entryToBeFlushed = [];
             }
             ++$i;
         }
 
         $this->em->flush();
+
+        if (!empty($entryToBeFlushed)) {
+            foreach ($entryToBeFlushed as $entry) {
+                $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
+            }
+        }
     }
 
     /**
@@ -137,12 +155,24 @@ abstract class BrowserImport extends AbstractImport
     public function parseEntry(array $importedEntry)
     {
         if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
+            if ($this->producer) {
+                $this->parseEntriesForProducer($importedEntry);
+
+                return;
+            }
+
             $this->parseEntries($importedEntry);
 
             return;
         }
 
         if (array_key_exists('children', $importedEntry)) {
+            if ($this->producer) {
+                $this->parseEntriesForProducer($importedEntry['children']);
+
+                return;
+            }
+
             $this->parseEntries($importedEntry['children']);
 
             return;
@@ -171,10 +201,10 @@ abstract class BrowserImport extends AbstractImport
         $entry->setTitle($data['title']);
 
         // update entry with content (in case fetching failed, the given entry will be return)
-        $entry = $this->fetchContent($entry, $data['url'], $data);
+        $this->fetchContent($entry, $data['url'], $data);
 
         if (array_key_exists('tags', $data)) {
-            $this->contentProxy->assignTagsToEntry(
+            $this->tagsAssigner->assignTagsToEntry(
                 $entry,
                 $data['tags']
             );
@@ -193,41 +223,6 @@ abstract class BrowserImport extends AbstractImport
         return $entry;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    protected function prepareEntry(array $entry = [])
-    {
-        $url = array_key_exists('uri', $entry) ? $entry['uri'] : $entry['url'];
-        $date = array_key_exists('date_added', $entry) ? $entry['date_added'] : $entry['dateAdded'];
-        $title = array_key_exists('name', $entry) ? $entry['name'] : $entry['title'];
-
-        if (16 === strlen($date)) {
-            // firefox ...
-            $date = (int) ceil($date / 1000000);
-        } else if (17 === strlen($date)) {
-            // chrome ...
-            $date = (int) ceil($date / 10000000);
-        } else {
-            $date = '';
-        }
-
-        $data = [
-            'title' => $title,
-            'html' => '',
-            'url' => $url,
-            'is_archived' => $this->markAsRead,
-            'tags' => '',
-            'created_at' => $date,
-        ];
-
-        if (array_key_exists('tags', $entry) && $entry['tags'] != '') {
-            $data['tags'] = $entry['tags'];
-        }
-
-        return $data;
-    }
-
     /**
      * {@inheritdoc}
      */