miniflux-legacy/vendor/PicoFeed/Parser.php

109 lines
2.9 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoFeed;
require_once __DIR__.'/Logging.php';
2013-02-18 03:48:21 +01:00
require_once __DIR__.'/Filter.php';
2013-06-01 20:57:17 +02:00
require_once __DIR__.'/Encoding.php';
require_once __DIR__.'/Grabber.php';
2013-02-18 03:48:21 +01:00
abstract class Parser
{
protected $content = '';
public $id = '';
public $url = '';
public $title = '';
public $updated = '';
public $items = array();
public $grabber = false;
public $grabber_ignore_urls = array();
2013-09-01 00:37:26 +02:00
public $grabber_timeout = null;
public $grabber_user_agent = null;
2013-02-18 03:48:21 +01:00
abstract public function execute();
2013-10-04 05:14:39 +02:00
public function __construct($content, $http_encoding = '')
2013-02-18 03:48:21 +01:00
{
2013-10-04 05:14:39 +02:00
$xml_encoding = Filter::getEncodingFromXmlTag($content);
Logging::log(\get_called_class().': HTTP Encoding "'.$http_encoding.'" ; XML Encoding "'.$xml_encoding.'"');
// Strip XML tag to avoid multiple encoding/decoding in the next XML processing
$this->content = Filter::stripXmlTag($content);
2013-06-01 20:57:17 +02:00
// Encode everything in UTF-8
2013-10-04 05:14:39 +02:00
if ($xml_encoding == 'windows-1251' || $http_encoding == 'windows-1251') {
$this->content = Encoding::cp1251ToUtf8($this->content);
}
else {
$this->content = Encoding::toUTF8($this->content);
}
2013-06-01 20:57:17 +02:00
// Workarounds
$this->content = $this->normalizeData($this->content);
2013-02-18 03:48:21 +01:00
}
public function filterHtml($item_content, $item_url)
2013-02-18 03:48:21 +01:00
{
$content = '';
if ($this->grabber && ! in_array($item_url, $this->grabber_ignore_urls)) {
$grabber = new Grabber($item_url);
$grabber->download($this->grabber_timeout, $this->grabber_user_agent);
2013-09-01 00:37:26 +02:00
if ($grabber->parse()) $item_content = $grabber->content;
}
2013-02-18 03:48:21 +01:00
if ($item_content) {
$filter = new Filter($item_content, $item_url);
2013-02-18 03:48:21 +01:00
$content = $filter->execute();
}
return $content;
}
public function getXmlErrors()
2013-02-18 03:48:21 +01:00
{
$errors = array();
2013-04-05 05:34:07 +02:00
foreach(\libxml_get_errors() as $error) {
2013-02-18 03:48:21 +01:00
$errors[] = sprintf('XML error: %s (Line: %d - Column: %d - Code: %d)',
2013-04-05 05:34:07 +02:00
$error->message,
$error->line,
$error->column,
$error->code
);
2013-02-18 03:48:21 +01:00
}
return implode(', ', $errors);
2013-02-18 03:48:21 +01:00
}
2013-04-05 05:34:07 +02:00
// Dirty quickfix before XML parsing
public function normalizeData($data)
2013-02-18 03:48:21 +01:00
{
2013-04-05 05:34:07 +02:00
return str_replace("\xc3\x20", '', $data);
2013-02-18 03:48:21 +01:00
}
2013-06-01 20:57:17 +02:00
// Trim whitespace from the begining, the end and inside a string and don't break utf-8 string
public function stripWhiteSpace($value)
{
$value = str_replace("\r", "", $value);
$value = str_replace("\t", "", $value);
$value = str_replace("\n", "", $value);
return trim($value);
}
public function generateId()
{
// crc32b seems to be faster and shorter than other hash algorithms
return hash('crc32b', implode(func_get_args()));
}
2013-02-18 03:48:21 +01:00
}