]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/WallabagImport.php
Change flash message for queued articles
[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 return false;
62 }
63
64 if ($this->producer) {
65 $this->parseEntriesForProducer($data);
66
67 return true;
68 }
69
70 $this->parseEntries($data);
71
72 return true;
73 }
74
75 /**
76 * Set file path to the json file.
77 *
78 * @param string $filepath
79 */
80 public function setFilepath($filepath)
81 {
82 $this->filepath = $filepath;
83
84 return $this;
85 }
86
87 /**
88 * {@inheritdoc}
89 */
90 public function parseEntry(array $importedEntry)
91 {
92 $existingEntry = $this->em
93 ->getRepository('WallabagCoreBundle:Entry')
94 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
95
96 if (false !== $existingEntry) {
97 ++$this->skippedEntries;
98
99 return;
100 }
101
102 $data = $this->prepareEntry($importedEntry);
103
104 $entry = $this->fetchContent(
105 new Entry($this->user),
106 $importedEntry['url'],
107 $data
108 );
109
110 // jump to next entry in case of problem while getting content
111 if (false === $entry) {
112 ++$this->skippedEntries;
113
114 return;
115 }
116
117 if (array_key_exists('tags', $data)) {
118 $this->contentProxy->assignTagsToEntry(
119 $entry,
120 $data['tags']
121 );
122 }
123
124 if (isset($importedEntry['preview_picture'])) {
125 $entry->setPreviewPicture($importedEntry['preview_picture']);
126 }
127
128 $entry->setArchived($data['is_archived']);
129 $entry->setStarred($data['is_starred']);
130
131 if (!empty($data['created_at'])) {
132 $entry->setCreatedAt(new \DateTime($data['created_at']));
133 }
134
135 $this->em->persist($entry);
136 ++$this->importedEntries;
137
138 return $entry;
139 }
140
141 /**
142 * This should return a cleaned array for a given entry to be given to `updateEntry`.
143 *
144 * @param array $entry Data from the imported file
145 *
146 * @return array
147 */
148 abstract protected function prepareEntry($entry = []);
149 }