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