diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-12-30 13:26:30 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-02 23:27:41 +0100 |
commit | b1d05721cf37ab94ec1a6837fe79cf19474dd0ff (patch) | |
tree | 95cb2413a6044af6603a40339a1ceb2961206ae3 /src/Wallabag/ImportBundle/Import | |
parent | 252ebd60719d32ec954d0519c9edf2b52b03310c (diff) | |
download | wallabag-b1d05721cf37ab94ec1a6837fe79cf19474dd0ff.tar.gz wallabag-b1d05721cf37ab94ec1a6837fe79cf19474dd0ff.tar.zst wallabag-b1d05721cf37ab94ec1a6837fe79cf19474dd0ff.zip |
Rewrote Wallabag v1 import
Diffstat (limited to 'src/Wallabag/ImportBundle/Import')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/PocketImport.php | 7 | ||||
-rw-r--r-- | src/Wallabag/ImportBundle/Import/WallabagV1Import.php | 137 |
2 files changed, 140 insertions, 4 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 1710d9d3..aeccc7bd 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php | |||
@@ -141,7 +141,7 @@ class PocketImport implements ImportInterface | |||
141 | 141 | ||
142 | $entries = $response->json(); | 142 | $entries = $response->json(); |
143 | 143 | ||
144 | $this->parsePocketEntries($entries['list']); | 144 | $this->parseEntries($entries['list']); |
145 | 145 | ||
146 | return true; | 146 | return true; |
147 | } | 147 | } |
@@ -194,11 +194,9 @@ class PocketImport implements ImportInterface | |||
194 | * | 194 | * |
195 | * @param $entries | 195 | * @param $entries |
196 | */ | 196 | */ |
197 | private function parsePocketEntries($entries) | 197 | private function parseEntries($entries) |
198 | { | 198 | { |
199 | foreach ($entries as $pocketEntry) { | 199 | foreach ($entries as $pocketEntry) { |
200 | $entry = new Entry($this->user); | ||
201 | |||
202 | $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; | 200 | $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; |
203 | 201 | ||
204 | $existingEntry = $this->em | 202 | $existingEntry = $this->em |
@@ -210,6 +208,7 @@ class PocketImport implements ImportInterface | |||
210 | continue; | 208 | continue; |
211 | } | 209 | } |
212 | 210 | ||
211 | $entry = new Entry($this->user); | ||
213 | $entry = $this->contentProxy->updateEntry($entry, $url); | 212 | $entry = $this->contentProxy->updateEntry($entry, $url); |
214 | 213 | ||
215 | // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted | 214 | // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted |
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php new file mode 100644 index 00000000..7b012674 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php | |||
@@ -0,0 +1,137 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Psr\Log\LoggerInterface; | ||
6 | use Psr\Log\NullLogger; | ||
7 | use Doctrine\ORM\EntityManager; | ||
8 | use Wallabag\CoreBundle\Entity\Entry; | ||
9 | use Wallabag\UserBundle\Entity\User; | ||
10 | use Wallabag\CoreBundle\Tools\Utils; | ||
11 | |||
12 | class WallabagV1Import implements ImportInterface | ||
13 | { | ||
14 | private $user; | ||
15 | private $em; | ||
16 | private $logger; | ||
17 | private $skippedEntries = 0; | ||
18 | private $importedEntries = 0; | ||
19 | private $filepath; | ||
20 | |||
21 | public function __construct(EntityManager $em) | ||
22 | { | ||
23 | $this->em = $em; | ||
24 | $this->logger = new NullLogger(); | ||
25 | } | ||
26 | |||
27 | public function setLogger(LoggerInterface $logger) | ||
28 | { | ||
29 | $this->logger = $logger; | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * We define the user in a custom call because on the import command there is no logged in user. | ||
34 | * So we can't retrieve user from the `security.token_storage` service. | ||
35 | * | ||
36 | * @param User $user | ||
37 | */ | ||
38 | public function setUser(User $user) | ||
39 | { | ||
40 | $this->user = $user; | ||
41 | |||
42 | return $this; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * {@inheritdoc} | ||
47 | */ | ||
48 | public function getName() | ||
49 | { | ||
50 | return 'Wallabag v1'; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * {@inheritdoc} | ||
55 | */ | ||
56 | public function getDescription() | ||
57 | { | ||
58 | return 'This importer will import all your wallabag v1 articles.'; | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * {@inheritdoc} | ||
63 | */ | ||
64 | public function import() | ||
65 | { | ||
66 | if (!$this->user) { | ||
67 | $this->logger->error('WallabagV1Import: user is not defined'); | ||
68 | |||
69 | return false; | ||
70 | } | ||
71 | |||
72 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | ||
73 | $this->logger->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath)); | ||
74 | |||
75 | return false; | ||
76 | } | ||
77 | |||
78 | $this->parseEntries(json_decode(file_get_contents($this->filepath), true)); | ||
79 | |||
80 | return true; | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * {@inheritdoc} | ||
85 | */ | ||
86 | public function getSummary() | ||
87 | { | ||
88 | return [ | ||
89 | 'skipped' => $this->skippedEntries, | ||
90 | 'imported' => $this->importedEntries, | ||
91 | ]; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Set file path to the json file. | ||
96 | * | ||
97 | * @param string $filepath | ||
98 | */ | ||
99 | public function setFilepath($filepath) | ||
100 | { | ||
101 | $this->filepath = $filepath; | ||
102 | |||
103 | return $this; | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * @param $entries | ||
108 | */ | ||
109 | private function parseEntries($entries) | ||
110 | { | ||
111 | foreach ($entries as $importedEntry) { | ||
112 | $existingEntry = $this->em | ||
113 | ->getRepository('WallabagCoreBundle:Entry') | ||
114 | ->existByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
115 | |||
116 | if (false !== $existingEntry) { | ||
117 | ++$this->skippedEntries; | ||
118 | continue; | ||
119 | } | ||
120 | |||
121 | // @see ContentProxy->updateEntry | ||
122 | $entry = new Entry($this->user); | ||
123 | $entry->setUrl($importedEntry['url']); | ||
124 | $entry->setTitle($importedEntry['title']); | ||
125 | $entry->setArchived($importedEntry['is_read']); | ||
126 | $entry->setStarred($importedEntry['is_fav']); | ||
127 | $entry->setContent($importedEntry['content']); | ||
128 | $entry->setReadingTime(Utils::getReadingTime($importedEntry['content'])); | ||
129 | $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST)); | ||
130 | |||
131 | $this->em->persist($entry); | ||
132 | ++$this->importedEntries; | ||
133 | } | ||
134 | |||
135 | $this->em->flush(); | ||
136 | } | ||
137 | } | ||