Skip to content

Commit 7e232a1

Browse files
CopilotPDowney
andauthored
refactor(vhost-import): code quality improvements – regex, dead code, and variable cleanup
Agent-Logs-Url: https://github.com/EngineScript/EngineScript/sessions/10aecbc9-7e84-477f-a613-f5eabf7fae21 Co-authored-by: PDowney <[email protected]>
1 parent 358f808 commit 7e232a1

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ Changes are organized by date, with the most recent changes listed first.
66

77
## 2026-04-10
88

9+
### 🔧 VHOST IMPORT CODE QUALITY IMPROVEMENTS (SECOND PASS)
10+
11+
- Made `URL_VALIDATION_REGEX` overridable via environment variable in `scripts/functions/vhost/vhost-import.sh`; the built-in pattern is now stored as `DEFAULT_URL_VALIDATION_REGEX` and used as the fallback, improving consistency and testability.
12+
- Fixed `extract_define` to escape special regex characters in the key parameter before passing it to `grep -E`, preventing unexpected behavior when keys contain metacharacters.
13+
- Removed a dead-code comment (`# Removed intermediate echo: ...`) from `extract_prefix_from_db` in `scripts/functions/vhost/vhost-import.sh`.
14+
- Eliminated the unnecessary `EXTRACTED_DOMAIN` intermediate variable; `SITE_URL` is now assigned directly from the `sed` expression, and the corresponding echo label updated accordingly.
15+
- Consolidated `ORIGINAL_URL` and `NEW_URL` into a single `NEW_URL` variable; the downstream `HTTPS_ORIGINAL_URL` assignment now references `NEW_URL` directly, removing redundant dead code.
16+
917
### 🔧 VHOST IMPORT CODE QUALITY IMPROVEMENTS
1018

1119
- Added explicit `return` statement at the end of `run_url_search_replace_if_present` in `scripts/functions/vhost/vhost-import.sh` to satisfy shell best-practice linting (SC2151/explicit-return warning).

scripts/functions/vhost/vhost-import.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ WP_EXTRACTED_PATH="${IMPORT_BASE_DIR}/extracted-root" # Temporary path for extra
2929

3030
# --- Supported DB Charset Configuration ---
3131
readonly ALLOWED_DB_CHARSETS=("utf8mb4" "utf8" "latin1")
32-
readonly URL_VALIDATION_REGEX="^https?://([A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)*[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(:[0-9]{1,5})?([/?#].*)?$"
32+
readonly DEFAULT_URL_VALIDATION_REGEX="^https?://([A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)*[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(:[0-9]{1,5})?([/?#].*)?$"
33+
URL_VALIDATION_REGEX="${URL_VALIDATION_REGEX:-$DEFAULT_URL_VALIDATION_REGEX}"
3334

3435
# --- Instructions for Preparing Files ---
3536
echo ""
@@ -88,9 +89,11 @@ sleep 1
8889
# Function to extract define values (Handles single/double quotes)
8990
extract_define() {
9091
local key="$1"
92+
local escaped_key
93+
escaped_key=$(printf '%s' "$key" | sed -E 's#[][(){}.^$*+?|\\/-]#\\&#g')
9194
# Find the line defining the key
9295
local line
93-
line=$(grep -E "^\s*define\(\s*['\"]${key}['\"]\s*," "$WP_CONFIG_PATH")
96+
line=$(grep -E "^\s*define\(\s*['\"]${escaped_key}['\"]\s*," "$WP_CONFIG_PATH")
9497
# Extract the value between single or double quotes after the comma
9598
local value
9699
value=$(echo "$line" | sed -E "s/.*,\s*['\"]([^'\"]*)['\"].*/\1/")
@@ -130,7 +133,6 @@ extract_prefix_from_db() {
130133
if [[ "${prefix: -1}" != "_" ]]; then
131134
prefix="${prefix}_"
132135
fi
133-
# Removed intermediate echo: echo "Found and cleaned prefix: ${prefix}"
134136
else
135137
echo "Warning: Could not find common table pattern (like 'prefix_options') in the DB file." >&2 # Output warning to stderr
136138
fi
@@ -278,8 +280,7 @@ if [[ -z "$SITE_URL_RAW" ]]; then
278280
fi
279281

280282
# Extract domain from URL (remove http(s):// and potential trailing slash)
281-
EXTRACTED_DOMAIN=$(echo "$SITE_URL_RAW" | sed -E 's#^https?://##; s#/$##') # Use the clean domain extracted from URL
282-
SITE_URL="${EXTRACTED_DOMAIN}"
283+
SITE_URL=$(echo "$SITE_URL_RAW" | sed -E 's#^https?://##; s#/$##') # Use the clean domain extracted from URL
283284

284285
# Extract DB Charset (optional, for reference)
285286
DB_CHARSET=$(extract_define 'DB_CHARSET')
@@ -289,7 +290,7 @@ if [[ -z "$DB_CHARSET" ]]; then
289290
fi
290291

291292
echo "Extracted Information:"
292-
echo " Domain (EXTRACTED_DOMAIN): ${EXTRACTED_DOMAIN}"
293+
echo " Domain (SITE_URL): ${SITE_URL}"
293294
echo " Table Prefix (PREFIX): ${PREFIX}" # Already extracted from DB
294295
echo " DB Charset: ${DB_CHARSET}"
295296
sleep 1 # Short pause
@@ -379,10 +380,9 @@ else
379380
echo "${BOLD}Pre-import Check: Passed${NORMAL}"
380381
fi
381382

382-
# Keep both variables intentionally: ORIGINAL_URL is the source URL and NEW_URL is the target URL
383-
# for search-replace workflows. They are initialized identically by default and may diverge later.
384-
ORIGINAL_URL="https://${SITE_URL}" # Assume https for consistency
385-
NEW_URL="https://${SITE_URL}"
383+
# Canonical URL used for import/search-replace workflows.
384+
# Currently source and target are the same during import.
385+
NEW_URL="https://${SITE_URL}" # Assume https for consistency
386386

387387
# Logging
388388
LOG_FILE="/var/log/EngineScript/vhost-import.log"
@@ -552,7 +552,7 @@ fi
552552
# Search and Replace URLs
553553
echo "Running search-replace for URL consistency in the database..."
554554
echo "Ensuring URL is '${NEW_URL}'"
555-
HTTPS_ORIGINAL_URL="${ORIGINAL_URL}"
555+
HTTPS_ORIGINAL_URL="${NEW_URL}"
556556
HTTP_ORIGINAL_URL="${HTTPS_ORIGINAL_URL/#https:\/\//http://}"
557557

558558
run_url_search_replace_if_present() {

0 commit comments

Comments
 (0)