]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
refactoring for fetching content
authorNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 26 Jan 2015 21:15:19 +0000 (22:15 +0100)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 26 Jan 2015 21:15:19 +0000 (22:15 +0100)
app/AppKernel.php
app/config/routing.yml
app/config/routing_dev.yml
src/Wallabag/CoreBundle/Controller/EntryController.php
src/Wallabag/CoreBundle/Helper/Content.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Service/Extractor.php [new file with mode: 0644]

index b0a66daddb5dcf4437cefa388beb933db62bb9e2..f03830388321714d7cfeed48fa8df0a2f943fa47 100644 (file)
@@ -16,13 +16,11 @@ class AppKernel extends Kernel
             new Symfony\Bundle\AsseticBundle\AsseticBundle(),
             new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
             new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
-            new AppBundle\AppBundle(),
             new Wallabag\CoreBundle\WallabagCoreBundle(),
         );
 
         if (in_array($this->getEnvironment(), array('dev', 'test'))) {
             $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
-            $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
             $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
             $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
             $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
index b3c5291a8745400bc137fbcb2554f61fe85439f5..95c224abf5821f6f48dc6e0282b985af9b4c326f 100644 (file)
@@ -4,4 +4,4 @@ app:
 
 homepage:
     pattern:   /
-    defaults:  { _controller: CoreBundle:Entry:showUnread }
\ No newline at end of file
+    defaults:  { _controller: WallabagCoreBundle:Entry:showUnread }
\ No newline at end of file
index 9913005865422a6f7af9489e5351db78b0803bba..1a236e2868d89fa3be70fe9f9ab82d11ab441799 100644 (file)
@@ -15,8 +15,4 @@ _errors:
     prefix:   /_error
 
 _main:
-    resource: routing.yml
-
-# AcmeDemoBundle routes (to be removed)
-_acme_demo:
-    resource: "@AcmeDemoBundle/Resources/config/routing.yml"
\ No newline at end of file
+    resource: routing.yml
\ No newline at end of file
index 2ebb416c654671e1b530f0b518c4d8e02072aa16..48d1930753a2f6799b1377e697bb3adbb20ca63e 100644 (file)
@@ -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 (file)
index 0000000..81acbad
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace Wallabag\CoreBundle\Helper;
+
+class Content
+{
+    private $title;
+
+    private $body;
+
+    public function __constructor() {
+
+    }
+
+    public function getTitle()
+    {
+        return $this->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 (file)
index 0000000..790386d
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+
+namespace Wallabag\CoreBundle\Service;
+
+use Wallabag\CoreBundle\Helper\Content;
+use Wallabag\Wallabag\Url;
+
+final class Extractor
+{
+    public static function extract($url) {
+        $pageContent = Extractor::getPageContent(new Url(base64_encode($url)));
+        $title = ($pageContent['rss']['channel']['item']['title'] != '') ? $pageContent['rss']['channel']['item']['title'] : _('Untitled');
+        $body = $pageContent['rss']['channel']['item']['description'];
+
+        $content = new Content();
+        $content->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