xref: /template/strap/ComboStrap/FetcherTraitWikiPath.php (revision 70bbd7f1f72440223cc13f3495efdcb2b0a11514)
104fd306cSNickeau<?php
204fd306cSNickeau
304fd306cSNickeaunamespace ComboStrap;
404fd306cSNickeau
504fd306cSNickeauuse ComboStrap\Web\Url;
604fd306cSNickeau
704fd306cSNickeau/**
804fd306cSNickeau * A trait to share between Fetcher
904fd306cSNickeau * if they depends on a {@link WikiPath}
1004fd306cSNickeau *
1104fd306cSNickeau * This trait can:
1204fd306cSNickeau *   * build the {@link FetcherTraitWikiPath::getSourcePath()} from {@link FetcherTraitWikiPath::buildOriginalPathFromTagAttributes() tag attributes}
1304fd306cSNickeau *   * add the {@link buildURLparams() url params to the fetch Url}
1404fd306cSNickeau *
1504fd306cSNickeau *
1604fd306cSNickeau * This is the case for the {@link FetcherSvg image}
1704fd306cSNickeau * and {@link FetcherRaster} but also for the {@link FetcherRailBar} that depends on the requested page
1804fd306cSNickeau *
1904fd306cSNickeau * Not all image depends on a path, that's why this is a trait to
2004fd306cSNickeau * share the code
2104fd306cSNickeau */
2204fd306cSNickeautrait FetcherTraitWikiPath
2304fd306cSNickeau{
2404fd306cSNickeau
2504fd306cSNickeau    private WikiPath $path;
2604fd306cSNickeau
2704fd306cSNickeau
2804fd306cSNickeau    /**
2904fd306cSNickeau     * @param WikiPath $path
3004fd306cSNickeau     * @return IFetcher
3104fd306cSNickeau     */
3204fd306cSNickeau    public function setSourcePath(WikiPath $path): IFetcher
3304fd306cSNickeau    {
3404fd306cSNickeau        $this->path = $path;
3504fd306cSNickeau        return $this;
3604fd306cSNickeau    }
3704fd306cSNickeau
3804fd306cSNickeau    /**
3904fd306cSNickeau     * @param TagAttributes $tagAttributes
4004fd306cSNickeau     * @return IFetcher
4104fd306cSNickeau     * @throws ExceptionBadArgument - if the wiki id (id/media) property was not found and the path was not set
4204fd306cSNickeau     * @throws ExceptionBadSyntax - thrown by other class via the overwritten of {@link setSourcePath} (bad image)
4304fd306cSNickeau     * @throws ExceptionNotExists - thrown by other class via the overwritten of {@link setSourceImage} (non-existing image)
4404fd306cSNickeau     * @throws ExceptionNotFound - thrown by other class via the overwritten of {@link setSourceImage} (not found image)
4504fd306cSNickeau     */
4604fd306cSNickeau    public function buildOriginalPathFromTagAttributes(TagAttributes $tagAttributes): IFetcher
4704fd306cSNickeau    {
4804fd306cSNickeau
4904fd306cSNickeau        if (!isset($this->path)) {
50*70bbd7f1Sgerardnico            $id = $tagAttributes->getValueAndRemove(MediaMarkup::$MEDIA_QUERY_PARAMETER);
5104fd306cSNickeau            $defaultDrive = WikiPath::MEDIA_DRIVE;
5204fd306cSNickeau            if ($id === null) {
5304fd306cSNickeau                $id = $tagAttributes->getValueAndRemove(FetcherRawLocalPath::SRC_QUERY_PARAMETER);
5404fd306cSNickeau            }
5504fd306cSNickeau            if ($id === null) {
5604fd306cSNickeau                $id = $tagAttributes->getValueAndRemove(DokuwikiId::DOKUWIKI_ID_ATTRIBUTE);
5704fd306cSNickeau                $defaultDrive = WikiPath::MARKUP_DRIVE;
5804fd306cSNickeau            }
5904fd306cSNickeau            if ($id === null) {
60*70bbd7f1Sgerardnico                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");
6104fd306cSNickeau            }
6204fd306cSNickeau            $drive = $tagAttributes->getValueAndRemove(WikiPath::DRIVE_ATTRIBUTE, $defaultDrive);
6304fd306cSNickeau            $rev = $tagAttributes->getValueAndRemove(WikiPath::REV_ATTRIBUTE);
6404fd306cSNickeau            $path = WikiPath::toValidAbsolutePath($id);
6504fd306cSNickeau            if ($drive == WikiPath::MARKUP_DRIVE) {
6604fd306cSNickeau                /**
6704fd306cSNickeau                 * Markup id have by default a txt extension
6804fd306cSNickeau                 * but they may have other
6904fd306cSNickeau                 */
7004fd306cSNickeau                $wikiPath = WikiPath::createMarkupPathFromPath($path, $rev);
7104fd306cSNickeau            } else {
7204fd306cSNickeau                $wikiPath = WikiPath::createFromPath($path, $drive, $rev);
7304fd306cSNickeau            }
7404fd306cSNickeau
7504fd306cSNickeau            $this->setSourcePath($wikiPath);
7604fd306cSNickeau        }
7704fd306cSNickeau
7804fd306cSNickeau        return $this;
7904fd306cSNickeau
8004fd306cSNickeau    }
8104fd306cSNickeau
8204fd306cSNickeau
8304fd306cSNickeau    public function getSourcePath(): WikiPath
8404fd306cSNickeau    {
8504fd306cSNickeau        return $this->path;
8604fd306cSNickeau    }
8704fd306cSNickeau
8804fd306cSNickeau    /**
8904fd306cSNickeau     * @throws ExceptionNotFound
9004fd306cSNickeau     */
9104fd306cSNickeau    public function getMime(): Mime
9204fd306cSNickeau    {
9304fd306cSNickeau        return FileSystems::getMime($this->path);
9404fd306cSNickeau    }
9504fd306cSNickeau
9604fd306cSNickeau    /**
9704fd306cSNickeau     * Add media and rev to url
9804fd306cSNickeau     * For dokuwiki implementation, see {@link ml()}
99*70bbd7f1Sgerardnico     * We still use the {@link MediaMarkup::MEDIA_QUERY_PARAMETER}
10004fd306cSNickeau     * to be Dokuwiki Compatible even if we can serve from other drive know
10104fd306cSNickeau     * @param Url $url
102*70bbd7f1Sgerardnico     * @param string $wikiIdKey - the key used to set the wiki id (ie {@link MediaMarkup::$MEDIA_QUERY_PARAMETER}
10304fd306cSNickeau     * or {@link DokuWikiId::DOKUWIKI_ID_ATTRIBUTE}
10404fd306cSNickeau     */
10504fd306cSNickeau    public function addLocalPathParametersToFetchUrl(Url $url, string $wikiIdKey): void
10604fd306cSNickeau    {
10704fd306cSNickeau
10804fd306cSNickeau        $url->addQueryParameterIfNotActualSameValue($wikiIdKey, $this->path->getWikiId());
10904fd306cSNickeau        if ($this->path->getDrive() !== WikiPath::MEDIA_DRIVE) {
11004fd306cSNickeau            $url->addQueryParameter(WikiPath::DRIVE_ATTRIBUTE, $this->path->getDrive());
11104fd306cSNickeau        }
11204fd306cSNickeau        try {
11304fd306cSNickeau            $rev = $this->path->getRevision();
11404fd306cSNickeau            $url->addQueryParameter(WikiPath::REV_ATTRIBUTE, $rev);
11504fd306cSNickeau        } catch (ExceptionNotFound $e) {
11604fd306cSNickeau            // ok no rev
11704fd306cSNickeau        }
11804fd306cSNickeau
11904fd306cSNickeau    }
12004fd306cSNickeau
12104fd306cSNickeau}
122