this is more correcter

This commit is contained in:
Matthew Exon 2022-03-02 21:20:20 +01:00
parent 7a9e5c5766
commit cd4c4f21bc

View file

@ -25,6 +25,7 @@ use Friendica\Core\Logger;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Util\Images; use Friendica\Util\Images;
use Friendica\Util\DateTimeFormat;
use Friendica\DI; use Friendica\DI;
if (!defined('PHOTOTRACK_DEFAULT_BATCH_SIZE')) { if (!defined('PHOTOTRACK_DEFAULT_BATCH_SIZE')) {
@ -68,10 +69,10 @@ function phototrack_module() {}
function phototrack_finished_row($table, $id) { function phototrack_finished_row($table, $id) {
$existing = DBA::selectFirst('phototrack_row_check', ['id'], ['table' => $table, 'row-id' => $id]); $existing = DBA::selectFirst('phototrack_row_check', ['id'], ['table' => $table, 'row-id' => $id]);
if (!is_bool($existing)) { if (!is_bool($existing)) {
DBA::e("UPDATE phototrack_row_check SET checked = NOW() WHERE `table` = '$table' AND `row-id` = '$id'"); DBA::update('phototrack_row_check', ['checked' => DateTimeFormat::utcNow()], ['table' => $table, 'row-id' = $id]);
} }
else { else {
DBA::e("INSERT INTO phototrack_row_check (`table`, `row-id`, `checked`) VALUES ('$table', '$id', NOW())"); DBA::insert('phototrack_row_check', ['table' => $table, 'row-id' = $id, 'checked' => DateTimeFormat::utcNow()]);
} }
} }
@ -87,17 +88,17 @@ function phototrack_photo_use($photo, $table, $field, $id) {
if (strlen($photo) != 32) { if (strlen($photo) != 32) {
return; return;
} }
$r = DBA::p("SELECT `resource-id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", DBA::escape($photo)); $r = DBA::selectFirst('photo', ['resource-id'], ['resource-id' => $photo]);
if (!count($r)) { if (!DBA::isResult($r)) {
return; return;
} }
$rid = $r[0]['resource-id']; $rid = $r['resource-id'];
$existing = DBA::p("SELECT id FROM phototrack_photo_use WHERE `resource-id` = '$rid' AND `table` = '$table' AND `field` = '$field' AND `row-id` = '$id'"); $existing = DBA::selectFirst('phototrack_photo_use', ['id'], ['resource-id' => $rid, 'table' => $table, 'field' => $field, 'row-id' = $id]);
if (count($existing)) { if (DBA::isResult($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::update('phototrack_photo_use', ['checked' => DateTimeFormat::utcNow()], ['resource-id' => $rid, 'table' => $table, 'field' => $field, 'row-id' = $id]);
} }
else { else {
DBA::e("INSERT INTO phototrack_photo_use (`resource-id`, `table`, `field`, `row-id`, `checked`) VALUES ('$rid', '$table', '$field', '$id', NOW())"); DBA::insert('phototrack_photo_use', ['resource-id' => $rid, 'table' => $table, 'field' => $field, 'row-id' = $id, 'checked' => DateTimeFormat::utcNow()]);
} }
} }
@ -199,11 +200,13 @@ function phototrack_batch_size() {
function phototrack_search_table($a, $table) { function phototrack_search_table($a, $table) {
$batch_size = phototrack_batch_size(); $batch_size = phototrack_batch_size();
$rows = DBA::p("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::p("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) { if (DBA::isResult($rows)) {
while ($row = DBA::fetch($rows)) {
phototrack_check_row($a, $table, $row); phototrack_check_row($a, $table, $row);
} }
}
$r = DBA::p("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::p("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(*)']; $remaining = DBA::fetch($r)['COUNT(*)'];
Logger::info('phototrack: searched ' . count($rows) . ' rows in table ' . $table . ', ' . $remaining . ' still remaining to search'); Logger::info('phototrack: searched ' . count($rows) . ' rows in table ' . $table . ', ' . $remaining . ' still remaining to search');
return $remaining; return $remaining;
} }