diff --git a/Changelog b/Changelog index 21aada7e8..2ea4a72c6 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-01-16 DariusIII + * Chg: Use Group model and remove nntmux\Groups class * Chg: Update Books class processBookReleases function * Fix: Fix ReleaseComment error in profile page * Chg: Update laravel/framework to version 5.5.29 and spatie/fractalistic to latest diff --git a/app/Models/Group.php b/app/Models/Group.php index 5d6190cad..d2ee14a3e 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -3,6 +3,13 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\DB; +use nntmux\ColorCLI; +use nntmux\NNTP; +use nntmux\NZB; +use nntmux\ReleaseImage; +use nntmux\Releases; class Group extends Model { @@ -21,8 +28,523 @@ class Group extends Model */ protected $guarded = []; + /** + * @var array + */ + protected static $cbpm = ['collections', 'binaries', 'parts', 'missed_parts']; + + /** + * @var array + */ + protected static $cbppTableNames; + + /** + * @var bool + */ + protected static $allasmgr = (int) Settings::settingValue('..allasmgr') === 1; + + public function release() { return $this->hasMany(Release::class, 'groups_id'); } + + /** + * Returns an associative array of groups for list selection. + * + * + * @return array + */ + public static function getGroupsForSelect(): array + { + $groups = self::getActive(); + $temp_array = []; + + $temp_array[-1] = '--Please Select--'; + + $grouped = $groups->mapToGroups(function ($group, $key) { + return [$group['name']]; + }); + + $temp_array += array_collapse($grouped->toArray()); + + return $temp_array; + } + + /** + * Get all properties of a single group by its ID. + * + * + * @param $id + * @return \Illuminate\Database\Eloquent\Model|null|static + */ + public static function getGroupByID($id) + { + return self::query()->where('id', $id)->first(); + } + + /** + * @return \Illuminate\Database\Eloquent\Collection|static[] + */ + public static function getActive() + { + return self::query()->where('active', '=', 1)->orderBy('name')->get(); + } + + /** + * Get active backfill groups ordered by name ascending. + * + * + * @param $order + * @return array|\Illuminate\Database\Eloquent\Collection|static[] + */ + public static function getActiveBackfill($order) + { + switch ($order) { + case '': + case 'normal': + return self::query()->where('backfill', '=', 1)->where('last_record', '!=', 0)->orderBy('name')->get(); + break; + case 'date': + return self::query()->where('backfill', '=', 1)->where('last_record', '!=', 0)->orderBy('first_record_postdate', 'DESC')->get(); + break; + default: + return []; + } + } + + /** + * Get all active group IDs. + * + * + * @return \Illuminate\Database\Eloquent\Collection|static[] + */ + public static function getActiveIDs() + { + return self::query()->where('active', '=', 1)->orderBy('name')->get(['id']); + } + + /** + * Get all group columns by Name. + * + * + * @param $grp + * @return \Illuminate\Database\Eloquent\Model|null|static + */ + public static function getByName($grp) + { + return self::query()->where('name', $grp)->first(); + } + + /** + * Get a group name using its ID. + * + * @param int|string $id The group ID. + * + * @return string Empty string on failure, groupName on success. + */ + public static function getNameByID($id): string + { + $res = self::query()->where('id', $id)->first(['name']); + + return $res !== null ? $res->name : ''; + } + + /** + * Get a group ID using its name. + * + * @param string $name The group name. + * + * @return string|int Empty string on failure, groups_id on success. + */ + public static function getIDByName($name) + { + $res = self::query()->where('name', $name)->first(['id']); + + return $res === null ? '' : $res->id; + } + + /** + * Gets a count of all groups in the table limited by parameters. + * + * @param string $groupname Constrain query to specific group name + * @param int $active Constrain query to active status + * + * @return mixed + */ + public static function getGroupsCount($groupname = '', $active = -1) + { + $res = self::query(); + + if ($groupname !== '') { + $res->where('name', 'LIKE', '%'.$groupname.'%'); + } + + if ($active > -1) { + $res->where('active', $active); + } + + return $res === null ? 0 : $res->count(['id']); + } + + /** + * Gets all groups and associated release counts. + * + * @param bool $offset + * @param bool $limit + * @param string $groupname The groupname we want if any + * @param bool|int $active The status of the group we want if any + * @return mixed + */ + public static function getGroupsRange($offset = false, $limit = false, $groupname = '', $active = false) + { + $groups = self::query()->groupBy('id')->orderBy('name'); + + if ($groupname !== '') { + $groups->where('name', 'LIKE', '%'.$groupname.'%'); + } + + if ($active === true) { + $groups->where('active', '=', 1); + } + + if ($offset !== false) { + $groups->limit($limit)->offset($offset); + } + + return $groups->get(); + } + + /** + * Update an existing group. + * + * @param array $group + * + * @return bool + */ + public static function updateGroup($group): bool + { + return self::query()->where('id', $group['id'])->update( + [ + 'name' => trim($group['name']), + 'description' => trim($group['description']), + 'backfill_target' => $group['backfill_target'], + 'first_record' => $group['first_record'], + 'last_record' => $group['last_record'], + 'last_updated' => Carbon::now(), + 'active' => $group['active'], + 'backfill' => $group['backfill'], + 'minsizetoformrelease' => $group['minsizetoformrelease'] === '' ? null : $group['minsizetoformrelease'], + 'minfilestoformrelease' => $group['minfilestoformrelease'] === '' ? null : $group['minfilestoformrelease'], + ] + ); + } + + /** + * Checks group name is standard and replaces any shorthand prefixes. + * + * @param string $groupName The full name of the usenet group being evaluated + * + * @return string|bool The name of the group replacing shorthand prefix or false if groupname was malformed + */ + public static function isValidGroup($groupName) + { + if (preg_match('/^([\w-]+\.)+[\w-]+$/i', $groupName)) { + return preg_replace('/^a\.b\./i', 'alt.binaries.', $groupName, 1); + } + + return false; + } + + /** + * Add a new group. + * + * @param array $group + * + * @return bool + */ + public static function addGroup($group): bool + { + return self::query()->insertGetId( + [ + 'name' => trim($group['name']), + 'description' => isset($group['description']) ? trim($group['description']) : '', + 'backfill_target' => $group['backfill_target'] ?? 1, + 'first_record' => $group['first_record'] ?? 0, + 'last_record' => $group['last_record'] ?? 0, + 'active' => $group['active'] ?? 0, + 'backfill' => $group['backfill'] ?? 0, + 'minsizetoformrelease' => $group['minsizetoformrelease'] === '' ? null : $group['minsizetoformrelease'], + 'minfilestoformrelease' => $group['minfilestoformrelease'] === '' ? null : $group['minfilestoformrelease'], + ] + ); + } + + /** + * Delete a group. + * + * @param int|string $id ID of the group. + * + * @return bool + * @throws \Exception + */ + public static function deleteGroup($id): bool + { + self::purge($id); + + return self::query()->where('id', $id)->delete(); + } + + /** + * Reset a group. + * + * @param string|int $id The group ID. + * + * @return bool + * @throws \Exception + */ + public static function reset($id): bool + { + // Remove rows from part repair. + MissedPart::query()->where('groups_id', $id)->delete(); + + foreach (self::$cbpm as $tablePrefix) { + DB::unprepared( + "DROP TABLE IF EXISTS {$tablePrefix}_{$id}" + ); + } + + // Reset the group stats. + return self::query()->where('id', $id)->update( + [ + 'backfill_target' => 1, + 'first_record' => 0, + 'first_record_postdate' => null, + 'last_record' => 0, + 'člast_record_postdate' => null, + 'last_updated' => null, + 'active' => 0, + ] + ); + } + + /** + * Reset all groups. + * + * @return bool + */ + public static function resetall(): bool + { + foreach (self::$cbpm as $tablePrefix) { + DB::unprepared("TRUNCATE TABLE {$tablePrefix}"); + } + + $groups = self::query()->select(['id'])->get(); + + if ($groups instanceof \Traversable) { + foreach ($groups as $group) { + foreach (self::$cbpm as $tablePrefix) { + DB::unprepared("DROP TABLE IF EXISTS {$tablePrefix}_{$group['id']}"); + } + } + } + + // Reset the group stats. + + return self::query()->update( + [ + 'backfill_target' => 1, + 'first_record' => 0, + 'first_record_postdate' => null, + 'last_record' => 0, + 'člast_record_postdate' => null, + 'last_updated' => null, + 'active' => 0, + ] + ); + } + + /** + * Purge a single group or all groups. + * + * @param int|string|bool $id The group ID. If false, purge all groups. + * @throws \Exception + */ + public static function purge($id = false) + { + if ($id === false) { + self::resetall(); + } else { + self::reset($id); + } + + $res = Release::query()->select(['id', 'guid']); + + if ($id !== false) { + $res->where('groups_id', $id); + } + + $res->get(); + + if ($res instanceof \Traversable) { + $releases = new Releases(['Groups' => self::class]); + $nzb = new NZB(); + $releaseImage = new ReleaseImage(); + foreach ($res as $row) { + $releases->deleteSingle( + [ + 'g' => $row['guid'], + 'i' => $row['id'], + ], + $nzb, + $releaseImage + ); + } + } + } + + /** + * Adds new newsgroups based on a regular expression match against USP available. + * + * @param string $groupList + * @param int $active + * @param int $backfill + * + * @return array|string + * @throws \Exception + */ + public static function addBulk($groupList, $active = 1, $backfill = 1) + { + if (preg_match('/^\s*$/m', $groupList)) { + $ret = 'No group list provided.'; + } else { + $nntp = new NNTP(['Echo' => false]); + if ($nntp->doConnect() !== true) { + return 'Problem connecting to usenet.'; + } + $groups = $nntp->getGroups(); + $nntp->doQuit(); + + if ($nntp->isError($groups)) { + return 'Problem fetching groups from usenet.'; + } + + $regFilter = '/'.$groupList.'/i'; + + $ret = []; + + foreach ($groups as $group) { + if (preg_match($regFilter, $group['group']) > 0) { + $res = self::getIDByName($group['group']); + if ($res === '') { + self::addGroup( + [ + 'name' => $group['group'], + 'active' => $active, + 'backfill' => $backfill, + 'description' => 'Added by bulkAdd', + ] + ); + $ret[] = ['group' => $group['group'], 'msg' => 'Created']; + } + } + } + + if (\count($ret) === 0) { + $ret = 'No groups found with your regex, try again!'; + } + } + + return $ret; + } + + /** + * Updates the group active/backfill status. + * + * @param int $id Which group ID + * @param string $column Which column active/backfill + * @param int $status Which status we are setting + * + * @return string + */ + public static function updateGroupStatus($id, $column, $status = 0): string + { + self::query()->where('id', $id)->update( + [ + $column => $status, + ] + ); + + return "Group {$id} has been ".(($status === 0) ? 'deactivated' : 'activated').'.'; + } + + /** + * Get the names of the collections/binaries/parts/part repair tables. + * If TPG is on, try to create new tables for the groups_id, if we fail, log the error and exit. + * + * @param int $groupID ID of the group. + * + * @return array The table names. + * @throws \Exception + */ + public static function getCBPTableNames($groupID): array + { + $groupKey = $groupID; + + // Check if buffered and return. Prevents re-querying MySQL when TPG is on. + if (isset(self::$cbppTableNames[$groupKey])) { + return self::$cbppTableNames[$groupKey]; + } + + if (NN_ECHOCLI && self::$allasmgr === false && self::createNewTPGTables($groupID) === false) { + exit('There is a problem creating new TPG tables for this group ID: '.$groupID.PHP_EOL); + } + + $tables = []; + $tables['cname'] = 'collections_'.$groupID; + $tables['bname'] = 'binaries_'.$groupID; + $tables['pname'] = 'parts_'.$groupID; + $tables['prname'] = 'missed_parts_'.$groupID; + + // Buffer. + self::$cbppTableNames[$groupKey] = $tables; + + return $tables; + } + + /** + * Check if the tables exist for the groups_id, make new tables for table per group. + * + * @param int $groupID + * + * @return bool + */ + public static function createNewTPGTables($groupID): bool + { + foreach (self::$cbpm as $tablePrefix) { + if (DB::unprepared( + "CREATE TABLE IF NOT EXISTS {$tablePrefix}_{$groupID} LIKE {$tablePrefix}" + ) === null + ) { + return false; + } + } + + return true; + } + + /** + * Disable group that does not exist on USP server. + * + * @param int $id The Group ID to disable + */ + public static function disableIfNotExist($id): void + { + self::updateGroupStatus($id, 'active'); + ColorCLI::doEcho( + ColorCLI::error( + 'Group does not exist on server, disabling' + ) + ); + } } diff --git a/misc/update/multiprocessing/.do_not_run/switch.php b/misc/update/multiprocessing/.do_not_run/switch.php index 130df7860..18333a95e 100644 --- a/misc/update/multiprocessing/.do_not_run/switch.php +++ b/misc/update/multiprocessing/.do_not_run/switch.php @@ -6,6 +6,7 @@ if (!isset($argv[1])) { require_once dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . 'bootstrap/autoload.php'; +use App\Models\Group; use App\Models\Settings; use App\Models\Tmux; use \nntmux\db\DB; @@ -14,7 +15,6 @@ use \nntmux\processing\ProcessReleases; use \nntmux\processing\post\ProcessAdditional; use nntmux\Backfill; use nntmux\Binaries; -use nntmux\Groups; use nntmux\Nfo; use nntmux\NNTP; use nntmux\processing\ProcessReleasesMultiGroup; @@ -69,14 +69,13 @@ switch ($options[1]) { case 'get_range': $pdo = new DB(); $nntp = nntp($pdo); - $groups = new Groups(); - $groupMySQL = $groups->getByName($options[3]); + $groupMySQL = Group::getByName($options[3]); if ($nntp->isError($nntp->selectGroup($groupMySQL['name']))) { if ($nntp->isError($nntp->dataError($nntp, $groupMySQL['name']))) { return; } } - $binaries = new Binaries(['NNTP' => $nntp, 'Settings' => $pdo, 'Groups' => $groups]); + $binaries = new Binaries(['NNTP' => $nntp, 'Settings' => $pdo, 'Groups' => null]); $return = $binaries->scan($groupMySQL, $options[4], $options[5], (Settings::settingValue('..safepartrepair') == 1 ? 'update' : 'backfill')); if (empty($return)) { exit(); @@ -133,8 +132,7 @@ switch ($options[1]) { */ case 'part_repair': $pdo = new DB(); - $groups = new Groups(['Settings' => $pdo]); - $groupMySQL = $groups->getByName($options[2]); + $groupMySQL = Group::getByName($options[2]); $nntp = nntp($pdo); // Select group, here, only once $data = $nntp->selectGroup($groupMySQL['name']); @@ -143,7 +141,7 @@ switch ($options[1]) { exit(); } } - (new Binaries(['NNTP' => $nntp, 'Groups' => $groups, 'Settings' => $pdo]))->partRepair($groupMySQL); + (new Binaries(['NNTP' => $nntp, 'Settings' => $pdo]))->partRepair($groupMySQL); break; // Process releases. @@ -186,9 +184,8 @@ switch ($options[1]) { case 'update_group_headers': $pdo = new DB(); $nntp = nntp($pdo); - $groups = new Groups(); - $groupMySQL = $groups->getByName($options[2]); - (new Binaries(['NNTP' => $nntp, 'Groups' => $groups, 'Settings' => $pdo]))->updateGroup($groupMySQL); + $groupMySQL = Group::getByName($options[2]); + (new Binaries(['NNTP' => $nntp, 'Settings' => $pdo]))->updateGroup($groupMySQL); break; diff --git a/misc/update/update_binaries.php b/misc/update/update_binaries.php index 4d79bda48..3fbe6bdc6 100644 --- a/misc/update/update_binaries.php +++ b/misc/update/update_binaries.php @@ -2,9 +2,9 @@ require_once dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'bootstrap/autoload.php'; +use App\Models\Group; use nntmux\NNTP; use nntmux\db\DB; -use nntmux\Groups; use nntmux\Binaries; use nntmux\ColorCLI; use App\Models\Settings; @@ -24,8 +24,7 @@ if (isset($argv[1]) && ! is_numeric($argv[1])) { $groupName = $argv[1]; echo ColorCLI::header("Updating group: $groupName"); - $grp = new Groups(['Settings' => $pdo]); - $group = $grp->getByName($groupName); + $group = Group::getByName($groupName); if (is_array($group)) { $binaries->updateGroup( $group, diff --git a/nntmux/Backfill.php b/nntmux/Backfill.php index 8bb7d9524..30fdbf9e0 100755 --- a/nntmux/Backfill.php +++ b/nntmux/Backfill.php @@ -2,44 +2,24 @@ namespace nntmux; +use App\Models\Group; use nntmux\db\DB; use App\Models\Settings; class Backfill { /** - * Instance of class Settings. - * - * @var DB + * @var \nntmux\db\DB */ public $pdo; /** - * @var Binaries + * @var */ protected $_binaries; /** - * Instance of class ColorCLI. - * - * @var ColorCLI - */ - protected $_colorCLI; - - /** - * Instance of class debugging. - * - * @var Logger - */ - protected $_debugging; - - /** - * @var Groups - */ - protected $_groups; - - /** - * @var NNTP + * @var \nntmux\NNTP */ protected $_nntp; /** @@ -100,7 +80,6 @@ class Backfill $this->_echoCLI = ($options['Echo'] && NN_ECHOCLI); $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); - $this->_groups = ($options['Groups'] instanceof Groups ? $options['Groups'] : new Groups(['Settings' => $this->pdo])); $this->_nntp = ( $options['NNTP'] instanceof NNTP ? $options['NNTP'] : new NNTP(['Settings' => $this->pdo]) @@ -126,12 +105,12 @@ class Backfill { $res = []; if ($groupName !== '') { - $grp = $this->_groups->getByName($groupName); + $grp = Group::getByName($groupName); if ($grp) { $res = [$grp]; } } else { - $res = $this->_groups->getActiveBackfill($type); + $res = Group::getActiveBackfill($type); } $groupCount = count($res); @@ -256,7 +235,7 @@ class Backfill ', skipping it, consider disabling backfill on it.'); if ($this->_disableBackfillGroup) { - $this->_groups->updateGroupStatus($groupArr['id'], 'backfill', 0); + Group::updateGroupStatus($groupArr['id'], 'backfill', 0); } if ($this->_echoCLI) { diff --git a/nntmux/Binaries.php b/nntmux/Binaries.php index da6c0d389..cd6e4ee68 100755 --- a/nntmux/Binaries.php +++ b/nntmux/Binaries.php @@ -51,16 +51,6 @@ class Binaries */ protected $_collectionsCleaning; - /** - * @var \nntmux\Logger - */ - protected $_debugging; - - /** - * @var \nntmux\Groups - */ - protected $_groups; - /** * @var \nntmux\NNTP */ @@ -262,7 +252,6 @@ class Binaries $this->_echoCLI = ($options['Echo'] && NN_ECHOCLI); $this->_pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); - $this->_groups = ($options['Groups'] instanceof Groups ? $options['Groups'] : new Groups(['Settings' => $this->_pdo])); $this->_colorCLI = ($options['ColorCLI'] instanceof ColorCLI ? $options['ColorCLI'] : new ColorCLI()); $this->_nntp = ($options['NNTP'] instanceof NNTP ? $options['NNTP'] : new NNTP(['Echo' => $this->_colorCLI, 'Settings' => $this->_pdo, 'ColorCLI' => $this->_colorCLI])); $this->_collectionsCleaning = ($options['CollectionsCleaning'] instanceof CollectionsCleaning ? $options['CollectionsCleaning'] : new CollectionsCleaning(['Settings' => $this->_pdo])); @@ -292,7 +281,7 @@ class Binaries */ public function updateAllGroups($maxHeaders = 100000): void { - $groups = $this->_groups->getActive(); + $groups = Group::getActive(); $groupCount = \count($groups); if ($groupCount > 0) { @@ -359,7 +348,7 @@ class Binaries $groupNNTP = $this->_nntp->dataError($this->_nntp, $groupMySQL['name']); if (isset($groupNNTP['code']) && (int) $groupNNTP['code'] === 411) { - $this->_groups->disableIfNotExist($groupMySQL['id']); + Group::disableIfNotExist($groupMySQL['id']); } if ($this->_nntp->isError($groupNNTP)) { return; @@ -604,7 +593,7 @@ class Binaries $this->notYEnc = $this->headersBlackListed = 0; // Check if MySQL tables exist, create if they do not, get their names at the same time. - $this->tableNames = $this->_groups->getCBPTableNames($this->groupMySQL['id']); + $this->tableNames = Group::getCBPTableNames($this->groupMySQL['id']); $mgrPosters = $this->getMultiGroupPosters(); @@ -772,7 +761,7 @@ class Binaries // Standard headers go second so we can switch tableNames back and do part repair to standard group tables if (! empty($stdHeaders)) { - $this->tableNames = $this->_groups->getCBPTableNames($this->groupMySQL['id']); + $this->tableNames = Group::getCBPTableNames($this->groupMySQL['id']); $this->storeHeaders($stdHeaders, false); } unset($stdHeaders); @@ -1137,7 +1126,7 @@ class Binaries $tableNames = $tables; if ($tableNames === '') { - $tableNames = $this->_groups->getCBPTableNames($groupArr['id']); + $tableNames = Group::getCBPTableNames($groupArr['id']); } // Get all parts in partrepair table. $missingParts = $this->_pdo->query( @@ -1275,10 +1264,10 @@ class Binaries public function postdate($post, array $groupData): int { // Set table names - $groupID = $this->_groups->getIDByName($groupData['group']); + $groupID = Group::getIDByName($groupData['group']); $group = []; if ($groupID !== '') { - $group = $this->_groups->getCBPTableNames($groupID); + $group = Group::getCBPTableNames($groupID); } $currentPost = $post; diff --git a/nntmux/Groups.php b/nntmux/Groups.php deleted file mode 100755 index 6ac63e4ac..000000000 --- a/nntmux/Groups.php +++ /dev/null @@ -1,560 +0,0 @@ - null, - 'ColorCLI' => null, - ]; - $options += $defaults; - - $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); - $this->colorCLI = ($options['ColorCLI'] instanceof ColorCLI ? $options['ColorCLI'] : new ColorCLI()); - $this->cbpm = ['collections', 'binaries', 'parts', 'missed_parts']; - $this->allasmgr = (int) Settings::settingValue('..allasmgr') === 1; - } - - /** - * Returns an associative array of groups for list selection. - * - * - * @return array - */ - public function getGroupsForSelect(): array - { - $groups = $this->getActive(); - $temp_array = []; - - $temp_array[-1] = '--Please Select--'; - - $grouped = $groups->mapToGroups(function ($group, $key) { - return [$group['name']]; - }); - - $temp_array += array_collapse($grouped->toArray()); - - return $temp_array; - } - - /** - * Get all properties of a single group by its ID. - * - * - * @param $id - * @return \Illuminate\Database\Eloquent\Model|null|static - */ - public function getByID($id) - { - return Group::query()->where('id', $id)->first(); - } - - /** - * @return \Illuminate\Database\Eloquent\Collection|static[] - */ - public function getActive() - { - return Group::query()->where('active', '=', 1)->orderBy('name')->get(); - } - - /** - * Get active backfill groups ordered by name ascending. - * - * - * @param $order - * @return array|\Illuminate\Database\Eloquent\Collection|static[] - */ - public function getActiveBackfill($order) - { - switch ($order) { - case '': - case 'normal': - return Group::query()->where('backfill', '=', 1)->where('last_record', '!=', 0)->orderBy('name')->get(); - break; - case 'date': - return Group::query()->where('backfill', '=', 1)->where('last_record', '!=', 0)->orderBy('first_record_postdate', 'DESC')->get(); - break; - default: - return []; - } - } - - /** - * Get all active group IDs. - * - * - * @return \Illuminate\Database\Eloquent\Collection|static[] - */ - public function getActiveIDs() - { - return Group::query()->where('active', '=', 1)->orderBy('name')->get(['id']); - } - - /** - * Get all group columns by Name. - * - * - * @param $grp - * @return \Illuminate\Database\Eloquent\Model|null|static - */ - public function getByName($grp) - { - return Group::query()->where('name', $grp)->first(); - } - - /** - * Get a group name using its ID. - * - * @param int|string $id The group ID. - * - * @return string Empty string on failure, groupName on success. - */ - public function getNameByID($id): string - { - $res = Group::query()->where('id', $id)->first(['name']); - - return $res !== null ? $res->name : ''; - } - - /** - * Get a group ID using its name. - * - * @param string $name The group name. - * - * @return string|int Empty string on failure, groups_id on success. - */ - public function getIDByName($name) - { - $res = Group::query()->where('name', $name)->first(['id']); - - return $res === null ? '' : $res->id; - } - - /** - * Gets a count of all groups in the table limited by parameters. - * - * @param string $groupname Constrain query to specific group name - * @param int $active Constrain query to active status - * - * @return mixed - */ - public function getCount($groupname = '', $active = -1) - { - $res = Group::query(); - - if ($groupname !== '') { - $res->where('name', 'LIKE', '%'.$groupname.'%'); - } - - if ($active > -1) { - $res->where('active', $active); - } - - return $res === null ? 0 : $res->count(['id']); - } - - /** - * Gets all groups and associated release counts. - * - * @param bool $offset - * @param bool $limit - * @param string $groupname The groupname we want if any - * @param bool|int $active The status of the group we want if any - * @return mixed - */ - public function getRange($offset = false, $limit = false, $groupname = '', $active = false) - { - $groups = Group::query()->groupBy('id')->orderBy('name'); - - if ($groupname !== '') { - $groups->where('name', 'LIKE', '%'.$groupname.'%'); - } - - if ($active === true) { - $groups->where('active', '=', 1); - } - - if ($offset !== false) { - $groups->limit($limit)->offset($offset); - } - - return $groups->get(); - } - - /** - * Update an existing group. - * - * @param array $group - * - * @return bool - */ - public function update($group): bool - { - return Group::query()->where('id', $group['id'])->update( - [ - 'name' => trim($group['name']), - 'description' => trim($group['description']), - 'backfill_target' => $group['backfill_target'], - 'first_record' => $group['first_record'], - 'last_record' => $group['last_record'], - 'last_updated' => Carbon::now(), - 'active' => $group['active'], - 'backfill' => $group['backfill'], - 'minsizetoformrelease' => $group['minsizetoformrelease'] === '' ? null : $group['minsizetoformrelease'], - 'minfilestoformrelease' => $group['minfilestoformrelease'] === '' ? null : $group['minfilestoformrelease'], - ] - ); - } - - /** - * Checks group name is standard and replaces any shorthand prefixes. - * - * @param string $groupName The full name of the usenet group being evaluated - * - * @return string|bool The name of the group replacing shorthand prefix or false if groupname was malformed - */ - public function isValidGroup($groupName) - { - if (preg_match('/^([\w-]+\.)+[\w-]+$/i', $groupName)) { - return preg_replace('/^a\.b\./i', 'alt.binaries.', $groupName, 1); - } - - return false; - } - - /** - * Add a new group. - * - * @param array $group - * - * @return bool - */ - public function add($group): bool - { - return Group::query()->insertGetId( - [ - 'name' => trim($group['name']), - 'description' => isset($group['description']) ? trim($group['description']) : '', - 'backfill_target' => $group['backfill_target'] ?? 1, - 'first_record' => $group['first_record'] ?? 0, - 'last_record' => $group['last_record'] ?? 0, - 'active' => $group['active'] ?? 0, - 'backfill' => $group['backfill'] ?? 0, - 'minsizetoformrelease' => $group['minsizetoformrelease'] === '' ? null : $group['minsizetoformrelease'], - 'minfilestoformrelease' => $group['minfilestoformrelease'] === '' ? null : $group['minfilestoformrelease'], - ] - ); - } - - /** - * Delete a group. - * - * @param int|string $id ID of the group. - * - * @return bool - * @throws \Exception - */ - public function delete($id): bool - { - $this->purge($id); - - return Group::query()->where('id', $id)->delete(); - } - - /** - * Reset a group. - * - * @param string|int $id The group ID. - * - * @return bool - * @throws \Exception - */ - public function reset($id): bool - { - // Remove rows from part repair. - MissedPart::query()->where('groups_id', $id)->delete(); - - foreach ($this->cbpm as $tablePrefix) { - DBFacade::unprepared( - "DROP TABLE IF EXISTS {$tablePrefix}_{$id}" - ); - } - - // Reset the group stats. - return Group::query()->where('id', $id)->update( - [ - 'backfill_target' => 1, - 'first_record' => 0, - 'first_record_postdate' => null, - 'last_record' => 0, - 'člast_record_postdate' => null, - 'last_updated' => null, - 'active' => 0, - ] - ); - } - - /** - * Reset all groups. - * - * @return bool - */ - public function resetall(): bool - { - foreach ($this->cbpm as $tablePrefix) { - DBFacade::unprepared("TRUNCATE TABLE {$tablePrefix}"); - } - - $groups = Group::query()->select(['id'])->get(); - - if ($groups instanceof \Traversable) { - foreach ($groups as $group) { - foreach ($this->cbpm as $tablePrefix) { - DBFacade::unprepared("DROP TABLE IF EXISTS {$tablePrefix}_{$group['id']}"); - } - } - } - - // Reset the group stats. - - return Group::query()->update( - [ - 'backfill_target' => 1, - 'first_record' => 0, - 'first_record_postdate' => null, - 'last_record' => 0, - 'člast_record_postdate' => null, - 'last_updated' => null, - 'active' => 0, - ] - ); - } - - /** - * Purge a single group or all groups. - * - * @param int|string|bool $id The group ID. If false, purge all groups. - * @throws \Exception - */ - public function purge($id = false) - { - if ($id === false) { - $this->resetall(); - } else { - $this->reset($id); - } - - $res = Release::query()->select(['id', 'guid']); - - if ($id !== false) { - $res->where('groups_id', $id); - } - - $res->get(); - - if ($res instanceof \Traversable) { - $releases = new Releases(['Groups' => $this]); - $nzb = new NZB(); - $releaseImage = new ReleaseImage(); - foreach ($res as $row) { - $releases->deleteSingle( - [ - 'g' => $row['guid'], - 'i' => $row['id'], - ], - $nzb, - $releaseImage - ); - } - } - } - - /** - * Adds new newsgroups based on a regular expression match against USP available. - * - * @param string $groupList - * @param int $active - * @param int $backfill - * - * @return array|string - * @throws \Exception - */ - public function addBulk($groupList, $active = 1, $backfill = 1) - { - if (preg_match('/^\s*$/m', $groupList)) { - $ret = 'No group list provided.'; - } else { - $nntp = new NNTP(['Echo' => false]); - if ($nntp->doConnect() !== true) { - return 'Problem connecting to usenet.'; - } - $groups = $nntp->getGroups(); - $nntp->doQuit(); - - if ($nntp->isError($groups)) { - return 'Problem fetching groups from usenet.'; - } - - $regFilter = '/'.$groupList.'/i'; - - $ret = []; - - foreach ($groups as $group) { - if (preg_match($regFilter, $group['group']) > 0) { - $res = $this->getIDByName($group['group']); - if ($res === '') { - $this->add( - [ - 'name' => $group['group'], - 'active' => $active, - 'backfill' => $backfill, - 'description' => 'Added by bulkAdd', - ] - ); - $ret[] = ['group' => $group['group'], 'msg' => 'Created']; - } - } - } - - if (\count($ret) === 0) { - $ret = 'No groups found with your regex, try again!'; - } - } - - return $ret; - } - - /** - * Updates the group active/backfill status. - * - * @param int $id Which group ID - * @param string $column Which column active/backfill - * @param int $status Which status we are setting - * - * @return string - */ - public function updateGroupStatus($id, $column, $status = 0): string - { - Group::query()->where('id', $id)->update( - [ - $column => $status, - ] - ); - - return "Group {$id} has been ".(($status === 0) ? 'deactivated' : 'activated').'.'; - } - - /** - * Get the names of the collections/binaries/parts/part repair tables. - * If TPG is on, try to create new tables for the groups_id, if we fail, log the error and exit. - * - * @param int $groupID ID of the group. - * - * @return array The table names. - * @throws \Exception - */ - public function getCBPTableNames($groupID): array - { - $groupKey = $groupID; - - // Check if buffered and return. Prevents re-querying MySQL when TPG is on. - if (isset($this->cbppTableNames[$groupKey])) { - return $this->cbppTableNames[$groupKey]; - } - - if (NN_ECHOCLI && $this->allasmgr === false && $this->createNewTPGTables($groupID) === false) { - exit('There is a problem creating new TPG tables for this group ID: '.$groupID.PHP_EOL); - } - - $tables = []; - $tables['cname'] = 'collections_'.$groupID; - $tables['bname'] = 'binaries_'.$groupID; - $tables['pname'] = 'parts_'.$groupID; - $tables['prname'] = 'missed_parts_'.$groupID; - - // Buffer. - $this->cbppTableNames[$groupKey] = $tables; - - return $tables; - } - - /** - * Check if the tables exist for the groups_id, make new tables for table per group. - * - * @param int $groupID - * - * @return bool - */ - public function createNewTPGTables($groupID): bool - { - foreach ($this->cbpm as $tablePrefix) { - if (DBFacade::unprepared( - "CREATE TABLE IF NOT EXISTS {$tablePrefix}_{$groupID} LIKE {$tablePrefix}" - ) === null - ) { - return false; - } - } - - return true; - } - - /** - * Disable group that does not exist on USP server. - * - * @param int $id The Group ID to disable - */ - public function disableIfNotExist($id): void - { - $this->updateGroupStatus($id, 'active'); - ColorCLI::doEcho( - ColorCLI::error( - 'Group does not exist on server, disabling' - ) - ); - } -} diff --git a/nntmux/NZBImport.php b/nntmux/NZBImport.php index 69ff45a0e..8939a0442 100755 --- a/nntmux/NZBImport.php +++ b/nntmux/NZBImport.php @@ -83,13 +83,6 @@ class NZBImport */ protected $nzbGuid; - /** - * Access point to add new groups. - * - * @var Groups - */ - private $groups; - /** * @var \nntmux\Releases */ @@ -122,7 +115,6 @@ class NZBImport $this->nzb = ($options['NZB'] instanceof NZB ? $options['NZB'] : new NZB()); $this->releaseCleaner = ($options['ReleaseCleaning'] instanceof ReleaseCleaning ? $options['ReleaseCleaning'] : new ReleaseCleaning($this->pdo)); $this->releases = ($options['Releases'] instanceof Releases ? $options['Releases'] : new Releases(['settings' => $this->pdo])); - $this->groups = new Groups(['Settings' => $this->pdo]); $this->crossPostt = Settings::settingValue('..crossposttime') !== '' ? Settings::settingValue('..crossposttime') : 2; $this->browser = $options['Browser']; @@ -299,9 +291,9 @@ class NZBImport $groupName = $group; } } else { - $group = $this->groups->isValidGroup($group); + $group = Group::isValidGroup($group); if ($group !== false) { - $groupID = $this->groups->add([ + $groupID = Group::addGroup([ 'name' => $group, 'description' => 'Added by NZBimport script.', 'backfill_target' => 1, diff --git a/nntmux/NameFixer.php b/nntmux/NameFixer.php index 4cb1c4d56..f8accc3f1 100755 --- a/nntmux/NameFixer.php +++ b/nntmux/NameFixer.php @@ -2,6 +2,7 @@ namespace nntmux; +use App\Models\Group; use nntmux\db\DB; use App\Models\Predb; use App\Models\Release; @@ -176,7 +177,6 @@ class NameFixer $this->consoletools = ($options['ConsoleTools'] instanceof ConsoleTools ? $options['ConsoleTools'] : new ConsoleTools()); $this->category = ($options['Categorize'] instanceof Categorize ? $options['Categorize'] : new Categorize(['Settings' => $this->pdo])); $this->text = ($options['Misc'] instanceof Utility ? $options['Misc'] : new Utility()); - $this->_groups = ($options['Groups'] instanceof Groups ? $options['Groups'] : new Groups(['Settings' => $this->pdo])); $this->sphinx = ($options['SphinxSearch'] instanceof SphinxSearch ? $options['SphinxSearch'] : new SphinxSearch()); } @@ -893,9 +893,9 @@ class NameFixer $newName = preg_replace(['/^[-=_\.:\s]+/', '/[-=_\.:\s]+$/'], '', $newName[0]); if ($this->echooutput === true && $show === 1) { - $groupName = $this->_groups->getNameByID($release['groups_id']); - $oldCatName = $this->category->getNameByID($release['categories_id']); - $newCatName = $this->category->getNameByID($determinedCategory); + $groupName = Group::getNameByID($release['groups_id']); + $oldCatName = Category::getNameByID($release['categories_id']); + $newCatName = Category::getNameByID($determinedCategory); if ($type === 'PAR2, ') { echo PHP_EOL; diff --git a/nntmux/Nfo.php b/nntmux/Nfo.php index ca6097d30..b7f16bbad 100755 --- a/nntmux/Nfo.php +++ b/nntmux/Nfo.php @@ -2,6 +2,7 @@ namespace nntmux; +use App\Models\Group; use App\Models\Release; use App\Models\Settings; use App\Models\ReleaseNfo; @@ -318,7 +319,6 @@ class Nfo } } - $groups = new Groups(); $nzbContents = new NZBContents( [ 'Echo' => $this->echo, @@ -331,7 +331,7 @@ class Nfo $movie = new Movie(['Echo' => $this->echo]); foreach ($res as $arr) { - $fetchedBinary = $nzbContents->getNfoFromNZB($arr['guid'], $arr['id'], $arr['groups_id'], $groups->getNameByID($arr['groups_id'])); + $fetchedBinary = $nzbContents->getNfoFromNZB($arr['guid'], $arr['id'], $arr['groups_id'], Group::getNameByID($arr['groups_id'])); if ($fetchedBinary !== false) { // Insert nfo into database. diff --git a/nntmux/Regexes.php b/nntmux/Regexes.php index 7f443f77b..7af962ccf 100755 --- a/nntmux/Regexes.php +++ b/nntmux/Regexes.php @@ -2,6 +2,7 @@ namespace nntmux; +use App\Models\Group; use nntmux\db\DB; use App\Models\Release; use App\Models\Category; @@ -176,14 +177,13 @@ class Regexes */ public function testCollectionRegex($groupName, $regex, $limit): array { - $groups = new Groups(['Settings' => $this->pdo]); - $groupID = $groups->getIDByName($groupName); + $groupID = Group::getIDByName($groupName); if (! $groupID) { return []; } - $tableNames = $groups->getCBPTableNames($groupID); + $tableNames = Group::getCBPTableNames($groupID); $rows = $this->pdo->query( sprintf( @@ -248,8 +248,7 @@ class Regexes */ public function testReleaseNamingRegex($groupName, $regex, $displayLimit, $queryLimit): array { - $groups = new Groups(['Settings' => $this->pdo]); - $groupID = $groups->getIDByName($groupName); + $groupID = Group::getIDByName($groupName); if (! $groupID) { return []; diff --git a/nntmux/Releases.php b/nntmux/Releases.php index 2b4166628..c87e0d737 100755 --- a/nntmux/Releases.php +++ b/nntmux/Releases.php @@ -2,6 +2,7 @@ namespace nntmux; +use App\Models\Group; use nntmux\db\DB; use App\Models\Release; use App\Models\Category; @@ -26,11 +27,6 @@ class Releases */ public $pdo; - /** - * @var \nntmux\Groups - */ - public $groups; - /** * @var \nntmux\ReleaseSearch */ @@ -64,7 +60,6 @@ class Releases $options += $defaults; $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); - $this->groups = ($options['Groups'] instanceof Groups ? $options['Groups'] : new Groups(['Settings' => $this->pdo])); $this->sphinxSearch = new SphinxSearch(); $this->releaseSearch = new ReleaseSearch($this->pdo); $this->showPasswords = self::showPasswords(); @@ -705,7 +700,7 @@ class Releases $this->showPasswords, NZB::NZB_ADDED, ($maxAge > 0 ? sprintf(' AND r.postdate > (NOW() - INTERVAL %d DAY) ', $maxAge) : ''), - ((int) $groupName !== -1 ? sprintf(' AND r.groups_id = %d ', $this->groups->getIDByName($groupName)) : ''), + ((int) $groupName !== -1 ? sprintf(' AND r.groups_id = %d ', Group::getIDByName($groupName)) : ''), (array_key_exists($sizeFrom, $sizeRange) ? ' AND r.size > '.(string) (104857600 * (int) $sizeRange[$sizeFrom]).' ' : ''), (array_key_exists($sizeTo, $sizeRange) ? ' AND r.size < '.(string) (104857600 * (int) $sizeRange[$sizeTo]).' ' : ''), ((int) $hasNfo !== 0 ? ' AND r.nfostatus = 1 ' : ''), diff --git a/nntmux/http/API.php b/nntmux/http/API.php index e022034ed..d731062a2 100755 --- a/nntmux/http/API.php +++ b/nntmux/http/API.php @@ -21,7 +21,7 @@ namespace nntmux\http; -use nntmux\Groups; +use App\Models\Group; use App\Models\Category; use App\Models\AudioData; use nntmux\utility\Utility; @@ -121,7 +121,7 @@ class API extends Capabilities { $groupName = -1; if (isset($this->getRequest['group'])) { - $group = (new Groups())->isValidGroup($this->getRequest['group']); + $group = Group::isValidGroup($this->getRequest['group']); if ($group !== false) { $groupName = $group; } diff --git a/nntmux/processing/PostProcess.php b/nntmux/processing/PostProcess.php index 37036c947..1d146bf6e 100755 --- a/nntmux/processing/PostProcess.php +++ b/nntmux/processing/PostProcess.php @@ -2,6 +2,7 @@ namespace nntmux\processing; +use App\Models\Group; use nntmux\Nfo; use nntmux\XXX; use nntmux\NNTP; @@ -10,7 +11,6 @@ use nntmux\db\DB; use nntmux\Games; use nntmux\Movie; use nntmux\Music; -use nntmux\Groups; use nntmux\Console; use nntmux\Sharing; use nntmux\NameFixer; @@ -67,11 +67,6 @@ class PostProcess */ private $echooutput; - /** - * @var \nntmux\Groups - */ - private $groups; - /** * @var \nntmux\Nfo */ @@ -102,7 +97,6 @@ class PostProcess // Class instances. $this->pdo = (($options['Settings'] instanceof DB) ? $options['Settings'] : new DB()); - $this->groups = (($options['Groups'] instanceof Groups) ? $options['Groups'] : new Groups(['Settings' => $this->pdo])); $this->_par2Info = new Par2Info(); $this->nameFixer = (($options['NameFixer'] instanceof NameFixer) ? $options['NameFixer'] : new NameFixer(['Echo' => $this->echooutput, 'Settings' => $this->pdo, 'Groups' => $this->groups])); $this->Nfo = (($options['Nfo'] instanceof Nfo) ? $options['Nfo'] : new Nfo()); @@ -324,7 +318,7 @@ class PostProcess } // Get the PAR2 file. - $par2 = $nntp->getMessages($this->groups->getNameByID($groupID), $messageID, $this->alternateNNTP); + $par2 = $nntp->getMessages(Group::getNameByID($groupID), $messageID, $this->alternateNNTP); if ($nntp->isError($par2)) { return false; } diff --git a/nntmux/processing/ProcessReleases.php b/nntmux/processing/ProcessReleases.php index f0262c3fa..25aae3a83 100755 --- a/nntmux/processing/ProcessReleases.php +++ b/nntmux/processing/ProcessReleases.php @@ -2,11 +2,11 @@ namespace nntmux\processing; +use App\Models\Group; use nntmux\NZB; use nntmux\NNTP; use nntmux\db\DB; use nntmux\Genres; -use nntmux\Groups; use nntmux\ColorCLI; use nntmux\Releases; use App\Models\Predb; @@ -35,11 +35,6 @@ class ProcessReleases public const FILE_INCOMPLETE = 0; // We don't have all the parts yet for the file (binaries table partcheck column). public const FILE_COMPLETE = 1; // We have all the parts for the file (binaries table partcheck column). - /** - * @var \nntmux\Groups - */ - public $groups; - /** * @var int */ @@ -139,7 +134,6 @@ class ProcessReleases $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); $this->consoleTools = ($options['ConsoleTools'] instanceof ConsoleTools ? $options['ConsoleTools'] : new ConsoleTools()); - $this->groups = ($options['Groups'] instanceof Groups ? $options['Groups'] : new Groups(['Settings' => $this->pdo])); $this->nzb = ($options['NZB'] instanceof NZB ? $options['NZB'] : new NZB()); $this->releaseCleaning = ($options['ReleaseCleaning'] instanceof ReleaseCleaning ? $options['ReleaseCleaning'] : new ReleaseCleaning($this->pdo)); $this->releases = ($options['Releases'] instanceof Releases ? $options['Releases'] : new Releases(['Settings' => $this->pdo, 'Groups' => $this->groups])); @@ -178,7 +172,7 @@ class ProcessReleases $groupID = ''; if (! empty($groupName) && $groupName !== 'mgr') { - $groupInfo = $this->groups->getByName($groupName); + $groupInfo = Group::getByName($groupName); if ($groupInfo !== null) { $groupID = $groupInfo['id']; } @@ -390,7 +384,7 @@ class ProcessReleases ); } - $groupID === '' ? $groupIDs = $this->groups->getActiveIDs() : $groupIDs = [['id' => $groupID]]; + $groupID === '' ? $groupIDs = Group::getActiveIDs() : $groupIDs = [['id' => $groupID]]; $minSizeDeleted = $maxSizeDeleted = $minFilesDeleted = 0; @@ -401,7 +395,7 @@ class ProcessReleases foreach ($groupIDs as $grpID) { $groupMinSizeSetting = $groupMinFilesSetting = 0; - $groupMinimums = $this->groups->getByID($grpID['id']); + $groupMinimums = Group::getGroupByID($grpID['id']); if ($groupMinimums !== null) { if (! empty($groupMinimums['minsizetoformrelease']) && $groupMinimums['minsizetoformrelease'] > 0) { $groupMinSizeSetting = (int) $groupMinimums['minsizetoformrelease']; @@ -504,7 +498,7 @@ class ProcessReleases */ protected function initiateTableNames($groupID): void { - $this->tables = $this->groups->getCBPTableNames($groupID); + $this->tables = Group::getCBPTableNames($groupID); } /** @@ -641,12 +635,12 @@ class ProcessReleases if (preg_match_all('#(\S+):\S+#', $collection['xref'], $matches)) { foreach ($matches[1] as $grp) { //check if the group name is in a valid format - $grpTmp = $this->groups->isValidGroup($grp); + $grpTmp = Group::isValidGroup($grp); if ($grpTmp !== false) { //check if the group already exists in database - $xrefGrpID = $this->groups->getIDByName($grpTmp); + $xrefGrpID = Group::getIDByName($grpTmp); if ($xrefGrpID === '') { - $xrefGrpID = $this->groups->add( + $xrefGrpID = Group::add( [ 'name' => $grpTmp, 'description' => 'Added by Release processing', @@ -1069,7 +1063,7 @@ class ProcessReleases echo ColorCLI::header('Process Releases -> Delete releases smaller/larger than minimum size/file count from group/site setting.'); } - $groupID === '' ? $groupIDs = $this->groups->getActiveIDs() : $groupIDs = [['id' => $groupID]]; + $groupID === '' ? $groupIDs = Group::getActiveIDs() : $groupIDs = [['id' => $groupID]]; $maxSizeSetting = Settings::settingValue('.release.maxsizetoformrelease'); $minSizeSetting = Settings::settingValue('.release.minsizetoformrelease'); diff --git a/nntmux/processing/post/ProcessAdditional.php b/nntmux/processing/post/ProcessAdditional.php index d1b7066a9..cc6c44330 100755 --- a/nntmux/processing/post/ProcessAdditional.php +++ b/nntmux/processing/post/ProcessAdditional.php @@ -2,11 +2,11 @@ namespace nntmux\processing\post; +use App\Models\Group; use nntmux\Nfo; use nntmux\NZB; use nntmux\NNTP; use nntmux\db\DB; -use nntmux\Groups; use nntmux\ColorCLI; use nntmux\Releases; use nntmux\NameFixer; @@ -70,11 +70,6 @@ class ProcessAdditional */ protected $_nzbContents; - /** - * @var \nntmux\Groups - */ - protected $_groups; - /** * @var \dariusiii\rarinfo\Par2Info */ @@ -402,10 +397,9 @@ class ProcessAdditional $this->_nntp = ($options['NNTP'] instanceof NNTP ? $options['NNTP'] : new NNTP(['Echo' => $this->_echoCLI, 'Settings' => $this->pdo])); $this->_nzb = ($options['NZB'] instanceof NZB ? $options['NZB'] : new NZB()); - $this->_groups = ($options['Groups'] instanceof Groups ? $options['Groups'] : new Groups(['Settings' => $this->pdo])); $this->_archiveInfo = new ArchiveInfo(); $this->_categorize = ($options['Categorize'] instanceof Categorize ? $options['Categorize'] : new Categorize(['Settings' => $this->pdo])); - $this->_nameFixer = ($options['NameFixer'] instanceof NameFixer ? $options['NameFixer'] : new NameFixer(['Echo' =>$this->_echoCLI, 'Groups' => $this->_groups, 'Settings' => $this->pdo, 'Categorize' => $this->_categorize])); + $this->_nameFixer = ($options['NameFixer'] instanceof NameFixer ? $options['NameFixer'] : new NameFixer(['Echo' =>$this->_echoCLI, 'Groups' => null, 'Settings' => $this->pdo, 'Categorize' => $this->_categorize])); $this->_releaseExtra = ($options['ReleaseExtra'] instanceof ReleaseExtra ? $options['ReleaseExtra'] : new ReleaseExtra($this->pdo)); $this->_releaseImage = ($options['ReleaseImage'] instanceof ReleaseImage ? $options['ReleaseImage'] : new ReleaseImage()); $this->_par2Info = new Par2Info(); @@ -2363,7 +2357,7 @@ class ProcessAdditional $this->_passwordStatus = [Releases::PASSWD_NONE]; $this->_releaseHasPassword = false; - $this->_releaseGroupName = $this->_groups->getNameByID($this->_release['groups_id']); + $this->_releaseGroupName = Group::getNameByID($this->_release['groups_id']); $this->_releaseHasNoNFO = false; // Make sure we don't already have an nfo. diff --git a/public/admin/ajax.php b/public/admin/ajax.php index 79ef40e59..fac950758 100644 --- a/public/admin/ajax.php +++ b/public/admin/ajax.php @@ -1,7 +1,7 @@ purge(); + Group::purge(); echo 'All groups purged.'; break; case 'group_edit_reset_all': - (new Groups($settings))->resetall(); + Group::resetall(); echo 'All groups reset.'; break; case 'group_edit_purge_single': $id = (int) $_GET['group_id']; session_write_close(); - (new Groups($settings))->purge($id); + Group::purge($id); echo "Group $id purged."; break; case 'group_edit_reset_single': $id = (int) $_GET['group_id']; session_write_close(); - (new Groups($settings))->reset($id); + Group::reset($id); echo "Group $id reset."; break; case 'group_edit_delete_single': $id = (int) $_GET['group_id']; session_write_close(); - (new Groups($settings))->delete($id); + Group::deleteGroup($id); echo "Group $id deleted."; break; case 'toggle_group_active_status': - print (new Groups($settings))->updateGroupStatus((int) $_GET['group_id'], 'active', (isset($_GET['group_status']) ? (int) $_GET['group_status'] : 0)); + print Group::updateGroupStatus((int) $_GET['group_id'], 'active', (isset($_GET['group_status']) ? (int) $_GET['group_status'] : 0)); break; case 'toggle_group_backfill_status': - print (new Groups($settings))->updateGroupStatus( + print Group::updateGroupStatus( (int) $_GET['group_id'], 'backfill', (isset($_GET['backfill_status']) ? (int) $_GET['backfill_status'] : 0) diff --git a/public/admin/ajax_group-edit.php b/public/admin/ajax_group-edit.php index 41041d255..51a6f30d6 100644 --- a/public/admin/ajax_group-edit.php +++ b/public/admin/ajax_group-edit.php @@ -2,44 +2,43 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Groups; +use App\Models\Group; $admin = new AdminPage; -$group = new Groups(['Settings' => $admin->settings]); // session_write_close(); allows the admin to use the site while the ajax request is being processed. if (isset($_GET['action']) && $_GET['action'] === 2) { $id = (int) $_GET['group_id']; session_write_close(); - $group->delete($id); + Group::deleteGroup($id); echo "Group $id deleted."; } elseif (isset($_GET['action']) && $_GET['action'] === 3) { $id = (int) $_GET['group_id']; session_write_close(); - $group->reset($id); + Group::reset($id); echo "Group $id reset."; } elseif (isset($_GET['action']) && $_GET['action'] === 4) { $id = (int) $_GET['group_id']; session_write_close(); - $group->purge($id); + Group::purge($id); echo "Group $id purged."; } elseif (isset($_GET['action']) && $_GET['action'] === 5) { - $group->resetall(); + Group::resetall(); echo 'All groups reset.'; } elseif (isset($_GET['action']) && $_GET['action'] === 6) { session_write_close(); - $group->purge(); + Group::purge(); echo 'All groups purged.'; } else { if (isset($_GET['group_id'])) { $id = (int) $_GET['group_id']; if (isset($_GET['group_status'])) { $status = isset($_GET['group_status']) ? (int) $_GET['group_status'] : 0; - echo $group->updateGroupStatus($id, 'active', $status); + echo Group::updateGroupStatus($id, 'active', $status); } if (isset($_GET['backfill_status'])) { $status = isset($_GET['backfill_status']) ? (int) $_GET['backfill_status'] : 0; - echo $group->updateGroupStatus($id, 'backfill', $status); + echo Group::updateGroupStatus($id, 'backfill', $status); } } } diff --git a/public/admin/group-bulk.php b/public/admin/group-bulk.php index f649079b0..0b75c4d51 100644 --- a/public/admin/group-bulk.php +++ b/public/admin/group-bulk.php @@ -2,18 +2,17 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Groups; +use App\Models\Group; $page = new AdminPage(); // set the current action -$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view'; +$action = $_REQUEST['action'] ?? 'view'; switch ($action) { case 'submit': if (isset($_POST['groupfilter']) && ! empty($_POST['groupfilter'])) { - $groups = new Groups; - $msgs = $groups->addBulk($_POST['groupfilter'], $_POST['active'], $_POST['backfill']); + $msgs = Group::addBulk($_POST['groupfilter'], $_POST['active'], $_POST['backfill']); } break; default: diff --git a/public/admin/group-edit.php b/public/admin/group-edit.php index 971c9cefa..bb2feb5fb 100644 --- a/public/admin/group-edit.php +++ b/public/admin/group-edit.php @@ -2,10 +2,9 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Groups; +use App\Models\Group; $page = new AdminPage(); -$groups = new Groups(['Settings' => $page->pdo]); $id = 0; // Set the current action. @@ -28,13 +27,13 @@ switch ($action) { case 'submit': if ($_POST['id'] === '') { // Add a new group. - $_POST['name'] = $groups->isValidGroup($_POST['name']); + $_POST['name'] = Group::isValidGroup($_POST['name']); if ($_POST['name'] !== false) { - $groups->add($_POST); + Group::addGroup($_POST); } } else { // Update an existing group. - $groups->update($_POST); + Group::updateGroup($_POST); } header('Location:'.WWW_TOP.'/group-list.php'); break; @@ -44,7 +43,7 @@ switch ($action) { if (isset($_GET['id'])) { $page->title = 'Newsgroup Edit'; $id = $_GET['id']; - $group = $groups->getByID($id); + $group = Group::getGroupByID($id); } else { $page->title = 'Newsgroup Add'; } diff --git a/public/admin/group-list-active.php b/public/admin/group-list-active.php index cce869545..629a8e5be 100644 --- a/public/admin/group-list-active.php +++ b/public/admin/group-list-active.php @@ -2,17 +2,16 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Groups; +use App\Models\Group; $page = new AdminPage(); -$groups = new Groups(['Settings' => $page->pdo]); $gname = ''; if (! empty($_REQUEST['groupname'])) { $gname = $_REQUEST['groupname']; } -$groupcount = $groups->getCount($gname, 1); +$groupcount = Group::getGroupsCount($gname, 1); $offset = $_REQUEST['offset'] ?? 0; $groupname = ! empty($_REQUEST['groupname']) ? $_REQUEST['groupname'] : ''; @@ -28,7 +27,7 @@ $page->smarty->assign('pagerquerybase', WWW_TOP.'/group-list-active.php?'.$group $pager = $page->smarty->fetch('pager.tpl'); $page->smarty->assign('pager', $pager); -$grouplist = $groups->getRange($offset, ITEMS_PER_PAGE, $gname, true); +$grouplist = Group::getGroupsRange($offset, ITEMS_PER_PAGE, $gname, true); $page->smarty->assign('grouplist', $grouplist); diff --git a/public/admin/group-list-inactive.php b/public/admin/group-list-inactive.php index d61cbae42..745794361 100644 --- a/public/admin/group-list-inactive.php +++ b/public/admin/group-list-inactive.php @@ -2,17 +2,16 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Groups; +use App\Models\Group; $page = new AdminPage(); -$groups = new Groups(['Settings' => $page->pdo]); $gname = ''; if (! empty($_REQUEST['groupname'])) { $gname = $_REQUEST['groupname']; } -$groupcount = $groups->getCount($gname, 0); +$groupcount = Group::getGroupsCount($gname, 0); $offset = $_REQUEST['offset'] ?? 0; $groupname = ! empty($_REQUEST['groupname']) ? $_REQUEST['groupname'] : ''; @@ -28,7 +27,7 @@ $page->smarty->assign('pagerquerybase', WWW_TOP.'/group-list-inactive.php?'.$gro $pager = $page->smarty->fetch('pager.tpl'); $page->smarty->assign('pager', $pager); -$grouplist = $groups->getRange($offset, ITEMS_PER_PAGE, $gname); +$grouplist = Group::getGroupsRange($offset, ITEMS_PER_PAGE, $gname); $page->smarty->assign('grouplist', $grouplist); diff --git a/public/admin/group-list.php b/public/admin/group-list.php index 6c0e87232..b1156ae93 100644 --- a/public/admin/group-list.php +++ b/public/admin/group-list.php @@ -2,10 +2,9 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Groups; +use App\Models\Group; $page = new AdminPage(); -$groups = new Groups(['Settings' => $page->pdo]); $groupName = $_REQUEST['groupname'] ?? ''; $offset = $_REQUEST['offset'] ?? 0; @@ -13,12 +12,12 @@ $offset = $_REQUEST['offset'] ?? 0; $page->smarty->assign( [ 'groupname' => $groupName, - 'pagertotalitems' => $groups->getCount($groupName, -1), + 'pagertotalitems' => Group::getGroupsCount($groupName, -1), 'pageroffset' => $offset, 'pageritemsperpage' => ITEMS_PER_PAGE, 'pagerquerybase' => WWW_TOP.'/group-list.php?'.(($groupName !== '') ? "groupname=$groupName" : '').'&offset=', 'pagerquerysuffix' => '', - 'grouplist' => $groups->getRange($offset, ITEMS_PER_PAGE, $groupName), + 'grouplist' => Group::getGroupsRange($offset, ITEMS_PER_PAGE, $groupName), ] ); $page->smarty->assign('pager', $page->smarty->fetch('pager.tpl')); diff --git a/public/pages/browsegroup.php b/public/pages/browsegroup.php index 79601c6fb..4525945c3 100644 --- a/public/pages/browsegroup.php +++ b/public/pages/browsegroup.php @@ -1,15 +1,13 @@ show403(); } -$groups = new Groups(['Settings' => $page->settings]); - -$grouplist = $groups->getRange(false, false, '', true); +$grouplist = Group::getGroupsRange(false, false, '', true); $page->smarty->assign('results', $grouplist); $page->meta_title = 'Browse Groups'; diff --git a/public/pages/search.php b/public/pages/search.php index b4ff81a53..09c32e133 100644 --- a/public/pages/search.php +++ b/public/pages/search.php @@ -1,6 +1,6 @@ show403(); } -$groups = new Groups(['Settings' => $page->settings]); $releases = new Releases(['Groups' => $groups, 'Settings' => $page->settings]); $page->meta_title = 'Search Nzbs'; @@ -202,7 +201,7 @@ $page->smarty->assign( 6 => '3GB', 7 => '4GB', 8 => '8GB', 9 => '16GB', 10 => '32GB', 11 => '64GB', ], 'results' => $results, 'sadvanced' => $searchType !== 'basic', - 'grouplist' => $groups->getGroupsForSelect(), + 'grouplist' => Group::getGroupsForSelect(), 'catlist' => Category::getForSelect(), 'search_description' => $search_description, 'pager' => $page->smarty->fetch('pager.tpl'),