Most queries are now done with the dba functions

This commit is contained in:
Michael 2018-02-22 06:52:58 +00:00
parent 8ad2ebbf47
commit 2897333c28

View file

@ -1048,9 +1048,7 @@ class Item extends BaseObject
// Is it a forum? Then we don't care about the rules from above // Is it a forum? Then we don't care about the rules from above
if (!$update && ($arr["network"] == NETWORK_DFRN) && ($arr["parent-uri"] === $arr["uri"])) { if (!$update && ($arr["network"] == NETWORK_DFRN) && ($arr["parent-uri"] === $arr["uri"])) {
$isforum = q("SELECT `forum` FROM `contact` WHERE `id` = %d AND `forum`", if (dba::exists('contact', ['id' => $arr['contact-id'], 'forum' => true])) {
intval($arr['contact-id']));
if (DBM::is_result($isforum)) {
$update = true; $update = true;
} }
} }
@ -1141,11 +1139,11 @@ class Item extends BaseObject
public static function getGuidById($id) public static function getGuidById($id)
{ {
$r = q("SELECT `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($id)); $item = dba::selectFirst('item', ['guid'], ['id' => $id]);
if (DBM::is_result($r)) { if (DBM::is_result($item)) {
return $r[0]["guid"]; return $item['guid'];
} else { } else {
return ""; return '';
} }
} }
@ -1160,26 +1158,28 @@ class Item extends BaseObject
// Does the given user have this item? // Does the given user have this item?
if ($uid) { if ($uid) {
$r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid` $items = dba::p("SELECT `item`.`id`, `user`.`nickname` FROM `item`
WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 AND `item`.`moderated` = 0 INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
AND `item`.`guid` = '%s' AND `item`.`uid` = %d", dbesc($guid), intval($uid)); WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
if (DBM::is_result($r)) { AND `item`.`guid` = ? AND `item`.`uid` = ?", $guid, $uid);
$id = $r[0]["id"]; if ($item = dba::fetch($items)) {
$nick = $r[0]["nickname"]; $id = $item["id"];
$nick = $item["nickname"];
} }
} }
// Or is it anywhere on the server? // Or is it anywhere on the server?
if ($nick == "") { if ($nick == "") {
$r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid` $items = dba::p("SELECT `item`.`id`, `user`.`nickname` FROM `item`
WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 AND `item`.`moderated` = 0 INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
AND `item`.`private` = 0 AND `item`.`wall` = 1 AND NOT `item`.`private` AND `item`.`wall`
AND `item`.`guid` = '%s'", dbesc($guid)); AND `item`.`guid` = ?", $guid);
if (DBM::is_result($r)) { if ($item = dba::fetch($items)) {
$id = $r[0]["id"]; $id = $item["id"];
$nick = $r[0]["nickname"]; $nick = $item["nickname"];
} }
} }
return ["nick" => $nick, "id" => $id]; return ["nick" => $nick, "id" => $id];
@ -1195,33 +1195,26 @@ class Item extends BaseObject
{ {
$mention = false; $mention = false;
$u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", $user = dba::selectFirst('user', [], ['uid' => $uid]);
intval($uid) if (!DBM::is_result($user)) {
);
if (!DBM::is_result($u)) {
return; return;
} }
$community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false); $community_page = (($user['page-flags'] == PAGE_COMMUNITY) ? true : false);
$prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false); $prvgroup = (($user['page-flags'] == PAGE_PRVGROUP) ? true : false);
$i = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $item = dba::selectFirst('item', [], ['id' => $item_id]);
intval($item_id), if (!DBM::is_result($item)) {
intval($uid)
);
if (!DBM::is_result($i)) {
return; return;
} }
$item = $i[0]; $link = normalise_link(System::baseUrl() . '/profile/' . $user['nickname']);
$link = normalise_link(System::baseUrl() . '/profile/' . $u[0]['nickname']);
/* /*
* Diaspora uses their own hardwired link URL in @-tags * Diaspora uses their own hardwired link URL in @-tags
* instead of the one we supply with webfinger * 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); $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) { if ($cnt) {
@ -1245,7 +1238,7 @@ class Item extends BaseObject
return; return;
} }
$arr = ['item' => $item, 'user' => $u[0]]; $arr = ['item' => $item, 'user' => $user];
Addon::callHooks('tagged', $arr); Addon::callHooks('tagged', $arr);
@ -1263,23 +1256,21 @@ class Item extends BaseObject
} }
// now change this copy of the post to a forum head message and deliver to all the tgroup members // now change this copy of the post to a forum head message and deliver to all the tgroup members
$c = q("SELECT `name`, `url`, `thumb` FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", $self = dba::selectFirst('contact', ['name', 'url', 'thumb'], ['uid' => $uid, 'self' => true]);
intval($u[0]['uid']) if (!DBM::is_result($self)) {
);
if (!DBM::is_result($c)) {
return; return;
} }
// also reset all the privacy bits to the forum default permissions // also reset all the privacy bits to the forum default permissions
$private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0; $private = ($user['allow_cid'] || $user['allow_gid'] || $user['deny_cid'] || $user['deny_gid']) ? 1 : 0;
$forum_mode = (($prvgroup) ? 2 : 1); $forum_mode = ($prvgroup ? 2 : 1);
$fields = ['wall' => true, 'origin' => true, 'forum_mode' => $forum_mode, $fields = ['wall' => true, 'origin' => true, 'forum_mode' => $forum_mode,
'owner-name' => $c[0]['name'], 'owner-link' => $c[0]['url'], 'owner-avatar' => $c[0]['thumb'], 'owner-name' => $self['name'], 'owner-link' => $self['url'], 'owner-avatar' => $self['thumb'],
'private' => $private, 'allow_cid' => $u[0]['allow_cid'], 'allow_gid' => $u[0]['allow_gid'], 'private' => $private, 'allow_cid' => $user['allow_cid'], 'allow_gid' => $user['allow_gid'],
'deny_cid' => $u[0]['deny_cid'], 'deny_gid' => $u[0]['deny_gid']]; 'deny_cid' => $user['deny_cid'], 'deny_gid' => $user['deny_gid']];
dba::update('item', $fields, ['id' => $item_id]); dba::update('item', $fields, ['id' => $item_id]);
self::updateThread($item_id); self::updateThread($item_id);
@ -1317,14 +1308,14 @@ class Item extends BaseObject
$datarray2 = $datarray; $datarray2 = $datarray;
logger('remote-self start - Contact '.$contact['url'].' - '.$contact['remote_self'].' Item '.print_r($datarray, true), LOGGER_DEBUG); logger('remote-self start - Contact '.$contact['url'].' - '.$contact['remote_self'].' Item '.print_r($datarray, true), LOGGER_DEBUG);
if ($contact['remote_self'] == 2) { if ($contact['remote_self'] == 2) {
$r = q("SELECT `id`,`url`,`name`,`thumb` FROM `contact` WHERE `uid` = %d AND `self`", $self = dba::selectFirst('contact', ['id', 'name', 'url', 'thumb'],
intval($contact['uid'])); ['uid' => $contact['uid'], 'self' => true]);
if (DBM::is_result($r)) { if (DBM::is_result($self)) {
$datarray['contact-id'] = $r[0]["id"]; $datarray['contact-id'] = $self["id"];
$datarray['owner-name'] = $r[0]["name"]; $datarray['owner-name'] = $self["name"];
$datarray['owner-link'] = $r[0]["url"]; $datarray['owner-link'] = $self["url"];
$datarray['owner-avatar'] = $r[0]["thumb"]; $datarray['owner-avatar'] = $self["thumb"];
$datarray['author-name'] = $datarray['owner-name']; $datarray['author-name'] = $datarray['owner-name'];
$datarray['author-link'] = $datarray['owner-link']; $datarray['author-link'] = $datarray['owner-link'];
@ -1405,12 +1396,9 @@ class Item extends BaseObject
if ($x) { if ($x) {
$res = substr($i, $x + 1); $res = substr($i, $x + 1);
$i = substr($i, 0, $x); $i = substr($i, 0, $x);
$r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d", $fields = ['data', 'type', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
dbesc($i), $photo = dba::selectFirst('photo', $fields, ['resource-id' => $i, 'scale' => $res, 'uid' => $uid]);
intval($res), if (DBM::is_result($photo)) {
intval($uid)
);
if (DBM::is_result($r)) {
/* /*
* Check to see if we should replace this photo link with an embedded image * Check to see if we should replace this photo link with an embedded image
* 1. No need to do so if the photo is public * 1. No need to do so if the photo is public
@ -1420,21 +1408,21 @@ class Item extends BaseObject
* permissions, regardless of order but first check to see if they're an exact * permissions, regardless of order but first check to see if they're an exact
* match to save some processing overhead. * match to save some processing overhead.
*/ */
if (self::hasPermissions($r[0])) { if (self::hasPermissions($photo)) {
if ($cid) { if ($cid) {
$recips = self::enumeratePermissions($r[0]); $recips = self::enumeratePermissions($photo);
if (in_array($cid, $recips)) { if (in_array($cid, $recips)) {
$replace = true; $replace = true;
} }
} elseif ($item) { } elseif ($item) {
if (self::samePermissions($item, $r[0])) { if (self::samePermissions($item, $photo)) {
$replace = true; $replace = true;
} }
} }
} }
if ($replace) { if ($replace) {
$data = $r[0]['data']; $data = $photo['data'];
$type = $r[0]['type']; $type = $photo['type'];
// If a custom width and height were specified, apply before embedding // If a custom width and height were specified, apply before embedding
if (preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) { if (preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
@ -1623,18 +1611,13 @@ class Item extends BaseObject
} }
} }
/// @TODO: This query seems to be really slow
public static function firstPostDate($uid, $wall = false) public static function firstPostDate($uid, $wall = false)
{ {
$r = q("SELECT `id`, `created` FROM `item` $condition = ['uid' => $uid, 'wall' => $wall, 'deleted' => false, 'visible' => true, 'moderated' => false];
WHERE `uid` = %d AND `wall` = %d AND `deleted` = 0 AND `visible` = 1 AND `moderated` = 0 $params = ['order' => ['created' => false]];
AND `id` = `parent` $thread = dba::selectFirst('thread', ['created'], $condition, $params);
ORDER BY `created` ASC LIMIT 1", if (DBM::is_result($thread)) {
intval($uid), return substr(DateTimeFormat::local($thread['created']), 0, 10);
intval($wall ? 1 : 0)
);
if (DBM::is_result($r)) {
return substr(DateTimeFormat::local($r[0]['created']),0,10);
} }
return false; return false;
} }
@ -1749,6 +1732,7 @@ class Item extends BaseObject
$verbs = "'".dbesc($activity)."'"; $verbs = "'".dbesc($activity)."'";
} }
/// @todo This query is expected to be a performance eater due to the "OR" - it has to be changed totally
$existing_like = q("SELECT `id`, `guid`, `verb` FROM `item` $existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
WHERE `verb` IN ($verbs) WHERE `verb` IN ($verbs)
AND `deleted` = 0 AND `deleted` = 0
@ -1865,16 +1849,16 @@ EOT;
private static function addThread($itemid, $onlyshadow = false) private static function addThread($itemid, $onlyshadow = false)
{ {
$items = q("SELECT `uid`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, $fields = ['uid', 'created', 'edited', 'commented', 'received', 'changed', 'wall', 'private', 'pubmail',
`moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, 'moderated', 'visible', 'spam', 'starred', 'bookmark', 'contact-id',
`deleted`, `origin`, `forum_mode`, `mention`, `network`, `author-id`, `owner-id` 'deleted', 'origin', 'forum_mode', 'mention', 'network', 'author-id', 'owner-id'];
FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid)); $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
$item = dba::selectFirst('item', $fields, $condition);
if (!$items) { if (!DBM::is_result($item)) {
return; return;
} }
$item = $items[0];
$item['iid'] = $itemid; $item['iid'] = $itemid;
if (!$onlyshadow) { if (!$onlyshadow) {
@ -1886,15 +1870,16 @@ EOT;
private static function updateThread($itemid, $setmention = false) private static function updateThread($itemid, $setmention = false)
{ {
$items = q("SELECT `uid`, `guid`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, $fields = ['uid', 'guid', 'title', 'body', 'created', 'edited', 'commented', 'received', 'changed',
`deleted`, `origin`, `forum_mode`, `network`, `rendered-html`, `rendered-hash` FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid)); 'wall', 'private', 'pubmail', 'moderated', 'visible', 'spam', 'starred', 'bookmark', 'contact-id',
'deleted', 'origin', 'forum_mode', 'network', 'rendered-html', 'rendered-hash'];
$condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
if (!DBM::is_result($items)) { $item = dba::selectFirst('item', $fields, $condition);
if (!DBM::is_result($item)) {
return; return;
} }
$item = $items[0];
if ($setmention) { if ($setmention) {
$item["mention"] = 1; $item["mention"] = 1;
} }
@ -1920,11 +1905,9 @@ EOT;
return; return;
} }
$result = dba::update( $fields = ['title' => $item['title'], 'body' => $item['body'],
'item', 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
['title' => $item['title'], 'body' => $item['body'], 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']], $result = dba::update('item', $fields, ['id' => $items['id']]);
['id' => $items['id']]
);
logger("Updating public shadow for post ".$items["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG); logger("Updating public shadow for post ".$items["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
} }
@ -1943,11 +1926,8 @@ EOT;
logger("deleteThread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG); logger("deleteThread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
if ($itemuri != "") { if ($itemuri != "") {
$r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT `deleted` AND NOT (`uid` IN (%d, 0))", $condition = ["`uri` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $itemuri, $item["uid"]];
dbesc($itemuri), if (!dba::exists('item', $condition)) {
intval($item["uid"])
);
if (!DBM::is_result($r)) {
dba::delete('item', ['uri' => $itemuri, 'uid' => 0]); dba::delete('item', ['uri' => $itemuri, 'uid' => 0]);
logger("deleteThread: Deleted shadow for item ".$itemuri, LOGGER_DEBUG); logger("deleteThread: Deleted shadow for item ".$itemuri, LOGGER_DEBUG);
} }