Unlinking

This commit is contained in:
2026-02-02 21:55:35 +01:00
parent e1102d9f06
commit 47a8bfa50a
3 changed files with 160 additions and 8 deletions

View File

@@ -87,6 +87,46 @@ class PageLink
return (bool)p_set_metadata($pageId, [self::META_KEY => $uuid]);
}
/**
* Remove the pagelink UUID from page metadata.
*/
public function removePageUuid(string $pageId): bool
{
if ($pageId === '') return false;
if (!function_exists('p_set_metadata')) return false;
return (bool)p_set_metadata($pageId, [self::META_KEY => '']);
}
/**
* Unlink a page: remove UUID, delete linked .pagelink file if present, and clear cache.
*
* @param string $pageId
* @return array{ok: bool, uuid: string|null, folder: string|null}
*/
public function unlinkPage(string $pageId): array
{
$uuid = $this->getPageUuid($pageId);
if ($uuid === null) {
return ['ok' => true, 'uuid' => null, 'folder' => null];
}
$linkInfo = $this->resolveUuid($uuid);
$folder = $linkInfo['folder'] ?? null;
if (is_string($folder) && $folder !== '') {
$file = rtrim($folder, '/\\') . '/.pagelink';
if (is_file($file) && !is_link($file)) {
@unlink($file);
}
}
$this->removeCacheEntry($uuid);
$this->removePageUuid($pageId);
return ['ok' => true, 'uuid' => $uuid, 'folder' => is_string($folder) ? $folder : null];
}
/**
* Resolve a pagelink UUID to a linked folder (if any).
*
@@ -177,6 +217,21 @@ class PageLink
$this->cacheDirty = false;
}
/**
* Remove a specific UUID from cache.
*/
public function removeCacheEntry(string $uuid): void
{
$uuid = self::normalizeUuid($uuid);
if ($uuid === null) return;
$cache = $this->loadCache();
if (!isset($cache[$uuid])) return;
unset($cache[$uuid]);
$this->writeCache($cache);
}
/**
* Get cache file path for pagelink mappings.
*/