From 7d0f47038986077c031db97332b83d2920e767d6 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Wed, 5 Apr 2017 11:23:04 +0100
Subject: [PATCH] Consolidate gnusocial post expiry script

---
 src/freedombone-app-gnusocial        | 64 +++-------------------------
 src/freedombone-app-postactiv        | 62 +++------------------------
 src/freedombone-utils-gnusocialtools | 63 +++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 115 deletions(-)

diff --git a/src/freedombone-app-gnusocial b/src/freedombone-app-gnusocial
index d2f866a60..27b87ca1d 100755
--- a/src/freedombone-app-gnusocial
+++ b/src/freedombone-app-gnusocial
@@ -13,7 +13,7 @@
 # License
 # =======
 #
-# Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
+# Copyright (C) 2014-2017 Bob Mottram <bob@freedombone.net>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
@@ -54,11 +54,11 @@ SHARINGS_THEME_COMMIT='7106c7ef03'
 GNUSOCIAL_NSFW_REPO="https://gitgud.io/ShitposterClub/SensitiveContent"
 GNUSOCIAL_NSFW_COMMIT='a096bbe0cfae9a9b177682920ffb58d32a48e136'
 
-# script which causes old posts to expire
-gnusocial_expire_script=/usr/bin/gnusocial-expire
-
 GNUSOCIAL_TITLE='Pleroma FE'
 
+# Number of months after which posts expire
+GNUSOCIAL_EXPIRE_MONTHS=3
+
 gnusocial_variables=(ONION_ONLY
                      GNUSOCIAL_DOMAIN_NAME
                      GNUSOCIAL_CODE
@@ -72,6 +72,7 @@ gnusocial_variables=(ONION_ONLY
                      SHARINGS_THEME_REPO
                      SHARINGS_THEME_COMMIT
                      GNUSOCIAL_TITLE
+                     GNUSOCIAL_EXPIRE_MONTHS
                      MY_USERNAME)
 
 function gnusocial_fix_endless_reloads {
@@ -843,59 +844,6 @@ function install_gnusocial_plugin_sharings_theme {
     install_completed gnusocial_plugin_sharings_theme
 }
 
-function expire_gnusocial_posts {
-    # To prevent the database size from growing endlessly this script expires posts
-    # after a number of months
-    if [ ! -d /var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs ]; then
-        return
-    fi
-
-    echo '<?php' > $gnusocial_expire_script
-    echo '' >> $gnusocial_expire_script
-    echo '// gnusocial post expiry script, based on StatExpire by Tony Baldwin' >> $gnusocial_expire_script
-    echo '// https://github.com/tonybaldwin/statexpire' >> $gnusocial_expire_script
-    echo '' >> $gnusocial_expire_script
-    echo '$oldate=date(("Y-m-d"), strtotime("-3 months"));' >> $gnusocial_expire_script
-    echo '$username="root";' >> $gnusocial_expire_script
-    echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $gnusocial_expire_script
-    echo '$database="gnusocial";' >> $gnusocial_expire_script
-    echo '' >> $gnusocial_expire_script
-    echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $gnusocial_expire_script
-    echo '    echo "Could not connect to mariadb";' >> $gnusocial_expire_script
-    echo '    exit;' >> $gnusocial_expire_script
-    echo '}' >> $gnusocial_expire_script
-    echo '' >> $gnusocial_expire_script
-    echo 'if (!mysql_select_db($database, $link)) {' >> $gnusocial_expire_script
-    echo '    echo "Could not select gnusocial database";' >> $gnusocial_expire_script
-    echo '    exit;' >> $gnusocial_expire_script
-    echo '}' >> $gnusocial_expire_script
-    echo '' >> $gnusocial_expire_script
-    echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
-    echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $gnusocial_expire_script
-    echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
-    echo '' >> $gnusocial_expire_script
-    echo 'mysql_query($notice_query);' >> $gnusocial_expire_script
-    echo '$rowaff1=mysql_affected_rows();' >> $gnusocial_expire_script
-    echo 'mysql_query($conversation_query);' >> $gnusocial_expire_script
-    echo '$rowaff2=mysql_affected_rows();' >> $gnusocial_expire_script
-    echo 'mysql_query($reply_query);' >> $gnusocial_expire_script
-    echo '$rowaff3=mysql_affected_rows();' >> $gnusocial_expire_script
-    echo 'mysql_close();' >> $gnusocial_expire_script
-    echo '' >> $gnusocial_expire_script
-    echo 'echo "Expire gnusocial posts: $rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $gnusocial_expire_script
-    chmod +x $gnusocial_expire_script
-
-    # Add a cron job
-    if ! grep -q "$gnusocial_expire_script" /etc/crontab; then
-        echo "10 3 5   *   *   root /usr/bin/timeout 500 /usr/bin/php $gnusocial_expire_script" >> /etc/crontab
-    fi
-
-    # remove old expire script
-    if [ -f /etc/cron.weekly/clear-gnusocial-database ]; then
-        rm /etc/cron.weekly/clear-gnusocial-database
-    fi
-}
-
 function install_gnusocial_markdown {
     GNUSOCIAL_PATH=/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs
 
@@ -983,7 +931,7 @@ function install_gnusocial {
     fi
 
     install_gnusocial_main
-    expire_gnusocial_posts
+    expire_gnusocial_posts "$GNUSOCIAL_DOMAIN_NAME" "gnusocial" "$GNUSOCIAL_EXPIRE_MONTHS"
     install_qvitter "$GNUSOCIAL_DOMAIN_NAME" "gnusocial"
     install_gnusocial_markdown
     #install_gnusocial_plugin_sharings
diff --git a/src/freedombone-app-postactiv b/src/freedombone-app-postactiv
index c2b3cf1e7..447bbc6da 100755
--- a/src/freedombone-app-postactiv
+++ b/src/freedombone-app-postactiv
@@ -13,7 +13,7 @@
 # License
 # =======
 #
-# Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
+# Copyright (C) 2016-2017 Bob Mottram <bob@freedombone.net>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
@@ -43,8 +43,8 @@ POSTACTIV_ADMIN_PASSWORD=
 POSTACTIV_BACKGROUND_IMAGE_URL=
 POSTACTIV_TITLE='PostActiv'
 
-# script which causes old posts to expire
-postactiv_expire_script=/usr/bin/postactiv-expire
+# Number of months after which posts expire
+POSTACTIV_EXPIRE_MONTHS=3
 
 postactiv_variables=(ONION_ONLY
                      POSTACTIV_DOMAIN_NAME
@@ -52,6 +52,7 @@ postactiv_variables=(ONION_ONLY
                      DDNS_PROVIDER
                      MY_USERNAME
                      POSTACTIV_BACKGROUND_IMAGE_URL
+                     POSTACTIV_EXPIRE_MONTHS
                      POSTACTIV_TITLE)
 
 function remove_user_postactiv {
@@ -710,59 +711,6 @@ function install_postactiv_main {
     install_completed postactiv_main
 }
 
-function expire_postactiv_posts {
-    # To prevent the database size from growing endlessly this script expires posts
-    # after a number of months
-    if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME/htdocs ]; then
-        return
-    fi
-
-    echo '<?php' > $postactiv_expire_script
-    echo '' >> $postactiv_expire_script
-    echo '// postactiv post expiry script, based on StatExpire by Tony Baldwin' >> $postactiv_expire_script
-    echo '// https://github.com/tonybaldwin/statexpire' >> $postactiv_expire_script
-    echo '' >> $postactiv_expire_script
-    echo '$oldate=date(("Y-m-d"), strtotime("-3 months"));' >> $postactiv_expire_script
-    echo '$username="root";' >> $postactiv_expire_script
-    echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $postactiv_expire_script
-    echo '$database="postactiv";' >> $postactiv_expire_script
-    echo '' >> $postactiv_expire_script
-    echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $postactiv_expire_script
-    echo '    echo "Could not connect to mariadb";' >> $postactiv_expire_script
-    echo '    exit;' >> $postactiv_expire_script
-    echo '}' >> $postactiv_expire_script
-    echo '' >> $postactiv_expire_script
-    echo 'if (!mysql_select_db($database, $link)) {' >> $postactiv_expire_script
-    echo '    echo "Could not select postactiv database";' >> $postactiv_expire_script
-    echo '    exit;' >> $postactiv_expire_script
-    echo '}' >> $postactiv_expire_script
-    echo '' >> $postactiv_expire_script
-    echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $postactiv_expire_script
-    echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $postactiv_expire_script
-    echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $postactiv_expire_script
-    echo '' >> $postactiv_expire_script
-    echo 'mysql_query($notice_query);' >> $postactiv_expire_script
-    echo '$rowaff1=mysql_affected_rows();' >> $postactiv_expire_script
-    echo 'mysql_query($conversation_query);' >> $postactiv_expire_script
-    echo '$rowaff2=mysql_affected_rows();' >> $postactiv_expire_script
-    echo 'mysql_query($reply_query);' >> $postactiv_expire_script
-    echo '$rowaff3=mysql_affected_rows();' >> $postactiv_expire_script
-    echo 'mysql_close();' >> $postactiv_expire_script
-    echo '' >> $postactiv_expire_script
-    echo 'echo "Expire postactiv posts: $rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $postactiv_expire_script
-    chmod +x $postactiv_expire_script
-
-    # Add a cron job
-    if ! grep -q "$postactiv_expire_script" /etc/crontab; then
-        echo "10 3 5   *   *   root /usr/bin/timeout 500 /usr/bin/php $postactiv_expire_script" >> /etc/crontab
-    fi
-
-    # remove old expire script
-    if [ -f /etc/cron.weekly/clear-postactiv-database ]; then
-        rm /etc/cron.weekly/clear-postactiv-database
-    fi
-}
-
 function install_postactiv {
     if [ ! $ONION_ONLY ]; then
         ONION_ONLY='no'
@@ -785,7 +733,7 @@ function install_postactiv {
         install_pleroma "postactiv" "$POSTACTIV_DOMAIN_NAME" "$POSTACTIV_BACKGROUND_IMAGE_URL" "$POSTACTIV_TITLE"
     fi
 
-    expire_postactiv_posts
+    expire_gnusocial_posts "$POSTACTIV_DOMAIN_NAME" "postactiv" "$POSTACTIV_EXPIRE_MONTHS"
 
     # unleash the daemons!
     /etc/cron.hourly/postactiv-daemons
diff --git a/src/freedombone-utils-gnusocialtools b/src/freedombone-utils-gnusocialtools
index 23dee1403..5ed37becf 100755
--- a/src/freedombone-utils-gnusocialtools
+++ b/src/freedombone-utils-gnusocialtools
@@ -273,5 +273,68 @@ function gnusocial_hourly_script {
     chmod +x /etc/cron.hourly/${gnusocial_type}-daemons
 }
 
+function expire_gnusocial_posts {
+    domain_name=$1
+    gnusocial_type=$2
+    expire_months=$3
+
+    if [ ! $expire_months ]; then
+        expire_months=3
+    fi
+
+    # To prevent the database size from growing endlessly this script expires posts
+    # after a number of months
+    if [ ! -d /var/www/${domain_name}/htdocs ]; then
+        return
+    fi
+
+    gnusocial_expire_script=/usr/bin/${gnusocial_type}-expire
+
+    echo '<?php' > $gnusocial_expire_script
+    echo '' >> $gnusocial_expire_script
+    echo "// ${gnusocial_type} post expiry script, based on StatExpire by Tony Baldwin" >> $gnusocial_expire_script
+    echo '// https://github.com/tonybaldwin/statexpire' >> $gnusocial_expire_script
+    echo '' >> $gnusocial_expire_script
+    echo "\$oldate=date((\"Y-m-d\"), strtotime(\"-${expire_months} months\"));" >> $gnusocial_expire_script
+    echo '$username="root";' >> $gnusocial_expire_script
+    echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $gnusocial_expire_script
+    echo "\$database=\"${gnusocial_type}\";" >> $gnusocial_expire_script
+    echo '' >> $gnusocial_expire_script
+    echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $gnusocial_expire_script
+    echo '    echo "Could not connect to mariadb";' >> $gnusocial_expire_script
+    echo '    exit;' >> $gnusocial_expire_script
+    echo '}' >> $gnusocial_expire_script
+    echo '' >> $gnusocial_expire_script
+    echo 'if (!mysql_select_db($database, $link)) {' >> $gnusocial_expire_script
+    echo "    echo \"Could not select ${gnusocial_type} database\";" >> $gnusocial_expire_script
+    echo '    exit;' >> $gnusocial_expire_script
+    echo '}' >> $gnusocial_expire_script
+    echo '' >> $gnusocial_expire_script
+    echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
+    echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $gnusocial_expire_script
+    echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
+    echo '' >> $gnusocial_expire_script
+    echo 'mysql_query($notice_query);' >> $gnusocial_expire_script
+    echo '$rowaff1=mysql_affected_rows();' >> $gnusocial_expire_script
+    echo 'mysql_query($conversation_query);' >> $gnusocial_expire_script
+    echo '$rowaff2=mysql_affected_rows();' >> $gnusocial_expire_script
+    echo 'mysql_query($reply_query);' >> $gnusocial_expire_script
+    echo '$rowaff3=mysql_affected_rows();' >> $gnusocial_expire_script
+    echo 'mysql_close();' >> $gnusocial_expire_script
+    echo '' >> $gnusocial_expire_script
+    echo -n "echo \"Expire ${gnusocial_type} posts: " >> $gnusocial_expire_script
+    echo '$rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $gnusocial_expire_script
+    chmod +x $gnusocial_expire_script
+
+    # Add a cron job
+    if ! grep -q "${gnusocial_type}_expire_script" /etc/crontab; then
+        echo "10 3 5   *   *   root /usr/bin/timeout 500 /usr/bin/php ${gnusocial_type}_expire_script" >> /etc/crontab
+    fi
+
+    # remove old expire script
+    if [ -f /etc/cron.weekly/clear-${gnusocial_type}-database ]; then
+        rm /etc/cron.weekly/clear-${gnusocial_type}-database
+    fi
+}
 
 # NOTE: deliberately there is no "exit 0"
-- 
GitLab