1<?php
2
3
4namespace ComboStrap;
5
6/**
7 * Class JavascriptLibrary
8 * @package ComboStrap
9 * A javascript library in the resource directory
10 */
11class JavascriptLibrary extends Media
12{
13
14    const EXTENSION = "js";
15
16
17    /**
18     * @param $dokuwikiId
19     * @return JavascriptLibrary
20     */
21    public static function createJavascriptLibraryFromDokuwikiId($dokuwikiId): JavascriptLibrary
22    {
23        $resource = DokuPath::createComboResource($dokuwikiId);
24        return new JavascriptLibrary($resource);
25    }
26
27
28    /**
29     * @return string
30     */
31    public function getUrl(): string
32    {
33        /**
34         * The ampersand must not be send encoded
35         *
36         * The url properties when used in a header
37         * are encoded via the {@link _tpl_metaheaders_action}
38         * that uses the {@link buildAttributes()} function
39         * that uses the function {@link htmlspecialchars} against the url
40         */
41        $ampersand = DokuwikiUrl::AMPERSAND_CHARACTER;
42
43        $path = $this->getPath();
44        if (!($path instanceof DokuPath)) {
45            LogUtility::msg("Only Javascript script from a wiki path can be served");
46            return "";
47        }
48        if(!FileSystems::exists($path)){
49            LogUtility::msg("The library path ($path) does not exist. No Url created.");
50            return "";
51        }
52        /**
53         * @var DokuPath $path
54         */
55        if ($path->getLibrary() !== DokuPath::COMBO_DRIVE) {
56            LogUtility::msg("Only Javascript script in the resource directory can be served, blank url returned");
57            return "";
58        };
59        $direct = true;
60        $att = [];
61        $this->addCacheBusterToQueryParameters($att);
62        $att[DokuPath::DRIVE_ATTRIBUTE] = $path->getLibrary();
63        return ml($path->getDokuwikiId(), $att, $direct, $ampersand, true);
64    }
65
66
67}
68