4in1_ws_web/tools/cli_util.php

111 lines
2.8 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
require_once __DIR__.'/../init.php';
require_once 'lib/admin.php';
(new cli())
->on('admin-add', function() {
list($login, $password) = _get_admin_login_password_input();
if (admin_exists($login))
cli::die("Admin ".$login." already exists");
$id = admin_add($login, $password);
echo "ok: id = $id\n";
})
->on('admin-delete', function() {
$login = cli::input('Login: ');
if (!admin_exists($login))
cli::die("No such admin");
if (!admin_delete($login))
cli::die("Database error");
echo "ok\n";
})
->on('admin-set-password', function() {
list($login, $password) = _get_admin_login_password_input();
echo admin_set_password($login, $password) ? 'ok' : 'fail';
echo "\n";
})
->on('blog-erase', function() {
$db = DB();
$tables = ['posts', 'posts_texts'];
foreach ($tables as $t) {
$db->query("TRUNCATE TABLE $t");
}
})
->on('posts-html', function() {
$kw = ['include_hidden' => true];
$posts = posts::getList(0, posts::getCount(...$kw), ...$kw);
foreach ($posts as $p) {
$texts = $p->getTexts();
foreach ($texts as $t) {
$t->updateHtml();
$t->updateText();
}
}
})
->on('posts-images', function() {
$kw = ['include_hidden' => true];
$posts = posts::getList(0, posts::getCount(...$kw), ...$kw);
foreach ($posts as $p) {
$texts = $p->getTexts();
foreach ($texts as $t) {
$t->updateImagePreviews(true);
}
}
})
->on('pages-html', function() {
$pages = Pages::getAll();
foreach ($pages as $p) {
$p->updateHtml();
}
})
->on('add-files-to-uploads', function() {
$path = cli::input('Enter path: ');
if (!file_exists($path))
cli::die("file $path doesn't exists");
$name = basename($path);
$ext = extension($name);
$id = Uploads::add($path, $name, '');
echo "upload id: $id\n";
})
->on('collection-reindex', function() {
require_once 'lib/files.php';
$collections = array_map(fn($c) => $c->value, FilesCollection::cases());
$s = cli::input('Enter collection to reindex (variants: '.implode(', ', $collections).': ');
$c = FilesCollection::from($s);
$f = "{$s}_reindex";
echo "calling $f()... ";
call_user_func($f);
echo "done\n";
})
->run();
function _get_admin_login_password_input(): array {
$login = cli::input('Login: ');
$pwd1 = cli::silentInput("Password: ");
$pwd2 = cli::silentInput("Again: ");
if ($pwd1 != $pwd2)
cli::die("Passwords do not match");
if (trim($pwd1) == '')
cli::die("Password can not be empty");
if (strlen($login) > ADMIN_LOGIN_MAX_LENGTH)
cli::die("Login is longer than max length (".ADMIN_LOGIN_MAX_LENGTH.")");
return [$login, $pwd1];
}