Skip to content

Commit

Permalink
Restructure Gazelle\Manager\Torrent into four classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Spine authored and itismadness committed Aug 15, 2021
1 parent 78863ea commit 3da718a
Show file tree
Hide file tree
Showing 44 changed files with 1,448 additions and 1,448 deletions.
2 changes: 1 addition & 1 deletion .docker/mysql/mysqld_sql_mode.cnf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mysqld]
sql_mode = ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
sql_mode = ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE
userstat = on

[client]
Expand Down
4 changes: 3 additions & 1 deletion app/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ public function create(int $userId, string $type, int $id) {
// RSS feed stuff
$Feed = new \Feed;

[$group, $list] = (new Manager\Torrent)->groupInfo($id);
$tgroup = (new Manager\Torrent)->findById($id);
$group = $tgroup->info();
$list = $tgroup->torrentList();
$labelMan = new Manager\TorrentLabel;
$userMan = new Manager\User;
foreach ($list as $torrent) {
Expand Down
9 changes: 7 additions & 2 deletions app/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,18 @@ public function add(array $info, $folderName = null) {
$this->skip($info);
return;
}
$contents = $this->torMan->torrentBody($info['TorrentID'], $this->user->announceUrl());
$torrent = $this->torMan->findById($info['TorrentID']);
if (is_null($torrent)) {
$this->fail($info);
return;
}
$contents = $torrent->torrentBody($this->user->announceUrl());
if ($contents === '') {
$this->fail($info);
return;
}
$folder = is_null($folderName) ? '' : (safeFilename($folderName) . '/');
$name = $this->torMan->torrentFilename($info, false, MAX_PATH_LEN - strlen($folder));
$name = $torrent->torrentFilename(false, MAX_PATH_LEN - strlen($folder));
$this->zip->addFile("$folder$name", $contents);

$this->totalAdded++;
Expand Down
12 changes: 5 additions & 7 deletions app/Contest/AbstractContest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ public function leaderboard(int $limit, int $offset): array {
$leaderboard = $this->db->to_array(false, MYSQLI_ASSOC);
$leaderboardCount = count($leaderboard);
for ($i = 0; $i < $leaderboardCount; $i++) {
[$group, $torrent] = $torMan
->setTorrentId($leaderboard[$i]['last_entry_id'])
->setGroupId($leaderboard[$i]['group_id'])
->torrentInfo();
$torrent = $torMan->findById($leaderboard[$i]['last_entry_id']);
$group = $torrent->group();
$leaderboard[$i]['last_entry_link'] = sprintf(
'%s - <a href="torrents.php?id=%d&amp;torrentid=%d">%s</a> - %s',
$torMan->artistHtml(),
$group->artistHtml(),
$leaderboard[$i]['group_id'],
$leaderboard[$i]['last_entry_id'],
$group['Name'],
$labelMan->load($torrent)->label()
$group->name(),
$labelMan->load($torrent->info())->label()
);
}
$this->cache->cache_value($key, $leaderboard, 3600);
Expand Down
142 changes: 61 additions & 81 deletions app/Json/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
namespace Gazelle\Json;

class Torrent extends \Gazelle\Json {
protected $id;
protected $infohash;
protected $torrent;
protected $userId;
protected $showSnatched;
protected $showSnatched = false;

public function __construct() {
parent::__construct();
$this->setMode(JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR);
}

public function setViewer(int $userId) {
public function setViewerId(int $userId) {
$this->userId = $userId;
return $this;
}
Expand All @@ -23,29 +22,21 @@ public function setShowSnatched(int $showSnatched) {
return $this;
}

public function setId(int $id) {
if (!$id) {
public function findById(int $id) {
$this->torrent = (new \Gazelle\Manager\Torrent)->findById($id);
if (!$this->torrent) {
$this->failure("bad id parameter");
return null;
}
$this->id = $id;
$this->infohash = null;
return $this;
}

public function setIdFromHash(string $hash) {
$torMan = new \Gazelle\Manager\Torrent;
if (!$torMan->isValidHash($hash)) {
public function findByInfohash(string $hash) {
$this->torrent = (new \Gazelle\Manager\Torrent)->findByInfohash($hash);
if (!$this->torrent) {
$this->failure("bad hash parameter");
return null;
} else {
$this->id = $torMan->hashToTorrentId($hash);
if (!$this->id) {
$this->failure("bad hash parameter");
return null;
}
}
$this->infohash = $hash;
return $this;
}

Expand All @@ -55,88 +46,77 @@ public function payload(): ?array {
return null;
}

$torMan = new \Gazelle\Manager\Torrent;
[$details, $torrent] = $torMan
->setTorrentId($this->id)
->setViewer($this->userId)
->setShowSnatched($this->showSnatched ?? 0)
->showFallbackImage(false)
->torrentInfo();
if (!$details) {
$this->failure("bad id parameter");
return null;
}
$groupID = $details['ID'];
$this->torrent->setViewerId($this->userId)->setShowSnatched($this->showSnatched);
$info = $this->torrent->info();
$group = $this->torrent->group()->showFallbackImage(false)->info();

// TODO: implement as a Gazelle class
global $Categories;
$categoryName = ($details['CategoryID'] == 0) ? "Unknown" : $Categories[$details['CategoryID'] - 1];
$categoryName = ($group['CategoryID'] == 0) ? "Unknown" : $Categories[$group['CategoryID'] - 1];

// Convert file list back to the old format
$fileList = explode("\n", $torrent['FileList']);
$torMan = new \Gazelle\Manager\Torrent;
$fileList = explode("\n", $info['FileList']);
foreach ($fileList as &$file) {
$file = $torMan->apiFilename($file);
}
unset($file);

$uploader = (new \Gazelle\Manager\User)->findById($torrent['UserID']);
$username = $uploader ? $uploader->username() : '';

return [
'group' => [
'wikiBody' => \Text::full_format($details['WikiBody']),
'wikiBBcode' => $details['WikiBody'],
'wikiImage' => $details['WikiImage'],
'id' => $details['ID'],
'name' => $details['Name'],
'year' => $details['Year'],
'recordLabel' => $details['RecordLabel'] ?? '',
'catalogueNumber' => $details['CatalogueNumber'] ?? '',
'releaseType' => $details['ReleaseType'] ?? '',
'releaseTypeName' => (new \Gazelle\ReleaseType)->findNameById($details['ReleaseType']),
'categoryId' => $details['CategoryID'],
'wikiBody' => \Text::full_format($group['WikiBody']),
'wikiBBcode' => $group['WikiBody'],
'wikiImage' => $group['WikiImage'],
'id' => $group['ID'],
'name' => $group['Name'],
'year' => $group['Year'],
'recordLabel' => $group['RecordLabel'] ?? '',
'catalogueNumber' => $group['CatalogueNumber'] ?? '',
'releaseType' => $group['ReleaseType'] ?? '',
'releaseTypeName' => (new \Gazelle\ReleaseType)->findNameById($group['ReleaseType']),
'categoryId' => $group['CategoryID'],
'categoryName' => $categoryName,
'time' => $details['Time'],
'vanityHouse' => $details['VanityHouse'],
'isBookmarked' => (new \Gazelle\Bookmark)->isTorrentBookmarked($this->userId, $groupID),
'tags' => explode('|', $details['tagNames']),
'time' => $group['Time'],
'vanityHouse' => $group['VanityHouse'],
'isBookmarked' => (new \Gazelle\Bookmark)->isTorrentBookmarked($this->userId, $group['ID']),
'tags' => explode('|', $group['tagNames']),
'musicInfo' => ($categoryName != "Music")
? null : \Artists::get_artist_by_type($groupID),
? null : \Artists::get_artist_by_type($group['ID']),
],
'torrent' => array_merge(
!is_null($this->infohash) || $torrent['UserID'] == $this->userId
? [ 'infoHash' => $torrent['InfoHash'] ]
!is_null($this->torrent->infohash()) || $this->torrent->uploader()->id() == $this->userId
? [ 'infoHash' => $this->torrent->infohash() ]
: [],
[
'id' => $torrent['ID'],
'media' => $torrent['Media'],
'format' => $torrent['Format'],
'encoding' => $torrent['Encoding'],
'remastered' => $torrent['Remastered'] == 1,
'remasterYear' => (int)$torrent['RemasterYear'],
'remasterTitle' => $torrent['RemasterTitle'] ?? '',
'remasterRecordLabel' => $torrent['RemasterRecordLabel'] ?? '',
'remasterCatalogueNumber' => $torrent['RemasterCatalogueNumber'] ?? '',
'scene' => $torrent['Scene'],
'hasLog' => $torrent['HasLog'],
'hasCue' => $torrent['HasCue'],
'logScore' => $torrent['LogScore'],
'logChecksum' => $torrent['LogChecksum'],
'logCount' => $torrent['LogCount'],
'ripLogIds' => $torrent['ripLogIds'],
'fileCount' => $torrent['FileCount'],
'size' => $torrent['Size'],
'seeders' => $torrent['Seeders'],
'leechers' => $torrent['Leechers'],
'snatched' => $torrent['Snatched'],
'freeTorrent' => $torrent['FreeTorrent'],
'reported' => count(\Torrents::get_reports($this->id)) > 0,
'time' => $torrent['Time'],
'description' => $torrent['Description'],
'id' => $this->torrent->id(),
'media' => $info['Media'],
'format' => $info['Format'],
'encoding' => $info['Encoding'],
'remastered' => $this->torrent->isRemastered(),
'remasterYear' => $info['RemasterYear'],
'remasterTitle' => $info['RemasterTitle'],
'remasterRecordLabel' => $info['RemasterRecordLabel'],
'remasterCatalogueNumber' => $info['RemasterCatalogueNumber'],
'scene' => $info['Scene'],
'hasLog' => $info['HasLog'],
'hasCue' => $info['HasCue'],
'logScore' => $info['LogScore'],
'logChecksum' => $info['LogChecksum'],
'logCount' => $info['LogCount'],
'ripLogIds' => $info['ripLogIds'],
'fileCount' => $info['FileCount'],
'size' => $info['Size'],
'seeders' => $info['Seeders'],
'leechers' => $info['Leechers'],
'snatched' => $info['Snatched'],
'freeTorrent' => $info['FreeTorrent'],
'reported' => count(\Torrents::get_reports($this->torrent->id())) > 0,
'time' => $info['Time'],
'description' => $info['Description'],
'fileList' => implode('|||', $fileList),
'filePath' => $torrent['FilePath'],
'userId' => $torrent['UserID'],
'username' => $username,
'filePath' => $info['FilePath'],
'userId' => $info['UserID'],
'username' => $this->torrent->uploader()->username(),
]
),
];
Expand Down
2 changes: 1 addition & 1 deletion app/Manager/Better.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function removeAttribute(string $type, int $id) {
', $table), $id
);

$this->cache->delete_value('torrents_details_' . (new \Gazelle\Manager\Torrent())->idToGroupId($id));
$this->cache->delete_value('torrents_details_' . (new \Gazelle\Manager\Torrent())->findById($id)->groupId());
}

public function missing(string $type, string $filter, string $search, int $limit, int $offset, int $userId) {
Expand Down
Loading

0 comments on commit 3da718a

Please sign in to comment.