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