Skip to content

Commit

Permalink
Fixes for rules
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
JKingweb committed Jan 7, 2021
1 parent 461e256 commit 6dba8aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
14 changes: 9 additions & 5 deletions lib/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace JKingWeb\Arsse;

use JKingWeb\Arsse\Misc\Date;
use JKingWeb\Arsse\Rule\Rule;
use PicoFeed\PicoFeedException;
use PicoFeed\Config\Config;
use PicoFeed\Client\Client;
Expand All @@ -25,6 +26,7 @@ class Feed {
public $nextFetch;
public $newItems = [];
public $changedItems = [];
public $filteredItems = [];

public static function discover(string $url, string $username = '', string $password = ''): string {
// fetch the candidate feed
Expand Down Expand Up @@ -452,14 +454,16 @@ protected function scrape(): void {
}

protected function computeFilterRules(int $feedID): void {
return;
$rules = Arsse::$db->feedRulesGet($feedID);
foreach ($rules as $r) {
$keep = "";
$block = "";
if (strlen($r['keep'])) {

$stats = ['new' => [], 'changed' => []];
foreach ($this->newItems as $index => $item) {
$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 changes: 6 additions & 4 deletions lib/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public static function validate(string $pattern): bool {
public static function apply(string $keepRule, string $blockRule, string $title, array $categories = []): bool {
// if neither rule is processed we should keep
$keep = true;
// add the title to the front of the category array
array_unshift($categories, $title);
// merge and clean the data to match
$data = array_map(function($str) {
return preg_replace('/\s+/', " ", $str);
}, array_merge([$title], $categories));
// process the keep rule if it exists
if (strlen($keepRule)) {
try {
Expand All @@ -56,7 +58,7 @@ public static function apply(string $keepRule, string $blockRule, string $title,
}
// if a keep rule is specified the default state is now not to keep
$keep = false;
foreach ($categories as $str) {
foreach ($data as $str) {
if (is_string($str)) {
if (preg_match($rule, $str)) {
// keep if the keep-rule matches one of the strings
Expand All @@ -73,7 +75,7 @@ public static function apply(string $keepRule, string $blockRule, string $title,
} catch (Exception $e) {
return true;
}
foreach ($categories as $str) {
foreach ($data as $str) {
if (is_string($str)) {
if (preg_match($rule, $str)) {
// do not keep if the block-rule matches one of the strings
Expand Down
1 change: 1 addition & 0 deletions tests/cases/Feed/TestFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function setUp(): void {
self::clearData();
self::setConf();
Arsse::$db = \Phake::mock(Database::class);
\Phake::when(Arsse::$db)->feedRulesGet->thenReturn(new Result([]));
}

public function testParseAFeed(): void {
Expand Down
22 changes: 12 additions & 10 deletions tests/cases/Misc/TestRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ public function testApplyRules(string $keepRule, string $blockRule, string $titl

public function provideApplications(): iterable {
return [
["", "", "Title", ["Dummy", "Category"], true],
["^Title$", "", "Title", ["Dummy", "Category"], true],
["^Category$", "", "Title", ["Dummy", "Category"], true],
["^Naught$", "", "Title", ["Dummy", "Category"], false],
["", "^Title$", "Title", ["Dummy", "Category"], false],
["", "^Category$", "Title", ["Dummy", "Category"], false],
["", "^Naught$", "Title", ["Dummy", "Category"], true],
["^Category$", "^Category$", "Title", ["Dummy", "Category"], false],
["[", "", "Title", ["Dummy", "Category"], true],
["", "[", "Title", ["Dummy", "Category"], true],
["", "", "Title", ["Dummy", "Category"], true],
["^Title$", "", "Title", ["Dummy", "Category"], true],
["^Category$", "", "Title", ["Dummy", "Category"], true],
["^Naught$", "", "Title", ["Dummy", "Category"], false],
["", "^Title$", "Title", ["Dummy", "Category"], false],
["", "^Category$", "Title", ["Dummy", "Category"], false],
["", "^Naught$", "Title", ["Dummy", "Category"], true],
["^Category$", "^Category$", "Title", ["Dummy", "Category"], false],
["[", "", "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],
];
}
}

0 comments on commit 6dba8aa

Please sign in to comment.