Skip to content

fix: optimize dpkg calls and fix sed backreferences in php-update.sh - #232

Merged
PDowney merged 3 commits into
masterfrom
copilot/optimize-dpkg-command-performance
May 2, 2026
Merged

PDowney merged 3 commits into
masterfrom
copilot/optimize-dpkg-command-performance

Conversation

Copilot AI commented May 2, 2026

Copy link
Copy Markdown
Contributor

Software Version Updates

Four correctness and performance fixes to scripts/update/php-update.sh.

Changed Versions

scripts/update/php-update.sh

  • Cache dpkg -l: Capture output once into DPKG_LIST_OUTPUT before the version-detection loop; grep against the variable instead of forking a new dpkg -l subprocess per iteration.
  • Precise already-installed check: Replace dpkg -l | grep -q with dpkg-query -W -f='${Status}' ... | grep -q 'install ok installed' for an exact package-state match.
  • Shared sed templates: Extract SOCKET_EXPR and FASTCGI_EXPR as printf-style pattern templates used by both php-fpm.conf and per-site nginx config updates, eliminating regex drift between the two update paths.
  • Fix sed backreference bug: The admin control panel API update had \1 referencing the wrong capture group. Restructured from s|php${OLD_VER}(-fpm)?|php${NEW_PHP_VER}\1|g to s|(php)${OLD_VER}(-fpm)?|\1${NEW_PHP_VER}\2|g so \1=php and \2=(-fpm)?.

Version Diff

-OLD_PHP_VERS=()
+DPKG_LIST_OUTPUT="$(dpkg -l)"
+OLD_PHP_VERS=()
 for ver in "${SUPPORTED_PHP_VERSIONS[@]}"; do
-    if [[ "${ver}" != "${NEW_PHP_VER}" ]] && dpkg -l | grep -q "php${ver}-fpm"; then
+    if [[ "${ver}" != "${NEW_PHP_VER}" ]] && grep -q "php${ver}-fpm" <<< "${DPKG_LIST_OUTPUT}"; then

-    if dpkg -l | grep -q "php${NEW_PHP_VER}-fpm"; then
+    if dpkg-query -W -f='${Status}' "php${NEW_PHP_VER}-fpm" 2>/dev/null | grep -q 'install ok installed'; then

+SOCKET_EXPR='s|(unix:/run/php/)php%s-fpm(\.sock)|\1php%s-fpm\2|g'
+FASTCGI_EXPR='s|(fastcgi_pass[[:space:]]+[^;]*php)%s(-fpm)|\1%s\2|g'

-        sed -E -i "/(php-fpm|fastcgi_pass|sock(et)?|service)/ s|php${OLD_VER}(-fpm)?|php${NEW_PHP_VER}\1|g"
+        sed -E -i "/(php-fpm|fastcgi_pass|sock(et)?|service)/ s|(php)${OLD_VER}(-fpm)?|\1${NEW_PHP_VER}\2|g"

Verification Checklist

  • Version numbers are correctly formatted
  • Version numbers are stable releases (not beta/RC)
  • No syntax errors in variables file

Notes

This is an automated pull request created by the software version checker workflow.
Please verify these versions are stable releases before merging.

Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"The dpkg command is executed inside the loop for each supported PHP version, which is inefficient. Consider capturing dpkg output once before the loop and then grep against that captured output, or use `dpkg-query` for better performance.","fixFiles":[{"filePath":"scripts/update/php-update.sh","diff":"diff --git a/scripts/update/php-update.sh b/scripts/update/php-update.sh\n--- a/scripts/update/php-update.sh\n+++ b/scripts/update/php-update.sh\n@@ -35,16 +35,17 @@\n fi\n \n # Auto-detect currently installed PHP-FPM version\n+DPKG_LIST_OUTPUT=\"$(dpkg -l)\"\n OLD_PHP_VERS=()\n for ver in \"${SUPPORTED_PHP_VERSIONS[@]}\"; do\n-    if [[ \"${ver}\" != \"${NEW_PHP_VER}\" ]] && dpkg -l | grep -q \"php${ver}-fpm\"; then\n+    if [[ \"${ver}\" != \"${NEW_PHP_VER}\" ]] && grep -q \"php${ver}-fpm\" <<< \"${DPKG_LIST_OUTPUT}\"; then\n         OLD_PHP_VERS+=(\"${ver}\")\n     fi\n done\n \n if [[ ${#OLD_PHP_VERS[@]} -eq 0 ]]; then\n     # Check if target version is already installed\n-    if dpkg -l | grep -q \"php${NEW_PHP_VER}-fpm\"; then\n+    if grep -q \"php${NEW_PHP_VER}-fpm\" <<< \"${DPKG_LIST_OUTPUT}\"; then\n         echo \"PHP ${NEW_PHP_VER} is already installed. Nothing to upgrade.\"\n         exit 0\n     else\n"}]},{"message":"Similar to the previous issue, this dpkg call could be optimized. Consider using `dpkg-query -W -f='${Status}' \\\"php${NEW_PHP_VER}-fpm\\\" 2>/dev/null | grep -q 'install ok installed'` for more efficient package checking.","fixFiles":[{"filePath":"scripts/update/php-update.sh","diff":"diff --git a/scripts/update/php-update.sh b/scripts/update/php-update.sh\n--- a/scripts/update/php-update.sh\n+++ b/scripts/update/php-update.sh\n@@ -44,7 +44,7 @@\n \n if [[ ${#OLD_PHP_VERS[@]} -eq 0 ]]; then\n     # Check if target version is already installed\n-    if dpkg -l | grep -q \"php${NEW_PHP_VER}-fpm\"; then\n+    if dpkg-query -W -f='${Status}' \"php${NEW_PHP_VER}-fpm\" 2>/dev/null | grep -q 'install ok installed'; then\n         echo \"PHP ${NEW_PHP_VER} is already installed. Nothing to upgrade.\"\n         exit 0\n     else\n"}]},{"message":"The regex pattern for updating nginx site configurations differs from the one used for php-fpm.conf (lines 136-138). Consider extracting this into a shared function or variable to ensure consistency across all configuration updates and reduce the risk of regex pattern drift.","fixFiles":[{"filePath":"scripts/update/php-update.sh","diff":"diff --git a/scripts/update/php-update.sh b/scripts/update/php-update.sh\n--- a/scripts/update/php-update.sh\n+++ b/scripts/update/php-update.sh\n@@ -129,12 +129,16 @@\n # Update Nginx configuration to use new PHP version\n echo \"Updating Nginx configuration for PHP ${NEW_PHP_VER}...\"\n \n+# Shared sed expressions for PHP version migrations in nginx configs\n+SOCKET_EXPR='s|(unix:/run/php/)php%s-fpm(\\.sock)|\\1php%s-fpm\\2|g'\n+FASTCGI_EXPR='s|(fastcgi_pass[[:space:]]+[^;]*php)%s(-fpm)|\\1%s\\2|g'\n+\n # Update php-fpm.conf\n if [[ -f \"/etc/nginx/globals/php-fpm.conf\" ]]; then\n     for OLD_VER in \"${MIGRATION_SOURCE_PHP_VERS[@]}\"; do\n         sed -E -i \\\n-            -e \"s|(unix:/run/php/)php${OLD_VER}-fpm(\\.sock)|\\1php${NEW_PHP_VER}-fpm\\2|g\" \\\n-            -e \"s|(fastcgi_pass[[:space:]]+[^;]*php)${OLD_VER}(-fpm)|\\1${NEW_PHP_VER}\\2|g\" \\\n+            -e \"$(printf \"$SOCKET_EXPR\" \"$OLD_VER\" \"$NEW_PHP_VER\")\" \\\n+            -e \"$(printf \"$FASTCGI_EXPR\" \"$OLD_VER\" \"$NEW_PHP_VER\")\" \\\n             \"/etc/nginx/globals/php-fpm.conf\"\n     done\n fi\n@@ -143,7 +143,7 @@\n for config_file in /etc/nginx/sites-available/*; do\n     if [[ -f \"$config_file\" ]]; then\n         for OLD_VER in \"${MIGRATION_SOURCE_PHP_VERS[@]}\"; do\n-            sed -E -i \"/^[[:space:]]*fastcgi_pass[[:space:]]+/ s|php${OLD_VER}(-fpm)?|php${NEW_PHP_VER}\\1|g\" \"$config_file\"\n+            sed -E -i \"/^[[:space:]]*fastcgi_pass[[:space:]]+/ $(printf \"$FASTCGI_EXPR\" \"$OLD_VER\" \"$NEW_PHP_VER\")\" \"$config_file\"\n         done\n     fi\n done\n"}]},{"message":"The backreference `\\1` in the replacement pattern refers to the capturing group `(-fpm)?`, but the address pattern contains multiple groups. This will not correctly replace the matched text. The replacement should use `\\2` to reference the `(-fpm)?` group, or restructure the regex to ensure correct backreference.","fixFiles":[{"filePath":"scripts/update/php-update.sh","diff":"diff --git a/scripts/update/php-update.sh b/scripts/update/php-update.sh\n--- a/scripts/update/php-update.sh\n+++ b/scripts/update/php-update.sh\n@@ -160,7 +160,7 @@\n if [[ -f \"/var/www/admin/control-panel/api.php\" ]]; then\n     echo \"Updating admin control panel API configuration...\"\n     for OLD_VER in \"${MIGRATION_SOURCE_PHP_VERS[@]}\"; do\n-        sed -E -i \"/(php-fpm|fastcgi_pas...

Copilot AI changed the title [WIP] Optimize dpkg command execution for PHP version check fix: optimize dpkg calls and fix sed backreferences in php-update.sh May 2, 2026
Copilot finished work on behalf of PDowney May 2, 2026 06:21
Copilot AI requested a review from PDowney May 2, 2026 06:21
Removed outdated PHP update section from CHANGELOG.
@github-actions

github-actions Bot commented May 2, 2026

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@sonarqubecloud

sonarqubecloud Bot commented May 2, 2026

Copy link
Copy Markdown

@PDowney
PDowney marked this pull request as ready for review May 2, 2026 07:40
Copilot AI review requested due to automatic review settings May 2, 2026 07:40
@PDowney
PDowney merged commit 8fdd37a into master May 2, 2026
11 checks passed
@github-actions
github-actions Bot deleted the copilot/optimize-dpkg-command-performance branch May 2, 2026 07:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves scripts/update/php-update.sh by reducing repeated package-manager calls during PHP version detection and by consolidating sed patterns used to migrate Nginx/PHP-related configuration references when upgrading PHP-FPM.

Changes:

  • Cache dpkg -l output for old-version detection and use dpkg-query for an exact “already installed” check.
  • Introduce shared SOCKET_EXPR / FASTCGI_EXPR sed templates used across Nginx config updates.
  • Fix sed backreference grouping for the admin control panel API update rule.

Comment on lines +135 to +136
SOCKET_EXPR='s|(unix:/run/php/)php%s-fpm(\.sock)|\1php%s-fpm\2|g'
FASTCGI_EXPR='s|(fastcgi_pass[[:space:]]+[^;]*php)%s(-fpm)|\1%s\2|g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants