2022-08-16 14:23:53 +02:00
|
|
|
<?php
|
|
|
|
|
2022-08-17 15:39:53 +02:00
|
|
|
namespace BookStack\References\ModelResolvers;
|
2022-08-16 14:23:53 +02:00
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\App\Model;
|
2022-08-16 14:23:53 +02:00
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
|
|
|
|
class PageLinkModelResolver implements CrossLinkModelResolver
|
|
|
|
{
|
|
|
|
public function resolve(string $link): ?Model
|
|
|
|
{
|
2022-08-17 15:39:53 +02:00
|
|
|
$pattern = '/^' . preg_quote(url('/books'), '/') . '\/([\w-]+)' . '\/page\/' . '([\w-]+)' . '([#?\/]|$)/';
|
2022-08-16 14:23:53 +02:00
|
|
|
$matches = [];
|
|
|
|
$match = preg_match($pattern, $link, $matches);
|
|
|
|
if (!$match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$bookSlug = $matches[1];
|
|
|
|
$pageSlug = $matches[2];
|
|
|
|
|
|
|
|
/** @var ?Page $model */
|
2022-08-17 17:59:23 +02:00
|
|
|
$model = Page::query()->whereSlugs($bookSlug, $pageSlug)->first(['id']);
|
2022-08-16 14:23:53 +02:00
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
2022-08-29 18:46:41 +02:00
|
|
|
}
|