1
0
Fork 0

"qu" is no more

This commit is contained in:
Michael 2017-09-15 21:00:39 +00:00
parent 6d7ececc42
commit dfd48dd6f6
8 changed files with 32 additions and 71 deletions

View file

@ -1361,45 +1361,6 @@ function q($sql) {
return $data; return $data;
} }
/**
* @brief Performs a query with "dirty reads" - deprecated
*
* Please use the dba:: functions instead:
* dba::select, dba::exists, dba::insert
* dba::delete, dba::update, dba::p, dba::e
*
* @param $args Query parameters (1 to N parameters of different types)
* @return array Query array
*/
function qu($sql) {
global $db;
$args = func_get_args();
unset($args[0]);
if ($db && $db->connected) {
$sql = $db->clean_query($sql);
$sql = $db->any_value_fallback($sql);
$stmt = @vsprintf($sql,$args); // Disabled warnings
if ($stmt === false)
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
$db->log_index($stmt);
$retval = $db->q($stmt);
return $retval;
}
/**
*
* This will happen occasionally trying to store the
* session data after abnormal program termination
*
*/
logger('dba: no database: ' . print_r($args,true));
return false;
}
function dba_timer() { function dba_timer() {
return microtime(true); return microtime(true);
} }

View file

@ -955,7 +955,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
* An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this. * An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this.
*/ */
if ($arr["uid"] == 0) { if ($arr["uid"] == 0) {
$r = qu("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc(trim($arr['uri']))); $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc(trim($arr['uri'])));
if (dbm::is_result($r)) { if (dbm::is_result($r)) {
logger('Global item already stored. URI: '.$arr['uri'].' on network '.$arr['network'], LOGGER_DEBUG); logger('Global item already stored. URI: '.$arr['uri'].' on network '.$arr['network'], LOGGER_DEBUG);
return 0; return 0;

View file

@ -48,7 +48,7 @@ function photo_albums($uid, $update = false) {
if (!Config::get('system', 'no_count', false)) { if (!Config::get('system', 'no_count', false)) {
/// @todo This query needs to be renewed. It is really slow /// @todo This query needs to be renewed. It is really slow
// At this time we just store the data in the cache // At this time we just store the data in the cache
$albums = qu("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created` $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
FROM `photo` FROM `photo`
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
GROUP BY `album` ORDER BY `created` DESC", GROUP BY `album` ORDER BY `created` DESC",
@ -58,7 +58,7 @@ function photo_albums($uid, $update = false) {
); );
} else { } else {
// This query doesn't do the count and is much faster // This query doesn't do the count and is much faster
$albums = qu("SELECT DISTINCT(`album`), '' AS `total` $albums = q("SELECT DISTINCT(`album`), '' AS `total`
FROM `photo` USE INDEX (`uid_album_scale_created`) FROM `photo` USE INDEX (`uid_album_scale_created`)
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra", WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra",
intval($uid), intval($uid),

View file

@ -457,7 +457,7 @@ function admin_page_federation(App $a) {
foreach ($platforms as $p) { foreach ($platforms as $p) {
// get a total count for the platform, the name and version of the // get a total count for the platform, the name and version of the
// highest version and the protocol tpe // highest version and the protocol tpe
$c = qu('SELECT COUNT(*) AS `total`, ANY_VALUE(`platform`) AS `platform`, $c = q('SELECT COUNT(*) AS `total`, ANY_VALUE(`platform`) AS `platform`,
ANY_VALUE(`network`) AS `network`, MAX(`version`) AS `version` FROM `gserver` ANY_VALUE(`network`) AS `network`, MAX(`version`) AS `version` FROM `gserver`
WHERE `platform` LIKE "%s" AND `last_contact` >= `last_failure` WHERE `platform` LIKE "%s" AND `last_contact` >= `last_failure`
ORDER BY `version` ASC;', $p); ORDER BY `version` ASC;', $p);
@ -465,7 +465,7 @@ function admin_page_federation(App $a) {
// what versions for that platform do we know at all? // what versions for that platform do we know at all?
// again only the active nodes // again only the active nodes
$v = qu('SELECT COUNT(*) AS `total`, `version` FROM `gserver` $v = q('SELECT COUNT(*) AS `total`, `version` FROM `gserver`
WHERE `last_contact` >= `last_failure` AND `platform` LIKE "%s" WHERE `last_contact` >= `last_failure` AND `platform` LIKE "%s"
GROUP BY `version` GROUP BY `version`
ORDER BY `version`;', $p); ORDER BY `version`;', $p);
@ -644,13 +644,13 @@ function admin_page_summary(App $a) {
logger('accounts: '.print_r($accounts,true),LOGGER_DATA); logger('accounts: '.print_r($accounts,true),LOGGER_DATA);
$r = qu("SELECT COUNT(`id`) AS `count` FROM `register`"); $r = q("SELECT COUNT(`id`) AS `count` FROM `register`");
$pending = $r[0]['count']; $pending = $r[0]['count'];
$r = qu("SELECT COUNT(*) AS `total` FROM `queue` WHERE 1"); $r = q("SELECT COUNT(*) AS `total` FROM `queue` WHERE 1");
$queue = (($r) ? $r[0]['total'] : 0); $queue = (($r) ? $r[0]['total'] : 0);
$r = qu("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE NOT `done`"); $r = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE NOT `done`");
$workerqueue = (($r) ? $r[0]['total'] : 0); $workerqueue = (($r) ? $r[0]['total'] : 0);
// We can do better, but this is a quick queue status // We can do better, but this is a quick queue status
@ -1487,7 +1487,7 @@ function admin_page_users(App $a) {
/* get users */ /* get users */
$total = qu("SELECT COUNT(*) AS `total` FROM `user` WHERE 1"); $total = q("SELECT COUNT(*) AS `total` FROM `user` WHERE 1");
if (count($total)) { if (count($total)) {
$a->set_pager_total($total[0]['total']); $a->set_pager_total($total[0]['total']);
$a->set_pager_itemspage(100); $a->set_pager_itemspage(100);
@ -1522,7 +1522,7 @@ function admin_page_users(App $a) {
$sql_order = "`".str_replace('.','`.`',$order)."`"; $sql_order = "`".str_replace('.','`.`',$order)."`";
$sql_order_direction = ($order_direction === "+")?"ASC":"DESC"; $sql_order_direction = ($order_direction === "+")?"ASC":"DESC";
$users = qu("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date` $users = q("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date`
FROM `user` FROM `user`
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self` INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
WHERE `user`.`verified` WHERE `user`.`verified`

View file

@ -188,7 +188,7 @@ function nodeinfo_cron() {
} }
logger('cron_start'); logger('cron_start');
$users = qu("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item` $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
FROM `user` FROM `user`
INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default` INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self` INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
@ -220,7 +220,7 @@ function nodeinfo_cron() {
Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly); Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
} }
$posts = qu("SELECT COUNT(*) AS local_posts FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0"); $posts = q("SELECT COUNT(*) AS local_posts FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0");
if (!is_array($posts)) { if (!is_array($posts)) {
$local_posts = -1; $local_posts = -1;
@ -231,7 +231,7 @@ function nodeinfo_cron() {
logger('local_posts: '.$local_posts, LOGGER_DEBUG); logger('local_posts: '.$local_posts, LOGGER_DEBUG);
$posts = qu("SELECT COUNT(*) FROM `contact` $posts = q("SELECT COUNT(*) FROM `contact`
INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `item`.`uid` = `contact`.`uid` AND INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `item`.`uid` = `contact`.`uid` AND
`item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s') `item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s')
WHERE `contact`.`self`", WHERE `contact`.`self`",

View file

@ -74,7 +74,7 @@ function photo_init(App $a) {
$uid = str_replace(array('.jpg','.png'),array('',''), $person); $uid = str_replace(array('.jpg','.png'),array('',''), $person);
$r = qu("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1", $r = q("SELECT * FROM `photo` WHERE `scale` = %d AND `uid` = %d AND `profile` = 1 LIMIT 1",
intval($resolution), intval($resolution),
intval($uid) intval($uid)
); );
@ -104,7 +104,7 @@ function photo_init(App $a) {
} }
// check if the photo exists and get the owner of the photo // check if the photo exists and get the owner of the photo
$r = qu("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", $r = q("SELECT `uid` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
dbesc($photo), dbesc($photo),
intval($resolution) intval($resolution)
); );
@ -114,7 +114,7 @@ function photo_init(App $a) {
// Now we'll see if we can access the photo // Now we'll see if we can access the photo
$r = qu("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1", $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` <= %d $sql_extra ORDER BY scale DESC LIMIT 1",
dbesc($photo), dbesc($photo),
intval($resolution) intval($resolution)
); );

View file

@ -29,7 +29,7 @@ function photos_init(App $a) {
if ($a->argc > 1) { if ($a->argc > 1) {
$nick = $a->argv[1]; $nick = $a->argv[1];
$user = qu("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1", $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
dbesc($nick) dbesc($nick)
); );
@ -153,7 +153,7 @@ function photos_post(App $a) {
} }
if ($contact_id) { if ($contact_id) {
$r = qu("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1", $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
intval($contact_id), intval($contact_id),
intval($page_owner_uid) intval($page_owner_uid)
); );
@ -170,7 +170,7 @@ function photos_post(App $a) {
killme(); killme();
} }
$r = qu("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` $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", WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
intval($page_owner_uid) intval($page_owner_uid)
); );
@ -192,7 +192,7 @@ function photos_post(App $a) {
return; // NOTREACHED return; // NOTREACHED
} }
$r = qu("SELECT `album` FROM `photo` WHERE `album` = '%s' AND `uid` = %d", $r = q("SELECT `album` FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
dbesc($album), dbesc($album),
intval($page_owner_uid) intval($page_owner_uid)
); );
@ -1378,7 +1378,7 @@ function photos_content(App $a) {
else else
$order = 'DESC'; $order = 'DESC';
$prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
$sql_extra ORDER BY `created` $order ", $sql_extra ORDER BY `created` $order ",
dbesc($ph[0]['album']), dbesc($ph[0]['album']),
intval($owner_uid) intval($owner_uid)
@ -1478,7 +1478,7 @@ function photos_content(App $a) {
if (dbm::is_result($linked_items)) { if (dbm::is_result($linked_items)) {
$link_item = $linked_items[0]; $link_item = $linked_items[0];
$r = qu("SELECT COUNT(*) AS `total` $r = q("SELECT COUNT(*) AS `total`
FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
@ -1495,7 +1495,7 @@ function photos_content(App $a) {
} }
$r = qu("SELECT `item`.*, `item`.`id` AS `item_id`, $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`,
`contact`.`rel`, `contact`.`thumb`, `contact`.`self`, `contact`.`rel`, `contact`.`thumb`, `contact`.`self`,
`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid` `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
@ -1835,7 +1835,7 @@ function photos_content(App $a) {
// Default - show recent photos with upload link (if applicable) // Default - show recent photos with upload link (if applicable)
//$o = ''; //$o = '';
$r = qu("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
$sql_extra GROUP BY `resource-id`", $sql_extra GROUP BY `resource-id`",
intval($a->data['user']['uid']), intval($a->data['user']['uid']),
dbesc('Contact Photos'), dbesc('Contact Photos'),
@ -1846,7 +1846,7 @@ function photos_content(App $a) {
$a->set_pager_itemspage(20); $a->set_pager_itemspage(20);
} }
$r = qu("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`, ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`,
ANY_VALUE(`created`) AS `created` FROM `photo` ANY_VALUE(`created`) AS `created` FROM `photo`
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'

View file

@ -118,7 +118,7 @@ function ping_init(App $a)
$notifs = ping_get_notifications(local_user()); $notifs = ping_get_notifications(local_user());
$items_unseen = qu("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`, $items_unseen = q("SELECT `item`.`id`, `item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
`item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`, `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
`pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink` `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`
FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent` FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id` = `item`.`parent`
@ -167,13 +167,13 @@ function ping_init(App $a)
} }
} }
$intros1 = qu("SELECT `intro`.`id`, `intro`.`datetime`, $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
`fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0", WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0",
intval(local_user()) intval(local_user())
); );
$intros2 = qu("SELECT `intro`.`id`, `intro`.`datetime`, $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
`contact`.`name`, `contact`.`url`, `contact`.`photo` `contact`.`name`, `contact`.`url`, `contact`.`photo`
FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id` FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0", WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0",
@ -184,7 +184,7 @@ function ping_init(App $a)
$intros = $intros1 + $intros2; $intros = $intros1 + $intros2;
$myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ; $myurl = System::baseUrl() . '/profile/' . $a->user['nickname'] ;
$mails = qu("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail` $mails = q("SELECT `id`, `from-name`, `from-url`, `from-photo`, `created` FROM `mail`
WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ", WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
intval(local_user()), intval(local_user()),
dbesc($myurl) dbesc($myurl)
@ -192,7 +192,7 @@ function ping_init(App $a)
$mail_count = count($mails); $mail_count = count($mails);
if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){ if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
$regs = qu("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total` $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) AS `total`
FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid` FROM `contact` RIGHT JOIN `register` ON `register`.`uid` = `contact`.`uid`
WHERE `contact`.`self` = 1"); WHERE `contact`.`self` = 1");
@ -204,7 +204,7 @@ function ping_init(App $a)
$cachekey = "ping_init:".local_user(); $cachekey = "ping_init:".local_user();
$ev = Cache::get($cachekey); $ev = Cache::get($cachekey);
if (is_null($ev)) { if (is_null($ev)) {
$ev = qu("SELECT type, start, adjust FROM `event` $ev = q("SELECT type, start, adjust FROM `event`
WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0 WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
ORDER BY `start` ASC ", ORDER BY `start` ASC ",
intval(local_user()), intval(local_user()),
@ -424,7 +424,7 @@ function ping_get_notifications($uid)
$a = get_app(); $a = get_app();
do { do {
$r = qu("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted` $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid` FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
WHERE `notify`.`uid` = %d AND `notify`.`msg` != '' WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
AND NOT (`notify`.`type` IN (%d, %d)) AND NOT (`notify`.`type` IN (%d, %d))