[link scan] new ext
This commit is contained in:
parent
ea1867f92d
commit
8e5c64e834
4 changed files with 121 additions and 0 deletions
17
ext/link_scan/info.php
Normal file
17
ext/link_scan/info.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shimmie2;
|
||||
|
||||
class LinkScanInfo extends ExtensionInfo
|
||||
{
|
||||
public const KEY = "link_scan";
|
||||
|
||||
public string $key = self::KEY;
|
||||
public string $name = "Link Scan";
|
||||
public string $url = self::SHIMMIE_URL;
|
||||
public array $authors = self::SHISH_AUTHOR;
|
||||
public string $license = self::LICENSE_GPLV2;
|
||||
public string $description = "Find posts that are referenced in a block of text";
|
||||
}
|
39
ext/link_scan/main.php
Normal file
39
ext/link_scan/main.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shimmie2;
|
||||
|
||||
class LinkScan extends Extension
|
||||
{
|
||||
/** @var LinkScanTheme */
|
||||
protected Themelet $theme;
|
||||
|
||||
public function onAdminBuilding(AdminBuildingEvent $event): void
|
||||
{
|
||||
$this->theme->display_form();
|
||||
}
|
||||
|
||||
public function onAdminAction(AdminActionEvent $event): void
|
||||
{
|
||||
global $page;
|
||||
if($event->action == "link_scan") {
|
||||
$text = $_POST['text'];
|
||||
$ids = [];
|
||||
|
||||
$matches = [];
|
||||
preg_match_all("/post\/view\/(\d+)/", $text, $matches);
|
||||
foreach($matches[1] as $match) {
|
||||
$ids[] = $match;
|
||||
}
|
||||
preg_match_all("/\b([0-9a-fA-F]{32})\b/", $text, $matches);
|
||||
foreach($matches[1] as $match) {
|
||||
$ids[] = Image::by_hash($match)->id;
|
||||
}
|
||||
|
||||
$event->redirect = false;
|
||||
$page->set_mode(PageMode::REDIRECT);
|
||||
$page->set_redirect(search_link(["id=".implode(",", $ids)]));
|
||||
}
|
||||
}
|
||||
}
|
40
ext/link_scan/test.php
Normal file
40
ext/link_scan/test.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shimmie2;
|
||||
|
||||
class LinkScanTest extends ShimmiePHPUnitTestCase
|
||||
{
|
||||
public function testScanPostView(): void
|
||||
{
|
||||
global $page;
|
||||
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "TeStCase");
|
||||
$image_id_2 = $this->post_image("tests/favicon.png", "TeStCase");
|
||||
|
||||
$_POST['text'] = "
|
||||
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
|
||||
";
|
||||
send_event(new AdminActionEvent("link_scan"));
|
||||
|
||||
$this->assertEquals(PageMode::REDIRECT, $page->mode);
|
||||
$this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect);
|
||||
}
|
||||
|
||||
public function testScanPostHash(): void
|
||||
{
|
||||
global $page;
|
||||
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "TeStCase");
|
||||
$image_id_2 = $this->post_image("tests/favicon.png", "TeStCase");
|
||||
|
||||
$_POST['text'] = "
|
||||
Look at http://example.com/_images/feb01bab5698a11dd87416724c7a89e3/foobar.jpg
|
||||
there is an image or search for e106ea2983e1b77f11e00c0c54e53805";
|
||||
send_event(new AdminActionEvent("link_scan"));
|
||||
|
||||
$this->assertEquals(PageMode::REDIRECT, $page->mode);
|
||||
$this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect);
|
||||
}
|
||||
}
|
25
ext/link_scan/theme.php
Normal file
25
ext/link_scan/theme.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shimmie2;
|
||||
|
||||
use function MicroHTML\{TEXTAREA,BR,TABLE,TR,TD};
|
||||
|
||||
class LinkScanTheme extends Themelet
|
||||
{
|
||||
public function display_form(): void
|
||||
{
|
||||
global $page;
|
||||
|
||||
$html = SHM_SIMPLE_FORM(
|
||||
"admin/link_scan",
|
||||
TABLE(
|
||||
["class" => "form"],
|
||||
TR(TD(TEXTAREA(["name" => 'text', "placeholder" => 'Paste text']))),
|
||||
TR(TD(SHM_SUBMIT('Find Posts'))),
|
||||
),
|
||||
);
|
||||
$page->add_block(new Block("Find Referenced Posts", $html));
|
||||
}
|
||||
}
|
Reference in a new issue