From 6e7bd68ebb027b90398eee4fcc3bd73912a881e3 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 11 Mar 2014 23:52:32 +0100 Subject: [PATCH] More "LIMIT 1" removed - and some other SQL improvements. --- include/Contact.php | 10 ++--- include/acl_selectors.php | 12 ++--- include/api.php | 44 ++++++++++++------- include/cache.php | 2 +- include/datetime.php | 6 +-- include/enotify.php | 16 +++---- include/event.php | 8 ++-- include/follow.php | 20 ++++----- include/group.php | 18 ++++---- include/notifier.php | 24 +++++----- include/oauth.php | 12 ++--- include/security.php | 6 +-- include/text.php | 10 ++--- mod/community.php | 3 +- mod/crepair.php | 4 +- mod/dfrn_confirm.php | 92 +++++++++++++++++++-------------------- mod/dfrn_request.php | 28 ++++++------ mod/events.php | 18 ++++---- mod/fsuggest.php | 4 +- mod/group.php | 16 +++---- mod/like.php | 10 ++--- mod/lostpass.php | 4 +- mod/mood.php | 10 ++--- mod/notifications.php | 4 +- mod/poke.php | 20 ++++----- mod/profile_photo.php | 26 +++++------ mod/profiles.php | 18 ++++---- mod/profperm.php | 8 ++-- mod/settings.php | 18 ++++---- mod/starred.php | 2 +- mod/subthread.php | 8 ++-- mod/tagger.php | 22 +++++----- mod/tagrm.php | 2 +- update.php | 24 +++++----- 34 files changed, 271 insertions(+), 258 deletions(-) diff --git a/include/Contact.php b/include/Contact.php index af77869989..a6429bbf49 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -64,13 +64,13 @@ function contact_remove($id) { $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts'); if($archive) { - q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d limit 1", + q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d", intval($id) ); return; } - q("DELETE FROM `contact` WHERE `id` = %d LIMIT 1", + q("DELETE FROM `contact` WHERE `id` = %d", intval($id) ); q("DELETE FROM `item` WHERE `contact-id` = %d ", @@ -148,7 +148,7 @@ function mark_for_death($contact) { return; if($contact['term-date'] == '0000-00-00 00:00:00') { - q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d LIMIT 1", + q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']) ); @@ -166,7 +166,7 @@ function mark_for_death($contact) { // archive them rather than delete // though if the owner tries to unarchive them we'll start the whole process over again - q("update contact set `archive` = 1 where id = %d limit 1", + q("update contact set `archive` = 1 where id = %d", intval($contact['id']) ); q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid'])); @@ -181,7 +181,7 @@ function mark_for_death($contact) { if(! function_exists('unmark_for_death')) { function unmark_for_death($contact) { // It's a miracle. Our dead contact has inexplicably come back to life. - q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d LIMIT 1", + q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d", dbesc('0000-00-00 00:00:00'), intval($contact['id']) ); diff --git a/include/acl_selectors.php b/include/acl_selectors.php index 72c434692c..ee74ccc16a 100644 --- a/include/acl_selectors.php +++ b/include/acl_selectors.php @@ -399,19 +399,19 @@ function acl_lookup(&$a, $out_type = 'json') { } else { $sql_extra = $sql_extra2 = ""; } - + // count groups and contacts if ($type=='' || $type=='g'){ - $r = q("SELECT COUNT(`id`) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra", + $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra", intval(local_user()) ); $group_count = (int)$r[0]['g']; } else { $group_count = 0; } - + if ($type=='' || $type=='c'){ - $r = q("SELECT COUNT(`id`) AS c FROM `contact` + $r = q("SELECT COUNT(*) AS c FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != '' $sql_extra2" , @@ -423,7 +423,7 @@ function acl_lookup(&$a, $out_type = 'json') { // autocomplete for Private Messages - $r = q("SELECT COUNT(`id`) AS c FROM `contact` + $r = q("SELECT COUNT(*) AS c FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `network` IN ('%s','%s','%s') $sql_extra2" , @@ -439,7 +439,7 @@ function acl_lookup(&$a, $out_type = 'json') { // autocomplete for Contacts - $r = q("SELECT COUNT(`id`) AS c FROM `contact` + $r = q("SELECT COUNT(*) AS c FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra2" , intval(local_user()) diff --git a/include/api.php b/include/api.php index 72a64f2ac4..0a63a2c049 100644 --- a/include/api.php +++ b/include/api.php @@ -384,26 +384,26 @@ intval(api_user()) ); + //AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''", // count public wall messages - $r = q("SELECT COUNT(`id`) as `count` FROM `item` + $r = q("SELECT count(*) as `count` FROM `item` force index (uid_type) WHERE `uid` = %d - AND `type`='wall' - AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''", + AND `type`='wall'", intval($uinfo[0]['uid']) ); $countitms = $r[0]['count']; } else { - $r = q("SELECT COUNT(`id`) as `count` FROM `item` - WHERE `contact-id` = %d - AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''", + //AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''", + $r = q("SELECT count(*) as `count` FROM `item` + WHERE `contact-id` = %d", intval($uinfo[0]['id']) ); $countitms = $r[0]['count']; } // count friends - $r = q("SELECT COUNT(`id`) as `count` FROM `contact` + $r = q("SELECT count(*) as `count` FROM `contact` WHERE `uid` = %d AND `rel` IN ( %d, %d ) AND `self`=0 AND `blocked`=0 AND `pending`=0 AND `hidden`=0", intval($uinfo[0]['uid']), @@ -412,7 +412,7 @@ ); $countfriends = $r[0]['count']; - $r = q("SELECT COUNT(`id`) as `count` FROM `contact` + $r = q("SELECT count(*) as `count` FROM `contact` WHERE `uid` = %d AND `rel` IN ( %d, %d ) AND `self`=0 AND `blocked`=0 AND `pending`=0 AND `hidden`=0", intval($uinfo[0]['uid']), @@ -421,7 +421,7 @@ ); $countfollowers = $r[0]['count']; - $r = q("SELECT count(`id`) as `count` FROM item where starred = 1 and uid = %d and deleted = 0", + $r = q("SELECT count(*) as `count` FROM item where starred = 1 and uid = %d and deleted = 0", intval($uinfo[0]['uid']) ); $starred = $r[0]['count']; @@ -528,7 +528,8 @@ $status_user["protected"] = (($item["allow_cid"] != "") OR ($item["allow_gid"] != "") OR ($item["deny_cid"] != "") OR - ($item["deny_gid"] != "")); + ($item["deny_gid"] != "") OR + $item["private"]); return ($status_user); } @@ -745,12 +746,27 @@ logger('api_status_show: user_info: '.print_r($user_info, true), LOGGER_DEBUG); // get last public wall message - $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `c`.`nick` as `reply_author`, `i`.`author-link` AS `item-author` - FROM `item`, `contact`, `item` as `i`, `contact` as `c` + //$lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `c`.`nick` as `reply_author`, `i`.`author-link` AS `item-author` + // FROM `item`, `contact`, `item` as `i`, `contact` as `c` + // WHERE `item`.`contact-id` = %d + // AND ((`item`.`author-link` IN ('%s', '%s')) OR (`item`.`owner-link` IN ('%s', '%s'))) + // AND `i`.`id` = `item`.`parent` + // AND `contact`.`id`=`item`.`contact-id` AND `c`.`id`=`i`.`contact-id` AND `contact`.`self`=1 + // AND `item`.`type`!='activity' + // AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`='' + // ORDER BY `item`.`created` DESC + // LIMIT 1", + // intval($user_info['cid']), + // dbesc($user_info['url']), + // dbesc(normalise_link($user_info['url'])), + // dbesc($user_info['url']), + // dbesc(normalise_link($user_info['url'])) + //); + $lastwall = q("SELECT `item`.*, `i`.`contact-id` as `reply_uid`, `i`.`author-link` AS `item-author` + FROM `item`, `item` as `i` WHERE `item`.`contact-id` = %d AND ((`item`.`author-link` IN ('%s', '%s')) OR (`item`.`owner-link` IN ('%s', '%s'))) AND `i`.`id` = `item`.`parent` - AND `contact`.`id`=`item`.`contact-id` AND `c`.`id`=`i`.`contact-id` AND `contact`.`self`=1 AND `item`.`type`!='activity' AND `item`.`allow_cid`='' AND `item`.`allow_gid`='' AND `item`.`deny_cid`='' AND `item`.`deny_gid`='' ORDER BY `item`.`created` DESC @@ -773,8 +789,6 @@ if ($lastwall['parent']!=$lastwall['id']) { $in_reply_to_status_id= intval($lastwall['parent']); $in_reply_to_status_id_str = (string) intval($lastwall['parent']); - //$in_reply_to_user_id = $lastwall['reply_uid']; - //$in_reply_to_screen_name = $lastwall['reply_author']; $r = q("SELECT * FROM unique_contacts WHERE `url` = '%s'", dbesc(normalise_link($lastwall['item-author']))); if ($r) { diff --git a/include/cache.php b/include/cache.php index e6ea8f045d..86539310af 100644 --- a/include/cache.php +++ b/include/cache.php @@ -47,7 +47,7 @@ * dbesc($key) * ); * if(count($r)) { - * q("UPDATE `cache` SET `v` = '%s', `updated = '%s' WHERE `k` = '%s' limit 1", + * q("UPDATE `cache` SET `v` = '%s', `updated = '%s' WHERE `k` = '%s'", * dbesc($value), * dbesc(datetime_convert()), * dbesc($key)); diff --git a/include/datetime.php b/include/datetime.php index efcfa0a177..3ad9ea3a10 100644 --- a/include/datetime.php +++ b/include/datetime.php @@ -19,7 +19,7 @@ if(! function_exists('select_timezone')) { function select_timezone($current = 'America/Los_Angeles') { $timezone_identifiers = DateTimeZone::listIdentifiers(); - + $o ='