Integrate graby

This commit is contained in:
Jeremy Benoist 2015-08-24 12:27:17 +02:00
parent 8c55a9e6c9
commit fad316151c
10 changed files with 58 additions and 180 deletions

View file

@ -27,20 +27,10 @@
"email": "hello@wallabag.org",
"issues": "https://github.com/wallabag/wallabag/issues"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/wallabag/php-readability"
},
{
"type": "vcs",
"url": "https://github.com/wallabag/Fivefilters_Libraries"
}
],
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.7.0",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/orm": "~2.3",
"doctrine/doctrine-bundle": "~1.2",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
@ -60,10 +50,9 @@
"willdurand/hateoas-bundle": "~0.5.0",
"htmlawed/htmlawed": "~1.1.19",
"liip/theme-bundle": "~1.1.3",
"wallabag/php-readability": "~1.0.0",
"wallabag/Fivefilters_Libraries": "~1.0",
"pagerfanta/pagerfanta": "~1.0.3",
"lexik/form-filter-bundle": "~4.0"
"lexik/form-filter-bundle": "~4.0",
"j0k3r/graby": "dev-master"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "~2.2.0",

View file

@ -147,11 +147,16 @@ class WallabagRestController extends Controller
{
$url = $request->request->get('url');
$content = Extractor::extract($url);
$content = $this->get('wallabag_core.graby')->fetchContent($url);
$entry = new Entry($this->getUser());
$entry->setUrl($url);
$entry->setTitle($request->request->get('title') ?: $content->getTitle());
$entry->setContent($content->getBody());
$entry->setTitle($request->request->get('title') ?: $content['title']);
$entry->setContent($content['html']);
$entry->setMimetype($content['content_type']);
if (isset($content['open_graph']['og_image'])) {
$entry->setPreviewPicture($content['open_graph']['og_image']);
}
$tags = $request->request->get('tags', '');
if (!empty($tags)) {

View file

@ -10,3 +10,6 @@ services:
tags:
- { name: monolog.logger, channel: wsse }
arguments: ['@security.context', '@security.authentication.manager', '@logger']
wallabag_core.graby:
class: Graby\Graby

View file

@ -31,10 +31,14 @@ class EntryController extends Controller
$form->handleRequest($request);
if ($form->isValid()) {
$content = Extractor::extract($entry->getUrl());
$content = $this->get('wallabag_core.graby')->fetchContent($entry->getUrl());
$entry->setTitle($content->getTitle());
$entry->setContent($content->getBody());
$entry->setTitle($content['title']);
$entry->setContent($content['html']);
$entry->setMimetype($content['content_type']);
if (isset($content['open_graph']['og_image'])) {
$entry->setPreviewPicture($content['open_graph']['og_image']);
}
$em = $this->getDoctrine()->getManager();
$em->persist($entry);

View file

@ -108,6 +108,13 @@ class Entry
*/
private $domainName;
/**
* @var string
*
* @ORM\Column(name="preview_picture", type="text", nullable=true)
*/
private $previewPicture;
/**
* @var bool
*
@ -419,4 +426,29 @@ class Entry
{
$this->tags->removeElement($tag);
}
/**
* Set previewPicture
*
* @param string $previewPicture
*
* @return Entry
*/
public function setPreviewPicture($previewPicture)
{
$this->previewPicture = $previewPicture;
return $this;
}
/**
* Get previewPicture
*
* @return string
*/
public function getPreviewPicture()
{
return $this->previewPicture;
}
}

View file

@ -1,34 +0,0 @@
<?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;
}
}

View file

@ -1,28 +0,0 @@
<?php
namespace Wallabag\CoreBundle\Helper;
class Url
{
public $url;
public function __construct($url)
{
$this->url = base64_decode($url);
}
public function getUrl()
{
return $this->url;
}
public function setUrl($url)
{
$this->url = $url;
}
public function isCorrect()
{
return filter_var($this->url, FILTER_VALIDATE_URL) !== false;
}
}

View file

@ -30,3 +30,6 @@ services:
wallabag_core.doctrine.prefixed_naming_strategy:
class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy
arguments: [%database_table_prefix%]
wallabag_core.graby:
class: Graby\Graby

View file

@ -10,7 +10,7 @@
<div class="nav-wrapper cyan darken-1">
<ul>
<li>
<a class="waves-effect" href="/">
<a class="waves-effect" href="{{ path('homepage') }}">
<i class="mdi-action-exit-to-app"></i>
</a>
</li>
@ -36,7 +36,7 @@
</nav>
<ul id="slide-out" class="collapsible side-nav fixed reader-mode" data-collapsible="accordion">
<li class="bold border-bottom hide-on-med-and-down">
<a class="waves-effect collapsible-header" href="/">
<a class="waves-effect collapsible-header" href="{{ path('homepage') }}">
<i class="mdi-action-exit-to-app small"></i>
<span>{% trans %}back{% endtrans %}</span>
</a>

View file

@ -1,96 +0,0 @@
<?php
namespace Wallabag\CoreBundle\Service;
use Wallabag\CoreBundle\Helper\Content;
use Wallabag\CoreBundle\Helper\Url;
final class Extractor
{
public static function extract($url)
{
$pageContent = self::getPageContent(new Url(base64_encode($url)));
$title = $pageContent['rss']['channel']['item']['title'] ?: parse_url($url, PHP_URL_HOST);
$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);
}
}