| @@ -36,8 +36,9 @@ | ||
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | $this->search_migration(); |
| 39 | 39 | $this->fix_search_table_collation(); |
| 40 | + $this->backfill_search_keyword_hash(); | |
| 40 | 41 | |
| 41 | 42 | /** |
| 42 | 43 | * Settings Migration |
| 43 | 44 | */ |
| @@ -82,12 +83,13 @@ | ||
| 82 | 83 | if ( $keyword == null ) { |
| 83 | 84 | $insert = $wpdb->query( |
| 84 | 85 | $wpdb->prepare( |
| 85 | 86 | "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword |
| 86 | - ( keyword ) | |
| 87 | - VALUES ( %s )", | |
| 87 | + ( keyword, keyword_hash ) | |
| 88 | + VALUES ( %s, %s )", | |
| 88 | 89 | [ |
| 89 | - $key | |
| 90 | + $key, | |
| 91 | + md5( $key ) | |
| 90 | 92 | ] |
| 91 | 93 | ) |
| 92 | 94 | ); |
| 93 | 95 | |
| @@ -154,6 +156,55 @@ | ||
| 154 | 156 | } |
| 155 | 157 | |
| 156 | 158 | // Mark migration as complete |
| 157 | 159 | $this->database->save( 'betterdocs_search_collation_fixed', '1.0' ); |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Populate keyword_hash for rows that predate the column. | |
| 164 | + * | |
| 165 | + * The column is added by dbDelta with an empty default; until it holds the | |
| 166 | + * hash, insert_search_keyword() cannot find those rows by index and would | |
| 167 | + * insert duplicates. Runs in bounded batches so a large keyword table does | |
| 168 | + * not stall the request that triggers the upgrade — anything left over is | |
| 169 | + * picked up on the next admin load. | |
| 170 | + * | |
| 171 | + * @since 1.0.3 | |
| 172 | + * @return void | |
| 173 | + */ | |
| 174 | + public function backfill_search_keyword_hash() { | |
| 175 | + global $wpdb; | |
| 176 | + | |
| 177 | + if ( $this->database->get( 'betterdocs_search_keyword_hash_filled', false ) ) { | |
| 178 | + return; | |
| 179 | + } | |
| 180 | + | |
| 181 | + $table = $wpdb->prefix . 'betterdocs_search_keyword'; | |
| 182 | + | |
| 183 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- one-shot schema backfill; identifier comes from $wpdb. | |
| 184 | + if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) !== $table ) { | |
| 185 | + return; | |
| 186 | + } | |
| 187 | + | |
| 188 | + // Bail if the column never made it (dbDelta failed) — retry next load. | |
| 189 | + if ( ! $wpdb->get_results( "SHOW COLUMNS FROM `{$table}` LIKE 'keyword_hash'" ) ) { | |
| 190 | + return; | |
| 191 | + } | |
| 192 | + | |
| 193 | + $batches = 0; | |
| 194 | + do { | |
| 195 | + $updated = $wpdb->query( | |
| 196 | + "UPDATE `{$table}` SET keyword_hash = MD5( keyword ) WHERE keyword_hash = '' LIMIT 2000" | |
| 197 | + ); | |
| 198 | + $batches++; | |
| 199 | + } while ( $updated > 0 && $batches < 25 ); | |
| 200 | + | |
| 201 | + // Only finish once nothing is left, so a table larger than 50k keywords | |
| 202 | + // resumes instead of being marked done half-filled. | |
| 203 | + $remaining = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$table}` WHERE keyword_hash = ''" ); | |
| 204 | + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 205 | + | |
| 206 | + if ( 0 === $remaining ) { | |
| 207 | + $this->database->save( 'betterdocs_search_keyword_hash_filled', '1.0.3' ); | |
| 208 | + } | |
| 158 | 209 | } |
| 159 | 210 | } |