]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/WallabagImport.php
Validate imported entry to avoid error on import
[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 $filepath;
10 // untitled in all languages from v1
11 protected $untitled = [
12 'Untitled',
13 'Sans titre',
14 'podle nadpisu',
15 'Sin título',
16 'با عنوان',
17 'per titolo',
18 'Sem título',
19 'Без названия',
20 'po naslovu',
21 'Без назви',
22 'No title found',
23 '',
24 ];
25
26 /**
27 * {@inheritdoc}
28 */
29 abstract public function getName();
30
31 /**
32 * {@inheritdoc}
33 */
34 abstract public function getUrl();
35
36 /**
37 * {@inheritdoc}
38 */
39 abstract public function getDescription();
40
41 /**
42 * {@inheritdoc}
43 */
44 public function import()
45 {
46 if (!$this->user) {
47 $this->logger->error('WallabagImport: user is not defined');
48
49 return false;
50 }
51
52 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
53 $this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]);
54
55 return false;
56 }
57
58 $data = json_decode(file_get_contents($this->filepath), true);
59
60 if (empty($data)) {
61 $this->logger->error('WallabagImport: no entries in imported file');
62
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 * Set file path to the json file.
79 *
80 * @param string $filepath
81 */
82 public function setFilepath($filepath)
83 {
84 $this->filepath = $filepath;
85
86 return $this;
87 }
88
89 /**
90 * {@inheritdoc}
91 */
92 public function validateEntry(array $importedEntry)
93 {
94 if (empty($importedEntry['url'])) {
95 return false;
96 }
97
98 return true;
99 }
100
101 /**
102 * {@inheritdoc}
103 */
104 public function parseEntry(array $importedEntry)
105 {
106 $existingEntry = $this->em
107 ->getRepository('WallabagCoreBundle:Entry')
108 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
109
110 if (false !== $existingEntry) {
111 ++$this->skippedEntries;
112
113 return;
114 }
115
116 $data = $this->prepareEntry($importedEntry);
117
118 $entry = new Entry($this->user);
119 $entry->setUrl($data['url']);
120 $entry->setTitle($data['title']);
121
122 // update entry with content (in case fetching failed, the given entry will be return)
123 $this->fetchContent($entry, $data['url'], $data);
124
125 if (array_key_exists('tags', $data)) {
126 $this->tagsAssigner->assignTagsToEntry(
127 $entry,
128 $data['tags'],
129 $this->em->getUnitOfWork()->getScheduledEntityInsertions()
130 );
131 }
132
133 if (isset($importedEntry['preview_picture'])) {
134 $entry->setPreviewPicture($importedEntry['preview_picture']);
135 }
136
137 $entry->setArchived($data['is_archived']);
138 $entry->setStarred($data['is_starred']);
139
140 if (!empty($data['created_at'])) {
141 $entry->setCreatedAt(new \DateTime($data['created_at']));
142 }
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 }