From 46f5d0fa233f49013b72e9eee19dd9339ee51249 Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 15 Oct 2021 23:34:51 +0200 Subject: [PATCH 01/12] [FilterMore] NEW bridge for advanced filtering --- bridges/FilterMoreBridge.php | 157 +++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 bridges/FilterMoreBridge.php diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php new file mode 100644 index 00000000..aa11251e --- /dev/null +++ b/bridges/FilterMoreBridge.php @@ -0,0 +1,157 @@ + array( + 'name' => 'Feed URL', + 'required' => true, + ), + 'conj_type' => array( + 'name' => 'Conjunction type type', + 'type' => 'list', + 'required' => false, + 'values' => array( + 'All conditions must be met' => 'and', + 'Any condition must be met' => 'or', + ), + 'defaultValue' => 'permit', + ), + + 'title_re' => array( + 'name' => 'Filter item title (regular expression, see php.net/pcre_match for details)', + 'required' => false, + 'exampleValue' => '/breaking\ news/i', + ), + 'body_re' => array( + 'name' => 'Filter body (regular expression)', + 'required' => false, + ), + 'author_re' => array( + 'name' => 'Filter author (regular expression)', + 'required' => false, + 'exampleValue' => '/(technology|politics)/i', + ), + 'newer_than' => array( + 'name' => 'Filter date: ok if newer than the value (see php.net/strtotime for details)', + 'required' => false, + 'exampleValue' => '-14 days', + ), + 'older_than' => array( + 'name' => 'Filter date: ok if older than the value (see php.net/strtotime for details)', + 'required' => false, + 'exampleValue' => '-1 hour', + ), + + 'invert_filter' => array( + 'name' => 'Invert filter result', + 'type' => 'checkbox', + 'required' => false, + 'defaultValue' => false, + ), + )); + + protected function parseItem($newItem){ + $item = parent::parseItem($newItem); + + $filters = ['filterByTitle', 'filterByBody', 'filterByAuthor', 'filterByDateNewer', 'filterByDateOlder']; + $results = []; + + foreach($filters as $filter) { + $filter_res = $this->$filter($item); + if($filter_res === null) continue; + $results[] = $filter_res; + } + if(count($results) === 0) { + return $item; + } + if($this->getConjType() === 'and') { + $result = !in_array(false, $results); + } else { // or + $result = in_array(true, $results); + } + if($this->getInvertResult()) { + $result = !$result; + } + if($result) + return $item; + else + return null; + } + + private function cmp($a, $b) { + if($a > $b) return 1; + if($a < $b) return -1; + return 0; + } + private function filterByFieldRegexp($field, $re){ + if($re === "") return null; + if(preg_match($re, $field)) { + return true; + } + return false; + } + protected function filterByTitle($item){ + $re = $this->getInput('title_re'); + return $this->filterByFieldRegexp($item['title'], $re); + } + protected function filterByBody($item){ + $re = $this->getInput('body_re'); + return $this->filterByFieldRegexp($item['content'], $re); + } + protected function filterByAuthor($item){ + $re = $this->getInput('author_re'); + return $this->filterByFieldRegexp($item['author'], $re); + } + private function filterByDate($item, $input, $expected){ + $val = $this->getInput($input); + if($val === "") return null; + $ts = strtotime($val); + if($ts === false) { + throw new Exception("Invalid time specification: " . $val); + } + $cmp = $this->cmp($item['timestamp'], $ts); // 1 if newer, -1 if older + return $cmp === $expected; + } + protected function filterByDateNewer($item){ + return $this->filterByDate($item, 'newer_than', 1); + } + protected function filterByDateOlder($item){ + return $this->filterByDate($item, 'older_than', -1); + } + + protected function getConjType(){ + return $this->getInput('conj_type'); + } + protected function getInvertResult(){ + return $this->getInput('invert_filter'); + } + + public function getURI(){ + $url = $this->getInput('url'); + + if(empty($url)) { + $url = parent::getURI(); + } + return $url; + } + + 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{ + $this->collectExpandableDatas($this->getURI()); + } catch (HttpException $e) { + $this->collectExpandableDatas($this->getURI()); + } + } +} + From 90f72155a8ac299f87901cf0cf27997f3e09ad01 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Oct 2021 00:11:46 +0200 Subject: [PATCH 02/12] [FilterMore] include enclosures in output --- bridges/FilterMoreBridge.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index aa11251e..c849fe4c 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -59,6 +59,26 @@ class FilterMoreBridge extends FeedExpander { protected function parseItem($newItem){ $item = parent::parseItem($newItem); + $item['enclosures'] = []; + if(isset($newItem->enclosure)) { + foreach($newItem->enclosure as $encl) { + $serialized = []; + foreach($encl->attributes() as $key => $value) { + $serialized[$key] = (string)$value; + } + $serialized["length"] = intval($serialized["length"]); + $item['enclosures'][] = $serialized; + } + } + if(isset($newItem->link)) { + foreach($newItem->link as $el) { + if(((string)$el['rel']) !== 'enclosure') continue; + $serialized = []; + $serialized['url'] = (string)$el['href']; + + $item['enclosures'][] = $serialized; + } + } $filters = ['filterByTitle', 'filterByBody', 'filterByAuthor', 'filterByDateNewer', 'filterByDateOlder']; $results = []; @@ -68,6 +88,12 @@ class FilterMoreBridge extends FeedExpander { if($filter_res === null) continue; $results[] = $filter_res; } + + $old_enclosures = $item['enclosures']; + $item['enclosures'] = []; + foreach($old_enclosures as $e) { + $item['enclosures'][] = $e['url']; + } if(count($results) === 0) { return $item; } From 6d77dda58100e6d08c72229be0627ce5aee2e120 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Oct 2021 00:12:11 +0200 Subject: [PATCH 03/12] [FilterMore] has_media --- bridges/FilterMoreBridge.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index c849fe4c..19e0b9e4 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -49,6 +49,13 @@ class FilterMoreBridge extends FeedExpander { 'exampleValue' => '-1 hour', ), + 'has_media' => array( + 'name' => 'Has at least 1 media inside', + 'type' => 'checkbox', + 'required' => false, + 'defaultValue' => false, + ), + 'invert_filter' => array( 'name' => 'Invert filter result', 'type' => 'checkbox', @@ -80,7 +87,7 @@ class FilterMoreBridge extends FeedExpander { } } - $filters = ['filterByTitle', 'filterByBody', 'filterByAuthor', 'filterByDateNewer', 'filterByDateOlder']; + $filters = ['filterByTitle', 'filterByBody', 'filterByAuthor', 'filterByDateNewer', 'filterByDateOlder', 'filterByMedia']; $results = []; foreach($filters as $filter) { @@ -151,6 +158,11 @@ class FilterMoreBridge extends FeedExpander { protected function filterByDateOlder($item){ return $this->filterByDate($item, 'older_than', -1); } + protected function filterByMedia($item) { + if(!$this->getInput('has_media')) return null; + if(count($item['enclosures']) > 0) return true; + return false; + } protected function getConjType(){ return $this->getInput('conj_type'); From 992e4d239c5f2163dc5c626bfeb9cd7731014031 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Oct 2021 00:42:45 +0200 Subject: [PATCH 04/12] [FilterMore] sort --- bridges/FilterMoreBridge.php | 50 ++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index 19e0b9e4..e00ca814 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -8,7 +8,8 @@ class FilterMoreBridge extends FeedExpander { const DESCRIPTION = 'Filters a feed of your choice'; const URI = 'https://git.lattuta.net/boyska/rss-bridge'; - const PARAMETERS = array(array( + const PARAMETERS = [ + [ 'url' => array( 'name' => 'Feed URL', 'required' => true, @@ -62,7 +63,31 @@ class FilterMoreBridge extends FeedExpander { 'required' => false, 'defaultValue' => false, ), - )); + + 'sort_by' => [ + 'name' => 'Sort by', + 'type' => 'list', + 'required' => true, + 'values' => [ + "Don't sort" => 'none', + 'Date' => 'timestamp', + 'Title' => 'title', + 'Random' => 'random', + ], + 'defaultValue' => 'date', + ], + 'sort_dir' => [ + 'name' => 'Sort direction', + 'type' => 'list', + 'required' => true, + 'values' => [ + 'Ascending' => 'asc', + 'Descending' => 'desc', + ], + 'defaultValue' => 'asc', + ], + + ]]; protected function parseItem($newItem){ $item = parent::parseItem($newItem); @@ -118,6 +143,27 @@ class FilterMoreBridge extends FeedExpander { return null; } + protected function sortItemKey($item) { + $sort_by = $this->getInput('sort_by'); + $key = $item[$sort_by]; + return $key; + } + public function collectExpandableDatas($url, $maxItems = -1){ + parent::collectExpandableDatas($url, $maxItems); + if($this->getInput('sort_by') === 'random') { + shuffle($this->items); + } elseif($this->getInput('sort_by') !== 'none') { + usort($this->items, function($itemA, $itemB) { + $valA = $this->sortItemKey($itemA); + $valB = $this->sortItemKey($itemB); + $cmp = strcmp($valA, $valB); + return $cmp; + }); + } + if($this->getInput('sort_dir') === 'desc') + $this->items = array_reverse($this->items); + } + private function cmp($a, $b) { if($a > $b) return 1; if($a < $b) return -1; From 51c6b6cd4786514c034d472ef6cdfd26e588cbf8 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Mar 2024 21:37:52 +0100 Subject: [PATCH 05/12] [FilterMore] improve style --- bridges/FilterMoreBridge.php | 258 +++++++++++++++++++---------------- 1 file changed, 140 insertions(+), 118 deletions(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index e00ca814..41828e22 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -1,68 +1,68 @@ array( - 'name' => 'Feed URL', - 'required' => true, - ), - 'conj_type' => array( - 'name' => 'Conjunction type type', - 'type' => 'list', - 'required' => false, - 'values' => array( - 'All conditions must be met' => 'and', - 'Any condition must be met' => 'or', - ), - 'defaultValue' => 'permit', - ), + 'url' => array( + 'name' => 'Feed URL', + 'required' => true, + ), + 'conj_type' => array( + 'name' => 'Conjunction type type', + 'type' => 'list', + 'required' => false, + 'values' => array( + 'All conditions must be met' => 'and', + 'Any condition must be met' => 'or', + ), + 'defaultValue' => 'permit', + ), - 'title_re' => array( - 'name' => 'Filter item title (regular expression, see php.net/pcre_match for details)', - 'required' => false, + 'title_re' => array( + 'name' => 'Filter item title (regular expression, see php.net/pcre_match for details)', + 'required' => false, 'exampleValue' => '/breaking\ news/i', - ), - 'body_re' => array( - 'name' => 'Filter body (regular expression)', - 'required' => false, - ), - 'author_re' => array( - 'name' => 'Filter author (regular expression)', - 'required' => false, + ), + 'body_re' => array( + 'name' => 'Filter body (regular expression)', + 'required' => false, + ), + 'author_re' => array( + 'name' => 'Filter author (regular expression)', + 'required' => false, 'exampleValue' => '/(technology|politics)/i', - ), - 'newer_than' => array( - 'name' => 'Filter date: ok if newer than the value (see php.net/strtotime for details)', - 'required' => false, + ), + 'newer_than' => array( + 'name' => 'Filter date: ok if newer than the value (see php.net/strtotime for details)', + 'required' => false, 'exampleValue' => '-14 days', - ), - 'older_than' => array( - 'name' => 'Filter date: ok if older than the value (see php.net/strtotime for details)', - 'required' => false, + ), + 'older_than' => array( + 'name' => 'Filter date: ok if older than the value (see php.net/strtotime for details)', + 'required' => false, 'exampleValue' => '-1 hour', - ), + ), - 'has_media' => array( - 'name' => 'Has at least 1 media inside', - 'type' => 'checkbox', - 'required' => false, - 'defaultValue' => false, - ), + 'has_media' => array( + 'name' => 'Has at least 1 media inside', + 'type' => 'checkbox', + 'required' => false, + 'defaultValue' => false, + ), - 'invert_filter' => array( - 'name' => 'Invert filter result', - 'type' => 'checkbox', - 'required' => false, - 'defaultValue' => false, - ), + 'invert_filter' => array( + 'name' => 'Invert filter result', + 'type' => 'checkbox', + 'required' => false, + 'defaultValue' => false, + ), 'sort_by' => [ 'name' => 'Sort by', @@ -87,24 +87,27 @@ class FilterMoreBridge extends FeedExpander { 'defaultValue' => 'asc', ], - ]]; + ]]; - protected function parseItem($newItem){ - $item = parent::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,32 +172,38 @@ 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){ - $re = $this->getInput('title_re'); + } + protected function filterByTitle($item) + { + $re = $this->getInput('title_re'); return $this->filterByFieldRegexp($item['title'], $re); - } - protected function filterByBody($item){ - $re = $this->getInput('body_re'); + } + protected function filterByBody($item) + { + $re = $this->getInput('body_re'); return $this->filterByFieldRegexp($item['content'], $re); - } - protected function filterByAuthor($item){ - $re = $this->getInput('author_re'); + } + protected function filterByAuthor($item) + { + $re = $this->getInput('author_re'); return $this->filterByFieldRegexp($item['author'], $re); - } - private function filterByDate($item, $input, $expected){ - $val = $this->getInput($input); + } + private function filterByDate($item, $input, $expected) + { + $val = $this->getInput($input); if($val === "") return null; $ts = strtotime($val); if($ts === false) { @@ -197,45 +211,53 @@ 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(){ - return $this->getInput('conj_type'); - } - protected function getInvertResult(){ - return $this->getInput('invert_filter'); - } + protected function getConjType() + { + return $this->getInput('conj_type'); + } + protected function getInvertResult() + { + return $this->getInput('invert_filter'); + } - public function getURI(){ - $url = $this->getInput('url'); + public function getURI() + { + $url = $this->getInput('url'); - if(empty($url)) { - $url = parent::getURI(); - } - return $url; - } + if (empty($url)) { + $url = parent::getURI(); + } + return $url; + } - 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{ - $this->collectExpandableDatas($this->getURI()); - } catch (HttpException $e) { - $this->collectExpandableDatas($this->getURI()); - } - } + 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 { + $this->collectExpandableDatas($this->getURI()); + } catch (HttpException $e) { + $this->collectExpandableDatas($this->getURI()); + } + } } From f509ff0f284b0e3f45aa55a54050bb68ea4cdbcf Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Mar 2024 21:42:01 +0100 Subject: [PATCH 06/12] [FilterMore] more style fixed --- bridges/FilterMoreBridge.php | 85 ++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index 41828e22..11e67369 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -10,59 +10,59 @@ class FilterMoreBridge extends FeedExpander const PARAMETERS = [ [ - 'url' => array( + 'url' => [ 'name' => 'Feed URL', 'required' => true, - ), - 'conj_type' => array( + ], + 'conj_type' => [ 'name' => 'Conjunction type type', 'type' => 'list', 'required' => false, - 'values' => array( + 'values' => [ 'All conditions must be met' => 'and', 'Any condition must be met' => 'or', - ), + ], 'defaultValue' => 'permit', - ), + ], - 'title_re' => array( + 'title_re' => [ 'name' => 'Filter item title (regular expression, see php.net/pcre_match for details)', 'required' => false, 'exampleValue' => '/breaking\ news/i', - ), - 'body_re' => array( + ], + 'body_re' => [ 'name' => 'Filter body (regular expression)', 'required' => false, - ), - 'author_re' => array( + ], + 'author_re' => [ 'name' => 'Filter author (regular expression)', 'required' => false, 'exampleValue' => '/(technology|politics)/i', - ), - 'newer_than' => array( + ], + 'newer_than' => [ 'name' => 'Filter date: ok if newer than the value (see php.net/strtotime for details)', 'required' => false, 'exampleValue' => '-14 days', - ), - 'older_than' => array( + ], + 'older_than' => [ 'name' => 'Filter date: ok if older than the value (see php.net/strtotime for details)', 'required' => false, 'exampleValue' => '-1 hour', - ), + ], - 'has_media' => array( + 'has_media' => [ 'name' => 'Has at least 1 media inside', 'type' => 'checkbox', 'required' => false, 'defaultValue' => false, - ), + ], - 'invert_filter' => array( + 'invert_filter' => [ 'name' => 'Invert filter result', 'type' => 'checkbox', 'required' => false, 'defaultValue' => false, - ), + ], 'sort_by' => [ 'name' => 'Sort by', @@ -87,7 +87,7 @@ class FilterMoreBridge extends FeedExpander 'defaultValue' => 'asc', ], - ]]; + ]]; protected function parseItem($newItem) { @@ -158,30 +158,35 @@ class FilterMoreBridge extends FeedExpander public function collectExpandableDatas($url, $maxItems = -1) { parent::collectExpandableDatas($url, $maxItems); - if($this->getInput('sort_by') === 'random') { + if ($this->getInput('sort_by') === 'random') { shuffle($this->items); - } elseif($this->getInput('sort_by') !== 'none') { - usort($this->items, function($itemA, $itemB) { + } elseif ($this->getInput('sort_by') !== 'none') { + usort($this->items, function ($itemA, $itemB) { $valA = $this->sortItemKey($itemA); $valB = $this->sortItemKey($itemB); $cmp = strcmp($valA, $valB); return $cmp; }); } - if($this->getInput('sort_dir') === 'desc') + if ($this->getInput('sort_dir') === 'desc') { $this->items = array_reverse($this->items); + } } private function cmp($a, $b) { - if($a > $b) return 1; - if($a < $b) return -1; + if ($a > $b) { + return 1; + } elseif ($a < $b) { + return -1; + } return 0; } private function filterByFieldRegexp($field, $re) { - if($re === "") return null; - if(preg_match($re, $field)) { + if ($re === '') { + return null; + } elseif (preg_match($re, $field)) { return true; } return false; @@ -204,12 +209,14 @@ class FilterMoreBridge extends FeedExpander private function filterByDate($item, $input, $expected) { $val = $this->getInput($input); - if($val === "") return null; - $ts = strtotime($val); - if($ts === false) { - throw new Exception("Invalid time specification: " . $val); + if ($val === '') { + return null; } - $cmp = $this->cmp($item['timestamp'], $ts); // 1 if newer, -1 if older + $ts = strtotime($val); + if ($ts === false) { + throw new Exception('Invalid time specification: ' . $val); + } + $cmp = $this->cmp($item['timestamp'], $ts); // 1 if newer, -1 if older return $cmp === $expected; } protected function filterByDateNewer($item) @@ -222,8 +229,11 @@ class FilterMoreBridge extends FeedExpander } protected function filterByMedia($item) { - if(!$this->getInput('has_media')) return null; - if(count($item['enclosures']) > 0) return true; + if (!$this->getInput('has_media')) { + return null; + } elseif (count($item['enclosures']) > 0) { + return true; + } return false; } @@ -248,8 +258,7 @@ class FilterMoreBridge extends FeedExpander public function collectData() { - if ($this->getInput('url') && substr($this->getInput('url'), 0, strlen('http')) !== 'http') - { + 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.'); } From ada8295850da97e20429bb480e4185b06f9bb5cf Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Mar 2024 21:57:14 +0100 Subject: [PATCH 07/12] [FilterMore] fix default sorting direction --- bridges/FilterMoreBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index 11e67369..fca327e4 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -74,7 +74,7 @@ class FilterMoreBridge extends FeedExpander 'Title' => 'title', 'Random' => 'random', ], - 'defaultValue' => 'date', + 'defaultValue' => 'timestamp', ], 'sort_dir' => [ 'name' => 'Sort direction', From d6ec70a44422a28eb313a4c29fe8892d061a92a7 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Mar 2024 21:57:31 +0100 Subject: [PATCH 08/12] [FilterMore] ignore unspecified fields --- bridges/FilterMoreBridge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index fca327e4..c2651e7a 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -184,7 +184,7 @@ class FilterMoreBridge extends FeedExpander } private function filterByFieldRegexp($field, $re) { - if ($re === '') { + if ($re === '' || $re === null) { return null; } elseif (preg_match($re, $field)) { return true; @@ -209,7 +209,7 @@ class FilterMoreBridge extends FeedExpander private function filterByDate($item, $input, $expected) { $val = $this->getInput($input); - if ($val === '') { + if ($val === '' || $val === null) { return null; } $ts = strtotime($val); From 707a8324e5e0f115e62a7dd51320972e847b181b Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 16 Mar 2024 22:00:50 +0100 Subject: [PATCH 09/12] [FilterMore] limit number of entries --- bridges/FilterMoreBridge.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index c2651e7a..a403f40b 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -86,6 +86,7 @@ class FilterMoreBridge extends FeedExpander ], 'defaultValue' => 'asc', ], + 'limit' => self::LIMIT, ]]; @@ -158,6 +159,7 @@ class FilterMoreBridge extends FeedExpander public function collectExpandableDatas($url, $maxItems = -1) { parent::collectExpandableDatas($url, $maxItems); + $limit = (int)($this->getInput('limit') ?: 10); if ($this->getInput('sort_by') === 'random') { shuffle($this->items); } elseif ($this->getInput('sort_by') !== 'none') { @@ -171,6 +173,9 @@ class FilterMoreBridge extends FeedExpander if ($this->getInput('sort_dir') === 'desc') { $this->items = array_reverse($this->items); } + if ($limit > 0) { + $this->items = array_slice($this->items, 0, $limit); + } } private function cmp($a, $b) From 1b41f43c7acc15015a1cc8f6eaa3c5ac9e328543 Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 21 Mar 2024 22:30:19 +0100 Subject: [PATCH 10/12] add example values --- bridges/FilterMoreBridge.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index a403f40b..7e07ad07 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -13,6 +13,7 @@ class FilterMoreBridge extends FeedExpander 'url' => [ 'name' => 'Feed URL', 'required' => true, + 'exampleValue' => 'https://example.com/feed/', ], 'conj_type' => [ 'name' => 'Conjunction type type', @@ -33,6 +34,7 @@ class FilterMoreBridge extends FeedExpander 'body_re' => [ 'name' => 'Filter body (regular expression)', 'required' => false, + 'exampleValue' => '/(technology|politics)/i', ], 'author_re' => [ 'name' => 'Filter author (regular expression)', From 95dfe1573d844e12252d2cde5e4fa9e79c344005 Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 19 Apr 2024 12:51:48 +0200 Subject: [PATCH 11/12] remove unneeded parent call --- bridges/FilterMoreBridge.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index 7e07ad07..b45cad3a 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -92,9 +92,8 @@ class FilterMoreBridge extends FeedExpander ]]; - protected function parseItem($newItem) + protected function parseItem($item) { - $item = parent::parseItem($newItem); $item['enclosures'] = []; if (isset($newItem->enclosure)) { foreach ($newItem->enclosure as $encl) { From 4269f3438a9de9309823c8017e826938e66102ce Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 19 Apr 2024 12:53:07 +0200 Subject: [PATCH 12/12] reorder methods --- bridges/FilterMoreBridge.php | 40 +++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/bridges/FilterMoreBridge.php b/bridges/FilterMoreBridge.php index b45cad3a..608003cb 100644 --- a/bridges/FilterMoreBridge.php +++ b/bridges/FilterMoreBridge.php @@ -92,6 +92,19 @@ class FilterMoreBridge extends FeedExpander ]]; + 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 { + $this->collectExpandableDatas($this->getURI()); + } catch (HttpException $e) { + $this->collectExpandableDatas($this->getURI()); + } + } + protected function parseItem($item) { $item['enclosures'] = []; @@ -151,12 +164,6 @@ class FilterMoreBridge extends FeedExpander } } - protected function sortItemKey($item) - { - $sort_by = $this->getInput('sort_by'); - $key = $item[$sort_by]; - return $key; - } public function collectExpandableDatas($url, $maxItems = -1) { parent::collectExpandableDatas($url, $maxItems); @@ -179,6 +186,14 @@ class FilterMoreBridge extends FeedExpander } } + protected function sortItemKey($item) + { + $sort_by = $this->getInput('sort_by'); + $key = $item[$sort_by]; + return $key; + } + + private function cmp($a, $b) { if ($a > $b) { @@ -261,18 +276,5 @@ class FilterMoreBridge extends FeedExpander } return $url; } - - 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 { - $this->collectExpandableDatas($this->getURI()); - } catch (HttpException $e) { - $this->collectExpandableDatas($this->getURI()); - } - } }