2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-06-14 16:05:55 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
require_once "mime_map.php";
|
|
|
|
require_once "file_extension.php";
|
|
|
|
require_once "mime_type.php";
|
|
|
|
|
|
|
|
class MimeSystem extends Extension
|
|
|
|
{
|
|
|
|
/** @var MimeSystemTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-06-14 16:05:55 +00:00
|
|
|
|
2021-12-14 18:32:47 +00:00
|
|
|
public const VERSION = "ext_mime_version";
|
2020-06-14 16:05:55 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onParseLinkTemplate(ParseLinkTemplateEvent $event): void
|
2020-06-14 16:05:55 +00:00
|
|
|
{
|
|
|
|
$event->replace('$ext', $event->image->get_ext());
|
|
|
|
$event->replace('$mime', $event->image->get_mime());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2020-06-14 16:05:55 +00:00
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
// These upgrades are primarily for initializing mime types on upgrade, and for adjusting mime types whenever an
|
|
|
|
// adjustment needs to be made to the mime types.
|
|
|
|
|
|
|
|
if ($this->get_version(self::VERSION) < 1) {
|
2024-01-15 22:56:06 +00:00
|
|
|
if ($database->is_transaction_open()) {
|
|
|
|
// Each of these commands could hit a lot of data, combining
|
|
|
|
// them into one big transaction would not be a good idea.
|
|
|
|
$database->commit();
|
|
|
|
}
|
2021-09-22 14:42:41 +00:00
|
|
|
$database->set_timeout(null); // These updates can take a little bit
|
2020-06-14 16:05:55 +00:00
|
|
|
|
|
|
|
$extensions = $database->get_col_iterable("SELECT DISTINCT ext FROM images");
|
|
|
|
|
|
|
|
foreach ($extensions as $ext) {
|
|
|
|
$mime = MimeType::get_for_extension($ext);
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
if (empty($mime) || $mime === MimeType::OCTET_STREAM) {
|
2024-02-11 15:47:40 +00:00
|
|
|
throw new UserError("Unknown extension: $ext");
|
2020-06-14 16:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$normalized_extension = FileExtension::get_for_mime($mime);
|
|
|
|
|
|
|
|
$database->execute(
|
2020-10-08 22:40:34 +00:00
|
|
|
"UPDATE images SET mime = :mime, ext = :new_ext WHERE ext = :ext AND (mime IS NULL OR mime != :mime OR ext != :new_ext)",
|
2020-06-14 16:05:55 +00:00
|
|
|
["mime" => $mime, "new_ext" => $normalized_extension, "ext" => $ext]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->set_version(self::VERSION, 1);
|
2024-01-15 22:50:43 +00:00
|
|
|
$database->begin_transaction();
|
2020-06-14 16:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onHelpPageBuilding(HelpPageBuildingEvent $event): void
|
2020-06-14 16:05:55 +00:00
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->key === HelpPages::SEARCH) {
|
2020-06-14 16:05:55 +00:00
|
|
|
$block = new Block();
|
|
|
|
$block->header = "File Types";
|
|
|
|
$block->body = $this->theme->get_help_html();
|
|
|
|
$event->add_block($block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event): void
|
2020-06-14 16:05:55 +00:00
|
|
|
{
|
|
|
|
if (is_null($event->term)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
// check for tags first as tag based searches are more common.
|
|
|
|
if (preg_match("/^ext[=|:]([a-zA-Z0-9]+)$/i", $event->term, $matches)) {
|
|
|
|
$ext = strtolower($matches[1]);
|
|
|
|
$event->add_querylet(new Querylet('images.ext = :ext', ["ext" => $ext]));
|
|
|
|
} elseif (preg_match("/^mime[=|:](.+)$/i", $event->term, $matches)) {
|
|
|
|
$mime = strtolower($matches[1]);
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("images.mime = :mime", ["mime" => $mime]));
|
2020-06-14 16:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|