Update of picoFeed

This commit is contained in:
Frederic Guillot 2013-06-28 21:50:15 -04:00
parent 271241fcf7
commit 6f2702b256
9 changed files with 187 additions and 24 deletions

View File

@ -257,8 +257,25 @@ class Filter
public function getAbsoluteUrl($path, $url)
{
//if (! filter_var($url, FILTER_VALIDATE_URL)) return '';
$components = parse_url($url);
if (! isset($components['scheme'])) $components['scheme'] = 'http';
if (! isset($components['host'])) {
if ($url) {
$components['host'] = $url;
$components['path'] = '/';
}
else {
return '';
}
}
if ($path{0} === '/') {
// Absolute path
@ -267,9 +284,10 @@ class Filter
else {
// Relative path
$url_path = $components['path'];
$url_path = isset($components['path']) && ! empty($components['path']) ? $components['path'] : '/';
$length = strlen($url_path);
if ($url_path{strlen($url_path) - 1} !== '/') {
if ($length > 1 && $url_path{$length - 1} !== '/') {
$url_path = dirname($url_path).'/';
}

View File

@ -1,8 +1,8 @@
<?php
namespace PicoFeed;
namespace PicoFeed\Parsers;
class Atom extends Parser
class Atom extends \PicoFeed\Parser
{
public function execute()
{

View File

@ -1,8 +1,8 @@
<?php
namespace PicoFeed;
namespace PicoFeed\Parsers;
class Rss10 extends Parser
class Rss10 extends \PicoFeed\Parser
{
public function execute()
{

View File

@ -1,8 +1,8 @@
<?php
namespace PicoFeed;
namespace PicoFeed\Parsers;
class Rss20 extends Parser
class Rss20 extends \PicoFeed\Parser
{
public function execute()
{

View File

@ -1,6 +1,6 @@
<?php
namespace PicoFeed;
namespace PicoFeed\Parsers;
require_once __DIR__.'/Rss20.php';

View File

@ -1,6 +1,6 @@
<?php
namespace PicoFeed;
namespace PicoFeed\Parsers;
require_once __DIR__.'/Rss20.php';

View File

@ -84,30 +84,30 @@ class Reader
if (strpos($first_tag, '<feed') !== false) {
require_once __DIR__.'/Parsers/Atom.php';
return new Atom($this->content);
return new Parsers\Atom($this->content);
}
else if (strpos($first_tag, '<rss') !== false &&
(strpos($first_tag, 'version="2.0"') !== false || strpos($first_tag, 'version=\'2.0\'') !== false)) {
require_once __DIR__.'/Parsers/Rss20.php';
return new Rss20($this->content);
return new Parsers\Rss20($this->content);
}
else if (strpos($first_tag, '<rss') !== false &&
(strpos($first_tag, 'version="0.92"') !== false || strpos($first_tag, 'version=\'0.92\'') !== false)) {
require_once __DIR__.'/Parsers/Rss92.php';
return new Rss92($this->content);
return new Parsers\Rss92($this->content);
}
else if (strpos($first_tag, '<rss') !== false &&
(strpos($first_tag, 'version="0.91"') !== false || strpos($first_tag, 'version=\'0.91\'') !== false)) {
require_once __DIR__.'/Parsers/Rss91.php';
return new Rss91($this->content);
return new Parsers\Rss91($this->content);
}
else if (strpos($first_tag, '<rdf:') !== false && strpos($first_tag, 'xmlns="http://purl.org/rss/1.0/"') !== false) {
require_once __DIR__.'/Parsers/Rss10.php';
return new Rss10($this->content);
return new Parsers\Rss10($this->content);
}
else if ($discover === true) {
@ -149,18 +149,21 @@ class Reader
$link = $nodes->item(0)->getAttribute('href');
// Relative links
if (strpos($link, 'http') !== 0) {
if (! empty($link)) {
if ($link{0} === '/') $link = substr($link, 1);
if ($this->url{strlen($this->url) - 1} !== '/') $this->url .= '/';
// Relative links
if (strpos($link, 'http') !== 0) {
$link = $this->url.$link;
if ($link{0} === '/') $link = substr($link, 1);
if ($this->url{strlen($this->url) - 1} !== '/') $this->url .= '/';
$link = $this->url.$link;
}
$this->download($link);
return true;
}
$this->download($link);
return true;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace PicoFeed;
abstract class Writer
{
public $items = array();
abstract public function execute($filename = '');
public function checkRequiredProperties()
{
foreach ($this->required_properties as $property) {
if (! isset($this->$property)) {
throw new \RuntimeException('Required property missing: '.$property);
}
}
}
}

121
vendor/PicoFeed/Writers/Atom.php vendored Normal file
View File

@ -0,0 +1,121 @@
<?php
namespace PicoFeed\Writers;
require_once __DIR__.'/../Writer.php';
class Atom extends \PicoFeed\Writer
{
protected $required_properties = array(
'title',
'site_url',
'feed_url'
);
public function execute($filename = '')
{
$this->checkRequiredProperties();
$dom = new \DomDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
// <feed/>
$feed = $dom->createElement('feed');
$feed->setAttributeNodeNS(new \DomAttr('xmlns', 'http://www.w3.org/2005/Atom'));
// <generator/>
$generator = $dom->createElement('generator', 'PicoFeed');
$generator->setAttribute('url', 'https://github.com/fguillot/picoFeed');
$feed->appendChild($generator);
// <title/>
$feed->appendChild($dom->createElement('title', $this->title));
// <updated/>
$feed->appendChild($dom->createElement('updated', date(DATE_ATOM, isset($this->updated) ? $this->updated : time())));
// <link rel="alternate" type="text/html" href="http://example.org/"/>
$link = $dom->createElement('link');
$link->setAttribute('rel', 'alternate');
$link->setAttribute('type', 'text/html');
$link->setAttribute('href', $this->site_url);
$feed->appendChild($link);
// <link rel="self" type="application/atom+xml" href="http://example.org/feed.atom"/>
$link = $dom->createElement('link');
$link->setAttribute('rel', 'self');
$link->setAttribute('type', 'application/atom+xml');
$link->setAttribute('href', $this->feed_url);
$feed->appendChild($link);
// <author/>
if (isset($this->author)) {
$name = $dom->createElement('name', $this->author);
$author = $dom->createElement('author');
$author->appendChild($name);
$feed->appendChild($author);
}
// <entry/>
foreach ($this->items as $item) {
$entry = $dom->createElement('entry');
// <title/>
$entry->appendChild($dom->createElement('title', $item['title']));
// <updated/>
$entry->appendChild($dom->createElement('updated', date(DATE_ATOM, isset($item['updated']) ? $item['updated'] : time())));
// <published/>
if (isset($item['published'])) {
$entry->appendChild($dom->createElement('published', date(DATE_ATOM, $item['published'])));
}
// <link rel="alternate" type="text/html" href="http://example.org/"/>
$link = $dom->createElement('link');
$link->setAttribute('rel', 'alternate');
$link->setAttribute('type', 'text/html');
$link->setAttribute('href', $item['url']);
$entry->appendChild($link);
// <summary/>
if (isset($item['summary'])) {
$entry->appendChild($dom->createElement('summary', $item['summary']));
}
// <content/>
if (isset($item['content'])) {
$content = $dom->createElement('content');
$content->setAttribute('type', 'html');
$content->appendChild($dom->createCDATASection($item['content']));
$entry->appendChild($content);
}
// <author/>
if (isset($item['author'])) {
$name = $dom->createElement('name', $item['author']);
$author = $dom->createElement('author');
$author->appendChild($name);
$entry->appendChild($author);
}
$feed->appendChild($entry);
}
$dom->appendChild($feed);
if ($filename) {
$dom->save($filename);
}
else {
return $dom->saveXML();
}
}
}