Skip to content

Commit 5033db9

Browse files
CopilotPDowney
andauthored
fix(vhost-import): apply code quality improvements (diffs 2-5)
- Use mapfile + find -print0 | sort -z for deterministic SQL file detection - Extract URL_VALIDATION_REGEX to readonly variable at top of script - Improve WordPress salt fetch error message with troubleshooting guidance - Replace wp db search | grep -qF with --quiet flag to avoid double scan Agent-Logs-Url: https://github.com/EngineScript/EngineScript/sessions/d84282a4-c592-4316-a4c7-7fee044ea66d Co-authored-by: PDowney <[email protected]>
1 parent 77f05dc commit 5033db9

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to EngineScript will be documented in this file.
44

55
Changes are organized by date, with the most recent changes listed first.
66

7+
## 2026-04-10 (2)
8+
9+
### 🔧 VHOST IMPORT CODE QUALITY IMPROVEMENTS (CONTINUED)
10+
11+
- Replaced two-pass `find -print -quit` + second `find ! -samefile` SQL file detection with `mapfile` + `find -print0 | sort -z` in `scripts/functions/vhost/vhost-import.sh`, ensuring deterministic selection and a single-pass exact-count check.
12+
- Extracted the URL validation regex to a `readonly URL_VALIDATION_REGEX` variable at the top of `scripts/functions/vhost/vhost-import.sh` to eliminate duplication and simplify future maintenance.
13+
- Improved the WordPress salt fetch error message to include troubleshooting guidance (internet connection, DNS/firewall/proxy settings, retry instructions).
14+
- Replaced `wp db search … | grep -qF` with `wp db search … --quiet` so the exit status is used directly, eliminating a redundant full-database grep pass in `run_url_search_replace_if_present`.
15+
716
## 2026-04-10
817

918
### 🔧 VHOST IMPORT CODE QUALITY IMPROVEMENTS

scripts/functions/vhost/vhost-import.sh

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ 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})?([/?#].*)?$"
3233

3334
# --- Instructions for Preparing Files ---
3435
echo ""
@@ -185,20 +186,14 @@ if [[ "$IMPORT_FORMAT" == "single_zip" ]]; then
185186
unzip -q "${SINGLE_ZIP_FILE}" -d "${WP_EXTRACTED_PATH}"
186187
EXTRACT_STATUS=$?
187188
if [[ $EXTRACT_STATUS -eq 0 ]]; then
188-
# Find exactly one .sql file within the extracted content
189-
DB_SOURCE_CANDIDATE=$(find "${WP_EXTRACTED_PATH}" -maxdepth 1 -type f -name "*.sql" -print -quit)
190-
if [[ -z "$DB_SOURCE_CANDIDATE" ]]; then
189+
# Find exactly one .sql file within the extracted content (deterministic order)
190+
mapfile -d '' -t DB_SOURCE_CANDIDATES < <(find "${WP_EXTRACTED_PATH}" -maxdepth 1 -type f -name "*.sql" -print0 | sort -z)
191+
if [[ ${#DB_SOURCE_CANDIDATES[@]} -ne 1 ]]; then
191192
echo "FAILED: Could not find exactly one .sql file within the extracted single zip content in ${WP_EXTRACTED_PATH}"
192193
EXTRACT_STATUS=1 # Mark as failure
193194
else
194-
DB_SOURCE_SECOND_CANDIDATE=$(find "${WP_EXTRACTED_PATH}" -maxdepth 1 -type f -name "*.sql" ! -samefile "$DB_SOURCE_CANDIDATE" -print -quit)
195-
if [[ -n "$DB_SOURCE_SECOND_CANDIDATE" ]]; then
196-
echo "FAILED: Could not find exactly one .sql file within the extracted single zip content in ${WP_EXTRACTED_PATH}"
197-
EXTRACT_STATUS=1 # Mark as failure
198-
else
199-
DB_SOURCE_PATH="$DB_SOURCE_CANDIDATE" # Set DB path for single_zip format
200-
echo "PASSED: Found database file within extracted content: ${DB_SOURCE_PATH}"
201-
fi
195+
DB_SOURCE_PATH="${DB_SOURCE_CANDIDATES[0]}" # Set DB path for single_zip format
196+
echo "PASSED: Found database file within extracted content: ${DB_SOURCE_PATH}"
202197
fi
203198
fi
204199
elif [[ "$IMPORT_FORMAT" == "two_file" ]]; then
@@ -309,7 +304,7 @@ while true; do
309304

310305
# Site URL input with validation
311306
while true; do
312-
new_site_url=$(prompt_input "Enter correct Site URL" "${SITE_URL}" 300 "^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})?([/?#].*)?$")
307+
new_site_url=$(prompt_input "Enter correct Site URL" "${SITE_URL}" 300 "${URL_VALIDATION_REGEX}")
313308
if [[ -n "$new_site_url" ]]; then
314309
if validate_url "$new_site_url"; then
315310
SITE_URL="$new_site_url"
@@ -488,7 +483,7 @@ configure_redis "${SITE_URL}" "${TARGET_WP_PATH}/wp-config.php"
488483
# WP Salt Creation (Generate new salts)
489484
echo "Generating new WordPress salts..."
490485
SALT=$(curl --fail --silent --show-error --location --retry 3 --connect-timeout 10 --max-time 30 "https://api.wordpress.org/secret-key/1.1/salt/") || {
491-
echo "Error: Failed to fetch WordPress salts from api.wordpress.org" >&2
486+
echo "Error: Failed to fetch WordPress salts from api.wordpress.org. Please check your internet connection, DNS/firewall/proxy settings, and try running the import again." >&2
492487
exit 1
493488
}
494489

@@ -551,7 +546,8 @@ run_url_search_replace_if_present() {
551546
local original_url="$1"
552547

553548
# Only run expensive full-table replacements when the source URL is present.
554-
if wp db search "${original_url}" --all-tables --allow-root 2>/dev/null | grep -qF "${original_url}"; then
549+
# Use `wp db search --quiet` exit status directly to avoid an extra grep pass.
550+
if wp db search "${original_url}" --all-tables --allow-root --quiet 2>/dev/null; then
555551
wp search-replace "${original_url}" "${NEW_URL}" --all-tables --report-changed-only --allow-root
556552
else
557553
echo "Skipping search-replace: '${original_url}' not found in database text columns."

0 commit comments

Comments
 (0)