diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/WallabagV1Import.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/WallabagV1Import.php | 177 |
1 files changed, 20 insertions, 157 deletions
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php index 82160bae..6cf3467a 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php | |||
@@ -2,49 +2,8 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Import; | 3 | namespace Wallabag\ImportBundle\Import; |
4 | 4 | ||
5 | use Psr\Log\LoggerInterface; | 5 | class WallabagV1Import extends WallabagImport |
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\Helper\ContentProxy; | ||
11 | |||
12 | class WallabagV1Import implements ImportInterface | ||
13 | { | 6 | { |
14 | protected $user; | ||
15 | protected $em; | ||
16 | protected $logger; | ||
17 | protected $contentProxy; | ||
18 | protected $skippedEntries = 0; | ||
19 | protected $importedEntries = 0; | ||
20 | protected $filepath; | ||
21 | protected $markAsRead; | ||
22 | |||
23 | public function __construct(EntityManager $em, ContentProxy $contentProxy) | ||
24 | { | ||
25 | $this->em = $em; | ||
26 | $this->logger = new NullLogger(); | ||
27 | $this->contentProxy = $contentProxy; | ||
28 | } | ||
29 | |||
30 | public function setLogger(LoggerInterface $logger) | ||
31 | { | ||
32 | $this->logger = $logger; | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * We define the user in a custom call because on the import command there is no logged in user. | ||
37 | * So we can't retrieve user from the `security.token_storage` service. | ||
38 | * | ||
39 | * @param User $user | ||
40 | */ | ||
41 | public function setUser(User $user) | ||
42 | { | ||
43 | $this->user = $user; | ||
44 | |||
45 | return $this; | ||
46 | } | ||
47 | |||
48 | /** | 7 | /** |
49 | * {@inheritdoc} | 8 | * {@inheritdoc} |
50 | */ | 9 | */ |
@@ -72,125 +31,29 @@ class WallabagV1Import implements ImportInterface | |||
72 | /** | 31 | /** |
73 | * {@inheritdoc} | 32 | * {@inheritdoc} |
74 | */ | 33 | */ |
75 | public function import() | 34 | protected function prepareEntry($entry = [], $markAsRead = false) |
76 | { | 35 | { |
77 | if (!$this->user) { | 36 | $data = [ |
78 | $this->logger->error('WallabagImport: user is not defined'); | 37 | 'title' => $entry['title'], |
79 | 38 | 'html' => $entry['content'], | |
80 | return false; | 39 | 'url' => $entry['url'], |
81 | } | 40 | 'content_type' => '', |
82 | 41 | 'language' => '', | |
83 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | 42 | 'is_archived' => $entry['is_read'] || $markAsRead, |
84 | $this->logger->error('WallabagImport: unable to read file', array('filepath' => $this->filepath)); | 43 | 'is_starred' => $entry['is_fav'], |
85 | 44 | 'tags' => '', | |
86 | return false; | ||
87 | } | ||
88 | |||
89 | $data = json_decode(file_get_contents($this->filepath), true); | ||
90 | |||
91 | if (empty($data)) { | ||
92 | return false; | ||
93 | } | ||
94 | |||
95 | $this->parseEntries($data); | ||
96 | |||
97 | return true; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * {@inheritdoc} | ||
102 | */ | ||
103 | public function getSummary() | ||
104 | { | ||
105 | return [ | ||
106 | 'skipped' => $this->skippedEntries, | ||
107 | 'imported' => $this->importedEntries, | ||
108 | ]; | 45 | ]; |
109 | } | ||
110 | |||
111 | /** | ||
112 | * Set file path to the json file. | ||
113 | * | ||
114 | * @param string $filepath | ||
115 | */ | ||
116 | public function setFilepath($filepath) | ||
117 | { | ||
118 | $this->filepath = $filepath; | ||
119 | |||
120 | return $this; | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Set whether articles must be all marked as read. | ||
125 | * | ||
126 | * @param bool $markAsRead | ||
127 | */ | ||
128 | public function setMarkAsRead($markAsRead) | ||
129 | { | ||
130 | $this->markAsRead = $markAsRead; | ||
131 | |||
132 | return $this; | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * @param $entries | ||
137 | */ | ||
138 | protected function parseEntries($entries) | ||
139 | { | ||
140 | $i = 1; | ||
141 | |||
142 | //Untitled in all languages from v1. This should never have been translated | ||
143 | $untitled = array('Untitled', 'Sans titre', 'podle nadpisu', 'Sin título', 'با عنوان', 'per titolo', 'Sem título', 'Без названия', 'po naslovu', 'Без назви', 'No title found', ''); | ||
144 | |||
145 | foreach ($entries as $importedEntry) { | ||
146 | $existingEntry = $this->em | ||
147 | ->getRepository('WallabagCoreBundle:Entry') | ||
148 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
149 | 46 | ||
150 | if (false !== $existingEntry) { | 47 | // force content to be refreshed in case on bad fetch in the v1 installation |
151 | ++$this->skippedEntries; | 48 | if (in_array($entry['title'], $this->untitled)) { |
152 | continue; | 49 | $data['title'] = ''; |
153 | } | 50 | $data['html'] = ''; |
154 | 51 | } | |
155 | $data = [ | ||
156 | 'title' => $importedEntry['title'], | ||
157 | 'html' => $importedEntry['content'], | ||
158 | 'url' => $importedEntry['url'], | ||
159 | 'content_type' => '', | ||
160 | 'language' => '', | ||
161 | ]; | ||
162 | |||
163 | // force content to be refreshed in case on bad fetch in the v1 installation | ||
164 | if (in_array($importedEntry['title'], $untitled)) { | ||
165 | $data = []; | ||
166 | } | ||
167 | |||
168 | $entry = $this->contentProxy->updateEntry( | ||
169 | new Entry($this->user), | ||
170 | $importedEntry['url'], | ||
171 | $data | ||
172 | ); | ||
173 | |||
174 | if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') { | ||
175 | $this->contentProxy->assignTagsToEntry( | ||
176 | $entry, | ||
177 | $importedEntry['tags'] | ||
178 | ); | ||
179 | } | ||
180 | |||
181 | $entry->setArchived($importedEntry['is_read'] || $this->markAsRead); | ||
182 | $entry->setStarred($importedEntry['is_fav']); | ||
183 | |||
184 | $this->em->persist($entry); | ||
185 | ++$this->importedEntries; | ||
186 | 52 | ||
187 | // flush every 20 entries | 53 | if (array_key_exists('tags', $entry) && $entry['tags'] != '') { |
188 | if (($i % 20) === 0) { | 54 | $data['tags'] = $entry['tags']; |
189 | $this->em->flush(); | ||
190 | } | ||
191 | ++$i; | ||
192 | } | 55 | } |
193 | 56 | ||
194 | $this->em->flush(); | 57 | return $data; |
195 | } | 58 | } |
196 | } | 59 | } |