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