]>
Commit | Line | Data |
---|---|---|
b1d05721 JB |
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 | { | |
6785f4aa NL |
14 | protected $user; |
15 | protected $em; | |
16 | protected $logger; | |
17 | protected $skippedEntries = 0; | |
18 | protected $importedEntries = 0; | |
19 | protected $filepath; | |
b1d05721 | 20 | |
b88cf91f | 21 | public function __construct(EntityManager $em) |
b1d05721 JB |
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 | { | |
d1af8ad4 | 50 | return 'wallabag v1'; |
b1d05721 JB |
51 | } |
52 | ||
7019c7cf JB |
53 | /** |
54 | * {@inheritdoc} | |
55 | */ | |
56 | public function getUrl() | |
57 | { | |
58 | return 'import_wallabag_v1'; | |
59 | } | |
60 | ||
b1d05721 JB |
61 | /** |
62 | * {@inheritdoc} | |
63 | */ | |
64 | public function getDescription() | |
65 | { | |
b88cf91f | 66 | return 'This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.'; |
b1d05721 JB |
67 | } |
68 | ||
69 | /** | |
70 | * {@inheritdoc} | |
71 | */ | |
72 | public function import() | |
73 | { | |
74 | if (!$this->user) { | |
6785f4aa | 75 | $this->logger->error('WallabagImport: user is not defined'); |
b1d05721 JB |
76 | |
77 | return false; | |
78 | } | |
79 | ||
80 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
6785f4aa | 81 | $this->logger->error('WallabagImport: unable to read file', array('filepath' => $this->filepath)); |
b1d05721 JB |
82 | |
83 | return false; | |
84 | } | |
85 | ||
7019c7cf JB |
86 | $data = json_decode(file_get_contents($this->filepath), true); |
87 | ||
88 | if (empty($data)) { | |
89 | return false; | |
90 | } | |
91 | ||
92 | $this->parseEntries($data); | |
b1d05721 JB |
93 | |
94 | return true; | |
95 | } | |
96 | ||
97 | /** | |
98 | * {@inheritdoc} | |
99 | */ | |
100 | public function getSummary() | |
101 | { | |
102 | return [ | |
103 | 'skipped' => $this->skippedEntries, | |
104 | 'imported' => $this->importedEntries, | |
105 | ]; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Set file path to the json file. | |
110 | * | |
111 | * @param string $filepath | |
112 | */ | |
113 | public function setFilepath($filepath) | |
114 | { | |
115 | $this->filepath = $filepath; | |
116 | ||
117 | return $this; | |
118 | } | |
119 | ||
120 | /** | |
121 | * @param $entries | |
122 | */ | |
6785f4aa | 123 | protected function parseEntries($entries) |
b1d05721 | 124 | { |
7019c7cf JB |
125 | $i = 1; |
126 | ||
b1d05721 JB |
127 | foreach ($entries as $importedEntry) { |
128 | $existingEntry = $this->em | |
129 | ->getRepository('WallabagCoreBundle:Entry') | |
78833672 | 130 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); |
b1d05721 JB |
131 | |
132 | if (false !== $existingEntry) { | |
133 | ++$this->skippedEntries; | |
134 | continue; | |
135 | } | |
136 | ||
137 | // @see ContentProxy->updateEntry | |
138 | $entry = new Entry($this->user); | |
139 | $entry->setUrl($importedEntry['url']); | |
140 | $entry->setTitle($importedEntry['title']); | |
141 | $entry->setArchived($importedEntry['is_read']); | |
142 | $entry->setStarred($importedEntry['is_fav']); | |
143 | $entry->setContent($importedEntry['content']); | |
144 | $entry->setReadingTime(Utils::getReadingTime($importedEntry['content'])); | |
145 | $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST)); | |
146 | ||
147 | $this->em->persist($entry); | |
148 | ++$this->importedEntries; | |
7019c7cf JB |
149 | |
150 | // flush every 20 entries | |
151 | if (($i % 20) === 0) { | |
8eedc8cf | 152 | $this->em->flush(); |
7019c7cf JB |
153 | } |
154 | ++$i; | |
b1d05721 JB |
155 | } |
156 | ||
157 | $this->em->flush(); | |
158 | } | |
159 | } |