]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Service/Extractor.php
phpdoc
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Service / Extractor.php
CommitLineData
6b767d1c
NL
1<?php
2
3namespace Wallabag\CoreBundle\Service;
4
5use Wallabag\CoreBundle\Helper\Content;
b9ec99e2 6use Wallabag\CoreBundle\Helper\Url;
6b767d1c
NL
7
8final class Extractor
9{
7df80cb3
J
10 public static function extract($url)
11 {
6b767d1c 12 $pageContent = Extractor::getPageContent(new Url(base64_encode($url)));
94f2364c 13 $title = $pageContent['rss']['channel']['item']['title'] ?: 'Untitled';
6b767d1c
NL
14 $body = $pageContent['rss']['channel']['item']['description'];
15
16 $content = new Content();
17 $content->setTitle($title);
7df80cb3 18 $content->setBody($body);
6b767d1c
NL
19
20 return $content;
21 }
94f2364c 22
6b767d1c
NL
23 /**
24 * Get the content for a given URL (by a call to FullTextFeed)
25 *
7df80cb3 26 * @param Url $url
6b767d1c
NL
27 * @return mixed
28 */
29 public static function getPageContent(Url $url)
30 {
31 // Saving and clearing context
32 $REAL = array();
7df80cb3
J
33 foreach ($GLOBALS as $key => $value) {
34 if ($key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS') {
6b767d1c
NL
35 $GLOBALS[$key] = array();
36 $REAL[$key] = $value;
37 }
38 }
39 // Saving and clearing session
40 if (isset($_SESSION)) {
41 $REAL_SESSION = array();
7df80cb3 42 foreach ($_SESSION as $key => $value) {
6b767d1c
NL
43 $REAL_SESSION[$key] = $value;
44 unset($_SESSION[$key]);
45 }
46 }
47
48 // Running code in different context
7df80cb3
J
49 $scope = function () {
50 extract(func_get_arg(1));
6b767d1c
NL
51 $_GET = $_REQUEST = array(
52 "url" => $url->getUrl(),
53 "max" => 5,
54 "links" => "preserve",
55 "exc" => "",
56 "format" => "json",
7df80cb3 57 "submit" => "Create Feed",
6b767d1c
NL
58 );
59 ob_start();
60 require func_get_arg(0);
61 $json = ob_get_contents();
62 ob_end_clean();
7df80cb3 63
6b767d1c
NL
64 return $json;
65 };
66
67 // Silence $scope function to avoid
68 // issues with FTRSS when error_reporting is to high
69 // FTRSS generates PHP warnings which break output
7df80cb3 70 $json = @$scope(__DIR__."/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php", array("url" => $url));
6b767d1c
NL
71
72 // Clearing and restoring context
73 foreach ($GLOBALS as $key => $value) {
7df80cb3 74 if ($key != "GLOBALS" && $key != "_SESSION") {
6b767d1c
NL
75 unset($GLOBALS[$key]);
76 }
77 }
78 foreach ($REAL as $key => $value) {
79 $GLOBALS[$key] = $value;
80 }
81
82 // Clearing and restoring session
83 if (isset($REAL_SESSION)) {
7df80cb3 84 foreach ($_SESSION as $key => $value) {
6b767d1c
NL
85 unset($_SESSION[$key]);
86 }
87
7df80cb3 88 foreach ($REAL_SESSION as $key => $value) {
6b767d1c
NL
89 $_SESSION[$key] = $value;
90 }
91 }
92
93 return json_decode($json, true);
94 }
7df80cb3 95}