1 <?php
2 
3 require_once(__DIR__ . '/../vendor/autoload.php');
4 
5 use ComboStrap\DatabasePageRow;
6 use ComboStrap\WikiPath;
7 use ComboStrap\ExceptionNotFound;
8 use ComboStrap\FileSystems;
9 use ComboStrap\LogUtility;
10 use ComboStrap\MarkupPath;
11 use ComboStrap\PageUrlPath;
12 use ComboStrap\PluginUtility;
13 
14 
15 /**
16  *   * Change the lang of the page if present
17  *   * Modify some style
18  */
19 class action_plugin_combo_lang extends DokuWiki_Action_Plugin
20 {
21 
22     const CANONICAL = "lang";
23 
24     /**
25      *
26      * hack as:
27      *   * {@link getID()} invoked later reads the id from the input variable
28      *   * {@link PluginUtility::getRequestedWikiId()} read it then also
29      *
30      * @param string $normalizedId
31      * @return void
32      */
33     private static function setNormalizedId(string $normalizedId)
34     {
35         global $INPUT;
36         $INPUT->set("id", $normalizedId);
37     }
38 
39     public function register(Doku_Event_Handler $controller)
40     {
41 
42         /**
43          * https://www.dokuwiki.org/devel:event:init_lang_load
44          */
45         $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'load_lang', array());
46         $controller->register_hook('INIT_LANG_LOAD', 'AFTER', $this, 'modifyRtlStyling', array());
47 
48 
49     }
50 
51     public function load_lang(Doku_Event $event, $param)
52     {
53         /**
54          * On the test setup of Dokuwiki
55          * this event is send without any context
56          * data
57          *
58          * This event is send before DokuWiki environment has initialized
59          * unfortunately
60          *
61          * We don't have any ID and we can't set them because
62          * they will be overwritten by calling the {@link getID()} function
63          *
64          */
65         /**
66          * Arabic characters should not be deleted, otherwise the page id abbr becomes the last name
67          * when URL encoding is used with arabic language
68          * ie:
69          * locale:%F8%B5%F9%81%F8%AD%F8%A9-id1tgpx9
70          * becomes
71          * locale:id1tgpx9
72          */
73         $clean = false;
74         /** @noinspection PhpConditionAlreadyCheckedInspection */
75         $id = getID("id", $clean);
76         $id = WikiPath::normalizeWikiPath($id);
77         self::setNormalizedId($id);
78         $page = MarkupPath::createMarkupFromId($id);
79         if (!FileSystems::exists($page->getPathObject())) {
80             // Is it a permanent link
81             try {
82                 $lastPartName = $page->getPathObject()->getLastNameWithoutExtension();
83             } catch (ExceptionNotFound $e) {
84                 // only the root does not have any name, it should therefore never happen
85                 LogUtility::internalError("No last name, we were unable to set the request id right", self::CANONICAL);
86                 return;
87             }
88             $encodedPageId = PageUrlPath::getShortEncodedPageIdFromUrlId($lastPartName);
89             if ($encodedPageId !== null) {
90                 $pageId = PageUrlPath::decodePageId($encodedPageId);
91                 if ($pageId !== null) {
92                     $page = DatabasePageRow::createFromPageIdAbbr($pageId)->getMarkupPath();
93                     if ($page === null) {
94                         return;
95                     }
96                     if (!FileSystems::exists($page->getPathObject())) {
97                         return;
98                     }
99 
100                     self::setNormalizedId($page->getPathObject()->getWikiId());
101 
102                 }
103             }
104         }
105         $pageLang = $page->getLangOrDefault();
106         global $conf;
107         $initialLang = $event->data;
108         if ($initialLang !== $pageLang) {
109             $conf['lang'] = $pageLang;
110             $event->data = $pageLang;
111         }
112 
113 
114     }
115 
116     /**
117      *
118      *
119      * In case of a RTL lang, we put the secedit button to the left
120      *
121      * @param Doku_Event $event
122      * @param $params
123      *
124      */
125     function modifyRtlStyling(Doku_Event $event, $params)
126     {
127 
128         /**
129          * Lang for a page
130          *
131          * https://www.w3.org/International/questions/qa-html-language-declarations
132          *   * Always use a language attribute on the html element.
133          *   * When serving XHTML 1.x (ie. using a MIME type such as application/xhtml+xml),
134          * use both the lang attribute and the xml:lang attribute together
135          *
136          * See also {@link \ComboStrap\Lang::processLangAttribute()} for the localization of an element
137          *
138          * put the button to the end when the page has a language direction of rtl
139          */
140         global $lang;
141         if ($lang['direction'] === "rtl") {
142             PluginUtility::getSnippetManager()->attachCssInternalStylesheet(self::CANONICAL . "-rtl");
143         }
144 
145 
146     }
147 
148 
149 }
150 
151 
152 
153