2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoFeed;
|
|
|
|
|
2013-07-06 20:29:45 +02:00
|
|
|
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';
|
2013-02-18 03:48:21 +01:00
|
|
|
|
|
|
|
abstract class Parser
|
|
|
|
{
|
|
|
|
protected $content = '';
|
|
|
|
|
|
|
|
public $id = '';
|
|
|
|
public $url = '';
|
|
|
|
public $title = '';
|
|
|
|
public $updated = '';
|
|
|
|
public $items = array();
|
|
|
|
|
|
|
|
|
|
|
|
abstract public function execute();
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($content)
|
|
|
|
{
|
2013-06-01 20:57:17 +02:00
|
|
|
// Strip XML tag to avoid multiple encoding/decoding in next XML processing
|
|
|
|
$this->content = $this->stripXmlTag($content);
|
|
|
|
|
|
|
|
// Encode everything in UTF-8
|
|
|
|
$this->content = Encoding::toUTF8($this->content);
|
|
|
|
|
|
|
|
// Workarounds
|
|
|
|
$this->content = $this->normalizeData($this->content);
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function filterHtml($str, $item_url)
|
|
|
|
{
|
|
|
|
$content = '';
|
|
|
|
|
|
|
|
if ($str) {
|
|
|
|
|
|
|
|
$filter = new Filter($str, $item_url);
|
|
|
|
$content = $filter->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-06 20:29:45 +02:00
|
|
|
public function getXmlErrors()
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
2013-07-06 20:29:45 +02: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
|
|
|
|
2013-07-06 20:29:45 +02: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
|
|
|
}
|
2013-07-06 20:29:45 +02: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
|
|
|
|
|
|
|
|
|
|
|
public function stripXmlTag($data)
|
|
|
|
{
|
|
|
|
if (strpos($data, '<?xml') !== false) {
|
|
|
|
|
|
|
|
$data = substr($data, strrpos($data, '?>') + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2013-06-29 19:41:36 +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);
|
|
|
|
}
|
2013-07-27 03:00:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|