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/core/imageboard/event.php

125 lines
2.9 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2019-02-22 19:57:45 +00:00
namespace Shimmie2;
2019-02-22 19:57:45 +00:00
/**
* An image is being added to the database.
*/
class ImageAdditionEvent extends Event
{
/**
* A new image is being added to the database - just the image,
* metadata will come later with ImageInfoSetEvent (and if that
* fails, then the image addition transaction will be rolled back)
*/
2022-10-28 00:45:35 +00:00
public function __construct(
public Image $image,
) {
2020-01-26 13:19:35 +00:00
parent::__construct();
}
2019-02-22 19:57:45 +00:00
}
/**
* An image is being deleted.
*/
class ImageDeletionEvent extends Event
{
/**
* Deletes an image.
*
* Used by things like tags and comments handlers to
* clean out related rows in their tables.
*/
2022-10-28 00:45:35 +00:00
public function __construct(
public Image $image,
public bool $force = false,
) {
2020-01-26 13:19:35 +00:00
parent::__construct();
}
2019-02-22 19:57:45 +00:00
}
/**
* An image is being replaced.
*/
class ImageReplaceEvent extends Event
{
public string $old_hash;
public string $new_hash;
/**
* Replaces an image file.
*
* Updates an existing ID in the database to use a new image
* file, leaving the tags and such unchanged. Also removes
* the old image file and thumbnail from the disk.
*/
2022-10-28 00:45:35 +00:00
public function __construct(
public Image $image,
public string $tmp_filename,
2022-10-28 00:45:35 +00:00
) {
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->old_hash = $image->hash;
$hash = md5_file($tmp_filename);
assert($hash !== false, "Failed to hash file $tmp_filename");
$this->new_hash = $hash;
}
2019-02-22 19:57:45 +00:00
}
class ImageReplaceException extends SCoreException
{
2019-02-22 19:57:45 +00:00
}
/**
* Request a thumbnail be made for an image object.
*/
class ThumbnailGenerationEvent extends Event
{
public bool $generated;
/**
* Request a thumbnail be made for an image object
*/
2022-10-28 00:45:35 +00:00
public function __construct(
public Image $image,
2023-11-11 21:49:12 +00:00
public bool $force = false
2022-10-28 00:45:35 +00:00
) {
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->generated = false;
}
2019-02-22 19:57:45 +00:00
}
/*
* ParseLinkTemplateEvent:
* $link -- the formatted text (with each element URL Escape'd)
* $text -- the formatted text (not escaped)
2019-02-22 19:57:45 +00:00
* $original -- the formatting string, for reference
* $image -- the image who's link is being parsed
*/
class ParseLinkTemplateEvent extends Event
{
public string $link;
public string $text;
public string $original;
public Image $image;
public function __construct(string $link, Image $image)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->link = $link;
$this->text = $link;
$this->original = $link;
$this->image = $image;
}
public function replace(string $needle, ?string $replace): void
{
2020-02-23 18:12:14 +00:00
if (!is_null($replace)) {
$this->link = str_replace($needle, url_escape($replace), $this->link);
$this->text = str_replace($needle, $replace, $this->text);
}
}
2019-02-22 19:57:45 +00:00
}