xref: /dokuwiki/inc/Parsing/Helpers/Media.php (revision 1e28e406b358f79221c515b2a56520d5dbbfb6c8)
1*1e28e406SAndreas Gohr<?php
2*1e28e406SAndreas Gohr
3*1e28e406SAndreas Gohrnamespace dokuwiki\Parsing\Helpers;
4*1e28e406SAndreas Gohr
5*1e28e406SAndreas Gohr/**
6*1e28e406SAndreas Gohr * Pure helper for parsing DokuWiki media parameters.
7*1e28e406SAndreas Gohr *
8*1e28e406SAndreas Gohr * Side-effect-free: returns data and leaves handler emission to the
9*1e28e406SAndreas Gohr * caller. Shared by DokuWiki's Media mode ({{...}}) and GfmMedia
10*1e28e406SAndreas Gohr * (![alt](url)).
11*1e28e406SAndreas Gohr */
12*1e28e406SAndreas Gohrclass Media
13*1e28e406SAndreas Gohr{
14*1e28e406SAndreas Gohr    /**
15*1e28e406SAndreas Gohr     * Split a media source into src and trailing parameter block.
16*1e28e406SAndreas Gohr     *
17*1e28e406SAndreas Gohr     * DokuWiki media syntax encodes width/height, cache, linking, and
18*1e28e406SAndreas Gohr     * alignment directives as URL-style parameters after the last ?.
19*1e28e406SAndreas Gohr     * Using the last ? means URLs that already carry a query string
20*1e28e406SAndreas Gohr     * survive untouched — e.g. https://example.com/img?v=2?100x200&right
21*1e28e406SAndreas Gohr     * has src https://example.com/img?v=2 and params 100x200&right.
22*1e28e406SAndreas Gohr     *
23*1e28e406SAndreas Gohr     * GfmMedia relies on the left/right/center keywords for alignment
24*1e28e406SAndreas Gohr     * because GFM has no equivalent of DW's whitespace-inside-braces
25*1e28e406SAndreas Gohr     * alignment trick.
26*1e28e406SAndreas Gohr     *
27*1e28e406SAndreas Gohr     * @param string $src Raw media source with optional ?params suffix
28*1e28e406SAndreas Gohr     * @return array{src: string, width: ?string, height: ?string, cache: string, linking: string, align: ?string}
29*1e28e406SAndreas Gohr     */
30*1e28e406SAndreas Gohr    public static function parseParameters(string $src): array
31*1e28e406SAndreas Gohr    {
32*1e28e406SAndreas Gohr        $pos = strrpos($src, '?');
33*1e28e406SAndreas Gohr        if ($pos !== false) {
34*1e28e406SAndreas Gohr            $out = substr($src, 0, $pos);
35*1e28e406SAndreas Gohr            $param = substr($src, $pos + 1);
36*1e28e406SAndreas Gohr        } else {
37*1e28e406SAndreas Gohr            $out = $src;
38*1e28e406SAndreas Gohr            $param = '';
39*1e28e406SAndreas Gohr        }
40*1e28e406SAndreas Gohr
41*1e28e406SAndreas Gohr        $w = $h = null;
42*1e28e406SAndreas Gohr        if (preg_match('#(\d+)(x(\d+))?#i', $param, $size)) {
43*1e28e406SAndreas Gohr            $w = empty($size[1]) ? null : $size[1];
44*1e28e406SAndreas Gohr            $h = empty($size[3]) ? null : $size[3];
45*1e28e406SAndreas Gohr        }
46*1e28e406SAndreas Gohr
47*1e28e406SAndreas Gohr        if (preg_match('/nolink/i', $param)) {
48*1e28e406SAndreas Gohr            $linking = 'nolink';
49*1e28e406SAndreas Gohr        } elseif (preg_match('/direct/i', $param)) {
50*1e28e406SAndreas Gohr            $linking = 'direct';
51*1e28e406SAndreas Gohr        } elseif (preg_match('/linkonly/i', $param)) {
52*1e28e406SAndreas Gohr            $linking = 'linkonly';
53*1e28e406SAndreas Gohr        } else {
54*1e28e406SAndreas Gohr            $linking = 'details';
55*1e28e406SAndreas Gohr        }
56*1e28e406SAndreas Gohr
57*1e28e406SAndreas Gohr        if (preg_match('/(nocache|recache)/i', $param, $cachemode)) {
58*1e28e406SAndreas Gohr            $cache = $cachemode[1];
59*1e28e406SAndreas Gohr        } else {
60*1e28e406SAndreas Gohr            $cache = 'cache';
61*1e28e406SAndreas Gohr        }
62*1e28e406SAndreas Gohr
63*1e28e406SAndreas Gohr        if (preg_match('/(left|right|center)/i', $param, $alignmode)) {
64*1e28e406SAndreas Gohr            $align = strtolower($alignmode[1]);
65*1e28e406SAndreas Gohr        } else {
66*1e28e406SAndreas Gohr            $align = null;
67*1e28e406SAndreas Gohr        }
68*1e28e406SAndreas Gohr
69*1e28e406SAndreas Gohr        return [
70*1e28e406SAndreas Gohr            'src' => $out,
71*1e28e406SAndreas Gohr            'width' => $w,
72*1e28e406SAndreas Gohr            'height' => $h,
73*1e28e406SAndreas Gohr            'cache' => $cache,
74*1e28e406SAndreas Gohr            'linking' => $linking,
75*1e28e406SAndreas Gohr            'align' => $align,
76*1e28e406SAndreas Gohr        ];
77*1e28e406SAndreas Gohr    }
78*1e28e406SAndreas Gohr}
79