58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
namespace AfFulltext;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The outcome of one extraction, diagnostics included.
|
||
|
|
*
|
||
|
|
* The diagnostics are the point. A site redesign turns a `body:` rule into a
|
||
|
|
* silent no-op that falls through to Readability, and the article still looks
|
||
|
|
* plausible -- that is how a broken webcomic rule went unnoticed here for years.
|
||
|
|
* Every consumer of this class can see exactly which rule ran and whether it
|
||
|
|
* actually matched anything.
|
||
|
|
*/
|
||
|
|
final class ExtractResult {
|
||
|
|
public string $html = '';
|
||
|
|
public ?string $title = null;
|
||
|
|
public ?string $author = null;
|
||
|
|
public ?string $date = null;
|
||
|
|
|
||
|
|
public string $backend = '';
|
||
|
|
public string $effective_url = '';
|
||
|
|
|
||
|
|
/** Rule files that contributed, nearest first. @var string[] */
|
||
|
|
public array $rule_sources = [];
|
||
|
|
|
||
|
|
/** True when a `body:` rule existed and selected at least one node. */
|
||
|
|
public bool $rule_matched = false;
|
||
|
|
|
||
|
|
/** True when the content came from Readability rather than a rule. */
|
||
|
|
public bool $fell_back = false;
|
||
|
|
|
||
|
|
/** True when a rule existed but matched nothing -- the stale-rule signal. */
|
||
|
|
public bool $rule_stale = false;
|
||
|
|
|
||
|
|
public int $pages = 1;
|
||
|
|
public int $timing_ms = 0;
|
||
|
|
|
||
|
|
/** @var string[] */
|
||
|
|
public array $errors = [];
|
||
|
|
|
||
|
|
public function ok(): bool {
|
||
|
|
return $this->html !== '';
|
||
|
|
}
|
||
|
|
|
||
|
|
/** One-line summary for logs and the prefs UI. */
|
||
|
|
public function summary(): string {
|
||
|
|
$rule = $this->rule_sources ? basename($this->rule_sources[0]) : 'none';
|
||
|
|
|
||
|
|
$how = match (true) {
|
||
|
|
$this->rule_matched => "rule=$rule",
|
||
|
|
$this->rule_stale => "rule=$rule STALE->readability",
|
||
|
|
default => 'readability',
|
||
|
|
};
|
||
|
|
|
||
|
|
return sprintf('%s %s %db %dms%s', $this->backend, $how, strlen($this->html),
|
||
|
|
$this->timing_ms, $this->pages > 1 ? " pages={$this->pages}" : '');
|
||
|
|
}
|
||
|
|
}
|