aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Import
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-02-01 14:17:20 +0100
committerJeremy Benoist <j0k3r@users.noreply.github.com>2016-02-01 14:17:20 +0100
commit85ad629a3ca209907effd75f5fd7f384f42361bc (patch)
tree6fde1374b404567153478373f3e65e63af9c1120 /src/Wallabag/ImportBundle/Import
parente43d27f8450dcdf79850e935b762ac390a19dd63 (diff)
parent6785f4aa749e381081b93e3db46424cc7475eab8 (diff)
downloadwallabag-85ad629a3ca209907effd75f5fd7f384f42361bc.tar.gz
wallabag-85ad629a3ca209907effd75f5fd7f384f42361bc.tar.zst
wallabag-85ad629a3ca209907effd75f5fd7f384f42361bc.zip
Merge pull request #1601 from wallabag/v2-import-from-v2
[#1590] Add JSON import from wallabag v2
Diffstat (limited to 'src/Wallabag/ImportBundle/Import')
-rw-r--r--src/Wallabag/ImportBundle/Import/WallabagV1Import.php18
-rw-r--r--src/Wallabag/ImportBundle/Import/WallabagV2Import.php75
2 files changed, 84 insertions, 9 deletions
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
index 6f8feaf3..0dac6203 100644
--- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
+++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
@@ -11,12 +11,12 @@ use Wallabag\CoreBundle\Tools\Utils;
11 11
12class WallabagV1Import implements ImportInterface 12class WallabagV1Import implements ImportInterface
13{ 13{
14 private $user; 14 protected $user;
15 private $em; 15 protected $em;
16 private $logger; 16 protected $logger;
17 private $skippedEntries = 0; 17 protected $skippedEntries = 0;
18 private $importedEntries = 0; 18 protected $importedEntries = 0;
19 private $filepath; 19 protected $filepath;
20 20
21 public function __construct(EntityManager $em) 21 public function __construct(EntityManager $em)
22 { 22 {
@@ -72,13 +72,13 @@ class WallabagV1Import implements ImportInterface
72 public function import() 72 public function import()
73 { 73 {
74 if (!$this->user) { 74 if (!$this->user) {
75 $this->logger->error('WallabagV1Import: user is not defined'); 75 $this->logger->error('WallabagImport: user is not defined');
76 76
77 return false; 77 return false;
78 } 78 }
79 79
80 if (!file_exists($this->filepath) || !is_readable($this->filepath)) { 80 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
81 $this->logger->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath)); 81 $this->logger->error('WallabagImport: unable to read file', array('filepath' => $this->filepath));
82 82
83 return false; 83 return false;
84 } 84 }
@@ -120,7 +120,7 @@ class WallabagV1Import implements ImportInterface
120 /** 120 /**
121 * @param $entries 121 * @param $entries
122 */ 122 */
123 private function parseEntries($entries) 123 protected function parseEntries($entries)
124 { 124 {
125 $i = 1; 125 $i = 1;
126 126
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV2Import.php b/src/Wallabag/ImportBundle/Import/WallabagV2Import.php
new file mode 100644
index 00000000..979c671e
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Import/WallabagV2Import.php
@@ -0,0 +1,75 @@
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Wallabag\CoreBundle\Entity\Entry;
6
7class WallabagV2Import extends WallabagV1Import implements ImportInterface
8{
9 /**
10 * {@inheritdoc}
11 */
12 public function getName()
13 {
14 return 'wallabag v2';
15 }
16
17 /**
18 * {@inheritdoc}
19 */
20 public function getUrl()
21 {
22 return 'import_wallabag_v2';
23 }
24
25 /**
26 * {@inheritdoc}
27 */
28 public function getDescription()
29 {
30 return 'This importer will import all your wallabag v2 articles. On the export sidebar, click on "JSON". You will have a "Unread articles.json" file.';
31 }
32
33 /**
34 * @param $entries
35 */
36 protected function parseEntries($entries)
37 {
38 $i = 1;
39
40 foreach ($entries as $importedEntry) {
41 $existingEntry = $this->em
42 ->getRepository('WallabagCoreBundle:Entry')
43 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
44
45 if (false !== $existingEntry) {
46 ++$this->skippedEntries;
47 continue;
48 }
49
50 // @see ContentProxy->updateEntry
51 $entry = new Entry($this->user);
52 $entry->setUrl($importedEntry['url']);
53 $entry->setTitle($importedEntry['title']);
54 $entry->setArchived($importedEntry['is_archived']);
55 $entry->setStarred($importedEntry['is_starred']);
56 $entry->setContent($importedEntry['content']);
57 $entry->setReadingTime($importedEntry['reading_time']);
58 $entry->setDomainName($importedEntry['domain_name']);
59 $entry->setMimetype($importedEntry['mimetype']);
60 $entry->setLanguage($importedEntry['language']);
61 $entry->setPreviewPicture($importedEntry['preview_picture']);
62
63 $this->em->persist($entry);
64 ++$this->importedEntries;
65
66 // flush every 20 entries
67 if (($i % 20) === 0) {
68 $this->em->flush();
69 }
70 ++$i;
71 }
72
73 $this->em->flush();
74 }
75}