Browse Source

Fixes for rules

- Whitespace is now collapsed before evaluating rules
- Feed tests are fixed to retrieve a dumy set of rules
- Rule evaluation during feed parsing also filled out
rpm
J. King 3 years ago
parent
commit
6dba8aa66b
  1. 14
      lib/Feed.php
  2. 10
      lib/Rule/Rule.php
  3. 1
      tests/cases/Feed/TestFeed.php
  4. 22
      tests/cases/Misc/TestRule.php

14
lib/Feed.php

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace JKingWeb\Arsse; namespace JKingWeb\Arsse;
use JKingWeb\Arsse\Misc\Date; use JKingWeb\Arsse\Misc\Date;
use JKingWeb\Arsse\Rule\Rule;
use PicoFeed\PicoFeedException; use PicoFeed\PicoFeedException;
use PicoFeed\Config\Config; use PicoFeed\Config\Config;
use PicoFeed\Client\Client; use PicoFeed\Client\Client;
@ -25,6 +26,7 @@ class Feed {
public $nextFetch; public $nextFetch;
public $newItems = []; public $newItems = [];
public $changedItems = []; public $changedItems = [];
public $filteredItems = [];
public static function discover(string $url, string $username = '', string $password = ''): string { public static function discover(string $url, string $username = '', string $password = ''): string {
// fetch the candidate feed // fetch the candidate feed
@ -452,14 +454,16 @@ class Feed {
} }
protected function computeFilterRules(int $feedID): void { protected function computeFilterRules(int $feedID): void {
return;
$rules = Arsse::$db->feedRulesGet($feedID); $rules = Arsse::$db->feedRulesGet($feedID);
foreach ($rules as $r) { foreach ($rules as $r) {
$keep = ""; $stats = ['new' => [], 'changed' => []];
$block = ""; foreach ($this->newItems as $index => $item) {
if (strlen($r['keep'])) { $stats['new'][$index] = Rule::apply($r['keep'], $r['block'], $item->title, $item->categories);
} }
foreach ($this->changedItems as $index => $item) {
$stats['changed'][$index] = Rule::apply($r['keep'], $r['block'], $item->title, $item->categories);
}
$this->filteredItems[$r['owner']] = $stats;
} }
} }
} }

10
lib/Rule/Rule.php

@ -45,8 +45,10 @@ abstract class Rule {
public static function apply(string $keepRule, string $blockRule, string $title, array $categories = []): bool { public static function apply(string $keepRule, string $blockRule, string $title, array $categories = []): bool {
// if neither rule is processed we should keep // if neither rule is processed we should keep
$keep = true; $keep = true;
// add the title to the front of the category array // merge and clean the data to match
array_unshift($categories, $title); $data = array_map(function($str) {
return preg_replace('/\s+/', " ", $str);
}, array_merge([$title], $categories));
// process the keep rule if it exists // process the keep rule if it exists
if (strlen($keepRule)) { if (strlen($keepRule)) {
try { try {
@ -56,7 +58,7 @@ abstract class Rule {
} }
// if a keep rule is specified the default state is now not to keep // if a keep rule is specified the default state is now not to keep
$keep = false; $keep = false;
foreach ($categories as $str) { foreach ($data as $str) {
if (is_string($str)) { if (is_string($str)) {
if (preg_match($rule, $str)) { if (preg_match($rule, $str)) {
// keep if the keep-rule matches one of the strings // keep if the keep-rule matches one of the strings
@ -73,7 +75,7 @@ abstract class Rule {
} catch (Exception $e) { } catch (Exception $e) {
return true; return true;
} }
foreach ($categories as $str) { foreach ($data as $str) {
if (is_string($str)) { if (is_string($str)) {
if (preg_match($rule, $str)) { if (preg_match($rule, $str)) {
// do not keep if the block-rule matches one of the strings // do not keep if the block-rule matches one of the strings

1
tests/cases/Feed/TestFeed.php

@ -95,6 +95,7 @@ class TestFeed extends \JKingWeb\Arsse\Test\AbstractTest {
self::clearData(); self::clearData();
self::setConf(); self::setConf();
Arsse::$db = \Phake::mock(Database::class); Arsse::$db = \Phake::mock(Database::class);
\Phake::when(Arsse::$db)->feedRulesGet->thenReturn(new Result([]));
} }
public function testParseAFeed(): void { public function testParseAFeed(): void {

22
tests/cases/Misc/TestRule.php

@ -35,16 +35,18 @@ class TestRule extends \JKingWeb\Arsse\Test\AbstractTest {
public function provideApplications(): iterable { public function provideApplications(): iterable {
return [ return [
["", "", "Title", ["Dummy", "Category"], true], ["", "", "Title", ["Dummy", "Category"], true],
["^Title$", "", "Title", ["Dummy", "Category"], true], ["^Title$", "", "Title", ["Dummy", "Category"], true],
["^Category$", "", "Title", ["Dummy", "Category"], true], ["^Category$", "", "Title", ["Dummy", "Category"], true],
["^Naught$", "", "Title", ["Dummy", "Category"], false], ["^Naught$", "", "Title", ["Dummy", "Category"], false],
["", "^Title$", "Title", ["Dummy", "Category"], false], ["", "^Title$", "Title", ["Dummy", "Category"], false],
["", "^Category$", "Title", ["Dummy", "Category"], false], ["", "^Category$", "Title", ["Dummy", "Category"], false],
["", "^Naught$", "Title", ["Dummy", "Category"], true], ["", "^Naught$", "Title", ["Dummy", "Category"], true],
["^Category$", "^Category$", "Title", ["Dummy", "Category"], false], ["^Category$", "^Category$", "Title", ["Dummy", "Category"], false],
["[", "", "Title", ["Dummy", "Category"], true], ["[", "", "Title", ["Dummy", "Category"], true],
["", "[", "Title", ["Dummy", "Category"], true], ["", "[", "Title", ["Dummy", "Category"], true],
["", "^A B C$", "A B\nC", ["X\n Y \t \r Z"], false],
["", "^X Y Z$", "A B\nC", ["X\n Y \t \r Z"], false],
]; ];
} }
} }

Loading…
Cancel
Save