xref: /dokuwiki/inc/Parsing/Helpers/Media.php (revision e7dae73bcd947f44c901faaac9dd45de67633a3b)
11e28e406SAndreas Gohr<?php
21e28e406SAndreas Gohr
31e28e406SAndreas Gohrnamespace dokuwiki\Parsing\Helpers;
41e28e406SAndreas Gohr
51e28e406SAndreas Gohr/**
61e28e406SAndreas Gohr * Pure helper for parsing DokuWiki media parameters.
71e28e406SAndreas Gohr *
81e28e406SAndreas Gohr * Side-effect-free: returns data and leaves handler emission to the
91e28e406SAndreas Gohr * caller. Shared by DokuWiki's Media mode ({{...}}) and GfmMedia
101e28e406SAndreas Gohr * (![alt](url)).
111e28e406SAndreas Gohr */
121e28e406SAndreas Gohrclass Media
131e28e406SAndreas Gohr{
141e28e406SAndreas Gohr    /**
151e28e406SAndreas Gohr     * Split a media source into src and trailing parameter block.
161e28e406SAndreas Gohr     *
171e28e406SAndreas Gohr     * DokuWiki media syntax encodes width/height, cache, linking, and
181e28e406SAndreas Gohr     * alignment directives as URL-style parameters after the last ?.
191e28e406SAndreas Gohr     * Using the last ? means URLs that already carry a query string
201e28e406SAndreas Gohr     * survive untouched — e.g. https://example.com/img?v=2?100x200&right
211e28e406SAndreas Gohr     * has src https://example.com/img?v=2 and params 100x200&right.
221e28e406SAndreas Gohr     *
231e28e406SAndreas Gohr     * GfmMedia relies on the left/right/center keywords for alignment
241e28e406SAndreas Gohr     * because GFM has no equivalent of DW's whitespace-inside-braces
251e28e406SAndreas Gohr     * alignment trick.
261e28e406SAndreas Gohr     *
271e28e406SAndreas Gohr     * @param string $src Raw media source with optional ?params suffix
281e28e406SAndreas Gohr     * @return array{src: string, width: ?string, height: ?string, cache: string, linking: string, align: ?string}
291e28e406SAndreas Gohr     */
301e28e406SAndreas Gohr    public static function parseParameters(string $src): array
311e28e406SAndreas Gohr    {
321e28e406SAndreas Gohr        $pos = strrpos($src, '?');
331e28e406SAndreas Gohr        if ($pos !== false) {
341e28e406SAndreas Gohr            $out = substr($src, 0, $pos);
351e28e406SAndreas Gohr            $param = substr($src, $pos + 1);
361e28e406SAndreas Gohr        } else {
371e28e406SAndreas Gohr            $out = $src;
381e28e406SAndreas Gohr            $param = '';
391e28e406SAndreas Gohr        }
40*e7dae73bSAndreas Gohr        $w = null;
41*e7dae73bSAndreas Gohr        $h = null;
421e28e406SAndreas Gohr        if (preg_match('#(\d+)(x(\d+))?#i', $param, $size)) {
431e28e406SAndreas Gohr            $w = empty($size[1]) ? null : $size[1];
441e28e406SAndreas Gohr            $h = empty($size[3]) ? null : $size[3];
451e28e406SAndreas Gohr        }
461e28e406SAndreas Gohr
471e28e406SAndreas Gohr        if (preg_match('/nolink/i', $param)) {
481e28e406SAndreas Gohr            $linking = 'nolink';
491e28e406SAndreas Gohr        } elseif (preg_match('/direct/i', $param)) {
501e28e406SAndreas Gohr            $linking = 'direct';
511e28e406SAndreas Gohr        } elseif (preg_match('/linkonly/i', $param)) {
521e28e406SAndreas Gohr            $linking = 'linkonly';
531e28e406SAndreas Gohr        } else {
541e28e406SAndreas Gohr            $linking = 'details';
551e28e406SAndreas Gohr        }
561e28e406SAndreas Gohr
571e28e406SAndreas Gohr        if (preg_match('/(nocache|recache)/i', $param, $cachemode)) {
581e28e406SAndreas Gohr            $cache = $cachemode[1];
591e28e406SAndreas Gohr        } else {
601e28e406SAndreas Gohr            $cache = 'cache';
611e28e406SAndreas Gohr        }
621e28e406SAndreas Gohr
631e28e406SAndreas Gohr        if (preg_match('/(left|right|center)/i', $param, $alignmode)) {
641e28e406SAndreas Gohr            $align = strtolower($alignmode[1]);
651e28e406SAndreas Gohr        } else {
661e28e406SAndreas Gohr            $align = null;
671e28e406SAndreas Gohr        }
681e28e406SAndreas Gohr
691e28e406SAndreas Gohr        return [
701e28e406SAndreas Gohr            'src' => $out,
711e28e406SAndreas Gohr            'width' => $w,
721e28e406SAndreas Gohr            'height' => $h,
731e28e406SAndreas Gohr            'cache' => $cache,
741e28e406SAndreas Gohr            'linking' => $linking,
751e28e406SAndreas Gohr            'align' => $align,
761e28e406SAndreas Gohr        ];
771e28e406SAndreas Gohr    }
781e28e406SAndreas Gohr}
79