miniflux-legacy/vendor/miniflux/picofeed/lib/PicoFeed/Scraper/Scraper.php

283 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2015-04-28 18:08:42 +02:00
<?php
namespace PicoFeed\Scraper;
use PicoFeed\Base;
2015-04-28 18:08:42 +02:00
use PicoFeed\Client\Client;
use PicoFeed\Client\ClientException;
use PicoFeed\Client\Url;
use PicoFeed\Encoding\Encoding;
use PicoFeed\Filter\Filter;
use PicoFeed\Logging\Logger;
use PicoFeed\Parser\XmlParser;
/**
* Scraper class.
2015-04-28 18:08:42 +02:00
*
* @author Frederic Guillot
*/
class Scraper extends Base
2015-04-28 18:08:42 +02:00
{
/**
* URL.
2015-04-28 18:08:42 +02:00
*
* @var string
*/
private $url = '';
/**
* Relevant content.
2015-04-28 18:08:42 +02:00
*
* @var string
*/
private $content = '';
/**
* HTML content.
2015-04-28 18:08:42 +02:00
*
* @var string
*/
private $html = '';
/**
* HTML content encoding.
2015-04-28 18:08:42 +02:00
*
* @var string
*/
private $encoding = '';
/**
* Flag to enable candidates parsing.
2015-04-28 18:08:42 +02:00
*
* @var bool
2015-04-28 18:08:42 +02:00
*/
private $enableCandidateParser = true;
/**
* Disable candidates parsing.
2015-04-28 18:08:42 +02:00
*
* @return Scraper
2015-04-28 18:08:42 +02:00
*/
public function disableCandidateParser()
{
$this->enableCandidateParser = false;
return $this;
}
/**
* Get encoding.
2015-04-28 18:08:42 +02:00
*
* @return string
2015-04-28 18:08:42 +02:00
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* Set encoding.
2015-04-28 18:08:42 +02:00
*
* @param string $encoding
*
* @return Scraper
2015-04-28 18:08:42 +02:00
*/
public function setEncoding($encoding)
{
$this->encoding = $encoding;
2015-04-28 18:08:42 +02:00
return $this;
}
/**
* Get URL to download.
2015-04-28 18:08:42 +02:00
*
* @return string
2015-04-28 18:08:42 +02:00
*/
public function getUrl()
{
return $this->url;
}
/**
* Set URL to download.
*
* @param string $url URL
2015-04-28 18:08:42 +02:00
*
* @return Scraper
2015-04-28 18:08:42 +02:00
*/
public function setUrl($url)
{
$this->url = $url;
2015-04-28 18:08:42 +02:00
return $this;
}
/**
* Return true if the scraper found relevant content.
2015-04-28 18:08:42 +02:00
*
* @return bool
2015-04-28 18:08:42 +02:00
*/
public function hasRelevantContent()
{
return !empty($this->content);
2015-04-28 18:08:42 +02:00
}
/**
* Get relevant content.
2015-04-28 18:08:42 +02:00
*
* @return string
*/
public function getRelevantContent()
{
return $this->content;
}
/**
* Get raw content (unfiltered).
2015-04-28 18:08:42 +02:00
*
* @return string
*/
public function getRawContent()
{
return $this->html;
}
/**
* Set raw content (unfiltered).
*
* @param string $html
2015-04-28 18:08:42 +02:00
*
* @return Scraper
*/
public function setRawContent($html)
{
$this->html = $html;
2015-04-28 18:08:42 +02:00
return $this;
}
/**
* Get filtered relevant content.
2015-04-28 18:08:42 +02:00
*
* @return string
*/
public function getFilteredContent()
{
$filter = Filter::html($this->content, $this->url);
$filter->setConfig($this->config);
2015-04-28 18:08:42 +02:00
return $filter->execute();
}
/**
* Download the HTML content.
2015-04-28 18:08:42 +02:00
*
* @return bool
2015-04-28 18:08:42 +02:00
*/
public function download()
{
if (!empty($this->url)) {
2015-04-28 18:08:42 +02:00
// Clear everything
$this->html = '';
$this->content = '';
$this->encoding = '';
try {
$client = Client::getInstance();
$client->setConfig($this->config);
$client->setTimeout($this->config->getGrabberTimeout());
$client->setUserAgent($this->config->getGrabberUserAgent());
$client->execute($this->url);
$this->url = $client->getUrl();
$this->html = $client->getContent();
$this->encoding = $client->getEncoding();
return true;
} catch (ClientException $e) {
2015-04-28 18:08:42 +02:00
Logger::setMessage(get_called_class().': '.$e->getMessage());
}
}
return false;
}
/**
* Execute the scraper.
2017-06-21 01:41:55 +02:00
*
* @param string $pageContent
* @param int $recursionDepth
2015-04-28 18:08:42 +02:00
*/
2016-12-26 23:32:18 +01:00
public function execute($pageContent = '', $recursionDepth = 0)
2015-04-28 18:08:42 +02:00
{
$this->html = '';
$this->encoding = '';
2016-12-26 23:32:18 +01:00
$this->content = '';
$this->download();
$this->prepareHtml();
2015-04-28 18:08:42 +02:00
$parser = $this->getParser();
if ($parser !== null) {
2016-12-26 23:32:18 +01:00
$maxRecursions = $this->config->getMaxRecursions();
if(!isset($maxRecursions)){
$maxRecursions = 25;
}
$pageContent .= $parser->execute();
// check if there is a link to next page and recursively get content (max 25 pages)
if((($nextLink = $parser->findNextLink()) !== null) && $recursionDepth < $maxRecursions){
$nextLink = Url::resolve($nextLink,$this->url);
$this->setUrl($nextLink);
$this->execute($pageContent,$recursionDepth+1);
}
else{
$this->content = $pageContent;
}
Logger::setMessage(get_called_class().': Content length: '.strlen($this->content).' bytes');
2015-04-28 18:08:42 +02:00
}
}
/**
* Get the parser.
2015-04-28 18:08:42 +02:00
*
* @return ParserInterface
*/
public function getParser()
{
$ruleLoader = new RuleLoader($this->config);
$rules = $ruleLoader->getRules($this->url);
if (!empty($rules['grabber'])) {
2015-04-28 18:08:42 +02:00
Logger::setMessage(get_called_class().': Parse content with rules');
foreach ($rules['grabber'] as $pattern => $rule) {
$url = new Url($this->url);
$sub_url = $url->getFullPath();
if (preg_match($pattern, $sub_url)) {
Logger::setMessage(get_called_class().': Matched url '.$sub_url);
return new RuleParser($this->html, $rule);
}
}
} elseif ($this->enableCandidateParser) {
2015-04-28 18:08:42 +02:00
Logger::setMessage(get_called_class().': Parse content with candidates');
}
return new CandidateParser($this->html);
2015-04-28 18:08:42 +02:00
}
/**
* Normalize encoding and strip head tag.
2015-04-28 18:08:42 +02:00
*/
public function prepareHtml()
{
$html_encoding = XmlParser::getEncodingFromMetaTag($this->html);
$this->html = Encoding::convert($this->html, $html_encoding ?: $this->encoding);
$this->html = Filter::stripHeadTags($this->html);
Logger::setMessage(get_called_class().': HTTP Encoding "'.$this->encoding.'" ; HTML Encoding "'.$html_encoding.'"');
}
}