diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/WallabagV1Import.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/WallabagV1Import.php | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php new file mode 100644 index 00000000..393089d6 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php | |||
@@ -0,0 +1,159 @@ | |||
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 getUrl() | ||
57 | { | ||
58 | return 'import_wallabag_v1'; | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * {@inheritdoc} | ||
63 | */ | ||
64 | public function getDescription() | ||
65 | { | ||
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.'; | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * {@inheritdoc} | ||
71 | */ | ||
72 | public function import() | ||
73 | { | ||
74 | if (!$this->user) { | ||
75 | $this->logger->error('WallabagV1Import: user is not defined'); | ||
76 | |||
77 | return false; | ||
78 | } | ||
79 | |||
80 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | ||
81 | $this->logger->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath)); | ||
82 | |||
83 | return false; | ||
84 | } | ||
85 | |||
86 | $data = json_decode(file_get_contents($this->filepath), true); | ||
87 | |||
88 | if (empty($data)) { | ||
89 | return false; | ||
90 | } | ||
91 | |||
92 | $this->parseEntries($data); | ||
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 | */ | ||
123 | private function parseEntries($entries) | ||
124 | { | ||
125 | $i = 1; | ||
126 | |||
127 | foreach ($entries as $importedEntry) { | ||
128 | $existingEntry = $this->em | ||
129 | ->getRepository('WallabagCoreBundle:Entry') | ||
130 | ->existByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
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; | ||
149 | |||
150 | // flush every 20 entries | ||
151 | if (($i % 20) === 0) { | ||
152 | $this->em->flush(); | ||
153 | } | ||
154 | ++$i; | ||
155 | } | ||
156 | |||
157 | $this->em->flush(); | ||
158 | } | ||
159 | } | ||