PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / stats / RedirectRowCountRepository.php

RedirectRowCountRepository.php in 404 Solution trunk, at includes/stats/RedirectRowCountRepository.php

79 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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