/** * Fetch content using graby and hydrate given entry with results information. * In case we couldn't find content, we'll try to use Open Graph data. * * We can also force the content, in case of an import from the v1 for example, so the function won't * fetch the content from the website but rather use information given with the $content parameter. * * @param Entry $entry Entry to update * @param string $url Url to grab content for * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url * * @return Entry */ public function updateEntry(Entry $entry, $url, array $content = []) { // do we have to fetch the content or the provided one is ok? if (empty($content) || false === $this->validateContent($content)) { $content = $this->graby->fetchContent($url); } $title = $content['title']; if (!$title && isset($content['open_graph']['og_title'])) { $title = $content['open_graph']['og_title']; } $html = $content['html']; if (false === $html) { $html = '<p>Unable to retrieve readable content.</p>'; if (isset($content['open_graph']['og_description'])) { $html .= '<p><i>But we found a short description: </i></p>'; $html .= $content['open_graph']['og_description']; } } $entry->setUrl($content['url'] ?: $url); $entry->setTitle($title); $entry->setContent($html); $entry->setLanguage($content['language']); $entry->setMimetype($content['content_type']); $entry->setReadingTime(Utils::getReadingTime($html)); $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); if (false !== $domainName) { $entry->setDomainName($domainName); } if (isset($content['open_graph']['og_image'])) { $entry->setPreviewPicture($content['open_graph']['og_image']); } // if content is an image define as a preview too if (in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { $entry->setPreviewPicture($content['url']); } try { $this->tagger->tag($entry); } catch (\Exception $e) { $this->logger->error('Error while trying to automatically tag an entry.', ['entry_url' => $url, 'error_msg' => $e->getMessage()]); } return $entry; }
/** * {@inheritdoc} */ public function load(ObjectManager $manager) { $entry1 = new Entry($this->getReference('admin-user')); $entry1->setUrl('http://0.0.0.0/entry1'); $entry1->setReadingTime(11); $entry1->setDomainName('domain.io'); $entry1->setMimetype('text/html'); $entry1->setTitle('test title entry1'); $entry1->setContent('This is my content /o/'); $entry1->setLanguage('en'); $manager->persist($entry1); $this->addReference('entry1', $entry1); $entry2 = new Entry($this->getReference('admin-user')); $entry2->setUrl('http://0.0.0.0/entry2'); $entry2->setReadingTime(1); $entry2->setDomainName('domain.io'); $entry2->setMimetype('text/html'); $entry2->setTitle('test title entry2'); $entry2->setContent('This is my content /o/'); $entry2->setLanguage('fr'); $manager->persist($entry2); $this->addReference('entry2', $entry2); $entry3 = new Entry($this->getReference('bob-user')); $entry3->setUrl('http://0.0.0.0/entry3'); $entry3->setReadingTime(1); $entry3->setDomainName('domain.io'); $entry3->setMimetype('text/html'); $entry3->setTitle('test title entry3'); $entry3->setContent('This is my content /o/'); $entry3->setLanguage('en'); $entry3->addTag($this->getReference('foo-tag')); $entry3->addTag($this->getReference('bar-tag')); $manager->persist($entry3); $this->addReference('entry3', $entry3); $entry4 = new Entry($this->getReference('admin-user')); $entry4->setUrl('http://0.0.0.0/entry4'); $entry4->setReadingTime(12); $entry4->setDomainName('domain.io'); $entry4->setMimetype('text/html'); $entry4->setTitle('test title entry4'); $entry4->setContent('This is my content /o/'); $entry4->setLanguage('en'); $entry4->addTag($this->getReference('foo-tag')); $entry4->addTag($this->getReference('bar-tag')); $manager->persist($entry4); $this->addReference('entry4', $entry4); $entry5 = new Entry($this->getReference('admin-user')); $entry5->setUrl('http://0.0.0.0/entry5'); $entry5->setReadingTime(12); $entry5->setDomainName('domain.io'); $entry5->setMimetype('text/html'); $entry5->setTitle('test title entry5'); $entry5->setContent('This is my content /o/'); $entry5->setStarred(true); $entry5->setLanguage('fr'); $entry5->setPreviewPicture('http://0.0.0.0/image.jpg'); $manager->persist($entry5); $this->addReference('entry5', $entry5); $entry6 = new Entry($this->getReference('admin-user')); $entry6->setUrl('http://0.0.0.0/entry6'); $entry6->setReadingTime(12); $entry6->setDomainName('domain.io'); $entry6->setMimetype('text/html'); $entry6->setTitle('test title entry6'); $entry6->setContent('This is my content /o/'); $entry6->setArchived(true); $entry6->setLanguage('de'); $manager->persist($entry6); $this->addReference('entry6', $entry6); $manager->flush(); }
/** * It will create a new entry. * Browse to it. * Then remove it. * * And it'll check that user won't be redirected to the view page of the content when it had been removed */ public function testViewAndDelete() { $this->logInAs('admin'); $client = $this->getClient(); $em = $client->getContainer()->get('doctrine.orm.entity_manager'); // add a new content to be removed later $user = $em->getRepository('WallabagUserBundle:User')->findOneByUserName('admin'); $content = new Entry($user); $content->setUrl('http://1.1.1.1/entry'); $content->setReadingTime(12); $content->setDomainName('domain.io'); $content->setMimetype('text/html'); $content->setTitle('test title entry'); $content->setContent('This is my content /o/'); $content->setArchived(true); $content->setLanguage('fr'); $em->persist($content); $em->flush(); $client->request('GET', '/view/' . $content->getId()); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $client->request('GET', '/delete/' . $content->getId()); $this->assertEquals(302, $client->getResponse()->getStatusCode()); $client->followRedirect(); $this->assertEquals(200, $client->getResponse()->getStatusCode()); }