Remove direct item sql queries
authorMichael <heluecht@pirati.ca>
Sun, 17 Jun 2018 06:20:47 +0000 (06:20 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 17 Jun 2018 06:20:47 +0000 (06:20 +0000)
advancedcontentfilter/advancedcontentfilter.php
mailstream/mailstream.php
pumpio/pumpio.php
statusnet/statusnet.php
twitter/twitter.php
windowsphonepush/windowsphonepush.php

index dd04e76..4394604 100644 (file)
@@ -405,7 +405,7 @@ function advancedcontentfilter_get_variables_guid(ServerRequestInterface $reques
 
        $condition = ["`guid` = ? AND (`uid` = ? OR `uid` = 0)", $args['guid'], local_user()];
        $params = ['order' => ['uid' => true]];
-       $item = Item::selectFirst(local_user(), [], $condition, $params);
+       $item = Item::selectFirst(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params);
 
        if (!\Friendica\Database\DBM::is_result($item)) {
                throw new HTTPException\NotFoundException(L10n::t('Unknown post with guid: %s', $args['guid']));
index 6c057b2..a68e0b3 100644 (file)
@@ -13,6 +13,7 @@ use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
 use Friendica\Database\DBM;
 use Friendica\Util\Network;
+use Friendica\Model\Item;
 
 function mailstream_install() {
        Addon::registerHook('addon_settings', 'addon/mailstream/mailstream.php', 'mailstream_addon_settings');
@@ -329,14 +330,13 @@ function mailstream_cron($a, $b) {
        // send the email itself before cron jumps in.  Only if
        // mailstream_post_remote_hook fails for some reason will this get
        // used, and in that case it's worth holding off a bit anyway.
-       $ms_item_ids = q("SELECT `mailstream_item`.`message-id`, `mailstream_item`.`uri`, `item`.`id` FROM `mailstream_item` JOIN `item` ON (`mailstream_item`.`uid` = `item`.`uid` AND `mailstream_item`.`uri` = `item`.`uri` AND `mailstream_item`.`contact-id` = `item`.`contact-id`) WHERE `mailstream_item`.`completed` IS NULL AND `mailstream_item`.`created` < DATE_SUB(NOW(), INTERVAL 1 HOUR) AND `item`.`visible` = 1 ORDER BY `mailstream_item`.`created` LIMIT 100");
+       $ms_item_ids = q("SELECT `mailstream_item`.`message-id`, `mailstream_item`.`uri`, `mailstream_item`.`uid`, `item`.`id` FROM `mailstream_item` JOIN `item` ON (`mailstream_item`.`uid` = `item`.`uid` AND `mailstream_item`.`uri` = `item`.`uri` AND `mailstream_item`.`contact-id` = `item`.`contact-id`) WHERE `mailstream_item`.`completed` IS NULL AND `mailstream_item`.`created` < DATE_SUB(NOW(), INTERVAL 1 HOUR) AND `item`.`visible` = 1 ORDER BY `mailstream_item`.`created` LIMIT 100");
        logger('mailstream_cron processing ' . count($ms_item_ids) . ' items', LOGGER_DEBUG);
        foreach ($ms_item_ids as $ms_item_id) {
                if (!$ms_item_id['message-id'] || !strlen($ms_item_id['message-id'])) {
                        logger('mailstream_cron: Item ' . $ms_item_id['id'] . ' URI ' . $ms_item_id['uri'] . ' has no message-id', LOGGER_NORMAL);
                }
-               $items = q('SELECT * FROM `item` WHERE `id` = %d', $ms_item_id['id']);
-               $item = $items[0];
+               $item = Item::selectFirst($ms_item_id['uid'], Item::DISPLAY_FIELDLIST, ['id' => $ms_item_id['id']]);
                $users = q("SELECT * FROM `user` WHERE `uid` = %d", intval($item['uid']));
                $user = $users[0];
                if ($user && $item) {
index cf65922..d57f148 100644 (file)
@@ -1217,14 +1217,6 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
                                                        DateTimeFormat::utc($post->object->created),
                                                        $post->links->self->href).
                                                $postarray['body']."[/share]";
-
-                       /*
-                       $postarray['body'] = "[share author='".$share_author.
-                                       "' profile='".$post->object->author->url.
-                                       "' avatar='".$post->object->author->image->url.
-                                       "' posted='".DateTimeFormat::convert($post->object->created, 'UTC', 'UTC', ).
-                                       "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
-                       */
                } else {
                        // Let shares look like wall-to-wall posts
                        $postarray['author-name'] = $post->object->author->displayName;
@@ -1249,59 +1241,6 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
 
                if ($threadcompletion)
                        pumpio_fetchallcomments($a, $uid, $postarray['parent-uri']);
-
-               $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
-                               intval($uid)
-                       );
-
-               if(!count($user))
-                       return $top_item;
-
-               $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
-
-               if (link_compare($own_id, $postarray['author-link']))
-                       return $top_item;
-
-               if (!function_exists("check_item_notification")) {
-                       $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
-                                       dbesc($postarray['parent-uri']),
-                                       intval($uid)
-                                       );
-
-                       if(count($myconv)) {
-
-                               foreach($myconv as $conv) {
-                                       // now if we find a match, it means we're in this conversation
-
-                                       if(!link_compare($conv['author-link'],$importer_url) && !link_compare($conv['author-link'],$own_id))
-                                               continue;
-
-                                       require_once('include/enotify.php');
-
-                                       $conv_parent = $conv['parent'];
-
-                                       notification([
-                                               'type'         => NOTIFY_COMMENT,
-                                               'notify_flags' => $user[0]['notify-flags'],
-                                               'language'     => $user[0]['language'],
-                                               'to_name'      => $user[0]['username'],
-                                               'to_email'     => $user[0]['email'],
-                                               'uid'          => $user[0]['uid'],
-                                               'item'         => $postarray,
-                                               'link'         => $a->get_baseurl().'/display/'.urlencode(Item::getGuidById($top_item)),
-                                               'source_name'  => $postarray['author-name'],
-                                               'source_link'  => $postarray['author-link'],
-                                               'source_photo' => $postarray['author-avatar'],
-                                               'verb'         => ACTIVITY_POST,
-                                               'otype'        => 'item',
-                                               'parent'       => $conv_parent,
-                                               ]);
-
-                                       // only send one notification
-                                       break;
-                               }
-                       }
-               }
        }
 
        return $top_item;
index a297bae..5663911 100644 (file)
@@ -461,21 +461,15 @@ function statusnet_post_hook(App $a, &$b)
                        return;
                }
 
-               $r = q("SELECT `item`.`author-link`, `item`.`uri`, `contact`.`nick` AS contact_nick
-                       FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                       WHERE `item`.`uri` = '%s' AND `item`.`uid` = %d LIMIT 1", dbesc($b["thr-parent"]), intval($b["uid"]));
-
-               if (!count($r)) {
+               $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
+               $orig_post = Item::selectFirst($b["uid"], ['author-link', 'uri'], $condition);
+               if (!count($orig_post)) {
                        logger("statusnet_post_hook: no parent found " . $b["thr-parent"]);
                        return;
                } else {
                        $iscomment = true;
-                       $orig_post = $r[0];
                }
 
-               //$nickname = "@[url=".$orig_post["author-link"]."]".$orig_post["contact_nick"]."[/url]";
-               //$nicknameplain = "@".$orig_post["contact_nick"];
-
                $nick = preg_replace("=https?://(.*)/(.*)=ism", "$2", $orig_post["author-link"]);
 
                $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nick . "[/url]";
@@ -688,17 +682,9 @@ function statusnet_prepare_body(App $a, &$b)
                $item = $b["item"];
                $item["plink"] = $a->get_baseurl() . "/display/" . $a->user["nickname"] . "/" . $item["parent"];
 
-               $r = q("SELECT `item`.`author-link`, `item`.`uri`, `contact`.`nick` AS contact_nick
-                        FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                        WHERE `item`.`uri` = '%s' AND `item`.`uid` = %d LIMIT 1",
-                        dbesc($item["thr-parent"]),
-                        intval(local_user()));
-
-               if (count($r)) {
-                       $orig_post = $r[0];
-                       //$nickname = "@[url=".$orig_post["author-link"]."]".$orig_post["contact_nick"]."[/url]";
-                       //$nicknameplain = "@".$orig_post["contact_nick"];
-
+               $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
+               $orig_post = Item::selectFirst(local_user(), ['author-link', 'uri'], $condition);
+               if (count($orig_post)) {
                        $nick = preg_replace("=https?://(.*)/(.*)=ism", "$2", $orig_post["author-link"]);
 
                        $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nick . "[/url]";
@@ -1214,75 +1200,6 @@ function statusnet_createpost(App $a, $uid, $post, $self, $create_user, $only_ex
        return $postarray;
 }
 
-function statusnet_checknotification(App $a, $uid, $own_url, $top_item, $postarray)
-{
-       // This function necer worked and need cleanup
-       $user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
-                       intval($uid)
-       );
-
-       if (!count($user)) {
-               return;
-       }
-
-       // Is it me?
-       if (link_compare($user[0]["url"], $postarray['author-link'])) {
-               return;
-       }
-
-       $own_user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
-                       intval($uid),
-                       dbesc($own_url)
-       );
-
-       if (!count($own_user)) {
-               return;
-       }
-
-       // Is it me from GNU Social?
-       if (link_compare($own_user[0]["url"], $postarray['author-link'])) {
-               return;
-       }
-
-       $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
-                       dbesc($postarray['parent-uri']),
-                       intval($uid)
-       );
-
-       if (count($myconv)) {
-               foreach ($myconv as $conv) {
-                       // now if we find a match, it means we're in this conversation
-                       if (!link_compare($conv['author-link'], $user[0]["url"]) && !link_compare($conv['author-link'], $own_user[0]["url"])) {
-                               continue;
-                       }
-
-                       require_once 'include/enotify.php';
-
-                       $conv_parent = $conv['parent'];
-
-                       notification([
-                               'type' => NOTIFY_COMMENT,
-                               'notify_flags' => $user[0]['notify-flags'],
-                               'language' => $user[0]['language'],
-                               'to_name' => $user[0]['username'],
-                               'to_email' => $user[0]['email'],
-                               'uid' => $user[0]['uid'],
-                               'item' => $postarray,
-                               'link' => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($top_item)),
-                               'source_name' => $postarray['author-name'],
-                               'source_link' => $postarray['author-link'],
-                               'source_photo' => $postarray['author-avatar'],
-                               'verb' => ACTIVITY_POST,
-                               'otype' => 'item',
-                               'parent' => $conv_parent,
-                       ]);
-
-                       // only send one notification
-                       break;
-               }
-       }
-}
-
 function statusnet_fetchhometimeline(App $a, $uid, $mode = 1)
 {
        $conversations = [];
@@ -1395,10 +1312,6 @@ function statusnet_fetchhometimeline(App $a, $uid, $mode = 1)
                                        $postarray["id"] = $item;
 
                                        logger('statusnet_fetchhometimeline: User ' . $self["nick"] . ' posted home timeline item ' . $item);
-
-                                       if ($item && !function_exists("check_item_notification")) {
-                                               statusnet_checknotification($a, $uid, $nick, $item, $postarray);
-                                       }
                                }
                        }
                }
@@ -1447,42 +1360,8 @@ function statusnet_fetchhometimeline(App $a, $uid, $mode = 1)
                                }
 
                                $item = Item::insert($postarray);
-                               $postarray["id"] = $item;
 
                                logger('statusnet_fetchhometimeline: User ' . $self["nick"] . ' posted mention timeline item ' . $item);
-
-                               if ($item && function_exists("check_item_notification")) {
-                                       check_item_notification($item, $uid, NOTIFY_TAGSELF);
-                               }
-                       }
-
-                       $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
-                               dbesc($postarray['uri']),
-                               intval($uid)
-                       );
-                       if (count($r)) {
-                               $item = $r[0]['id'];
-                               $parent_id = $r[0]['parent'];
-                       }
-
-                       if (($item != 0) && !function_exists("check_item_notification")) {
-                               require_once 'include/enotify.php';
-                               notification([
-                                       'type'         => NOTIFY_TAGSELF,
-                                       'notify_flags' => $u[0]['notify-flags'],
-                                       'language'     => $u[0]['language'],
-                                       'to_name'      => $u[0]['username'],
-                                       'to_email'     => $u[0]['email'],
-                                       'uid'          => $u[0]['uid'],
-                                       'item'         => $postarray,
-                                       'link'         => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($item)),
-                                       'source_name'  => $postarray['author-name'],
-                                       'source_link'  => $postarray['author-link'],
-                                       'source_photo' => $postarray['author-avatar'],
-                                       'verb'         => ACTIVITY_TAG,
-                                       'otype'        => 'item',
-                                       'parent'       => $parent_id,
-                               ]);
                        }
                }
        }
@@ -1518,10 +1397,6 @@ function statusnet_complete_conversation(App $a, $uid, $self, $create_user, $nic
                        $postarray["id"] = $item;
 
                        logger('statusnet_complete_conversation: User ' . $self["nick"] . ' posted home timeline item ' . $item);
-
-                       if ($item && !function_exists("check_item_notification")) {
-                               statusnet_checknotification($a, $uid, $nick, $item, $postarray);
-                       }
                }
        }
 }
index e69ebb8..0b8f5c3 100644 (file)
@@ -454,16 +454,13 @@ function twitter_post_hook(App $a, &$b)
                        return;
                }
 
-               $r = q("SELECT * FROM item WHERE item.uri = '%s' AND item.uid = %d LIMIT 1",
-                       dbesc($b["thr-parent"]),
-                       intval($b["uid"]));
-
-               if (!DBM::is_result($r)) {
+               $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
+               $orig_post = Item::selectFirst($b["uid"], [], $condition);
+               if (!DBM::is_result($orig_post)) {
                        logger("twitter_post_hook: no parent found " . $b["thr-parent"]);
                        return;
                } else {
                        $iscomment = true;
-                       $orig_post = $r[0];
                }
 
 
@@ -761,13 +758,9 @@ function twitter_prepare_body(App $a, &$b)
                $item = $b["item"];
                $item["plink"] = $a->get_baseurl() . "/display/" . $a->user["nickname"] . "/" . $item["parent"];
 
-               $r = q("SELECT `author-link` FROM item WHERE item.uri = '%s' AND item.uid = %d LIMIT 1",
-                       dbesc($item["thr-parent"]),
-                       intval(local_user()));
-
-               if (DBM::is_result($r)) {
-                       $orig_post = $r[0];
-
+               $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
+               $orig_post = Item::selectFirst(local_user(), ['author-link'], $condition);
+               if (DBM::is_result($orig_post)) {
                        $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
                        $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
                        $nicknameplain = "@" . $nicknameplain;
@@ -1493,75 +1486,6 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
        return $postarray;
 }
 
-function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray)
-{
-       /// TODO: this whole function doesn't seem to work. Needs complete check
-       $user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
-               intval($uid)
-       );
-
-       if (!DBM::is_result($user)) {
-               return;
-       }
-
-       // Is it me?
-       if (link_compare($user[0]["url"], $postarray['author-link'])) {
-               return;
-       }
-
-       $own_user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
-               intval($uid),
-               dbesc("twitter::".$own_id)
-       );
-
-       if (!DBM::is_result($own_user)) {
-               return;
-       }
-
-       // Is it me from twitter?
-       if (link_compare($own_user[0]["url"], $postarray['author-link'])) {
-               return;
-       }
-
-       $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
-               dbesc($postarray['parent-uri']),
-               intval($uid)
-       );
-
-       if (DBM::is_result($myconv)) {
-               foreach ($myconv as $conv) {
-                       // now if we find a match, it means we're in this conversation
-                       if (!link_compare($conv['author-link'], $user[0]["url"]) && !link_compare($conv['author-link'], $own_user[0]["url"])) {
-                               continue;
-                       }
-
-                       require_once 'include/enotify.php';
-
-                       $conv_parent = $conv['parent'];
-
-                       notification([
-                               'type' => NOTIFY_COMMENT,
-                               'notify_flags' => $user[0]['notify-flags'],
-                               'language' => $user[0]['language'],
-                               'to_name' => $user[0]['username'],
-                               'to_email' => $user[0]['email'],
-                               'uid' => $user[0]['uid'],
-                               'item' => $postarray,
-                               'link' => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($top_item)),
-                               'source_name' => $postarray['author-name'],
-                               'source_link' => $postarray['author-link'],
-                               'source_photo' => $postarray['author-avatar'],
-                               'verb' => ACTIVITY_POST,
-                               'otype' => 'item',
-                               'parent' => $conv_parent,
-                       ]);
-
-                       // only send one notification
-                       break;
-               }
-       }
-}
-
 function twitter_fetchparentposts(App $a, $uid, $post, $connection, $self, $own_id)
 {
        logger("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, LOGGER_DEBUG);
@@ -1616,10 +1540,6 @@ function twitter_fetchparentposts(App $a, $uid, $post, $connection, $self, $own_
                        $postarray["id"] = $item;
 
                        logger('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
-
-                       if ($item && !function_exists("check_item_notification")) {
-                               twitter_checknotification($a, $uid, $own_id, $item, $postarray);
-                       }
                }
        }
 }
@@ -1751,10 +1671,6 @@ function twitter_fetchhometimeline(App $a, $uid)
                        $postarray["id"] = $item;
 
                        logger('twitter_fetchhometimeline: User ' . $self["nick"] . ' posted home timeline item ' . $item);
-
-                       if ($item && !function_exists("check_item_notification")) {
-                               twitter_checknotification($a, $uid, $own_id, $item, $postarray);
-                       }
                }
        }
        PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
@@ -1805,50 +1721,8 @@ function twitter_fetchhometimeline(App $a, $uid)
                        }
 
                        $item = Item::insert($postarray);
-                       $postarray["id"] = $item;
-
-                       if ($item && function_exists("check_item_notification")) {
-                               check_item_notification($item, $uid, NOTIFY_TAGSELF);
-                       }
-
-                       if (!isset($postarray["parent"]) || ($postarray["parent"] == 0)) {
-                               $postarray["parent"] = $item;
-                       }
 
                        logger('twitter_fetchhometimeline: User ' . $self["nick"] . ' posted mention timeline item ' . $item);
-
-                       if ($item == 0) {
-                               $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
-                                       dbesc($postarray['uri']),
-                                       intval($uid)
-                               );
-                               if (DBM::is_result($r)) {
-                                       $item = $r[0]['id'];
-                                       $parent_id = $r[0]['parent'];
-                               }
-                       } else {
-                               $parent_id = $postarray['parent'];
-                       }
-
-                       if (($item != 0) && !function_exists("check_item_notification")) {
-                               require_once 'include/enotify.php';
-                               notification([
-                                       'type'         => NOTIFY_TAGSELF,
-                                       'notify_flags' => $u[0]['notify-flags'],
-                                       'language'     => $u[0]['language'],
-                                       'to_name'      => $u[0]['username'],
-                                       'to_email'     => $u[0]['email'],
-                                       'uid'          => $u[0]['uid'],
-                                       'item'         => $postarray,
-                                       'link'         => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($item)),
-                                       'source_name'  => $postarray['author-name'],
-                                       'source_link'  => $postarray['author-link'],
-                                       'source_photo' => $postarray['author-avatar'],
-                                       'verb'         => ACTIVITY_TAG,
-                                       'otype'        => 'item',
-                                       'parent'       => $parent_id
-                               ]);
-                       }
                }
        }
 
index 3aaf1d3..a7453b1 100644 (file)
@@ -33,6 +33,7 @@ use Friendica\Core\Addon;
 use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
 use Friendica\Model\User;
+use Friendica\Model\Item;
 
 function windowsphonepush_install()
 {
@@ -196,19 +197,19 @@ function windowsphonepush_cron()
                                        $senditemtext = PConfig::get($rr['uid'], 'windowsphonepush', 'senditemtext');
                                        if ($senditemtext == 1) {
                                                // load item with the max id
-                                               $item = q("SELECT `author-name` as author, `body` as body FROM `item` where `id` = %d", intval($count[0]['max']));
+                                               $item = Item::selectFirst(local_user(), ['author-name', 'body'], ['id' => $count[0]['max']]);
 
                                                // as user allows to send the item, we want to show the sender of the item in the toast
                                                // toasts are limited to one line, therefore place is limited - author shall be in
                                                // max. 15 chars (incl. dots); author is displayed in bold font
-                                               $author = $item[0]['author'];
+                                               $author = $item['author-name'];
                                                $author = ((strlen($author) > 12) ? substr($author, 0, 12) . "..." : $author);
 
                                                // normally we show the body of the item, however if it is an url or an image we cannot
                                                // show this in the toast (only test), therefore changing to an alternate text
                                                // Otherwise BBcode-Tags will be eliminated and plain text cutted to 140 chars (incl. dots)
                                                // BTW: information only possible in English
-                                               $body = $item[0]['body'];
+                                               $body = $item['body'];
                                                if (substr($body, 0, 4) == "[url") {
                                                        $body = "URL/Image ...";
                                                } else {