mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-16 11:50:55 +00:00
[FilterMore] improve style
This commit is contained in:
parent
992e4d239c
commit
51c6b6cd47
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
class FilterMoreBridge extends FeedExpander {
|
||||
|
||||
class FilterMoreBridge extends FeedExpander
|
||||
{
|
||||
const MAINTAINER = 'boyska';
|
||||
const NAME = 'FilterMore';
|
||||
const CACHE_TIMEOUT = 2;
|
||||
@ -89,22 +89,25 @@ class FilterMoreBridge extends FeedExpander {
|
||||
|
||||
]];
|
||||
|
||||
protected function parseItem($newItem){
|
||||
protected function parseItem($newItem)
|
||||
{
|
||||
$item = parent::parseItem($newItem);
|
||||
$item['enclosures'] = [];
|
||||
if(isset($newItem->enclosure)) {
|
||||
foreach($newItem->enclosure as $encl) {
|
||||
if (isset($newItem->enclosure)) {
|
||||
foreach ($newItem->enclosure as $encl) {
|
||||
$serialized = [];
|
||||
foreach($encl->attributes() as $key => $value) {
|
||||
foreach ($encl->attributes() as $key => $value) {
|
||||
$serialized[$key] = (string)$value;
|
||||
}
|
||||
$serialized["length"] = intval($serialized["length"]);
|
||||
$serialized['length'] = intval($serialized['length']);
|
||||
$item['enclosures'][] = $serialized;
|
||||
}
|
||||
}
|
||||
if(isset($newItem->link)) {
|
||||
foreach($newItem->link as $el) {
|
||||
if(((string)$el['rel']) !== 'enclosure') continue;
|
||||
if (isset($newItem->link)) {
|
||||
foreach ($newItem->link as $el) {
|
||||
if (((string)$el['rel']) !== 'enclosure') {
|
||||
continue;
|
||||
}
|
||||
$serialized = [];
|
||||
$serialized['url'] = (string)$el['href'];
|
||||
|
||||
@ -115,40 +118,45 @@ class FilterMoreBridge extends FeedExpander {
|
||||
$filters = ['filterByTitle', 'filterByBody', 'filterByAuthor', 'filterByDateNewer', 'filterByDateOlder', 'filterByMedia'];
|
||||
$results = [];
|
||||
|
||||
foreach($filters as $filter) {
|
||||
foreach ($filters as $filter) {
|
||||
$filter_res = $this->$filter($item);
|
||||
if($filter_res === null) continue;
|
||||
if ($filter_res === null) {
|
||||
continue;
|
||||
}
|
||||
$results[] = $filter_res;
|
||||
}
|
||||
|
||||
$old_enclosures = $item['enclosures'];
|
||||
$item['enclosures'] = [];
|
||||
foreach($old_enclosures as $e) {
|
||||
foreach ($old_enclosures as $e) {
|
||||
$item['enclosures'][] = $e['url'];
|
||||
}
|
||||
if(count($results) === 0) {
|
||||
if (count($results) === 0) {
|
||||
return $item;
|
||||
}
|
||||
if($this->getConjType() === 'and') {
|
||||
if ($this->getConjType() === 'and') {
|
||||
$result = !in_array(false, $results);
|
||||
} else { // or
|
||||
$result = in_array(true, $results);
|
||||
}
|
||||
if($this->getInvertResult()) {
|
||||
if ($this->getInvertResult()) {
|
||||
$result = !$result;
|
||||
}
|
||||
if($result)
|
||||
if ($result) {
|
||||
return $item;
|
||||
else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function sortItemKey($item) {
|
||||
protected function sortItemKey($item)
|
||||
{
|
||||
$sort_by = $this->getInput('sort_by');
|
||||
$key = $item[$sort_by];
|
||||
return $key;
|
||||
}
|
||||
public function collectExpandableDatas($url, $maxItems = -1){
|
||||
public function collectExpandableDatas($url, $maxItems = -1)
|
||||
{
|
||||
parent::collectExpandableDatas($url, $maxItems);
|
||||
if($this->getInput('sort_by') === 'random') {
|
||||
shuffle($this->items);
|
||||
@ -164,31 +172,37 @@ class FilterMoreBridge extends FeedExpander {
|
||||
$this->items = array_reverse($this->items);
|
||||
}
|
||||
|
||||
private function cmp($a, $b) {
|
||||
private function cmp($a, $b)
|
||||
{
|
||||
if($a > $b) return 1;
|
||||
if($a < $b) return -1;
|
||||
return 0;
|
||||
}
|
||||
private function filterByFieldRegexp($field, $re){
|
||||
private function filterByFieldRegexp($field, $re)
|
||||
{
|
||||
if($re === "") return null;
|
||||
if(preg_match($re, $field)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected function filterByTitle($item){
|
||||
protected function filterByTitle($item)
|
||||
{
|
||||
$re = $this->getInput('title_re');
|
||||
return $this->filterByFieldRegexp($item['title'], $re);
|
||||
}
|
||||
protected function filterByBody($item){
|
||||
protected function filterByBody($item)
|
||||
{
|
||||
$re = $this->getInput('body_re');
|
||||
return $this->filterByFieldRegexp($item['content'], $re);
|
||||
}
|
||||
protected function filterByAuthor($item){
|
||||
protected function filterByAuthor($item)
|
||||
{
|
||||
$re = $this->getInput('author_re');
|
||||
return $this->filterByFieldRegexp($item['author'], $re);
|
||||
}
|
||||
private function filterByDate($item, $input, $expected){
|
||||
private function filterByDate($item, $input, $expected)
|
||||
{
|
||||
$val = $this->getInput($input);
|
||||
if($val === "") return null;
|
||||
$ts = strtotime($val);
|
||||
@ -198,40 +212,48 @@ class FilterMoreBridge extends FeedExpander {
|
||||
$cmp = $this->cmp($item['timestamp'], $ts); // 1 if newer, -1 if older
|
||||
return $cmp === $expected;
|
||||
}
|
||||
protected function filterByDateNewer($item){
|
||||
protected function filterByDateNewer($item)
|
||||
{
|
||||
return $this->filterByDate($item, 'newer_than', 1);
|
||||
}
|
||||
protected function filterByDateOlder($item){
|
||||
protected function filterByDateOlder($item)
|
||||
{
|
||||
return $this->filterByDate($item, 'older_than', -1);
|
||||
}
|
||||
protected function filterByMedia($item) {
|
||||
protected function filterByMedia($item)
|
||||
{
|
||||
if(!$this->getInput('has_media')) return null;
|
||||
if(count($item['enclosures']) > 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getConjType(){
|
||||
protected function getConjType()
|
||||
{
|
||||
return $this->getInput('conj_type');
|
||||
}
|
||||
protected function getInvertResult(){
|
||||
protected function getInvertResult()
|
||||
{
|
||||
return $this->getInput('invert_filter');
|
||||
}
|
||||
|
||||
public function getURI(){
|
||||
public function getURI()
|
||||
{
|
||||
$url = $this->getInput('url');
|
||||
|
||||
if(empty($url)) {
|
||||
if (empty($url)) {
|
||||
$url = parent::getURI();
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function collectData(){
|
||||
if($this->getInput('url') && substr($this->getInput('url'), 0, strlen('http')) !== 'http') {
|
||||
public function collectData()
|
||||
{
|
||||
if ($this->getInput('url') && substr($this->getInput('url'), 0, strlen('http')) !== 'http')
|
||||
{
|
||||
// just in case someone find a way to access local files by playing with the url
|
||||
returnClientError('The url parameter must either refer to http or https protocol.');
|
||||
}
|
||||
try{
|
||||
try {
|
||||
$this->collectExpandableDatas($this->getURI());
|
||||
} catch (HttpException $e) {
|
||||
$this->collectExpandableDatas($this->getURI());
|
||||
|
Loading…
Reference in New Issue
Block a user