diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-03-28 16:43:33 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-03-28 16:43:33 +0200 |
commit | b787a7757ea73b9d10c14cb21758feb07dfc5885 (patch) | |
tree | 13989f7a843332cdee095d456863535a2854862c /src | |
parent | 0e49487bb0a004d526eb41e7d3fb44b566441e34 (diff) | |
download | wallabag-b787a7757ea73b9d10c14cb21758feb07dfc5885.tar.gz wallabag-b787a7757ea73b9d10c14cb21758feb07dfc5885.tar.zst wallabag-b787a7757ea73b9d10c14cb21758feb07dfc5885.zip |
Refacto wallabag import
Use an abstract class to store all common action from wallabag vX import.
Move specificity in v1 & v2 import.
Diffstat (limited to 'src')
6 files changed, 353 insertions, 308 deletions
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagController.php b/src/Wallabag/ImportBundle/Controller/WallabagController.php new file mode 100644 index 00000000..01883d4a --- /dev/null +++ b/src/Wallabag/ImportBundle/Controller/WallabagController.php | |||
@@ -0,0 +1,85 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Controller; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | ||
8 | |||
9 | /** | ||
10 | * Define Wallabag import for v1 and v2, since there are very similar. | ||
11 | */ | ||
12 | abstract class WallabagController extends Controller | ||
13 | { | ||
14 | /** | ||
15 | * Return the service to handle the import. | ||
16 | * | ||
17 | * @return \Wallabag\ImportBundle\Import\ImportInterface | ||
18 | */ | ||
19 | abstract protected function getImportService(); | ||
20 | |||
21 | /** | ||
22 | * Return the template used for the form. | ||
23 | * | ||
24 | * @return string | ||
25 | */ | ||
26 | abstract protected function getImportTemplate(); | ||
27 | |||
28 | /** | ||
29 | * Handle import request. | ||
30 | * | ||
31 | * @param Request $request | ||
32 | * | ||
33 | * @return Response|RedirectResponse | ||
34 | */ | ||
35 | public function indexAction(Request $request) | ||
36 | { | ||
37 | $form = $this->createForm(UploadImportType::class); | ||
38 | $form->handleRequest($request); | ||
39 | |||
40 | $wallabag = $this->getImportService(); | ||
41 | |||
42 | if ($form->isValid()) { | ||
43 | $file = $form->get('file')->getData(); | ||
44 | $markAsRead = $form->get('mark_as_read')->getData(); | ||
45 | $name = $this->getUser()->getId().'.json'; | ||
46 | |||
47 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | ||
48 | $res = $wallabag | ||
49 | ->setUser($this->getUser()) | ||
50 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | ||
51 | ->setMarkAsRead($markAsRead) | ||
52 | ->import(); | ||
53 | |||
54 | $message = 'flashes.import.notice.failed'; | ||
55 | |||
56 | if (true === $res) { | ||
57 | $summary = $wallabag->getSummary(); | ||
58 | $message = $this->get('translator')->trans('flashes.import.notice.summary', array( | ||
59 | '%imported%' => $summary['imported'], | ||
60 | '%skipped%' => $summary['skipped'], | ||
61 | )); | ||
62 | |||
63 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | ||
64 | } | ||
65 | |||
66 | $this->get('session')->getFlashBag()->add( | ||
67 | 'notice', | ||
68 | $message | ||
69 | ); | ||
70 | |||
71 | return $this->redirect($this->generateUrl('homepage')); | ||
72 | } else { | ||
73 | $this->get('session')->getFlashBag()->add( | ||
74 | 'notice', | ||
75 | 'flashes.import.notice.failed_on_file' | ||
76 | ); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | return $this->render($this->getImportTemplate(), [ | ||
81 | 'form' => $form->createView(), | ||
82 | 'import' => $wallabag, | ||
83 | ]); | ||
84 | } | ||
85 | } | ||
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php index 1bc9696d..3e748d57 100644 --- a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php +++ b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php | |||
@@ -2,64 +2,32 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | 3 | namespace Wallabag\ImportBundle\Controller; |
4 | 4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
7 | use Symfony\Component\HttpFoundation\Request; | 6 | use Symfony\Component\HttpFoundation\Request; |
8 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | ||
9 | 7 | ||
10 | class WallabagV1Controller extends Controller | 8 | class WallabagV1Controller extends WallabagController |
11 | { | 9 | { |
12 | /** | 10 | /** |
13 | * @Route("/wallabag-v1", name="import_wallabag_v1") | 11 | * {@inheritdoc} |
14 | */ | 12 | */ |
15 | public function indexAction(Request $request) | 13 | protected function getImportService() |
16 | { | 14 | { |
17 | $form = $this->createForm(UploadImportType::class); | 15 | return $this->get('wallabag_import.wallabag_v1.import'); |
18 | $form->handleRequest($request); | 16 | } |
19 | |||
20 | $wallabag = $this->get('wallabag_import.wallabag_v1.import'); | ||
21 | |||
22 | if ($form->isValid()) { | ||
23 | $file = $form->get('file')->getData(); | ||
24 | $markAsRead = $form->get('mark_as_read')->getData(); | ||
25 | $name = $this->getUser()->getId().'.json'; | ||
26 | |||
27 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | ||
28 | $res = $wallabag | ||
29 | ->setUser($this->getUser()) | ||
30 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | ||
31 | ->setMarkAsRead($markAsRead) | ||
32 | ->import(); | ||
33 | |||
34 | $message = 'flashes.import.notice.failed'; | ||
35 | |||
36 | if (true === $res) { | ||
37 | $summary = $wallabag->getSummary(); | ||
38 | $message = $this->get('translator')->trans('flashes.import.notice.summary', array( | ||
39 | '%imported%' => $summary['imported'], | ||
40 | '%skipped%' => $summary['skipped'], | ||
41 | )); | ||
42 | |||
43 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | ||
44 | } | ||
45 | |||
46 | $this->get('session')->getFlashBag()->add( | ||
47 | 'notice', | ||
48 | $message | ||
49 | ); | ||
50 | 17 | ||
51 | return $this->redirect($this->generateUrl('homepage')); | 18 | /** |
52 | } else { | 19 | * {@inheritdoc} |
53 | $this->get('session')->getFlashBag()->add( | 20 | */ |
54 | 'notice', | 21 | protected function getImportTemplate() |
55 | 'flashes.import.notice.failed_on_file' | 22 | { |
56 | ); | 23 | return 'WallabagImportBundle:WallabagV1:index.html.twig'; |
57 | } | 24 | } |
58 | } | ||
59 | 25 | ||
60 | return $this->render('WallabagImportBundle:WallabagV1:index.html.twig', [ | 26 | /** |
61 | 'form' => $form->createView(), | 27 | * @Route("/wallabag-v1", name="import_wallabag_v1") |
62 | 'import' => $wallabag, | 28 | */ |
63 | ]); | 29 | public function indexAction(Request $request) |
30 | { | ||
31 | return parent::indexAction($request); | ||
64 | } | 32 | } |
65 | } | 33 | } |
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php index 3e6428a0..c2a42165 100644 --- a/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php +++ b/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php | |||
@@ -2,64 +2,32 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | 3 | namespace Wallabag\ImportBundle\Controller; |
4 | 4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
7 | use Symfony\Component\HttpFoundation\Request; | 6 | use Symfony\Component\HttpFoundation\Request; |
8 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | ||
9 | 7 | ||
10 | class WallabagV2Controller extends Controller | 8 | class WallabagV2Controller extends WallabagController |
11 | { | 9 | { |
12 | /** | 10 | /** |
13 | * @Route("/wallabag-v2", name="import_wallabag_v2") | 11 | * {@inheritdoc} |
14 | */ | 12 | */ |
15 | public function indexAction(Request $request) | 13 | protected function getImportService() |
16 | { | 14 | { |
17 | $form = $this->createForm(UploadImportType::class); | 15 | return $this->get('wallabag_import.wallabag_v2.import'); |
18 | $form->handleRequest($request); | 16 | } |
19 | |||
20 | $wallabag = $this->get('wallabag_import.wallabag_v2.import'); | ||
21 | |||
22 | if ($form->isValid()) { | ||
23 | $file = $form->get('file')->getData(); | ||
24 | $markAsRead = $form->get('mark_as_read')->getData(); | ||
25 | $name = $this->getUser()->getId().'.json'; | ||
26 | |||
27 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | ||
28 | $res = $wallabag | ||
29 | ->setUser($this->getUser()) | ||
30 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | ||
31 | ->setMarkAsRead($markAsRead) | ||
32 | ->import(); | ||
33 | |||
34 | $message = 'flashes.import.notice.failed'; | ||
35 | |||
36 | if (true === $res) { | ||
37 | $summary = $wallabag->getSummary(); | ||
38 | $message = $this->get('translator')->trans('flashes.import.notice.summary', array( | ||
39 | '%imported%' => $summary['imported'], | ||
40 | '%skipped%' => $summary['skipped'], | ||
41 | )); | ||
42 | |||
43 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | ||
44 | } | ||
45 | |||
46 | $this->get('session')->getFlashBag()->add( | ||
47 | 'notice', | ||
48 | $message | ||
49 | ); | ||
50 | 17 | ||
51 | return $this->redirect($this->generateUrl('homepage')); | 18 | /** |
52 | } else { | 19 | * {@inheritdoc} |
53 | $this->get('session')->getFlashBag()->add( | 20 | */ |
54 | 'notice', | 21 | protected function getImportTemplate() |
55 | 'flashes.import.notice.failed_on_file' | 22 | { |
56 | ); | 23 | return 'WallabagImportBundle:WallabagV2:index.html.twig'; |
57 | } | 24 | } |
58 | } | ||
59 | 25 | ||
60 | return $this->render('WallabagImportBundle:WallabagV2:index.html.twig', [ | 26 | /** |
61 | 'form' => $form->createView(), | 27 | * @Route("/wallabag-v2", name="import_wallabag_v2") |
62 | 'import' => $wallabag, | 28 | */ |
63 | ]); | 29 | public function indexAction(Request $request) |
30 | { | ||
31 | return parent::indexAction($request); | ||
64 | } | 32 | } |
65 | } | 33 | } |
diff --git a/src/Wallabag/ImportBundle/Import/WallabagImport.php b/src/Wallabag/ImportBundle/Import/WallabagImport.php new file mode 100644 index 00000000..d65bc530 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/WallabagImport.php | |||
@@ -0,0 +1,204 @@ | |||
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\Helper\ContentProxy; | ||
11 | |||
12 | abstract class WallabagImport implements ImportInterface | ||
13 | { | ||
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 | // untitled in all languages from v1 | ||
23 | protected $untitled = [ | ||
24 | 'Untitled', | ||
25 | 'Sans titre', | ||
26 | 'podle nadpisu', | ||
27 | 'Sin título', | ||
28 | 'با عنوان', | ||
29 | 'per titolo', | ||
30 | 'Sem título', | ||
31 | 'Без названия', | ||
32 | 'po naslovu', | ||
33 | 'Без назви', | ||
34 | 'No title found', | ||
35 | '', | ||
36 | ]; | ||
37 | |||
38 | public function __construct(EntityManager $em, ContentProxy $contentProxy) | ||
39 | { | ||
40 | $this->em = $em; | ||
41 | $this->logger = new NullLogger(); | ||
42 | $this->contentProxy = $contentProxy; | ||
43 | } | ||
44 | |||
45 | public function setLogger(LoggerInterface $logger) | ||
46 | { | ||
47 | $this->logger = $logger; | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * We define the user in a custom call because on the import command there is no logged in user. | ||
52 | * So we can't retrieve user from the `security.token_storage` service. | ||
53 | * | ||
54 | * @param User $user | ||
55 | */ | ||
56 | public function setUser(User $user) | ||
57 | { | ||
58 | $this->user = $user; | ||
59 | |||
60 | return $this; | ||
61 | } | ||
62 | |||
63 | /** | ||
64 | * {@inheritdoc} | ||
65 | */ | ||
66 | abstract public function getName(); | ||
67 | |||
68 | /** | ||
69 | * {@inheritdoc} | ||
70 | */ | ||
71 | abstract public function getUrl(); | ||
72 | |||
73 | /** | ||
74 | * {@inheritdoc} | ||
75 | */ | ||
76 | abstract public function getDescription(); | ||
77 | |||
78 | /** | ||
79 | * {@inheritdoc} | ||
80 | */ | ||
81 | public function import() | ||
82 | { | ||
83 | if (!$this->user) { | ||
84 | $this->logger->error('WallabagImport: user is not defined'); | ||
85 | |||
86 | return false; | ||
87 | } | ||
88 | |||
89 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | ||
90 | $this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]); | ||
91 | |||
92 | return false; | ||
93 | } | ||
94 | |||
95 | $data = json_decode(file_get_contents($this->filepath), true); | ||
96 | |||
97 | if (empty($data)) { | ||
98 | return false; | ||
99 | } | ||
100 | |||
101 | $this->parseEntries($data); | ||
102 | |||
103 | return true; | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * {@inheritdoc} | ||
108 | */ | ||
109 | public function getSummary() | ||
110 | { | ||
111 | return [ | ||
112 | 'skipped' => $this->skippedEntries, | ||
113 | 'imported' => $this->importedEntries, | ||
114 | ]; | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Set file path to the json file. | ||
119 | * | ||
120 | * @param string $filepath | ||
121 | */ | ||
122 | public function setFilepath($filepath) | ||
123 | { | ||
124 | $this->filepath = $filepath; | ||
125 | |||
126 | return $this; | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * Set whether articles must be all marked as read. | ||
131 | * | ||
132 | * @param bool $markAsRead | ||
133 | */ | ||
134 | public function setMarkAsRead($markAsRead) | ||
135 | { | ||
136 | $this->markAsRead = $markAsRead; | ||
137 | |||
138 | return $this; | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * Parse and insert all given entries. | ||
143 | * | ||
144 | * @param $entries | ||
145 | */ | ||
146 | protected function parseEntries($entries) | ||
147 | { | ||
148 | $i = 1; | ||
149 | |||
150 | foreach ($entries as $importedEntry) { | ||
151 | $existingEntry = $this->em | ||
152 | ->getRepository('WallabagCoreBundle:Entry') | ||
153 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
154 | |||
155 | if (false !== $existingEntry) { | ||
156 | ++$this->skippedEntries; | ||
157 | continue; | ||
158 | } | ||
159 | |||
160 | $data = $this->prepareEntry($importedEntry, $this->markAsRead); | ||
161 | |||
162 | $entry = $this->contentProxy->updateEntry( | ||
163 | new Entry($this->user), | ||
164 | $importedEntry['url'], | ||
165 | $data | ||
166 | ); | ||
167 | |||
168 | if (array_key_exists('tags', $data)) { | ||
169 | $this->contentProxy->assignTagsToEntry( | ||
170 | $entry, | ||
171 | $data['tags'] | ||
172 | ); | ||
173 | } | ||
174 | |||
175 | if (isset($importedEntry['preview_picture'])) { | ||
176 | $entry->setPreviewPicture($importedEntry['preview_picture']); | ||
177 | } | ||
178 | |||
179 | $entry->setArchived($data['is_archived']); | ||
180 | $entry->setStarred($data['is_starred']); | ||
181 | |||
182 | $this->em->persist($entry); | ||
183 | ++$this->importedEntries; | ||
184 | |||
185 | // flush every 20 entries | ||
186 | if (($i % 20) === 0) { | ||
187 | $this->em->flush(); | ||
188 | } | ||
189 | ++$i; | ||
190 | } | ||
191 | |||
192 | $this->em->flush(); | ||
193 | } | ||
194 | |||
195 | /** | ||
196 | * This should return a cleaned array for a given entry to be given to `updateEntry`. | ||
197 | * | ||
198 | * @param array $entry Data from the imported file | ||
199 | * @param bool $markAsRead Should we mark as read content? | ||
200 | * | ||
201 | * @return array | ||
202 | */ | ||
203 | abstract protected function prepareEntry($entry = [], $markAsRead = false); | ||
204 | } | ||
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 | } |
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV2Import.php b/src/Wallabag/ImportBundle/Import/WallabagV2Import.php index b31d63a3..d0035b63 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagV2Import.php +++ b/src/Wallabag/ImportBundle/Import/WallabagV2Import.php | |||
@@ -2,9 +2,7 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Import; | 3 | namespace Wallabag\ImportBundle\Import; |
4 | 4 | ||
5 | use Wallabag\CoreBundle\Entity\Entry; | 5 | class WallabagV2Import extends WallabagImport |
6 | |||
7 | class WallabagV2Import extends WallabagV1Import implements ImportInterface | ||
8 | { | 6 | { |
9 | /** | 7 | /** |
10 | * {@inheritdoc} | 8 | * {@inheritdoc} |
@@ -31,55 +29,14 @@ class WallabagV2Import extends WallabagV1Import implements ImportInterface | |||
31 | } | 29 | } |
32 | 30 | ||
33 | /** | 31 | /** |
34 | * @param $entries | 32 | * {@inheritdoc} |
35 | */ | 33 | */ |
36 | protected function parseEntries($entries) | 34 | protected function prepareEntry($entry = [], $markAsRead = false) |
37 | { | 35 | { |
38 | $i = 1; | 36 | return [ |
39 | 37 | 'html' => $entry['content'], | |
40 | foreach ($entries as $importedEntry) { | 38 | 'content_type' => $entry['mimetype'], |
41 | $existingEntry = $this->em | 39 | 'is_archived' => ($entry['is_archived'] || $markAsRead), |
42 | ->getRepository('WallabagCoreBundle:Entry') | 40 | ] + $entry; |
43 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
44 | |||
45 | if (false !== $existingEntry) { | ||
46 | ++$this->skippedEntries; | ||
47 | continue; | ||
48 | } | ||
49 | |||
50 | $importedEntry['html'] = $importedEntry['content']; | ||
51 | $importedEntry['content_type'] = $importedEntry['mimetype']; | ||
52 | |||
53 | $entry = $this->contentProxy->updateEntry( | ||
54 | new Entry($this->user), | ||
55 | $importedEntry['url'], | ||
56 | $importedEntry | ||
57 | ); | ||
58 | |||
59 | if (array_key_exists('tags', $importedEntry) && !empty($importedEntry['tags'])) { | ||
60 | $this->contentProxy->assignTagsToEntry( | ||
61 | $entry, | ||
62 | $importedEntry['tags'] | ||
63 | ); | ||
64 | } | ||
65 | |||
66 | if (isset($importedEntry['preview_picture'])) { | ||
67 | $entry->setPreviewPicture($importedEntry['preview_picture']); | ||
68 | } | ||
69 | |||
70 | $entry->setArchived($importedEntry['is_archived'] || $this->markAsRead); | ||
71 | $entry->setStarred($importedEntry['is_starred']); | ||
72 | |||
73 | $this->em->persist($entry); | ||
74 | ++$this->importedEntries; | ||
75 | |||
76 | // flush every 20 entries | ||
77 | if (($i % 20) === 0) { | ||
78 | $this->em->flush(); | ||
79 | } | ||
80 | ++$i; | ||
81 | } | ||
82 | |||
83 | $this->em->flush(); | ||
84 | } | 41 | } |
85 | } | 42 | } |