From: Brion Vibber <brion@pobox.com>
Date: Tue, 29 Dec 2009 22:05:43 +0000 (-0800)
Subject: Add progress output and optional --sleep-time parameter to triminboxes.php
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e3850e5273904a222580ff8daa3e778518721161;p=quix0rs-gnu-social.git

Add progress output and optional --sleep-time parameter to triminboxes.php
---

diff --git a/classes/Notice_inbox.php b/classes/Notice_inbox.php
index b39006542c..d3ddad656a 100644
--- a/classes/Notice_inbox.php
+++ b/classes/Notice_inbox.php
@@ -106,6 +106,13 @@ class Notice_inbox extends Memcached_DataObject
         return Memcached_DataObject::pkeyGet('Notice_inbox', $kv);
     }
 
+    /**
+     * Trim inbox for a given user to latest NOTICE_INBOX_LIMIT items
+     * (up to NOTICE_INBOX_GC_MAX will be deleted).
+     *
+     * @param int $user_id
+     * @return int count of notices dropped from the inbox, if any
+     */
     static function gc($user_id)
     {
         $entry = new Notice_inbox();
@@ -133,6 +140,8 @@ class Notice_inbox extends Memcached_DataObject
                 $notices = array();
             }
         }
+
+        return $total;
     }
 
     static function deleteMatching($user_id, $notices)
diff --git a/scripts/triminboxes.php b/scripts/triminboxes.php
index da09817e5b..ea47513051 100644
--- a/scripts/triminboxes.php
+++ b/scripts/triminboxes.php
@@ -21,19 +21,21 @@
 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
 
 $shortoptions = 'u::';
-$longoptions = array('start-user-id::');
+$longoptions = array('start-user-id=', 'sleep-time=');
 
 $helptext = <<<END_OF_TRIM_HELP
 Batch script for trimming notice inboxes to a reasonable size.
 
     -u <id>
     --start-user-id=<id>   User ID to start after. Default is all.
+    --sleep-time=<integer> Amount of time to wait (in seconds) between trims. Default is zero.
 
 END_OF_TRIM_HELP;
 
 require_once INSTALLDIR.'/scripts/commandline.inc';
 
 $id = null;
+$sleep_time = 0;
 
 if (have_option('u')) {
     $id = get_option_value('u');
@@ -43,6 +45,12 @@ if (have_option('u')) {
     $id = null;
 }
 
+if (have_option('--sleep-time')) {
+    $sleep_time = intval(get_option_value('--sleep-time'));
+}
+
+$quiet = have_option('q') || have_option('--quiet');
+
 $user = new User();
 
 if (!empty($id)) {
@@ -52,5 +60,17 @@ if (!empty($id)) {
 $cnt = $user->find();
 
 while ($user->fetch()) {
-    Notice_inbox::gc($user->id);
+    if (!$quiet) {
+        print "Trimming inbox for user $user->id";
+    }
+    $count = Notice_inbox::gc($user->id);
+    if ($count) {
+        if (!$quiet) {
+            print ": $count trimmed...";
+        }
+        sleep($sleep_time);
+    }
+    if (!$quiet) {
+        print "\n";
+    }
 }