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 / repositories / PermalinkCacheRepository.php

PermalinkCacheRepository.php in 404 Solution trunk, at includes/repositories/PermalinkCacheRepository.php

165 lines 5.2 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 // allow-no-test-found: covered by tests/ContentRepositoryDecompositionTest.php through ContentRepository facade entry points.
8
9 /**
10 * Owns reads and writes for the permalink cache table.
11 */
12 class ABJ_404_Solution_PermalinkCacheRepository {
13
14 /** @var ABJ_404_Solution_DatabaseCore */
15 private $dbCore;
16
17 /**
18 * @param ABJ_404_Solution_DatabaseCore $dbCore
19 */
20 public function __construct(ABJ_404_Solution_DatabaseCore $dbCore) {
21 $this->dbCore = $dbCore;
22 }
23
24 /** @return void */
25 public function truncatePermalinkCacheTable(): void {
26 $query = "truncate table {wp_abj404_permalink_cache}";
27 $this->dbCore->queryAndGetResults($query);
28
29 abj_service('ngram_filter')->invalidateCoverageCaches();
30 }
31
32 /** @param int $post_id @return void */
33 public function removeFromPermalinkCache(int $post_id): void {
34 $query = "delete from {wp_abj404_permalink_cache} where id = %d";
35 $this->dbCore->queryAndGetResults($query, array('query_params' => array($post_id)));
36
37 abj_service('ngram_filter')->invalidateCoverageCaches();
38 }
39
40 /**
41 * @param int|string $id
42 * @return string|null
43 */
44 public function getPermalinkFromCache($id) {
45 $id = absint($id);
46 // allow-unbounded-select: bounded keyset: WHERE id = <pk> / id IN (<caller ids>) single/few-row lookup
47 $query = "select url from {wp_abj404_permalink_cache} where id = " . $id;
48 $results = $this->dbCore->queryAndGetResults($query);
49
50 $rows = is_array($results['rows']) ? $results['rows'] : array();
51 if (empty($rows)) {
52 return null;
53 }
54
55 $row1 = is_array($rows[0] ?? null) ? $rows[0] : array();
56 return isset($row1['url']) && is_string($row1['url']) ? $row1['url'] : null;
57 }
58
59 /**
60 * @param array<int, int|string> $ids
61 * @return array<int, object>
62 */
63 public function getPermalinksByIds(array $ids) {
64 if (empty($ids)) {
65 return array();
66 }
67 $sanitized = array_map('absint', $ids);
68 $placeholders = implode(',', $sanitized);
69 // allow-unbounded-select: bounded keyset: WHERE id = <pk> / id IN (<caller ids>) single/few-row lookup
70 $query = "select id, url from {wp_abj404_permalink_cache} where id in (" . $placeholders . ")";
71 $query = $this->dbCore->doTableNameReplacements($query);
72 $results = $this->dbCore->queryAndGetResults($query);
73 return $this->objectRows($results['rows'] ?? array());
74 }
75
76 /**
77 * @param int|string $id
78 * @return array<string, mixed>|null
79 */
80 public function getPermalinkEtcFromCache($id) {
81 $id = absint($id);
82 // allow-unbounded-select: bounded keyset: WHERE id = <pk> / id IN (<caller ids>) single/few-row lookup
83 $query = "select id, url, meta, url_length, post_parent from {wp_abj404_permalink_cache} where id = " . $id;
84 $results = $this->dbCore->queryAndGetResults($query);
85
86 $rows = is_array($results['rows']) ? $results['rows'] : array();
87 if (empty($rows)) {
88 return null;
89 }
90
91 if (!isset($rows[0]) || !is_array($rows[0])) {
92 return null;
93 }
94
95 return $this->stringKeyedRow($rows[0]);
96 }
97
98 /** @return array<string, mixed> */
99 public function updatePermalinkCache() {
100 $query = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ .
101 "/../sql/updatePermalinkCache.sql");
102
103 $this->dbCore->tableNameResolver()->setSqlBigSelects();
104
105 $results = $this->dbCore->queryAndGetResults($query);
106
107 return $results;
108 }
109
110 /** @return array<string, mixed> */
111 public function updatePermalinkCacheParentPages() {
112 $query = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ .
113 "/../sql/updatePermalinkCacheParentPages.sql");
114
115 $depthSoFar = 0;
116 $results = array();
117 do {
118 $results = $this->dbCore->queryAndGetResults($query);
119 $depthSoFar++;
120 } while ($results['rows_affected'] != 0 && $depthSoFar < 15);
121
122 return $results;
123 }
124
125 /** @return int */
126 public function getPermalinkCacheCount(): int {
127 $table = $this->dbCore->doTableNameReplacements('{wp_abj404_permalink_cache}');
128 return $this->dbCore->queryScalarInt("SELECT COUNT(*) FROM `{$table}`");
129 }
130
131 /**
132 * @param mixed $rows
133 * @return array<int, object>
134 */
135 private function objectRows($rows): array {
136 if (!is_array($rows)) {
137 return array();
138 }
139
140 $objects = array();
141 foreach ($rows as $row) {
142 if (is_object($row)) {
143 $objects[] = $row;
144 } else if (is_array($row)) {
145 $objects[] = (object)$row;
146 }
147 }
148 return $objects;
149 }
150
151 /**
152 * @param array<mixed, mixed> $row
153 * @return array<string, mixed>
154 */
155 private function stringKeyedRow(array $row): array {
156 $stringKeyed = array();
157 foreach ($row as $key => $value) {
158 if (is_string($key)) {
159 $stringKeyed[$key] = $value;
160 }
161 }
162 return $stringKeyed;
163 }
164 }
165