| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** Reads uncached row totals from the redirects table. */ |
| 8 |
class ABJ_404_Solution_RedirectRowCountRepository { |
| 9 |
|
| 10 |
/** @var ABJ_404_Solution_DatabaseQueryInterface */ |
| 11 |
private $dbCore; |
| 12 |
|
| 13 |
/** @var ABJ_404_Solution_TableReadinessGate */ |
| 14 |
private $readiness; |
| 15 |
|
| 16 |
public function __construct( |
| 17 |
ABJ_404_Solution_DatabaseQueryInterface $dbCore, |
| 18 |
ABJ_404_Solution_TableReadinessGate $readiness |
| 19 |
) { |
| 20 |
$this->dbCore = $dbCore; |
| 21 |
$this->readiness = $readiness; |
| 22 |
} |
| 23 |
|
| 24 |
/** Return the uncached number of captured redirects. */ |
| 25 |
public function getCapturedCount(): int { |
| 26 |
if ($this->readiness->isKnownAbsent('{wp_abj404_redirects}')) { |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
// allow-unbounded-select: COUNT aggregate; returns a single row |
| 31 |
$query = "select count(id) from {wp_abj404_redirects} where status = " . absint(ABJ404_STATUS_CAPTURED); |
| 32 |
$result = $this->dbCore->queryAndGetResults($query); |
| 33 |
if (!empty($result['timed_out']) || (isset($result['last_error']) && $result['last_error'] != '')) { |
| 34 |
return 0; |
| 35 |
} |
| 36 |
$rows = is_array($result['rows'] ?? null) ? $result['rows'] : array(); |
| 37 |
if (empty($rows)) { |
| 38 |
return 0; |
| 39 |
} |
| 40 |
$first = $rows[0]; |
| 41 |
$value = is_array($first) ? reset($first) : $first; |
| 42 |
return self::scalarToInt($value); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Return the uncached number of live or trashed redirects in the statuses. |
| 47 |
* |
| 48 |
* @param array<int, int> $types Status codes to include. |
| 49 |
* @param int $trashed 0 for live rows, 1 for trashed rows. |
| 50 |
*/ |
| 51 |
public function getRecordCount(array $types = array(), $trashed = 0): int { |
| 52 |
if (count($types) < 1) { |
| 53 |
return 0; |
| 54 |
} |
| 55 |
if ($this->readiness->isKnownAbsent('{wp_abj404_redirects}')) { |
| 56 |
return 0; |
| 57 |
} |
| 58 |
$filteredTypes = array_map('absint', $types); |
| 59 |
$typesForSQL = implode(', ', $filteredTypes); |
| 60 |
// allow-unbounded-select: COUNT aggregate; returns a single row |
| 61 |
$query = "select count(id) as count from {wp_abj404_redirects} where 1 and (status in (" |
| 62 |
. $typesForSQL . "))" |
| 63 |
. " and disabled = " . absint($trashed); |
| 64 |
|
| 65 |
$result = $this->dbCore->queryAndGetResults($query); |
| 66 |
$rows = is_array($result['rows'] ?? null) ? $result['rows'] : array(); |
| 67 |
if (empty($rows)) { |
| 68 |
return 0; |
| 69 |
} |
| 70 |
$row = is_array($rows[0] ?? null) ? $rows[0] : array(); |
| 71 |
return isset($row['count']) && is_scalar($row['count']) ? intval($row['count']) : 0; |
| 72 |
} |
| 73 |
|
| 74 |
/** @param mixed $value */ |
| 75 |
private static function scalarToInt($value): int { |
| 76 |
return is_scalar($value) ? intval($value) : 0; |
| 77 |
} |
| 78 |
} |
| 79 |
|