Compare commits

...

4 commits

Author SHA1 Message Date
koke228666
ba5f4db9f2
Merge c82e2c9f4c into 4b7d2b9b17 2025-05-20 00:22:40 +03:00
celestora
4b7d2b9b17
feat: database upgrade command (#1236) 2025-05-19 23:38:47 +03:00
koke228666
c82e2c9f4c поправил табы 2025-03-09 17:08:23 +03:00
koke228666
04d24aaba3 make creating avatar better 2025-03-07 22:51:31 +03:00
13 changed files with 415 additions and 27 deletions

View file

@ -17,14 +17,16 @@ define("NANOTON", 1000000000);
class FetchToncoinTransactions extends Command
{
private $images;
private $transactions;
protected static $defaultName = "fetch-ton";
public function __construct()
{
$this->transactions = DatabaseConnection::i()->getContext()->table("cryptotransactions");
$ctx = DatabaseConnection::i()->getContext();
if (in_array("cryptotransactions", $ctx->getStructure()->getTables())) {
$this->transactions = $ctx->table("cryptotransactions");
}
parent::__construct();
}
@ -77,7 +79,6 @@ class FetchToncoinTransactions extends Command
$header->writeln("Gonna up the balance of users");
foreach ($response["result"] as $transfer) {
$outputArray;
preg_match('/' . OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["regex"] . '/', $transfer["in_msg"]["message"], $outputArray);
$userId = ctype_digit($outputArray[1]) ? intval($outputArray[1]) : null;
if (is_null($userId)) {

View file

@ -20,7 +20,10 @@ class RebuildImagesCommand extends Command
public function __construct()
{
$this->images = DatabaseConnection::i()->getContext()->table("photos");
$ctx = DatabaseConnection::i()->getContext();
if (in_array("photos", $ctx->getStructure()->getTables())) {
$this->images = $ctx->table("photos");
}
parent::__construct();
}

364
CLI/UpgradeCommand.php Normal file
View file

@ -0,0 +1,364 @@
<?php
declare(strict_types=1);
namespace openvk\CLI;
use Nette\Database\Connection;
use Chandler\Database\DatabaseConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class UpgradeCommand extends Command
{
protected static $defaultName = "upgrade";
private Connection $db;
private Connection $eventDb;
private array $chandlerTables = [
"CHANDLERACLPERMISSIONALIASES",
"CHANDLERACLGROUPSPERMISSIONS",
"CHANDLERACLUSERSPERMISSIONS",
"CHANDLERACLRELATIONS",
"CHANDLERGROUPS",
"CHANDLERTOKENS",
"CHANDLERUSERS",
];
public function __construct()
{
$this->db = DatabaseConnection::i()->getConnection();
$this->eventDb = eventdb()->getConnection();
parent::__construct();
}
protected function configure(): void
{
$this->setDescription("Upgrade OpenVK installation")
->setHelp("This command upgrades database schema after OpenVK was updated")
->addOption(
"quick",
"Q",
InputOption::VALUE_NEGATABLE,
"Don't display warning before migrating database",
false
)
->addOption(
"repair",
"R",
InputOption::VALUE_NEGATABLE,
"Attempt to repair database schema if tables are missing",
false
)
->addOption(
"oneshot",
"O",
InputOption::VALUE_NONE,
"Only execute one operation"
)
->addArgument(
"chandler",
InputArgument::OPTIONAL,
"Location of Chandler installation"
);
}
protected function checkDatabaseReadiness(bool &$chandlerOk, bool &$ovkOk, bool &$eventOk, bool &$migrationsOk): void
{
$tables = $this->db->query("SHOW TABLES")->fetchAll();
$tables = array_map(fn($x) => strtoupper($x->offsetGet(0)), $tables);
$missingTables = array_diff($this->chandlerTables, $tables);
if (sizeof($missingTables) == 0) {
$chandlerOk = true;
} elseif (sizeof($missingTables) == sizeof($this->chandlerTables)) {
$chandlerOk = null;
} else {
$chandlerOk = false;
}
if (is_null($this->eventDb)) {
$eventOk = false;
} elseif (is_null($this->eventDb->query("SHOW TABLES LIKE \"notifications\"")->fetch())) {
$eventOk = null;
} else {
$eventOk = true;
}
$ovkOk = in_array("PROFILES", $tables);
$migrationsOk = in_array("OVK_UPGRADE_HISTORY", $tables);
}
protected function executeSqlScript(
int $errCode,
string $script,
SymfonyStyle $io,
bool $transaction = false,
bool $eventDb = false
): int {
$pdo = ($eventDb ? $this->eventDb : $this->db)->getPdo();
$res = false;
try {
if ($transaction) {
$res = $pdo->beginTransaction();
}
$res = $pdo->exec($script);
if ($transaction) {
$res = $pdo->commit();
}
} catch (\PDOException $e) {
}
if ($res === false) {
goto error;
}
return 0;
error:
$io->getErrorStyle()->error([
"Failed to execute SQL statement:",
implode("\t", $pdo->errorInfo()),
]);
return $errCode;
}
protected function getNextLevel(bool $eventDb = false): int
{
$db = $eventDb ? $this->eventDb : $this->db;
$tbl = $eventDb ? "ovk_events_upgrade_history" : "ovk_upgrade_history";
$record = $db->query("SELECT level FROM $tbl ORDER BY level DESC LIMIT 1");
if (!$record->getRowCount()) {
return 0;
}
return $record->fetchField() + 1;
}
protected function getMigrationFiles(bool $eventDb = false): array
{
$files = [];
$root = dirname(__DIR__ . "/../install/init-static-db.sql");
$dir = $eventDb ? "sqls/eventdb" : "sqls";
foreach (glob("$root/$dir/*.sql") as $file) {
$files[(int) basename($file)] = basename($file);
}
ksort($files);
return $files;
}
protected function installChandler(InputInterface $input, SymfonyStyle $io, bool $drop = false): int
{
$chandlerLocation = $input->getArgument("chandler") ?? (__DIR__ . "/../../../../");
$chandlerConfigLocation = "$chandlerLocation/chandler.yml";
if (!file_exists($chandlerConfigLocation)) {
$err = ["Could not find chandler location. Perhaps your config is too unique?"];
if (!$input->getOption("chandler")) {
$err[] = "Specify absolute path to your chandler installation using the --chandler option.";
}
$io->getErrorStyle()->error($err);
return 21;
}
if ($drop) {
$bar = new ProgressBar($io, sizeof($this->chandlerTables));
$io->writeln("Dropping chandler tables...");
foreach ($bar->iterate($this->chandlerTables) as $table) {
$this->db->query("DROP TABLE IF EXISTS $table;");
}
$io->newLine();
}
$installFile = file_get_contents("$chandlerLocation/install/init-db.sql");
return $this->executeSqlScript(22, $installFile, $io);
}
protected function initSchema(SymfonyStyle $io): int
{
$installFile = file_get_contents(__DIR__ . "/../install/init-static-db.sql");
return $this->executeSqlScript(31, $installFile, $io);
}
protected function initEventSchema(SymfonyStyle $io): int
{
$installFile = file_get_contents(__DIR__ . "/../install/init-event-db.sql");
return $this->executeSqlScript(31, $installFile, $io, true, true);
}
protected function initUpgradeLog(SymfonyStyle $io): int
{
$installFile = file_get_contents(__DIR__ . "/../install/init-migration-table.sql");
$rc = $this->executeSqlScript(31, $installFile, $io);
if ($rc) {
var_dump($rc);
return $rc;
}
$installFile = file_get_contents(__DIR__ . "/../install/init-migration-table-event.sql");
return $this->executeSqlScript(32, $installFile, $io, false, true);
}
protected function runMigrations(SymfonyStyle $io, bool $eventDb, bool $oneshot): int
{
$dir = $eventDb ? "sqls/eventdb" : "sqls";
$tbl = $eventDb ? "ovk_events_upgrade_history" : "ovk_upgrade_history";
$db = $eventDb ? $this->eventDb : $this->db;
$nextLevel = $this->getNextLevel($eventDb);
$migrations = array_filter(
$this->getMigrationFiles($eventDb),
fn($id) => $id >= $nextLevel,
ARRAY_FILTER_USE_KEY
);
if (!sizeof($migrations)) {
return 24;
}
$uname = addslashes(`whoami`);
$bar = new ProgressBar($io, sizeof($migrations));
$bar->setFormat("very_verbose");
foreach ($bar->iterate($migrations) as $num => $migration) {
$script = file_get_contents(__DIR__ . "/../install/$dir/$migration");
$res = $this->executeSqlScript(100 + $num, $script, $io, true, $eventDb);
if ($res != 0) {
$io->getErrorStyle()->error("Error while executing migration №$num");
return $res;
}
$t = time();
$db->query("INSERT INTO $tbl VALUES ($num, $t, \"$uname\");");
if ($oneshot) {
return 5;
}
}
$io->newLine();
return 0;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$oneShotMode = $input->getOption("oneshot");
$io = new SymfonyStyle($input, $output);
if (!$input->getOption("quick")) {
$io->writeln("Do full backup of the database before executing this command!");
$io->writeln("Command will resume execution after 5 seconds.");
$io->writeln("You can skip this warning with --quick option.");
sleep(5);
}
$migrationsOk = false;
$chandlerOk = false;
$eventOk = false;
$ovkOk = false;
$this->checkDatabaseReadiness($chandlerOk, $ovkOk, $eventOk, $migrationsOk);
$res = -1;
if ($chandlerOk === null) {
$io->writeln("Chandler schema not detected, attempting to install...");
$res = $this->installChandler($input, $io);
} elseif ($chandlerOk === false) {
if ($input->getOption("repair")) {
$io->warning("Chandler schema detected but is broken, attempting to repair...");
$res = $this->installChandler($input, $io, true);
} else {
$io->writeln("Chandler schema detected but is broken");
$io->writeln("Run command with --repair to repair (PERMISSIONS WILL BE LOST)");
return 1;
}
}
if ($res > 0) {
return $res;
} elseif ($res == 0 && $oneShotMode) {
return 5;
}
if (!$ovkOk) {
$io->writeln("Initializing OpenVK schema...");
$res = $this->initSchema($io);
if ($res > 0) {
return $res;
} elseif ($oneShotMode) {
return 5;
}
}
if (!$migrationsOk) {
$io->writeln("Initializing upgrade log...");
$res = $this->initUpgradeLog($io);
if ($res > 0) {
return $res;
} elseif ($oneShotMode) {
return 5;
}
}
if ($eventOk !== false) {
if ($eventOk === null) {
$io->writeln("Initializing event database...");
$res = $this->initEventSchema($io);
if ($res > 0) {
return $res;
} elseif ($oneShotMode) {
return 5;
}
}
$io->writeln("Upgrading event database...");
$res = $this->runMigrations($io, true, $oneShotMode);
if ($res == 24) {
$output->writeln("Event database already up to date.");
} elseif ($res > 0) {
return $res;
}
}
$io->writeln("Upgrading database...");
$res = $this->runMigrations($io, false, $oneShotMode);
if (!$res) {
$io->success("Database has been upgraded!");
return 0;
} elseif ($res != 24) {
return $res;
}
$io->writeln("Database up to date. Nothing left to do.");
return 0;
}
}

View file

@ -129,13 +129,15 @@
{var $avatarLink = ((is_null($avatarPhoto) ? FALSE : $avatarPhoto->isAnonymous()) ? "/photo" . ("s/" . base_convert((string) $avatarPhoto->getId(), 10, 32)) : $club->getAvatarLink())}
<div class="avatar_block" style="position:relative;" data-club="{$club->getId()}">
{if $thisUser && $club->canBeModifiedBy($thisUser)}
<a {if $avatarPhoto}style="display:none"{/if} class="add_image_text" id="add_image">{_add_image}</a>
<div {if !$avatarPhoto}style="display:none"{/if} class="avatar_controls">
<div class="avatarDelete hoverable"></div>
<div class="avatar_variants">
<a class="_add_image hoverable" id="add_image"><span>{_upload_new_picture}</span></a>
</div>
<div class="avatar_controls">
<div {if !$hasAvatar}style="display:none"{/if} class="avatarDelete hoverable"></div>
<div class="avatar_variants">
<a {if $hasAvatar}style="display:none"{/if} class="_add_image hoverable upload_image" id="add_image">
<span>{_add_image}</span></a>
<a {if !$hasAvatar}style="display:none"{/if} class="_add_image hoverable set_image" id="add_image">
<span>{_upload_new_picture}</span></a>
</div>
</div>
{/if}
<a href="{$avatarLink|nocheck}">

View file

@ -73,11 +73,13 @@
{var $hasAvatar = !str_contains($user->getAvatarUrl('miniscule'), "/assets/packages/static/openvk/img/camera_200.png")}
{if $thisUser && $user->getId() == $thisUser->getId()}
<a {if $hasAvatar}style="display:none"{/if} class="add_image_text" id="add_image">{_add_image}</a>
<div {if !$hasAvatar}style="display:none"{/if} class="avatar_controls">
<div class="avatarDelete hoverable"></div>
<div class="avatar_controls">
<div {if !$hasAvatar}style="display:none"{/if} class="avatarDelete hoverable"></div>
<div class="avatar_variants">
<a class="_add_image hoverable" id="add_image"><span>{_upload_new_picture}</span></a>
<a {if $hasAvatar}style="display:none"{/if} class="_add_image hoverable upload_image" id="add_image">
<span>{_add_image}</span></a>
<a {if !$hasAvatar}style="display:none"{/if} class="_add_image hoverable set_image" id="add_image">
<span>{_upload_new_picture}</span></a>
</div>
</div>
{/if}

View file

@ -2179,9 +2179,11 @@ $(document).on("click", "#add_image", (e) => {
document.querySelector("#bigAvatar").src = response.url
document.querySelector("#bigAvatar").parentNode.href = "/photo" + response.new_photo
document.querySelector(".add_image_text").style.display = "none"
document.querySelector(".avatar_controls").style.display = "block"
document.querySelector(".avatar_controls .set_image").style.display = "block"
document.querySelector(".avatar_controls .avatarDelete").style.display = "block"
document.querySelector(".avatar_controls .upload_image").style.display = "none"
}
})
})
@ -2305,17 +2307,18 @@ $(document).on("click", ".avatarDelete", (e) => {
}
document.querySelector(".avatarDelete").classList.remove("lagged")
u("body").removeClass("dimmed");
document.querySelector("html").style.overflowY = "scroll"
u(".ovk-diag-cont").remove()
document.querySelector("#bigAvatar").src = response.url
document.querySelector("#bigAvatar").parentNode.href = response.new_photo ? ("/photo" + response.new_photo) : "javascript:void(0)"
if(!response.has_new_photo) {
document.querySelector(".avatar_controls").style.display = "none"
document.querySelector(".add_image_text").style.display = "block"
document.querySelector(".avatar_controls .set_image").style.display = "none"
document.querySelector(".avatar_controls .avatarDelete").style.display = "none"
document.querySelector(".avatar_controls .upload_image").style.display = "block"
}
}
})

View file

@ -443,7 +443,10 @@ function downloadable_name(string $text): string
}
return (function () {
_ovk_check_environment();
if (php_sapi_name() != "cli") {
_ovk_check_environment();
}
require __DIR__ . "/vendor/autoload.php";
setlocale(LC_TIME, "POSIX");

View file

@ -0,0 +1,5 @@
CREATE TABLE `ovk_events_upgrade_history` (
`level` smallint UNSIGNED NOT NULL,
`timestamp` bigint(20) UNSIGNED NOT NULL,
`operator` varchar(256) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT "Maintenance Script"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

View file

@ -0,0 +1,5 @@
CREATE TABLE `ovk_upgrade_history` (
`level` smallint UNSIGNED NOT NULL,
`timestamp` bigint(20) UNSIGNED NOT NULL,
`operator` varchar(256) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT "Maintenance Script"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

View file

@ -10,13 +10,10 @@ CREATE TABLE IF NOT EXISTS `coin_vouchers` (
`deleted` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DELIMITER $$
CREATE TRIGGER `coinVoucherTokenAutoGen` BEFORE INSERT ON `coin_vouchers`
FOR EACH ROW IF NEW.token IS NULL THEN
SET NEW.token = SUBSTRING(UPPER(REPLACE(UUID(), "-", "")), 1, 24);
END IF
$$
DELIMITER ;
END IF;
CREATE TABLE IF NOT EXISTS `gifts` (
`id` bigint(20) unsigned NOT NULL,

View file

@ -1,6 +1,6 @@
CREATE TABLE `cryptotransactions` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`hash` varchar(45) COLLATE utf8mb4_general_nopad_ci NOT NULL,
`hash` varchar(45) COLLATE utf8mb4_unicode_520_ci NOT NULL,
`lt` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_nopad_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

View file

@ -10,6 +10,7 @@ use Symfony\Component\Console\Application;
require(__DIR__ . "/chandler_loader.php");
$application = new Application();
$application->add(new CLI\UpgradeCommand());
$application->add(new CLI\RebuildImagesCommand());
$application->add(new CLI\FetchToncoinTransactions());

2
openvkctl.cmd Normal file
View file

@ -0,0 +1,2 @@
@echo off
php openvkctl %*