]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/WallabagImport.php
Convert other imports to Rabbit
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / WallabagImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Wallabag\CoreBundle\Entity\Entry;
6 use Wallabag\UserBundle\Entity\User;
7
8 abstract class WallabagImport extends AbstractImport
9 {
10 protected $user;
11 protected $skippedEntries = 0;
12 protected $importedEntries = 0;
13 protected $filepath;
14 // untitled in all languages from v1
15 protected $untitled = [
16 'Untitled',
17 'Sans titre',
18 'podle nadpisu',
19 'Sin título',
20 'با عنوان',
21 'per titolo',
22 'Sem título',
23 'Без названия',
24 'po naslovu',
25 'Без назви',
26 'No title found',
27 '',
28 ];
29
30 /**
31 * {@inheritdoc}
32 */
33 abstract public function getName();
34
35 /**
36 * {@inheritdoc}
37 */
38 abstract public function getUrl();
39
40 /**
41 * {@inheritdoc}
42 */
43 abstract public function getDescription();
44
45 /**
46 * {@inheritdoc}
47 */
48 public function import()
49 {
50 if (!$this->user) {
51 $this->logger->error('WallabagImport: user is not defined');
52
53 return false;
54 }
55
56 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
57 $this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]);
58
59 return false;
60 }
61
62 $data = json_decode(file_get_contents($this->filepath), true);
63
64 if (empty($data)) {
65 return false;
66 }
67
68 if ($this->producer) {
69 $this->parseEntriesForProducer($data);
70
71 return true;
72 }
73
74 $this->parseEntries($data);
75
76 return true;
77 }
78
79 /**
80 * {@inheritdoc}
81 */
82 public function getSummary()
83 {
84 return [
85 'skipped' => $this->skippedEntries,
86 'imported' => $this->importedEntries,
87 ];
88 }
89
90 /**
91 * Set file path to the json file.
92 *
93 * @param string $filepath
94 */
95 public function setFilepath($filepath)
96 {
97 $this->filepath = $filepath;
98
99 return $this;
100 }
101
102 /**
103 * {@inheritdoc}
104 */
105 public function parseEntry(array $importedEntry)
106 {
107 $existingEntry = $this->em
108 ->getRepository('WallabagCoreBundle:Entry')
109 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
110
111 if (false !== $existingEntry) {
112 ++$this->skippedEntries;
113
114 return;
115 }
116
117 $data = $this->prepareEntry($importedEntry);
118
119 $entry = $this->fetchContent(
120 new Entry($this->user),
121 $importedEntry['url'],
122 $data
123 );
124
125 // jump to next entry in case of problem while getting content
126 if (false === $entry) {
127 ++$this->skippedEntries;
128
129 return;
130 }
131
132 if (array_key_exists('tags', $data)) {
133 $this->contentProxy->assignTagsToEntry(
134 $entry,
135 $data['tags']
136 );
137 }
138
139 if (isset($importedEntry['preview_picture'])) {
140 $entry->setPreviewPicture($importedEntry['preview_picture']);
141 }
142
143 $entry->setArchived($data['is_archived']);
144 $entry->setStarred($data['is_starred']);
145
146 $this->em->persist($entry);
147 ++$this->importedEntries;
148
149 return $entry;
150 }
151
152 /**
153 * This should return a cleaned array for a given entry to be given to `updateEntry`.
154 *
155 * @param array $entry Data from the imported file
156 *
157 * @return array
158 */
159 abstract protected function prepareEntry($entry = []);
160 }