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 public static string $MEDIA_QUERY_PARAMETER = "media"; 26 private WikiPath $path; 27 28 29 /** 30 * @param WikiPath $path 31 * @return IFetcher 32 */ 33 public function setSourcePath(WikiPath $path): IFetcher 34 { 35 $this->path = $path; 36 return $this; 37 } 38 39 /** 40 * @param TagAttributes $tagAttributes 41 * @return IFetcher 42 * @throws ExceptionBadArgument - if the wiki id (id/media) property was not found and the path was not set 43 * @throws ExceptionBadSyntax - thrown by other class via the overwritten of {@link setSourcePath} (bad image) 44 * @throws ExceptionNotExists - thrown by other class via the overwritten of {@link setSourceImage} (non-existing image) 45 * @throws ExceptionNotFound - thrown by other class via the overwritten of {@link setSourceImage} (not found image) 46 */ 47 public function buildOriginalPathFromTagAttributes(TagAttributes $tagAttributes): IFetcher 48 { 49 50 if (!isset($this->path)) { 51 $id = $tagAttributes->getValueAndRemove(self::$MEDIA_QUERY_PARAMETER); 52 $defaultDrive = WikiPath::MEDIA_DRIVE; 53 if ($id === null) { 54 $id = $tagAttributes->getValueAndRemove(FetcherRawLocalPath::SRC_QUERY_PARAMETER); 55 } 56 if ($id === null) { 57 $id = $tagAttributes->getValueAndRemove(DokuwikiId::DOKUWIKI_ID_ATTRIBUTE); 58 $defaultDrive = WikiPath::MARKUP_DRIVE; 59 } 60 if ($id === null) { 61 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 } 63 $drive = $tagAttributes->getValueAndRemove(WikiPath::DRIVE_ATTRIBUTE, $defaultDrive); 64 $rev = $tagAttributes->getValueAndRemove(WikiPath::REV_ATTRIBUTE); 65 $path = WikiPath::toValidAbsolutePath($id); 66 if ($drive == WikiPath::MARKUP_DRIVE) { 67 /** 68 * Markup id have by default a txt extension 69 * but they may have other 70 */ 71 $wikiPath = WikiPath::createMarkupPathFromPath($path, $rev); 72 } else { 73 $wikiPath = WikiPath::createFromPath($path, $drive, $rev); 74 } 75 76 $this->setSourcePath($wikiPath); 77 } 78 79 return $this; 80 81 } 82 83 84 public function getSourcePath(): WikiPath 85 { 86 return $this->path; 87 } 88 89 /** 90 * @throws ExceptionNotFound 91 */ 92 public function getMime(): Mime 93 { 94 return FileSystems::getMime($this->path); 95 } 96 97 /** 98 * Add media and rev to url 99 * For dokuwiki implementation, see {@link ml()} 100 * We still use the {@link FetcherRawLocalPath::MEDIA_QUERY_PARAMETER} 101 * to be Dokuwiki Compatible even if we can serve from other drive know 102 * @param Url $url 103 * @param string $wikiIdKey - the key used to set the wiki id (ie {@link FetcherTraitWikiPath::$MEDIA_QUERY_PARAMETER} 104 * or {@link DokuWikiId::DOKUWIKI_ID_ATTRIBUTE} 105 */ 106 public function addLocalPathParametersToFetchUrl(Url $url, string $wikiIdKey): void 107 { 108 109 $url->addQueryParameterIfNotActualSameValue($wikiIdKey, $this->path->getWikiId()); 110 if ($this->path->getDrive() !== WikiPath::MEDIA_DRIVE) { 111 $url->addQueryParameter(WikiPath::DRIVE_ATTRIBUTE, $this->path->getDrive()); 112 } 113 try { 114 $rev = $this->path->getRevision(); 115 $url->addQueryParameter(WikiPath::REV_ATTRIBUTE, $rev); 116 } catch (ExceptionNotFound $e) { 117 // ok no rev 118 } 119 120 } 121 122} 123