1
1
Fork 0

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
commit 7f3fb34c24
21 changed files with 275 additions and 501 deletions

View file

@ -631,37 +631,37 @@ function api_get_user(App $a, $contact_id = null)
// if the contact wasn't found, fetch it from the contacts with uid = 0
if (!DBA::isResult($uinfo)) {
$r = [];
if ($url != "") {
$r = q("SELECT * FROM `contact` WHERE `uid` = 0 AND `nurl` = '%s' LIMIT 1", DBA::escape(normalise_link($url)));
if ($url == "") {
throw new BadRequestException("User not found.");
}
if (DBA::isResult($r)) {
$network_name = ContactSelector::networkToName($r[0]['network'], $r[0]['url']);
$contact = DBA::selectFirst('contact', [], ['uid' => 0, 'nurl' => normalise_link($url)]);
if (DBA::isResult($contact)) {
$network_name = ContactSelector::networkToName($contact['network'], $contact['url']);
// If no nick where given, extract it from the address
if (($r[0]['nick'] == "") || ($r[0]['name'] == $r[0]['nick'])) {
$r[0]['nick'] = api_get_nick($r[0]["url"]);
if (($contact['nick'] == "") || ($contact['name'] == $contact['nick'])) {
$contact['nick'] = api_get_nick($contact["url"]);
}
$ret = [
'id' => $r[0]["id"],
'id_str' => (string) $r[0]["id"],
'name' => $r[0]["name"],
'screen_name' => (($r[0]['nick']) ? $r[0]['nick'] : $r[0]['name']),
'location' => ($r[0]["location"] != "") ? $r[0]["location"] : $network_name,
'description' => $r[0]["about"],
'profile_image_url' => $r[0]["micro"],
'profile_image_url_https' => $r[0]["micro"],
'profile_image_url_profile_size' => $r[0]["thumb"],
'profile_image_url_large' => $r[0]["photo"],
'url' => $r[0]["url"],
'id' => $contact["id"],
'id_str' => (string) $contact["id"],
'name' => $contact["name"],
'screen_name' => (($contact['nick']) ? $contact['nick'] : $contact['name']),
'location' => ($contact["location"] != "") ? $contact["location"] : $network_name,
'description' => $contact["about"],
'profile_image_url' => $contact["micro"],
'profile_image_url_https' => $contact["micro"],
'profile_image_url_profile_size' => $contact["thumb"],
'profile_image_url_large' => $contact["photo"],
'url' => $contact["url"],
'protected' => false,
'followers_count' => 0,
'friends_count' => 0,
'listed_count' => 0,
'created_at' => api_date($r[0]["created"]),
'created_at' => api_date($contact["created"]),
'favourites_count' => 0,
'utc_offset' => 0,
'time_zone' => 'UTC',
@ -676,12 +676,12 @@ function api_get_user(App $a, $contact_id = null)
'follow_request_sent' => false,
'statusnet_blocking' => false,
'notifications' => false,
'statusnet_profile_url' => $r[0]["url"],
'statusnet_profile_url' => $contact["url"],
'uid' => 0,
'cid' => Contact::getIdForURL($r[0]["url"], api_user(), true),
'pid' => Contact::getIdForURL($r[0]["url"], 0, true),
'cid' => Contact::getIdForURL($contact["url"], api_user(), true),
'pid' => Contact::getIdForURL($contact["url"], 0, true),
'self' => 0,
'network' => $r[0]["network"],
'network' => $contact["network"],
];
return $ret;
@ -4340,12 +4340,8 @@ function check_acl_input($acl_string)
foreach ($cid_array as $cid) {
$cid = str_replace("<", "", $cid);
$cid = str_replace(">", "", $cid);
$contact = q(
"SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d",
intval($cid),
intval(api_user())
);
$contact_not_found |= !DBA::isResult($contact);
$condition = ['id' => $cid, 'uid' => api_user()];
$contact_not_found |= !DBA::exists('contact', $condition);
}
return $contact_not_found;
}
@ -4527,7 +4523,7 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f
{
// get data about the api authenticated user
$uri = Item::newURI(intval(api_user()));
$owner_record = q("SELECT * FROM `contact` WHERE `uid`= %d AND `self` LIMIT 1", intval(api_user()));
$owner_record = DBA::selectFirst('contact', [], ['uid' => api_user(), 'self' => true]);
$arr = [];
$arr['guid'] = System::createGUID(32);
@ -4537,13 +4533,13 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f
$arr['type'] = 'photo';
$arr['wall'] = 1;
$arr['resource-id'] = $hash;
$arr['contact-id'] = $owner_record[0]['id'];
$arr['owner-name'] = $owner_record[0]['name'];
$arr['owner-link'] = $owner_record[0]['url'];
$arr['owner-avatar'] = $owner_record[0]['thumb'];
$arr['author-name'] = $owner_record[0]['name'];
$arr['author-link'] = $owner_record[0]['url'];
$arr['author-avatar'] = $owner_record[0]['thumb'];
$arr['contact-id'] = $owner_record['id'];
$arr['owner-name'] = $owner_record['name'];
$arr['owner-link'] = $owner_record['url'];
$arr['owner-avatar'] = $owner_record['thumb'];
$arr['author-name'] = $owner_record['name'];
$arr['author-link'] = $owner_record['url'];
$arr['author-avatar'] = $owner_record['thumb'];
$arr['title'] = "";
$arr['allow_cid'] = $allow_cid;
$arr['allow_gid'] = $allow_gid;
@ -4559,7 +4555,7 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f
];
// adds link to the thumbnail scale photo
$arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $owner_record[0]['nick'] . '/image/' . $hash . ']'
$arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $owner_record['nick'] . '/image/' . $hash . ']'
. '[img]' . System::baseUrl() . '/photo/' . $hash . '-' . "2" . '.'. $typetoext[$filetype] . '[/img]'
. '[/url]';
@ -5830,11 +5826,11 @@ function api_friendica_profile_show($type)
}
// return settings, authenticated user and profiles data
$self = q("SELECT `nurl` FROM `contact` WHERE `uid`= %d AND `self` LIMIT 1", intval(api_user()));
$self = DBA::selectFirst('contact', ['nurl'], ['uid' => api_user(), 'self' => true]);
$result = ['multi_profiles' => $multi_profiles ? true : false,
'global_dir' => $directory,
'friendica_owner' => api_get_user($a, $self[0]['nurl']),
'friendica_owner' => api_get_user($a, $self['nurl']),
'profiles' => $profiles];
return api_format_data("friendica_profiles", $type, ['$result' => $result]);
}

View file

@ -125,14 +125,9 @@ function notification($params)
// Check to see if there was already a tag notify or comment notify for this post.
// If so don't create a second notification
$p = q("SELECT `id` FROM `notify` WHERE `type` IN (%d, %d, %d) AND `link` = '%s' AND `uid` = %d LIMIT 1",
intval(NOTIFY_TAGSELF),
intval(NOTIFY_COMMENT),
intval(NOTIFY_SHARE),
DBA::escape($params['link']),
intval($params['uid'])
);
if ($p && count($p)) {
$condition = ['type' => [NOTIFY_TAGSELF, NOTIFY_COMMENT, NOTIFY_SHARE],
'link' => $params['link'], 'uid' => $params['uid']];
if (DBA::exists('notify', $condition)) {
L10n::popLang();
return;
}
@ -446,9 +441,7 @@ function notification($params)
do {
$dups = false;
$hash = random_string();
$r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
DBA::escape($hash));
if (DBA::isResult($r)) {
if (DBA::exists('notify', ['hash' => $hash])) {
$dups = true;
}
} while ($dups == true);
@ -478,33 +471,14 @@ function notification($params)
}
// create notification entry in DB
q("INSERT INTO `notify` (`hash`, `name`, `url`, `photo`, `date`, `uid`, `link`, `iid`, `parent`, `type`, `verb`, `otype`, `name_cache`)
values('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s')",
DBA::escape($datarray['hash']),
DBA::escape($datarray['name']),
DBA::escape($datarray['url']),
DBA::escape($datarray['photo']),
DBA::escape($datarray['date']),
intval($datarray['uid']),
DBA::escape($datarray['link']),
intval($datarray['iid']),
intval($datarray['parent']),
intval($datarray['type']),
DBA::escape($datarray['verb']),
DBA::escape($datarray['otype']),
DBA::escape($datarray["name_cache"])
);
$fields = ['hash' => $datarray['hash'], 'name' => $datarray['name'], 'url' => $datarray['url'],
'photo' => $datarray['photo'], 'date' => $datarray['date'], 'uid' => $datarray['uid'],
'link' => $datarray['link'], 'iid' => $datarray['iid'], 'parent' => $datarray['parent'],
'type' => $datarray['type'], 'verb' => $datarray['verb'], 'otype' => $datarray['otype'],
'name_cache' => $datarray["name_cache"]];
DBA::insert('notify', $fields);
$r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1",
DBA::escape($hash),
intval($params['uid'])
);
if ($r) {
$notify_id = $r[0]['id'];
} else {
L10n::popLang();
return False;
}
$notify_id = DBA::lastInsertId();
// we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
// After we've stored everything, look again to see if there are any duplicates and if so remove them
@ -529,12 +503,10 @@ function notification($params)
$itemlink = System::baseUrl().'/notify/view/'.$notify_id;
$msg = replace_macros($epreamble, ['$itemlink' => $itemlink]);
$msg_cache = format_notification_message($datarray['name_cache'], strip_tags(BBCode::convert($msg)));
q("UPDATE `notify` SET `msg` = '%s', `msg_cache` = '%s' WHERE `id` = %d AND `uid` = %d",
DBA::escape($msg),
DBA::escape($msg_cache),
intval($notify_id),
intval($params['uid'])
);
$fields = ['msg' => $msg, 'msg_cache' => $msg_cache];
$condition = ['id' => $notify_id, 'uid' => $params['uid']];
DBA::update('notify', $fields, $condition);
}
// send email notification if notification preferences permit
@ -548,21 +520,12 @@ function notification($params)
$id_for_parent = $params['parent']."@".$hostname;
// Is this the first email notification for this parent item and user?
$r = q("SELECT `id` FROM `notify-threads` WHERE `master-parent-item` = %d AND `receiver-uid` = %d LIMIT 1",
intval($params['parent']),
intval($params['uid']));
// If so, create the record of it and use a message-id smtp header.
if (!$r) {
if (!DBA::exists('notify-threads', ['master-parent-item' => $params['parent'], 'receiver-uid' => $params['uid']])) {
logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
q("INSERT INTO `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
values(%d, %d, %d, %d)",
intval($notify_id),
intval($params['parent']),
intval($params['uid']),
0);
$fields = ['notify-id' => $notify_id, 'master-parent-item' => $params['parent'],
'receiver-uid' => $params['uid'], 'parent-item' => 0];
DBA::insert('notify-threads', $fields);
$additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
$log_msg = "include/enotify: No previous notification found for this parent:\n".
@ -571,7 +534,7 @@ function notification($params)
} else {
// If not, just "follow" the thread.
$additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
logger("There's already a notification for this parent:\n".print_r($r, true), LOGGER_DEBUG);
logger("There's already a notification for this parent.", LOGGER_DEBUG);
}
}

View file

@ -288,25 +288,30 @@ function consume_feed($xml, array $importer, array $contact, &$hub, $datedir = 0
function subscribe_to_hub($url, array $importer, array $contact, $hubmode = 'subscribe')
{
$a = BaseObject::getApp();
$r = null;
if (!empty($importer)) {
$r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
intval($importer['uid'])
);
}
/*
* Diaspora has different message-ids in feeds than they do
* through the direct Diaspora protocol. If we try and use
* the feed, we'll get duplicates. So don't.
*/
if ((!DBA::isResult($r)) || $contact['network'] === Protocol::DIASPORA) {
if ($contact['network'] === Protocol::DIASPORA) {
return;
}
$push_url = System::baseUrl() . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
// Without an importer we don't have a user id - so we quit
if (empty($importer)) {
return;
}
$a = BaseObject::getApp();
$user = DBA::selectFirst('user', ['nickname'], ['uid' => $importer['uid']]);
// No user, no nickname, we quit
if (!DBA::isResult($user)) {
return;
}
$push_url = System::baseUrl() . '/pubsub/' . $user['nickname'] . '/' . $contact['id'];
// Use a single verify token, even if multiple hubs
$verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());

View file

@ -299,11 +299,7 @@ function permissions_sql($owner_id, $remote_verified = false, $groups = null)
*/
if (!$remote_verified) {
$r = q("SELECT id FROM contact WHERE id = %d AND uid = %d AND blocked = 0 LIMIT 1",
intval($remote_user),
intval($owner_id)
);
if (DBA::isResult($r)) {
if (DBA::exists('contact', ['id' => $remote_user, 'uid' => $owner_id, 'blocked' => false])) {
$remote_verified = true;
$groups = Group::getIdsByContactId($remote_user);
}

View file

@ -1459,26 +1459,6 @@ function return_bytes($size_str) {
}
}
/**
* @return string
*/
function generate_user_guid() {
$found = true;
do {
$guid = System::createGUID(32);
$x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
DBA::escape($guid)
);
if (!DBA::isResult($x)) {
$found = false;
}
} while ($found == true);
return $guid;
}
/**
* @param string $s
* @param boolean $strip_padding