xref: /plugin/pagesicon/helper.php (revision 437e13a7f4b0e352780dcdc554e067446a3b6187)
1da933f89SLORTET<?php
2da933f89SLORTETif (!defined('DOKU_INC')) die();
3da933f89SLORTET
4*437e13a7SLORTETclass 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
179b603bbe1SLORTET    /**
180b603bbe1SLORTET     * Added in version 2026-03-09.
181b603bbe1SLORTET     * Resolves the icon media ID for a page, or false when no icon matches.
182b603bbe1SLORTET     * Replaces the older getPageImage() name.
183b603bbe1SLORTET     */
18474a9e763SLORTET    public function getPageIconId(
18574a9e763SLORTET        string $namespace,
18674a9e763SLORTET        string $pageID,
18774a9e763SLORTET        string $size = 'bigorsmall'
18874a9e763SLORTET    )
189da933f89SLORTET    {
190da933f89SLORTET        $sizeMode = $this->normalizeSizeMode($size);
191b603bbe1SLORTET        $extensions = $this->getConfiguredExtensions();
192da933f89SLORTET        $namespace = $namespace ?: '';
193da933f89SLORTET        $pageBase = $namespace ? ($namespace . ':' . $pageID) : $pageID;
194da933f89SLORTET        $nsBase = $namespace ? ($namespace . ':') : '';
195da933f89SLORTET
196da933f89SLORTET        $genericBig = [
197da933f89SLORTET            $pageBase,
198da933f89SLORTET            $pageBase . ':logo',
199da933f89SLORTET            $nsBase . 'logo',
200da933f89SLORTET        ];
201da933f89SLORTET        $genericSmall = [
202da933f89SLORTET            $pageBase . ':thumbnail',
203da933f89SLORTET            $nsBase . 'thumbnail',
204da933f89SLORTET        ];
205da933f89SLORTET
206da933f89SLORTET        if ($sizeMode === 'big') {
207da933f89SLORTET            $generic = $genericBig;
208da933f89SLORTET        } elseif ($sizeMode === 'small') {
209da933f89SLORTET            $generic = $genericSmall;
210da933f89SLORTET        } elseif ($sizeMode === 'smallorbig') {
211da933f89SLORTET            $generic = array_merge($genericSmall, $genericBig);
212da933f89SLORTET        } else {
213da933f89SLORTET            $generic = array_merge($genericBig, $genericSmall);
214da933f89SLORTET        }
215da933f89SLORTET
216da933f89SLORTET        $imageNames = array_merge($this->buildConfiguredCandidates($namespace, $pageID, $sizeMode), $generic);
217da933f89SLORTET
218da933f89SLORTET        foreach ($imageNames as $name) {
219da933f89SLORTET            if ($this->hasKnownExtension($name, $extensions)) {
220da933f89SLORTET                if (@file_exists(mediaFN($name))) return $name;
221da933f89SLORTET                continue;
222da933f89SLORTET            }
223da933f89SLORTET
224da933f89SLORTET            foreach ($extensions as $ext) {
225da933f89SLORTET                $path = $name . '.' . $ext;
226da933f89SLORTET                if (@file_exists(mediaFN($path))) return $path;
227da933f89SLORTET            }
228da933f89SLORTET        }
229da933f89SLORTET
230da933f89SLORTET        return false;
231da933f89SLORTET    }
232da933f89SLORTET
233b603bbe1SLORTET    /**
234b603bbe1SLORTET     * Added in version 2026-03-06.
235b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
236b603bbe1SLORTET     * Use getPageIconId() instead.
237b603bbe1SLORTET     */
23874a9e763SLORTET    public function getPageImage(
23974a9e763SLORTET        string $namespace,
24074a9e763SLORTET        string $pageID,
24174a9e763SLORTET        string $size = 'bigorsmall',
24274a9e763SLORTET        bool $withDefault = false
24374a9e763SLORTET    ) {
24474a9e763SLORTET        return $this->getPageIconId($namespace, $pageID, $size);
24574a9e763SLORTET    }
24674a9e763SLORTET
247b603bbe1SLORTET    /**
248b603bbe1SLORTET     * Added in version 2026-03-06.
249b603bbe1SLORTET     * Returns the icon management URL for a page, or null when upload is not allowed.
250b603bbe1SLORTET     */
251b603bbe1SLORTET    public function getUploadIconPage(string $targetPage = '') {
252da933f89SLORTET        global $ID;
253da933f89SLORTET
254da933f89SLORTET        $targetPage = cleanID($targetPage);
255da933f89SLORTET        if ($targetPage === '') {
256da933f89SLORTET            $targetPage = cleanID(getNS((string)$ID));
257da933f89SLORTET        }
258da933f89SLORTET        if ($targetPage === '') {
259da933f89SLORTET            $targetPage = cleanID((string)$ID);
260da933f89SLORTET        }
261da933f89SLORTET        if ($targetPage === '') return null;
262da933f89SLORTET
263da933f89SLORTET        if (auth_quickaclcheck($targetPage) < AUTH_UPLOAD) {
264da933f89SLORTET            return null;
265da933f89SLORTET        }
266da933f89SLORTET
267da933f89SLORTET        return wl($targetPage, ['do' => 'pagesicon']);
268da933f89SLORTET    }
269da933f89SLORTET
270b603bbe1SLORTET    /**
271b603bbe1SLORTET     * Added in version 2026-03-09.
272b603bbe1SLORTET     * Resolves the icon media ID associated with a media file, or false when none matches.
273b603bbe1SLORTET     * Replaces the older getMediaImage() name.
274b603bbe1SLORTET     */
275b603bbe1SLORTET    public function getMediaIconId(string $mediaID, string $size = 'bigorsmall') {
276da933f89SLORTET        $mediaID = cleanID($mediaID);
277da933f89SLORTET        if ($mediaID === '') return false;
278da933f89SLORTET
279da933f89SLORTET        $namespace = getNS($mediaID);
280da933f89SLORTET        $filename = noNS($mediaID);
281da933f89SLORTET        $base = (string)pathinfo($filename, PATHINFO_FILENAME);
282da933f89SLORTET        $pageID = cleanID($base);
283da933f89SLORTET        if ($pageID === '') return false;
284da933f89SLORTET
28574a9e763SLORTET        return $this->getPageIconId($namespace, $pageID, $size);
286da933f89SLORTET    }
287da933f89SLORTET
288b603bbe1SLORTET    /**
289b603bbe1SLORTET     * Added in version 2026-03-06.
290b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
291b603bbe1SLORTET     * Use getMediaIconId() instead.
292b603bbe1SLORTET     */
293b603bbe1SLORTET    public function getMediaImage(string $mediaID, string $size = 'bigorsmall', bool $withDefault = false) {
29474a9e763SLORTET        return $this->getMediaIconId($mediaID, $size);
29574a9e763SLORTET    }
29674a9e763SLORTET
297b603bbe1SLORTET    private function matchesPageIconVariant(string $mediaID, string $namespace, string $pageID): bool {
298b603bbe1SLORTET        $bigIconID = $this->getPageIconId($namespace, $pageID, 'big');
299b603bbe1SLORTET        if ($bigIconID && cleanID((string)$bigIconID) === $mediaID) return true;
300b603bbe1SLORTET
301b603bbe1SLORTET        $smallIconID = $this->getPageIconId($namespace, $pageID, 'small');
302b603bbe1SLORTET        if ($smallIconID && cleanID((string)$smallIconID) === $mediaID) return true;
303b603bbe1SLORTET
304b603bbe1SLORTET        return false;
305b603bbe1SLORTET    }
306b603bbe1SLORTET
307b603bbe1SLORTET    /**
308b603bbe1SLORTET     * Added in version 2026-03-11.
309b603bbe1SLORTET     * Checks whether a media ID should be considered a page icon managed by the pagesicon plugin.
310b603bbe1SLORTET     */
311b603bbe1SLORTET    public function isPageIconMedia(string $mediaID): bool {
312b603bbe1SLORTET        global $conf;
313b603bbe1SLORTET
314b603bbe1SLORTET        $mediaID = cleanID($mediaID);
315b603bbe1SLORTET        if ($mediaID === '') return false;
316b603bbe1SLORTET
317b603bbe1SLORTET        $namespace = getNS($mediaID);
318b603bbe1SLORTET        $filename = noNS($mediaID);
319b603bbe1SLORTET        $basename = cleanID((string)pathinfo($filename, PATHINFO_FILENAME));
320b603bbe1SLORTET        if ($basename === '') return false;
321b603bbe1SLORTET
322b603bbe1SLORTET        // Case 1: this media is the big or small icon selected for a page with the same base name.
323b603bbe1SLORTET        $sameNamePageID = $namespace !== '' ? ($namespace . ':' . $basename) : $basename;
324b603bbe1SLORTET        if (page_exists($sameNamePageID)) {
325b603bbe1SLORTET            if ($this->matchesPageIconVariant($mediaID, $namespace, $basename)) return true;
326b603bbe1SLORTET        }
327b603bbe1SLORTET
328b603bbe1SLORTET        // Case 2: this media is the big or small icon selected for a page whose ID matches the namespace.
329b603bbe1SLORTET        if ($namespace !== '' && page_exists($namespace)) {
330b603bbe1SLORTET            $parentNamespace = getNS($namespace);
331b603bbe1SLORTET            $pageID = noNS($namespace);
332b603bbe1SLORTET            if ($this->matchesPageIconVariant($mediaID, $parentNamespace, $pageID)) return true;
333b603bbe1SLORTET        }
334b603bbe1SLORTET
335b603bbe1SLORTET        // Case 3: this media is the big or small icon selected for a page whose ID
336b603bbe1SLORTET        // matches the namespace leaf, for example "...:playground:playground".
337b603bbe1SLORTET        if ($namespace !== '') {
338b603bbe1SLORTET            $namespaceLeaf = noNS($namespace);
339b603bbe1SLORTET            $leafPageID = cleanID($namespace . ':' . $namespaceLeaf);
340b603bbe1SLORTET            if ($leafPageID !== '' && page_exists($leafPageID)) {
341b603bbe1SLORTET                if ($this->matchesPageIconVariant($mediaID, $namespace, $namespaceLeaf)) return true;
342b603bbe1SLORTET            }
343b603bbe1SLORTET        }
344b603bbe1SLORTET
345b603bbe1SLORTET        // Case 4: this media is the big or small icon selected for the namespace start page
346b603bbe1SLORTET        // (for example "...:start"), which often carries the visible page content.
347b603bbe1SLORTET        if ($namespace !== '' && isset($conf['start'])) {
348b603bbe1SLORTET            $startId = cleanID((string)$conf['start']);
349b603bbe1SLORTET            $startPage = $startId !== '' ? cleanID($namespace . ':' . $startId) : '';
350b603bbe1SLORTET            if ($startPage !== '' && page_exists($startPage)) {
351b603bbe1SLORTET                if ($this->matchesPageIconVariant($mediaID, $namespace, noNS($startPage))) return true;
352b603bbe1SLORTET            }
353b603bbe1SLORTET        }
354b603bbe1SLORTET
355b603bbe1SLORTET        return false;
356b603bbe1SLORTET    }
357b603bbe1SLORTET
358b603bbe1SLORTET    /**
359b603bbe1SLORTET     * Added in version 2026-03-09.
360b603bbe1SLORTET     * Returns the configured default icon URL, or the bundled fallback image when available.
361b603bbe1SLORTET     */
362b603bbe1SLORTET    public function getDefaultIconUrl(array $params = ['width' => 55], ?int &$mtime = null) {
36374a9e763SLORTET        $mediaID = $this->getConfiguredDefaultImageMediaID();
36474a9e763SLORTET        if ($mediaID) {
36574a9e763SLORTET            $mtime = $this->getMediaMTime((string)$mediaID);
36674a9e763SLORTET            $url = (string)ml((string)$mediaID, $params);
36774a9e763SLORTET            if ($url === '') return false;
36874a9e763SLORTET            return $this->appendVersionToUrl($url, $mtime);
36974a9e763SLORTET        }
37074a9e763SLORTET
37174a9e763SLORTET        $mtime = 0;
37274a9e763SLORTET        $bundled = $this->getBundledDefaultImageUrl();
37374a9e763SLORTET        if ($bundled !== '') return $bundled;
37474a9e763SLORTET
37574a9e763SLORTET        return false;
37674a9e763SLORTET    }
37774a9e763SLORTET
378b603bbe1SLORTET    /**
379b603bbe1SLORTET     * Added in version 2026-03-09.
380b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
381b603bbe1SLORTET     * Use getDefaultIconUrl() instead.
382b603bbe1SLORTET     */
383b603bbe1SLORTET    public function getDefaultImageIcon(array $params = ['width' => 55], ?int &$mtime = null) {
38474a9e763SLORTET        return $this->getDefaultIconUrl($params, $mtime);
38574a9e763SLORTET    }
38674a9e763SLORTET
387b603bbe1SLORTET    /**
388b603bbe1SLORTET     * Added in version 2026-03-09.
389b603bbe1SLORTET     * Returns a versioned icon URL for a page, or false when no icon matches.
390b603bbe1SLORTET     * Replaces the older getImageIcon() name.
391b603bbe1SLORTET     */
39274a9e763SLORTET    public function getPageIconUrl(
393da933f89SLORTET        string $namespace,
394da933f89SLORTET        string $pageID,
395da933f89SLORTET        string $size = 'bigorsmall',
396da933f89SLORTET        array $params = ['width' => 55],
39774a9e763SLORTET        ?int &$mtime = null,
39874a9e763SLORTET        bool $withDefault = false
399da933f89SLORTET    ) {
40074a9e763SLORTET        $mediaID = $this->getPageIconId($namespace, $pageID, $size);
401da933f89SLORTET        if (!$mediaID) {
40274a9e763SLORTET            if ($withDefault) {
40374a9e763SLORTET                return $this->getDefaultIconUrl($params, $mtime);
40474a9e763SLORTET            }
405da933f89SLORTET            $mtime = 0;
406da933f89SLORTET            return false;
407da933f89SLORTET        }
408da933f89SLORTET
409da933f89SLORTET        $mtime = $this->getMediaMTime((string)$mediaID);
410da933f89SLORTET        $url = (string)ml((string)$mediaID, $params);
411da933f89SLORTET        if ($url === '') return false;
412da933f89SLORTET        return $this->appendVersionToUrl($url, $mtime);
413da933f89SLORTET    }
414da933f89SLORTET
415b603bbe1SLORTET    /**
416b603bbe1SLORTET     * Added in version 2026-03-06.
417b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
418b603bbe1SLORTET     * Use getPageIconUrl() instead.
419b603bbe1SLORTET     */
42074a9e763SLORTET    public function getImageIcon(
42174a9e763SLORTET        string $namespace,
42274a9e763SLORTET        string $pageID,
42374a9e763SLORTET        string $size = 'bigorsmall',
42474a9e763SLORTET        array $params = ['width' => 55],
42574a9e763SLORTET        ?int &$mtime = null,
42674a9e763SLORTET        bool $withDefault = false
42774a9e763SLORTET    ) {
42874a9e763SLORTET        return $this->getPageIconUrl($namespace, $pageID, $size, $params, $mtime, $withDefault);
42974a9e763SLORTET    }
43074a9e763SLORTET
431b603bbe1SLORTET    /**
432b603bbe1SLORTET     * Added in version 2026-03-09.
433b603bbe1SLORTET     * Returns a versioned icon URL for a media file, or false when no icon matches.
434b603bbe1SLORTET     * Replaces the older getMediaIcon() name.
435b603bbe1SLORTET     */
43674a9e763SLORTET    public function getMediaIconUrl(
437da933f89SLORTET        string $mediaID,
438da933f89SLORTET        string $size = 'bigorsmall',
439da933f89SLORTET        array $params = ['width' => 55],
44074a9e763SLORTET        ?int &$mtime = null,
44174a9e763SLORTET        bool $withDefault = false
442da933f89SLORTET    ) {
44374a9e763SLORTET        $iconMediaID = $this->getMediaIconId($mediaID, $size);
444da933f89SLORTET        if (!$iconMediaID) {
44574a9e763SLORTET            if ($withDefault) {
44674a9e763SLORTET                return $this->getDefaultIconUrl($params, $mtime);
44774a9e763SLORTET            }
448da933f89SLORTET            $mtime = 0;
449da933f89SLORTET            return false;
450da933f89SLORTET        }
451da933f89SLORTET
452da933f89SLORTET        $mtime = $this->getMediaMTime((string)$iconMediaID);
453da933f89SLORTET        $url = (string)ml((string)$iconMediaID, $params);
454da933f89SLORTET        if ($url === '') return false;
455da933f89SLORTET        return $this->appendVersionToUrl($url, $mtime);
456da933f89SLORTET    }
457da933f89SLORTET
458b603bbe1SLORTET    /**
459b603bbe1SLORTET     * Added in version 2026-03-06.
460b603bbe1SLORTET     * Deprecated since version 2026-03-09, kept for backward compatibility.
461b603bbe1SLORTET     * Use getMediaIconUrl() instead.
462b603bbe1SLORTET     */
46374a9e763SLORTET    public function getMediaIcon(
46474a9e763SLORTET        string $mediaID,
46574a9e763SLORTET        string $size = 'bigorsmall',
46674a9e763SLORTET        array $params = ['width' => 55],
46774a9e763SLORTET        ?int &$mtime = null,
46874a9e763SLORTET        bool $withDefault = false
46974a9e763SLORTET    ) {
47074a9e763SLORTET        return $this->getMediaIconUrl($mediaID, $size, $params, $mtime, $withDefault);
47174a9e763SLORTET    }
47274a9e763SLORTET
473b603bbe1SLORTET    /**
474b603bbe1SLORTET     * Added in version 2026-03-06.
475b603bbe1SLORTET     * Returns the icon management URL associated with a media file, or null when unavailable.
476b603bbe1SLORTET     */
477b603bbe1SLORTET    public function getUploadMediaIconPage(string $mediaID = '') {
478da933f89SLORTET        $mediaID = cleanID($mediaID);
479da933f89SLORTET        if ($mediaID === '') return null;
480da933f89SLORTET
481da933f89SLORTET        $namespace = getNS($mediaID);
482da933f89SLORTET        $filename = noNS($mediaID);
483da933f89SLORTET        $base = (string)pathinfo($filename, PATHINFO_FILENAME);
484da933f89SLORTET        $targetPage = cleanID($namespace !== '' ? ($namespace . ':' . $base) : $base);
485da933f89SLORTET        if ($targetPage === '') return null;
486da933f89SLORTET
487da933f89SLORTET        return $this->getUploadIconPage($targetPage);
488da933f89SLORTET    }
489da933f89SLORTET}
490