Fix language PIP GUI if no language files exist yet
authorMatthias Schmidt <gravatronics@live.com>
Sat, 5 Jan 2019 09:03:30 +0000 (10:03 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 5 Jan 2019 09:03:30 +0000 (10:03 +0100)
See #2545

wcfsetup/install/files/lib/system/devtools/pip/TMultiXmlGuiPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/LanguagePackageInstallationPlugin.class.php

index aef88887556a31633eb801222ebd7f376ee00a10..6ceb695a8f193b8d1b3bb3f9aaa442cfe8f53405 100644 (file)
@@ -38,7 +38,7 @@ trait TMultiXmlGuiPackageInstallationPlugin {
         * @param       IFormDocument           $form
         */
        public function addEntry(IFormDocument $form) {
-               foreach ($this->getProjectXmls() as $xml) {
+               foreach ($this->getProjectXmls(true) as $xml) {
                        $newElement = $this->createAndInsertNewXmlElement($xml, $form);
                        
                        $this->saveObject($newElement);
@@ -72,7 +72,7 @@ trait TMultiXmlGuiPackageInstallationPlugin {
         */
        public function editEntry(IFormDocument $form, $identifier) {
                $newElement = null;
-               foreach ($this->getProjectXmls() as $xml) {
+               foreach ($this->getProjectXmls(true) as $xml) {
                        $element = $this->getElementByIdentifier($xml, $identifier);
                        $newElement = $this->replaceXmlElement($xml, $form, $identifier);
                        
@@ -134,9 +134,10 @@ trait TMultiXmlGuiPackageInstallationPlugin {
        /**
         * Returns the xml objects for this pip.
         * 
+        * @param       boolean         $createXmlFiles         if `true` and if a relevant XML file does not exist, it is created
         * @return      XML[]
         */
-       abstract protected function getProjectXmls();
+       abstract protected function getProjectXmls($createXmlFiles = false);
        
        /**
         * @inheritDoc
index b53b730230f72df18bfe0268c212b70a6083370e..c5cfd0ccb7368ecf0fae007aa54d75df20a72448 100644 (file)
@@ -27,6 +27,7 @@ use wcf\system\language\LanguageFactory;
 use wcf\system\package\PackageArchive;
 use wcf\system\WCF;
 use wcf\util\DOMUtil;
+use wcf\util\FileUtil;
 use wcf\util\StringUtil;
 use wcf\util\XML;
 
@@ -473,23 +474,16 @@ class LanguagePackageInstallationPlugin extends AbstractXMLPackageInstallationPl
                ]);
                
                // add one field per language
-               foreach ($this->getProjectXmls() as $xml) {
-                       $languageCode = $xml->getDocument()->documentElement->getAttribute('languagecode');
-                       $languageName = LanguageFactory::getInstance()->getLanguageByCode($languageCode)->languageName;
-                       
-                       if ($dataContainer->getNodeById($languageCode) !== null) {
-                               throw new \LogicException("Duplicate language file with language code '{$languageCode}'.");
-                       }
-                       
+               foreach (LanguageFactory::getInstance()->getLanguages() as $language) {
                        $description = null;
-                       $descriptionLanguageItem = 'wcf.acp.pip.language.languageItemValue.' . $languageCode . '.description';
+                       $descriptionLanguageItem = 'wcf.acp.pip.language.languageItemValue.' . $language->languageCode . '.description';
                        if (WCF::getLanguage()->get($descriptionLanguageItem, true) !== '') {
                                $description = $descriptionLanguageItem;
                        }
                        
                        $dataContainer->appendChild(
-                               MultilineTextFormField::create($languageCode)
-                                       ->label($languageName)
+                               MultilineTextFormField::create($language->languageCode)
+                                       ->label($language->languageName)
                                        ->description($description)
                        );
                }
@@ -642,27 +636,45 @@ XML;
        
        /**
         * Returns the xml objects for this pip.
-        *
+        * 
+        * @param       boolean         $createXmlFiles         if `true` and if a relevant XML file does not exist, it is created
         * @return      XML[]
         */
-       protected function getProjectXmls() {
+       protected function getProjectXmls($createXmlFiles = false) {
                $xmls = [];
                
-               foreach ($this->installation->getProject()->getLanguageFiles() as $languageFile) {
-                       $xml = new XML();
-                       if (!file_exists($languageFile)) {
-                               $xml->loadXML($languageFile, $this->getEmptyXml(substr(basename($languageFile), 0, -4)));
-                       }
-                       else {
-                               $xml->load($languageFile);
+               if ($createXmlFiles) {
+                       $directory = $this->installation->getProject()->path . ($this->installation->getProject()->isCore() ? 'wcfsetup/install/lang/' : 'language/');
+                       if (!is_dir($directory)) {
+                               FileUtil::makePath($directory);
                        }
                        
-                       // only consider installed languages
-                       $languageCode = $xml->getDocument()->documentElement->getAttribute('languagecode');
-                       if (LanguageFactory::getInstance()->getLanguageByCode($languageCode) !== null) {
+                       foreach (LanguageFactory::getInstance()->getLanguages() as $language) {
+                               $languageFile = $directory . $language->languageCode . '.xml';
+                               
+                               $xml = new XML();
+                               if (!file_exists($languageFile)) {
+                                       $xml->loadXML($languageFile, $this->getEmptyXml(substr(basename($languageFile), 0, -4)));
+                               }
+                               else {
+                                       $xml->load($languageFile);
+                               }
+                               
                                $xmls[] = $xml;
                        }
                }
+               else {
+                       foreach ($this->installation->getProject()->getLanguageFiles() as $languageFile) {
+                               $xml = new XML();
+                               $xml->load($languageFile);
+                               
+                               // only consider installed languages
+                               $languageCode = $xml->getDocument()->documentElement->getAttribute('languagecode');
+                               if (LanguageFactory::getInstance()->getLanguageByCode($languageCode) !== null) {
+                                       $xmls[] = $xml;
+                               }
+                       }
+               }
                
                return $xmls;
        }