diff --git a/app/Base.php b/app/Base.php index c4054a991..20feac36e 100644 --- a/app/Base.php +++ b/app/Base.php @@ -3,6 +3,8 @@ namespace Gazelle; abstract class Base { + use Pg; + protected static DB\Mysql $db; protected static Cache $cache; protected static BaseRequestContext $requestContext; diff --git a/app/BasePgObject.php b/app/BasePgObject.php index 50a59b3ad..a12cbd97b 100644 --- a/app/BasePgObject.php +++ b/app/BasePgObject.php @@ -3,17 +3,19 @@ namespace Gazelle; abstract class BasePgObject extends BaseObject { - use Pg; - public function modify(): bool { if (!$this->dirty()) { return false; } - $set = implode(', ', [...array_map(fn($f) => "$f = ?", array_keys($this->updateField))]); + $set = implode( + ', ', + [...array_map(fn($f) => "$f = ?", array_keys($this->updateField))] + ); $args = [...array_values($this->updateField)]; $args[] = $this->id(); $rowCount = $this->pg()->prepared_query( - "UPDATE " . static::tableName . " SET $set WHERE " . static::pkName . " = ?", + "update /* BasePgObject */ " . static::tableName + . " set $set where " . static::pkName . " = ?", ...$args ); $success = ($rowCount === 1); diff --git a/app/Collage.php b/app/Collage.php index 0b2241be4..580347b94 100644 --- a/app/Collage.php +++ b/app/Collage.php @@ -6,7 +6,6 @@ use Gazelle\Enum\CollageType; use Gazelle\Enum\LeechType; use Gazelle\Enum\LeechReason; use Gazelle\Intf\CollageEntry; -use Pg; class Collage extends BaseObject { /** diff --git a/app/Donate/Monero.php b/app/Donate/Monero.php index 90e9bb6e6..4900e54ea 100644 --- a/app/Donate/Monero.php +++ b/app/Donate/Monero.php @@ -2,11 +2,10 @@ namespace Gazelle\Donate; -use Gazelle\Pg; use MoneroIntegrations\MoneroPhp; class Monero { - use Pg; + use \Gazelle\Pg; private readonly string $spendKey; private readonly string $viewKey; diff --git a/app/Forum.php b/app/Forum.php index 5608ae1a9..31c671753 100644 --- a/app/Forum.php +++ b/app/Forum.php @@ -3,8 +3,6 @@ namespace Gazelle; class Forum extends BaseObject { - use Pg; - final public const tableName = 'forums'; final public const CACHE_FORUM = 'forum_%d'; final public const CACHE_THREAD_INFO = 'thread_%d_info'; diff --git a/app/Login.php b/app/Login.php index e6a35cbe8..e04785c30 100644 --- a/app/Login.php +++ b/app/Login.php @@ -5,8 +5,6 @@ namespace Gazelle; use Gazelle\Enum\UserAuditEvent; class Login extends Base { - use Pg; - final public const NO_ERROR = 0; final public const ERR_CREDENTIALS = 1; final public const ERR_UNCONFIRMED = 2; diff --git a/app/LoginWatch.php b/app/LoginWatch.php index 6d82daf27..97918fc04 100644 --- a/app/LoginWatch.php +++ b/app/LoginWatch.php @@ -3,8 +3,6 @@ namespace Gazelle; class LoginWatch extends Base { - use Pg; - protected int $id; protected int $userId = 0; diff --git a/app/Manager/DNU.php b/app/Manager/DNU.php index 50ec11b28..b89df5c72 100644 --- a/app/Manager/DNU.php +++ b/app/Manager/DNU.php @@ -3,8 +3,6 @@ namespace Gazelle\Manager; class DNU extends \Gazelle\Base { - use \Gazelle\Pg; - public function create( string $name, string $description, diff --git a/app/Manager/EmailBlacklist.php b/app/Manager/EmailBlacklist.php index 18e96ebd7..6f06b224b 100644 --- a/app/Manager/EmailBlacklist.php +++ b/app/Manager/EmailBlacklist.php @@ -8,8 +8,6 @@ namespace Gazelle\Manager; */ class EmailBlacklist extends \Gazelle\Base { - use \Gazelle\Pg; - protected string $filterComment; protected string $filterEmail; diff --git a/app/Manager/IPv4.php b/app/Manager/IPv4.php index 14b5dba6a..e26761244 100644 --- a/app/Manager/IPv4.php +++ b/app/Manager/IPv4.php @@ -8,8 +8,6 @@ namespace Gazelle\Manager; */ class IPv4 extends \Gazelle\Base { - use \Gazelle\Pg; - final protected const CACHE_KEY = 'ipv4_bans_'; protected string $filterNotes; diff --git a/app/Manager/InviteSource.php b/app/Manager/InviteSource.php index af868de9c..b1974f32c 100644 --- a/app/Manager/InviteSource.php +++ b/app/Manager/InviteSource.php @@ -3,10 +3,8 @@ namespace Gazelle\Manager; class InviteSource extends \Gazelle\Base { - use \Gazelle\Pg; - /** - * Create an invitation source name (usually the initials or acronym of a tracker + * Create an invitation source name (usually the initials or acronym of a tracker) */ public function create(string $name): int { self::$db->prepared_query(" diff --git a/app/Manager/Notification.php b/app/Manager/Notification.php index 1031675ca..4da74286e 100644 --- a/app/Manager/Notification.php +++ b/app/Manager/Notification.php @@ -6,8 +6,6 @@ use Gazelle\Enum\NotificationTicketState; use Gazelle\Enum\NotificationType; class Notification extends \Gazelle\Base { - use \Gazelle\Pg; - // Option types final public const OPT_PUSH = 3; final public const OPT_POPUP_PUSH = 4; diff --git a/app/Manager/ReportAuto.php b/app/Manager/ReportAuto.php index 45796b91a..61908614f 100644 --- a/app/Manager/ReportAuto.php +++ b/app/Manager/ReportAuto.php @@ -3,8 +3,6 @@ namespace Gazelle\Manager; class ReportAuto extends \Gazelle\BaseManager { - use \Gazelle\Pg; - protected array $reportCategories; public function __construct( diff --git a/app/Manager/ReportAutoType.php b/app/Manager/ReportAutoType.php index c9afd0fff..d43da7205 100644 --- a/app/Manager/ReportAutoType.php +++ b/app/Manager/ReportAutoType.php @@ -3,8 +3,6 @@ namespace Gazelle\Manager; class ReportAutoType extends \Gazelle\BaseManager { - use \Gazelle\Pg; - protected array $reportTypes = []; public function findById(int $id): ?\Gazelle\ReportAuto\Type { diff --git a/app/Manager/SSLHost.php b/app/Manager/SSLHost.php index b4e63287e..207e57e25 100644 --- a/app/Manager/SSLHost.php +++ b/app/Manager/SSLHost.php @@ -3,8 +3,6 @@ namespace Gazelle\Manager; class SSLHost extends \Gazelle\Base { - use \Gazelle\Pg; - public function lookup(string $hostname, int $port): array { if (!preg_match('/^(?:[\w-]+)(?:\.[\w-]+)+$/', $hostname)) { return []; diff --git a/app/Manager/SiteLog.php b/app/Manager/SiteLog.php index 2ee96a128..d95526c9e 100644 --- a/app/Manager/SiteLog.php +++ b/app/Manager/SiteLog.php @@ -3,8 +3,6 @@ namespace Gazelle\Manager; class SiteLog extends \Gazelle\Base { - use \Gazelle\Pg; - final protected const CACHE_TERM = 'site_log_'; protected array $usernames = []; diff --git a/app/Manager/Tor.php b/app/Manager/Tor.php index 2ee479400..0f955dc82 100644 --- a/app/Manager/Tor.php +++ b/app/Manager/Tor.php @@ -3,28 +3,24 @@ namespace Gazelle\Manager; class Tor extends \Gazelle\Base { - public function __construct( - protected \Gazelle\DB\Pg $pg = new \Gazelle\DB\Pg(PG_RW_DSN) - ) { } - public function add(string $text): int { if (!preg_match_all('/(\d{1,3}(?:\.\d{1,3}){3})/', $text, $match)) { return 0; } $quad = array_unique($match[0]); - $this->pg->pdo()->beginTransaction(); - $this->pg->pdo()->query(" + $this->pg()->pdo()->beginTransaction(); + $this->pg()->pdo()->query(" CREATE TEMPORARY TABLE tor_node_new (ipv4 inet) "); - $this->pg->prepared_query( + $this->pg()->prepared_query( sprintf(" INSERT INTO tor_node_new (ipv4) VALUES %s ", placeholders($quad, '(?)') ), ...$quad ); - $st = $this->pg->pdo()->query(" + $st = $this->pg()->pdo()->query(" DELETE FROM tor_node WHERE ipv4 NOT IN ( SELECT ipv4 FROM tor_node_new @@ -34,29 +30,29 @@ class Tor extends \Gazelle\Base { if ($st) { $changed = -$st->rowCount(); } - $this->pg->pdo()->query(" + $this->pg()->pdo()->query(" DELETE FROM tor_node_new WHERE ipv4 IN ( SELECT ipv4 FROM tor_node ) "); - $st = $this->pg->pdo()->query(" + $st = $this->pg()->pdo()->query(" INSERT INTO tor_node (ipv4) SELECT ipv4 FROM tor_node_new "); if ($st) { $changed += $st->rowCount(); } - $this->pg->pdo()->query(" + $this->pg()->pdo()->query(" DROP TABLE tor_node_new "); - $this->pg->pdo()->commit(); + $this->pg()->pdo()->commit(); return $changed; } public function exitNodeList(): array { - return $this->pg->all(" + return $this->pg()->all(" SELECT t.ipv4, t.created, coalesce(a.cc, 'XX') as cc, @@ -70,7 +66,7 @@ class Tor extends \Gazelle\Base { } public function isExitNode(string $ip): bool { - return BLOCK_TOR ? (bool)$this->pg->scalar(" + return BLOCK_TOR ? (bool)$this->pg()->scalar(" SELECT 1 FROM tor_node WHERE ipv4 = ? ", $ip ) : false; diff --git a/app/Manager/UserToken.php b/app/Manager/UserToken.php index bfa823f5d..c9d552de4 100644 --- a/app/Manager/UserToken.php +++ b/app/Manager/UserToken.php @@ -5,8 +5,6 @@ namespace Gazelle\Manager; use Gazelle\Enum\UserTokenType; class UserToken extends \Gazelle\BaseManager { - use \Gazelle\Pg; - public function create(UserTokenType $type, \Gazelle\User $user, string|null $value = null): \Gazelle\User\Token { $field = ['type', 'id_user']; $args = [$type->value, $user->id()]; diff --git a/app/ReportAuto/Type.php b/app/ReportAuto/Type.php index b1376a99d..06c743d71 100644 --- a/app/ReportAuto/Type.php +++ b/app/ReportAuto/Type.php @@ -6,8 +6,6 @@ namespace Gazelle\ReportAuto; * A ReportAuto\Type is a type for some report. This Type can belong to a category. */ class Type extends \Gazelle\Base { - use \Gazelle\Pg; - protected array $info; public function __construct( diff --git a/app/Search/ASN.php b/app/Search/ASN.php index 5c5291051..28d7be977 100644 --- a/app/Search/ASN.php +++ b/app/Search/ASN.php @@ -3,20 +3,16 @@ namespace Gazelle\Search; class ASN extends \Gazelle\Base { - public function __construct( - protected \Gazelle\DB\Pg $pg = new \Gazelle\DB\Pg(PG_RW_DSN) - ) { } - public function findByASN(int $asn): array { return [ - 'info' => $this->pg->rowAssoc(" + 'info' => $this->pg()->rowAssoc(" SELECT a.name, a.cc FROM geo.asn a WHERE a.id_asn = ? ", $asn ), - 'network' => $this->pg->all(" + 'network' => $this->pg()->all(" SELECT an.network FROM geo.asn_network an WHERE an.id_asn = ? @@ -32,7 +28,7 @@ class ASN extends \Gazelle\Base { } $ipList = array_map(fn ($ip) => $ip === '' ? '0.0.0.0' : $ip, $ipList); $ipList = array_map(fn ($ip) => str_contains($ip, '%3A') ? '0.0.0.0' : $ip, $ipList); // filter truncated IPv6 addresses from ocelot - $result = $this->pg->all(" + $result = $this->pg()->all(" SELECT lu.ip, an.network, coalesce(a.cc, 'XX') AS cc, @@ -54,7 +50,7 @@ class ASN extends \Gazelle\Base { } public function searchName(string $text): array { - return $this->pg->all(" + return $this->pg()->all(" SELECT id_asn, cc, name, @@ -70,7 +66,7 @@ class ASN extends \Gazelle\Base { } public function similarName(string $text): array { - return $this->pg->all(" + return $this->pg()->all(" SELECT word, similarity(word, ?) as similarity FROM geo.asn_trg diff --git a/app/Search/IPv4.php b/app/Search/IPv4.php index 558533b14..f41c0dc9d 100644 --- a/app/Search/IPv4.php +++ b/app/Search/IPv4.php @@ -3,8 +3,6 @@ namespace Gazelle\Search; class IPv4 extends \Gazelle\Base { - use \Gazelle\Pg; - final public const ASC = 0; final public const DESC = 1; diff --git a/app/Stats/Economic.php b/app/Stats/Economic.php index 957093611..cd5a8d3b4 100644 --- a/app/Stats/Economic.php +++ b/app/Stats/Economic.php @@ -5,8 +5,6 @@ namespace Gazelle\Stats; use Gazelle\Enum\UserStatus; class Economic extends \Gazelle\Base { - use \Gazelle\Pg; - final public const CACHE_KEY = 'stats_eco'; protected array $info; diff --git a/app/Torrent.php b/app/Torrent.php index 0e599e00f..0126743e1 100644 --- a/app/Torrent.php +++ b/app/Torrent.php @@ -3,8 +3,6 @@ namespace Gazelle; class Torrent extends TorrentAbstract { - use Pg; - final public const tableName = 'torrents'; final public const CACHE_KEY = 't2_%d'; final public const CACHE_FOLDERNAME = 'foldername_%s'; diff --git a/app/User/AuditTrail.php b/app/User/AuditTrail.php index e53fe5cb9..7cf44f731 100644 --- a/app/User/AuditTrail.php +++ b/app/User/AuditTrail.php @@ -6,8 +6,6 @@ use Gazelle\Enum\UserAuditEvent; use Gazelle\Enum\UserAuditOrder; class AuditTrail extends \Gazelle\BaseUser { - use \Gazelle\Pg; - public function flush(): static { return $this; } diff --git a/app/User/ExternalProfile.php b/app/User/ExternalProfile.php index 98e5f6f7d..ee3c22676 100644 --- a/app/User/ExternalProfile.php +++ b/app/User/ExternalProfile.php @@ -3,8 +3,6 @@ namespace Gazelle\User; class ExternalProfile extends \Gazelle\BaseUser { - use \Gazelle\Pg; - final public const tableName = 'user_external_profile'; final public const pkName = 'id_user'; diff --git a/app/User/History.php b/app/User/History.php index 465ed8000..9f15620c4 100644 --- a/app/User/History.php +++ b/app/User/History.php @@ -3,8 +3,6 @@ namespace Gazelle\User; class History extends \Gazelle\BaseUser { - use \Gazelle\Pg; - public function __construct( \Gazelle\User $user, protected string $column = 'ip', diff --git a/app/User/MultiFactorAuth.php b/app/User/MultiFactorAuth.php index c1c8a7655..6b5d70902 100644 --- a/app/User/MultiFactorAuth.php +++ b/app/User/MultiFactorAuth.php @@ -8,8 +8,6 @@ use Gazelle\Manager; use Gazelle\User; class MultiFactorAuth extends \Gazelle\BaseUser { - use \Gazelle\Pg; - protected const RECOVERY_KEY_LEN = 20; private string|false $secret; diff --git a/app/User/Notification.php b/app/User/Notification.php index db7efea10..38bf77f2c 100644 --- a/app/User/Notification.php +++ b/app/User/Notification.php @@ -3,8 +3,6 @@ namespace Gazelle\User; class Notification extends \Gazelle\BaseUser { - use \Gazelle\Pg; - final public const tableName = 'users_notifications_settings'; final protected const CACHE_KEY = 'u_notif2_%d'; diff --git a/app/User/Token.php b/app/User/Token.php index 36fdda710..6fc360244 100644 --- a/app/User/Token.php +++ b/app/User/Token.php @@ -5,8 +5,6 @@ namespace Gazelle\User; use Gazelle\Enum\UserTokenType; class Token extends \Gazelle\BaseUser { - use \Gazelle\Pg; - final public const tableName = 'user_token'; public function flush(): static { diff --git a/app/User/Warning.php b/app/User/Warning.php index 7bfef9dc4..b5c7d19f2 100644 --- a/app/User/Warning.php +++ b/app/User/Warning.php @@ -3,8 +3,6 @@ namespace Gazelle\User; class Warning extends \Gazelle\BaseUser { - use \Gazelle\Pg; - final public const tableName = 'user_warning'; protected array $info; diff --git a/app/UserCreator.php b/app/UserCreator.php index 831aa3d2a..b16a2f140 100644 --- a/app/UserCreator.php +++ b/app/UserCreator.php @@ -9,8 +9,6 @@ use Gazelle\Exception\UserCreatorException; use Gazelle\Util\Time; class UserCreator extends Base { - use Pg; - protected bool $newInstall; protected array $note = []; protected array $email = [];