Extract Pagelink functionality
This commit is contained in:
@@ -4,7 +4,7 @@ use dokuwiki\Extension\SyntaxPlugin;
|
||||
use dokuwiki\plugin\luxtools\Crawler;
|
||||
use dokuwiki\plugin\luxtools\Output;
|
||||
use dokuwiki\plugin\luxtools\Path;
|
||||
use dokuwiki\plugin\luxtools\PageLink;
|
||||
use dokuwiki\plugin\luxtools\PageLinkTrait;
|
||||
|
||||
require_once(__DIR__ . '/../autoload.php');
|
||||
|
||||
@@ -16,6 +16,7 @@ require_once(__DIR__ . '/../autoload.php');
|
||||
if (!class_exists('syntax_plugin_luxtools_abstract', false)) {
|
||||
abstract class syntax_plugin_luxtools_abstract extends SyntaxPlugin
|
||||
{
|
||||
use PageLinkTrait;
|
||||
/**
|
||||
* Returns the syntax keyword (e.g., 'files', 'directory', 'images').
|
||||
* Used for pattern matching and plugin registration.
|
||||
@@ -209,16 +210,12 @@ abstract class syntax_plugin_luxtools_abstract extends SyntaxPlugin
|
||||
protected function getPathInfoSafe(string $basePath, \Doku_Renderer $renderer)
|
||||
{
|
||||
try {
|
||||
$pathConfig = (string)$this->getConf('paths');
|
||||
$blobsRoot = $this->resolveBlobsRoot();
|
||||
if ($blobsRoot === '' && $this->isBlobsPath($basePath)) {
|
||||
$this->renderPageNotLinked($renderer);
|
||||
return false;
|
||||
}
|
||||
if ($blobsRoot !== '') {
|
||||
$pathConfig = rtrim($pathConfig) . "\n" . $blobsRoot . "\nA> blobs";
|
||||
}
|
||||
$pathHelper = new Path($pathConfig);
|
||||
$pathHelper = $this->createPathHelperWithBlobs($blobsRoot);
|
||||
return $pathHelper->getPathInfo($basePath);
|
||||
} catch (\Exception $e) {
|
||||
$this->renderError($renderer, 'error_outsidejail');
|
||||
@@ -226,99 +223,6 @@ abstract class syntax_plugin_luxtools_abstract extends SyntaxPlugin
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given path uses the blobs alias.
|
||||
*/
|
||||
protected function isBlobsPath(string $path): bool
|
||||
{
|
||||
$trimmed = ltrim($path, '/');
|
||||
return preg_match('/^blobs(\/|$)/', $trimmed) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the "Page not linked" message with copy ID affordance.
|
||||
*/
|
||||
protected function renderPageNotLinked(\Doku_Renderer $renderer): void
|
||||
{
|
||||
$uuid = $this->getPageUuidSafe();
|
||||
$text = (string)$this->getLang('pagelink_unlinked');
|
||||
|
||||
if ($renderer instanceof \Doku_Renderer_xhtml) {
|
||||
$renderer->doc .= '<a href="#" class="luxtools-pagelink-copy" data-luxtools-pagelink-copy="1"'
|
||||
. ' data-uuid="' . hsc($uuid) . '"'
|
||||
. '>' . hsc($text) . '</a>';
|
||||
return;
|
||||
}
|
||||
|
||||
$renderer->cdata('[n/a: ' . $text . ']');
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the current page UUID (if any).
|
||||
*/
|
||||
protected function getPageUuidSafe(): string
|
||||
{
|
||||
global $ID;
|
||||
$pageId = is_string($ID) ? $ID : '';
|
||||
if ($pageId === '') return '';
|
||||
|
||||
if (function_exists('cleanID')) {
|
||||
$pageId = (string)cleanID($pageId);
|
||||
}
|
||||
if ($pageId === '') return '';
|
||||
|
||||
$depth = (int)$this->getConf('pagelink_search_depth');
|
||||
if ($depth < 0) $depth = 0;
|
||||
|
||||
$pageLink = new PageLink((string)$this->getConf('paths'), $depth);
|
||||
$uuid = $pageLink->getPageUuid($pageId);
|
||||
return $uuid ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the current page's pagelink folder for the blobs alias.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function resolveBlobsRoot(): string
|
||||
{
|
||||
static $cached = [];
|
||||
|
||||
global $ID;
|
||||
$pageId = is_string($ID) ? $ID : '';
|
||||
if ($pageId === '') return '';
|
||||
|
||||
if (function_exists('cleanID')) {
|
||||
$pageId = (string)cleanID($pageId);
|
||||
}
|
||||
if ($pageId === '') return '';
|
||||
|
||||
if (isset($cached[$pageId])) {
|
||||
return (string)$cached[$pageId];
|
||||
}
|
||||
|
||||
$pathConfig = (string)$this->getConf('paths');
|
||||
$depth = (int)$this->getConf('pagelink_search_depth');
|
||||
if ($depth < 0) $depth = 0;
|
||||
|
||||
$pageLink = new PageLink($pathConfig, $depth);
|
||||
$uuid = $pageLink->getPageUuid($pageId);
|
||||
if ($uuid === null) {
|
||||
$cached[$pageId] = '';
|
||||
return '';
|
||||
}
|
||||
|
||||
$linkInfo = $pageLink->resolveUuid($uuid);
|
||||
$folder = $linkInfo['folder'] ?? '';
|
||||
if (!is_string($folder) || $folder === '') {
|
||||
$cached[$pageId] = '';
|
||||
return '';
|
||||
}
|
||||
|
||||
$cached[$pageId] = $folder;
|
||||
return $folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and configure a Crawler instance.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user