1
1
Fork 0

Improve dba::selectFirst calls

- Fix remaining $r[0] references
- Rename $r to meaningful names
This commit is contained in:
Hypolite Petovan 2018-01-11 03:26:30 -05:00
commit 5fc4927764
29 changed files with 228 additions and 250 deletions

View file

@ -226,7 +226,7 @@ function api_login(App $a)
}
}
if (!$record || !count($record)) {
if (DBM::is_result($record)) {
logger('API_login failure: ' . print_r($_SERVER, true), LOGGER_DEBUG);
header('WWW-Authenticate: Basic realm="Friendica"');
//header('HTTP/1.0 401 Unauthorized');
@ -4870,22 +4870,22 @@ function api_friendica_remoteauth()
// traditional DFRN
$r = dba::selectFirst('contact', [], ['uid' => api_user(), 'nurl' => $c_url]);
$contact = dba::selectFirst('contact', [], ['uid' => api_user(), 'nurl' => $c_url]);
if (!DBM::is_result($r) || ($r['network'] !== NETWORK_DFRN)) {
if (!DBM::is_result($contact) || ($contact['network'] !== NETWORK_DFRN)) {
throw new BadRequestException("Unknown contact");
}
$cid = $r['id'];
$cid = $contact['id'];
$dfrn_id = defaults($r, 'issued-id', $r['dfrn-id']);
$dfrn_id = defaults($contact, 'issued-id', $contact['dfrn-id']);
if ($r['duplex'] && $r['issued-id']) {
$orig_id = $r['issued-id'];
if ($contact['duplex'] && $contact['issued-id']) {
$orig_id = $contact['issued-id'];
$dfrn_id = '1:' . $orig_id;
}
if ($r['duplex'] && $r['dfrn-id']) {
$orig_id = $r['dfrn-id'];
if ($contact['duplex'] && $contact['dfrn-id']) {
$orig_id = $contact['dfrn-id'];
$dfrn_id = '0:' . $orig_id;
}
@ -4901,10 +4901,10 @@ function api_friendica_remoteauth()
intval(time() + 45)
);
logger($r['name'] . ' ' . $sec, LOGGER_DEBUG);
logger($contact['name'] . ' ' . $sec, LOGGER_DEBUG);
$dest = ($url ? '&destination_url=' . $url : '');
goaway(
$r['poll'] . '?dfrn_id=' . $dfrn_id
$contact['poll'] . '?dfrn_id=' . $dfrn_id
. '&dfrn_version=' . DFRN_PROTOCOL_VERSION
. '&type=profile&sec=' . $sec . $dest . $quiet
);

View file

@ -224,14 +224,14 @@ function common_friends_visitor_widget($profile_uid)
if (!$cid) {
if (get_my_url()) {
$r = dba::selectFirst('contact', ['id'],
$contact = dba::selectFirst('contact', ['id'],
['nurl' => normalise_link(get_my_url()), 'uid' => $profile_uid]);
if (DBM::is_result($r)) {
$cid = $r['id'];
if (DBM::is_result($contact)) {
$cid = $contact['id'];
} else {
$r = dba::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(get_my_url())]);
if (DBM::is_result($r)) {
$zcid = $r['id'];
$gcontact = dba::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(get_my_url())]);
if (DBM::is_result($gcontact)) {
$zcid = $gcontact['id'];
}
}
}

View file

@ -968,10 +968,15 @@ function best_link_url($item, &$sparkle, $url = '') {
$clean_url = normalise_link($item['author-link']);
if (local_user()) {
$r = dba::selectFirst('contact', ['id'],
['network' => NETWORK_DFRN, 'uid' => local_user(), 'nurl' => normalise_link($clean_url), 'pending' => false]);
if (DBM::is_result($r)) {
$best_url = 'redir/' . $r['id'];
$condition = [
'network' => NETWORK_DFRN,
'uid' => local_user(),
'nurl' => normalise_link($clean_url),
'pending' => false
];
$contact = dba::selectFirst('contact', ['id'], $condition);
if (DBM::is_result($contact)) {
$best_url = 'redir/' . $contact['id'];
$sparkle = true;
if ($url != '') {
$hostname = get_app()->get_hostname();
@ -1019,11 +1024,12 @@ function item_photo_menu($item) {
$cid = 0;
$network = '';
$rel = 0;
$r = dba::selectFirst('contact', array('id', 'network', 'rel'), array('uid' => local_user(), 'nurl' => normalise_link($item['author-link'])));
if (DBM::is_result($r)) {
$cid = $r['id'];
$network = $r['network'];
$rel = $r['rel'];
$condition = ['uid' => local_user(), 'nurl' => normalise_link($item['author-link'])];
$contact = dba::selectFirst('contact', ['id', 'network', 'rel'], $condition);
if (DBM::is_result($contact)) {
$cid = $contact['id'];
$network = $contact['network'];
$rel = $contact['rel'];
}
if ($sparkle) {

View file

@ -106,8 +106,8 @@ function notification($params)
}
if ($params['type'] == NOTIFY_COMMENT) {
$p = dba::selectFirst('thread', ['ignored'], ['iid' => $parent_id]);
if (DBM::is_result($p) && $p["ignored"]) {
$thread = dba::selectFirst('thread', ['ignored'], ['iid' => $parent_id]);
if (DBM::is_result($thread) && $thread["ignored"]) {
logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
return;
}
@ -128,13 +128,13 @@ function notification($params)
// if it's a post figure out who's post it is.
$p = null;
$item = null;
if ($params['otype'] === 'item' && $parent_id) {
$p = dba::selectFirst('item', [], ['id' => $parent_id]);
$item = dba::selectFirst('item', [], ['id' => $parent_id]);
}
$item_post_type = item_post_type($p);
$item_post_type = item_post_type($item);
// "a post"
$dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
@ -143,7 +143,7 @@ function notification($params)
$item_post_type);
// "George Bull's post"
if ($p) {
if ($item) {
$dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
$itemlink,
@ -152,7 +152,7 @@ function notification($params)
}
// "your post"
if ($p['owner-name'] == $p['author-name'] && $p['wall']) {
if (DBM::is_result($item) && $item['owner-name'] == $item['author-name'] && $item['wall']) {
$dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
'[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
$itemlink,

View file

@ -154,28 +154,27 @@ function profile_load(App $a, $nickname, $profile = 0, $profiledata = array(), $
*
* @param string $nickname nick
* @param int $uid uid
* @param int $profile ID of the profile
* @param int $profile_id ID of the profile
* @returns array
*/
function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0)
function get_profiledata_by_nick($nickname, $uid = 0, $profile_id = 0)
{
if (remote_user() && count($_SESSION['remote'])) {
foreach ($_SESSION['remote'] as $visitor) {
if ($visitor['uid'] == $uid) {
$r = dba::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
if (DBM::is_result($r)) {
$profile = $r['profile-id'];
$contact = dba::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
if (DBM::is_result($contact)) {
$profile_id = $contact['profile-id'];
}
break;
}
}
}
$r = null;
$profile = null;
if ($profile) {
$profile_int = intval($profile);
$r = dba::fetch_first(
if ($profile_id) {
$profile = 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`.*,
@ -185,11 +184,11 @@ function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0)
INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1",
$nickname,
$profile_int
intval($profile_id)
);
}
if (!DBM::is_result($r)) {
$r = dba::fetch_first(
if (!DBM::is_result($profile)) {
$profile = 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`.*,
@ -202,7 +201,7 @@ function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0)
);
}
return $r;
return $profile;
}
/**
@ -215,7 +214,7 @@ function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0)
* @param int $block
* @param boolean $show_connect Show connect link
*
* @return HTML string stuitable for sidebar inclusion
* @return HTML string suitable for sidebar inclusion
*
* @note Returns empty string if passed $profile is wrong type or not populated
*

View file

@ -68,11 +68,7 @@ function send_message($recipient = 0, $body = '', $subject = '', $replyto = '')
'created' => datetime_convert(), 'updated' => datetime_convert(),
'subject' => $subject, 'recips' => $handles);
dba::insert('conv', $fields);
$r = dba::selectFirst('conv', ['id'], ['guid' => $conv_guid, 'uid' => local_user()]);
if (DBM::is_result($r)) {
$convid = $r['id'];
}
$convid = dba::lastInsertId();
}
if (!$convid) {
@ -103,15 +99,7 @@ function send_message($recipient = 0, $body = '', $subject = '', $replyto = '')
dbesc($replyto),
datetime_convert()
);
$r = q("SELECT * FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
dbesc($uri),
intval(local_user())
);
if (DBM::is_result($r)) {
$post_id = $r[0]['id'];
}
$post_id = dba::lastInsertId();
/**
*
@ -187,15 +175,13 @@ function send_wallmessage($recipient = '', $body = '', $subject = '', $replyto =
'created' => datetime_convert(), 'updated' => datetime_convert(),
'subject' => $subject, 'recips' => $handles);
dba::insert('conv', $fields);
$r = dba::selectFirst('conv', ['id'], ['guid' => $conv_guid, 'uid' => $recipient['uid']]);
if (!DBM::is_result($r)) {
$convid = dba::lastInsertId();
if (!$convid) {
logger('send message: conversation not found.');
return -4;
}
$convid = $r['id'];
q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
`contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",

View file

@ -94,9 +94,9 @@ function nav_info(App $a)
$nav['usermenu'][] = array('notes/', t('Personal notes'), '', t('Your personal notes'));
// user info
$r = dba::selectFirst('contact', ['micro'], ['uid' => $a->user['uid'], 'self' => true]);
$contact = dba::selectFirst('contact', ['micro'], ['uid' => $a->user['uid'], 'self' => true]);
$userinfo = array(
'icon' => (DBM::is_result($r) ? $a->remove_baseurl($r['micro']) : 'images/person-48.jpg'),
'icon' => (DBM::is_result($contact) ? $a->remove_baseurl($contact['micro']) : 'images/person-48.jpg'),
'name' => $a->user['username'],
);
} else {

View file

@ -33,10 +33,10 @@ function ref_session_read($id)
return '';
}
$r = dba::selectFirst('session', ['data'], ['sid' => $id]);
if (DBM::is_result($r)) {
$session = dba::selectFirst('session', ['data'], ['sid' => $id]);
if (DBM::is_result($session)) {
$session_exists = true;
return $r['data'];
return $session['data'];
} else {
logger("no data for session $id", LOGGER_TRACE);
}