]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/PocketImport.php
Few phpDoc fix
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
CommitLineData
ff7b031d
NL
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
252ebd60
JB
5use Psr\Log\LoggerInterface;
6use Psr\Log\NullLogger;
ff7b031d
NL
7use Doctrine\ORM\EntityManager;
8use GuzzleHttp\Client;
252ebd60 9use GuzzleHttp\Exception\RequestException;
0aa344dc 10use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
ff7b031d 11use Wallabag\CoreBundle\Entity\Entry;
87f23b00 12use Wallabag\CoreBundle\Entity\Tag;
252ebd60 13use Wallabag\CoreBundle\Helper\ContentProxy;
ff7b031d
NL
14
15class PocketImport implements ImportInterface
16{
17 private $user;
ff7b031d 18 private $em;
252ebd60
JB
19 private $contentProxy;
20 private $logger;
8eedc8cf 21 private $client;
ff7b031d 22 private $consumerKey;
303768df
NL
23 private $skippedEntries = 0;
24 private $importedEntries = 0;
252ebd60 25 protected $accessToken;
ff7b031d 26
252ebd60 27 public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey)
ff7b031d
NL
28 {
29 $this->user = $tokenStorage->getToken()->getUser();
ff7b031d 30 $this->em = $em;
252ebd60 31 $this->contentProxy = $contentProxy;
ff7b031d 32 $this->consumerKey = $consumerKey;
252ebd60
JB
33 $this->logger = new NullLogger();
34 }
35
36 public function setLogger(LoggerInterface $logger)
37 {
38 $this->logger = $logger;
ff7b031d
NL
39 }
40
0aa344dc
JB
41 /**
42 * {@inheritdoc}
43 */
d51b38ed
NL
44 public function getName()
45 {
46 return 'Pocket';
47 }
48
7019c7cf
JB
49 /**
50 * {@inheritdoc}
51 */
52 public function getUrl()
53 {
54 return 'import_pocket';
55 }
56
0aa344dc
JB
57 /**
58 * {@inheritdoc}
59 */
d51b38ed
NL
60 public function getDescription()
61 {
7019c7cf 62 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data. Pocket doesn\'t allow us to retrieve content from their service, so the readable content of each article will be re-fetched by Wallabag.';
d51b38ed
NL
63 }
64
ff7b031d 65 /**
252ebd60
JB
66 * Return the oauth url to authenticate the client.
67 *
68 * @param string $redirectUri Redirect url in case of error
69 *
70 * @return string request_token for callback method
7ec2897e 71 */
252ebd60 72 public function getRequestToken($redirectUri)
7ec2897e
JB
73 {
74 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
75 [
76 'body' => json_encode([
77 'consumer_key' => $this->consumerKey,
78 'redirect_uri' => $redirectUri,
79 ]),
80 ]
81 );
82
252ebd60
JB
83 try {
84 $response = $this->client->send($request);
85 } catch (RequestException $e) {
86 $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
7ec2897e 87
252ebd60
JB
88 return false;
89 }
7ec2897e 90
252ebd60 91 return $response->json()['code'];
7ec2897e
JB
92 }
93
94 /**
252ebd60
JB
95 * Usually called by the previous callback to authorize the client.
96 * Then it return a token that can be used for next requests.
97 *
98 * @param string $code request_token from getRequestToken
99 *
100 * @return bool
7ec2897e 101 */
252ebd60 102 public function authorize($code)
7ec2897e
JB
103 {
104 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
105 [
106 'body' => json_encode([
107 'consumer_key' => $this->consumerKey,
252ebd60 108 'code' => $code,
7ec2897e
JB
109 ]),
110 ]
111 );
112
252ebd60
JB
113 try {
114 $response = $this->client->send($request);
115 } catch (RequestException $e) {
116 $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
7ec2897e 117
252ebd60
JB
118 return false;
119 }
120
121 $this->accessToken = $response->json()['access_token'];
122
123 return true;
7ec2897e
JB
124 }
125
126 /**
127 * {@inheritdoc}
128 */
252ebd60 129 public function import()
7ec2897e
JB
130 {
131 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
132 [
133 'body' => json_encode([
134 'consumer_key' => $this->consumerKey,
252ebd60 135 'access_token' => $this->accessToken,
7ec2897e
JB
136 'detailType' => 'complete',
137 'state' => 'all',
138 'sort' => 'oldest',
139 ]),
140 ]
141 );
142
252ebd60
JB
143 try {
144 $response = $this->client->send($request);
145 } catch (RequestException $e) {
146 $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
147
148 return false;
149 }
150
7ec2897e
JB
151 $entries = $response->json();
152
b1d05721 153 $this->parseEntries($entries['list']);
7ec2897e 154
252ebd60 155 return true;
7ec2897e
JB
156 }
157
158 /**
252ebd60 159 * {@inheritdoc}
ff7b031d 160 */
252ebd60 161 public function getSummary()
ff7b031d 162 {
252ebd60
JB
163 return [
164 'skipped' => $this->skippedEntries,
165 'imported' => $this->importedEntries,
166 ];
ff7b031d
NL
167 }
168
87f23b00 169 /**
252ebd60 170 * Set the Guzzle client.
87f23b00 171 *
252ebd60 172 * @param Client $client
87f23b00 173 */
252ebd60 174 public function setClient(Client $client)
87f23b00 175 {
252ebd60 176 $this->client = $client;
dda57bb9
NL
177 }
178
179 /**
252ebd60 180 * @todo move that in a more global place
dda57bb9 181 */
87f23b00
NL
182 private function assignTagsToEntry(Entry $entry, $tags)
183 {
184 foreach ($tags as $tag) {
185 $label = trim($tag['tag']);
186 $tagEntity = $this->em
187 ->getRepository('WallabagCoreBundle:Tag')
c5c7f90a 188 ->findOneByLabel($label);
87f23b00
NL
189
190 if (is_object($tagEntity)) {
191 $entry->addTag($tagEntity);
192 } else {
c5c7f90a 193 $newTag = new Tag();
87f23b00 194 $newTag->setLabel($label);
c5c7f90a 195
87f23b00
NL
196 $entry->addTag($newTag);
197 }
198 $this->em->flush();
199 }
200 }
201
ff7b031d 202 /**
252ebd60
JB
203 * @see https://getpocket.com/developer/docs/v3/retrieve
204 *
ff7b031d
NL
205 * @param $entries
206 */
b1d05721 207 private function parseEntries($entries)
ff7b031d 208 {
7019c7cf
JB
209 $i = 1;
210
87f23b00 211 foreach ($entries as $pocketEntry) {
252ebd60 212 $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url'];
dda57bb9
NL
213
214 $existingEntry = $this->em
215 ->getRepository('WallabagCoreBundle:Entry')
5a4bbcc9 216 ->existByUrlAndUserId($url, $this->user->getId());
dda57bb9 217
5a4bbcc9 218 if (false !== $existingEntry) {
dda57bb9
NL
219 ++$this->skippedEntries;
220 continue;
221 }
222
b1d05721 223 $entry = new Entry($this->user);
252ebd60 224 $entry = $this->contentProxy->updateEntry($entry, $url);
dda57bb9 225
252ebd60 226 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
87f23b00
NL
227 if ($pocketEntry['status'] == 1) {
228 $entry->setArchived(true);
229 }
252ebd60
JB
230
231 // 0 or 1 - 1 If the item is favorited
87f23b00
NL
232 if ($pocketEntry['favorite'] == 1) {
233 $entry->setStarred(true);
234 }
235
252ebd60
JB
236 $title = 'Untitled';
237 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
238 $title = $pocketEntry['resolved_title'];
239 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
240 $title = $pocketEntry['given_title'];
87f23b00 241 }
ff7b031d 242
252ebd60 243 $entry->setTitle($title);
ff7b031d 244
252ebd60
JB
245 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
246 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) {
247 $entry->setPreviewPicture($pocketEntry['images'][1]['src']);
ff7b031d
NL
248 }
249
252ebd60 250 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
303768df 251 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
ff7b031d
NL
252 }
253
87f23b00 254 $this->em->persist($entry);
dda57bb9 255 ++$this->importedEntries;
7019c7cf
JB
256
257 // flush every 20 entries
258 if (($i % 20) === 0) {
8eedc8cf 259 $this->em->flush();
7019c7cf
JB
260 }
261 ++$i;
ff7b031d
NL
262 }
263
264 $this->em->flush();
265 }
ff7b031d 266}