PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
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 / logs / LogsLookupRepository.php

LogsLookupRepository.php in 404 Solution 4.3.0, at includes/logs/LogsLookupRepository.php

73 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 /**
8 * Repository for the wp_abj404_lookup table (lkup_value to id mapping).
9 *
10 * The lookup table stores small repeating strings (currently user names) by id
11 * so the wide logsv2 table can keep a compact integer reference instead of
12 * repeating the string per log row. Three operations: insert-or-get-id by
13 * value, reverse-lookup by value, and duplicate cleanup (used during
14 * fresh-install repair).
15 *
16 * Extracted from LogsRepository under M201. Consumed by LogsWriter during log
17 * writes (insertLookupValueAndGetID) and by DatabaseUpgradeTableRepair (via
18 * the LogsRepository facade) for duplicate cleanup.
19 */
20 class ABJ_404_Solution_LogsLookupRepository {
21
22 /** @var ABJ_404_Solution_DatabaseCore */
23 private $dbCore;
24
25 public function __construct(ABJ_404_Solution_DatabaseCore $dbCore) {
26 $this->dbCore = $dbCore;
27 }
28
29 /**
30 * Insert a lookup value (or fetch its existing id) and return the id.
31 *
32 * @param string $valueToInsert
33 * @return int
34 */
35 public function insertLookupValueAndGetID($valueToInsert) {
36 global $wpdb;
37 $query = "INSERT INTO {wp_abj404_lookup} (lkup_value) VALUES (%s) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)";
38 $this->dbCore->queryAndGetResults($query, array('query_params' => array($valueToInsert)));
39 return intval($wpdb->insert_id);
40 }
41
42 /**
43 * Reverse-lookup an id for an existing value. Returns -1 if missing.
44 *
45 * @param string $userName
46 * @return int
47 */
48 public function getLookupIDForUser($userName) {
49 $query = "select id from {wp_abj404_lookup} where lkup_value = %s";
50 $results = $this->dbCore->queryAndGetResults($query, array('query_params' => array($userName)));
51 $lookupRows = is_array($results['rows']) ? $results['rows'] : array();
52 if (count($lookupRows) > 0) {
53 $row1 = is_array($lookupRows[0]) ? $lookupRows[0] : array();
54 $id = isset($row1['id']) ? $row1['id'] : 0;
55 return is_scalar($id) ? intval($id) : 0;
56 }
57 return -1;
58 }
59
60 /**
61 * Repair the lookup table after duplicate lkup_value rows accumulate
62 * (pre-4.1.8 installs could insert duplicates before the unique key).
63 * Runs with log_errors=false / skip_repair=true so a missing table during
64 * fresh-install doesn't spam the debug log or trigger repair recursion.
65 *
66 * @return void
67 */
68 public function correctDuplicateLookupValues(): void {
69 $query = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ . "/../sql/correctLookupTableIssue.sql");
70 $this->dbCore->queryAndGetResults($query, array('log_errors' => false, 'skip_repair' => true));
71 }
72 }
73