From ae8098eaf24155d3ad8f6f5747abf3bad94d7749 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 05:53:07 +0000 Subject: [PATCH 01/13] Config now works with the new database routines --- src/Core/Config.php | 34 ++++++++++++++-------------------- src/Core/PConfig.php | 28 ++++++++++------------------ 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/Core/Config.php b/src/Core/Config.php index d454de392..c04615313 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -1,6 +1,7 @@ config[$k] = $rr['v']; - } else { - $a->config[$family][$k] = $rr['v']; - self::$cache[$family][$k] = $rr['v']; - self::$in_db[$family][$k] = true; - } + $r = dba::select('config', array('v', 'k'), array('cat' => $family), array("order" => array("cat", "k"))); + while ($rr = dba::fetch($r)) { + $k = $rr['k']; + if ($family === 'config') { + $a->config[$k] = $rr['v']; + } else { + $a->config[$family][$k] = $rr['v']; + self::$cache[$family][$k] = $rr['v']; + self::$in_db[$family][$k] = true; } } } @@ -98,13 +97,11 @@ class Config { } } - $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s'", - dbesc($family), - dbesc($key) - ); + $ret = dba::select('config', array('v'), array('cat' => $family, 'k' => $key), + array("order" => array("cat", "k"), 'limit' => 1)); if (dbm::is_result($ret)) { // manage array value - $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v']) ? unserialize($ret[0]['v']) : $ret[0]['v']); + $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']); // Assign the value from the database to the cache self::$cache[$family][$key] = $val; @@ -206,11 +203,8 @@ class Config { unset(self::$cache[$family][$key]); unset(self::$in_db[$family][$key]); } - $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'", - dbesc($family), - dbesc($key) - ); + $ret = dba::delete('config', array('cat' => $family, 'k' => $key)); return $ret; } } diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index 2cfcfc235..6408b56ff 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -1,6 +1,7 @@ $family, 'uid' => $uid), + array("order" => array('uid', 'cat', 'k'))); if (dbm::is_result($r)) { - foreach ($r as $rr) { + while ($rr = dba::fetch($r)) { $k = $rr['k']; $a->config[$uid][$family][$k] = $rr['v']; self::$in_db[$uid][$family][$k] = true; @@ -89,14 +89,10 @@ class PConfig { } } - $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1", - intval($uid), - dbesc($family), - dbesc($key) - ); - - if (count($ret)) { - $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']); + $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), + array("order" => array('uid', 'cat', 'k'), 'limit' => 1)); + if (dbm::is_result($ret)) { + $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v']) ? unserialize($ret[0]['v']) : $ret[0]['v']); $a->config[$uid][$family][$key] = $val; self::$in_db[$uid][$family][$key] = true; @@ -193,11 +189,7 @@ class PConfig { unset(self::$in_db[$uid][$family][$key]); } - $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'", - intval($uid), - dbesc($family), - dbesc($key) - ); + $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key)); return $ret; } From 64718b53d1733df0dcd758fb50cf5b215ccc0832 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 06:16:19 +0000 Subject: [PATCH 02/13] Works now --- src/Core/Config.php | 3 ++- src/Core/PConfig.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Core/Config.php b/src/Core/Config.php index c04615313..fbb79bc5b 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -98,7 +98,7 @@ class Config { } $ret = dba::select('config', array('v'), array('cat' => $family, 'k' => $key), - array("order" => array("cat", "k"), 'limit' => 1)); + array("order" => array("cat", "k"), 'limit' => 1)); if (dbm::is_result($ret)) { // manage array value $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']); @@ -205,6 +205,7 @@ class Config { } $ret = dba::delete('config', array('cat' => $family, 'k' => $key)); + return $ret; } } diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index 6408b56ff..4b43d969a 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -92,7 +92,7 @@ class PConfig { $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), array("order" => array('uid', 'cat', 'k'), 'limit' => 1)); if (dbm::is_result($ret)) { - $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v']) ? unserialize($ret[0]['v']) : $ret[0]['v']); + $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']); $a->config[$uid][$family][$key] = $val; self::$in_db[$uid][$family][$key] = true; From a5e7f7bf701cb0e2b06750d6a61857e6235888d1 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 07:57:33 +0000 Subject: [PATCH 03/13] We don't need an order. --- src/Core/Config.php | 5 ++--- src/Core/PConfig.php | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Core/Config.php b/src/Core/Config.php index fbb79bc5b..71189bd0e 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -46,7 +46,7 @@ class Config { $a = get_app(); - $r = dba::select('config', array('v', 'k'), array('cat' => $family), array("order" => array("cat", "k"))); + $r = dba::select('config', array('v', 'k'), array('cat' => $family)); while ($rr = dba::fetch($r)) { $k = $rr['k']; if ($family === 'config') { @@ -97,8 +97,7 @@ class Config { } } - $ret = dba::select('config', array('v'), array('cat' => $family, 'k' => $key), - array("order" => array("cat", "k"), 'limit' => 1)); + $ret = dba::select('config', array('v'), array('cat' => $family, 'k' => $key), array('limit' => 1)); if (dbm::is_result($ret)) { // manage array value $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']); diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index 4b43d969a..ab3d548c0 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -36,8 +36,7 @@ class PConfig { public static function load($uid, $family) { $a = get_app(); - $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid), - array("order" => array('uid', 'cat', 'k'))); + $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid)); if (dbm::is_result($r)) { while ($rr = dba::fetch($r)) { $k = $rr['k']; @@ -89,8 +88,7 @@ class PConfig { } } - $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), - array("order" => array('uid', 'cat', 'k'), 'limit' => 1)); + $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), array('limit' => 1)); if (dbm::is_result($ret)) { $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']); $a->config[$uid][$family][$key] = $val; From fce72cbbc8151a824269b7492e9ec7b6d551aa36 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 21:12:41 +0000 Subject: [PATCH 04/13] Modifed "update" and "insert" function / many changed queries --- include/contact_selectors.php | 8 ++++---- include/conversation.php | 19 +++++++++--------- include/dba.php | 37 ++++++++++++++++------------------- include/identity.php | 32 ++++++++++++++---------------- include/items.php | 27 ++----------------------- include/plugin.php | 14 ++++++------- mod/display.php | 4 +--- mod/network.php | 10 ++-------- src/App.php | 9 +++++---- src/Core/Config.php | 14 +++---------- src/Core/PConfig.php | 15 ++------------ 11 files changed, 66 insertions(+), 123 deletions(-) diff --git a/include/contact_selectors.php b/include/contact_selectors.php index 27bdb4757..05059e335 100644 --- a/include/contact_selectors.php +++ b/include/contact_selectors.php @@ -101,12 +101,12 @@ function network_to_name($s, $profile = "") { $networkname = str_replace($search, $replace, $s); if ((in_array($s, array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) && ($profile != "")) { - $r = q("SELECT `gserver`.`platform` FROM `gcontact` + $r = dba::fetch_first("SELECT `gserver`.`platform` FROM `gcontact` INNER JOIN `gserver` ON `gserver`.`nurl` = `gcontact`.`server_url` - WHERE `gcontact`.`nurl` = '%s' AND `platform` != ''", - dbesc(normalise_link($profile))); + WHERE `gcontact`.`nurl` = ? AND `platform` != ''", normalise_link($profile)); + if (dbm::is_result($r)) { - $networkname = $r[0]["platform"]; + $networkname = $r['platform']; } } diff --git a/include/conversation.php b/include/conversation.php index bd6e71465..a8ac83c1a 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -921,10 +921,11 @@ function best_link_url($item, &$sparkle, $ssl_state = false) { $clean_url = normalise_link($item['author-link']); if (local_user()) { - $r = q("SELECT `id` FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` = '%s' AND NOT `pending` LIMIT 1", - dbesc(NETWORK_DFRN), intval(local_user()), dbesc(normalise_link($clean_url))); + $r = dba::select('contact', array('id'), + array('network' => NETWORK_DFRN, 'uid' => local_user(), 'nurl' => normalise_link($clean_url), 'pending' => false), + array('limit' => 1)); if (dbm::is_result($r)) { - $best_url = 'redir/' . $r[0]['id']; + $best_url = 'redir/' . $r['id']; $sparkle = true; } } @@ -940,7 +941,6 @@ function best_link_url($item, &$sparkle, $ssl_state = false) { } -if (! function_exists('item_photo_menu')) { function item_photo_menu($item) { $ssl_state = false; @@ -970,12 +970,11 @@ function item_photo_menu($item) { $cid = 0; $network = ''; $rel = 0; - $r = q("SELECT `id`, `network`, `rel` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1", - intval(local_user()), dbesc(normalise_link($item['author-link']))); + $r = dba::select('contact', array('id', 'network', 'rel'), array('uid' => local_user(), 'nurl' => normalise_link($item['author-link'])), array('limit' => 1)); if (dbm::is_result($r)) { - $cid = $r[0]['id']; - $network = $r[0]['network']; - $rel = $r[0]['rel']; + $cid = $r['id']; + $network = $r['network']; + $rel = $r['rel']; } if ($sparkle) { @@ -1036,7 +1035,7 @@ function item_photo_menu($item) { } } return $o; -}} +} if (! function_exists('builtin_activity_puller')) { /** diff --git a/include/dba.php b/include/dba.php index c2ae78c0f..954d218c1 100644 --- a/include/dba.php +++ b/include/dba.php @@ -891,12 +891,20 @@ class dba { * * @param string $table Table name * @param array $param parameter array + * @param bool $on_duplicate_update Do an update on a duplicate entry * * @return boolean was the insert successfull? */ - static public function insert($table, $param) { + static public function insert($table, $param, $on_duplicate_update = false) { $sql = "INSERT INTO `".self::$dbo->escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (". - substr(str_repeat("?, ", count($param)), 0, -2).");"; + substr(str_repeat("?, ", count($param)), 0, -2).")"; + + if ($on_duplicate_update) { + $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?"; + + $values = array_values($param); + $param = array_merge_recursive($values, $values); + } return self::e($sql, $param); } @@ -1160,34 +1168,27 @@ class dba { * @param string $table Table name * @param array $fields contains the fields that are updated * @param array $condition condition array with the key values - * @param array|boolean $old_fields array with the old field values that are about to be replaced + * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate) * * @return boolean was the update successfull? */ static public function update($table, $fields, $condition, $old_fields = array()) { - /** @todo We may use MySQL specific functions here: - * INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'" - * But I think that it doesn't make sense here. - */ - $table = self::$dbo->escape($table); if (is_bool($old_fields)) { $sql = "SELECT * FROM `".$table."` WHERE `". implode("` = ? AND `", array_keys($condition))."` = ? LIMIT 1"; - $params = array(); - foreach ($condition AS $value) { - $params[] = $value; - } + $params = array_values($condition); $do_insert = $old_fields; $old_fields = self::fetch_first($sql, $params); if (is_bool($old_fields)) { if ($do_insert) { - return self::insert($table, $fields); + $values = array_merge($condition, $fields); + return self::insert($table, $values, $do_insert); } $old_fields = array(); } @@ -1213,13 +1214,9 @@ class dba { implode("` = ?, `", array_keys($fields))."` = ? WHERE `". implode("` = ? AND `", array_keys($condition))."` = ?"; - $params = array(); - foreach ($fields AS $value) { - $params[] = $value; - } - foreach ($condition AS $value) { - $params[] = $value; - } + $params1 = array_values($fields); + $params2 = array_values($condition); + $params = array_merge_recursive($params1, $params2); return self::e($sql, $params); } diff --git a/include/identity.php b/include/identity.php index b24a5314a..03a541645 100644 --- a/include/identity.php +++ b/include/identity.php @@ -136,49 +136,47 @@ function profile_load(App $a, $nickname, $profile = 0, $profiledata = array()) { */ function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0) { if (remote_user() && count($_SESSION['remote'])) { - foreach ($_SESSION['remote'] as $visitor) { - if ($visitor['uid'] == $uid) { - $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1", - intval($visitor['cid']) - ); - if (dbm::is_result($r)) - $profile = $r[0]['profile-id']; - break; + foreach ($_SESSION['remote'] as $visitor) { + if ($visitor['uid'] == $uid) { + $r = dba::select('contact', array('profile-id'), array('id' => $visitor['cid']), array('limit' => 1)); + if (dbm::is_result($r)) { + $profile = $r['profile-id']; } + break; } } + } $r = null; if ($profile) { $profile_int = intval($profile); - $r = q("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`, + $r = dba::fetch_first("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`, `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`, `profile`.`uid` AS `profile_uid`, `profile`.*, `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.* FROM `profile` INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` - WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1", - dbesc($nickname), - intval($profile_int) + WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1", + $nickname, + $profile_int ); } if (!dbm::is_result($r)) { - $r = q("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`, + $r = dba::fetch_first("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`, `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`, `profile`.`uid` AS `profile_uid`, `profile`.*, `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.* FROM `profile` INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` - WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` LIMIT 1", - dbesc($nickname) + WHERE `user`.`nickname` = ? AND `profile`.`is-default` LIMIT 1", + $nickname ); } - return $r[0]; - + return $r; } diff --git a/include/items.php b/include/items.php index 94401509b..521298647 100644 --- a/include/items.php +++ b/include/items.php @@ -446,15 +446,6 @@ function store_conversation($arr) { $conversation['source'] = $arr['source']; } - if (!Lock::set('store_conversation')) { - // When using semaphores, this case never can't happen - unset($arr['conversation-uri']); - unset($arr['conversation-href']); - unset($arr['protocol']); - unset($arr['source']); - return $arr; - } - $old_conv = dba::fetch_first("SELECT `item-uri`, `reply-to-uri`, `conversation-uri`, `conversation-href`, `protocol`, `source` FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']); if (dbm::is_result($old_conv)) { @@ -472,11 +463,10 @@ function store_conversation($arr) { logger('Conversation: update for '.$conversation['item-uri'].' from '.$conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG); } } else { - if (!dba::insert('conversation', $conversation)) { + if (!dba::insert('conversation', $conversation, true)) { logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG); } } - Lock::remove('store_conversation'); } unset($arr['conversation-uri']); @@ -966,23 +956,10 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f } } - // Store the unescaped version - $unescaped = $arr; - - dbm::esc_array($arr, true); - logger('item_store: ' . print_r($arr,true), LOGGER_DATA); dba::transaction(); - - $r = dbq("INSERT INTO `item` (`" - . implode("`, `", array_keys($arr)) - . "`) VALUES (" - . implode(", ", array_values($arr)) - . ")"); - - // And restore it - $arr = $unescaped; + $r = dba::insert('item', $arr); // When the item was successfully stored we fetch the ID of the item. if (dbm::is_result($r)) { diff --git a/include/plugin.php b/include/plugin.php index f5f0bb2b2..60ef6138b 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -183,20 +183,18 @@ function unregister_hook($hook,$file,$function) { }} -if (! function_exists('load_hooks')) { function load_hooks() { $a = get_app(); $a->hooks = array(); - $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`"); + $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file'))); - if (dbm::is_result($r)) { - foreach ($r as $rr) { - if (! array_key_exists($rr['hook'],$a->hooks)) - $a->hooks[$rr['hook']] = array(); - $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']); + while ($rr = dba::fetch($r)) { + if (! array_key_exists($rr['hook'],$a->hooks)) { + $a->hooks[$rr['hook']] = array(); } + $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']); } -}} +} /** * @brief Calls a hook. diff --git a/mod/display.php b/mod/display.php index 0d347882e..08b51bab4 100644 --- a/mod/display.php +++ b/mod/display.php @@ -423,9 +423,7 @@ function display_content(App $a, $update = 0) { intval($r[0]['parent'])); if ($unseen) { - q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d AND `unseen`", - intval($r[0]['parent']) - ); + dba::update('item', array('unseen' => false), array('parent' => $r[0]['parent'], 'unseen' => true)); } } diff --git a/mod/network.php b/mod/network.php index aad56bfd6..ebbd56dfb 100644 --- a/mod/network.php +++ b/mod/network.php @@ -794,18 +794,12 @@ function network_content(App $a, $update = 0) { if (!$group && !$cid && !$star) { - - $unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `uid` = %d LIMIT 1", - intval(local_user())); + $unseen = dba::select('item', array('id'), array('unseen' => true, 'uid' => local_user()), array('limit' => 1)); if (dbm::is_result($unseen)) { - $r = q("UPDATE `item` SET `unseen` = 0 - WHERE `unseen` = 1 AND `uid` = %d", - intval(local_user()) - ); + $r = dba::update('item', array('unseen' => false), array('uid' => local_user(), 'unseen' => true)); } } elseif ($update_unseen) { - $unseen = q("SELECT `id` FROM `item` ".$update_unseen. " LIMIT 1"); if (dbm::is_result($unseen)) { diff --git a/src/App.php b/src/App.php index ec0382993..4cdaa94cc 100644 --- a/src/App.php +++ b/src/App.php @@ -6,6 +6,7 @@ use Friendica\Core\Config; use Friendica\Core\PConfig; use Cache; +use dba; use dbm; use Detection\MobileDetect; @@ -712,20 +713,20 @@ class App { $this->remove_inactive_processes(); - q('START TRANSACTION'); + dba::transaction(); $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid())); if (!dbm::is_result($r)) { q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')", intval(getmypid()), dbesc($command), dbesc(datetime_convert())); } - q('COMMIT'); + dba::commit(); } /** * @brief Remove inactive processes */ function remove_inactive_processes() { - q('START TRANSACTION'); + dba::transaction(); $r = q('SELECT `pid` FROM `process`'); if (dbm::is_result($r)) { @@ -735,7 +736,7 @@ class App { } } } - q('COMMIT'); + dba::commit(); } /** diff --git a/src/Core/Config.php b/src/Core/Config.php index 71189bd0e..0cdccb953 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -164,19 +164,11 @@ class Config { $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); if (is_null($stored) || !self::$in_db[$family][$key]) { - $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'", - dbesc($family), - dbesc($key), - dbesc($dbvalue), - dbesc($dbvalue) - ); + dba::insert('config', array('cat' => $family, 'k' => $key, 'v' => $dbvalue), true); } else { - $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'", - dbesc($dbvalue), - dbesc($family), - dbesc($key) - ); + dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true); } + if ($ret) { self::$in_db[$family][$key] = true; return $value; diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index ab3d548c0..e5a852e40 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -142,20 +142,9 @@ class PConfig { $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); if (is_null($stored) || !self::$in_db[$uid][$family][$key]) { - $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'", - intval($uid), - dbesc($family), - dbesc($key), - dbesc($dbvalue), - dbesc($dbvalue) - ); + dba::insert('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key, 'v' => $dbvalue), true); } else { - $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'", - dbesc($dbvalue), - intval($uid), - dbesc($family), - dbesc($key) - ); + dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true); } if ($ret) { From 69a3f66d563ea0a006000dc3c64254617976b30c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 21:34:39 +0000 Subject: [PATCH 05/13] Just more simplyfication --- src/Core/Config.php | 6 +----- src/Core/PConfig.php | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Core/Config.php b/src/Core/Config.php index 0cdccb953..f3c5dc13a 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -163,11 +163,7 @@ class Config { // manage array value $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); - if (is_null($stored) || !self::$in_db[$family][$key]) { - dba::insert('config', array('cat' => $family, 'k' => $key, 'v' => $dbvalue), true); - } else { - dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true); - } + dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true); if ($ret) { self::$in_db[$family][$key] = true; diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index e5a852e40..808057333 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -141,11 +141,7 @@ class PConfig { // manage array value $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); - if (is_null($stored) || !self::$in_db[$uid][$family][$key]) { - dba::insert('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key, 'v' => $dbvalue), true); - } else { - dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true); - } + dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true); if ($ret) { self::$in_db[$uid][$family][$key] = true; From 6180d62ebc97aaba21fb3f11b5f99fa00b9b90d6 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 22:04:00 +0000 Subject: [PATCH 06/13] Some more inserts --- include/items.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/include/items.php b/include/items.php index 521298647..f8a0851bd 100644 --- a/include/items.php +++ b/include/items.php @@ -1073,12 +1073,8 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f logger("Repaired double encoded signature from handle ".$dsprsig->signer, LOGGER_DEBUG); } - q("INSERT INTO `sign` (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ", - intval($current_post), - dbesc($dsprsig->signed_text), - dbesc($dsprsig->signature), - dbesc($dsprsig->signer) - ); + dba::insert('sign', array('iid' => $current_post, 'signed_text' => $dsprsig->signed_text, + 'signature' => $dsprsig->signature, 'signer' => $dsprsig->signer)); } $deleted = tag_deliver($arr['uid'], $current_post); @@ -1708,13 +1704,9 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) { $hash = random_string(); if (is_array($contact_record)) { - $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`) - VALUES ( %d, %d, 0, 0, '%s', '%s' )", - intval($importer['uid']), - intval($contact_record['id']), - dbesc($hash), - dbesc(datetime_convert()) - ); + dba::insert('intro', array('uid' => $importer['uid'], 'contact-id' => $contact_record['id'], + 'blocked' => false, 'knowyou' => false, + 'hash' => $hash, 'datetime' => datetime_convert())); } $def_gid = get_default_group($importer['uid'], $contact_record["network"]); From c6b04aa922326ab86339790f3106b8128b5005ac Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 22:23:46 +0000 Subject: [PATCH 07/13] And some more inserts --- include/diaspora.php | 20 ++++---------------- include/oembed.php | 7 ++----- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/include/diaspora.php b/include/diaspora.php index 77c459e64..60c613178 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -1475,10 +1475,7 @@ class Diaspora { // Formerly we stored the signed text, the signature and the author in different fields. // We now store the raw data so that we are more flexible. - q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')", - intval($message_id), - dbesc(json_encode($data)) - ); + dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data))); // notify others proc_run(PRIORITY_HIGH, "include/notifier.php", "comment-import", $message_id); @@ -1783,10 +1780,7 @@ class Diaspora { // Formerly we stored the signed text, the signature and the author in different fields. // We now store the raw data so that we are more flexible. - q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')", - intval($message_id), - dbesc(json_encode($data)) - ); + dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data))); // notify others proc_run(PRIORITY_HIGH, "include/notifier.php", "comment-import", $message_id); @@ -3728,10 +3722,7 @@ class Diaspora { * Now store the signature more flexible to dynamically support new fields. * This will break Diaspora compatibility with Friendica versions prior to 3.5. */ - q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')", - intval($post_id), - dbesc(json_encode($message)) - ); + dba::insert('sign', array('iid' => $post_id, 'signed_text' => json_encode($message))); logger('Stored diaspora like signature'); return true; @@ -3763,10 +3754,7 @@ class Diaspora { * Now store the signature more flexible to dynamically support new fields. * This will break Diaspora compatibility with Friendica versions prior to 3.5. */ - q("INSERT INTO `sign` (`iid`, `signed_text`) VALUES (%d, '%s')", - intval($message_id), - dbesc(json_encode($message)) - ); + dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($message))); logger('Stored diaspora comment signature'); return true; diff --git a/include/oembed.php b/include/oembed.php index 0f3296b25..de597bbb3 100755 --- a/include/oembed.php +++ b/include/oembed.php @@ -82,11 +82,8 @@ function oembed_fetch_url($embedurl, $no_rich_type = false){ } else { //save in cache $j = json_decode($txt); if ($j->type != "error") { - q("INSERT INTO `oembed` (`url`, `content`, `created`) VALUES ('%s', '%s', '%s') - ON DUPLICATE KEY UPDATE `content` = '%s', `created` = '%s'", - dbesc(normalise_link($embedurl)), - dbesc($txt), dbesc(datetime_convert()), - dbesc($txt), dbesc(datetime_convert())); + dba::insert('oembed', array('url' => normalise_link($embedurl), + 'content' => $txt, 'created' => datetime_convert())); } Cache::set($a->videowidth.$embedurl, $txt, CACHE_DAY); From 03b86d37662c59b5ec838105c3e6801b453d8996 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 23:02:57 +0000 Subject: [PATCH 08/13] Now we have less than 100 insert commands, yeah --- include/fcontact.php | 12 ++---------- include/group.php | 13 ++----------- include/oembed.php | 2 +- include/plugin.php | 17 +++++------------ src/App.php | 2 +- src/ParseUrl.php | 8 +++----- 6 files changed, 14 insertions(+), 40 deletions(-) diff --git a/include/fcontact.php b/include/fcontact.php index b2706e51a..a559ede30 100644 --- a/include/fcontact.php +++ b/include/fcontact.php @@ -13,11 +13,7 @@ function fcontact_store($url,$name,$photo) { if (dbm::is_result($r)) return $r[0]['id']; - $r = q("INSERT INTO `fcontact` ( `url`, `name`, `photo` ) VALUES ( '%s', '%s', '%s' ) ", - dbesc($nurl), - dbesc($name), - dbesc($photo) - ); + $r = dba::insert('fcontact', array('url' => $nurl, 'name' => $name, 'photo' => $photo)); if (dbm::is_result($r)) { $r = q("SELECT `id` FROM `fcontact` WHERE `url` = '%s' LIMIT 1", @@ -31,11 +27,7 @@ function fcontact_store($url,$name,$photo) { } function ffinder_store($uid,$cid,$fid) { - $r = q("INSERT INTO `ffinder` ( `uid`, `cid`, `fid` ) VALUES ( %d, %d, %d ) ", - intval($uid), - intval($cid), - intval($fid) - ); + $r = dba::insert('ffinder', array('uid' => $uid, 'cid' => $cid, 'fid' => $fid)); return $r; } diff --git a/include/group.php b/include/group.php index ab805e687..8956d21c3 100644 --- a/include/group.php +++ b/include/group.php @@ -26,11 +26,7 @@ function group_add($uid,$name) { } return true; } - $r = q("INSERT INTO `group` ( `uid`, `name` ) - VALUES( %d, '%s' ) ", - intval($uid), - dbesc($name) - ); + $r = dba::insert('group', array('uid' => $uid, 'name' => $name)); $ret = $r; } return $ret; @@ -144,12 +140,7 @@ function group_add_member($uid,$name,$member,$gid = 0) { // we indicate success because the group member was in fact created // -- It was just created at another time if (! dbm::is_result($r)) { - $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`) - VALUES( %d, %d, %d ) ", - intval($uid), - intval($gid), - intval($member) - ); + $r = dba::insert('group_member', array('uid' => $uid, 'gid' => $gid, 'contact-id' => $member)); } return $r; } diff --git a/include/oembed.php b/include/oembed.php index de597bbb3..cb23517cd 100755 --- a/include/oembed.php +++ b/include/oembed.php @@ -83,7 +83,7 @@ function oembed_fetch_url($embedurl, $no_rich_type = false){ $j = json_decode($txt); if ($j->type != "error") { dba::insert('oembed', array('url' => normalise_link($embedurl), - 'content' => $txt, 'created' => datetime_convert())); + 'content' => $txt, 'created' => datetime_convert()), true); } Cache::set($a->videowidth.$embedurl, $txt, CACHE_DAY); diff --git a/include/plugin.php b/include/plugin.php index 60ef6138b..3a2423575 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -46,13 +46,10 @@ function install_plugin($plugin) { $func = $plugin . '_install'; $func(); - $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0); + $plugin_admin = (function_exists($plugin."_plugin_admin") ? 1 : 0); - $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ", - dbesc($plugin), - intval($t), - $plugin_admin - ); + dba::insert('addon', array('name' => $plugin, 'installed' => true, + 'timestamp' => $t, 'plugin_admin' => $plugin_admin)); // we can add the following with the previous SQL // once most site tables have been updated. @@ -154,12 +151,8 @@ function register_hook($hook,$file,$function,$priority=0) { if (dbm::is_result($r)) return true; - $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ", - dbesc($hook), - dbesc($file), - dbesc($function), - dbesc($priority) - ); + $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority)); + return $r; }} diff --git a/src/App.php b/src/App.php index 4cdaa94cc..c416a0b12 100644 --- a/src/App.php +++ b/src/App.php @@ -717,7 +717,7 @@ class App { $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid())); if (!dbm::is_result($r)) { - q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')", intval(getmypid()), dbesc($command), dbesc(datetime_convert())); + dba::insert('process', array('pid' => getmypid(), 'command' => $command, 'created' => datetime_convert())); } dba::commit(); } diff --git a/src/ParseUrl.php b/src/ParseUrl.php index 9e21736a8..0de7663e4 100644 --- a/src/ParseUrl.php +++ b/src/ParseUrl.php @@ -66,11 +66,9 @@ class ParseUrl { $data = self::getSiteinfo($url, $no_guessing, $do_oembed); - q("INSERT INTO `parsed_url` (`url`, `guessing`, `oembed`, `content`, `created`) VALUES ('%s', %d, %d, '%s', '%s') - ON DUPLICATE KEY UPDATE `content` = '%s', `created` = '%s'", - dbesc(normalise_link($url)), intval(!$no_guessing), intval($do_oembed), - dbesc(serialize($data)), dbesc(datetime_convert()), - dbesc(serialize($data)), dbesc(datetime_convert())); + dba::insert('parsed_url', array('url' => normalise_link($url), 'guessing' => !$no_guessing, + 'oembed' => $do_oembed, 'content' => serialize($data), + 'created' => datetime_convert()), true); return $data; } From f1a57dd9c6b37d033bbedd64346f2dba22b0cedb Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 9 Aug 2017 23:08:25 +0000 Subject: [PATCH 09/13] Avoid an error --- src/ParseUrl.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ParseUrl.php b/src/ParseUrl.php index 0de7663e4..1674d5691 100644 --- a/src/ParseUrl.php +++ b/src/ParseUrl.php @@ -10,6 +10,7 @@ namespace Friendica; use Friendica\Core\Config; use xml; +use dba; use DomXPath; use DOMDocument; From bd8efb7f92d39449e35f2c36ed2f707945d0eaf5 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 10 Aug 2017 04:31:46 +0000 Subject: [PATCH 10/13] Several updates are transformed as well --- include/items.php | 99 ++++++++++++++--------------------------------- 1 file changed, 30 insertions(+), 69 deletions(-) diff --git a/include/items.php b/include/items.php index f8a0851bd..e253f8de6 100644 --- a/include/items.php +++ b/include/items.php @@ -844,7 +844,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f $self = normalise_link(App::get_baseurl() . '/profile/' . $u[0]['nickname']); logger("item_store: 'myself' is ".$self." for parent ".$parent_id." checking against ".$arr['author-link']." and ".$arr['owner-link'], LOGGER_DEBUG); if ((normalise_link($arr['author-link']) == $self) || (normalise_link($arr['owner-link']) == $self)) { - q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($parent_id)); + dba::update('thread', array('mention' => true), array('iid' => $parent_id)); logger("item_store: tagged thread ".$parent_id." as mention for user ".$self, LOGGER_DEBUG); } } @@ -908,7 +908,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f $arr["global"] = true; // Set the global flag on all items if this was a global item entry - q("UPDATE `item` SET `global` = 1 WHERE `uri` = '%s'", dbesc($arr["uri"])); + dba::update('item', array('global' => true), array('uri' => $arr["uri"])); } else { $isglobal = q("SELECT `global` FROM `item` WHERE `uid` = 0 AND `uri` = '%s'", dbesc($arr["uri"])); @@ -1039,10 +1039,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f } // Set parent id - $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d", - intval($parent_id), - intval($current_post) - ); + $r = dba::update('item', array('parent' => $parent_id), array('id' => $current_post)); $arr['id'] = $current_post; $arr['parent'] = $parent_id; @@ -1050,16 +1047,9 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f // update the commented timestamp on the parent // Only update "commented" if it is really a comment if (($arr['verb'] == ACTIVITY_POST) || !get_config("system", "like_no_comment")) { - q("UPDATE `item` SET `commented` = '%s', `changed` = '%s' WHERE `id` = %d", - dbesc(datetime_convert()), - dbesc(datetime_convert()), - intval($parent_id) - ); + dba::update('item', array('commented' => datetime_convert(), 'changed' => datetime_convert()), array('id' => $parent_id)); } else { - q("UPDATE `item` SET `changed` = '%s' WHERE `id` = %d", - dbesc(datetime_convert()), - intval($parent_id) - ); + dba::update('item', array('changed' => datetime_convert()), array('id' => $parent_id)); } if ($dsprsig) { @@ -1162,26 +1152,17 @@ function item_set_last_item($arr) { } if ($update) { - q("UPDATE `contact` SET `success_update` = '%s', `last-item` = '%s' WHERE `id` = %d", - dbesc($arr['received']), - dbesc($arr['received']), - intval($arr['contact-id']) - ); + dba::update('contact', array('success_update' => $arr['received'], 'last-item' => $arr['received']), + array('id' => $arr['contact-id'])); } // Now do the same for the system wide contacts with uid=0 if (!$arr['private']) { - q("UPDATE `contact` SET `success_update` = '%s', `last-item` = '%s' WHERE `id` = %d", - dbesc($arr['received']), - dbesc($arr['received']), - intval($arr['owner-id']) - ); + dba::update('contact', array('success_update' => $arr['received'], 'last-item' => $arr['received']), + array('id' => $arr['owner-id'])); if ($arr['owner-id'] != $arr['author-id']) { - q("UPDATE `contact` SET `success_update` = '%s', `last-item` = '%s' WHERE `id` = %d", - dbesc($arr['received']), - dbesc($arr['received']), - intval($arr['author-id']) - ); + dba::update('contact', array('success_update' => $arr['received'], 'last-item' => $arr['received']), + array('id' => $arr['author-id'])); } } } @@ -1487,7 +1468,7 @@ function tgroup_check($uid, $item) { * * @todo fix type-hints (both array) */ -function edited_timestamp_is_newer ($existing, $update) { +function edited_timestamp_is_newer($existing, $update) { if (!x($existing, 'edited') || !$existing['edited']) { return true; } @@ -1660,11 +1641,8 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) { if (is_array($contact)) { if (($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING) || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) { - $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d", - intval(CONTACT_IS_FRIEND), - intval($contact['id']), - intval($importer['uid']) - ); + $r = dba::update('contact', array('rel' => CONTACT_IS_FRIEND, 'writable' => true), + array('id' => $contact['id'], 'uid' => $importer['uid'])); } // send email notification to owner? } else { @@ -1747,10 +1725,7 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) { function lose_follower($importer, $contact, array $datarray = array(), $item = "") { if (($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) { - q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d", - intval(CONTACT_IS_SHARING), - intval($contact['id']) - ); + dba::update('contact', array('rel' => CONTACT_IS_SHARING), array('id' => $contact['id'])); } else { contact_remove($contact['id']); } @@ -1759,10 +1734,7 @@ function lose_follower($importer, $contact, array $datarray = array(), $item = " function lose_sharer($importer, $contact, array $datarray = array(), $item = "") { if (($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) { - q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d", - intval(CONTACT_IS_FOLLOWER), - intval($contact['id']) - ); + dba::update('contact', array('rel' => CONTACT_IS_FOLLOWER), array('id' => $contact['id'])); } else { contact_remove($contact['id']); } @@ -1797,10 +1769,7 @@ function subscribe_to_hub($url, $importer, $contact, $hubmode = 'subscribe') { logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: ' . $push_url . ' with verifier ' . $verify_token); if (!strlen($contact['hub-verify']) || ($contact['hub-verify'] != $verify_token)) { - $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d", - dbesc($verify_token), - intval($contact['id']) - ); + $r = dba::update('contact', array('hub-verify' => $verify_token), array('id' => $contact['id'])); } post_url($url, $params); @@ -2168,13 +2137,12 @@ function drop_item($id, $interactive = true) { } logger('delete item: ' . $item['id'], LOGGER_DEBUG); - // delete the item - $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d", - dbesc(datetime_convert()), - dbesc(datetime_convert()), - intval($item['id']) - ); + // delete the item + $r = dba::update('item', array('deleted' => true, 'title' => '', 'body' => '', + 'edited' => datetime_convert(), 'changed' => datetime_convert()), + array('id' => $item['id'])); + create_tags_from_item($item['id']); create_files_from_item($item['id']); delete_thread($item['id'], $item['parent-uri']); @@ -2257,33 +2225,26 @@ function drop_item($id, $interactive = true) { // If it's the parent of a comment thread, kill all the kids if ($item['uri'] == $item['parent-uri']) { - $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = '' - WHERE `parent-uri` = '%s' AND `uid` = %d ", - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc($item['parent-uri']), - intval($item['uid']) - ); + $r = dba::update('item', array('deleted' => true, 'title' => '', 'body' => '', + 'edited' => datetime_convert(), 'changed' => datetime_convert()), + array('parent-uri' => $item['parent-uri'], 'uid' => $item['uid'])); + create_tags_from_itemuri($item['parent-uri'], $item['uid']); create_files_from_itemuri($item['parent-uri'], $item['uid']); delete_thread_uri($item['parent-uri'], $item['uid']); // ignore the result } else { // ensure that last-child is set in case the comment that had it just got wiped. - q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ", - dbesc(datetime_convert()), - dbesc($item['parent-uri']), - intval($item['uid']) - ); + dba::update('item', array('last-child' => false, 'changed' => datetime_convert()), + array('parent-uri' => $item['parent-uri'], 'uid' => $item['uid'])); + // who is the last child now? $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1", dbesc($item['parent-uri']), intval($item['uid']) ); if (dbm::is_result($r)) { - q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d", - intval($r[0]['id']) - ); + dba::update('item', array('last-child' => true), array('id' => $r[0]['id'])); } } From eb7cbb9adafa003a06f2e2d2df70b4f1a46b1a85 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 10 Aug 2017 05:07:23 +0000 Subject: [PATCH 11/13] Now there are fewer than 300 updates left ... --- include/diaspora.php | 35 +++++++++++------------------------ include/plugin.php | 9 ++------- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/include/diaspora.php b/include/diaspora.php index 60c613178..7cc3e8122 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -928,11 +928,9 @@ class Diaspora { * Normally this should have handled by getting a request - but this could get lost */ if ($contact["rel"] == CONTACT_IS_FOLLOWER && in_array($importer["page-flags"], array(PAGE_FREELOVE))) { - q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d", - intval(CONTACT_IS_FRIEND), - intval($contact["id"]), - intval($importer["uid"]) - ); + dba::update('contact', array('rel' => CONTACT_IS_FRIEND, 'writable' => true), + array('id' => $contact["id"], 'uid' => $contact["uid"])); + $contact["rel"] = CONTACT_IS_FRIEND; logger("defining user ".$contact["nick"]." as friend"); } @@ -1557,10 +1555,7 @@ class Diaspora { dba::unlock(); - q("UPDATE `conv` SET `updated` = '%s' WHERE `id` = %d", - dbesc(datetime_convert()), - intval($conversation["id"]) - ); + dba::update('conv', array('updated' => datetime_convert()), array('id' => $conversation["id"])); notification(array( "type" => NOTIFY_MAIL, @@ -1865,11 +1860,7 @@ class Diaspora { dba::unlock(); - q("UPDATE `conv` SET `updated` = '%s' WHERE `id` = %d", - dbesc(datetime_convert()), - intval($conversation["id"]) - ); - + dba::update('conv', array('updated' => datetime_convert()), array('id' => $conversation["id"])); return true; } @@ -2013,11 +2004,8 @@ class Diaspora { $a = get_app(); if ($contact["rel"] == CONTACT_IS_FOLLOWER && in_array($importer["page-flags"], array(PAGE_FREELOVE))) { - q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d", - intval(CONTACT_IS_FRIEND), - intval($contact["id"]), - intval($importer["uid"]) - ); + dba::update('contact', array('rel' => CONTACT_IS_FRIEND, 'writable' => true), + array('id' => $contact["id"], 'uid' => $importer["uid"])); } // send notification @@ -2468,11 +2456,10 @@ class Diaspora { } // Currently we don't have a central deletion function that we could use in this case. The function "item_drop" doesn't work for that case - q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = '' WHERE `id` = %d", - dbesc(datetime_convert()), - dbesc(datetime_convert()), - intval($r[0]["id"]) - ); + dba::update('item', array('deleted' => true, 'title' => '', 'body' => '', + 'edited' => datetime_convert(), 'changed' => datetime_convert()), + array('id' => $r[0]["id"])); + delete_thread($r[0]["id"], $r[0]["parent-uri"]); logger("Deleted target ".$target_guid." (".$r[0]["id"].") from user ".$importer["uid"]." parent: ".$p[0]["id"], LOGGER_DEBUG); diff --git a/include/plugin.php b/include/plugin.php index 3a2423575..25e65ec24 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -56,9 +56,7 @@ function install_plugin($plugin) { // This way the system won't fall over dead during the update. if (file_exists('addon/' . $plugin . '/.hidden')) { - q("UPDATE `addon` SET `hidden` = 1 WHERE `name` = '%s'", - dbesc($plugin) - ); + dba::update('addon', array('hidden' => true), array('name' => $plugin)); } return true; } @@ -106,10 +104,7 @@ function reload_plugins() { $func = $pl . '_install'; $func(); } - q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d", - intval($t), - intval($i['id']) - ); + dba::update('addon', array('timestamp' => $t), array('id' => $i['id'])); } } } From 33b61ab26283deccb9ecf7f7b28fce7aa27ef2f1 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 10 Aug 2017 06:33:24 +0000 Subject: [PATCH 12/13] Database connections should be closed --- include/plugin.php | 1 + src/Core/Config.php | 1 + src/Core/PConfig.php | 1 + 3 files changed, 3 insertions(+) diff --git a/include/plugin.php b/include/plugin.php index 25e65ec24..be06122ea 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -182,6 +182,7 @@ function load_hooks() { } $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']); } + dba::close($r); } /** diff --git a/src/Core/Config.php b/src/Core/Config.php index f3c5dc13a..be0d0def1 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -57,6 +57,7 @@ class Config { self::$in_db[$family][$k] = true; } } + dba::close($r); } /** diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index 808057333..5e1724324 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -43,6 +43,7 @@ class PConfig { $a->config[$uid][$family][$k] = $rr['v']; self::$in_db[$uid][$family][$k] = true; } + dba::close($r); } else if ($family != 'config') { // Negative caching $a->config[$uid][$family] = "!!"; From 7b6664f0f7a4a4756f86cbb2b96e7d54a36f92ad Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 10 Aug 2017 12:38:32 +0000 Subject: [PATCH 13/13] Improved parameter handling --- include/dba.php | 53 ++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/include/dba.php b/include/dba.php index 5e2e4b419..dd95a74c5 100644 --- a/include/dba.php +++ b/include/dba.php @@ -509,6 +509,22 @@ class dba { return $sql; } + /** + * @brief Convert parameter array to an universal form + * @param array $args Parameter array + * @return array universalized parameter array + */ + private static function getParam($args) { + unset($args[0]); + + // When the second function parameter is an array then use this as the parameter array + if ((count($args) > 0) && (is_array($args[1]))) { + return $args[1]; + } else { + return $args; + } + } + /** * @brief Executes a prepared statement that returns data * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid); @@ -520,15 +536,7 @@ class dba { $stamp1 = microtime(true); - $args = func_get_args(); - unset($args[0]); - - // When the second function parameter is an array then use this as the parameter array - if ((count($args) > 0) && (is_array($args[1]))) { - $params = $args[1]; - } else { - $params = $args; - } + $params = self::getParam(func_get_args()); // Renumber the array keys to be sure that they fit $i = 0; @@ -560,10 +568,10 @@ class dba { self::$dbo->affected_rows = 0; // We have to make some things different if this function is called from "e" - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); - if (isset($trace[2])) { - $called_from = $trace[2]; + if (isset($trace[1])) { + $called_from = $trace[1]; } else { // We use just something that is defined to avoid warnings $called_from = $trace[0]; @@ -719,13 +727,13 @@ class dba { $stamp = microtime(true); - $args = func_get_args(); + $params = self::getParam(func_get_args()); // In a case of a deadlock we are repeating the query 20 times $timeout = 20; do { - $stmt = call_user_func_array('self::p', $args); + $stmt = self::p($sql, $params); if (is_bool($stmt)) { $retval = $stmt; @@ -744,15 +752,6 @@ class dba { $error = self::$dbo->error; $errorno = self::$dbo->errorno; - array_shift($args); - - // When the second function parameter is an array then use this as the parameter array - if ((count($args) > 0) && (is_array($args[0]))) { - $params = $args[0]; - } else { - $params = $args; - } - logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". $a->callstack(8)."\n".self::replace_parameters($sql, $params)); @@ -772,9 +771,9 @@ class dba { * @return boolean Are there rows for that query? */ static public function exists($sql) { - $args = func_get_args(); + $params = self::getParam(func_get_args()); - $stmt = call_user_func_array('self::p', $args); + $stmt = self::p($sql, $params); if (is_bool($stmt)) { $retval = $stmt; @@ -794,9 +793,9 @@ class dba { * @return array first row of query */ static public function fetch_first($sql) { - $args = func_get_args(); + $params = self::getParam(func_get_args()); - $stmt = call_user_func_array('self::p', $args); + $stmt = self::p($sql, $params); if (is_bool($stmt)) { $retval = $stmt;