This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/alias_editor/main.php

199 lines
6.5 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
2019-12-26 00:36:32 +00:00
use MicroCRUD\ActionColumn;
2019-12-01 01:02:18 +00:00
use MicroCRUD\TextColumn;
use MicroCRUD\Table;
class AliasTable extends Table
{
public function __construct(\FFSPHP\PDO $db)
{
parent::__construct($db);
$this->table = "aliases";
$this->base_query = "SELECT * FROM aliases";
$this->primary_key = "oldtag";
$this->size = 100;
$this->limit = 1000000;
2019-12-26 00:36:32 +00:00
$this->set_columns([
new AutoCompleteColumn("oldtag", "Old Tag"),
new AutoCompleteColumn("newtag", "New Tag"),
2019-12-26 00:36:32 +00:00
new ActionColumn("oldtag"),
]);
2019-12-01 01:02:18 +00:00
$this->order_by = ["oldtag"];
2024-01-08 21:03:04 +00:00
$this->table_attrs = ["class" => "zebra form"];
2019-12-01 01:02:18 +00:00
}
}
class AddAliasEvent extends Event
{
public string $oldtag;
public string $newtag;
public function __construct(string $oldtag, string $newtag)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->oldtag = trim($oldtag);
$this->newtag = trim($newtag);
}
}
2020-01-29 00:49:21 +00:00
class DeleteAliasEvent extends Event
{
public string $oldtag;
2020-01-29 00:49:21 +00:00
public function __construct(string $oldtag)
{
2020-02-01 18:11:00 +00:00
parent::__construct();
$this->oldtag = $oldtag;
}
}
2024-02-11 15:47:40 +00:00
class AddAliasException extends UserError
{
}
class AliasEditor extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var AliasEditorTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-01-26 13:19:35 +00:00
public function onPageRequest(PageRequestEvent $event): void
{
global $config, $database, $page, $user;
2024-02-11 11:34:09 +00:00
if ($event->page_matches("alias/add", method: "POST", permission: Permissions::MANAGE_ALIAS_LIST)) {
$input = validate_input(["c_oldtag" => "string", "c_newtag" => "string"]);
2024-02-11 14:51:55 +00:00
send_event(new AddAliasEvent($input['c_oldtag'], $input['c_newtag']));
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("alias/list"));
2024-02-11 11:34:09 +00:00
}
if ($event->page_matches("alias/remove", method: "POST", permission: Permissions::MANAGE_ALIAS_LIST)) {
$input = validate_input(["d_oldtag" => "string"]);
send_event(new DeleteAliasEvent($input['d_oldtag']));
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("alias/list"));
}
if ($event->page_matches("alias/list")) {
$t = new AliasTable($database->raw_db());
$t->token = $user->get_auth_token();
$t->inputs = $event->GET;
$t->size = $config->get_int('alias_items_per_page', 30);
if ($user->can(Permissions::MANAGE_ALIAS_LIST)) {
$t->create_url = make_link("alias/add");
$t->delete_url = make_link("alias/remove");
}
2024-02-11 11:34:09 +00:00
$this->theme->display_aliases($t->table($t->query()), $t->paginator());
}
if ($event->page_matches("alias/export/aliases.csv")) {
$page->set_mode(PageMode::DATA);
$page->set_mime(MimeType::CSV);
$page->set_filename("aliases.csv");
$page->set_data($this->get_alias_csv($database));
}
if ($event->page_matches("alias/import", method: "POST", permission: Permissions::MANAGE_ALIAS_LIST)) {
if (count($_FILES) > 0) {
$tmp = $_FILES['alias_file']['tmp_name'];
$contents = \Safe\file_get_contents($tmp);
2024-02-11 11:34:09 +00:00
$this->add_alias_csv($contents);
log_info("alias_editor", "Imported aliases from file", "Imported aliases"); # FIXME: how many?
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("alias/list"));
} else {
$this->theme->display_error(400, "No File Specified", "You have to upload a file");
}
}
}
public function onAddAlias(AddAliasEvent $event): void
{
global $database;
2019-12-15 16:01:32 +00:00
$row = $database->get_row(
"SELECT * FROM aliases WHERE lower(oldtag)=lower(:oldtag)",
2023-11-11 21:49:12 +00:00
["oldtag" => $event->oldtag]
2019-12-15 16:01:32 +00:00
);
if ($row) {
throw new AddAliasException("{$row['oldtag']} is already an alias for {$row['newtag']}");
}
$row = $database->get_row(
"SELECT * FROM aliases WHERE lower(oldtag)=lower(:newtag)",
["newtag" => $event->newtag]
);
if ($row) {
throw new AddAliasException("{$row['oldtag']} is itself an alias for {$row['newtag']}");
}
2019-12-15 16:01:32 +00:00
$database->execute(
"INSERT INTO aliases(oldtag, newtag) VALUES(:oldtag, :newtag)",
["oldtag" => $event->oldtag, "newtag" => $event->newtag]
);
log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}", "Added alias");
}
public function onDeleteAlias(DeleteAliasEvent $event): void
2020-01-29 00:49:21 +00:00
{
global $database;
$database->execute("DELETE FROM aliases WHERE oldtag=:oldtag", ["oldtag" => $event->oldtag]);
log_info("alias_editor", "Deleted alias for {$event->oldtag}", "Deleted alias");
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
{
2023-11-11 21:49:12 +00:00
if ($event->parent == "tags") {
$event->add_nav_link("aliases", new Link('alias/list'), "Aliases", NavLink::is_active(["alias"]));
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::MANAGE_ALIAS_LIST)) {
$event->add_link("Alias Editor", make_link("alias/list"));
}
}
private function get_alias_csv(Database $database): string
{
$csv = "";
$aliases = $database->get_pairs("SELECT oldtag, newtag FROM aliases ORDER BY newtag");
foreach ($aliases as $old => $new) {
assert(is_string($new));
$csv .= "\"$old\",\"$new\"\n";
}
return $csv;
}
2023-01-11 18:28:43 +00:00
private function add_alias_csv(string $csv): int
{
$csv = str_replace("\r", "\n", $csv);
2020-03-02 15:21:27 +00:00
$i = 0;
foreach (explode("\n", $csv) as $line) {
$parts = str_getcsv($line);
if (count($parts) == 2) {
assert(is_string($parts[0]));
assert(is_string($parts[1]));
2024-02-11 15:47:40 +00:00
send_event(new AddAliasEvent($parts[0], $parts[1]));
$i++;
}
}
2020-03-02 15:21:27 +00:00
return $i;
}
/**
* Get the priority for this extension.
*
* Add alias *after* mass tag editing, else the MTE will
* search for the images and be redirected to the alias,
* missing out the images tagged with the old tag.
*/
public function get_priority(): int
{
return 60;
}
}