From 04471a287520ed37b18d7310c353b21d9ab94ca5 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 16 Jan 2024 12:47:32 +0000 Subject: [PATCH] [link scan] handle missing posts --- ext/link_scan/main.php | 10 ++++++++-- ext/link_scan/test.php | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ext/link_scan/main.php b/ext/link_scan/main.php index 872221ca..00f1ecc0 100644 --- a/ext/link_scan/main.php +++ b/ext/link_scan/main.php @@ -32,11 +32,17 @@ class LinkScan extends Extension $matches = []; preg_match_all("/post\/view\/(\d+)/", $text, $matches); foreach($matches[1] as $match) { - $ids[] = $match; + $img = Image::by_id((int)$match); + if ($img) { + $ids[] = $img->id; + } } preg_match_all("/\b([0-9a-fA-F]{32})\b/", $text, $matches); foreach($matches[1] as $match) { - $ids[] = Image::by_hash($match)->id; + $img = Image::by_hash($match); + if ($img) { + $ids[] = $img->id; + } } return array_unique($ids); } diff --git a/ext/link_scan/test.php b/ext/link_scan/test.php index 435f1c45..9fe8a785 100644 --- a/ext/link_scan/test.php +++ b/ext/link_scan/test.php @@ -16,6 +16,8 @@ class LinkScanTest extends ShimmiePHPUnitTestCase Look at http://example.com/post/view/{$image_id_1} there is an image http://example.com/post/view/{$image_id_2} is another one + + But there is no http://example.com/post/view/65432 "; $page = $this->get_page("post/list", ["search" => $text]); @@ -31,7 +33,8 @@ class LinkScanTest extends ShimmiePHPUnitTestCase $text = " Look at http://example.com/_images/feb01bab5698a11dd87416724c7a89e3/foobar.jpg - there is an image or search for e106ea2983e1b77f11e00c0c54e53805"; + there is an image or search for e106ea2983e1b77f11e00c0c54e53805 but one that + doesn't exist is e106ea2983e1b77f11e00c0c54e50000 o.o"; $page = $this->get_page("post/list", ["search" => $text]); $this->assertEquals(PageMode::REDIRECT, $page->mode);