]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/fixup_utf8.php
replace 'public' in documentation with 'top'
[quix0rs-gnu-social.git] / scripts / fixup_utf8.php
old mode 100644 (file)
new mode 100755 (executable)
index 2046d2b..796021b
@@ -1,8 +1,8 @@
 #!/usr/bin/env php
 <?php
 /*
- * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2009, Control Yourself, Inc.
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
  *
  * 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
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-# Abort if called from a web server
-if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
-    print "This script must be run from the command line\n";
-    exit(1);
-}
-
-ini_set("max_execution_time", "0");
-ini_set("max_input_time", "0");
-set_time_limit(0);
-mb_internal_encoding('UTF-8');
+// Abort if called from a web server
 
 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
-define('LACONICA', true);
 
-require_once(INSTALLDIR . '/lib/common.php');
-require_once('DB.php');
+$helptext = <<<ENDOFHELP
+fixup_utf8.php <maxdate> <maxid> <minid>
+
+Fixup records in a database that stored the data incorrectly (pre-0.7.4 for StatusNet).
+
+ENDOFHELP;
+
+require_once INSTALLDIR.'/scripts/commandline.inc';
+require_once 'DB.php';
 
 class UTF8FixerUpper
 {
@@ -45,7 +42,7 @@ class UTF8FixerUpper
     {
         $this->args = $args;
 
-        if (array_key_exists('max_date', $args)) {
+        if (!empty($args['max_date'])) {
             $this->max_date = strftime('%Y-%m-%d %H:%M:%S', strtotime($args['max_date']));
         } else {
             $this->max_date = strftime('%Y-%m-%d %H:%M:%S', time());
@@ -101,6 +98,7 @@ class UTF8FixerUpper
                             $this->args['min_notice']);
         $this->fixupProfiles();
         $this->fixupGroups();
+        $this->fixupMessages();
     }
 
     function fixupNotices($max_id, $min_id) {
@@ -147,7 +145,7 @@ class UTF8FixerUpper
 
             echo "$id...";
 
-            $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
+            $result = $this->dbu->execute($sth, array($content, $rendered, $id));
 
             if (PEAR::isError($result)) {
                 echo "ERROR: " . $result->getMessage() . "\n";
@@ -163,6 +161,7 @@ class UTF8FixerUpper
 
             $notice = Notice::staticGet('id', $id);
             $notice->decache();
+            $notice->free();
 
             echo "OK\n";
         }
@@ -210,7 +209,7 @@ class UTF8FixerUpper
 
             echo "$id...";
 
-            $result =& $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
+            $result = $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
 
             if (PEAR::isError($result)) {
                 echo "ERROR: " . $result->getMessage() . "\n";
@@ -226,6 +225,7 @@ class UTF8FixerUpper
 
             $profile = Profile::staticGet('id', $id);
             $profile->decache();
+            $profile->free();
 
             echo "OK\n";
         }
@@ -249,7 +249,7 @@ class UTF8FixerUpper
         $sql = 'SELECT id, fullname, location, description FROM user_group ' .
           'WHERE LENGTH(fullname) != CHAR_LENGTH(fullname) '.
           'OR LENGTH(location) != CHAR_LENGTH(location) '.
-          'OR LENGTH(description) != CHAR_LENGTH(description) ';
+          'OR LENGTH(description) != CHAR_LENGTH(description) '.
           'AND modified < "'.$this->max_date.'" '.
           'ORDER BY modified DESC';
 
@@ -273,7 +273,7 @@ class UTF8FixerUpper
 
             echo "$id...";
 
-            $result =& $this->dbu->execute($sth, array($fullname, $location, $description, $id));
+            $result = $this->dbu->execute($sth, array($fullname, $location, $description, $id));
 
             if (PEAR::isError($result)) {
                 echo "ERROR: " . $result->getMessage() . "\n";
@@ -289,15 +289,73 @@ class UTF8FixerUpper
 
             $user_group = User_group::staticGet('id', $id);
             $user_group->decache();
+            $user_group->free();
+
+            echo "OK\n";
+        }
+    }
+
+    function fixupMessages() {
+
+        // Do a separate DB connection
+
+        $sth = $this->dbu->prepare("UPDATE message SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
+
+        if (PEAR::isError($sth)) {
+            echo "ERROR: " . $sth->getMessage() . "\n";
+            return;
+        }
+
+        $sql = 'SELECT id, content, rendered FROM message ' .
+          'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
+          'AND modified < "'.$this->max_date.'" '.
+          'ORDER BY id DESC';
+
+        $rn = $this->dbl->query($sql);
+
+        if (PEAR::isError($rn)) {
+            echo "ERROR: " . $rn->getMessage() . "\n";
+            return;
+        }
+
+        echo "Number of rows: " . $rn->numRows() . "\n";
+
+        $message = array();
+
+        while (DB_OK == $rn->fetchInto($message)) {
+
+            $id = ($message[0])+0;
+            $content = bin2hex($message[1]);
+            $rendered = bin2hex($message[2]);
+
+            echo "$id...";
+
+            $result = $this->dbu->execute($sth, array($content, $rendered, $id));
+
+            if (PEAR::isError($result)) {
+                echo "ERROR: " . $result->getMessage() . "\n";
+                continue;
+            }
+
+            $cnt = $this->dbu->affectedRows();
+
+            if ($cnt != 1) {
+                echo "ERROR: 0 rows affected\n";
+                continue;
+            }
+
+            $message = Message::staticGet('id', $id);
+            $message->decache();
+            $message->free();
 
             echo "OK\n";
         }
     }
 }
 
-$max_date = ($argc > 1) ? $argv[1] : null;
-$max_id = ($argc > 2) ? $argv[2] : null;
-$min_id = ($argc > 3) ? $argv[3] : null;
+$max_date = (count($args) > 0) ? $args[0] : null;
+$max_id = (count($args) > 1) ? $args[1] : null;
+$min_id = (count($args) > 2) ? $args[2] : null;
 
 $fixer = new UTF8FixerUpper(array('max_date' => $max_date,
                                   'max_notice' => $max_id,