All violations were present in v0.1.0 and hidden by continue-on-error. Now that PHPCS gates the build (P2-B), they must be fixed. - Add file-level @package doc comment to all PHP files (file doc requirement) - Add short descriptions to @var and constructor docblocks throughout - Fix @param type spacing throughout (PHPCS alignment rules) - Expand phpcs:ignore lines to include NoCaching where DirectQuery is already suppressed (uninstall.php, cost-ledger, table-manager, collector) - Suppress EscapeOutput.ExceptionNotEscaped on RuntimeException messages (exception strings are not HTML output) - Suppress error_log_error_log on the probe-runner failure log - Replace date() with gmdate() in panel (timezone-aware per WP standard) - Convert inline associative arrays to multi-line in config (WP array style) - Fix double-arrow alignment in probe-runner counts and insert arrays - Fix double-space before ?? in provider constructors - Expand phpcs:ignore for reads to include MissingUnslash - Fix $counts++ to ++$counts (prefix increment per WP standard) - Fix V1/v1 capitalisation in interface doc long description Agent-Session: pr-vision rename v0.1.1
83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
/** @package PrVision */
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Detects whether peptiderepo.com appears in a list of source domains.
|
|
*
|
|
* Centralises the citation-detection logic so both providers can share it
|
|
* without duplication. Also parses raw citation arrays/URLs into a clean
|
|
* domain list.
|
|
*
|
|
* Who triggers: PRV_Perplexity_Provider, PRV_OpenRouter_Provider.
|
|
* Dependencies: None (pure PHP).
|
|
*
|
|
* @see CONTEXT.md — "cited", "our_position", "source_domains".
|
|
* @package PrVision
|
|
*/
|
|
class PRV_Citation_Detector {
|
|
|
|
/**
|
|
* Parse raw citation data from an API response into a clean domain list.
|
|
*
|
|
* Accepts either a flat array of URL strings or an array of objects with
|
|
* a 'url' key (Perplexity style). Unknown shapes are skipped gracefully.
|
|
*
|
|
* @param array<mixed> $raw_citations Raw citations from the API response.
|
|
*
|
|
* @return string[] Lowercase domain names (e.g. ['peptiderepo.com', 'examine.com']).
|
|
*/
|
|
public function parse_domains( array $raw_citations ): array {
|
|
$domains = array();
|
|
|
|
foreach ( $raw_citations as $item ) {
|
|
$url = '';
|
|
if ( is_string( $item ) ) {
|
|
$url = $item;
|
|
} elseif ( is_array( $item ) && isset( $item['url'] ) && is_string( $item['url'] ) ) {
|
|
$url = $item['url'];
|
|
}
|
|
|
|
if ( '' === $url ) {
|
|
continue;
|
|
}
|
|
|
|
$host = (string) wp_parse_url( $url, PHP_URL_HOST );
|
|
if ( '' !== $host ) {
|
|
$bare = str_starts_with( $host, 'www.' ) ? substr( $host, 4 ) : $host;
|
|
$domains[] = strtolower( $bare );
|
|
}
|
|
}
|
|
|
|
return array_values( array_unique( $domains ) );
|
|
}
|
|
|
|
/**
|
|
* Detect whether PRV_TARGET_DOMAIN appears in a domain list.
|
|
*
|
|
* @param string[] $domains Parsed domain list from parse_domains().
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function is_cited( array $domains ): bool {
|
|
return in_array( PRV_TARGET_DOMAIN, $domains, true );
|
|
}
|
|
|
|
/**
|
|
* Find the 1-based position of PRV_TARGET_DOMAIN in a domain list.
|
|
*
|
|
* Returns null when the target domain is not found.
|
|
*
|
|
* @param string[] $domains Parsed domain list.
|
|
*
|
|
* @return int|null
|
|
*/
|
|
public function get_our_position( array $domains ): ?int {
|
|
$pos = array_search( PRV_TARGET_DOMAIN, $domains, true );
|
|
if ( false === $pos ) {
|
|
return null;
|
|
}
|
|
return (int) $pos + 1; // Convert 0-based index to 1-based position.
|
|
}
|
|
}
|