Some easy to replace "q" calls have been replaced by "DBA" calls (#5632)

* Some easy to replace "q" calls have been replaced by "DBA" calls

* Simplified the GUID creation

* And one in the API ...

* And OStatus has got some DBA calls more

* Just some more replaced database calls

* The event query is now simplified

* Events are now shown again

* subthread is now using the DBA calls as well

* Some more replaced database calls

* And some more replaced database calls and prevented notices

* Better use gravity

* Some more replaced database stuff

* Some more replaced database calls in DFRN.php

* The gcontact class now has got the new DBA functions as well

* The Contact class is now changed to new database functions as well

* Small correction

* We can now delete without cascade

* One more functionality is safe for future changes
This commit is contained in:
Michael Vogel 2018-08-19 14:46:11 +02:00 committed by Hypolite Petovan
parent dfe4413463
commit 7f3fb34c24
21 changed files with 275 additions and 501 deletions

View file

@ -1056,13 +1056,10 @@ class DFRN
}
foreach ($mentioned as $mention) {
$r = q(
"SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
intval($owner["uid"]),
DBA::escape(normalise_link($mention))
);
$condition = ['uid' => $owner["uid"], 'nurl' => normalise_link($mention)];
$contact = DBA::selectFirst('contact', ['forum', 'prv'], $condition);
if (DBA::isResult($r) && ($r[0]["forum"] || $r[0]["prv"])) {
if (DBA::isResult($contact) && ($contact["forum"] || $contact["prv"])) {
XML::addElement(
$doc,
$entry,
@ -1232,14 +1229,11 @@ class DFRN
$final_dfrn_id = '';
if ($perm) {
if ((($perm == 'rw') && (! intval($contact['writable'])))
|| (($perm == 'r') && (intval($contact['writable'])))
if ((($perm == 'rw') && !intval($contact['writable']))
|| (($perm == 'r') && intval($contact['writable']))
) {
q(
"update contact set writable = %d where id = %d",
intval(($perm == 'rw') ? 1 : 0),
intval($contact['id'])
);
DBA::update('contact', ['writable' => ($perm == 'rw')], ['id' => $contact['id']]);
$contact['writable'] = (string) 1 - intval($contact['writable']);
}
}
@ -1480,15 +1474,9 @@ class DFRN
private static function birthdayEvent($contact, $birthday)
{
// Check for duplicates
$r = q(
"SELECT `id` FROM `event` WHERE `uid` = %d AND `cid` = %d AND `start` = '%s' AND `type` = '%s' LIMIT 1",
intval($contact['uid']),
intval($contact['id']),
DBA::escape(DateTimeFormat::utc($birthday)),
DBA::escape('birthday')
);
if (DBA::isResult($r)) {
$condition = ['uid' => $contact['uid'], 'cid' => $contact['id'],
'start' => DateTimeFormat::utc($birthday), 'type' => 'birthday'];
if (DBA::exists('event', $condition)) {
return;
}
@ -1910,13 +1898,6 @@ class DFRN
// Does our member already have a friend matching this description?
$r = q(
"SELECT `id` FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
DBA::escape($suggest["name"]),
DBA::escape(normalise_link($suggest["url"])),
intval($suggest["uid"])
);
/*
* The valid result means the friend we're about to send a friend
* suggestion already has them in their contact, which means no further
@ -1924,37 +1905,29 @@ class DFRN
*
* @see https://github.com/friendica/friendica/pull/3254#discussion_r107315246
*/
if (DBA::isResult($r)) {
$condition = ['name' => $suggest["name"], 'nurl' => normalise_link($suggest["url"]),
'uid' => $suggest["uid"]];
if (DBA::exists('contact', $condition)) {
return false;
}
// Do we already have an fcontact record for this person?
$fid = 0;
$r = q(
"SELECT `id` FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
DBA::escape($suggest["url"]),
DBA::escape($suggest["name"]),
DBA::escape($suggest["request"])
);
if (DBA::isResult($r)) {
$fid = $r[0]["id"];
$condition = ['url' => $suggest["url"], 'name' => $suggest["name"], 'request' => $suggest["request"]];
$fcontact = DBA::selectFirst('fcontact', ['id'], $condition);
if (DBA::isResult($fcontact)) {
$fid = $fcontact["id"];
// OK, we do. Do we already have an introduction for this person ?
$r = q(
"SELECT `id` FROM `intro` WHERE `uid` = %d AND `fid` = %d LIMIT 1",
intval($suggest["uid"]),
intval($fid)
);
/*
* The valid result means the friend we're about to send a friend
* suggestion already has them in their contact, which means no further
* action is required.
*
* @see https://github.com/friendica/friendica/pull/3254#discussion_r107315246
*/
if (DBA::isResult($r)) {
// OK, we do. Do we already have an introduction for this person?
if (DBA::exists('intro', ['uid' => $suggest["uid"], 'fid' => $fid])) {
/*
* The valid result means the friend we're about to send a friend
* suggestion already has them in their contact, which means no further
* action is required.
*
* @see https://github.com/friendica/friendica/pull/3254#discussion_r107315246
*/
return false;
}
}
@ -1967,18 +1940,15 @@ class DFRN
DBA::escape($suggest["request"])
);
}
$r = q(
"SELECT `id` FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
DBA::escape($suggest["url"]),
DBA::escape($suggest["name"]),
DBA::escape($suggest["request"])
);
$condition = ['url' => $suggest["url"], 'name' => $suggest["name"], 'request' => $suggest["request"]];
$fcontact = DBA::selectFirst('fcontact', ['id'], $condition);
/*
* If no record in fcontact is found, below INSERT statement will not
* link an introduction to it.
*/
if (!DBA::isResult($r)) {
if (!DBA::isResult($fcontact)) {
// Database record did not get created. Quietly give up.
killme();
}
@ -2646,13 +2616,10 @@ class DFRN
$ev["guid"] = $item["guid"];
$ev["plink"] = $item["plink"];
$r = q(
"SELECT `id` FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
DBA::escape($item["uri"]),
intval($importer["importer_uid"])
);
if (DBA::isResult($r)) {
$ev["id"] = $r[0]["id"];
$condition = ['uri' => $item["uri"], 'uid' => $importer["importer_uid"]];
$event = DBA::selectFirst('event', ['id'], $condition);
if (DBA::isResult($event)) {
$ev["id"] = $event["id"];
}
$event_id = Event::store($ev);
@ -3035,23 +3002,21 @@ class DFRN
return false;
}
$u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
intval($uid)
);
if (!DBA::isResult($u)) {
$user = DBA::selectFirst('user', ['page-flags', 'nickname'], ['uid' => $uid]);
if (!DBA::isResult($user)) {
return false;
}
$community_page = ($u[0]['page-flags'] == Contact::PAGE_COMMUNITY);
$prvgroup = ($u[0]['page-flags'] == Contact::PAGE_PRVGROUP);
$community_page = ($user['page-flags'] == Contact::PAGE_COMMUNITY);
$prvgroup = ($user['page-flags'] == Contact::PAGE_PRVGROUP);
$link = normalise_link(System::baseUrl() . '/profile/' . $u[0]['nickname']);
$link = normalise_link(System::baseUrl() . '/profile/' . $user['nickname']);
/*
* Diaspora uses their own hardwired link URL in @-tags
* instead of the one we supply with webfinger
*/
$dlink = normalise_link(System::baseUrl() . '/u/' . $u[0]['nickname']);
$dlink = normalise_link(System::baseUrl() . '/u/' . $user['nickname']);
$cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) {

View file

@ -970,13 +970,10 @@ class Diaspora
logger("contact id is ".$contact_id." - pcontact id is ".$pcontact_id, LOGGER_DEBUG);
if ($pcontact_id != 0) {
$r = q(
"SELECT `addr` FROM `contact` WHERE `id` = %d AND `addr` != ''",
intval($pcontact_id)
);
$contact = DBA::selectFirst('contact', ['addr'], ['id' => $pcontact_id]);
if (DBA::isResult($r)) {
return strtolower($r[0]["addr"]);
if (DBA::isResult($contact) && !empty($contact["addr"])) {
return strtolower($contact["addr"]);
}
}
@ -1788,12 +1785,7 @@ class Diaspora
DBA::lock('mail');
$r = q(
"SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
DBA::escape($msg_guid),
intval($importer["uid"])
);
if (DBA::isResult($r)) {
if (DBA::exists('mail', ['guid' => $msg_guid, 'uid' => $importer["uid"]])) {
logger("duplicate message already delivered.", LOGGER_DEBUG);
return false;
}
@ -1868,16 +1860,8 @@ class Diaspora
return false;
}
$conversation = null;
$c = q(
"SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
intval($importer["uid"]),
DBA::escape($guid)
);
if ($c)
$conversation = $c[0];
else {
$conversation = DBA::selectFirst('conv', [], ['uid' => $importer["uid"], 'guid' => $guid]);
if (!DBA::isResult($conversation)) {
$r = q(
"INSERT INTO `conv` (`uid`, `guid`, `creator`, `created`, `updated`, `subject`, `recips`)
VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')",
@ -1890,15 +1874,7 @@ class Diaspora
DBA::escape($participants)
);
if ($r) {
$c = q(
"SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
intval($importer["uid"]),
DBA::escape($guid)
);
}
if ($c) {
$conversation = $c[0];
$conversation = DBA::selectFirst('conv', [], ['uid' => $importer["uid"], 'guid' => $guid]);
}
}
if (!$conversation) {
@ -2049,14 +2025,10 @@ class Diaspora
$conversation = null;
$c = q(
"SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
intval($importer["uid"]),
DBA::escape($conversation_guid)
);
if ($c) {
$conversation = $c[0];
} else {
$condition = ['uid' => $importer["uid"], 'guid' => $conversation_guid];
$conversation = DBA::selectFirst('conv', [], $condition);
if (!DBA::isResult($conversation)) {
logger("conversation not available.");
return false;
}
@ -2075,12 +2047,7 @@ class Diaspora
DBA::lock('mail');
$r = q(
"SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
DBA::escape($guid),
intval($importer["uid"])
);
if (DBA::isResult($r)) {
if (DBA::exists('mail', ['guid' => $guid, 'uid' => $importer["uid"]])) {
logger("duplicate message already delivered.", LOGGER_DEBUG);
return false;
}
@ -2363,10 +2330,10 @@ class Diaspora
// If we are now friends, we are sending a share message.
// Normally we needn't to do so, but the first message could have been vanished.
if (in_array($contact["rel"], [Contact::FRIEND])) {
$u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"]));
if ($u) {
$user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]);
if (DBA::isResult($user)) {
logger("Sending share message to author ".$author." - Contact: ".$contact["id"]." - User: ".$importer["uid"], LOGGER_DEBUG);
$ret = self::sendShare($u[0], $contact);
$ret = self::sendShare($user, $contact);
}
}
return true;
@ -2485,10 +2452,10 @@ class Diaspora
intval($contact_record["id"])
);
$u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"]));
if ($u) {
$user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]);
if (DBA::isResult($user)) {
logger("Sending share message (Relation: ".$new_relation.") to author ".$author." - Contact: ".$contact_record["id"]." - User: ".$importer["uid"], LOGGER_DEBUG);
$ret = self::sendShare($u[0], $contact_record);
$ret = self::sendShare($user, $contact_record);
// Send the profile data, maybe it weren't transmitted before
self::sendProfile($importer["uid"], [$contact_record]);
@ -3830,19 +3797,13 @@ class Diaspora
logger("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
// fetch the original signature
$r = q(
"SELECT `signed_text`, `signature`, `signer` FROM `sign` WHERE `iid` = %d LIMIT 1",
intval($item["id"])
);
if (!$r) {
$fields = ['signed_text', 'signature', 'signer'];
$signature = DBA::selectFirst('sign', $fields, ['iid' => $item["id"]]);
if (!DBA::isResult($signature)) {
logger("Couldn't fetch signatur for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
return false;
}
$signature = $r[0];
// Old way - is used by the internal Friendica functions
/// @todo Change all signatur storing functions to the new format
if ($signature['signed_text'] && $signature['signature'] && $signature['signer']) {
@ -3923,17 +3884,11 @@ class Diaspora
{
$myaddr = self::myHandle($owner);
$r = q(
"SELECT * FROM `conv` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($item["convid"]),
intval($item["uid"])
);
if (!DBA::isResult($r)) {
$cnv = DBA::selectFirst('conv', [], ['id' => $item["convid"], 'uid' => $item["uid"]]);
if (!DBA::isResult($cnv)) {
logger("conversation not found.");
return;
}
$cnv = $r[0];
$conv = [
"author" => $cnv["creator"],
@ -4168,12 +4123,12 @@ class Diaspora
return false;
}
$r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($contact['uid']));
if (!DBA::isResult($r)) {
$user = DBA::selectFirst('user', ['prvkey'], ['uid' => $contact["uid"]]);
if (!DBA::isResult($user)) {
return false;
}
$contact["uprvkey"] = $r[0]['prvkey'];
$contact["uprvkey"] = $user['prvkey'];
$item = Item::selectFirst([], ['id' => $post_id]);
if (!DBA::isResult($item)) {

View file

@ -1597,12 +1597,9 @@ class OStatus
}
if (!DBA::isResult($r)) {
$r = q(
"SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
DBA::escape(normalise_link($url))
);
$gcontact = DBA::selectFirst('gcontact', [], ['nurl' => normalise_link($url)]);
if (DBA::isResult($r)) {
$contact = $r[0];
$contact = $gcontact;
$contact["uid"] = -1;
$contact["success_update"] = $contact["updated"];
}
@ -1803,14 +1800,11 @@ class OStatus
$item['follow'] = $contact['alias'];
}
$r = q(
"SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
intval($owner['uid']),
DBA::escape(normalise_link($contact["url"]))
);
$condition = ['uid' => $owner['uid'], 'nurl' => normalise_link($contact["url"])];
$user_contact = DBA::selectFirst('contact', ['id'], $condition);
if (DBA::isResult($r)) {
$connect_id = $r[0]['id'];
if (DBA::isResult($user_contact)) {
$connect_id = $user_contact['id'];
} else {
$connect_id = 0;
}