switch to new way of executing SQL

This commit is contained in:
Matthew Exon 2022-03-02 16:15:59 +01:00
parent 6e12c60296
commit 1f181edf6f
2 changed files with 51 additions and 51 deletions

View file

@ -67,10 +67,10 @@ function phototrack_module() {}
function phototrack_finished_row($table, $id) {
$existing = DBA::selectFirst('phototrack_row_check', ['id'], ['table' => $table, 'row-id' => $id]);
if (!is_bool($existing)) {
DBA:e("UPDATE phototrack_row_check SET checked = NOW() WHERE `table` = '$table' AND `row-id` = '$id'");
DBA::e("UPDATE phototrack_row_check SET checked = NOW() WHERE `table` = '$table' AND `row-id` = '$id'");
}
else {
DBA:e("INSERT INTO phototrack_row_check (`table`, `row-id`, `checked`) VALUES ('$table', '$id', NOW())");
DBA::e("INSERT INTO phototrack_row_check (`table`, `row-id`, `checked`) VALUES ('$table', '$id', NOW())");
}
}
@ -86,17 +86,17 @@ function phototrack_photo_use($photo, $table, $field, $id) {
if (strlen($photo) != 32) {
return;
}
$r = DBA:e("SELECT `resource-id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", DBA::escape($photo));
$r = DBA::e("SELECT `resource-id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", DBA::escape($photo));
if (!count($r)) {
return;
}
$rid = $r[0]['resource-id'];
$existing = DBA:e("SELECT id FROM phototrack_photo_use WHERE `resource-id` = '$rid' AND `table` = '$table' AND `field` = '$field' AND `row-id` = '$id'");
$existing = DBA::e("SELECT id FROM phototrack_photo_use WHERE `resource-id` = '$rid' AND `table` = '$table' AND `field` = '$field' AND `row-id` = '$id'");
if (count($existing)) {
DBA:e("UPDATE phototrack_photo_use SET checked = NOW() WHERE `resource-id` = '$rid' AND `table` = '$table' AND `field` = '$field' AND `row-id` = '$id'");
DBA::e("UPDATE phototrack_photo_use SET checked = NOW() WHERE `resource-id` = '$rid' AND `table` = '$table' AND `field` = '$field' AND `row-id` = '$id'");
}
else {
DBA:e("INSERT INTO phototrack_photo_use (`resource-id`, `table`, `field`, `row-id`, `checked`) VALUES ('$rid', '$table', '$field', '$id', NOW())");
DBA::e("INSERT INTO phototrack_photo_use (`resource-id`, `table`, `field`, `row-id`, `checked`) VALUES ('$rid', '$table', '$field', '$id', NOW())");
}
}
@ -197,11 +197,11 @@ function phototrack_batch_size() {
function phototrack_search_table($a, $table) {
$batch_size = phototrack_batch_size();
$rows = DBA:e("SELECT `$table`.* FROM `$table` LEFT OUTER JOIN phototrack_row_check ON ( phototrack_row_check.`table` = '$table' AND phototrack_row_check.`row-id` = `$table`.id ) WHERE ( ( phototrack_row_check.checked IS NULL ) OR ( phototrack_row_check.checked < DATE_SUB(NOW(), INTERVAL 1 MONTH) ) ) ORDER BY phototrack_row_check.checked LIMIT $batch_size");
$rows = DBA::e("SELECT `$table`.* FROM `$table` LEFT OUTER JOIN phototrack_row_check ON ( phototrack_row_check.`table` = '$table' AND phototrack_row_check.`row-id` = `$table`.id ) WHERE ( ( phototrack_row_check.checked IS NULL ) OR ( phototrack_row_check.checked < DATE_SUB(NOW(), INTERVAL 1 MONTH) ) ) ORDER BY phototrack_row_check.checked LIMIT $batch_size");
foreach ($rows as $row) {
phototrack_check_row($a, $table, $row);
}
$r = DBA:e("SELECT COUNT(*) FROM `$table` LEFT OUTER JOIN phototrack_row_check ON ( phototrack_row_check.`table` = '$table' AND phototrack_row_check.`row-id` = `$table`.id ) WHERE ( ( phototrack_row_check.checked IS NULL ) OR ( phototrack_row_check.checked < DATE_SUB(NOW(), INTERVAL 1 MONTH) ) )");
$r = DBA::e("SELECT COUNT(*) FROM `$table` LEFT OUTER JOIN phototrack_row_check ON ( phototrack_row_check.`table` = '$table' AND phototrack_row_check.`row-id` = `$table`.id ) WHERE ( ( phototrack_row_check.checked IS NULL ) OR ( phototrack_row_check.checked < DATE_SUB(NOW(), INTERVAL 1 MONTH) ) )");
$remaining = $r[0]['COUNT(*)'];
Logger::info('phototrack: searched ' . count($rows) . ' rows in table ' . $table . ', ' . $remaining . ' still remaining to search');
return $remaining;
@ -250,20 +250,20 @@ function phototrack_cron($a, $b) {
function phototrack_tidy() {
$batch_size = phototrack_batch_size();
DBA:e('CREATE TABLE IF NOT EXISTS `phototrack-temp` (`resource-id` char(255) not null)');
DBA:e('INSERT INTO `phototrack-temp` SELECT DISTINCT(`resource-id`) FROM photo WHERE photo.`created` < DATE_SUB(NOW(), INTERVAL 2 MONTH)');
$rows = DBA:e('SELECT `phototrack-temp`.`resource-id` FROM `phototrack-temp` LEFT OUTER JOIN phototrack_photo_use ON (`phototrack-temp`.`resource-id` = phototrack_photo_use.`resource-id`) WHERE phototrack_photo_use.id IS NULL limit ' . /*$batch_size*/1000);
DBA::e('CREATE TABLE IF NOT EXISTS `phototrack-temp` (`resource-id` char(255) not null)');
DBA::e('INSERT INTO `phototrack-temp` SELECT DISTINCT(`resource-id`) FROM photo WHERE photo.`created` < DATE_SUB(NOW(), INTERVAL 2 MONTH)');
$rows = DBA::e('SELECT `phototrack-temp`.`resource-id` FROM `phototrack-temp` LEFT OUTER JOIN phototrack_photo_use ON (`phototrack-temp`.`resource-id` = phototrack_photo_use.`resource-id`) WHERE phototrack_photo_use.id IS NULL limit ' . /*$batch_size*/1000);
if (DBA::isResult($rows)) {
foreach ($rows as $row) {
Logger::debug('phototrack: remove photo ' . $row['resource-id']);
DBA:e('DELETE FROM photo WHERE `resource-id` = "' . $row['resource-id'] . '"');
DBA::e('DELETE FROM photo WHERE `resource-id` = "' . $row['resource-id'] . '"');
}
Logger::info('phototrack_tidy: deleted ' . count($rows) . ' photos');
}
DBA:e('DROP TABLE `phototrack-temp`');
$rows = DBA:e('SELECT id FROM phototrack_photo_use WHERE checked < DATE_SUB(NOW(), INTERVAL 14 DAY)');
DBA::e('DROP TABLE `phototrack-temp`');
$rows = DBA::e('SELECT id FROM phototrack_photo_use WHERE checked < DATE_SUB(NOW(), INTERVAL 14 DAY)');
foreach ($rows as $row) {
DBA:e('DELETE FROM phototrack_photo_use WHERE id = ' . $row['id']);
DBA::e( 'DELETE FROM phototrack_photo_use WHERE id = ' . $row['id']);
}
Logger::info('phototrack_tidy: deleted ' . count($rows) . ' phototrack_photo_use rows');
}

View file

@ -28,11 +28,11 @@ SELECT *
OR (`reason` = 'publicise')
ORDER BY `contact`.`name`
EOF;
return DBA:e($query, intval(local_user()));
return DBA::e($query, intval(local_user()));
}
function publicise_get_user($uid) {
$r = DBA:e('SELECT * FROM `user` WHERE `uid` = %d', intval($uid));
$r = DBA::e('SELECT * FROM `user` WHERE `uid` = %d', intval($uid));
if (count($r) != 1) {
Logger::warning('Publicise: unexpected number of results for uid ' . $uid);
}
@ -52,7 +52,7 @@ function publicise_addon_admin(&$a,&$o) {
$comments = 1;
$url = $v['url'];
if ($enabled) {
$r = DBA:e('SELECT * FROM `user` WHERE `uid` = %d', intval($v['uid']));
$r = DBA::e('SELECT * FROM `user` WHERE `uid` = %d', intval($v['uid']));
$expire = $r[0]['expire'];
$url = $a->get_baseurl() . '/profile/' . $v['nick'];
if ($r[0]['page-flags'] == PAGE_SOAPBOX) {
@ -138,7 +138,7 @@ function publicise_create_user($owner, $contact) {
'expire' => publicise_make_int($expire),
);
Logger::debug('Publicise: creating user ' . print_r($newuser, true));
$r = DBA:e("INSERT INTO `user` (`"
$r = DBA::e("INSERT INTO `user` (`"
. implode("`, `", array_keys($newuser))
. "`) VALUES ("
. implode(", ", array_values($newuser))
@ -147,7 +147,7 @@ function publicise_create_user($owner, $contact) {
Logger::warning('Publicise: create user failed');
return;
}
$r = DBA:e('SELECT * FROM `user` WHERE `guid` = "%s"', DBA::escape($guid));
$r = DBA::e('SELECT * FROM `user` WHERE `guid` = "%s"', DBA::escape($guid));
if (count($r) != 1) {
Logger::warning('Publicise: unexpected number of uids returned');
return;
@ -179,21 +179,21 @@ function publicise_create_self_contact($a, $contact, $uid) {
'avatar-date' => publicise_make_string(datetime_convert()),
'closeness' => publicise_make_int(0),
);
$existing = DBA:e("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1", intval($uid));
$existing = DBA::e("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1", intval($uid));
if (count($existing)) {
$newcontact = $existing[0];
Logger::debug('Publicise: self contact already exists for user ' . $uid . ' id ' . $newcontact['id']);
} else {
Logger::debug('Publicise: create contact ' . print_r($newcontact, true));
DBA:e("INSERT INTO `contact` (`"
DBA::e("INSERT INTO `contact` (`"
. implode("`, `", array_keys($newcontact))
. "`) VALUES ("
. implode(", ", array_values($newcontact))
. ")" );
$results = DBA:e("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` = 1", intval($uid));
$results = DBA::e("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` = 1", intval($uid));
if (count($results) != 1) {
Logger::warning('Publicise: create self contact failed, will delete uid ' . $uid);
$r = DBA:e("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
$r = DBA::e("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
return;
}
$newcontact = $results[0];
@ -216,7 +216,7 @@ function publicise_create_profile($contact, $uid) {
'net-publish' => publicise_make_int(1),
);
Logger::debug('Publicise: create profile ' . print_r($newprofile, true));
$r = DBA:e("INSERT INTO `profile` (`"
$r = DBA::e("INSERT INTO `profile` (`"
. implode("`, `", array_keys($newprofile))
. "`) VALUES ("
. implode(", ", array_values($newprofile))
@ -224,7 +224,7 @@ function publicise_create_profile($contact, $uid) {
if (!$r) {
Logger::warning('Publicise: create profile failed');
}
$newprofile = DBA:e('SELECT `id` FROM `profile` WHERE `uid` = %d AND `is-default` = 1', intval($uid));
$newprofile = DBA::e('SELECT `id` FROM `profile` WHERE `uid` = %d AND `is-default` = 1', intval($uid));
if (count($newprofile) != 1) {
Logger::warning('Publicise: create profile produced unexpected number of results');
return;
@ -243,15 +243,15 @@ function publicise_set_up_user($a, $contact, $owner) {
if (!$self_contact) {
notice(sprintf(t("Failed to create self contact for user \"%s\""), $contact['name']) . EOL);
Logger::warning("Publicise: unable to create self contact, deleting user " . $user['uid']);
DBA:e('DELETE FROM `user` WHERE `uid` = %d', intval($user['uid']));
DBA::e('DELETE FROM `user` WHERE `uid` = %d', intval($user['uid']));
return;
}
$profile = publicise_create_profile($contact, $user['uid']);
if (!$profile) {
notice(sprintf(t("Failed to create profile for user \"%s\""), $contact['name']) . EOL);
Logger::warning("Publicise: unable to create profile, deleting user $uid contact $self_contact");
DBA:e('DELETE FROM `user` WHERE `uid` = %d', intval($user['uid']));
DBA:e('DELETE FROM `contact` WHERE `id` = %d', intval($self_contact));
DBA::e('DELETE FROM `user` WHERE `uid` = %d', intval($user['uid']));
DBA::e('DELETE FROM `contact` WHERE `id` = %d', intval($self_contact));
return;
}
return $user;
@ -267,13 +267,13 @@ function publicise($a, &$contact, &$owner) {
// Check if we're changing our mind about a feed we earlier depublicised
Logger::info('@@@ Publicise: ' . 'SELECT * FROM `user` WHERE `account_expires_on` != "0000-00-00 00:00:00" AND `nickname` = "' . $contact['nick'] . '" AND `email` = "' . $owner['email'] . '" AND `page-flags` in (' . intval(PAGE_COMMUNITY) . ', ' . intval(PAGE_SOAPBOX) . ')');
$existing = DBA:e('SELECT * FROM `user` WHERE `account_expires_on` != "0000-00-00 00:00:00" AND `nickname` = "%s" AND `email` = "%s" AND `page-flags` in (%d, %d)',
$existing = DBA::e('SELECT * FROM `user` WHERE `account_expires_on` != "0000-00-00 00:00:00" AND `nickname` = "%s" AND `email` = "%s" AND `page-flags` in (%d, %d)',
DBA::escape($contact['nick']), DBA::escape($owner['email']), intval(PAGE_COMMUNITY), intval(PAGE_SOAPBOX));
if (count($existing) == 1) {
Logger::info('@@@ Publicise: there is existing');
$owner = $existing[0];
DBA:e('UPDATE `user` SET `account_expires_on` = "0000-00-00 00:00:00", `account_removed` = 0, `account_expired` = 0 WHERE `uid` = %d', intval($owner['uid']));
DBA:e('UPDATE `profile` SET `publish` = 1, `net-publish` = 1 WHERE `uid` = %d AND `is-default` = 1', intval($owner['uid']));
DBA::e('UPDATE `user` SET `account_expires_on` = "0000-00-00 00:00:00", `account_removed` = 0, `account_expired` = 0 WHERE `uid` = %d', intval($owner['uid']));
DBA::e('UPDATE `profile` SET `publish` = 1, `net-publish` = 1 WHERE `uid` = %d AND `is-default` = 1', intval($owner['uid']));
Logger::debug('Publicise: recycled previous user ' . $owner['uid']);
}
else {
@ -286,19 +286,19 @@ function publicise($a, &$contact, &$owner) {
}
Logger::info('Publicise: new contact user is ' . $owner['uid']);
$r = DBA:e("UPDATE `contact` SET `uid` = %d, `reason` = 'publicise', `hidden` = 1 WHERE id = %d", intval($owner['uid']), intval($contact['id']));
$r = DBA::e("UPDATE `contact` SET `uid` = %d, `reason` = 'publicise', `hidden` = 1 WHERE id = %d", intval($owner['uid']), intval($contact['id']));
if (!$r) {
Logger::warning('Publicise: update contact failed, user is probably in a bad state ' . $user['uid']);
}
$contact['uid'] = $owner['uid'];
$contact['reason'] = 'publicise';
$contact['hidden'] = 1;
$r = DBA:e("UPDATE `item` SET `uid` = %d, type = 'wall', wall = 1, private = 0 WHERE `contact-id` = %d",
$r = DBA::e("UPDATE `item` SET `uid` = %d, type = 'wall', wall = 1, private = 0 WHERE `contact-id` = %d",
intval($owner['uid']), intval($contact['id']));
Logger::debug('Publicise: moved items from contact ' . $contact['id'] . ' to uid ' . $owner['uid']);
// Update the retriever config
$r = DBA:e("UPDATE `retriever_rule` SET `uid` = %d WHERE `contact-id` = %d",
$r = DBA::e("UPDATE `retriever_rule` SET `uid` = %d WHERE `contact-id` = %d",
intval($owner['uid']), intval($contact['id']));
info(sprintf(t("Moved feed \"%s\" to dedicated account"), $contact['name']) . EOL);
@ -306,7 +306,7 @@ function publicise($a, &$contact, &$owner) {
}
function publicise_self_contact($uid) {
$r = DBA:e('SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1', intval($uid));
$r = DBA::e('SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1', intval($uid));
if (count($r) != 1) {
Logger::warning('Publicise: unexpected number of self contacts for user ' . $uid);
return;
@ -330,7 +330,7 @@ function depublicise($a, $contact, $user) {
// If the local_user() is subscribed to the feed, take ownership
// of the feed and all its items and photos. Otherwise they will
// be deleted when the account expires.
$r = DBA:e('SELECT * FROM `contact` WHERE `uid` = %d AND `url` = "%s"',
$r = DBA::e('SELECT * FROM `contact` WHERE `uid` = %d AND `url` = "%s"',
intval(local_user()), DBA::escape($self_contact['url']));
if (count($r)) {
// Delete the contact to the feed user and any
@ -338,32 +338,32 @@ function depublicise($a, $contact, $user) {
// which will be brought back into the local_user's feed along
// with the feed contact itself.
foreach ($r as $my_contact) {
DBA:e('DELETE FROM `item` WHERE `contact-id` = %d', intval($my_contact['id']));
DBA:e('DELETE FROM `contact` WHERE `id` = %d', intval($my_contact['id']));
DBA::e('DELETE FROM `item` WHERE `contact-id` = %d', intval($my_contact['id']));
DBA::e('DELETE FROM `contact` WHERE `id` = %d', intval($my_contact['id']));
}
// Move the feed contact to local_user. Existing items stay
// attached to the original feed contact, but must have their uid
// updated. Also update the fields we scribbled over in
// publicise_post_remote_hook.
DBA:e('UPDATE `contact` SET `uid` = %d, `reason` = "", hidden = 0 WHERE id = %d',
DBA::e('UPDATE `contact` SET `uid` = %d, `reason` = "", hidden = 0 WHERE id = %d',
intval(local_user()), intval($contact['id']));
DBA:e('UPDATE `item` SET `uid` = %d, `wall` = 0, `type` = "remote", `private` = 2 WHERE `contact-id` = %d',
DBA::e('UPDATE `item` SET `uid` = %d, `wall` = 0, `type` = "remote", `private` = 2 WHERE `contact-id` = %d',
intval(local_user()), intval($contact['id']));
// Take ownership of any photos created by the feed user
DBA:e('UPDATE `photo` SET `uid` = %d WHERE `uid` = %d',
DBA::e('UPDATE `photo` SET `uid` = %d WHERE `uid` = %d',
intval(local_user()), intval($user['uid']));
// Update the retriever config
$r = DBA:e("UPDATE `retriever_rule` SET `uid` = %d WHERE `contact-id` = %d",
$r = DBA::e("UPDATE `retriever_rule` SET `uid` = %d WHERE `contact-id` = %d",
intval($owner['uid']), intval($contact['id']));
}
// Set the account to removed and expired right now. It will be cleaned up by cron after 3 days, giving a chance to change your mind
DBA:e('UPDATE `user` SET `account_removed` = 1, `account_expired` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d',
DBA::e('UPDATE `user` SET `account_removed` = 1, `account_expired` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d',
intval($user['uid']));
DBA:e('UPDATE `profile` SET `publish` = 0, `net-publish` = 0 WHERE `uid` = %d AND `is-default` = 1', intval($user['uid']));
DBA::e('UPDATE `profile` SET `publish` = 0, `net-publish` = 0 WHERE `uid` = %d AND `is-default` = 1', intval($user['uid']));
info(sprintf(t("Removed dedicated account for feed \"%s\""), $contact['name']) . EOL);
}
@ -393,22 +393,22 @@ function publicise_addon_admin_post ($a) {
}
}
if ($_POST['publicise-expire-' . $contact['id']] != $user['expire']) {
DBA:e('UPDATE `user` SET `expire` = %d WHERE `uid` = %d',
DBA::e('UPDATE `user` SET `expire` = %d WHERE `uid` = %d',
intval($_POST['publicise-expire-' . $contact['id']]), intval($user['uid']));
}
if ($_POST['publicise-comments-' . $contact['id']]) {
if ($user['page-flags'] != PAGE_COMMUNITY) {
DBA:e('UPDATE `user` SET `page-flags` = %d WHERE `uid` = %d',
DBA::e('UPDATE `user` SET `page-flags` = %d WHERE `uid` = %d',
intval(PAGE_COMMUNITY), intval($user['uid']));
DBA:e('UPDATE `contact` SET `rel` = %d WHERE `uid` = %d AND `network` = "dfrn"',
DBA::e('UPDATE `contact` SET `rel` = %d WHERE `uid` = %d AND `network` = "dfrn"',
intval(CONTACT_IS_SHARING), intval($user['uid']));
}
}
else {
if ($user['page-flags'] != PAGE_SOAPBOX) {
DBA:e('UPDATE `user` SET `page-flags` = %d WHERE `uid` = %d',
DBA::e('UPDATE `user` SET `page-flags` = %d WHERE `uid` = %d',
intval(PAGE_SOAPBOX), intval($user['uid']));
DBA:e('UPDATE `contact` SET `rel` = %d WHERE `uid` = %d AND `network` = "dfrn"',
DBA::e('UPDATE `contact` SET `rel` = %d WHERE `uid` = %d AND `network` = "dfrn"',
intval(CONTACT_IS_FOLLOWER), intval($user['uid']));
}
}
@ -417,7 +417,7 @@ function publicise_addon_admin_post ($a) {
}
function publicise_post_remote_hook(&$a, &$item) {
$r1 = DBA:e("SELECT `uid` FROM `contact` WHERE `id` = %d AND `reason` = 'publicise'", intval($item['contact-id']));
$r1 = DBA::e("SELECT `uid` FROM `contact` WHERE `id` = %d AND `reason` = 'publicise'", intval($item['contact-id']));
if (!$r1) {
return;
}