From 3a57817fc226c7b9b55e89fda0b786e0683e2a40 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 9 Feb 2020 19:22:25 +0000 Subject: [PATCH] Spread ParseLinkTemplate work across relevant extensions --- SPEED.md | 1 - core/imageboard/event.php | 6 ++++-- core/imageboard/image.php | 43 ++------------------------------------- core/tests/util.test.php | 1 - ext/image/main.php | 14 +++++++++++++ ext/media/main.php | 15 ++++++++++++++ ext/rule34/main.php | 5 ++++- ext/setup/main.php | 7 +++++++ ext/tag_edit/main.php | 10 +++++++-- 9 files changed, 54 insertions(+), 48 deletions(-) diff --git a/SPEED.md b/SPEED.md index 52687bec..c98fe559 100644 --- a/SPEED.md +++ b/SPEED.md @@ -34,7 +34,6 @@ Notable behaviour changes: - We only show the first 500 pages of results for any query, except for the most simple (no tags, or one positive tag) - We only ever show the first 5,000 results for complex queries -- `ParseLinkTemplateEvent` is disabled - Only comments from the past 24 hours show up in /comment/list - Web crawlers are blocked from creating too many nonsense searches - The first 10 pages in the index get extra caching diff --git a/core/imageboard/event.php b/core/imageboard/event.php index 51b92489..ca163771 100644 --- a/core/imageboard/event.php +++ b/core/imageboard/event.php @@ -135,8 +135,10 @@ class ParseLinkTemplateEvent extends Event $this->image = $image; } - public function replace(string $needle, string $replace): void + public function replace(string $needle, ?string $replace): void { - $this->link = str_replace($needle, $replace, $this->link); + if(!is_null($replace)) { + $this->link = str_replace($needle, $replace, $this->link); + } } } diff --git a/core/imageboard/image.php b/core/imageboard/image.php index bbfb72bf..0d834406 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -763,47 +763,8 @@ class Image public function parse_link_template(string $tmpl, int $n=0): string { - global $config; - - // don't bother hitting the database if it won't be used... - $tags = ""; - if (strpos($tmpl, '$tags') !== false) { // * stabs dynamically typed languages with a rusty spoon * - $tags = $this->get_tag_list(); - $tags = str_replace("/", "", $tags); - $tags = preg_replace("/^\.+/", "", $tags); - } - - $base_href = $config->get_string('base_href'); - $fname = $this->get_filename(); - $base_fname = strpos($fname, '.') ? substr($fname, 0, strrpos($fname, '.')) : $fname; - - $tmpl = str_replace('$id', $this->id, $tmpl); - $tmpl = str_replace('$hash_ab', substr($this->hash, 0, 2), $tmpl); - $tmpl = str_replace('$hash_cd', substr($this->hash, 2, 2), $tmpl); - $tmpl = str_replace('$hash', $this->hash, $tmpl); - $tmpl = str_replace('$tags', $tags, $tmpl); - $tmpl = str_replace('$base', $base_href, $tmpl); - $tmpl = str_replace('$ext', $this->ext, $tmpl); - if ($this->width && $this->height && $this->length) { - $s = ((int)($this->length / 100))/10; - $tmpl = str_replace('$size', "{$this->width}x{$this->height}, ${s}s", $tmpl); - } elseif ($this->width && $this->height) { - $tmpl = str_replace('$size', "{$this->width}x{$this->height}", $tmpl); - } elseif ($this->length) { - $s = ((int)($this->length / 100))/10; - $tmpl = str_replace('$size', "${s}s", $tmpl); - } - $tmpl = str_replace('$filesize', to_shorthand_int($this->filesize), $tmpl); - $tmpl = str_replace('$filename', $base_fname, $tmpl); - $tmpl = str_replace('$title', $config->get_string(SetupConfig::TITLE), $tmpl); - $tmpl = str_replace('$date', autodate($this->posted, false), $tmpl); - - // nothing seems to use this, sending the event out to 50 exts is a lot of overhead - if (!SPEED_HAX) { - $plte = send_event(new ParseLinkTemplateEvent($tmpl, $this)); - $tmpl = $plte->link; - } - + $plte = send_event(new ParseLinkTemplateEvent($tmpl, $this)); + $tmpl = $plte->link; return load_balance_url($tmpl, $this->hash, $n); } diff --git a/core/tests/util.test.php b/core/tests/util.test.php index e950747c..be002783 100644 --- a/core/tests/util.test.php +++ b/core/tests/util.test.php @@ -79,6 +79,5 @@ class UtilTest extends \PHPUnit\Framework\TestCase load_balance_url("https://{foo=10,bar=5,baz=5}.mycdn.com/$hash.$ext", $hash, 0), load_balance_url("https://{foo=10,bar=5,baz=5}.mycdn.com/$hash.$ext", $hash, 1) ); - } } diff --git a/ext/image/main.php b/ext/image/main.php index 7f60eb04..48b91696 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -250,6 +250,20 @@ class ImageIO extends Extension $event->panel->add_block($sb); } + public function onParseLinkTemplate(ParseLinkTemplateEvent $event) + { + $fname = $event->image->get_filename(); + $base_fname = strpos($fname, '.') ? substr($fname, 0, strrpos($fname, '.')) : $fname; + + $event->replace('$id', (string)$event->image->id); + $event->replace('$hash_ab', substr($event->image->hash, 0, 2)); + $event->replace('$hash_cd', substr($event->image->hash, 2, 2)); + $event->replace('$hash', $event->image->hash); + $event->replace('$filesize', to_shorthand_int($event->image->filesize)); + $event->replace('$filename', $base_fname); + $event->replace('$date', autodate($event->image->posted, false)); + } + private function send_file(int $image_id, string $type) { global $config; diff --git a/ext/media/main.php b/ext/media/main.php index d20667cd..34c34e2f 100644 --- a/ext/media/main.php +++ b/ext/media/main.php @@ -296,6 +296,21 @@ class Media extends Extension } } + public function onParseLinkTemplate(ParseLinkTemplateEvent $event) + { + if ($event->image->width && $event->image->height && $event->image->length) { + $s = ((int)($event->image->length / 100))/10; + $event->replace('$size', "{$event->image->width}x{$event->image->height}, ${s}s"); + } elseif ($event->image->width && $event->image->height) { + $event->replace('$size', "{$event->image->width}x{$event->image->height}"); + } elseif ($event->image->length) { + $s = ((int)($event->image->length / 100))/10; + $event->replace('$size', "${s}s"); + } + + $event->replace('$ext', $event->image->ext); + } + /** * Check Memory usage limits * diff --git a/ext/rule34/main.php b/ext/rule34/main.php index 6fd91636..32d7fd20 100644 --- a/ext/rule34/main.php +++ b/ext/rule34/main.php @@ -1,6 +1,9 @@ add_link("Board Config", make_link("setup")); } } + + public function onParseLinkTemplate(ParseLinkTemplateEvent $event) + { + global $config; + $event->replace('$base', $config->get_string('base_href')); + $event->replace('$title', $config->get_string(SetupConfig::TITLE)); + } } diff --git a/ext/tag_edit/main.php b/ext/tag_edit/main.php index 093e223a..df87e9a2 100644 --- a/ext/tag_edit/main.php +++ b/ext/tag_edit/main.php @@ -230,7 +230,6 @@ class TagEdit extends Extension $this->theme->display_mass_editor(); } - public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { if ($event->parent=="tags") { @@ -238,7 +237,6 @@ class TagEdit extends Extension } } - /** * When an alias is added, oldtag becomes inaccessible. */ @@ -270,6 +268,14 @@ class TagEdit extends Extension } } + public function onParseLinkTemplate(ParseLinkTemplateEvent $event) + { + $tags = $event->image->get_tag_list(); + $tags = str_replace("/", "", $tags); + $tags = preg_replace("/^\.+/", "", $tags); + $event->replace('$tags', $tags); + } + private function mass_tag_edit(string $search, string $replace) { global $database;