X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FImportBundle%2FImport%2FPocketImport.php;h=678d016a8bd11fcdcc178a4a9527924d43be0d5b;hb=8642f14220a834a434225d812e2ea07680b04cb2;hp=617d551483aa1ac96c451adbf97f413a9438916d;hpb=b0b893eafd3c1d66b8681a4a6420603af42ec084;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 617d5514..678d016a 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -2,42 +2,34 @@ namespace Wallabag\ImportBundle\Import; -use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Doctrine\ORM\EntityManager; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Helper\ContentProxy; +use Craue\ConfigBundle\Util\Config; -class PocketImport implements ImportInterface +class PocketImport extends AbstractImport { private $user; - private $em; - private $contentProxy; - private $logger; private $client; private $consumerKey; private $skippedEntries = 0; private $importedEntries = 0; + private $markAsRead; protected $accessToken; - public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey) + public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, Config $craueConfig) { $this->user = $tokenStorage->getToken()->getUser(); $this->em = $em; $this->contentProxy = $contentProxy; - $this->consumerKey = $consumerKey; + $this->consumerKey = $craueConfig->get('pocket_consumer_key'); $this->logger = new NullLogger(); } - public function setLogger(LoggerInterface $logger) - { - $this->logger = $logger; - } - /** * {@inheritdoc} */ @@ -59,7 +51,7 @@ class PocketImport implements ImportInterface */ public function getDescription() { - return 'This importer will import all your Pocket 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.'; + return 'import.pocket.description'; } /** @@ -67,7 +59,7 @@ class PocketImport implements ImportInterface * * @param string $redirectUri Redirect url in case of error * - * @return string request_token for callback method + * @return string|false request_token for callback method */ public function getRequestToken($redirectUri) { @@ -123,6 +115,26 @@ class PocketImport implements ImportInterface return true; } + /** + * Set whether articles must be all marked as read. + * + * @param bool $markAsRead + */ + public function setMarkAsRead($markAsRead) + { + $this->markAsRead = $markAsRead; + + return $this; + } + + /** + * Get whether articles must be all marked as read. + */ + public function getMarkAsRead() + { + return $this->markAsRead; + } + /** * {@inheritdoc} */ @@ -176,26 +188,6 @@ class PocketImport implements ImportInterface $this->client = $client; } - private function assignTagsToEntry(Entry $entry, $tags) - { - foreach ($tags as $tag) { - $label = trim($tag['tag']); - $tagEntity = $this->em - ->getRepository('WallabagCoreBundle:Tag') - ->findOneByLabel($label); - - if (is_object($tagEntity)) { - $entry->addTag($tagEntity); - } else { - $newTag = new Tag(); - $newTag->setLabel($label); - - $entry->addTag($newTag); - } - $this->em->flush(); - } - } - /** * @see https://getpocket.com/developer/docs/v3/retrieve * @@ -218,10 +210,16 @@ class PocketImport implements ImportInterface } $entry = new Entry($this->user); - $entry = $this->contentProxy->updateEntry($entry, $url); + $entry = $this->fetchContent($entry, $url); + + // jump to next entry in case of problem while getting content + if (false === $entry) { + ++$this->skippedEntries; + continue; + } // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted - if ($pocketEntry['status'] == 1) { + if ($pocketEntry['status'] == 1 || $this->markAsRead) { $entry->setArchived(true); } @@ -245,7 +243,10 @@ class PocketImport implements ImportInterface } if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { - $this->assignTagsToEntry($entry, $pocketEntry['tags']); + $this->contentProxy->assignTagsToEntry( + $entry, + array_keys($pocketEntry['tags']) + ); } $this->em->persist($entry); @@ -254,6 +255,7 @@ class PocketImport implements ImportInterface // flush every 20 entries if (($i % 20) === 0) { $this->em->flush(); + $this->em->clear($entry); } ++$i; }