BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. #declare(strict_types=1);
  3. $ENV = ENV::go();
  4. // We keep torrent groups cached. However, the peer counts change often, so our solutions are to not cache them for long, or to update them. Here is where we updated them.
  5. if ((!isset($argv[1]) || $argv[1]!== $ENV->getPriv('SCHEDULE_KEY'))
  6. && !check_perms('admin_schedule')) { // auth fix to let people with perms hit this page
  7. error(403);
  8. }
  9. if (check_perms('admin_schedule')) {
  10. View::show_header();
  11. echo '<pre>';
  12. }
  13. ignore_user_abort();
  14. ini_set('max_execution_time', 300);
  15. ob_end_flush();
  16. gc_enable();
  17. $Cache->InternalCache = false; // We don't want PHP to cache all results internally
  18. $DB->query("TRUNCATE TABLE torrents_peerlists_compare");
  19. $DB->query("
  20. INSERT INTO torrents_peerlists_compare
  21. SELECT ID, GroupID, Seeders, Leechers, Snatched
  22. FROM torrents
  23. ON DUPLICATE KEY UPDATE
  24. Seeders = VALUES(Seeders),
  25. Leechers = VALUES(Leechers),
  26. Snatches = VALUES(Snatches)");
  27. $DB->query("
  28. CREATE TEMPORARY TABLE tpc_temp
  29. (TorrentID int, GroupID int, Seeders int, Leechers int, Snatched int,
  30. PRIMARY KEY (GroupID, TorrentID))");
  31. $DB->query("
  32. INSERT INTO tpc_temp
  33. SELECT t2.*
  34. FROM torrents_peerlists AS t1
  35. JOIN torrents_peerlists_compare AS t2
  36. USING(TorrentID)
  37. WHERE t1.Seeders != t2.Seeders
  38. OR t1.Leechers != t2.Leechers
  39. OR t1.Snatches != t2.Snatches");
  40. $StepSize = 30000;
  41. $DB->query("
  42. SELECT *
  43. FROM tpc_temp
  44. ORDER BY GroupID ASC, TorrentID ASC
  45. LIMIT $StepSize");
  46. $RowNum = 0;
  47. $LastGroupID = 0;
  48. $UpdatedKeys = $UncachedGroups = 0;
  49. list($TorrentID, $GroupID, $Seeders, $Leechers, $Snatches) = $DB->next_record(MYSQLI_NUM, false);
  50. while ($TorrentID) {
  51. if ($LastGroupID != $GroupID) {
  52. $CachedData = $Cache->get_value("torrent_group_$GroupID");
  53. if ($CachedData !== false) {
  54. if (isset($CachedData['ver']) && $CachedData['ver'] == Cache::GROUP_VERSION) {
  55. $CachedStats = &$CachedData['d']['Torrents'];
  56. }
  57. } else {
  58. $UncachedGroups++;
  59. }
  60. $LastGroupID = $GroupID;
  61. }
  62. while ($LastGroupID == $GroupID) {
  63. $RowNum++;
  64. if (isset($CachedStats) && is_array($CachedStats[$TorrentID])) {
  65. $OldValues = &$CachedStats[$TorrentID];
  66. $OldValues['Seeders'] = $Seeders;
  67. $OldValues['Leechers'] = $Leechers;
  68. $OldValues['Snatched'] = $Snatches;
  69. $Changed = true;
  70. unset($OldValues);
  71. }
  72. if (!($RowNum % $StepSize)) {
  73. $DB->query("
  74. SELECT *
  75. FROM tpc_temp
  76. WHERE GroupID > $GroupID
  77. OR (GroupID = $GroupID AND TorrentID > $TorrentID)
  78. ORDER BY GroupID ASC, TorrentID ASC
  79. LIMIT $StepSize");
  80. }
  81. $LastGroupID = $GroupID;
  82. list($TorrentID, $GroupID, $Seeders, $Leechers, $Snatches) = $DB->next_record(MYSQLI_NUM, false);
  83. }
  84. if ($Changed) {
  85. $Cache->cache_value("torrent_group_$LastGroupID", $CachedData, 0);
  86. unset($CachedStats);
  87. $UpdatedKeys++;
  88. $Changed = false;
  89. }
  90. }
  91. printf("Updated %d keys, skipped %d keys in %.6fs (%d kB memory)\n", $UpdatedKeys, $UncachedGroups, microtime(true) - $ScriptStartTime, memory_get_usage(true) >> 10);
  92. $DB->query("TRUNCATE TABLE torrents_peerlists");
  93. $DB->query("
  94. INSERT INTO torrents_peerlists
  95. SELECT *
  96. FROM torrents_peerlists_compare");
  97. if (check_perms('admin_schedule')) {
  98. echo '<pre>';
  99. View::show_footer();
  100. }