xref: /plugin/pagesicon/helper.php (revision 1d43dc323e38bb223bfb92d6539eb15bdb4bddbe)
1da933f89SLORTET<?php
2da933f89SLORTETif (!defined('DOKU_INC')) die();
3da933f89SLORTET
4437e13a7SLORTETclass helper_plugin_pagesicon extends DokuWiki_Plugin {
5b603bbe1SLORTET    private const BUNDLED_DEFAULT_IMAGE_RELATIVE_PATH = 'lib/plugins/pagesicon/images/default_image.png';
6b603bbe1SLORTET
7b603bbe1SLORTET    private function getBundledDefaultImagePath(): string {
8b603bbe1SLORTET        return DOKU_INC . self::BUNDLED_DEFAULT_IMAGE_RELATIVE_PATH;
974a9e763SLORTET    }
1074a9e763SLORTET
11b603bbe1SLORTET    private function getBundledDefaultImageUrl(): string {
1274a9e763SLORTET        $path = $this->getBundledDefaultImagePath();
1374a9e763SLORTET        if (!@file_exists($path)) return '';
1474a9e763SLORTET
1574a9e763SLORTET        $base = rtrim((string)DOKU_BASE, '/');
16b603bbe1SLORTET        $url = $base . '/' . self::BUNDLED_DEFAULT_IMAGE_RELATIVE_PATH;
1774a9e763SLORTET        $mtime = @filemtime($path);
1874a9e763SLORTET        return $this->appendVersionToUrl($url, $mtime ? (int)$mtime : 0);
1974a9e763SLORTET    }
2074a9e763SLORTET
21b603bbe1SLORTET    private function getConfiguredDefaultImageMediaID() {
2274a9e763SLORTET        $mediaID = cleanID((string)$this->getConf('default_image'));
2374a9e763SLORTET        if ($mediaID === '') return false;
2474a9e763SLORTET        if (!@file_exists(mediaFN($mediaID))) return false;
2574a9e763SLORTET        return $mediaID;
2674a9e763SLORTET    }
2774a9e763SLORTET
28b603bbe1SLORTET    private function getMediaMTime(string $mediaID): int {
29da933f89SLORTET        $mediaID = cleanID($mediaID);
30da933f89SLORTET        if ($mediaID === '') return 0;
31da933f89SLORTET        $file = mediaFN($mediaID);
32da933f89SLORTET        if (!@file_exists($file)) return 0;
33da933f89SLORTET        $mtime = @filemtime($file);
34da933f89SLORTET        return $mtime ? (int)$mtime : 0;
35da933f89SLORTET    }
36da933f89SLORTET
37b603bbe1SLORTET    private function appendVersionToUrl(string $url, int $mtime): string {
38da933f89SLORTET        if ($url === '' || $mtime <= 0) return $url;
39da933f89SLORTET        $sep = strpos($url, '?') === false ? '?' : '&';
40da933f89SLORTET        return $url . $sep . 'pi_ts=' . $mtime;
41da933f89SLORTET    }
42da933f89SLORTET
43b603bbe1SLORTET    /**
44b603bbe1SLORTET     * Added in version 2026-03-06.
45b603bbe1SLORTET     * Notifies consumers that an icon changed and triggers cache invalidation hooks.
46b603bbe1SLORTET     */
47b603bbe1SLORTET    public function notifyIconUpdated(string $targetPage, string $action = 'update', string $mediaID = ''): void {
48da933f89SLORTET        global $conf;
49da933f89SLORTET
50da933f89SLORTET        @io_saveFile($conf['cachedir'] . '/purgefile', time());
51da933f89SLORTET
52da933f89SLORTET        $data = [
53da933f89SLORTET            'target_page' => cleanID($targetPage),
54da933f89SLORTET            'action' => $action,
55da933f89SLORTET            'media_id' => cleanID($mediaID),
56da933f89SLORTET        ];
57da933f89SLORTET        \dokuwiki\Extension\Event::createAndTrigger('PLUGIN_PAGESICON_UPDATED', $data);
58da933f89SLORTET    }
59da933f89SLORTET
60b603bbe1SLORTET    /**
61b603bbe1SLORTET     * Added in version 2026-03-11.
62b603bbe1SLORTET     * Returns the configured filename templates for the requested icon variant.
63b603bbe1SLORTET     */
64b603bbe1SLORTET    public function getVariantTemplates(string $variant): array {
65b603bbe1SLORTET        $confKey = $variant === 'small' ? 'icon_thumbnail_name' : 'icon_name';
66b603bbe1SLORTET        $raw = (string)$this->getConf($confKey);
67b603bbe1SLORTET
68b603bbe1SLORTET        if (trim($raw) === '') {
69b603bbe1SLORTET            trigger_error('pagesicon: missing required configuration "' . $confKey . '"', E_USER_WARNING);
70b603bbe1SLORTET            return [];
71b603bbe1SLORTET        }
72b603bbe1SLORTET
73b603bbe1SLORTET        $templates = array_values(array_unique(array_filter(array_map('trim', explode(';', $raw)))));
74b603bbe1SLORTET        if (!$templates) {
75b603bbe1SLORTET            trigger_error('pagesicon: configuration "' . $confKey . '" does not contain any usable value', E_USER_WARNING);
76b603bbe1SLORTET        }
77b603bbe1SLORTET
78b603bbe1SLORTET        return $templates;
79b603bbe1SLORTET    }
80b603bbe1SLORTET
81b603bbe1SLORTET    /**
82b603bbe1SLORTET     * Added in version 2026-03-11.
83b603bbe1SLORTET     * Normalizes an icon filename candidate to its base media name without namespace or extension.
84b603bbe1SLORTET     */
85b603bbe1SLORTET    public function normalizeIconBaseName(string $name): string {
86b603bbe1SLORTET        $name = trim($name);
87b603bbe1SLORTET        if ($name === '') return '';
88b603bbe1SLORTET        $name = noNS($name);
89b603bbe1SLORTET        $name = preg_replace('/\.[a-z0-9]+$/i', '', $name);
90b603bbe1SLORTET        $name = cleanID($name);
91b603bbe1SLORTET        return str_replace(':', '', $name);
92b603bbe1SLORTET    }
93b603bbe1SLORTET
94b603bbe1SLORTET    /**
95b603bbe1SLORTET     * Added in version 2026-03-11.
96b603bbe1SLORTET     * Returns the allowed target base names for an upload, indexed by their normalized value.
97b603bbe1SLORTET     */
98b603bbe1SLORTET    public function getUploadNameChoices(string $targetPage, string $variant): array {
99b603bbe1SLORTET        $pageID = noNS($targetPage);
100b603bbe1SLORTET        $choices = [];
101b603bbe1SLORTET
102b603bbe1SLORTET        foreach ($this->getVariantTemplates($variant) as $tpl) {
103b603bbe1SLORTET            $resolved = str_replace('~pagename~', $pageID, $tpl);
104b603bbe1SLORTET            $base = $this->normalizeIconBaseName($resolved);
105b603bbe1SLORTET            if ($base === '') continue;
106b603bbe1SLORTET            $choices[$base] = $base . '.ext';
107b603bbe1SLORTET        }
108b603bbe1SLORTET
109b603bbe1SLORTET        return $choices;
110b603bbe1SLORTET    }
111b603bbe1SLORTET
112b603bbe1SLORTET    private function buildConfiguredCandidatesFromRaw(string $raw, string $namespace, string $pageID): array {
113da933f89SLORTET        $configured = [];
114da933f89SLORTET        $entries = array_filter(array_map('trim', explode(';', $raw)));
115da933f89SLORTET
116da933f89SLORTET        foreach ($entries as $entry) {
117da933f89SLORTET            $name = str_replace('~pagename~', $pageID, $entry);
118da933f89SLORTET            if ($name === '') continue;
119da933f89SLORTET
120da933f89SLORTET            if (strpos($name, ':') === false && $namespace !== '') {
121da933f89SLORTET                $configured[] = $namespace . ':' . $name;
122da933f89SLORTET            } else {
123da933f89SLORTET                $configured[] = ltrim($name, ':');
124da933f89SLORTET            }
125da933f89SLORTET        }
126da933f89SLORTET
127da933f89SLORTET        return array_values(array_unique($configured));
128da933f89SLORTET    }
129da933f89SLORTET
130b603bbe1SLORTET    private function buildConfiguredCandidates(string $namespace, string $pageID, string $sizeMode): array {
131da933f89SLORTET        $bigRaw = trim((string)$this->getConf('icon_name'));
132da933f89SLORTET        $smallRaw = trim((string)$this->getConf('icon_thumbnail_name'));
133da933f89SLORTET
134da933f89SLORTET        $big = $this->buildConfiguredCandidatesFromRaw($bigRaw, $namespace, $pageID);
135da933f89SLORTET        $small = $this->buildConfiguredCandidatesFromRaw($smallRaw, $namespace, $pageID);
136da933f89SLORTET
137da933f89SLORTET        if ($sizeMode === 'big') return $big;
138da933f89SLORTET        if ($sizeMode === 'small') return $small;
139da933f89SLORTET        if ($sizeMode === 'smallorbig') return array_values(array_unique(array_merge($small, $big)));
140da933f89SLORTET
141da933f89SLORTET        // Default: bigorsmall
142da933f89SLORTET        return array_values(array_unique(array_merge($big, $small)));
143da933f89SLORTET    }
144da933f89SLORTET
145b603bbe1SLORTET    private function normalizeSizeMode(string $size): string {
146da933f89SLORTET        $size = strtolower(trim($size));
147da933f89SLORTET        $allowed = ['big', 'small', 'bigorsmall', 'smallorbig'];
148da933f89SLORTET        if (in_array($size, $allowed, true)) return $size;
149da933f89SLORTET        return 'bigorsmall';
150da933f89SLORTET    }
151da933f89SLORTET
152b603bbe1SLORTET    /**
153b603bbe1SLORTET     * Added in version 2026-03-11.
154b603bbe1SLORTET     * Returns the configured list of allowed icon file extensions.
155b603bbe1SLORTET     */
156b603bbe1SLORTET    public function getConfiguredExtensions(): array {
157da933f89SLORTET        $raw = trim((string)$this->getConf('extensions'));
158b603bbe1SLORTET        if ($raw === '') {
159b603bbe1SLORTET            trigger_error('pagesicon: missing required configuration "extensions"', E_USER_WARNING);
160b603bbe1SLORTET            return [];
161b603bbe1SLORTET        }
162da933f89SLORTET
163da933f89SLORTET        $extensions = array_values(array_unique(array_filter(array_map(function ($ext) {
164da933f89SLORTET            return strtolower(ltrim(trim((string)$ext), '.'));
165da933f89SLORTET        }, explode(';', $raw)))));
166da933f89SLORTET
167b603bbe1SLORTET        if (!$extensions) {
168b603bbe1SLORTET            trigger_error('pagesicon: configuration "extensions" does not contain any usable value', E_USER_WARNING);
169da933f89SLORTET        }
170da933f89SLORTET
171b603bbe1SLORTET        return $extensions;
172b603bbe1SLORTET    }
173b603bbe1SLORTET
174b603bbe1SLORTET    private function hasKnownExtension(string $name, array $extensions): bool {
175da933f89SLORTET        $fileExt = strtolower((string)pathinfo($name, PATHINFO_EXTENSION));
176da933f89SLORTET        return $fileExt !== '' && in_array($fileExt, $extensions, true);
177da933f89SLORTET    }
178da933f89SLORTET
179a3ea184dSLORTET    private function getParentFallbackMode(): string {
180a3ea184dSLORTET        $mode = strtolower(trim((string)$this->getConf('parent_fallback')));
181a3ea184dSLORTET        if ($mode !== 'direct' && $mode !== 'first') return 'none';
182a3ea184dSLORTET        return $mode;
183a3ea184dSLORTET    }
184a3ea184dSLORTET
185a3ea184dSLORTET    private function resolveOwnPageIconId(string $namespace, string $pageID, string $sizeMode, array $extensions) {
18619821f1cSLORTET        $imageNames = $this->buildConfiguredCandidates($namespace, $pageID, $sizeMode);
187da933f89SLORTET
188da933f89SLORTET        foreach ($imageNames as $name) {
189da933f89SLORTET            if ($this->hasKnownExtension($name, $extensions)) {
190da933f89SLORTET                if (@file_exists(mediaFN($name))) return $name;
191da933f89SLORTET                continue;
192da933f89SLORTET            }
193da933f89SLORTET
194da933f89SLORTET            foreach ($extensions as $ext) {
195da933f89SLORTET                $path = $name . '.' . $ext;
196da933f89SLORTET                if (@file_exists(mediaFN($path))) return $path;
197da933f89SLORTET            }
198da933f89SLORTET        }
199da933f89SLORTET
200da933f89SLORTET        return false;
201da933f89SLORTET    }
202da933f89SLORTET
203a3ea184dSLORTET    private function resolveNamespacePageIconId(string $namespace, string $sizeMode, array $extensions) {
204a3ea184dSLORTET        global $conf;
205a3ea184dSLORTET
206a3ea184dSLORTET        $namespace = cleanID($namespace);
207a3ea184dSLORTET        if ($namespace === '') return false;
208a3ea184dSLORTET
209a3ea184dSLORTET        $parentNamespace = (string)(getNS($namespace) ?: '');
210a3ea184dSLORTET        $pageID = noNS($namespace);
211a3ea184dSLORTET
212a3ea184dSLORTET        $iconID = $this->resolveOwnPageIconId($parentNamespace, $pageID, $sizeMode, $extensions);
213a3ea184dSLORTET        if ($iconID) return $iconID;
214a3ea184dSLORTET
215a3ea184dSLORTET        $leafPageID = cleanID($namespace . ':' . $pageID);
216a3ea184dSLORTET        if ($leafPageID !== '' && page_exists($leafPageID)) {
217a3ea184dSLORTET            $iconID = $this->resolveOwnPageIconId($namespace, $pageID, $sizeMode, $extensions);
218a3ea184dSLORTET            if ($iconID) return $iconID;
219a3ea184dSLORTET        }
220a3ea184dSLORTET
221a3ea184dSLORTET        if (isset($conf['start'])) {
222a3ea184dSLORTET            $startId = cleanID((string)$conf['start']);
223a3ea184dSLORTET            if ($startId !== '') {
224a3ea184dSLORTET                $iconID = $this->resolveOwnPageIconId($namespace, $startId, $sizeMode, $extensions);
225a3ea184dSLORTET                if ($iconID) return $iconID;
226a3ea184dSLORTET            }
227a3ea184dSLORTET        }
228a3ea184dSLORTET
229a3ea184dSLORTET        return false;
230a3ea184dSLORTET    }
231a3ea184dSLORTET
232a3ea184dSLORTET    /**
233a3ea184dSLORTET     * Added in version 2026-03-09.
234a3ea184dSLORTET     * Resolves the icon media ID for a page, or false when no icon matches.
235a3ea184dSLORTET     * Replaces the older getPageImage() name.
236a3ea184dSLORTET     */
237a3ea184dSLORTET    public function getPageIconId(
238a3ea184dSLORTET        string $namespace,
239a3ea184dSLORTET        string $pageID,
240a3ea184dSLORTET        string $size = 'bigorsmall'
241a3ea184dSLORTET    )
242a3ea184dSLORTET    {
243a3ea184dSLORTET        $sizeMode = $this->normalizeSizeMode($size);
244a3ea184dSLORTET        $extensions = $this->getConfiguredExtensions();
245a3ea184dSLORTET        $iconID = $this->resolveOwnPageIconId($namespace, $pageID, $sizeMode, $extensions);
246a3ea184dSLORTET        if ($iconID) return $iconID;
247a3ea184dSLORTET
248a3ea184dSLORTET        $fallbackMode = $this->getParentFallbackMode();
249a3ea184dSLORTET        if ($fallbackMode === 'none') return false;
250a3ea184dSLORTET
251a3ea184dSLORTET        $currentNamespace = $namespace ?: '';
252a3ea184dSLORTET        while ($currentNamespace !== '') {
253a3ea184dSLORTET            $parentNamespace = (string)(getNS($currentNamespace) ?: '');
254a3ea184dSLORTET            $lookupNamespace = $parentNamespace !== '' ? $parentNamespace : $currentNamespace;
255a3ea184dSLORTET            $iconID = $this->resolveNamespacePageIconId($lookupNamespace, $sizeMode, $extensions);
256a3ea184dSLORTET            if ($iconID) return $iconID;
257a3ea184dSLORTET            if ($fallbackMode === 'direct' || $parentNamespace === '') break;
258a3ea184dSLORTET            $currentNamespace = $parentNamespace;
259a3ea184dSLORTET        }
260a3ea184dSLORTET
261a3ea184dSLORTET        return false;
262a3ea184dSLORTET    }
263a3ea184dSLORTET
264b603bbe1SLORTET    /**
265b603bbe1SLORTET     * Added in version 2026-03-06.
266b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
267b603bbe1SLORTET     * Use getPageIconId() instead.
268b603bbe1SLORTET     */
26974a9e763SLORTET    public function getPageImage(
27074a9e763SLORTET        string $namespace,
27174a9e763SLORTET        string $pageID,
27274a9e763SLORTET        string $size = 'bigorsmall',
27374a9e763SLORTET        bool $withDefault = false
27474a9e763SLORTET    ) {
27574a9e763SLORTET        return $this->getPageIconId($namespace, $pageID, $size);
27674a9e763SLORTET    }
27774a9e763SLORTET
278b603bbe1SLORTET    /**
279b603bbe1SLORTET     * Added in version 2026-03-06.
280b603bbe1SLORTET     * Returns the icon management URL for a page, or null when upload is not allowed.
281b603bbe1SLORTET     */
282b603bbe1SLORTET    public function getUploadIconPage(string $targetPage = '') {
283da933f89SLORTET        global $ID;
284da933f89SLORTET
285da933f89SLORTET        $targetPage = cleanID($targetPage);
286da933f89SLORTET        if ($targetPage === '') {
287da933f89SLORTET            $targetPage = cleanID(getNS((string)$ID));
288da933f89SLORTET        }
289da933f89SLORTET        if ($targetPage === '') {
290da933f89SLORTET            $targetPage = cleanID((string)$ID);
291da933f89SLORTET        }
292da933f89SLORTET        if ($targetPage === '') return null;
293da933f89SLORTET
294da933f89SLORTET        if (auth_quickaclcheck($targetPage) < AUTH_UPLOAD) {
295da933f89SLORTET            return null;
296da933f89SLORTET        }
297da933f89SLORTET
298da933f89SLORTET        return wl($targetPage, ['do' => 'pagesicon']);
299da933f89SLORTET    }
300da933f89SLORTET
301b603bbe1SLORTET    /**
302b603bbe1SLORTET     * Added in version 2026-03-09.
303b603bbe1SLORTET     * Resolves the icon media ID associated with a media file, or false when none matches.
304b603bbe1SLORTET     * Replaces the older getMediaImage() name.
305b603bbe1SLORTET     */
306b603bbe1SLORTET    public function getMediaIconId(string $mediaID, string $size = 'bigorsmall') {
307da933f89SLORTET        $mediaID = cleanID($mediaID);
308da933f89SLORTET        if ($mediaID === '') return false;
309da933f89SLORTET
310da933f89SLORTET        $namespace = getNS($mediaID);
311da933f89SLORTET        $filename = noNS($mediaID);
312da933f89SLORTET        $base = (string)pathinfo($filename, PATHINFO_FILENAME);
313da933f89SLORTET        $pageID = cleanID($base);
314da933f89SLORTET        if ($pageID === '') return false;
315da933f89SLORTET
31674a9e763SLORTET        return $this->getPageIconId($namespace, $pageID, $size);
317da933f89SLORTET    }
318da933f89SLORTET
319b603bbe1SLORTET    /**
320b603bbe1SLORTET     * Added in version 2026-03-06.
321b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
322b603bbe1SLORTET     * Use getMediaIconId() instead.
323b603bbe1SLORTET     */
324b603bbe1SLORTET    public function getMediaImage(string $mediaID, string $size = 'bigorsmall', bool $withDefault = false) {
32574a9e763SLORTET        return $this->getMediaIconId($mediaID, $size);
32674a9e763SLORTET    }
32774a9e763SLORTET
328b603bbe1SLORTET    private function matchesPageIconVariant(string $mediaID, string $namespace, string $pageID): bool {
329b603bbe1SLORTET        $bigIconID = $this->getPageIconId($namespace, $pageID, 'big');
330b603bbe1SLORTET        if ($bigIconID && cleanID((string)$bigIconID) === $mediaID) return true;
331b603bbe1SLORTET
332b603bbe1SLORTET        $smallIconID = $this->getPageIconId($namespace, $pageID, 'small');
333b603bbe1SLORTET        if ($smallIconID && cleanID((string)$smallIconID) === $mediaID) return true;
334b603bbe1SLORTET
335b603bbe1SLORTET        return false;
336b603bbe1SLORTET    }
337b603bbe1SLORTET
338b603bbe1SLORTET    /**
339b603bbe1SLORTET     * Added in version 2026-03-11.
340b603bbe1SLORTET     * Checks whether a media ID should be considered a page icon managed by the pagesicon plugin.
341b603bbe1SLORTET     */
342b603bbe1SLORTET    public function isPageIconMedia(string $mediaID): bool {
343b603bbe1SLORTET        global $conf;
344b603bbe1SLORTET
345b603bbe1SLORTET        $mediaID = cleanID($mediaID);
346b603bbe1SLORTET        if ($mediaID === '') return false;
347b603bbe1SLORTET
348b603bbe1SLORTET        $namespace = getNS($mediaID);
349b603bbe1SLORTET        $filename = noNS($mediaID);
350b603bbe1SLORTET        $basename = cleanID((string)pathinfo($filename, PATHINFO_FILENAME));
351b603bbe1SLORTET        if ($basename === '') return false;
352b603bbe1SLORTET
353b603bbe1SLORTET        // Case 1: this media is the big or small icon selected for a page with the same base name.
354b603bbe1SLORTET        $sameNamePageID = $namespace !== '' ? ($namespace . ':' . $basename) : $basename;
355b603bbe1SLORTET        if (page_exists($sameNamePageID)) {
356b603bbe1SLORTET            if ($this->matchesPageIconVariant($mediaID, $namespace, $basename)) return true;
357b603bbe1SLORTET        }
358b603bbe1SLORTET
359b603bbe1SLORTET        // Case 2: this media is the big or small icon selected for a page whose ID matches the namespace.
360b603bbe1SLORTET        if ($namespace !== '' && page_exists($namespace)) {
361b603bbe1SLORTET            $parentNamespace = getNS($namespace);
362b603bbe1SLORTET            $pageID = noNS($namespace);
363b603bbe1SLORTET            if ($this->matchesPageIconVariant($mediaID, $parentNamespace, $pageID)) return true;
364b603bbe1SLORTET        }
365b603bbe1SLORTET
366b603bbe1SLORTET        // Case 3: this media is the big or small icon selected for a page whose ID
367b603bbe1SLORTET        // matches the namespace leaf, for example "...:playground:playground".
368b603bbe1SLORTET        if ($namespace !== '') {
369b603bbe1SLORTET            $namespaceLeaf = noNS($namespace);
370b603bbe1SLORTET            $leafPageID = cleanID($namespace . ':' . $namespaceLeaf);
371b603bbe1SLORTET            if ($leafPageID !== '' && page_exists($leafPageID)) {
372b603bbe1SLORTET                if ($this->matchesPageIconVariant($mediaID, $namespace, $namespaceLeaf)) return true;
373b603bbe1SLORTET            }
374b603bbe1SLORTET        }
375b603bbe1SLORTET
376b603bbe1SLORTET        // Case 4: this media is the big or small icon selected for the namespace start page
377b603bbe1SLORTET        // (for example "...:start"), which often carries the visible page content.
378b603bbe1SLORTET        if ($namespace !== '' && isset($conf['start'])) {
379b603bbe1SLORTET            $startId = cleanID((string)$conf['start']);
380b603bbe1SLORTET            $startPage = $startId !== '' ? cleanID($namespace . ':' . $startId) : '';
381b603bbe1SLORTET            if ($startPage !== '' && page_exists($startPage)) {
382b603bbe1SLORTET                if ($this->matchesPageIconVariant($mediaID, $namespace, noNS($startPage))) return true;
383b603bbe1SLORTET            }
384b603bbe1SLORTET        }
385b603bbe1SLORTET
386b603bbe1SLORTET        return false;
387b603bbe1SLORTET    }
388b603bbe1SLORTET
389b603bbe1SLORTET    /**
390b603bbe1SLORTET     * Added in version 2026-03-09.
391b603bbe1SLORTET     * Returns the configured default icon URL, or the bundled fallback image when available.
392b603bbe1SLORTET     */
393*1d43dc32SLORTET    public function getDefaultIconUrl(array $params = ['w' => 55], ?int &$mtime = null) {
39474a9e763SLORTET        $mediaID = $this->getConfiguredDefaultImageMediaID();
39574a9e763SLORTET        if ($mediaID) {
39674a9e763SLORTET            $mtime = $this->getMediaMTime((string)$mediaID);
39774a9e763SLORTET            $url = (string)ml((string)$mediaID, $params);
39874a9e763SLORTET            if ($url === '') return false;
39974a9e763SLORTET            return $this->appendVersionToUrl($url, $mtime);
40074a9e763SLORTET        }
40174a9e763SLORTET
40274a9e763SLORTET        $mtime = 0;
40374a9e763SLORTET        $bundled = $this->getBundledDefaultImageUrl();
40474a9e763SLORTET        if ($bundled !== '') return $bundled;
40574a9e763SLORTET
40674a9e763SLORTET        return false;
40774a9e763SLORTET    }
40874a9e763SLORTET
409b603bbe1SLORTET    /**
410b603bbe1SLORTET     * Added in version 2026-03-09.
411b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
412b603bbe1SLORTET     * Use getDefaultIconUrl() instead.
413b603bbe1SLORTET     */
414*1d43dc32SLORTET    public function getDefaultImageIcon(array $params = ['w' => 55], ?int &$mtime = null) {
41574a9e763SLORTET        return $this->getDefaultIconUrl($params, $mtime);
41674a9e763SLORTET    }
41774a9e763SLORTET
418b603bbe1SLORTET    /**
419b603bbe1SLORTET     * Added in version 2026-03-09.
420b603bbe1SLORTET     * Returns a versioned icon URL for a page, or false when no icon matches.
421b603bbe1SLORTET     * Replaces the older getImageIcon() name.
422b603bbe1SLORTET     */
42374a9e763SLORTET    public function getPageIconUrl(
424da933f89SLORTET        string $namespace,
425da933f89SLORTET        string $pageID,
426da933f89SLORTET        string $size = 'bigorsmall',
427*1d43dc32SLORTET        array $params = ['w' => 55],
42874a9e763SLORTET        ?int &$mtime = null,
42974a9e763SLORTET        bool $withDefault = false
430da933f89SLORTET    ) {
43174a9e763SLORTET        $mediaID = $this->getPageIconId($namespace, $pageID, $size);
432da933f89SLORTET        if (!$mediaID) {
43374a9e763SLORTET            if ($withDefault) {
43474a9e763SLORTET                return $this->getDefaultIconUrl($params, $mtime);
43574a9e763SLORTET            }
436da933f89SLORTET            $mtime = 0;
437da933f89SLORTET            return false;
438da933f89SLORTET        }
439da933f89SLORTET
440da933f89SLORTET        $mtime = $this->getMediaMTime((string)$mediaID);
441da933f89SLORTET        $url = (string)ml((string)$mediaID, $params);
442da933f89SLORTET        if ($url === '') return false;
443da933f89SLORTET        return $this->appendVersionToUrl($url, $mtime);
444da933f89SLORTET    }
445da933f89SLORTET
446b603bbe1SLORTET    /**
447b603bbe1SLORTET     * Added in version 2026-03-06.
448b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
449b603bbe1SLORTET     * Use getPageIconUrl() instead.
450b603bbe1SLORTET     */
45174a9e763SLORTET    public function getImageIcon(
45274a9e763SLORTET        string $namespace,
45374a9e763SLORTET        string $pageID,
45474a9e763SLORTET        string $size = 'bigorsmall',
455*1d43dc32SLORTET        array $params = ['w' => 55],
45674a9e763SLORTET        ?int &$mtime = null,
45774a9e763SLORTET        bool $withDefault = false
45874a9e763SLORTET    ) {
45974a9e763SLORTET        return $this->getPageIconUrl($namespace, $pageID, $size, $params, $mtime, $withDefault);
46074a9e763SLORTET    }
46174a9e763SLORTET
462b603bbe1SLORTET    /**
463b603bbe1SLORTET     * Added in version 2026-03-09.
464b603bbe1SLORTET     * Returns a versioned icon URL for a media file, or false when no icon matches.
465b603bbe1SLORTET     * Replaces the older getMediaIcon() name.
466b603bbe1SLORTET     */
46774a9e763SLORTET    public function getMediaIconUrl(
468da933f89SLORTET        string $mediaID,
469da933f89SLORTET        string $size = 'bigorsmall',
470*1d43dc32SLORTET        array $params = ['w' => 55],
47174a9e763SLORTET        ?int &$mtime = null,
47274a9e763SLORTET        bool $withDefault = false
473da933f89SLORTET    ) {
47474a9e763SLORTET        $iconMediaID = $this->getMediaIconId($mediaID, $size);
475da933f89SLORTET        if (!$iconMediaID) {
47674a9e763SLORTET            if ($withDefault) {
47774a9e763SLORTET                return $this->getDefaultIconUrl($params, $mtime);
47874a9e763SLORTET            }
479da933f89SLORTET            $mtime = 0;
480da933f89SLORTET            return false;
481da933f89SLORTET        }
482da933f89SLORTET
483da933f89SLORTET        $mtime = $this->getMediaMTime((string)$iconMediaID);
484da933f89SLORTET        $url = (string)ml((string)$iconMediaID, $params);
485da933f89SLORTET        if ($url === '') return false;
486da933f89SLORTET        return $this->appendVersionToUrl($url, $mtime);
487da933f89SLORTET    }
488da933f89SLORTET
489b603bbe1SLORTET    /**
490b603bbe1SLORTET     * Added in version 2026-03-06.
491b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
492b603bbe1SLORTET     * Use getMediaIconUrl() instead.
493b603bbe1SLORTET     */
49474a9e763SLORTET    public function getMediaIcon(
49574a9e763SLORTET        string $mediaID,
49674a9e763SLORTET        string $size = 'bigorsmall',
497*1d43dc32SLORTET        array $params = ['w' => 55],
49874a9e763SLORTET        ?int &$mtime = null,
49974a9e763SLORTET        bool $withDefault = false
50074a9e763SLORTET    ) {
50174a9e763SLORTET        return $this->getMediaIconUrl($mediaID, $size, $params, $mtime, $withDefault);
50274a9e763SLORTET    }
50374a9e763SLORTET
504b603bbe1SLORTET    /**
505b603bbe1SLORTET     * Added in version 2026-03-06.
506b603bbe1SLORTET     * Returns the icon management URL associated with a media file, or null when unavailable.
507b603bbe1SLORTET     */
508b603bbe1SLORTET    public function getUploadMediaIconPage(string $mediaID = '') {
509da933f89SLORTET        $mediaID = cleanID($mediaID);
510da933f89SLORTET        if ($mediaID === '') return null;
511da933f89SLORTET
512da933f89SLORTET        $namespace = getNS($mediaID);
513da933f89SLORTET        $filename = noNS($mediaID);
514da933f89SLORTET        $base = (string)pathinfo($filename, PATHINFO_FILENAME);
515da933f89SLORTET        $targetPage = cleanID($namespace !== '' ? ($namespace . ':' . $base) : $base);
516da933f89SLORTET        if ($targetPage === '') return null;
517da933f89SLORTET
518da933f89SLORTET        return $this->getUploadIconPage($targetPage);
519da933f89SLORTET    }
520da933f89SLORTET}
521