[DATABASE] Columns not in GROUP BY must not be queried

This commit is contained in:
Alexei Sorokin 2019-09-11 13:12:41 +03:00 committed by Diogo Peralta Cordeiro
parent 0e0c375e65
commit 37e5983aca
4 changed files with 78 additions and 83 deletions

View File

@ -1,45 +1,38 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
/* /*
* StatusNet - the distributed open-source microblogging tool
*
* Handler for reminder queue items which send reminder emails to all users * Handler for reminder queue items which send reminder emails to all users
* we would like to complete a given process (e.g.: registration). * we would like to complete a given process (e.g.: registration).
* *
* 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 <http://www.gnu.org/licenses/>.
*
* @category Email * @category Email
* @package StatusNet * @package GNUsocial
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
if (!defined('STATUSNET')) { defined('GNUSOCIAL') || die();
exit(1);
}
/** /**
* Handler for reminder queue items which send reminder emails to all users * Handler for reminder queue items which send reminder emails to all users
* we would like to complete a given process (e.g.: registration) * we would like to complete a given process (e.g.: registration)
* *
* @category Email
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
class SiteConfirmReminderHandler extends QueueHandler class SiteConfirmReminderHandler extends QueueHandler
{ {
@ -52,7 +45,7 @@ class SiteConfirmReminderHandler extends QueueHandler
* *
* @return string * @return string
*/ */
function transport() public function transport()
{ {
return 'siterem'; return 'siterem';
} }
@ -63,14 +56,14 @@ class SiteConfirmReminderHandler extends QueueHandler
* @param array $remitem type of reminder to send and any special options * @param array $remitem type of reminder to send and any special options
* @return boolean true on success, false on failure * @return boolean true on success, false on failure
*/ */
function handle($remitem) : bool public function handle($remitem): bool
{ {
list($type, $opts) = $remitem; list($type, $opts) = $remitem;
$qm = QueueManager::get(); $qm = QueueManager::get();
try { try {
switch($type) { switch ($type) {
case UserConfirmRegReminderHandler::REGISTER_REMINDER: case UserConfirmRegReminderHandler::REGISTER_REMINDER:
$confirm = new Confirm_address(); $confirm = new Confirm_address();
$confirm->address_type = $type; $confirm->address_type = $type;
@ -87,7 +80,11 @@ class SiteConfirmReminderHandler extends QueueHandler
case UserInviteReminderHandler::INVITE_REMINDER: case UserInviteReminderHandler::INVITE_REMINDER:
$invitation = new Invitation(); $invitation = new Invitation();
// Only send one reminder (the latest one), regardless of how many invitations a user has // Only send one reminder (the latest one), regardless of how many invitations a user has
$sql = 'SELECT * FROM (SELECT * FROM invitation WHERE registered_user_id IS NULL ORDER BY created DESC) invitees GROUP BY invitees.address'; $sql = 'SELECT * FROM invitation ' .
'WHERE (address, created) IN ' .
'(SELECT address, MAX(created) FROM invitation GROUP BY address) AND ' .
'registered_user_id IS NULL ' .
'ORDER BY created DESC';
$invitation->query($sql); $invitation->query($sql);
while ($invitation->fetch()) { while ($invitation->fetch()) {
try { try {

View File

@ -1,44 +1,36 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* Data class to mark notices as bookmarks * Data class to mark notices as bookmarks
* *
* PHP version 5
*
* @category PollPlugin * @category PollPlugin
* @package StatusNet * @package GNUsocial
* @author Brion Vibber <brion@status.net> * @author Brion Vibber <brion@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 * @copyright 2011 StatusNet, Inc.
* @link http://status.net/ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* 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 <http://www.gnu.org/licenses/>.
*/ */
if (!defined('STATUSNET')) { defined('GNUSOCIAL') || die();
exit(1);
}
/** /**
* For storing the poll options and such * For storing the poll options and such
* *
* @category PollPlugin * @copyright 2011 StatusNet, Inc.
* @package StatusNet * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @author Brion Vibber <brion@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
* *
* @see DB_DataObject * @see DB_DataObject
*/ */
@ -136,7 +128,9 @@ class Poll extends Managed_DataObject
$pr = new Poll_response(); $pr = new Poll_response();
$pr->poll_id = $this->id; $pr->poll_id = $this->id;
$pr->groupBy('selection'); $pr->groupBy('selection');
$pr->selectAdd('count(profile_id) as votes'); $pr->selectAdd();
$pr->selectAdd('selection');
$pr->selectAdd('COUNT(profile_id) AS votes');
$pr->find(); $pr->find();
$raw = array(); $raw = array();

View File

@ -110,6 +110,8 @@ class SearchSubPlugin extends Plugin
// with a lot of searches! // with a lot of searches!
$sub = new SearchSub(); $sub = new SearchSub();
$sub->groupBy('search'); $sub->groupBy('search');
$sub->selectAdd();
$sub->selectAdd('search');
$sub->find(); $sub->find();
while ($sub->fetch()) { while ($sub->fetch()) {
$search = $sub->search; $search = $sub->search;

View File

@ -1,21 +1,23 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
/* // This file is part of GNU social - https://www.gnu.org/software/social
* StatusNet - a distributed open-source microblogging tool //
* Copyright (C) 2008, 2009, StatusNet, Inc. // GNU social is free software: you can redistribute it and/or modify
* // it under the terms of the GNU Affero General Public License as published by
* This program is free software: you can redistribute it and/or modify // the Free Software Foundation, either version 3 of the License, or
* it under the terms of the GNU Affero General Public License as published by // (at your option) any later version.
* the Free Software Foundation, either version 3 of the License, or //
* (at your option) any later version. // GNU social is distributed in the hope that it will be useful,
* // but WITHOUT ANY WARRANTY; without even the implied warranty of
* This program is distributed in the hope that it will be useful, // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* but WITHOUT ANY WARRANTY; without even the implied warranty of // GNU Affero General Public License for more details.
* 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. /**
* @copyright 2008, 2009 StatusNet, Inc.
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
define('INSTALLDIR', dirname(__DIR__)); define('INSTALLDIR', dirname(__DIR__));
@ -44,7 +46,7 @@ if (!have_option('y', 'yes')) {
} }
$file = new File(); $file = new File();
$file->query('SELECT id, url, COUNT(*) AS c FROM file GROUP BY url HAVING c > 1'); $file->query('SELECT url FROM file GROUP BY url HAVING COUNT(*) > 1');
print "\nFound {$file->N} URLs with duplicate entries in file table"; print "\nFound {$file->N} URLs with duplicate entries in file table";
while ($file->fetch()) { while ($file->fetch()) {
// We've got a URL that is duplicated in the file table // We've got a URL that is duplicated in the file table
@ -54,7 +56,7 @@ while ($file->fetch()) {
print "\nDeleting duplicate entries in file table for URL: {$file->url} ["; print "\nDeleting duplicate entries in file table for URL: {$file->url} [";
// Leave one of the URLs in the database by using ->find(true) // Leave one of the URLs in the database by using ->find(true)
// and only deleting starting with this fetch. // and only deleting starting with this fetch.
while($dupfile->fetch()) { while ($dupfile->fetch()) {
print "."; print ".";
$dupfile->delete(); $dupfile->delete();
} }
@ -65,7 +67,7 @@ while ($file->fetch()) {
} }
$file = new File_redirection(); $file = new File_redirection();
$file->query('SELECT file_id, url, COUNT(*) AS c FROM file_redirection GROUP BY url HAVING c > 1'); $file->query('SELECT url FROM file_redirection GROUP BY url HAVING COUNT(*) > 1');
print "\nFound {$file->N} URLs with duplicate entries in file_redirection table"; print "\nFound {$file->N} URLs with duplicate entries in file_redirection table";
while ($file->fetch()) { while ($file->fetch()) {
// We've got a URL that is duplicated in the file_redirection table // We've got a URL that is duplicated in the file_redirection table
@ -75,7 +77,7 @@ while ($file->fetch()) {
print "\nDeleting duplicate entries in file table for URL: {$file->url} ["; print "\nDeleting duplicate entries in file table for URL: {$file->url} [";
// Leave one of the URLs in the database by using ->find(true) // Leave one of the URLs in the database by using ->find(true)
// and only deleting starting with this fetch. // and only deleting starting with this fetch.
while($dupfile->fetch()) { while ($dupfile->fetch()) {
print "."; print ".";
$dupfile->delete(); $dupfile->delete();
} }