]> git.mxchange.org Git - friendica.git/commitdiff
Use the item functions at many more places
authorMichael <heluecht@pirati.ca>
Mon, 18 Jun 2018 20:36:34 +0000 (20:36 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 18 Jun 2018 20:36:34 +0000 (20:36 +0000)
13 files changed:
include/api.php
include/conversation.php
include/items.php
mod/display.php
mod/editpost.php
mod/fetch.php
mod/photos.php
mod/tagrm.php
mod/viewsrc.php
src/Model/Item.php
src/Model/User.php
src/Object/Post.php
src/Protocol/OStatus.php

index 1fbdd8f7fc458fbaf7e26f0e51bfed5089d434b0..8f3dea6167da20723add51ba24cd59987325ac6e 100644 (file)
@@ -1122,18 +1122,8 @@ function api_statuses_update($type)
                if ($throttle_day > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60);
 
-                       $r = q(
-                               "SELECT COUNT(*) AS `posts_day` FROM `item` WHERE `uid`=%d AND `wall`
-                               AND `created` > '%s' AND `id` = `parent`",
-                               intval(api_user()),
-                               dbesc($datefrom)
-                       );
-
-                       if (DBM::is_result($r)) {
-                               $posts_day = $r[0]["posts_day"];
-                       } else {
-                               $posts_day = 0;
-                       }
+                       $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom];
+                       $posts_day = dba::count('item', $condition);
 
                        if ($posts_day > $throttle_day) {
                                logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG);
@@ -1146,18 +1136,8 @@ function api_statuses_update($type)
                if ($throttle_week > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7);
 
-                       $r = q(
-                               "SELECT COUNT(*) AS `posts_week` FROM `item` WHERE `uid`=%d AND `wall`
-                               AND `created` > '%s' AND `id` = `parent`",
-                               intval(api_user()),
-                               dbesc($datefrom)
-                       );
-
-                       if (DBM::is_result($r)) {
-                               $posts_week = $r[0]["posts_week"];
-                       } else {
-                               $posts_week = 0;
-                       }
+                       $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom];
+                       $posts_week = dba::count('item', $condition);
 
                        if ($posts_week > $throttle_week) {
                                logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG);
@@ -1170,18 +1150,8 @@ function api_statuses_update($type)
                if ($throttle_month > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30);
 
-                       $r = q(
-                               "SELECT COUNT(*) AS `posts_month` FROM `item` WHERE `uid`=%d AND `wall`
-                               AND `created` > '%s' AND `id` = `parent`",
-                               intval(api_user()),
-                               dbesc($datefrom)
-                       );
-
-                       if (DBM::is_result($r)) {
-                               $posts_month = $r[0]["posts_month"];
-                       } else {
-                               $posts_month = 0;
-                       }
+                       $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom];
+                       $posts_month = dba::count('item', $condition);
 
                        if ($posts_month > $throttle_month) {
                                logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG);
@@ -2755,14 +2725,10 @@ function api_format_items_activities(&$item, $type = "json")
                'attendmaybe' => [],
        ];
 
-       $items = q(
-               'SELECT * FROM `item`
-                       WHERE `uid` = %d AND `thr-parent` = "%s" AND `visible` AND NOT `deleted`',
-               intval($item['uid']),
-               dbesc($item['uri'])
-       );
+       $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri']];
+       $ret = Item::selectForUser($item['uid'], ['author-id', 'verb'], $condition);
 
-       foreach ($items as $i) {
+       while ($i = dba::fetch($ret)) {
                // not used as result should be structured like other user data
                //builtin_activity_puller($i, $activities);
 
@@ -2789,6 +2755,8 @@ function api_format_items_activities(&$item, $type = "json")
                }
        }
 
+       dba::close($ret);
+
        if ($type == "xml") {
                $xml_activities = [];
                foreach ($activities as $k => $v) {
@@ -3872,16 +3840,13 @@ function api_fr_photoalbum_delete($type)
        // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
        // to the user and the contacts of the users (drop_items() performs the federation of the deletion to other networks
        foreach ($r as $rr) {
-               $photo_item = q(
-                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'",
-                       intval(local_user()),
-                       dbesc($rr['resource-id'])
-               );
+               $condition = ['uid' => local_user(), 'resource-id' => $rr['resource-id'], 'type' => 'photo'];
+               $photo_item = Item::selectFirstForUser(local_user(), ['id'], $condition);
 
                if (!DBM::is_result($photo_item)) {
                        throw new InternalServerErrorException("problem with deleting items occured");
                }
-               Item::deleteForUser(['id' => $photo_item[0]['id']], api_user());
+               Item::deleteForUser(['id' => $photo_item['id']], api_user());
        }
 
        // now let's delete all photos from the album
@@ -4162,18 +4127,15 @@ function api_fr_photo_delete($type)
        // return success of deletion or error message
        if ($result) {
                // retrieve the id of the parent element (the photo element)
-               $photo_item = q(
-                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'",
-                       intval(local_user()),
-                       dbesc($photo_id)
-               );
+               $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
+               $photo_item = Item::selectFirstForUser(local_user(), ['id'], $condition);
 
                if (!DBM::is_result($photo_item)) {
                        throw new InternalServerErrorException("problem with deleting items occured");
                }
                // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
                // to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion)
-               Item::deleteForUser(['id' => $photo_item[0]['id']], api_user());
+               Item::deleteForUser(['id' => $photo_item['id']], api_user());
 
                $answer = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.'];
                return api_format_data("photo_delete", $type, ['$result' => $answer]);
@@ -4661,12 +4623,10 @@ function prepare_photo_data($type, $scale, $photo_id)
        }
 
        // retrieve item element for getting activities (like, dislike etc.) related to photo
-       $item = q(
-               "SELECT * FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'",
-               intval(local_user()),
-               dbesc($photo_id)
-       );
-       $data['photo']['friendica_activities'] = api_format_items_activities($item[0], $type);
+       $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
+       $item = Item::selectFirstForUser(local_user(), ['id'], $condition);
+
+       $data['photo']['friendica_activities'] = api_format_items_activities($item, $type);
 
        // retrieve comments on photo
        $condition = ["`parent` = ? AND `uid` = ? AND (`verb` = ? OR `type`='photo')",
@@ -4961,35 +4921,26 @@ function api_in_reply_to($item)
        $in_reply_to['screen_name'] = null;
 
        if (($item['thr-parent'] != $item['uri']) && (intval($item['parent']) != intval($item['id']))) {
-               $r = q(
-                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
-                       intval($item['uid']),
-                       dbesc($item['thr-parent'])
-               );
-
-               if (DBM::is_result($r)) {
-                       $in_reply_to['status_id'] = intval($r[0]['id']);
+               $parent = Item::selectFirst(['id'], ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
+               if (DBM::is_result($parent)) {
+                       $in_reply_to['status_id'] = intval($parent['id']);
                } else {
                        $in_reply_to['status_id'] = intval($item['parent']);
                }
 
                $in_reply_to['status_id_str'] = (string) intval($in_reply_to['status_id']);
 
-               $r = q(
-                       "SELECT `contact`.`nick`, `contact`.`name`, `contact`.`id`, `contact`.`url` FROM `item`
-                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`author-id`
-                       WHERE `item`.`id` = %d LIMIT 1",
-                       intval($in_reply_to['status_id'])
-               );
+               $fields = ['author-nick', 'author-name', 'author-id', 'author-link'];
+               $parent = Item::selectFirst($fields, ['id' => $in_reply_to['status_id']]);
 
-               if (DBM::is_result($r)) {
-                       if ($r[0]['nick'] == "") {
-                               $r[0]['nick'] = api_get_nick($r[0]["url"]);
+               if (DBM::is_result($parent)) {
+                       if ($parent['author-nick'] == "") {
+                               $parent['author-nick'] = api_get_nick($parent['author-link']);
                        }
 
-                       $in_reply_to['screen_name'] = (($r[0]['nick']) ? $r[0]['nick'] : $r[0]['name']);
-                       $in_reply_to['user_id'] = intval($r[0]['id']);
-                       $in_reply_to['user_id_str'] = (string) intval($r[0]['id']);
+                       $in_reply_to['screen_name'] = (($parent['author-nick']) ? $parent['author-nick'] : $parent['author-name']);
+                       $in_reply_to['user_id'] = intval($parent['author-id']);
+                       $in_reply_to['user_id_str'] = (string) intval($parent['author-id']);
                }
 
                // There seems to be situation, where both fields are identical:
index 97be7e9aa984436725c748be807553e14a06e574..6ef2e73aa2692f6d64b34aff14a3b0fe30dd0716 100644 (file)
@@ -633,16 +633,12 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order =
                                $location_e = $location;
                                $owner_name_e = $owner_name;
 
-                               if ($item['item_network'] == "") {
-                                       $item['item_network'] = $item['network'];
-                               }
-
                                $tmp_item = [
                                        'template' => $tpl,
                                        'id' => (($preview) ? 'P0' : $item['item_id']),
                                        'guid' => (($preview) ? 'Q0' : $item['guid']),
-                                       'network' => $item['item_network'],
-                                       'network_name' => ContactSelector::networkToName($item['item_network'], $profile_link),
+                                       'network' => $item['network'],
+                                       'network_name' => ContactSelector::networkToName($item['network'], $profile_link),
                                        'linktitle' => L10n::t('View %s\'s profile @ %s', $profile_name, $item['author-link']),
                                        'profile_url' => $profile_link,
                                        'item_photo_menu' => item_photo_menu($item),
@@ -688,7 +684,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order =
                                Addon::callHooks('display_item', $arr);
 
                                $threads[$threadsid]['id'] = $item['item_id'];
-                               $threads[$threadsid]['network'] = $item['item_network'];
+                               $threads[$threadsid]['network'] = $item['network'];
                                $threads[$threadsid]['items'] = [$arr['output']];
 
                        }
index c54869c3e51c310143f86d908969bef2bc15bb08..ee514c8fc414c37e2f86c1fd21ac3ceab0080297 100644 (file)
@@ -335,17 +335,14 @@ function drop_item($id) {
 
        // locate item to be deleted
 
-       $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
-               intval($id)
-       );
+       $fields = ['id', 'uid', 'contact-id', 'deleted'];
+       $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $id]);
 
-       if (!DBM::is_result($r)) {
+       if (!DBM::is_result($item)) {
                notice(L10n::t('Item not found.') . EOL);
                goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
        }
 
-       $item = $r[0];
-
        if ($item['deleted']) {
                return 0;
        }
@@ -364,7 +361,6 @@ function drop_item($id) {
        }
 
        if ((local_user() == $item['uid']) || $contact_id) {
-
                // Check if we should do HTML-based delete confirmation
                if ($_REQUEST['confirm']) {
                        // <form> can't take arguments in its "action" parameter
index 13ec9331be55bc1dbb4f9916141aea31ce04bd71..ff1ae6741b0746a0c6df9839403c7b035b40128b 100644 (file)
@@ -43,6 +43,8 @@ function display_init(App $a)
 
        $r = false;
 
+       $fields = ['id', 'parent', 'author-id', 'body', 'uid'];
+
        // If there is only one parameter, then check if this parameter could be a guid
        if ($a->argc == 2) {
                $nick = "";
@@ -50,9 +52,7 @@ function display_init(App $a)
 
                // Does the local user have this item?
                if (local_user()) {
-                       $r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid`
-                               FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
-                                       AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
+                       $r = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
                        if (DBM::is_result($r)) {
                                $nick = $a->user["nickname"];
                        }
@@ -60,54 +60,44 @@ function display_init(App $a)
 
                // Is it an item with uid=0?
                if (!DBM::is_result($r)) {
-                       $r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid`
-                               FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
-                                       AND NOT `private` AND `uid` = 0
-                                       AND `guid` = ? LIMIT 1", $a->argv[1]);
-               }
-
-               if (!DBM::is_result($r)) {
-                       $a->error = 404;
-                       notice(L10n::t('Item not found.') . EOL);
-                       return;
+                       $r = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => false, 'uid' => 0]);
                }
        } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
-               $r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid`
-                       FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
-                               AND NOT `private` AND `uid` = 0
-                               AND `id` = ? LIMIT 1", $a->argv[2]);
+               $r = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => false, 'uid' => 0]);
        }
 
-       if (DBM::is_result($r)) {
-               if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
-                       logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
-                       displayShowFeed($r["id"], false);
-               }
+       if (!DBM::is_result($r) || $r['deleted']) {
+               $a->error = 404;
+               notice(L10n::t('Item not found.') . EOL);
+               return;
+       }
 
-               if ($r["id"] != $r["parent"]) {
-                       $r = dba::fetch_first("SELECT `id`, `author-id`, `body`, `uid` FROM `item`
-                               WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
-                                       AND `id` = ?", $r["parent"]);
-               }
+       if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
+               logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
+               displayShowFeed($r["id"], false);
+       }
 
-               $profiledata = display_fetchauthor($a, $r);
+       if ($r["id"] != $r["parent"]) {
+               $r = Item::selectFirstForUser(local_user(), $fields, ['id' => $r["parent"]]);
+       }
 
-               if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
-                       $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
+       $profiledata = display_fetchauthor($a, $r);
 
-                       if (($nickname != $a->user["nickname"])) {
-                               $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
-                                       INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
-                                       WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
-                                       $nickname
-                               );
-                               if (DBM::is_result($r)) {
-                                       $profiledata = $r;
-                               }
-                               $profiledata["network"] = NETWORK_DFRN;
-                       } else {
-                               $profiledata = [];
+       if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
+               $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
+
+               if (($nickname != $a->user["nickname"])) {
+                       $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
+                               INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
+                               WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
+                               $nickname
+                       );
+                       if (DBM::is_result($r)) {
+                               $profiledata = $r;
                        }
+                       $profiledata["network"] = NETWORK_DFRN;
+               } else {
+                       $profiledata = [];
                }
        }
 
index aa2c296845c95ce7d3dd34f201fa48024a703854..dc98e93d9199b11176d1db0235f13b80dcfcfb06 100644 (file)
@@ -8,6 +8,7 @@ use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
+use Friendica\Model\Item;
 use Friendica\Database\DBM;
 
 function editpost_content(App $a) {
@@ -26,11 +27,9 @@ function editpost_content(App $a) {
                return;
        }
 
-       $itm = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-               intval($post_id),
-               intval(local_user())
-       );
-
+       $fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
+               'type', 'body', 'title', 'file'];
+       $itm = Item::selectFirstForUser(local_user(), $fields, ['id' => $post_id, 'uid' => local_user()]);
        if (! DBM::is_result($itm)) {
                notice(L10n::t('Item not found') . EOL);
                return;
@@ -124,8 +123,8 @@ function editpost_content(App $a) {
                '$shortnoloc' => L10n::t('clear location'),
                '$wait' => L10n::t('Please wait'),
                '$permset' => L10n::t('Permission settings'),
-               '$ptyp' => $itm[0]['type'],
-               '$content' => undo_post_tagging($itm[0]['body']),
+               '$ptyp' => $itm['type'],
+               '$content' => undo_post_tagging($itm['body']),
                '$post_id' => $post_id,
                '$baseurl' => System::baseUrl(),
                '$defloc' => $a->user['default-location'],
@@ -134,9 +133,9 @@ function editpost_content(App $a) {
                '$emailcc' => L10n::t('CC: email addresses'),
                '$public' => L10n::t('Public post'),
                '$jotnets' => $jotnets,
-               '$title' => htmlspecialchars($itm[0]['title']),
+               '$title' => htmlspecialchars($itm['title']),
                '$placeholdertitle' => L10n::t('Set title'),
-               '$category' => file_tag_file_to_list($itm[0]['file'], 'category'),
+               '$category' => file_tag_file_to_list($itm['file'], 'category'),
                '$placeholdercategory' => (Feature::isEnabled(local_user(),'categories') ? L10n::t("Categories \x28comma-separated list\x29") : ''),
                '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
                '$lockstate' => $lockstate,
index 6892990c247ef54c7f8004f38e4375e1a724f6ab..da616ad5bb103ba16a7726a8fbff7135864f5ce5 100644 (file)
@@ -7,7 +7,10 @@ use Friendica\App;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
 use Friendica\Protocol\Diaspora;
+use Friendica\Model\Item;
+use Friendica\Model\User;
 use Friendica\Util\XML;
+use Friendica\Database\DBM;
 
 function fetch_init(App $a)
 {
@@ -20,24 +23,14 @@ function fetch_init(App $a)
        $guid = $a->argv[2];
 
        // Fetch the item
-       $item = q(
-               "SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`, `coord`
-                       FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
-               dbesc($guid),
-               NETWORK_DFRN,
-               NETWORK_DIASPORA
-       );
-       if (!$item) {
-               $r = q(
-                       "SELECT `author-link`
-                       FROM `item` WHERE `uid` = 0 AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
-                       dbesc($guid),
-                       NETWORK_DFRN,
-                       NETWORK_DIASPORA
-               );
-
-               if ($r) {
-                       $parts = parse_url($r[0]["author-link"]);
+       $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network'];
+       $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
+       $item = Item::selectFirst($fields, $condition);
+       if (!DBM::is_result($item)) {
+               $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
+               $item = Item::selectFirst(['author-link'], $condition);
+               if (DBM::is_result($item)) {
+                       $parts = parse_url($item["author-link"]);
                        $host = $parts["scheme"]."://".$parts["host"];
 
                        if (normalise_link($host) != normalise_link(System::baseUrl())) {
@@ -54,20 +47,13 @@ function fetch_init(App $a)
        }
 
        // Fetch some data from the author (We could combine both queries - but I think this is more readable)
-       $r = q(
-               "SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user`
-               INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
-               WHERE `user`.`uid` = %d",
-               intval($item[0]["uid"])
-       );
-
-       if (!$r) {
+       $user = User::getOwnerDataById($item["uid"]);
+       if (!$user) {
                header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
                killme();
        }
-       $user = $r[0];
 
-       $status = Diaspora::buildStatus($item[0], $user);
+       $status = Diaspora::buildStatus($item, $user);
        $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
 
        // Send the envelope
index f6540c2608b57c407a4f2320979e8aeeaad0ef14..9cd3482746734f71696684bcc12af6b8fa6cbd44 100644 (file)
@@ -19,6 +19,7 @@ use Friendica\Model\Group;
 use Friendica\Model\Item;
 use Friendica\Model\Photo;
 use Friendica\Model\Profile;
+use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
 use Friendica\Protocol\DFRN;
@@ -175,19 +176,14 @@ function photos_post(App $a)
                killme();
        }
 
-       $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
-               WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
-               intval($page_owner_uid)
-       );
+       $owner_record = User::getOwnerDataById($page_owner_uid);
 
-       if (!DBM::is_result($r)) {
+       if (!$owner_record) {
                notice(L10n::t('Contact information unavailable') . EOL);
                logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
                killme();
        }
 
-       $owner_record = $r[0];
-
        if ($a->argc > 3 && $a->argv[2] === 'album') {
                $album = hex2bin($a->argv[3]);
 
@@ -487,14 +483,11 @@ function photos_post(App $a)
                }
 
                if ($item_id) {
-                       $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                               intval($item_id),
-                               intval($page_owner_uid)
-                       );
+                       $item = Item::selectFirst(['tag', 'inform'], ['id' => $item_id, 'uid' => $page_owner_uid]);
                }
-               if (DBM::is_result($r)) {
-                       $old_tag    = $r[0]['tag'];
-                       $old_inform = $r[0]['inform'];
+               if (DBM::is_result($item)) {
+                       $old_tag    = $item['tag'];
+                       $old_inform = $item['inform'];
                }
 
                if (strlen($rawtags)) {
index b9991d68da5b5ff0ea4c9a7a360dfe2fe1464ed8..dbe6f108776986528d25d1561bef238a5eacc301 100644 (file)
@@ -10,29 +10,25 @@ use Friendica\Core\System;
 use Friendica\Database\DBM;
 use Friendica\Model\Item;
 
-function tagrm_post(App $a) {
-
+function tagrm_post(App $a)
+{
        if (!local_user()) {
                goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
        }
 
-       if ((x($_POST,'submit')) && ($_POST['submit'] === L10n::t('Cancel'))) {
+       if (x($_POST,'submit') && ($_POST['submit'] === L10n::t('Cancel'))) {
                goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
        }
 
-       $tag =  ((x($_POST,'tag'))  ? hex2bin(notags(trim($_POST['tag']))) : '');
-       $item = ((x($_POST,'item')) ? intval($_POST['item'])               : 0 );
-
-       $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-               intval($item),
-               intval(local_user())
-       );
+       $tag =  (x($_POST,'tag')  ? hex2bin(notags(trim($_POST['tag']))) : '');
+       $item_id = (x($_POST,'item') ? intval($_POST['item'])               : 0);
 
-       if (!DBM::is_result($r)) {
+       $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
+       if (!DBM::is_result($item)) {
                goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
        }
 
-       $arr = explode(',', $r[0]['tag']);
+       $arr = explode(',', $item['tag']);
        for ($x = 0; $x < count($arr); $x ++) {
                if ($arr[$x] === $tag) {
                        unset($arr[$x]);
@@ -42,7 +38,7 @@ function tagrm_post(App $a) {
 
        $tag_str = implode(',',$arr);
 
-       Item::update(['tag' => $tag_str], ['id' => $item]);
+       Item::update(['tag' => $tag_str], ['id' => $item_id]);
 
        info(L10n::t('Tag removed') . EOL );
        goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
@@ -52,8 +48,8 @@ function tagrm_post(App $a) {
 
 
 
-function tagrm_content(App $a) {
-
+function tagrm_content(App $a)
+{
        $o = '';
 
        if (!local_user()) {
@@ -61,22 +57,18 @@ function tagrm_content(App $a) {
                // NOTREACHED
        }
 
-       $item = (($a->argc > 1) ? intval($a->argv[1]) : 0);
-       if (!$item) {
+       $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
+       if (!$item_id) {
                goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
                // NOTREACHED
        }
 
-       $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-               intval($item),
-               intval(local_user())
-       );
-
-       if (!DBM::is_result($r)) {
+       $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
+       if (!DBM::is_result($item)) {
                goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
        }
 
-       $arr = explode(',', $r[0]['tag']);
+       $arr = explode(',', $item['tag']);
 
        if (!count($arr)) {
                goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
@@ -87,7 +79,7 @@ function tagrm_content(App $a) {
        $o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>';
 
        $o .= '<form id="tagrm" action="tagrm" method="post" >';
-       $o .= '<input type="hidden" name="item" value="' . $item . '" />';
+       $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
        $o .= '<ul>';
 
        foreach ($arr as $x) {
@@ -100,5 +92,4 @@ function tagrm_content(App $a) {
        $o .= '</form>';
 
        return $o;
-
 }
index d2f0d01a07b183bc7b45f79f30e8f5b502363ad7..4b511eab13f2d501bbd48286e3f97a807d7189ff 100644 (file)
@@ -5,38 +5,33 @@
 use Friendica\App;
 use Friendica\Core\L10n;
 use Friendica\Database\DBM;
+use Friendica\Model\Item;
 
-function viewsrc_content(App $a) {
-
-       if (! local_user()) {
+function viewsrc_content(App $a)
+{
+       if (!local_user()) {
                notice(L10n::t('Access denied.') . EOL);
                return;
        }
 
-       $o = ''; 
+       $o = '';
        $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
 
-       if(! $item_id) {
+       if (!$item_id) {
                $a->error = 404;
                notice(L10n::t('Item not found.') . EOL);
                return;
        }
 
-       $r = q("SELECT `item`.`body` FROM `item`
-               WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
-               and `item`.`moderated` = 0
-               AND `item`.`id` = '%s' LIMIT 1",
-               intval(local_user()),
-               dbesc($item_id)
-       );
+       $item = Item::selectFirst(['body'], ['uid' => local_user(), 'id' => $item_id]);
 
-       if (DBM::is_result($r))
-               if(is_ajax()) {
-                       echo str_replace("\n",'<br />',$r[0]['body']);
+       if (DBM::is_result($item)) {
+               if (is_ajax()) {
+                       echo str_replace("\n",'<br />',$item['body']);
                        killme();
                } else {
-                       $o .= str_replace("\n",'<br />',$r[0]['body']);
+                       $o .= str_replace("\n",'<br />',$item['body']);
                }
+       }
        return $o;
 }
-
index dcf944cc46a6c16453ad790c76686fb011863958..adb0baa6885d64ffb250a26ef62b8c96ef8346e7 100644 (file)
@@ -38,8 +38,8 @@ class Item extends BaseObject
                        'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
                        'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'attach',
                        'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
-                       'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id', 'item_network',
-                       'author-id', 'author-link', 'author-name', 'author-avatar',
+                       'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id',
+                       'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
                        'owner-id', 'owner-link', 'owner-name', 'owner-avatar',
                        'contact-id', 'contact-link', 'contact-name', 'contact-avatar',
                        'network', 'url', 'name', 'writable', 'self', 'cid', 'alias',
@@ -250,17 +250,19 @@ class Item extends BaseObject
                        'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
                        'unseen', 'deleted', 'origin', 'forum_mode', 'mention',
                        'rendered-hash', 'rendered-html', 'global', 'shadow', 'content-warning',
-                       'id' => 'item_id', 'network' => 'item_network'];
+                       'id' => 'item_id', 'network'];
 
-               $fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'thumb' => 'author-avatar'];
+               $fields['author'] = ['url' => 'author-link', 'name' => 'author-name',
+                       'thumb' => 'author-avatar', 'nick' => 'author-nick', 'network' => 'author-network'];
 
-               $fields['owner'] = ['url' => 'owner-link', 'name' => 'owner-name', 'thumb' => 'owner-avatar'];
+               $fields['owner'] = ['url' => 'owner-link', 'name' => 'owner-name',
+                       'thumb' => 'owner-avatar', 'nick' => 'owner-nick', 'network' => 'owner-network'];
 
                $fields['contact'] = ['url' => 'contact-link', 'name' => 'contact-name', 'thumb' => 'contact-avatar',
                        'network', 'url', 'name', 'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid',
-                       'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id'];
+                       'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id', 'network' => 'contact-network'];
 
-               $fields['parent-item'] = ['guid' => 'parent-guid'];
+               $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network'];
 
                $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name'];
 
index 39c544c3a8786f62053093ec1c4ff734d5eb4c4f..76769e14c78debdc8ea3003fc414276ac4e40988 100644 (file)
@@ -65,6 +65,21 @@ class User
                return $r;
        }
 
+       /**
+        * @brief Get owner data by nick name
+        *
+        * @param int $nick
+        * @return boolean|array
+        */
+       public static function getOwnerDataByNick($nick)
+       {
+               $user = dba::selectFirst('user', ['uid'], ['nickname' => $nick]);
+               if (!DBM::is_result($user)) {
+                       return false;
+               }
+               return self::getOwnerDataById($user['uid']);
+       }
+
        /**
         * @brief Returns the default group for a given user and network
         *
index a66e48a380b26995c5917d795e69c51d01933592..bceee38444ca4f88bcec7caf08198b7b236df91b 100644 (file)
@@ -326,17 +326,17 @@ class Post extends BaseObject
                $owner_name_e = $this->getOwnerName();
 
                // Disable features that aren't available in several networks
-               if (!in_array($item["item_network"], [NETWORK_DFRN, NETWORK_DIASPORA]) && isset($buttons["dislike"])) {
+               if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA]) && isset($buttons["dislike"])) {
                        unset($buttons["dislike"]);
                        $isevent = false;
                        $tagger = '';
                }
 
-               if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) {
+               if (($item["network"] == NETWORK_FEED) && isset($buttons["like"])) {
                        unset($buttons["like"]);
                }
 
-               if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) {
+               if (($item["network"] == NETWORK_MAIL) && isset($buttons["like"])) {
                        unset($buttons["like"]);
                }
 
@@ -401,8 +401,8 @@ class Post extends BaseObject
                        'wait'            => L10n::t('Please wait'),
                        'thread_level'    => $thread_level,
                        'edited'          => $edited,
-                       'network'         => $item["item_network"],
-                       'network_name'    => ContactSelector::networkToName($item['item_network'], $profile_link),
+                       'network'         => $item["network"],
+                       'network_name'    => ContactSelector::networkToName($item['network'], $profile_link),
                        'received'        => $item['received'],
                        'commented'       => $item['commented'],
                        'created_date'    => $item['created'],
index ce9e6642edec4edab337907fdc57bbbbd3bcda6c..36ed3580e7354ffba32fa6b4f1449e9f2008491f 100644 (file)
@@ -15,6 +15,7 @@ use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
 use Friendica\Model\Item;
+use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
@@ -1619,22 +1620,14 @@ class OStatus
 
                $title = self::entryHeader($doc, $entry, $owner, $item, $toplevel);
 
-               $r = q(
-                       "SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' AND NOT `private` AND `network` IN ('%s', '%s', '%s') LIMIT 1",
-                       intval($owner["uid"]),
-                       dbesc($repeated_guid),
-                       dbesc(NETWORK_DFRN),
-                       dbesc(NETWORK_DIASPORA),
-                       dbesc(NETWORK_OSTATUS)
-               );
-               if (DBM::is_result($r)) {
-                       $repeated_item = $r[0];
-               } else {
+               $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => false,
+                       'network' => [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]];
+               $repeated_item = Item::selectFirst([], $condition);
+               if (!DBM::is_result($repeated_item)) {
                        return false;
                }
-               $contact = self::contactEntry($repeated_item['author-link'], $owner);
 
-               $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
+               $contact = self::contactEntry($repeated_item['author-link'], $owner);
 
                $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
 
@@ -1695,16 +1688,11 @@ class OStatus
 
                $as_object = $doc->createElement("activity:object");
 
-               $parent = q(
-                       "SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
-                       dbesc($item["thr-parent"]),
-                       intval($item["uid"])
-               );
-               $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
+               $parent = Item::selectFirst([], ['uri' => $item["thr-parent"], 'uid' => $item["uid"]]);
 
-               XML::addElement($doc, $as_object, "activity:object-type", self::constructObjecttype($parent[0]));
+               XML::addElement($doc, $as_object, "activity:object-type", self::constructObjecttype($parent));
 
-               self::entryContent($doc, $as_object, $parent[0], $owner, "New entry");
+               self::entryContent($doc, $as_object, $parent, $owner, "New entry");
 
                $entry->appendChild($as_object);
 
@@ -1952,22 +1940,19 @@ class OStatus
                $mentioned = [];
 
                if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
-                       $parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"]));
+                       $parent = item::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item["parent"]]);
                        $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
 
-                       $thrparent = q(
-                               "SELECT `guid`, `author-link`, `owner-link`, `plink` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
-                               intval($owner["uid"]),
-                               dbesc($parent_item)
-                       );
-                       if ($thrparent) {
-                               $mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"];
-                               $mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"];
-                               $parent_plink = $thrparent[0]["plink"];
+                       $thrparent = item::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $parent_item]);
+
+                       if (DBM::is_result($thrparent)) {
+                               $mentioned[$thrparent["author-link"]] = $thrparent["author-link"];
+                               $mentioned[$thrparent["owner-link"]] = $thrparent["owner-link"];
+                               $parent_plink = $thrparent["plink"];
                        } else {
-                               $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"];
-                               $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"];
-                               $parent_plink = System::baseUrl()."/display/".$parent[0]["guid"];
+                               $mentioned[$parent["author-link"]] = $parent["author-link"];
+                               $mentioned[$parent["owner-link"]] = $parent["owner-link"];
+                               $parent_plink = System::baseUrl()."/display/".$parent["guid"];
                        }
 
                        $attributes = [
@@ -2130,13 +2115,8 @@ class OStatus
                        return $result['feed'];
                }
 
-               $owner = dba::fetch_first(
-                       "SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`, `user`.`account-type`
-                               FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
-                               WHERE `contact`.`self` AND `user`.`nickname` = ? LIMIT 1",
-                       $owner_nick
-               );
-               if (!DBM::is_result($owner)) {
+               $owner = User::getOwnerDataByNick($owner_nick);
+               if (!$owner) {
                        return;
                }
 
@@ -2147,37 +2127,28 @@ class OStatus
                $check_date = DateTimeFormat::utc($last_update);
                $authorid = Contact::getIdForURL($owner["url"], 0, true);
 
-               $sql_extra = '';
+               $condition = ["`uid` = ? AND `created` > ? AND NOT `deleted`
+                       AND NOT `private` AND `visible` AND `wall` AND `parent-network` IN (?, ?)",
+                       $owner["uid"], $check_date, NETWORK_OSTATUS, NETWORK_DFRN];
+
                if ($filter === 'posts') {
-                       $sql_extra .= ' AND `item`.`id` = `item`.`parent` ';
+                       $condition[0] .= " AND `id` = `parent`";
                }
 
                if ($filter === 'comments') {
-                       $sql_extra .= sprintf(" AND `item`.`object-type` = '%s' ", dbesc(ACTIVITY_OBJ_COMMENT));
+                       $condition[0] .= " AND `object-type` = ? ";
+                       $condition[] = ACTIVITY_OBJ_COMMENT;
                }
 
                if ($owner['account-type'] != ACCOUNT_TYPE_COMMUNITY) {
-                       $sql_extra .= sprintf(" AND `item`.`contact-id` = %d AND `item`.`author-id` = %d ", intval($owner["id"]), intval($authorid));
-               }
-
-               $items = q(
-                       "SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
-                               STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
-                               WHERE `item`.`uid` = %d
-                               AND `item`.`created` > '%s'
-                               AND NOT `item`.`deleted`
-                               AND NOT `item`.`private`
-                               AND `item`.`visible`
-                               AND `item`.`wall`
-                               AND `thread`.`network` IN ('%s', '%s')
-                               $sql_extra
-                               ORDER BY `item`.`created` DESC LIMIT %d",
-                       intval($owner["uid"]),
-                       dbesc($check_date),
-                       dbesc(NETWORK_OSTATUS),
-                       dbesc(NETWORK_DFRN),
-                       intval($max_items)
-               );
+                       $condition[0] .= " AND `contact-id` = ? AND `author-id` = ?";
+                       $condition[] = $owner["id"];
+                       $condition[] = $authorid;
+               }
+
+               $params = ['order' => ['created' => true], 'limit' => $max_items];
+               $ret = Item::select([], $condition, $params);
+               $items = dba::inArray($ret);
 
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;