/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $domains = DomainQuery::create()->find(); $exportPointer = new ExportPointer(); // $basePath = 'jarves-export/' . date('Ymd-His'). '/'; $basePath = 'app/jarves/'; $fs = new Filesystem(); $fs->remove($basePath); foreach ($domains as $domain) { $domainData = $domain->toArray(TableMap::TYPE_CAMELNAME); $exportPointer->pushPath('website/' . $domainData['domain']); $nodes = NodeQuery::create()->filterByDomain($domain)->orderByLft()->filterByLft(1, Criteria::GREATER_THAN)->filterByLvl(1)->find(); foreach ($nodes as $idx => $node) { $this->exportNode($idx, $domain, $node, $exportPointer); } $domainData = $this->clearData($domainData, ['id', 'startnodeId']); if ($domain->hasVirtualColumn('startnodePath')) { $domainData['startnode'] = $domain->getVirtualColumn('startnodePath'); } else { $output->writeln(sprintf('<error>Domain %s has no start node defined</error>', $domain->getDomain())); } $exportPointer->addData($domainData, '.yml'); $exportPointer->popPath(); } $files = FileQuery::create()->find(); $fileReferences = []; foreach ($files as $file) { $fileReferences[$file->getId()] = $file->getPath(); } $exportPointer->addData($fileReferences, 'file_references.yml'); foreach ($exportPointer->data as $path => $data) { $path = $basePath . $path; if (!is_dir(dirname($path))) { mkdir(dirname($path), 0770, true); } $yml = $this->dumpYaml($data); $output->writeln(sprintf('write %s', $path)); file_put_contents($path, $yml); } $output->writeln(sprintf('Done.')); }
/** * Translates the internal id to the real path. * Example: getPath(45) => '/myImageFolder/Picture1.png' * * @static * * @param integer|string $id String for backward compatibility * * @return string */ public function getPath($id) { if (!is_numeric($id)) { return $id; } return FileQuery::create()->select('path')->findOneById($id); }
protected function import(InputInterface $input, OutputInterface $output) { $basePath = 'app/jarves/'; $openedFiles = []; $domains = []; $nodes = []; $rootNodes = []; $ymlParser = new Parser(); Propel::getWriteConnection('default')->beginTransaction(); //import files $fileReferencesPath = $basePath . '/file_references.yml'; if (file_exists($fileReferencesPath)) { $openedFiles[$fileReferencesPath] = filemtime($fileReferencesPath); $fileReferences = $ymlParser->parse(file_get_contents($fileReferencesPath)); $output->writeln(sprintf('Import %d file references ...', count($fileReferences))); FileQuery::create()->deleteAll(); foreach ($fileReferences as $id => $path) { $file = new File(); $file->setId($id); $file->setPath($path); $file->save(); } } $relativePathBase = $basePath . 'website/'; $dir = opendir($relativePathBase); while ($domainName = readdir($dir)) { if ('.' === $domainName || '..' === $domainName || '.yml' === substr($domainName, -4)) { continue; } $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($basePath . 'website/' . $domainName)); $files = iterator_to_array($files); ksort($files); $output->writeln(sprintf('Import domain %s ...', $domainName)); $domain = DomainQuery::create()->findOneByDomain($domainName); if (!$domain) { $domain = new Domain(); } $ymlPath = $relativePathBase . $domainName . '.yml'; $domainFromYml = $ymlParser->parse(file_get_contents($ymlPath)); $openedFiles[$ymlPath] = filemtime($ymlPath); $oldData = $domain->toArray(TableMap::TYPE_CAMELNAME); $domain->fromArray(array_merge($oldData, $domainFromYml), TableMap::TYPE_CAMELNAME); $domain->setStartnodeId(null); if (isset($domainFromYml['startnode'])) { $domain->setVirtualColumn('startnodePath', $domainFromYml['startnode']); } $domain->save(); $domains[$domainName] = $domain; NodeQuery::create()->filterByDomain($domain)->delete(); $rootNode = new Node(); $rootNode->setTitle('root'); $rootNode->makeRoot(); $rootNode->setDomain($domain); $rootNode->save(); $rootNodes[$domainName] = $rootNode; $nodes[''] = $rootNode; $parentNodeQueue = []; /** @var \SplFileInfo $file */ foreach ($files as $file) { if ('.' === $file->getFilename() || '..' === $file->getFilename() || '.yml' !== substr($file->getFilename(), -4)) { continue; } $path = $file->getPath() . '/' . $file->getFilename(); $path = substr($path, strlen($relativePathBase . $domainName) + 1); if (!$path) { continue; } $parentPath = ''; if (false !== strpos($path, '/')) { $parentPath = dirname($path); } $baseName = substr(basename($path), 0, -4); //without .yml if ($baseName) { //its a node $node = isset($nodes[$path]) ? $nodes[$path] : new Node(); $ymlPath = $relativePathBase . $domainName . '/' . $path; $nodeFromYml = $ymlParser->parse(file_get_contents($ymlPath)); $openedFiles[$ymlPath] = filemtime($ymlPath); $node->fromArray($nodeFromYml, TableMap::TYPE_CAMELNAME); $node->setDomain($domain); $urn = $baseName; if (false !== ($dotPos = strpos($baseName, '.'))) { $prefix = substr($baseName, 0, $dotPos); if ($prefix) { $urn = substr($baseName, $dotPos + 1); } } $node->setUrn($urn); $output->writeln(sprintf('Import page %s%s ...', str_repeat(' ', substr_count($path, '/')), $node->getTitle())); $nodes[substr($path, 0, -4)] = $node; $end = isset($parentNodeQueue[$parentPath]) ? count($parentNodeQueue[$parentPath]) : 1; $position = isset($nodeFromYml['sort']) ? $nodeFromYml['sort'] : $end; $parentNodeQueue[$parentPath][$position][] = $node; if (isset($nodeFromYml['contents'])) { foreach ($nodeFromYml['contents'] as $idx => $contentFromYaml) { if (isset($contentFromYaml['content']) && is_array($contentFromYaml['content'])) { $contentFromYaml['content'] = json_encode($contentFromYaml['content']); } $content = new Content(); $content->setSort($idx); $content->fromArray($contentFromYaml, TableMap::TYPE_CAMELNAME); $content->setNode($node); } if ($domain->hasVirtualColumn('startnodePath') && substr($path, 0, -4) === $domain->getVirtualColumn('startnodePath')) { $domain->setStartnode($node); } } } } //save queued nodes foreach ($parentNodeQueue as $parentPath => $nodesQueue) { ksort($nodesQueue); //key is sort /** @var Node $node */ foreach ($nodesQueue as $position => $nodesToInsert) { foreach ($nodesToInsert as $node) { $node->insertAsLastChildOf($nodes[$parentPath]); $node->save(); //saves Content as well. } } } $domain->save(); } Propel::getWriteConnection('default')->commit(); $cacher = $this->getContainer()->get('jarves.cache.cacher'); $cacher->invalidateCache('core'); return $openedFiles; }