]> git.mxchange.org Git - friendica.git/commitdiff
dbclean is now using the new database functions / new functions for commit, rollback...
authorMichael <heluecht@pirati.ca>
Thu, 11 May 2017 20:13:45 +0000 (20:13 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 11 May 2017 20:13:45 +0000 (20:13 +0000)
include/dba.php
include/dbclean.php
include/items.php
include/poller.php
mod/item.php
src/App.php

index d0ebda1916b8a84300dfee87e9ebe9e03243b640..39eb1f460744ee3408ce2d303dc3e64215a9d9aa 100644 (file)
@@ -22,6 +22,7 @@ class dba {
        public  $connected = false;
        public  $error = false;
        private $_server_info = '';
+       private $in_transaction = false;
        private static $dbo;
        private static $relation = array();
 
@@ -602,7 +603,8 @@ class dba {
                }
 
                if (self::$dbo->errorno != 0) {
-                       logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".self::replace_parameters($sql, $args));
+                       logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
+                               $a->callstack(8))."\n".self::replace_parameters($sql, $args);
                }
 
                $a->save_timestamp($stamp1, 'database');
@@ -779,6 +781,48 @@ class dba {
                return self::e($sql, $param);
        }
 
+       /**
+        * @brief Starts a transaction
+        *
+        * @return boolean Was the command executed successfully?
+        */
+       static public function transaction() {
+               if (!self::e('COMMIT')) {
+                       return false;
+               }
+               if (!self::e('START TRANSACTION')) {
+                       return false;
+               }
+               self::$in_transaction = true;
+               return true;
+       }
+
+       /**
+        * @brief Does a commit
+        *
+        * @return boolean Was the command executed successfully?
+        */
+       static public function commit() {
+               if (!self::e('COMMIT')) {
+                       return false;
+               }
+               self::$in_transaction = false;
+               return true;
+       }
+
+       /**
+        * @brief Does a rollback
+        *
+        * @return boolean Was the command executed successfully?
+        */
+       static public function rollback() {
+               if (!self::e('ROLLBACK')) {
+                       return false;
+               }
+               self::$in_transaction = false;
+               return true;
+       }
+
        /**
         * @brief Build the array with the table relations
         *
@@ -805,12 +849,12 @@ class dba {
         *
         * @param string $table Table name
         * @param array $param parameter array
-        * @param boolean $in_commit Internal use: Only do a commit after the last delete
+        * @param boolean $in_process Internal use: Only do a commit after the last delete
         * @param array $callstack Internal use: prevent endless loops
         *
-        * @return boolean|array was the delete successfull? When $in_commit is set: deletion data
+        * @return boolean|array was the delete successfull? When $in_process is set: deletion data
         */
-       static public function delete($table, $param, $in_commit = false, &$callstack = array()) {
+       static public function delete($table, $param, $in_process = false, &$callstack = array()) {
 
                $commands = array();
 
@@ -872,10 +916,11 @@ class dba {
                        }
                }
 
-               if (!$in_commit) {
+               if (!$in_process) {
                        // Now we finalize the process
-                       self::p("COMMIT");
-                       self::p("START TRANSACTION");
+                       if (!self::$in_transaction) {
+                               self::transaction();
+                       }
 
                        $compacted = array();
                        $counter = array();
@@ -887,7 +932,9 @@ class dba {
                                        logger(dba::replace_parameters($sql, $command['param']), LOGGER_DATA);
 
                                        if (!self::e($sql, $command['param'])) {
-                                               self::p("ROLLBACK");
+                                               if (!self::$in_transaction) {
+                                                       self::rollback();
+                                               }
                                                return false;
                                        }
                                } else {
@@ -915,13 +962,17 @@ class dba {
                                                logger(dba::replace_parameters($sql, $field_values), LOGGER_DATA);
 
                                                if (!self::e($sql, $field_values)) {
-                                                       self::p("ROLLBACK");
+                                                       if (!self::$in_transaction) {
+                                                               self::rollback();
+                                                       }
                                                        return false;
                                                }
                                        }
                                }
                        }
-                       self::p("COMMIT");
+                       if (!self::$in_transaction) {
+                               self::commit();
+                       }
                        return true;
                }
 
index f31bfef8abbb91ff0136d947ae58b8e2ac7085d5..29842834cb8e210f9cd5529ed619dd899f530353 100644 (file)
@@ -50,11 +50,13 @@ function remove_orphans($stage = 0) {
                if ($count > 0) {
                        logger("found global item orphans: ".$count);
                        while ($orphan = dba::fetch($r)) {
-                               q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
+                               dba::delete('item', array('id' => $orphan["id"]));
                        }
+               } else {
+                       logger("No global item orphans found");
                }
                dba::close($r);
-               logger("Done deleting old global item entries from item table without user copy");
+               logger("Done deleting ".$count." old global item entries from item table without user copy");
        }
 
        if (($stage == 2) OR ($stage == 0)) {
@@ -64,11 +66,13 @@ function remove_orphans($stage = 0) {
                if ($count > 0) {
                        logger("found item orphans without parents: ".$count);
                        while ($orphan = dba::fetch($r)) {
-                               q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
+                               dba::delete('item', array('id' => $orphan["id"]));
                        }
+               } else {
+                       logger("No item orphans without parents found");
                }
                dba::close($r);
-               logger("Done deleting items without parents");
+               logger("Done deleting ".$count." items without parents");
        }
 
        if (($stage == 3) OR ($stage == 0)) {
@@ -78,11 +82,14 @@ function remove_orphans($stage = 0) {
                if ($count > 0) {
                        logger("found thread orphans: ".$count);
                        while ($orphan = dba::fetch($r)) {
-                               q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
+                               dba::delete('thread', array('iid' => $orphan["iid"]));
                        }
+               } else {
+                       logger("No thread orphans found");
                }
+
                dba::close($r);
-               logger("Done deleting orphaned data from thread table");
+               logger("Done deleting ".$count." orphaned data from thread table");
        }
 
        if (($stage == 4) OR ($stage == 0)) {
@@ -92,11 +99,13 @@ function remove_orphans($stage = 0) {
                if ($count > 0) {
                        logger("found notify orphans: ".$count);
                        while ($orphan = dba::fetch($r)) {
-                               q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
+                               dba::delete('notify', array('iid' => $orphan["iid"]));
                        }
+               } else {
+                       logger("No notify orphans found");
                }
                dba::close($r);
-               logger("Done deleting orphaned data from notify table");
+               logger("Done deleting ".$count." orphaned data from notify table");
        }
 
        if (($stage == 5) OR ($stage == 0)) {
@@ -106,11 +115,13 @@ function remove_orphans($stage = 0) {
                if ($count > 0) {
                        logger("found notify-threads orphans: ".$count);
                        while ($orphan = dba::fetch($r)) {
-                               q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
+                               dba::delete('notify-threads', array('id' => $orphan["id"]));
                        }
+               } else {
+                       logger("No notify-threads orphans found");
                }
                dba::close($r);
-               logger("Done deleting orphaned data from notify-threads table");
+               logger("Done deleting ".$count." orphaned data from notify-threads table");
        }
 
 
@@ -121,11 +132,13 @@ function remove_orphans($stage = 0) {
                if ($count > 0) {
                        logger("found sign orphans: ".$count);
                        while ($orphan = dba::fetch($r)) {
-                               q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
+                               dba::delete('sign', array('iid' => $orphan["iid"]));
                        }
+               } else {
+                       logger("No sign orphans found");
                }
                dba::close($r);
-               logger("Done deleting orphaned data from sign table");
+               logger("Done deleting ".$count." orphaned data from sign table");
        }
 
 
@@ -136,11 +149,13 @@ function remove_orphans($stage = 0) {
                if ($count > 0) {
                        logger("found term orphans: ".$count);
                        while ($orphan = dba::fetch($r)) {
-                               q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
+                               dba::delete('term', array('oid' => $orphan["oid"]));
                        }
+               } else {
+                       logger("No term orphans found");
                }
                dba::close($r);
-               logger("Done deleting orphaned data from term table");
+               logger("Done deleting ".$count." orphaned data from term table");
        }
 
        // Call it again if not all entries were purged
index 3c5e88a38ac8cff30a7ad8e85de6b4990515b322..3b790b6a121684ee9b6abc5eeb8ec412ab4f8f8b 100644 (file)
@@ -951,8 +951,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
 
        logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
 
-       q("COMMIT");
-       q("START TRANSACTION;");
+       dba::transaction();
 
        $r = dbq("INSERT INTO `item` (`"
                        . implode("`, `", array_keys($arr))
@@ -974,7 +973,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
                }
        } else {
                // This can happen - for example - if there are locking timeouts.
-               q("ROLLBACK");
+               dba::rollback();
 
                // Store the data into a spool file so that we can try again later.
 
@@ -999,7 +998,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
        if ($current_post == 0) {
                // This is one of these error messages that never should occur.
                logger("couldn't find created item - we better quit now.");
-               q("ROLLBACK");
+               dba::rollback();
                return 0;
        }
 
@@ -1014,7 +1013,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
        if (!dbm::is_result($r)) {
                // This shouldn't happen, since COUNT always works when the database connection is there.
                logger("We couldn't count the stored entries. Very strange ...");
-               q("ROLLBACK");
+               dba::rollback();
                return 0;
        }
 
@@ -1024,12 +1023,12 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
 
                // Yes, we could do a rollback here - but we are having many users with MyISAM.
                q("DELETE FROM `item` WHERE `id` = %d", intval($current_post));
-               q("COMMIT");
+               dba::commit();
                return 0;
        } elseif ($r[0]["entries"] == 0) {
                // This really should never happen since we quit earlier if there were problems.
                logger("Something is terribly wrong. We haven't found our created entry.");
-               q("ROLLBACK");
+               dba::rollback();
                return 0;
        }
 
@@ -1109,7 +1108,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
                update_thread($parent_id);
        }
 
-       q("COMMIT");
+       dba::commit();
 
        /*
         * Due to deadlock issues with the "term" table we are doing these steps after the commit.
index 08e71fd486b69c23b68b52ee3e560b715e25c2e8..eb97d48531247fc630d41fc71f6f3aba48e2fdd6 100644 (file)
@@ -128,7 +128,7 @@ function poller_execute($queue) {
 
        if (!$upd) {
                logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);
-               q("COMMIT");
+               dba::commit();
                return true;
        }
 
@@ -136,18 +136,18 @@ function poller_execute($queue) {
        $id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
        if (!$id) {
                logger("Queue item ".$queue["id"]." vanished - skip this execution", LOGGER_DEBUG);
-               q("COMMIT");
+               dba::commit();
                return true;
        } elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
                logger("Entry for queue item ".$queue["id"]." wasn't stored - skip this execution", LOGGER_DEBUG);
-               q("COMMIT");
+               dba::commit();
                return true;
        } elseif ($id[0]["pid"] != $mypid) {
                logger("Queue item ".$queue["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
-               q("COMMIT");
+               dba::commit();
                return true;
        }
-       q("COMMIT");
+       dba::commit();
 
        $argv = json_decode($queue["parameter"]);
 
@@ -558,7 +558,7 @@ function poller_passing_slow(&$highest_priority) {
  */
 function poller_worker_process() {
 
-       q("START TRANSACTION;");
+       dba::transaction();
 
        // Check if we should pass some low priority process
        $highest_priority = 0;
index f2d7babcde243ae0bcbf52de907c31a6c853a6f5..f696a96a7ee7d1c10d4965325cfe01a344a6d200 100644 (file)
@@ -809,8 +809,7 @@ function item_post(App $a) {
                $post_id = 0;
        }
 
-       q("COMMIT");
-       q("START TRANSACTION;");
+       dba::transaction();
 
        $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,
                                        `owner-name`,`owner-link`,`owner-avatar`, `owner-id`,
@@ -900,7 +899,7 @@ function item_post(App $a) {
        }
 
        if ($post_id == 0) {
-               q("COMMIT");
+               dba::commit();
                logger('mod_item: unable to retrieve post that was just stored.');
                notice(t('System error. Post not saved.') . EOL);
                goaway($return_path);
@@ -1026,7 +1025,7 @@ function item_post(App $a) {
                update_thread($parent, true);
        }
 
-       q("COMMIT");
+       dba::commit();
 
        create_tags_from_item($post_id);
        create_files_from_item($post_id);
index 15538e88da00f4ac2f925c9c9cbc07038d8ff105..c52b52820b7e4d81fee59214b0d66cb743b7aca9 100644 (file)
@@ -722,8 +722,8 @@ class App {
         *
         * @return string
         */
-       function callstack() {
-               $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6);
+       function callstack($depth = 4) {
+               $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $depth + 2);
 
                // We remove the first two items from the list since they contain data that we don't need.
                array_shift($trace);