1) { throw new \Qs_Exception('Export for multilanguage sites is not implemented.'); } $this->_sorterBase = time() - strtotime('2012-12-01 00:00:00'); $this->_initDPageOption(); parent::_init(); return $this; } /** * @param array $data * @return Obj */ public function dumpSql(array $data) { $pages = $this->_getPages($data['pages']); echo 'SET AUTOCOMMIT=0;' . PHP_EOL; echo 'START TRANSACTION;' . PHP_EOL . PHP_EOL; if (!empty($data['dictionaries'])) { $this->_dumpDictionaries($data['dictionaries']); } $this->_dumpPages($pages); echo PHP_EOL . 'COMMIT;' . PHP_EOL; return $this; } /** * @return array */ public function getDictionariesList() { return $this->_dictionariesList; } /** * @param array $dictionariesList * @return Obj */ protected function _dumpDictionaries(array $dictionariesList) { foreach ($this->_dictionariesList as $alias) { if (in_array($alias, $dictionariesList)) { echo '-- Dictionary: "' . $alias . '" ----------------------------------------------', PHP_EOL, PHP_EOL; $this->_renderSqlCreateTable($alias); $list = $this->_getDictionary($alias); foreach ($list as $row) { $this->_renderSqlInsert($alias, $row); } echo PHP_EOL; } } return $this; } /** * @param array $ids * @return array */ protected function _getPages(array $ids) { $cmsObj = \App_Cms_Obj::getInstance(); $pages = array(); foreach ($ids as $id) { $page = $cmsObj->getPageById($id); if ($page) { $page['metaOptions'] = $cmsObj->getMetaOptions($id); $page['sub'] = array(); $pages[$page['id']] = $page; } } $relations = \Qs_Array::fetchPairs($pages, array('id', 'idParent')); $successfulRelations = array(); foreach ($relations as $id => $parentId) { if ($parentId && array_key_exists($parentId, $pages)) { $pages[$parentId]['sub'][$id] = &$pages[$id]; $successfulRelations[$id] = $parentId; } } foreach (array_keys($successfulRelations) as $id) { unset($pages[$id]); } return $pages; } /** * @param array $pages * @param bool $addParentId * @return Obj */ protected function _dumpPages(array $pages, $addParentId = false) { foreach ($pages as $page) { echo PHP_EOL, '-- Page: "' . $page['fullAlias'] . '" (id = ' . $page['id'] . ', ', 'idParent = ' . $page['idParent'] . ') ', '------------------------------------------------', PHP_EOL, PHP_EOL; $this->_sqlPage($page, $addParentId); $this->_sqlPageContent($page); $this->_sqlPageOption($page); if (!empty($page['items'])) { $this->_dumpItems($page['items']); } if (!empty($page['sub'])) { $this->_dumpPages($page['sub'], true); } } return $this; } /** * @param array $items * @return Obj */ protected function _dumpItems(array $items) { $fields = array('idGroup', 'type', 'number', 'name', 'title', 'allowDelete', 'sorter', 'added', 'changed'); foreach ($items as $item) { $row = $this->_getArrayValues($item, $fields); foreach (array('number', 'name', 'title') as $field) { if (empty($row[$field])) { unset($row[$field]); } } $itemMethod = '_prepare' . ucfirst(str_replace(array('\\', '_'), '', $item['type'])) . 'Item'; if (method_exists($this, $itemMethod)) { if (!$this->{$itemMethod}($row)) { continue; } } $row['idPage'] = '@pageId' . $item['idPage']; $this->_renderSqlInsert('PageItem', $row); if (!empty($item['config'])) { $this->_renderSqlLastInsetId('pageItemId' . $item['id']); $config = \Qs_Array::flattenAssoc($item['config']); unset($config['id']); foreach ($config as $name => $value) { $row = array('idItem' => '@pageItemId' . $item['id'], 'name' => $name, 'value' => $value); $this->_renderSqlInsert('PageItemConfig', $row); } } } return $this; } /** * @param array $item * @return bool */ protected function _prepareHtmlBlockItem(array &$item) { $select = $this->_db->select(); $select->from($this->_getPair('HtmlBlockContent')); $select->where('`idBlock` = ?', $item['number'], \Qs_Db::INT_TYPE); $select->where('`language` = ?', \Qs_Language::getDefault()); $htmlBlock = $this->_db->fetchRow($select); if (!empty($htmlBlock)) { $row = array('added' => $htmlBlock['added']); $this->_renderSqlInsert('HtmlBlock', $row); $this->_renderSqlLastInsetId('htmlBlockId' . $item['number']); $htmlBlock['idBlock'] = '@htmlBlockId' . $item['number']; $urlParseFields = array('content'); foreach ($urlParseFields as $field) { if (isset($htmlBlock[$field])) { $htmlBlock[$field] = $this->_baseUrl2Tag($htmlBlock[$field]); } } $this->_renderSqlInsert('HtmlBlockContent', $htmlBlock); $item['number'] = '@htmlBlockId' . $item['number']; return true; } return false; } /** * @param array $page * @param bool $addParentId * @return Obj */ protected function _sqlPage(array $page, $addParentId = false) { $fields = array( 'alias', 'handler', 'bodyTemplate', 'showInSiteMap', 'showInMenu', 'showInFooter', 'enabled', 'allowDelete', 'isSecure', 'isIndexing', 'showInDropdowns', 'isRoot', 'hasBlocks', 'final', 'system', 'redirect', 'redirectType', 'redirectPageId', 'redirectStatus', 'redirectAlias', 'headerImage', 'added', 'changed' ); $row = $this->_getArrayValues($page, $fields); foreach ($fields as $field) { $row[$field] = $page[$field]; } $row['sorter'] = $this->_sorterBase + $page['sorter']; if ($addParentId) { $row['idParent'] = '@pageId' . $page['idParent']; } $urlParseFields = array('redirectAlias'); foreach ($urlParseFields as $field) { if (isset($row[$field])) { $row[$field] = $this->_baseUrl2Tag($row[$field]); } } $this->_renderSqlInsert('Page', $row); $this->_renderSqlLastInsetId('pageId' . $page['id']); return $this; } /** * @param array $page * @return Obj */ protected function _sqlPageContent(array $page) { $fields = array('language', 'title', 'menuTitle', 'header', 'keywords', 'description'); $row = $this->_getArrayValues($page, $fields); if ($row['title'] == $row['menuTitle']) { $row['title'] = ''; } if ($row['header'] == $row['menuTitle']) { $row['header'] = ''; } if (empty($row['language'])) { $row['language'] = \Qs_Language::getDefault(); } $row['idPage'] = '@pageId' . $page['id']; $this->_renderSqlInsert('PageContent', $row); return $this; } /** * @param array $page * @return Obj */ protected function _sqlPageOption(array $page) { foreach ($this->_dPageOption as $option => $defaults) { $row = array('idPage' => '@pageId' . $page['id']); if (array_key_exists($option, $page['metaOptions'])) { $row['name'] = $page['metaOptions'][$option]['name']; $row['show'] = $page['metaOptions'][$option]['show']; $row['value'] = $page['metaOptions'][$option]['value']; } else { $row['name'] = $defaults['name']; $row['show'] = $defaults['show']; $row['value'] = $defaults['value']; } $this->_renderSqlInsert('PageOption', $row); } return $this; } /** * @param string $tableAlias * @param array $data * @param bool $onDuplicateUpdate * @return Obj */ protected function _renderSqlInsert($tableAlias, array $data, $onDuplicateUpdate = false) { if (0 === strpos($tableAlias, '`')) { $tableName = $tableAlias; } else { $tableName = $this->_quoteIdentifier($this->_getTableName($tableAlias)); } $sql = 'INSERT INTO ' . $tableName . ' '; $columns = $this->_quoteIdentifier(array_keys($data)); $values = $this->_quoteValue(array_values($data)); $sql .= '(' . implode(', ', $columns) . ') ' . 'VALUES (' . implode(', ', $values) . ');'; if ($onDuplicateUpdate) { $primary = $this->_getTable($tableAlias)->getPrimary(); $updateColumns = \Qs_Array::excludeArray($data, $primary); $columns = array(); foreach ($updateColumns as $column => $value) { $columns[] = $this->_quoteIdentifier($column) . ' = ' . $this->_quoteValue($value); } $sql = trim($sql, ';'); $sql .= ' ON DUPLICATE KEY UPDATE ' . implode(', ', $columns) . ';'; } echo $sql, PHP_EOL; return $this; } /** * @param $varName * @return Obj */ protected function _renderSqlLastInsetId($varName) { $sql = 'SET @' . $varName . ' := LAST_INSERT_ID();'; echo $sql, PHP_EOL; return $this; } /** * @param string $tableAlias * @return Obj */ protected function _renderSqlCreateTable($tableAlias) { $tableName = $this->_getTableName($tableAlias); $sql = 'DROP TABLE IF EXISTS ' . $this->_quoteIdentifier($tableName) . ';' . PHP_EOL; $createTableSql = 'SHOW CREATE TABLE ' . $this->_quoteIdentifier($tableName); $sql .= $this->_db->query($createTableSql)->fetchColumn(1) . ';' . PHP_EOL; echo $sql, PHP_EOL; return $this; } /** * @param string $tableAlias * @return string */ protected function _getTemporaryTableName($tableAlias) { return 'temporary_export_' . $tableAlias; } /** * @param string $tableAlias * @return array */ protected function _getDictionary($tableAlias) { $select = $this->_db->select(); $select->from($this->_getPair($tableAlias)); return $this->_db->fetchAll($select); } /** * @return Obj */ protected function _initDPageOption() { if (null === $this->_dPageOption) { $select = $this->_db->select(); $select->from($this->_getPair('DPageOption'), array('name', 'name', 'show', 'value')); $select->order('sorter'); $this->_dPageOption = $this->_db->fetchAssoc($select); } return $this; } /** * @param array $array * @param array $keys * @return array */ protected function _getArrayValues(array $array, array $keys) { $result = array(); foreach ($keys as $key) { if (array_key_exists($key, $array)) { $result[$key] = $array[$key]; } } return $result; } /** * @param array|string $identifier * @param string|null $tableAlias * @return array|string */ protected function _quoteIdentifier($identifier, $tableAlias = null) { if (is_array($identifier)) { $function = function (&$value, $key, $params) { /** @var \Zend_Db_Adapter_Pdo_Mysql $db */ $db = $params['db']; if (!empty($params['tableAlias'])) { $value = $params['tableAlias'] . '.' . $value; } $value = $db->quoteIdentifier($value); }; $params = array('db' => $this->_db, 'tableAlias' => $tableAlias); array_walk($identifier, $function, $params); } else { if (null !== $tableAlias) { $identifier = $tableAlias . '.' . $identifier; } $identifier = $this->_db->quoteIdentifier($identifier); } return $identifier; } /** * @param array|string $value * @return array|string */ protected function _quoteValue($value) { if (is_array($value)) { $function = function (&$value, $key, \Zend_Db_Adapter_Pdo_Mysql $db) { if ('@' != substr($value, 0, 1)) { $value = $db->quote($value); } }; array_walk($value, $function, $this->_db); } else { $value = $this->_db->quote($value); } return $value; } }