Merge pull request #3622 from annando/new-dba

Enhanced dba functions, several replaced database calls
This commit is contained in:
Tobias Diekershoff 2017-08-11 08:11:20 +02:00 committed by GitHub
commit e6e8ebbac5
16 changed files with 183 additions and 367 deletions

View file

@ -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'];
}
}

View file

@ -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')) {
/**

View file

@ -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;
@ -891,12 +890,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 +1167,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 +1213,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);
}

View file

@ -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");
}
@ -1475,10 +1473,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);
@ -1560,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,
@ -1783,10 +1775,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);
@ -1871,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;
}
@ -2019,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
@ -2474,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);
@ -3728,10 +3709,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 +3741,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;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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']);
@ -854,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);
}
}
@ -918,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"]));
@ -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)) {
@ -1062,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;
@ -1073,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) {
@ -1096,12 +1063,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);
@ -1189,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']));
}
}
}
@ -1514,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;
}
@ -1687,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 {
@ -1731,13 +1682,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"]);
@ -1778,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']);
}
@ -1790,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']);
}
@ -1828,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);
@ -2199,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']);
@ -2288,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']));
}
}

View file

@ -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()), true);
}
Cache::set($a->videowidth.$embedurl, $txt, CACHE_DAY);

View file

@ -46,22 +46,17 @@ 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.
// 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;
}
@ -109,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']));
}
}
}
@ -154,12 +146,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;
}}
@ -183,20 +171,19 @@ 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']);
}
}}
dba::close($r);
}
/**
* @brief Calls a hook.

View file

@ -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));
}
}

View file

@ -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)) {

View file

@ -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()));
dba::insert('process', array('pid' => getmypid(), 'command' => $command, 'created' => 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();
}
/**

View file

@ -1,6 +1,7 @@
<?php
namespace Friendica\Core;
use dba;
use dbm;
/**
@ -45,19 +46,18 @@ class Config {
$a = get_app();
$r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
if (dbm::is_result($r)) {
foreach ($r as $rr) {
$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;
}
$r = dba::select('config', array('v', 'k'), array('cat' => $family));
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;
}
}
dba::close($r);
}
/**
@ -98,13 +98,10 @@ 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('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;
@ -167,20 +164,8 @@ class Config {
// manage array value
$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)
);
} 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;
@ -206,10 +191,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;
}

View file

@ -1,6 +1,7 @@
<?php
namespace Friendica\Core;
use dba;
use dbm;
/**
@ -34,16 +35,15 @@ class PConfig {
*/
public static function load($uid, $family) {
$a = get_app();
$r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
dbesc($family),
intval($uid)
);
$r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid));
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;
}
dba::close($r);
} else if ($family != 'config') {
// Negative caching
$a->config[$uid][$family] = "!<unset>!";
@ -89,14 +89,9 @@ 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('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;
self::$in_db[$uid][$family][$key] = true;
@ -147,22 +142,7 @@ class PConfig {
// manage array value
$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)
);
} 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) {
self::$in_db[$uid][$family][$key] = true;
@ -193,11 +173,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;
}

View file

@ -10,6 +10,7 @@ namespace Friendica;
use Friendica\Core\Config;
use xml;
use dba;
use DomXPath;
use DOMDocument;
@ -66,11 +67,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;
}