Spread ParseLinkTemplate work across relevant extensions

This commit is contained in:
Shish 2020-02-09 19:22:25 +00:00
parent 41a205d24a
commit 3a57817fc2
9 changed files with 54 additions and 48 deletions

View File

@ -34,7 +34,6 @@ Notable behaviour changes:
- We only show the first 500 pages of results for any query, except for - We only show the first 500 pages of results for any query, except for
the most simple (no tags, or one positive tag) the most simple (no tags, or one positive tag)
- We only ever show the first 5,000 results for complex queries - 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 - Only comments from the past 24 hours show up in /comment/list
- Web crawlers are blocked from creating too many nonsense searches - Web crawlers are blocked from creating too many nonsense searches
- The first 10 pages in the index get extra caching - The first 10 pages in the index get extra caching

View File

@ -135,8 +135,10 @@ class ParseLinkTemplateEvent extends Event
$this->image = $image; $this->image = $image;
} }
public function replace(string $needle, string $replace): void public function replace(string $needle, ?string $replace): void
{ {
if(!is_null($replace)) {
$this->link = str_replace($needle, $replace, $this->link); $this->link = str_replace($needle, $replace, $this->link);
} }
} }
}

View File

@ -763,47 +763,8 @@ class Image
public function parse_link_template(string $tmpl, int $n=0): string 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)); $plte = send_event(new ParseLinkTemplateEvent($tmpl, $this));
$tmpl = $plte->link; $tmpl = $plte->link;
}
return load_balance_url($tmpl, $this->hash, $n); return load_balance_url($tmpl, $this->hash, $n);
} }

View File

@ -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, 0),
load_balance_url("https://{foo=10,bar=5,baz=5}.mycdn.com/$hash.$ext", $hash, 1) load_balance_url("https://{foo=10,bar=5,baz=5}.mycdn.com/$hash.$ext", $hash, 1)
); );
} }
} }

View File

@ -250,6 +250,20 @@ class ImageIO extends Extension
$event->panel->add_block($sb); $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) private function send_file(int $image_id, string $type)
{ {
global $config; global $config;

View File

@ -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 * Check Memory usage limits
* *

View File

@ -1,6 +1,9 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
use function MicroHTML\{TR,TH,TD,A}; use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;
use function MicroHTML\A;
if ( // kill these glitched requests immediately if ( // kill these glitched requests immediately
!empty($_SERVER["REQUEST_URI"]) !empty($_SERVER["REQUEST_URI"])

View File

@ -446,4 +446,11 @@ class Setup extends Extension
$event->add_link("Board Config", make_link("setup")); $event->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));
}
} }

View File

@ -230,7 +230,6 @@ class TagEdit extends Extension
$this->theme->display_mass_editor(); $this->theme->display_mass_editor();
} }
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{ {
if ($event->parent=="tags") { if ($event->parent=="tags") {
@ -238,7 +237,6 @@ class TagEdit extends Extension
} }
} }
/** /**
* When an alias is added, oldtag becomes inaccessible. * 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) private function mass_tag_edit(string $search, string $replace)
{ {
global $database; global $database;