in RSS feed, add link to wallabag URL #556

This commit is contained in:
Nicolas Lœuillet 2014-04-06 16:39:11 +02:00
parent 2dd5c1e4a3
commit f86784c22d
2 changed files with 216 additions and 203 deletions

View file

@ -10,177 +10,189 @@
*/ */
class FeedItem class FeedItem
{ {
private $elements = array(); //Collection of feed elements private $elements = array(); //Collection of feed elements
private $version; private $version;
/** /**
* Constructor * Constructor
* *
* @param contant (RSS1/RSS2/ATOM) RSS2 is default. * @param contant (RSS1/RSS2/ATOM) RSS2 is default.
*/ */
function __construct($version = RSS2) function __construct($version = RSS2)
{ {
$this->version = $version; $this->version = $version;
} }
/** /**
* Set element (overwrites existing elements with $elementName) * Set element (overwrites existing elements with $elementName)
* *
* @access public * @access public
* @param srting The tag name of an element * @param srting The tag name of an element
* @param srting The content of tag * @param srting The content of tag
* @param array Attributes(if any) in 'attrName' => 'attrValue' format * @param array Attributes(if any) in 'attrName' => 'attrValue' format
* @return void * @return void
*/ */
public function setElement($elementName, $content, $attributes = null) public function setElement($elementName, $content, $attributes = null)
{ {
if (isset($this->elements[$elementName])) { if (isset($this->elements[$elementName])) {
unset($this->elements[$elementName]); unset($this->elements[$elementName]);
} }
$this->addElement($elementName, $content, $attributes); $this->addElement($elementName, $content, $attributes);
} }
/** /**
* Add an element to elements array * Add an element to elements array
* *
* @access public * @access public
* @param srting The tag name of an element * @param srting The tag name of an element
* @param srting The content of tag * @param srting The content of tag
* @param array Attributes(if any) in 'attrName' => 'attrValue' format * @param array Attributes(if any) in 'attrName' => 'attrValue' format
* @return void * @return void
*/ */
public function addElement($elementName, $content, $attributes = null) public function addElement($elementName, $content, $attributes = null)
{ {
$i = 0; $i = 0;
if (isset($this->elements[$elementName])) { if (isset($this->elements[$elementName])) {
$i = count($this->elements[$elementName]); $i = count($this->elements[$elementName]);
} else { } else {
$this->elements[$elementName] = array(); $this->elements[$elementName] = array();
} }
$this->elements[$elementName][$i]['name'] = $elementName; $this->elements[$elementName][$i]['name'] = $elementName;
$this->elements[$elementName][$i]['content'] = $content; $this->elements[$elementName][$i]['content'] = $content;
$this->elements[$elementName][$i]['attributes'] = $attributes; $this->elements[$elementName][$i]['attributes'] = $attributes;
} }
/** /**
* Set multiple feed elements from an array. * Set multiple feed elements from an array.
* Elements which have attributes cannot be added by this method * Elements which have attributes cannot be added by this method
* *
* @access public * @access public
* @param array array of elements in 'tagName' => 'tagContent' format. * @param array array of elements in 'tagName' => 'tagContent' format.
* @return void * @return void
*/ */
public function addElementArray($elementArray) public function addElementArray($elementArray)
{ {
if(! is_array($elementArray)) return; if(! is_array($elementArray)) return;
foreach ($elementArray as $elementName => $content) foreach ($elementArray as $elementName => $content)
{ {
$this->addElement($elementName, $content); $this->addElement($elementName, $content);
} }
} }
/** /**
* Return the collection of elements in this feed item * Return the collection of elements in this feed item
* *
* @access public * @access public
* @return array * @return array
*/ */
public function getElements() public function getElements()
{ {
return $this->elements; return $this->elements;
} }
// Wrapper functions ------------------------------------------------------ // Wrapper functions ------------------------------------------------------
/** /**
* Set the 'dscription' element of feed item * Set the 'dscription' element of feed item
* *
* @access public * @access public
* @param string The content of 'description' element * @param string The content of 'description' element
* @return void * @return void
*/ */
public function setDescription($description) public function setDescription($description)
{ {
$tag = 'description'; $tag = 'description';
$this->setElement($tag, $description); $this->setElement($tag, $description);
} }
/** /**
* @desc Set the 'title' element of feed item * @desc Set the 'title' element of feed item
* @access public * @access public
* @param string The content of 'title' element * @param string The content of 'title' element
* @return void * @return void
*/ */
public function setTitle($title) public function setTitle($title)
{ {
$this->setElement('title', $title); $this->setElement('title', $title);
} }
/** /**
* Set the 'date' element of feed item * Set the 'date' element of feed item
* *
* @access public * @access public
* @param string The content of 'date' element * @param string The content of 'date' element
* @return void * @return void
*/ */
public function setDate($date) public function setDate($date)
{ {
if(! is_numeric($date)) if(! is_numeric($date))
{ {
$date = strtotime($date); $date = strtotime($date);
} }
if($this->version == RSS2) if($this->version == RSS2)
{ {
$tag = 'pubDate'; $tag = 'pubDate';
$value = date(DATE_RSS, $date); $value = date(DATE_RSS, $date);
} }
else else
{ {
$tag = 'dc:date'; $tag = 'dc:date';
$value = date("Y-m-d", $date); $value = date("Y-m-d", $date);
} }
$this->setElement($tag, $value); $this->setElement($tag, $value);
} }
/** /**
* Set the 'link' element of feed item * Set the 'link' element of feed item
* *
* @access public * @access public
* @param string The content of 'link' element * @param string The content of 'link' element
* @return void * @return void
*/ */
public function setLink($link) public function setLink($link)
{ {
if($this->version == RSS2 || $this->version == RSS1) if($this->version == RSS2 || $this->version == RSS1)
{ {
$this->setElement('link', $link); $this->setElement('link', $link);
$this->setElement('guid', $link); $this->setElement('guid', $link);
} }
else else
{ {
$this->setElement('link','',array('href'=>$link)); $this->setElement('link','',array('href'=>$link));
$this->setElement('id', FeedWriter::uuid($link,'urn:uuid:')); $this->setElement('id', FeedWriter::uuid($link,'urn:uuid:'));
} }
} }
/** /**
* Set the 'encloser' element of feed item * Set the 'source' element of feed item
* For RSS 2.0 only *
* * @access public
* @access public * @param string The content of 'source' element
* @param string The url attribute of encloser tag * @return void
* @param string The length attribute of encloser tag */
* @param string The type attribute of encloser tag public function setSource($link)
* @return void {
*/ $this->setElement('source', $link);
public function setEncloser($url, $length, $type) }
{
$attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type); /**
$this->setElement('enclosure','',$attributes); * Set the 'encloser' element of feed item
} * For RSS 2.0 only
*
* @access public
* @param string The url attribute of encloser tag
* @param string The length attribute of encloser tag
* @param string The type attribute of encloser tag
* @return void
*/
public function setEncloser($url, $length, $type)
{
$attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type);
$this->setElement('enclosure','',$attributes);
}
} // end of class FeedItem } // end of class FeedItem
?> ?>

View file

@ -828,7 +828,7 @@ class Poche
define('IMPORT_LIMIT', 5); define('IMPORT_LIMIT', 5);
} }
if (!defined('IMPORT_DELAY')) { if (!defined('IMPORT_DELAY')) {
define('IMPORT_DELAY', 5); define('IMPORT_DELAY', 5);
} }
if ( isset($_FILES['file']) ) { if ( isset($_FILES['file']) ) {
@ -844,18 +844,18 @@ class Poche
$read = 0; $read = 0;
foreach (array('ol','ul') as $list) { foreach (array('ol','ul') as $list) {
foreach ($html->find($list) as $ul) { foreach ($html->find($list) as $ul) {
foreach ($ul->find('li') as $li) { foreach ($ul->find('li') as $li) {
$tmpEntry = array(); $tmpEntry = array();
$a = $li->find('a'); $a = $li->find('a');
$tmpEntry['url'] = $a[0]->href; $tmpEntry['url'] = $a[0]->href;
$tmpEntry['tags'] = $a[0]->tags; $tmpEntry['tags'] = $a[0]->tags;
$tmpEntry['is_read'] = $read; $tmpEntry['is_read'] = $read;
if ($tmpEntry['url']) { if ($tmpEntry['url']) {
$data[] = $tmpEntry; $data[] = $tmpEntry;
} }
} }
# the second <ol/ul> is for read links # the second <ol/ul> is for read links
$read = ((sizeof($data) && $read)?0:1); $read = ((sizeof($data) && $read)?0:1);
} }
} }
} }
@ -866,7 +866,7 @@ class Poche
$data[] = $record; $data[] = $record;
foreach ($record as $record2) { foreach ($record as $record2) {
if (is_array($record2)) { if (is_array($record2)) {
$data[] = $record2; $data[] = $record2;
} }
} }
} }
@ -886,7 +886,7 @@ class Poche
//increment no of records inserted //increment no of records inserted
$i++; $i++;
if ( isset($record['tags']) && trim($record['tags']) ) { if ( isset($record['tags']) && trim($record['tags']) ) {
//@TODO: set tags //@TODO: set tags
} }
} }
@ -919,17 +919,17 @@ class Poche
$purifier = new HTMLPurifier($config); $purifier = new HTMLPurifier($config);
foreach ($items as $item) { foreach ($items as $item) {
$url = new Url(base64_encode($item['url'])); $url = new Url(base64_encode($item['url']));
$content = Tools::getPageContent($url); $content = Tools::getPageContent($url);
$title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled')); $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'));
$body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined')); $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined'));
//clean content to prevent xss attack //clean content to prevent xss attack
$title = $purifier->purify($title); $title = $purifier->purify($title);
$body = $purifier->purify($body); $body = $purifier->purify($body);
$this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId()); $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId());
} }
} }
@ -944,8 +944,8 @@ class Poche
*/ */
public function export() public function export()
{ {
$filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json"; $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json";
header('Content-Disposition: attachment; filename='.$filename); header('Content-Disposition: attachment; filename='.$filename);
$entries = $this->store->retrieveAll($this->user->getId()); $entries = $this->store->retrieveAll($this->user->getId());
echo $this->tpl->render('export.twig', array( echo $this->tpl->render('export.twig', array(
@ -978,13 +978,13 @@ class Poche
public function generateToken() public function generateToken()
{ {
if (ini_get('open_basedir') === '') { if (ini_get('open_basedir') === '') {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!'; echo 'This is a server using Windows!';
// alternative to /dev/urandom for Windows // alternative to /dev/urandom for Windows
$token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
} else { } else {
$token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
} }
} }
else { else {
$token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
@ -1031,6 +1031,7 @@ class Poche
foreach ($entries as $entry) { foreach ($entries as $entry) {
$newItem = $feed->createNewItem(); $newItem = $feed->createNewItem();
$newItem->setTitle($entry['title']); $newItem->setTitle($entry['title']);
$newItem->setSource(Tools::getPocheUrl() . '?view=view&amp;id=' . $entry['id']);
$newItem->setLink($entry['url']); $newItem->setLink($entry['url']);
$newItem->setDate(time()); $newItem->setDate(time());
$newItem->setDescription($entry['content']); $newItem->setDescription($entry['content']);