move pg() accessor into Base class

This commit is contained in:
Spine
2025-03-19 01:24:14 +00:00
parent 964a39ef6a
commit 3447e855e8
31 changed files with 25 additions and 81 deletions

View File

@@ -3,6 +3,8 @@
namespace Gazelle;
abstract class Base {
use Pg;
protected static DB\Mysql $db;
protected static Cache $cache;
protected static BaseRequestContext $requestContext;

View File

@@ -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);

View File

@@ -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 {
/**

View File

@@ -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;

View File

@@ -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';

View File

@@ -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;

View File

@@ -3,8 +3,6 @@
namespace Gazelle;
class LoginWatch extends Base {
use Pg;
protected int $id;
protected int $userId = 0;

View File

@@ -3,8 +3,6 @@
namespace Gazelle\Manager;
class DNU extends \Gazelle\Base {
use \Gazelle\Pg;
public function create(
string $name,
string $description,

View File

@@ -8,8 +8,6 @@ namespace Gazelle\Manager;
*/
class EmailBlacklist extends \Gazelle\Base {
use \Gazelle\Pg;
protected string $filterComment;
protected string $filterEmail;

View File

@@ -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;

View File

@@ -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("

View File

@@ -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;

View File

@@ -3,8 +3,6 @@
namespace Gazelle\Manager;
class ReportAuto extends \Gazelle\BaseManager {
use \Gazelle\Pg;
protected array $reportCategories;
public function __construct(

View File

@@ -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 {

View File

@@ -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 [];

View File

@@ -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 = [];

View File

@@ -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;

View File

@@ -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()];

View File

@@ -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(

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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';

View File

@@ -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;
}

View File

@@ -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';

View File

@@ -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',

View File

@@ -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;

View File

@@ -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';

View File

@@ -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 {

View File

@@ -3,8 +3,6 @@
namespace Gazelle\User;
class Warning extends \Gazelle\BaseUser {
use \Gazelle\Pg;
final public const tableName = 'user_warning';
protected array $info;

View File

@@ -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 = [];