| 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 |
// allow-unbounded-select: single lkup_value equality; only the first row is used |
| 50 |
$query = "select id from {wp_abj404_lookup} where lkup_value = %s"; |
| 51 |
$results = $this->dbCore->queryAndGetResults($query, array('query_params' => array($userName))); |
| 52 |
$lookupRows = is_array($results['rows']) ? $results['rows'] : array(); |
| 53 |
if (count($lookupRows) > 0) { |
| 54 |
$row1 = is_array($lookupRows[0]) ? $lookupRows[0] : array(); |
| 55 |
$id = isset($row1['id']) ? $row1['id'] : 0; |
| 56 |
return is_scalar($id) ? intval($id) : 0; |
| 57 |
} |
| 58 |
return -1; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Repair the lookup table after duplicate lkup_value rows accumulate |
| 63 |
* (pre-4.1.8 installs could insert duplicates before the unique key). |
| 64 |
* Runs with log_errors=false / skip_repair=true so a missing table during |
| 65 |
* fresh-install doesn't spam the debug log or trigger repair recursion. |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function correctDuplicateLookupValues(): void { |
| 70 |
$query = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ . "/../sql/correctLookupTableIssue.sql"); |
| 71 |
$this->dbCore->queryAndGetResults($query, array('log_errors' => false, 'skip_repair' => true)); |
| 72 |
} |
| 73 |
} |
| 74 |
|