X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FImportBundle%2FImport%2FPocketImport.php;h=798cfdaefe05cc0746340d8f21b2b8b38e2d616b;hb=33adf8dc915f4911fd454e98194c1b2323514d4a;hp=aeccc7bda0beaf99a9f893a3367dfd39467e2d4d;hpb=b1d05721cf37ab94ec1a6837fe79cf19474dd0ff;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index aeccc7bd..798cfdae 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -2,47 +2,48 @@ 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) + /** + * {@inheritdoc} + */ + public function getName() { - $this->logger = $logger; + return 'Pocket'; } /** * {@inheritdoc} */ - public function getName() + public function getUrl() { - return 'Pocket'; + return 'import_pocket'; } /** @@ -50,7 +51,7 @@ class PocketImport implements ImportInterface */ public function getDescription() { - return 'This importer will import all your Pocket data.'; + return 'import.pocket.description'; } /** @@ -58,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) { @@ -114,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} */ @@ -167,28 +188,6 @@ class PocketImport implements ImportInterface $this->client = $client; } - /** - * @todo move that in a more global place - */ - private function assignTagsToEntry(Entry $entry, $tags) - { - foreach ($tags as $tag) { - $label = trim($tag['tag']); - $tagEntity = $this->em - ->getRepository('WallabagCoreBundle:Tag') - ->findOneByLabelAndUserId($label, $this->user->getId()); - - if (is_object($tagEntity)) { - $entry->addTag($tagEntity); - } else { - $newTag = new Tag($this->user); - $newTag->setLabel($label); - $entry->addTag($newTag); - } - $this->em->flush(); - } - } - /** * @see https://getpocket.com/developer/docs/v3/retrieve * @@ -196,12 +195,14 @@ class PocketImport implements ImportInterface */ private function parseEntries($entries) { + $i = 1; + foreach ($entries as $pocketEntry) { $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; $existingEntry = $this->em ->getRepository('WallabagCoreBundle:Entry') - ->existByUrlAndUserId($url, $this->user->getId()); + ->findByUrlAndUserId($url, $this->user->getId()); if (false !== $existingEntry) { ++$this->skippedEntries; @@ -209,14 +210,20 @@ 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); } - // 0 or 1 - 1 If the item is favorited + // 0 or 1 - 1 If the item is starred if ($pocketEntry['favorite'] == 1) { $entry->setStarred(true); } @@ -236,11 +243,21 @@ 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); ++$this->importedEntries; + + // flush every 20 entries + if (($i % 20) === 0) { + $this->em->flush(); + $this->em->clear($entry); + } + ++$i; } $this->em->flush();