diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@smile.fr> | 2015-10-26 14:38:24 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-02 23:24:17 +0100 |
commit | dda57bb9443817e3a080d5d25343f5a7e15dd14f (patch) | |
tree | 75a2447f84aad68ff6a1efe01019b77a2148d339 | |
parent | 87f23b005c5f68f7463333a74317efa4eb9a9565 (diff) | |
download | wallabag-dda57bb9443817e3a080d5d25343f5a7e15dd14f.tar.gz wallabag-dda57bb9443817e3a080d5d25343f5a7e15dd14f.tar.zst wallabag-dda57bb9443817e3a080d5d25343f5a7e15dd14f.zip |
fix #1502 avoid duplicate entry and store pocket url in config
7 files changed, 87 insertions, 36 deletions
diff --git a/app/config/config.yml b/app/config/config.yml index 421b2db5..88b1a59f 100644 --- a/app/config/config.yml +++ b/app/config/config.yml | |||
@@ -33,6 +33,14 @@ wallabag_core: | |||
33 | import: | 33 | import: |
34 | allow_mimetypes: ['application/octet-stream', 'application/json', 'text/plain'] | 34 | allow_mimetypes: ['application/octet-stream', 'application/json', 'text/plain'] |
35 | 35 | ||
36 | wallabag_import: | ||
37 | importers: | ||
38 | pocket_urls: | ||
39 | oauth_request: https://getpocket.com/v3/oauth/request | ||
40 | auth_authorize: https://getpocket.com/auth/authorize | ||
41 | oauth_authorize: https://getpocket.com/v3/oauth/authorize | ||
42 | get: https://getpocket.com/v3/get | ||
43 | |||
36 | # Twig Configuration | 44 | # Twig Configuration |
37 | twig: | 45 | twig: |
38 | debug: "%kernel.debug%" | 46 | debug: "%kernel.debug%" |
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index fa580133..de2eedcb 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php | |||
@@ -41,6 +41,7 @@ class EntryController extends Controller | |||
41 | */ | 41 | */ |
42 | public function addEntryFormAction(Request $request) | 42 | public function addEntryFormAction(Request $request) |
43 | { | 43 | { |
44 | $em = $this->getDoctrine()->getManager(); | ||
44 | $entry = new Entry($this->getUser()); | 45 | $entry = new Entry($this->getUser()); |
45 | 46 | ||
46 | $form = $this->createForm(new NewEntryType(), $entry); | 47 | $form = $this->createForm(new NewEntryType(), $entry); |
@@ -48,6 +49,19 @@ class EntryController extends Controller | |||
48 | $form->handleRequest($request); | 49 | $form->handleRequest($request); |
49 | 50 | ||
50 | if ($form->isValid()) { | 51 | if ($form->isValid()) { |
52 | $existingEntry = $em | ||
53 | ->getRepository('WallabagCoreBundle:Entry') | ||
54 | ->findOneByUrl($entry->getUrl()); | ||
55 | |||
56 | if (!is_null($existingEntry)) { | ||
57 | $this->get('session')->getFlashBag()->add( | ||
58 | 'notice', | ||
59 | 'Entry already saved on '.$existingEntry->getCreatedAt()->format('d-m-Y') | ||
60 | ); | ||
61 | |||
62 | return $this->redirect($this->generateUrl('view', array('id' => $existingEntry->getId()))); | ||
63 | } | ||
64 | |||
51 | $this->updateEntry($entry); | 65 | $this->updateEntry($entry); |
52 | $this->get('session')->getFlashBag()->add( | 66 | $this->get('session')->getFlashBag()->add( |
53 | 'notice', | 67 | 'notice', |
diff --git a/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php index bacaff31..3c14104e 100644 --- a/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php +++ b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\DependencyInjection; | 3 | namespace Wallabag\ImportBundle\DependencyInjection; |
4 | 4 | ||
5 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
5 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; | 6 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
6 | use Symfony\Component\Config\Definition\ConfigurationInterface; | 7 | use Symfony\Component\Config\Definition\ConfigurationInterface; |
7 | 8 | ||
@@ -12,6 +13,22 @@ class Configuration implements ConfigurationInterface | |||
12 | $treeBuilder = new TreeBuilder(); | 13 | $treeBuilder = new TreeBuilder(); |
13 | $rootNode = $treeBuilder->root('wallabag_import'); | 14 | $rootNode = $treeBuilder->root('wallabag_import'); |
14 | 15 | ||
16 | $rootNode | ||
17 | ->children() | ||
18 | ->arrayNode('importers') | ||
19 | ->append($this->getURLs()) | ||
20 | ->end() | ||
21 | ->end() | ||
22 | ; | ||
23 | |||
15 | return $treeBuilder; | 24 | return $treeBuilder; |
16 | } | 25 | } |
26 | |||
27 | private function getURLs() | ||
28 | { | ||
29 | $node = new ArrayNodeDefinition('pocket_urls'); | ||
30 | $node->prototype('scalar')->end(); | ||
31 | |||
32 | return $node; | ||
33 | } | ||
17 | } | 34 | } |
diff --git a/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php index 4efcaace..07dc378d 100644 --- a/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php +++ b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php | |||
@@ -13,6 +13,7 @@ class WallabagImportExtension extends Extension | |||
13 | { | 13 | { |
14 | $configuration = new Configuration(); | 14 | $configuration = new Configuration(); |
15 | $config = $this->processConfiguration($configuration, $configs); | 15 | $config = $this->processConfiguration($configuration, $configs); |
16 | $container->setParameter('wallabag_import.pocket', $config['importers']['pocket_urls']); | ||
16 | 17 | ||
17 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | 18 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
18 | $loader->load('services.yml'); | 19 | $loader->load('services.yml'); |
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 6b93c180..51f73f4c 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php | |||
@@ -15,13 +15,19 @@ class PocketImport implements ImportInterface | |||
15 | private $session; | 15 | private $session; |
16 | private $em; | 16 | private $em; |
17 | private $consumerKey; | 17 | private $consumerKey; |
18 | private $skippedEntries; | ||
19 | private $importedEntries; | ||
20 | private $pocketURL; | ||
18 | 21 | ||
19 | public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) | 22 | public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey, $pocketURL) |
20 | { | 23 | { |
21 | $this->user = $tokenStorage->getToken()->getUser(); | 24 | $this->user = $tokenStorage->getToken()->getUser(); |
22 | $this->session = $session; | 25 | $this->session = $session; |
23 | $this->em = $em; | 26 | $this->em = $em; |
24 | $this->consumerKey = $consumerKey; | 27 | $this->consumerKey = $consumerKey; |
28 | $this->skippedEntries = 0; | ||
29 | $this->importedEntries = 0; | ||
30 | $this->pocketURL = $pocketURL; | ||
25 | } | 31 | } |
26 | 32 | ||
27 | public function getName() | 33 | public function getName() |
@@ -64,9 +70,25 @@ class PocketImport implements ImportInterface | |||
64 | return $pocketEntry['resolved_title']; | 70 | return $pocketEntry['resolved_title']; |
65 | } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { | 71 | } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { |
66 | return $pocketEntry['given_title']; | 72 | return $pocketEntry['given_title']; |
67 | } else { | ||
68 | return 'Untitled'; | ||
69 | } | 73 | } |
74 | |||
75 | return 'Untitled'; | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Returns the good URL for current entry. | ||
80 | * | ||
81 | * @param $pocketEntry | ||
82 | * | ||
83 | * @return string | ||
84 | */ | ||
85 | private function guessURL($pocketEntry) | ||
86 | { | ||
87 | if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') { | ||
88 | return $pocketEntry['resolved_url']; | ||
89 | } | ||
90 | |||
91 | return $pocketEntry['given_url']; | ||
70 | } | 92 | } |
71 | 93 | ||
72 | private function assignTagsToEntry(Entry $entry, $tags) | 94 | private function assignTagsToEntry(Entry $entry, $tags) |
@@ -95,7 +117,20 @@ class PocketImport implements ImportInterface | |||
95 | { | 117 | { |
96 | foreach ($entries as $pocketEntry) { | 118 | foreach ($entries as $pocketEntry) { |
97 | $entry = new Entry($this->user); | 119 | $entry = new Entry($this->user); |
98 | $entry->setUrl($pocketEntry['given_url']); | 120 | $url = $this->guessURL($pocketEntry); |
121 | |||
122 | $existingEntry = $this->em | ||
123 | ->getRepository('WallabagCoreBundle:Entry') | ||
124 | ->findOneByUrl($url); | ||
125 | |||
126 | if (!is_null($existingEntry)) { | ||
127 | ++$this->skippedEntries; | ||
128 | continue; | ||
129 | } | ||
130 | |||
131 | $entry->setUrl($url); | ||
132 | $entry->setDomainName(parse_url($url, PHP_URL_HOST)); | ||
133 | |||
99 | if ($pocketEntry['status'] == 1) { | 134 | if ($pocketEntry['status'] == 1) { |
100 | $entry->setArchived(true); | 135 | $entry->setArchived(true); |
101 | } | 136 | } |
@@ -118,20 +153,20 @@ class PocketImport implements ImportInterface | |||
118 | } | 153 | } |
119 | 154 | ||
120 | if (!empty($pocketEntry['tags'])) { | 155 | if (!empty($pocketEntry['tags'])) { |
121 | $this->assignTagsToEntry($entry, $pocketEntry['tags']); | 156 | // $this->assignTagsToEntry($entry, $pocketEntry['tags']); |
122 | } | 157 | } |
123 | 158 | ||
124 | $this->em->persist($entry); | 159 | $this->em->persist($entry); |
160 | ++$this->importedEntries; | ||
125 | } | 161 | } |
126 | 162 | ||
127 | $this->user->setLastPocketImport(new \DateTime()); | ||
128 | $this->em->flush(); | 163 | $this->em->flush(); |
129 | } | 164 | } |
130 | 165 | ||
131 | public function oAuthRequest($redirectUri, $callbackUri) | 166 | public function oAuthRequest($redirectUri, $callbackUri) |
132 | { | 167 | { |
133 | $client = $this->createClient(); | 168 | $client = $this->createClient(); |
134 | $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', | 169 | $request = $client->createRequest('POST', $this->pocketURL['oauth_request'], |
135 | [ | 170 | [ |
136 | 'body' => json_encode([ | 171 | 'body' => json_encode([ |
137 | 'consumer_key' => $this->consumerKey, | 172 | 'consumer_key' => $this->consumerKey, |
@@ -146,14 +181,14 @@ class PocketImport implements ImportInterface | |||
146 | // store code in session for callback method | 181 | // store code in session for callback method |
147 | $this->session->set('pocketCode', $values['code']); | 182 | $this->session->set('pocketCode', $values['code']); |
148 | 183 | ||
149 | return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; | 184 | return $this->pocketURL['auth_authorize'].'?request_token='.$values['code'].'&redirect_uri='.$callbackUri; |
150 | } | 185 | } |
151 | 186 | ||
152 | public function oAuthAuthorize() | 187 | public function oAuthAuthorize() |
153 | { | 188 | { |
154 | $client = $this->createClient(); | 189 | $client = $this->createClient(); |
155 | 190 | ||
156 | $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', | 191 | $request = $client->createRequest('POST', $this->pocketURL['oauth_authorize'], |
157 | [ | 192 | [ |
158 | 'body' => json_encode([ | 193 | 'body' => json_encode([ |
159 | 'consumer_key' => $this->consumerKey, | 194 | 'consumer_key' => $this->consumerKey, |
@@ -170,9 +205,8 @@ class PocketImport implements ImportInterface | |||
170 | public function import($accessToken) | 205 | public function import($accessToken) |
171 | { | 206 | { |
172 | $client = $this->createClient(); | 207 | $client = $this->createClient(); |
173 | $since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : ''); | ||
174 | 208 | ||
175 | $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', | 209 | $request = $client->createRequest('POST', $this->pocketURL['get'], |
176 | [ | 210 | [ |
177 | 'body' => json_encode([ | 211 | 'body' => json_encode([ |
178 | 'consumer_key' => $this->consumerKey, | 212 | 'consumer_key' => $this->consumerKey, |
@@ -180,7 +214,6 @@ class PocketImport implements ImportInterface | |||
180 | 'detailType' => 'complete', | 214 | 'detailType' => 'complete', |
181 | 'state' => 'all', | 215 | 'state' => 'all', |
182 | 'sort' => 'oldest', | 216 | 'sort' => 'oldest', |
183 | 'since' => $since, | ||
184 | ]), | 217 | ]), |
185 | ] | 218 | ] |
186 | ); | 219 | ); |
@@ -192,7 +225,7 @@ class PocketImport implements ImportInterface | |||
192 | 225 | ||
193 | $this->session->getFlashBag()->add( | 226 | $this->session->getFlashBag()->add( |
194 | 'notice', | 227 | 'notice', |
195 | count($entries['list']).' entries imported' | 228 | $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' |
196 | ); | 229 | ); |
197 | } | 230 | } |
198 | } | 231 | } |
diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml index 82628f08..8f224d88 100644 --- a/src/Wallabag/ImportBundle/Resources/config/services.yml +++ b/src/Wallabag/ImportBundle/Resources/config/services.yml | |||
@@ -6,3 +6,4 @@ services: | |||
6 | - @session | 6 | - @session |
7 | - @doctrine.orm.entity_manager | 7 | - @doctrine.orm.entity_manager |
8 | - %pocket_consumer_key% | 8 | - %pocket_consumer_key% |
9 | - %wallabag_import.pocket% | ||
diff --git a/src/Wallabag/UserBundle/Entity/User.php b/src/Wallabag/UserBundle/Entity/User.php index 4851999f..e6528420 100644 --- a/src/Wallabag/UserBundle/Entity/User.php +++ b/src/Wallabag/UserBundle/Entity/User.php | |||
@@ -84,13 +84,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
84 | */ | 84 | */ |
85 | private $trusted; | 85 | private $trusted; |
86 | 86 | ||
87 | /** | ||
88 | * @var date | ||
89 | * | ||
90 | * @ORM\Column(name="last_pocket_import", type="datetime", nullable=true) | ||
91 | */ | ||
92 | private $lastPocketImport; | ||
93 | |||
94 | public function __construct() | 87 | public function __construct() |
95 | { | 88 | { |
96 | parent::__construct(); | 89 | parent::__construct(); |
@@ -247,20 +240,4 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
247 | 240 | ||
248 | return false; | 241 | return false; |
249 | } | 242 | } |
250 | |||
251 | /** | ||
252 | * @return date | ||
253 | */ | ||
254 | public function getLastPocketImport() | ||
255 | { | ||
256 | return $this->lastPocketImport; | ||
257 | } | ||
258 | |||
259 | /** | ||
260 | * @param date $lastPocketImport | ||
261 | */ | ||
262 | public function setLastPocketImport($lastPocketImport) | ||
263 | { | ||
264 | $this->lastPocketImport = $lastPocketImport; | ||
265 | } | ||
266 | } | 243 | } |