From 6b767d1cc0e9697af95ec399fd612d203d10826a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 26 Jan 2015 22:15:19 +0100 Subject: refactoring for fetching content --- .../CoreBundle/Controller/EntryController.php | 11 ++- src/Wallabag/CoreBundle/Helper/Content.php | 34 ++++++++ src/Wallabag/CoreBundle/Service/Extractor.php | 92 ++++++++++++++++++++++ 3 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Helper/Content.php create mode 100644 src/Wallabag/CoreBundle/Service/Extractor.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 2ebb416c..48d19307 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Wallabag\CoreBundle\Repository; use Wallabag\CoreBundle\Entity\Entries; +use Wallabag\CoreBundle\Service\Extractor; use Wallabag\Wallabag\Tools; use Wallabag\Wallabag\Url; @@ -32,10 +33,12 @@ class EntryController extends Controller if ($form->isValid()) { - $content = Tools::getPageContent(new Url($entry->getUrl())); - var_dump($content);die; + $content = Extractor::extract($entry->getUrl()); - $em = $this->getDoctrine()->getEntityManager(); + $entry->setTitle($content->getTitle()); + $entry->setContent($content->getBody()); + + $em = $this->getDoctrine()->getManager(); $em->persist($entry); $em->flush(); @@ -170,7 +173,7 @@ class EntryController extends Controller */ public function deleteEntryAction(Request $request, Entries $entry) { - $em = $this->getDoctrine()->getEntityManager(); + $em = $this->getDoctrine()->getManager(); $em->remove($entry); $em->flush(); diff --git a/src/Wallabag/CoreBundle/Helper/Content.php b/src/Wallabag/CoreBundle/Helper/Content.php new file mode 100644 index 00000000..81acbad7 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/Content.php @@ -0,0 +1,34 @@ +title; + } + + public function setTitle($title) + { + $this->title = $title; + } + + public function getBody() + { + return $this->body; + } + + public function setBody($body) + { + $this->body = $body; + } +} \ No newline at end of file diff --git a/src/Wallabag/CoreBundle/Service/Extractor.php b/src/Wallabag/CoreBundle/Service/Extractor.php new file mode 100644 index 00000000..790386d4 --- /dev/null +++ b/src/Wallabag/CoreBundle/Service/Extractor.php @@ -0,0 +1,92 @@ +setTitle($title); + $content->setBody($body);; + + return $content; + } + /** + * Get the content for a given URL (by a call to FullTextFeed) + * + * @param Url $url + * @return mixed + */ + public static function getPageContent(Url $url) + { + // Saving and clearing context + $REAL = array(); + foreach( $GLOBALS as $key => $value ) { + if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) { + $GLOBALS[$key] = array(); + $REAL[$key] = $value; + } + } + // Saving and clearing session + if (isset($_SESSION)) { + $REAL_SESSION = array(); + foreach( $_SESSION as $key => $value ) { + $REAL_SESSION[$key] = $value; + unset($_SESSION[$key]); + } + } + + // Running code in different context + $scope = function() { + extract( func_get_arg(1) ); + $_GET = $_REQUEST = array( + "url" => $url->getUrl(), + "max" => 5, + "links" => "preserve", + "exc" => "", + "format" => "json", + "submit" => "Create Feed" + ); + ob_start(); + require func_get_arg(0); + $json = ob_get_contents(); + ob_end_clean(); + return $json; + }; + + // Silence $scope function to avoid + // issues with FTRSS when error_reporting is to high + // FTRSS generates PHP warnings which break output + $json = @$scope(__DIR__ . "/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php", array("url" => $url)); + + // Clearing and restoring context + foreach ($GLOBALS as $key => $value) { + if($key != "GLOBALS" && $key != "_SESSION" ) { + unset($GLOBALS[$key]); + } + } + foreach ($REAL as $key => $value) { + $GLOBALS[$key] = $value; + } + + // Clearing and restoring session + if (isset($REAL_SESSION)) { + foreach($_SESSION as $key => $value) { + unset($_SESSION[$key]); + } + + foreach($REAL_SESSION as $key => $value) { + $_SESSION[$key] = $value; + } + } + + return json_decode($json, true); + } +} \ No newline at end of file -- cgit v1.2.3