From 3e722b9a93df572d71bf6cc6bec891bf8a40c3af Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sun, 1 May 2022 11:01:18 +0100
Subject: [PATCH] Refactor fix-https command

Previously "https-siteurl", a somewhat opaque name.
---
 bin/noblogs.in | 55 ++++++++++++++++++++++----------------------------
 1 file changed, 24 insertions(+), 31 deletions(-)

diff --git a/bin/noblogs.in b/bin/noblogs.in
index a01b43f..c2abb3b 100755
--- a/bin/noblogs.in
+++ b/bin/noblogs.in
@@ -58,7 +58,7 @@ Known commands:
 	-mail	sends email to ai-changes@investici.org
 	-json	print JSON output
 
-   https-siteurl BLOG
+   fix-https BLOG
       Adds https (instead of http) to siteurl and home options.
 
    fix-cdn BLOG
@@ -386,39 +386,32 @@ function do_check_updates($args) {
 
 }
 
-// replaces http with https, and removes ending slash
-function add_https_to_uri($uri){
-    $re = "#^http[s]?://([^.]+\.noblogs\.org)[/]?$#i";
-    $subst = "https://$1";
-    return preg_replace($re,$subst,$uri);
-}
-
 // adds https to siteurl and home options (and also flattrs custom image)
-function do_https_siteurl($args){
-    $field_to_update = array('home', 'siteurl', 'flattrss_custom_image_url');
-    foreach ($args as $arg) {
-        $blog = noblogs_get_blog($arg);
-        if (!$blog) {
-            echo "Blog {$arg} not found.\n";
-            continue;
-        }
-        switch_to_blog($blog->blog_id);
-
-        foreach ($field_to_update as $field){
-            $old = get_option($field);
-            if ( $old === false ){
-                continue;
-            }
-            $new = add_https_to_uri($old);
-            if ($old === $new){
-                continue;
-            }
-            update_option($field,$new);
-        }
+function do_fix_https($args){
+  $fields_to_update = array('home', 'siteurl', 'flattrss_custom_image_url');
+  foreach ($args as $arg) {
+    $blog = noblogs_get_blog($arg);
+    if (!$blog) {
+      echo "Blog {$arg} not found.\n";
+      continue;
+    }
+    switch_to_blog($blog->blog_id);
 
-        echo "{$arg}\n";
-        restore_current_blog();
+    foreach ($fields_to_update as $field) {
+      $old_value = get_option($field);
+      if ($old_value === false) {
+        continue;
+      }
+      $new_value = str_replace("http://", "https://", $old_value);
+      if ($old_value === $new_value){
+        continue;
+      }
+      update_option($field, $new_value);
     }
+
+    echo "{$arg}\n";
+    restore_current_blog();
+  }
 }
 
 // rewrites the correct settings for the CDN
-- 
GitLab