3 namespace Wallabag\CoreBundle\Helper
;
6 use Psr\Log\LoggerInterface
as Logger
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\CoreBundle\Entity\Tag
;
9 use Wallabag\CoreBundle\Tools\Utils
;
10 use Wallabag\CoreBundle\Repository\TagRepository
;
13 * This kind of proxy class take care of getting the content from an url
14 * and update the entry with what it found.
21 protected $tagRepository;
23 public function __construct(Graby
$graby, RuleBasedTagger
$tagger, TagRepository
$tagRepository, Logger
$logger)
25 $this->graby
= $graby;
26 $this->tagger
= $tagger;
27 $this->logger
= $logger;
28 $this->tagRepository
= $tagRepository;
32 * Fetch content using graby and hydrate given entry with results information.
33 * In case we couldn't find content, we'll try to use Open Graph data.
35 * We can also force the content, in case of an import from the v1 for example, so the function won't
36 * fetch the content from the website but rather use information given with the $content parameter.
38 * @param Entry $entry Entry to update
39 * @param string $url Url to grab content for
40 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
44 public function updateEntry(Entry
$entry, $url, array $content = [])
46 // do we have to fetch the content or the provided one is ok?
47 if (empty($content) || false === $this->validateContent($content)) {
48 $content = $this->graby
->fetchContent($url);
51 $title = $content['title'];
52 if (!$title && isset($content['open_graph']['og_title'])) {
53 $title = $content['open_graph']['og_title'];
56 $html = $content['html'];
57 if (false === $html) {
58 $html = '<p>Unable to retrieve readable content.</p>';
60 if (isset($content['open_graph']['og_description'])) {
61 $html .= '<p><i>But we found a short description: </i></p>';
62 $html .= $content['open_graph']['og_description'];
66 $entry->setUrl($content['url'] ?: $url);
67 $entry->setTitle($title);
68 $entry->setContent($html);
69 $entry->setLanguage($content['language']);
70 $entry->setMimetype($content['content_type']);
71 $entry->setReadingTime(Utils
::getReadingTime($html));
73 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
74 if (false !== $domainName) {
75 $entry->setDomainName($domainName);
78 if (isset($content['open_graph']['og_image'])) {
79 $entry->setPreviewPicture($content['open_graph']['og_image']);
83 $this->tagger
->tag($entry);
84 } catch (\Exception
$e) {
85 $this->logger
->error('Error while trying to automatically tag an entry.', [
87 'error_msg' => $e->getMessage(),
95 * Assign some tags to an entry.
98 * @param array|string $tags An array of tag or a string coma separated of tag
99 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
100 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
102 public function assignTagsToEntry(Entry
$entry, $tags, array $entitiesReady = [])
104 if (!is_array($tags)) {
105 $tags = explode(',', $tags);
108 // keeps only Tag entity from the "not yet flushed entities"
109 $tagsNotYetFlushed = [];
110 foreach ($entitiesReady as $entity) {
111 if ($entity instanceof Tag
) {
112 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
116 foreach ($tags as $label) {
117 $label = trim($label);
120 if (0 === strlen($label)) {
124 if (isset($tagsNotYetFlushed[$label])) {
125 $tagEntity = $tagsNotYetFlushed[$label];
127 $tagEntity = $this->tagRepository
->findOneByLabel($label);
129 if (is_null($tagEntity)) {
130 $tagEntity = new Tag();
131 $tagEntity->setLabel($label);
135 // only add the tag on the entry if the relation doesn't exist
136 if (false === $entry->getTags()->contains($tagEntity)) {
137 $entry->addTag($tagEntity);
143 * Validate that the given content as enough value to be used
144 * instead of fetch the content from the url.
146 * @param array $content
148 * @return bool true if valid otherwise false
150 private function validateContent(array $content)
152 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
156 * Changing pictures URL in article content.
158 public static function filterPicture($content, $url, $id)
161 $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice
162 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER
);
163 foreach ($matches as $i => $link) {
164 $link[1] = trim($link[1]);
165 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) {
166 $absolute_path = self
::_getAbsoluteLink($link[2], $url);
167 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
168 $directory = self
::_createAssetsDirectory($id);
169 $fullpath = $directory.'/'.$filename;
171 if (in_array($absolute_path, $processing_pictures) === true) {
172 // replace picture's URL only if processing is OK : already processing -> go to next picture
176 if (self
::_downloadPictures($absolute_path, $fullpath) === true) {
177 $content = str_replace($matches[$i][2], Tools
::getPocheUrl().$fullpath, $content);
180 $processing_pictures[] = $absolute_path;
190 private static function _getAbsoluteLink($relativeLink, $url)
192 /* return if already absolute URL */
193 if (parse_url($relativeLink, PHP_URL_SCHEME
) != '') {
194 return $relativeLink;
197 /* queries and anchors */
198 if ($relativeLink[0] == '#' || $relativeLink[0] == '?') {
199 return $url.$relativeLink;
202 /* parse base URL and convert to local variables:
203 $scheme, $host, $path */
204 extract(parse_url($url));
206 /* remove non-directory element from path */
207 $path = preg_replace('#/[^/]*$#', '', $path);
209 /* destroy path if relative url points to root */
210 if ($relativeLink[0] == '/') {
214 /* dirty absolute URL */
215 $abs = $host.$path.'/'.$relativeLink;
217 /* replace '//' or '/./' or '/foo/../' with '/' */
218 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
219 for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {
222 /* absolute URL is ready! */
223 return $scheme.'://'.$abs;
227 * Downloading pictures.
229 * @return bool true if the download and processing is OK, false else
231 private static function _downloadPictures($absolute_path, $fullpath)
233 $rawdata = Tools
::getFile($absolute_path);
234 $fullpath = urldecode($fullpath);
236 if (file_exists($fullpath)) {
241 $file_ext = strrchr($fullpath, '.');
242 $whitelist = array('.jpg', '.jpeg', '.gif', '.png');
243 if (!(in_array($file_ext, $whitelist))) {
244 Tools
::logm('processed image with not allowed extension. Skipping '.$fullpath);
250 $imageinfo = getimagesize($absolute_path);
251 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/jpg' && $imageinfo['mime'] != 'image/png') {
252 Tools
::logm('processed image with bad header. Skipping '.$fullpath);
258 $im = imagecreatefromstring($rawdata);
260 Tools
::logm('error while regenerating image '.$fullpath);
265 switch ($imageinfo['mime']) {
267 $result = imagegif($im, $fullpath);
271 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
274 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
283 * Create a directory for an article.
285 * @param $id ID of the article
289 private static function _createAssetsDirectory($id)
291 $assets_path = ABS_PATH
;
292 if (!is_dir($assets_path)) {
293 mkdir($assets_path, 0715);
296 $article_directory = $assets_path.$id;
297 if (!is_dir($article_directory)) {
298 mkdir($article_directory, 0715);
301 return $article_directory;
305 * Remove the directory.
311 public static function removeDirectory($directory)
313 if (is_dir($directory)) {
314 $files = array_diff(scandir($directory), array('.', '..'));
315 foreach ($files as $file) {
316 (is_dir("$directory/$file")) ? self::removeDirectory("$directory/$file") : unlink("$directory/$file");
319 return rmdir($directory);