From fe851d2a10b199379002256fbccc3c93d846a647 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 26 Nov 2015 19:26:00 +0100 Subject: [PATCH] Added page-PIP --- .../system/language/LanguageFactory.class.php | 9 + .../PagePackageInstallationPlugin.class.php | 172 ++++++++++++------ wcfsetup/setup/db/install.sql | 2 +- 3 files changed, 126 insertions(+), 57 deletions(-) diff --git a/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php b/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php index afeb514efd..d2cd5fca36 100644 --- a/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php +++ b/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php @@ -238,6 +238,15 @@ class LanguageFactory extends SingletonFactory { return preg_replace('/-[a-z0-9]+/', '', $languageCode); } + /** + * Returns the default language object. + * + * @return Language + */ + public function getDefaultLanguage() { + return $this->getLanguage($this->cache['default']); + } + /** * Returns the default language id * diff --git a/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php index a52cae1ece..34c3e30410 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php @@ -1,6 +1,10 @@ tableName." - WHERE actionName = ? - AND actionClassName = ? + $sql = "DELETE FROM wcf".WCF_N."_page + WHERE name = ? AND packageID = ?"; $statement = WCF::getDB()->prepareStatement($sql); + + WCF::getDB()->beginTransaction(); foreach ($items as $item) { - $statement->execute(array( + $statement->execute([ $item['attributes']['name'], - $item['elements']['actionclassname'], $this->installation->getPackageID() - )); + ]); } + WCF::getDB()->commitTransaction(); } /** @@ -47,41 +57,90 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin protected function getElement(\DOMXPath $xpath, array &$elements, \DOMElement $element) { $nodeValue = $element->nodeValue; - // read pages - if ($element->tagName == 'pages') { - $nodeValue = array(); + // read content + if ($element->tagName === 'content') { + if (!isset($elements['content'])) $elements['content'] = []; - $pages = $xpath->query('child::ns:page', $element); - foreach ($pages as $page) { - $nodeValue[] = $page->nodeValue; + $children = []; + /** @var \DOMElement $child */ + foreach ($xpath->query('child::*', $element) as $child) { + $children[$child->tagName] = $child->nodeValue; } + + $elements[$element->tagName][$element->getAttribute('language')] = $children; + } + else if ($element->tagName === 'displayname') { + // can occur multiple times using the `language` attribute + if (!isset($elements['displayName'])) $elements['displayName'] = []; + + $elements['displayName'][$element->getAttribute('language')] = $element->nodeValue; + } + else { + $elements[$element->tagName] = $nodeValue; } - - $elements[$element->tagName] = $nodeValue; } /** * @see AbstractXMLPackageInstallationPlugin::prepareImport() + * @throws SystemException */ protected function prepareImport(array $data) { - $isStatic = (!empty($data['content'])); + $isStatic = false; + if (!empty($data['elements']['content'])) { + $isStatic = true; + + $content = []; + foreach ($data['elements']['content'] as $language => $contentData) { + $content[$language] = [ + 'content' => $contentData['content'], + 'customURL' => $contentData['customurl'], + 'metaDescription' => (!empty($contentData['metadescription'])) ? StringUtil::trim($contentData['metadescription']) : '', + 'metaKeywords' => (!empty($contentData['metakeywords'])) ? StringUtil::trim($contentData['metakeywords']) : '', + 'title' => $contentData['title'] + ]; + } + + $data['elements']['content'] = $content; + } + + // pick the display name by choosing the default language, or 'en' or '' (empty string) + $defaultLanguageCode = LanguageFactory::getInstance()->getDefaultLanguage()->getFixedLanguageCode(); + if (isset($data['elements']['displayName'][$defaultLanguageCode])) { + // use the default language + $displayName = $data['elements']['displayName'][$defaultLanguageCode]; + } + else if (isset($data['elements']['displayName']['en'])) { + // use the value for English + $displayName = $data['elements']['displayName']['en']; + } + else { + // fallback to the display name without/empty language attribute + $displayName = $data['elements']['displayName']['']; + } + + $parentPageID = null; + if (!empty($data['elements']['parent'])) { + $sql = "SELECT pageID + FROM wcf".WCF_N."_".$this->tableName." + WHERE name = ?"; + $statement = WCF::getDB()->prepareStatement($sql, 1); + $statement->execute([$data['elements']['parent']]); + $row = $statement->fetchSingleRow(); + if ($row === false) { + throw new SystemException("Unknown parent page '" . $data['elements']['parent'] . "' for page identifier '" . $data['attributes']['name'] . "'"); + } + + $parentPageID = $row['pageID']; + } return [ + 'content' => ($isStatic) ? $data['elements']['content'] : [], 'controller' => ($isStatic) ? '' : $data['elements']['controller'], - 'controllerCustomURL' => ($isStatic) ? '' : $data['elements']['customurl'], - 'displayName' => $data['elements']['displayname'], - 'name' => $data['attributes']['name'] + 'controllerCustomURL' => ($isStatic || empty($data['elements']['customurl'])) ? '' : $data['elements']['customurl'], + 'displayName' => $displayName, + 'name' => $data['attributes']['name'], + 'parentPageID' => $parentPageID ]; - - $showOrder = (isset($data['elements']['showorder'])) ? intval($data['elements']['showorder']) : null; - $showOrder = $this->getShowOrder($showOrder, $data['elements']['actionclassname'], 'actionClassName'); - - return array( - 'actionClassName' => $data['elements']['actionclassname'], - 'actionName' => $data['attributes']['name'], - 'pages' => $data['elements']['pages'], - 'showOrder' => $showOrder - ); } /** @@ -90,12 +149,10 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin protected function findExistingItem(array $data) { $sql = "SELECT * FROM wcf".WCF_N."_".$this->tableName." - WHERE actionName = ? - AND actionClassName = ? + WHERE name = ? AND packageID = ?"; $parameters = array( - $data['actionName'], - $data['actionClassName'], + $data['name'], $this->installation->getPackageID() ); @@ -109,42 +166,45 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin * @see AbstractXMLPackageInstallationPlugin::import() */ protected function import(array $row, array $data) { - // extract pages - $pages = $data['pages']; - unset($data['pages']); + // extract content + $content = $data['content']; + unset($data['content']); // import or update action $object = parent::import($row, $data); - // store pages for later import - $this->pages[$object->actionID] = $pages; + // store content for later import + $this->content[$object->pageID] = $content; } /** * @see AbstractXMLPackageInstallationPlugin::postImport() */ protected function postImport() { - // clear pages - $sql = "DELETE FROM wcf".WCF_N."_clipboard_page - WHERE packageID = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(array($this->installation->getPackageID())); - - if (!empty($this->pages)) { - // insert pages - $sql = "INSERT INTO wcf".WCF_N."_clipboard_page - (pageClassName, packageID, actionID) - VALUES (?, ?, ?)"; + if (!empty($this->content)) { + $sql = "INSERT IGNORE INTO wcf".WCF_N."_page_content + (pageID, languageID, title, content, metaDescription, metaKeywords, customURL) + VALUES (?, ?, ?, ?, ?, ?, ?)"; $statement = WCF::getDB()->prepareStatement($sql); - foreach ($this->pages as $actionID => $pages) { - foreach ($pages as $pageClassName) { - $statement->execute(array( - $pageClassName, - $this->installation->getPackageID(), - $actionID - )); + + WCF::getDB()->beginTransaction(); + foreach ($this->content as $pageID => $contentData) { + foreach ($contentData as $languageCode => $content) { + $language = LanguageFactory::getInstance()->getLanguageByCode($languageCode); + if ($language !== null) { + $statement->execute([ + $pageID, + $language->languageID, + $content['title'], + $content['content'], + $content['metaDescription'], + $content['metaKeywords'], + $content['customURL'] + ]); + } } } + WCF::getDB()->commitTransaction(); } } } diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index d2c208be34..e5f5152cec 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -822,7 +822,7 @@ CREATE TABLE wcf1_page_content ( metaKeywords TEXT, customURL VARCHAR(255) NOT NULL, - KEY (pageID, languageID) + UNIQUE KEY (pageID, languageID) ); DROP TABLE IF EXISTS wcf1_page_menu_item; -- 2.20.1