Extract Pagelink functionality
This commit is contained in:
162
src/PageLinkTrait.php
Normal file
162
src/PageLinkTrait.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace dokuwiki\plugin\luxtools;
|
||||
|
||||
/**
|
||||
* Trait for pagelink-related functionality shared across syntax handlers.
|
||||
*
|
||||
* Provides methods for:
|
||||
* - Detecting blobs alias paths
|
||||
* - Resolving the blobs root folder from page metadata
|
||||
* - Rendering "page not linked" messages
|
||||
* - Building path configs with blobs alias support
|
||||
*
|
||||
* Requirements for using classes:
|
||||
* - Must have getConf() method (from SyntaxPlugin)
|
||||
* - Must have getLang() method (from SyntaxPlugin)
|
||||
*/
|
||||
trait PageLinkTrait
|
||||
{
|
||||
/**
|
||||
* Check if the given path uses the blobs alias.
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @param \Doku_Renderer $renderer
|
||||
*/
|
||||
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).
|
||||
*
|
||||
* @return string The UUID or empty string
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* Results are cached per page ID within a single request.
|
||||
*
|
||||
* @return string The linked folder path or empty string if not linked
|
||||
*/
|
||||
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];
|
||||
}
|
||||
|
||||
$depth = (int)$this->getConf('pagelink_search_depth');
|
||||
if ($depth < 0) $depth = 0;
|
||||
|
||||
$pageLink = new PageLink((string)$this->getConf('paths'), $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a path config string with the blobs alias appended (if available).
|
||||
*
|
||||
* @param string|null $blobsRoot The blobs root folder (or null to auto-resolve)
|
||||
* @return string The path config string
|
||||
*/
|
||||
protected function buildPathConfigWithBlobs(?string $blobsRoot = null): string
|
||||
{
|
||||
$pathConfig = (string)$this->getConf('paths');
|
||||
|
||||
if ($blobsRoot === null) {
|
||||
$blobsRoot = $this->resolveBlobsRoot();
|
||||
}
|
||||
|
||||
if ($blobsRoot !== '') {
|
||||
$pathConfig = rtrim($pathConfig) . "\n" . $blobsRoot . "\nA> blobs";
|
||||
}
|
||||
|
||||
return $pathConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Path helper with blobs alias support.
|
||||
*
|
||||
* @param string|null $blobsRoot The blobs root folder (or null to auto-resolve)
|
||||
* @return Path
|
||||
*/
|
||||
protected function createPathHelperWithBlobs(?string $blobsRoot = null): Path
|
||||
{
|
||||
return new Path($this->buildPathConfigWithBlobs($blobsRoot));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Path helper using only the base paths config (no blobs alias).
|
||||
*
|
||||
* @return Path
|
||||
*/
|
||||
protected function createPathHelper(): Path
|
||||
{
|
||||
return new Path((string)$this->getConf('paths'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user