From 03e69e8c31c62c0a518f6d8a42211a54b49fd8d9 Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Mon, 10 Aug 2020 19:29:04 +0300 Subject: [PATCH] [DATABASE] Fix remaining misuses of SQL's GROUP BY --- lib/groups/groupsbymemberssection.php | 14 ++-- lib/groups/groupsbypostssection.php | 74 +++++++++---------- lib/ui/topposterssection.php | 18 +++-- .../GroupFavorited/actions/groupfavorited.php | 25 ++++--- .../actions/favoritedslice.php | 39 +++++----- 5 files changed, 94 insertions(+), 76 deletions(-) diff --git a/lib/groups/groupsbymemberssection.php b/lib/groups/groupsbymemberssection.php index ef04468b17..f9dff27feb 100644 --- a/lib/groups/groupsbymemberssection.php +++ b/lib/groups/groupsbymemberssection.php @@ -38,11 +38,15 @@ class GroupsByMembersSection extends GroupSection { $limit = GROUPS_PER_SECTION; - $qry = 'SELECT user_group.*, COUNT(*) AS value ' . - 'FROM user_group INNER JOIN group_member '. - 'ON user_group.id = group_member.group_id ' . - 'GROUP BY user_group.id, user_group.nickname, user_group.fullname, user_group.homepage, user_group.description, user_group.location, user_group.original_logo, user_group.homepage_logo, user_group.stream_logo, user_group.mini_logo, user_group.created, user_group.modified ' . - 'ORDER BY value DESC LIMIT ' . $limit; + $qry = <<. + /** - * StatusNet, the distributed open-source microblogging tool - * * Groups with the most posts section * - * PHP version 5 - * - * LICENCE: This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * * @category Widget - * @package StatusNet + * @package GNUsocial * @author Evan Prodromou * @copyright 2009 StatusNet, Inc. - * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 - * @link http://status.net/ + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} +defined('GNUSOCIAL') || die(); /** * Groups with the most posts section * - * @category Widget - * @package StatusNet - * @author Evan Prodromou - * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 - * @link http://status.net/ + * @category Widget + * @package GNUsocial + * @author Evan Prodromou + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ class GroupsByPostsSection extends GroupSection { - function getGroups() + public function getGroups() { $limit = GROUPS_PER_SECTION; - $qry = 'SELECT user_group.*, count(*) as value ' . - 'FROM user_group JOIN group_inbox '. - 'ON user_group.id = group_inbox.group_id ' . - 'GROUP BY user_group.id,user_group.nickname,user_group.fullname,user_group.homepage,user_group.description,user_group.location,user_group.original_logo,user_group.homepage_logo,user_group.stream_logo,user_group.mini_logo,user_group.created,user_group.modified ' . - 'ORDER BY value DESC LIMIT ' . $limit; + $qry = <<group->id; $weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff')); $cutoff = sprintf( - "fave.modified > TIMESTAMP '%s'", - common_sql_date(time() - common_config('popular', 'cutoff')) + "fave.modified > CURRENT_TIMESTAMP - INTERVAL '%d' SECOND", + common_config('popular', 'cutoff') ); $offset = ($this->page - 1) * NOTICES_PER_PAGE; - $limit = NOTICES_PER_PAGE + 1; + $limit = NOTICES_PER_PAGE + 1; - $qry = 'SELECT notice.*, ' . $weightexpr . ' AS weight ' . - 'FROM notice ' . - 'INNER JOIN group_inbox ON notice.id = group_inbox.notice_id ' . - 'INNER JOIN fave ON notice.id = fave.notice_id ' . - 'WHERE ' . $cutoff . ' AND group_id = ' . $groupId . ' ' . - 'GROUP BY id, profile_id, uri, content, rendered, url, created, notice.modified, reply_to, is_local, source, notice.conversation ' . - 'ORDER BY weight DESC LIMIT ' . $limit . ' OFFSET ' . $offset; + $qry = << TIMESTAMP '%s'", - common_sql_date(time() - common_config('popular', 'cutoff')) + "fave.modified > CURRENT_TIMESTAMP - INTERVAL '%d' SECOND", + common_config('popular', 'cutoff') ); $offset = ($this->page - 1) * NOTICES_PER_PAGE; $limit = NOTICES_PER_PAGE + 1; - $qry = 'SELECT notice.*, ' . $weightexpr . ' AS weight ' . - 'FROM notice INNER JOIN fave ON notice.id = fave.notice_id ' . - 'WHERE ' . $cutoff . ' AND ' . $slice . ' ' . - 'GROUP BY id, profile_id, uri, content, rendered, url, created, notice.modified, reply_to, is_local, source, notice.conversation ' . - 'ORDER BY weight DESC LIMIT ' . $limit . ' OFFSET ' . $offset; + $qry = <<nicknamesToIds($this->includeUsers); $exclude = $this->nicknamesToIds($this->excludeUsers); - if (count($include) == 1) { - return "profile_id = " . intval($include[0]); - } elseif (count($include) > 1) { - return "profile_id IN (" . implode(',', $include) . ")"; - } elseif (count($exclude) === 1) { - return "profile_id != " . intval($exclude[0]); - } elseif (count($exclude) > 1) { - return "profile_id NOT IN (" . implode(',', $exclude) . ")"; - } else { - return false; + $sql = []; + if (count($include) > 0) { + $sql[] = 'notice.profile_id IN (' . implode(',', $include) . ')'; } + if (count($exclude) > 0) { + $sql[] = 'notice.profile_id NOT IN (' . implode(',', $exclude) . ')'; + } + + return implode(' AND ', $sql) ?: false; } /**