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

@ -1386,22 +1386,14 @@ class Contact extends BaseObject
// the poll url is more reliable than the profile url, as we may have
// indirect links or webfinger links
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' AND NOT `pending` LIMIT 1",
intval($uid),
DBA::escape($ret['poll']),
DBA::escape(normalise_link($ret['poll'])),
DBA::escape($ret['network'])
);
if (!DBA::isResult($r)) {
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' AND NOT `pending` LIMIT 1",
intval($uid),
DBA::escape(normalise_link($url)),
DBA::escape($ret['network'])
);
$condition = ['uid' => $uid, 'poll' => [$ret['poll'], normalise_link($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
$contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
if (!DBA::isResult($contact)) {
$condition = ['uid' => $uid, 'nurl' => normalise_link($url), 'network' => $ret['network'], 'pending' => false];
$contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
}
if (($ret['network'] === Protocol::DFRN) && !DBA::isResult($r)) {
if (($ret['network'] === Protocol::DFRN) && !DBA::isResult($contact)) {
if ($interactive) {
if (strlen($a->urlpath)) {
$myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
@ -1463,14 +1455,14 @@ class Contact extends BaseObject
$writeable = 1;
}
if (DBA::isResult($r)) {
if (DBA::isResult($contact)) {
// update contact
$new_relation = (($r[0]['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING);
$new_relation = (($contact['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING);
$fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
DBA::update('contact', $fields, ['id' => $r[0]['id']]);
DBA::update('contact', $fields, ['id' => $contact['id']]);
} else {
$new_relation = ((in_array($ret['network'], [Protocol::MAIL])) ? self::FRIEND : self::SHARING);
$new_relation = (in_array($ret['network'], [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
// create contact record
DBA::insert('contact', [
@ -1517,12 +1509,9 @@ class Contact extends BaseObject
Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");
$r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
intval($uid)
);
$owner = User::getOwnerDataById($uid);
if (DBA::isResult($r)) {
if (DBA::isResult($owner)) {
if (in_array($contact['network'], [Protocol::OSTATUS, Protocol::DFRN])) {
// create a follow slap
$item = [];
@ -1533,9 +1522,9 @@ class Contact extends BaseObject
$item['guid'] = '';
$item['tag'] = '';
$item['attach'] = '';
$slap = OStatus::salmon($item, $r[0]);
$slap = OStatus::salmon($item, $owner);
if (!empty($contact['notify'])) {
Salmon::slapper($r[0], $contact['notify'], $slap);
Salmon::slapper($owner, $contact['notify'], $slap);
}
} elseif ($contact['network'] == Protocol::DIASPORA) {
$ret = Diaspora::sendShare($a->user, $contact);
@ -1672,10 +1661,8 @@ class Contact extends BaseObject
}
} elseif (DBA::isResult($user) && in_array($user['page-flags'], [self::PAGE_SOAPBOX, self::PAGE_FREELOVE, self::PAGE_COMMUNITY])) {
q("UPDATE `contact` SET `pending` = 0 WHERE `uid` = %d AND `url` = '%s' AND `pending` LIMIT 1",
intval($importer['uid']),
DBA::escape($url)
);
$condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
DBA::update('contact', ['pending' => false], $condition);
}
}
}
@ -1724,10 +1711,9 @@ class Contact extends BaseObject
*/
// Check for duplicates
$s = q("SELECT `id` FROM `event` WHERE `uid` = %d AND `cid` = %d AND `start` = '%s' AND `type` = '%s' LIMIT 1",
intval($rr['uid']), intval($rr['id']), DBA::escape(DateTimeFormat::utc($nextbd)), DBA::escape('birthday'));
if (DBA::isResult($s)) {
$condition = ['uid' => $rr['uid'], 'cid' => $rr['id'],
'start' => DateTimeFormat::utc($nextbd), 'type' => 'birthday'];
if (DBA::exists('event', $condition)) {
continue;
}
@ -1741,7 +1727,6 @@ class Contact extends BaseObject
intval(0)
);
// update bdyear
q("UPDATE `contact` SET `bdyear` = '%s', `bd` = '%s' WHERE `uid` = %d AND `id` = %d", DBA::escape(substr($nextbd, 0, 4)),
DBA::escape($nextbd), intval($rr['uid']), intval($rr['id'])

View file

@ -550,10 +550,13 @@ class Event extends BaseObject
$fmt = L10n::t('l, F j');
foreach ($event_result as $event) {
$item = Item::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link'], ['id' => $event['itemid']]);
if (DBA::isResult($item)) {
$event = array_merge($event, $item);
if (!DBA::isResult($item)) {
// Using default values when no item had been found
$item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => ''];
}
$event = array_merge($event, $item);
$start = $event['adjust'] ? DateTimeFormat::local($event['start'], 'c') : DateTimeFormat::utc($event['start'], 'c');
$j = $event['adjust'] ? DateTimeFormat::local($event['start'], 'j') : DateTimeFormat::utc($event['start'], 'j');
$day = $event['adjust'] ? DateTimeFormat::local($event['start'], $fmt) : DateTimeFormat::utc($event['start'], $fmt);

View file

@ -98,33 +98,8 @@ class GContact
return;
}
$r = q(
"SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
intval($cid),
intval($uid),
intval($gcid),
intval($zcid)
);
if (!DBA::isResult($r)) {
q(
"INSERT INTO `glink` (`cid`, `uid`, `gcid`, `zcid`, `updated`) VALUES (%d, %d, %d, %d, '%s') ",
intval($cid),
intval($uid),
intval($gcid),
intval($zcid),
DBA::escape(DateTimeFormat::utcNow())
);
} else {
q(
"UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
DBA::escape(DateTimeFormat::utcNow()),
intval($cid),
intval($uid),
intval($gcid),
intval($zcid)
);
}
$condition = ['cid' => $cid, 'uid' => $uid, 'gcid' => $gcid, 'zcid' => $zcid];
DBA::update('glink', ['updated' => DateTimeFormat::utcNow()], $condition, true);
}
/**
@ -175,24 +150,19 @@ class GContact
}
if (!isset($gcontact['network'])) {
$r = q(
"SELECT `network` FROM `contact` WHERE `uid` = 0 AND `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
DBA::escape(normalise_link($gcontact['url'])),
DBA::escape(Protocol::STATUSNET)
);
if (DBA::isResult($r)) {
$gcontact['network'] = $r[0]["network"];
$condition = ["`uid` = 0 AND `nurl` = ? AND `network` != '' AND `network` != ?",
normalise_link($gcontact['url']), Protocol::STATUSNET];
$contact = DBA::selectFirst('contact', ['network'], $condition);
if (DBA::isResult($contact)) {
$gcontact['network'] = $contact["network"];
}
if (($gcontact['network'] == "") || ($gcontact['network'] == Protocol::OSTATUS)) {
$r = q(
"SELECT `network`, `url` FROM `contact` WHERE `uid` = 0 AND `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
DBA::escape($gcontact['url']),
DBA::escape(normalise_link($gcontact['url'])),
DBA::escape(Protocol::STATUSNET)
);
if (DBA::isResult($r)) {
$gcontact['network'] = $r[0]["network"];
$condition = ["`uid` = 0 AND `alias` IN (?, ?) AND `network` != '' AND `network` != ?",
$gcontact['url'], normalise_link($gcontact['url']), Protocol::STATUSNET];
$contact = DBA::selectFirst('contact', ['network'], $condition);
if (DBA::isResult($contact)) {
$gcontact['network'] = $contact["network"];
}
}
}
@ -200,23 +170,20 @@ class GContact
$gcontact['server_url'] = '';
$gcontact['network'] = '';
$x = q(
"SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
DBA::escape(normalise_link($gcontact['url']))
);
if (DBA::isResult($x)) {
if (!isset($gcontact['network']) && ($x[0]["network"] != Protocol::STATUSNET)) {
$gcontact['network'] = $x[0]["network"];
$fields = ['network', 'updated', 'server_url', 'url', 'addr'];
$gcnt = DBA::selectFirst('gcontact', $fields, ['nurl' => normalise_link($gcontact['url'])]);
if (DBA::isResult($gcnt)) {
if (!isset($gcontact['network']) && ($gcnt["network"] != Protocol::STATUSNET)) {
$gcontact['network'] = $gcnt["network"];
}
if ($gcontact['updated'] <= NULL_DATE) {
$gcontact['updated'] = $x[0]["updated"];
$gcontact['updated'] = $gcnt["updated"];
}
if (!isset($gcontact['server_url']) && (normalise_link($x[0]["server_url"]) != normalise_link($x[0]["url"]))) {
$gcontact['server_url'] = $x[0]["server_url"];
if (!isset($gcontact['server_url']) && (normalise_link($gcnt["server_url"]) != normalise_link($gcnt["url"]))) {
$gcontact['server_url'] = $gcnt["server_url"];
}
if (!isset($gcontact['addr'])) {
$gcontact['addr'] = $x[0]["addr"];
$gcontact['addr'] = $gcnt["addr"];
}
}
@ -689,20 +656,17 @@ class GContact
}
DBA::lock('gcontact');
$r = q(
"SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
DBA::escape(normalise_link($contact["url"]))
);
if (DBA::isResult($r)) {
$gcontact_id = $r[0]["id"];
$fields = ['id', 'last_contact', 'last_failure', 'network'];
$gcnt = DBA::selectFirst('gcontact', $fields, ['nurl' => normalise_link($contact["url"])]);
if (DBA::isResult($gcnt)) {
$gcontact_id = $gcnt["id"];
// Update every 90 days
if (in_array($r[0]["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, ""])) {
$last_failure_str = $r[0]["last_failure"];
$last_failure = strtotime($r[0]["last_failure"]);
$last_contact_str = $r[0]["last_contact"];
$last_contact = strtotime($r[0]["last_contact"]);
if (in_array($gcnt["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, ""])) {
$last_failure_str = $gcnt["last_failure"];
$last_failure = strtotime($gcnt["last_failure"]);
$last_contact_str = $gcnt["last_contact"];
$last_contact = strtotime($gcnt["last_contact"]);
$doprobing = (((time() - $last_contact) > (90 * 86400)) && ((time() - $last_failure) > (90 * 86400)));
}
} else {
@ -728,15 +692,11 @@ class GContact
intval($contact["generation"])
);
$r = q(
"SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
DBA::escape(normalise_link($contact["url"]))
);
if (DBA::isResult($r)) {
$gcontact_id = $r[0]["id"];
$doprobing = in_array($r[0]["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, ""]);
$condition = ['nurl' => normalise_link($contact["url"])];
$cnt = DBA::selectFirst('gcontact', ['id', 'network'], $condition, ['order' => ['id']]);
if (DBA::isResult($cnt)) {
$gcontact_id = $cnt["id"];
$doprobing = in_array($cnt["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, ""]);
}
}
DBA::unlock();

View file

@ -111,7 +111,7 @@ class Item extends BaseObject
* @param string $activity activity string
* @return integer Activity index
*/
private static function activityToIndex($activity)
public static function activityToIndex($activity)
{
$index = array_search($activity, self::ACTIVITIES);
@ -2456,6 +2456,12 @@ class Item extends BaseObject
}
}
/**
* This function is only used for the old Friendica app on Android that doesn't like paths with guid
* @param string $guid item guid
* @param int $uid user id
* @return array with id and nick of the item with the given guid
*/
public static function getIdAndNickByGuid($guid, $uid = 0)
{
$nick = "";
@ -2467,28 +2473,28 @@ class Item extends BaseObject
// Does the given user have this item?
if ($uid) {
/// @todo This query has to be abstracted for the "uri-id" changes
$item = DBA::fetchFirst("SELECT `item`.`id`, `user`.`nickname` FROM `item`
INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND `item`.`guid` = ? AND `item`.`uid` = ?", $guid, $uid);
$item = self::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
if (DBA::isResult($item)) {
$id = $item["id"];
$nick = $item["nickname"];
$user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
if (!DBA::isResult($user)) {
return;
}
$id = $item['id'];
$nick = $user['nickname'];
}
}
// Or is it anywhere on the server?
if ($nick == "") {
/// @todo This query has to be abstracted for the "uri-id" changes
$item = DBA::fetchFirst("SELECT `item`.`id`, `user`.`nickname` FROM `item`
INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND NOT `item`.`private` AND `item`.`wall`
AND `item`.`guid` = ?", $guid);
$condition = ["`guid` = ? AND `uid` != 0", $guid];
$item = self::selectFirst(['id', 'uid'], $condition);
if (DBA::isResult($item)) {
$id = $item["id"];
$nick = $item["nickname"];
$user = DBA::selectFirst('user', ['nickname'], ['uid' => $item['uid']]);
if (!DBA::isResult($user)) {
return;
}
$id = $item['id'];
$nick = $user['nickname'];
}
}
return ["nick" => $nick, "id" => $id];
@ -3177,8 +3183,7 @@ class Item extends BaseObject
return;
}
// Using dba::delete at this time could delete the associated item entries
$result = DBA::e("DELETE FROM `thread` WHERE `iid` = ?", $itemid);
$result = DBA::delete('thread', ['iid' => $itemid], ['cascade' => false]);
logger("deleteThread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);

View file

@ -56,13 +56,11 @@ class Mail
if (strlen($replyto)) {
$reply = true;
$r = q("SELECT `convid` FROM `mail` WHERE `uid` = %d AND (`uri` = '%s' OR `parent-uri` = '%s') LIMIT 1",
intval(local_user()),
DBA::escape($replyto),
DBA::escape($replyto)
);
if (DBA::isResult($r)) {
$convid = $r[0]['convid'];
$condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
local_user(), $replyto, $replyto];
$mail = DBA::selectFirst('mail', ['convid'], $condition);
if (DBA::isResult($mail)) {
$convid = $mail['convid'];
}
}

View file

@ -124,12 +124,10 @@ class Profile
// fetch user tags if this isn't the default profile
if (!$pdata['is-default']) {
$x = q(
"SELECT `pub_keywords` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
intval($pdata['profile_uid'])
);
if ($x && count($x)) {
$pdata['pub_keywords'] = $x[0]['pub_keywords'];
$condition = ['uid' => $pdata['profile_uid'], 'is-default' => true];
$profile = DBA::selectFirst('profile', ['pub_keywords'], $condition);
if (DBA::isResult($profile)) {
$pdata['pub_keywords'] = $profile['pub_keywords'];
}
}
@ -641,37 +639,26 @@ class Profile
$bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
$classtoday = '';
$s = DBA::p(
"SELECT `event`.*
FROM `event`
INNER JOIN `item`
ON `item`.`uid` = `event`.`uid`
AND `item`.`parent-uri` = `event`.`uri`
WHERE `event`.`uid` = ?
AND `event`.`type` != 'birthday'
AND `event`.`start` < ?
AND `event`.`start` >= ?
AND `item`.`author-id` = ?
AND (`item`.`verb` = ? OR `item`.`verb` = ?)
AND `item`.`visible`
AND NOT `item`.`deleted`
ORDER BY `event`.`start` ASC",
local_user(),
DateTimeFormat::utc('now + 7 days'),
DateTimeFormat::utc('now - 1 days'),
public_contact(),
ACTIVITY_ATTEND,
ACTIVITY_ATTENDMAYBE
);
$condition = ["`uid` = ? AND `type` != 'birthday' AND `start` < ? AND `start` >= ?",
local_user(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utc('now - 1 days')];
$s = DBA::select('event', [], $condition, ['order' => ['start']]);
$r = [];
if (DBA::isResult($s)) {
$istoday = false;
$total = 0;
while ($rr = DBA::fetch($s)) {
$condition = ['parent-uri' => $rr['uri'], 'uid' => $rr['uid'], 'author-id' => public_contact(),
'activity' => [Item::activityToIndex(ACTIVITY_ATTEND), Item::activityToIndex(ACTIVITY_ATTENDMAYBE)],
'visible' => true, 'deleted' => false];
if (!Item::exists($condition)) {
continue;
}
if (strlen($rr['summary'])) {
$total ++;
$total++;
}
$strt = DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', 'Y-m-d');

View file

@ -493,7 +493,7 @@ class User
$spubkey = $sres['pubkey'];
$insert_result = DBA::insert('user', [
'guid' => generate_user_guid(),
'guid' => System::createGUID(32),
'username' => $username,
'password' => $new_password_encoded,
'email' => $email,