diff --git a/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php b/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php index 50f7f9f7..1c12f2c6 100644 --- a/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php +++ b/app/Console/Commands/Upgrade/MigrateTorrentsTableTextColumn.php @@ -29,7 +29,7 @@ class MigrateTorrentsTableTextColumn extends Command public function handle() { if (Schema::hasTable("torrent_extras") && Schema::hasColumn("torrents", "descr")) { - NexusDB::statement("insert into torrent_extras (torrent_id, descr, media_info, nfo, pt_gen, created_at) select id, descr, technical_info, nfo, pt_gen, now() from torrents on duplicate key update torrent_id = values(torrent_id)"); + NexusDB::statement("insert into torrent_extras (torrent_id, descr, media_info, nfo, pt_gen, created_at) select id, descr, technical_info, nfo, pt_gen, now() from torrents " . NexusDB::upsertField(['torrent_id'], ['torrent_id'])); } $columns = ["ori_descr", "descr", "nfo", "technical_info", "pt_gen"]; $sql = "alter table torrents "; diff --git a/app/Jobs/RemoveUserDonorStatus.php b/app/Jobs/RemoveUserDonorStatus.php index f17ec33b..c47def22 100644 --- a/app/Jobs/RemoveUserDonorStatus.php +++ b/app/Jobs/RemoveUserDonorStatus.php @@ -28,7 +28,6 @@ class RemoveUserDonorStatus ->with('language') ->where('donor', 'yes') ->whereNotNull('donoruntil') - ->where('donoruntil', '!=', '0000-00-00 00:00:00') ->where('donoruntil', '<', now()) ->get(); $userModifyLogs = []; diff --git a/app/Models/User.php b/app/Models/User.php index 88e19655..019161f9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -372,7 +372,6 @@ class User extends Authenticatable implements FilamentUser, HasName { return $query->where('donor', 'yes')->where(function (Builder $query) { return $query->whereNull('donoruntil') - ->orWhere('donoruntil', '0000-00-00 00:00:00') ->orWhere('donoruntil', '>=', now()); }); } diff --git a/app/Repositories/AttendanceRepository.php b/app/Repositories/AttendanceRepository.php index 49fdfad3..08b67d52 100644 --- a/app/Repositories/AttendanceRepository.php +++ b/app/Repositories/AttendanceRepository.php @@ -244,8 +244,8 @@ class AttendanceRepository extends BaseRepository return 0; } $sql = sprintf( - "insert into `%s` (`uid`, `points`, `date`) values %s on duplicate key update `uid` = values(`uid`)", - $table, implode(',', $insert) + 'insert into %s (uid, points, "date") values %s %s', + $table, implode(',', $insert), NexusDB::upsertField(['uid'], ['uid']) ); NexusDB::statement($sql); $insertCount = count($insert); diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index c0318351..d5790f14 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -1049,13 +1049,13 @@ class ExamRepository extends BaseRepository if ($donateStatus == User::DONATE_YES) { $baseQuery->where(function (Builder $query) { $query->where('donor', 'yes')->where(function (Builder $query) { - $query->where('donoruntil', '0000-00-00 00:00:00')->orWhereNull('donoruntil')->orWhere('donoruntil', '>=', Carbon::now()); + $query->whereNull('donoruntil')->orWhere('donoruntil', '>=', Carbon::now()); }); }); } elseif ($donateStatus == User::DONATE_NO) { $baseQuery->where(function (Builder $query) { $query->where('donor', 'no')->orWhere(function (Builder $query) { - $query->where('donoruntil', '!=','0000-00-00 00:00:00')->whereNotNull('donoruntil')->where('donoruntil', '<', Carbon::now()); + $query->whereNotNull('donoruntil')->where('donoruntil', '<', Carbon::now()); }); }); } else { diff --git a/app/Repositories/HitAndRunRepository.php b/app/Repositories/HitAndRunRepository.php index 5f0c65fe..78d769a9 100644 --- a/app/Repositories/HitAndRunRepository.php +++ b/app/Repositories/HitAndRunRepository.php @@ -400,7 +400,7 @@ class HitAndRunRepository extends BaseRepository ->selectRaw("count(*) as counts, uid") ->where('status', HitAndRun::STATUS_UNREACHED) ->groupBy('uid') - ->having("counts", '>=', $disableCounts) + ->havingRaw("count(*) >= $disableCounts") ; if ($setting['diff_in_section']) { $query->whereHas('torrent.basic_category', function (Builder $query) use ($setting) { diff --git a/app/Repositories/SettingRepository.php b/app/Repositories/SettingRepository.php index fe92bc93..bceb76f7 100644 --- a/app/Repositories/SettingRepository.php +++ b/app/Repositories/SettingRepository.php @@ -45,8 +45,8 @@ class SettingRepository extends BaseRepository return true; } $sql = sprintf( - "insert into `%s` (`name`, `value`) values %s on duplicate key update `value` = values(`value`)", - $settingModel->getTable(), implode(', ', $values) + 'insert into %s (name, "value") values %s %s', + $settingModel->getTable(), implode(', ', $values), NexusDB::upsertField(['name'], ['value']) ); $result = DB::insert($sql); do_log("sql: $sql, result: $result"); diff --git a/app/Repositories/TagRepository.php b/app/Repositories/TagRepository.php index 218536c4..6f7bc14f 100644 --- a/app/Repositories/TagRepository.php +++ b/app/Repositories/TagRepository.php @@ -133,7 +133,7 @@ class TagRepository extends BaseRepository } $page++; } - $sql .= sprintf("%s on duplicate key update updated_at = values(updated_at)", implode(', ', $values)); + $sql .= sprintf("%s %s", implode(', ', $values), NexusDB::upsertField(['torrent_id', 'tag_id'], ['updated_at'])); do_log("migrate sql: $sql"); NexusDB::statement($sql); do_log("[MIGRATE_TORRENT_TAG] done!"); diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index b3c4c37a..7d03b39a 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -749,7 +749,7 @@ class TorrentRepository extends BaseRepository $values[] = sprintf("(%s, %s, '%s', '%s')", $torrentId, $tagId, $time, $time); } } - $sql .= implode(', ', $values) . " on duplicate key update updated_at = values(updated_at)"; + $sql .= implode(', ', $values) . " " . NexusDB::upsertField(['torrent_id', 'tag_id'], ['updated_at']); if ($remove) { TorrentTag::query()->whereIn('torrent_id', $idArr)->delete(); } diff --git a/composer.json b/composer.json index 4d1071b5..955c35a4 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "ext-xml": "*", "ext-zend-opcache": "*", "ext-zip": "*", + "ext-pdo": "*", "calebporzio/sushi": "^2.5", "cybercog/laravel-clickhouse": "dev-master", "elasticsearch/elasticsearch": "^7.16", @@ -87,7 +88,8 @@ "@php artisan key:generate --ansi" ], "post-update-cmd": [ - "@php artisan filament:upgrade" + "@php artisan filament:upgrade", + "@php artisan livewire:publish --assets" ] }, "extra": { diff --git a/include/cleanup.php b/include/cleanup.php index 10ee2bdd..7c43610f 100644 --- a/include/cleanup.php +++ b/include/cleanup.php @@ -139,7 +139,8 @@ function promotion($class, $down_floor_gb, $minratio, $time_week, $addinvite = 0 function demotion($class,$deratio){ $newclass = $class - 1; - $sql = "SELECT id FROM users WHERE class = $class AND uploaded / downloaded < $deratio"; +// $sql = "SELECT id FROM users WHERE class = $class AND uploaded / downloaded < $deratio"; + $sql = "SELECT id FROM users WHERE class = $class AND uploaded < downloaded * $deratio"; $res = sql_query($sql) or sqlerr(__FILE__, __LINE__); $matchUserCount = mysql_num_rows($res); do_log("sql: $sql, match user count: $matchUserCount"); @@ -585,7 +586,8 @@ function docleanup($forceAll = 0, $printProgress = false) { //3.delete unconfirmed accounts $deadtime = time() - $signup_timeout; - sql_query("DELETE FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME($deadtime) AND last_login < FROM_UNIXTIME($deadtime) AND last_access < FROM_UNIXTIME($deadtime)") or sqlerr(__FILE__, __LINE__); + $deadlineField = \Nexus\Database\NexusDB::fromUnixTimestampField($deadtime); + sql_query("DELETE FROM users WHERE status = 'pending' AND added < $deadlineField AND last_login < $deadlineField AND last_access < $deadlineField") or sqlerr(__FILE__, __LINE__); // $query = \App\Models\User::query() // ->where('status', 'pending') // ->whereRaw("added < FROM_UNIXTIME($deadtime)") @@ -619,7 +621,7 @@ function docleanup($forceAll = 0, $printProgress = false) { } //7.delete regimage codes - sql_query("TRUNCATE TABLE `regimages`") or sqlerr(__FILE__, __LINE__); + sql_query("TRUNCATE TABLE regimages") or sqlerr(__FILE__, __LINE__); $log = "delete regimage codes"; do_log($log); if ($printProgress) { @@ -1069,7 +1071,7 @@ function docleanup($forceAll = 0, $printProgress = false) { //delete old shoutbox $until = TIMENOW - $length; - sql_query("DELETE FROM shoutbox WHERE `date` < ".sqlesc($until)) or sqlerr(__FILE__, __LINE__); + sql_query("DELETE FROM shoutbox WHERE date < $until") or sqlerr(__FILE__, __LINE__); $log = "delete old shoutbox"; do_log($log); if ($printProgress) { @@ -1078,7 +1080,7 @@ function docleanup($forceAll = 0, $printProgress = false) { //delete old general log $until = date("Y-m-d H:i:s",(TIMENOW - $length)); - sql_query("DELETE FROM sitelog WHERE added < ".sqlesc($until)) or sqlerr(__FILE__, __LINE__); + sql_query("DELETE FROM sitelog WHERE added < " . sqlesc($until)) or sqlerr(__FILE__, __LINE__); $log = "delete old general log"; do_log($log); if ($printProgress) { @@ -1157,7 +1159,10 @@ function docleanup($forceAll = 0, $printProgress = false) { //8.lock topics where last post was made more than x days ago $secs = 365*24*60*60; - sql_query("UPDATE topics, posts SET topics.locked='yes' WHERE topics.lastpost = posts.id AND topics.sticky = 'no' AND UNIX_TIMESTAMP(posts.added) < ".TIMENOW." - $secs") or sqlerr(__FILE__, __LINE__); + $postAddedField = \Nexus\Database\NexusDB::unixTimestampField('posts.added'); + $diff = TIMENOW - $secs; +// sql_query("UPDATE topics, posts SET topics.locked='yes' WHERE topics.lastpost = posts.id AND topics.sticky = 'no' AND $postAddedField < ".TIMENOW." - $secs") or sqlerr(__FILE__, __LINE__); + sql_query("UPDATE topics SET locked='yes' WHERE sticky = 'no' AND lastpost in (select id from posts where $postAddedField < $diff)"); $log = "lock topics where last post was made more than x days ago"; do_log($log); diff --git a/include/functions.php b/include/functions.php index 6a202c09..7d3c1c64 100644 --- a/include/functions.php +++ b/include/functions.php @@ -5418,14 +5418,7 @@ function saveSetting(string $prefix, array $nameAndValue, string $autoload = 'ye } $data[] = sprintf("(%s, %s, %s, %s, '%s')", sqlesc("$prefix.$name"), sqlesc($value), sqlesc($datetimeNow), sqlesc($datetimeNow), $autoload); } - $sql .= implode(",", $data); - if (\Nexus\Database\NexusDB::isMysql()) { - $sql .= " on duplicate key update value = values(value)"; - } else if (\Nexus\Database\NexusDB::isPgsql()) { - $sql .= " on conflict (name) do update set value = EXCLUDED.value"; - } else { - throw new \RuntimeException('Not supported database.'); - } + $sql .= implode(",", $data) . " " . \Nexus\Database\NexusDB::upsertField(['name'], ['value']); \Nexus\Database\NexusDB::statement($sql); clear_setting_cache(); do_action("nexus_setting_update"); diff --git a/nexus/Database/NexusDB.php b/nexus/Database/NexusDB.php index 90b225b8..5f124029 100644 --- a/nexus/Database/NexusDB.php +++ b/nexus/Database/NexusDB.php @@ -522,4 +522,39 @@ class NexusDB } } + public static function fromUnixTimestampField(int $timestamp): string + { + if (self::isMysql()) { + return sprintf("FROM_UNIXTIME(%d)", $timestamp); + } elseif (self::isPgsql()) { + return sprintf("to_timestamp(%d)", $timestamp); + } else { + throw new \RuntimeException('Not supported database.'); + } + } + + public static function upsertField(array $uniqueFields, array $updateFields): string + { + if (self::isMysql()) { + $updates = []; + foreach ($updateFields ?: ['id'] as $field) { + $updates[] = "`$field` = VALUES(`$field`)"; + } + return sprintf("ON DUPLICATE KEY UPDATE %s", implode(', ', $updates)); + } elseif (self::isPgsql()) { + if (empty($updateFields)) { + $updateStr = "NOTHING"; + } else { + $updates = []; + foreach ($updateFields as $field) { + $updates[] = "$field = EXCLUDED.$field"; + } + $updateStr = "UPDATE SET " . implode(', ', $updates); + } + return sprintf("ON CONFLICT (%s) DO %s", implode(', ', $uniqueFields), $updateStr); + } else { + throw new \RuntimeException('Not supported database.'); + } + } + } diff --git a/public/announce.php b/public/announce.php index 294336ab..bf691872 100644 --- a/public/announce.php +++ b/public/announce.php @@ -566,8 +566,8 @@ if (($left > 0 || $event == "completed") && $az['class'] < \App\Models\HitAndRun if ($snatchInfo['downloaded'] >= $requiredDownloaded) { $nowStr = date('Y-m-d H:i:s'); $sql = sprintf( - "insert into hit_and_runs (uid, torrent_id, snatched_id, created_at, updated_at) values (%d, %d, %d, '%s', '%s') on duplicate key update updated_at = '%s'", - $userid, $torrentid, $snatchInfo['id'], $nowStr, $nowStr, $nowStr + "insert into hit_and_runs (uid, torrent_id, snatched_id, created_at, updated_at) values (%d, %d, %d, '%s', '%s') %s", + $userid, $torrentid, $snatchInfo['id'], $nowStr, $nowStr, \Nexus\Database\NexusDB::upsertField(['uid', 'torrent_id'], ['updated_at']) ); $affectedRows = sql_query($sql); $hitAndRunId = mysql_insert_id();