1<?php
2
3namespace ComboStrap;
4
5use ComboStrap\Web\Url;
6
7/**
8 * A trait to share between Fetcher
9 * if they depends on a {@link WikiPath}
10 *
11 * This trait can:
12 *   * build the {@link FetcherTraitWikiPath::getSourcePath()} from {@link FetcherTraitWikiPath::buildOriginalPathFromTagAttributes() tag attributes}
13 *   * add the {@link buildURLparams() url params to the fetch Url}
14 *
15 *
16 * This is the case for the {@link FetcherSvg image}
17 * and {@link FetcherRaster} but also for the {@link FetcherRailBar} that depends on the requested page
18 *
19 * Not all image depends on a path, that's why this is a trait to
20 * share the code
21 */
22trait FetcherTraitWikiPath
23{
24
25    private WikiPath $path;
26
27
28    /**
29     * @param WikiPath $path
30     * @return IFetcher
31     */
32    public function setSourcePath(WikiPath $path): IFetcher
33    {
34        $this->path = $path;
35        return $this;
36    }
37
38    /**
39     * @param TagAttributes $tagAttributes
40     * @return IFetcher
41     * @throws ExceptionBadArgument - if the wiki id (id/media) property was not found and the path was not set
42     * @throws ExceptionBadSyntax - thrown by other class via the overwritten of {@link setSourcePath} (bad image)
43     * @throws ExceptionNotExists - thrown by other class via the overwritten of {@link setSourceImage} (non-existing image)
44     * @throws ExceptionNotFound - thrown by other class via the overwritten of {@link setSourceImage} (not found image)
45     */
46    public function buildOriginalPathFromTagAttributes(TagAttributes $tagAttributes): IFetcher
47    {
48
49        if (!isset($this->path)) {
50            $id = $tagAttributes->getValueAndRemove(MediaMarkup::$MEDIA_QUERY_PARAMETER);
51            $defaultDrive = WikiPath::MEDIA_DRIVE;
52            if ($id === null) {
53                $id = $tagAttributes->getValueAndRemove(FetcherRawLocalPath::SRC_QUERY_PARAMETER);
54            }
55            if ($id === null) {
56                $id = $tagAttributes->getValueAndRemove(DokuwikiId::DOKUWIKI_ID_ATTRIBUTE);
57                $defaultDrive = WikiPath::MARKUP_DRIVE;
58            }
59            if ($id === null) {
60                throw new ExceptionBadArgument("The (" . MediaMarkup::$MEDIA_QUERY_PARAMETER . ", " . FetcherRawLocalPath::SRC_QUERY_PARAMETER . " or " . DokuwikiId::DOKUWIKI_ID_ATTRIBUTE . ") query property is mandatory and was not defined");
61            }
62            $drive = $tagAttributes->getValueAndRemove(WikiPath::DRIVE_ATTRIBUTE, $defaultDrive);
63            $rev = $tagAttributes->getValueAndRemove(WikiPath::REV_ATTRIBUTE);
64            $path = WikiPath::toValidAbsolutePath($id);
65            if ($drive == WikiPath::MARKUP_DRIVE) {
66                /**
67                 * Markup id have by default a txt extension
68                 * but they may have other
69                 */
70                $wikiPath = WikiPath::createMarkupPathFromPath($path, $rev);
71            } else {
72                $wikiPath = WikiPath::createFromPath($path, $drive, $rev);
73            }
74
75            $this->setSourcePath($wikiPath);
76        }
77
78        return $this;
79
80    }
81
82
83    public function getSourcePath(): WikiPath
84    {
85        return $this->path;
86    }
87
88    /**
89     * @throws ExceptionNotFound
90     */
91    public function getMime(): Mime
92    {
93        return FileSystems::getMime($this->path);
94    }
95
96    /**
97     * Add media and rev to url
98     * For dokuwiki implementation, see {@link ml()}
99     * We still use the {@link MediaMarkup::MEDIA_QUERY_PARAMETER}
100     * to be Dokuwiki Compatible even if we can serve from other drive know
101     * @param Url $url
102     * @param string $wikiIdKey - the key used to set the wiki id (ie {@link MediaMarkup::$MEDIA_QUERY_PARAMETER}
103     * or {@link DokuWikiId::DOKUWIKI_ID_ATTRIBUTE}
104     */
105    public function addLocalPathParametersToFetchUrl(Url $url, string $wikiIdKey): void
106    {
107
108        $url->addQueryParameterIfNotActualSameValue($wikiIdKey, $this->path->getWikiId());
109        if ($this->path->getDrive() !== WikiPath::MEDIA_DRIVE) {
110            $url->addQueryParameter(WikiPath::DRIVE_ATTRIBUTE, $this->path->getDrive());
111        }
112        try {
113            $rev = $this->path->getRevision();
114            $url->addQueryParameter(WikiPath::REV_ATTRIBUTE, $rev);
115        } catch (ExceptionNotFound $e) {
116            // ok no rev
117        }
118
119    }
120
121}
122