/**
  * @param \TYPO3\Form\Core\Model\FinisherContext $finisherContext
  * @return void
  * @throws \TYPO3\Setup\Exception
  */
 public function importSite(\TYPO3\Form\Core\Model\FinisherContext $finisherContext)
 {
     $formValues = $finisherContext->getFormRuntime()->getFormState()->getFormValues();
     if (isset($formValues['prune']) && intval($formValues['prune']) === 1) {
         $this->nodeRepository->removeAll();
         $this->workspaceRepository->removeAll();
         $this->domainRepository->removeAll();
         $this->siteRepository->removeAll();
         $this->persistenceManager->persistAll();
     }
     if (!empty($formValues['packageKey'])) {
         if ($this->packageManager->isPackageAvailable($formValues['packageKey'])) {
             throw new \TYPO3\Setup\Exception(sprintf('The package key "%s" already exists.', $formValues['packageKey']), 1346759486);
         }
         $packageKey = $formValues['packageKey'];
         $siteName = $formValues['packageKey'];
         $this->packageManager->createPackage($packageKey, NULL, Files::getUnixStylePath(Files::concatenatePaths(array(FLOW3_PATH_PACKAGES, 'Sites'))));
         $this->generatorService->generateSitesXml($packageKey, $siteName);
         $this->generatorService->generateSitesTypoScript($packageKey, $siteName);
         $this->generatorService->generateSitesTemplate($packageKey, $siteName);
         $this->packageManager->activatePackage($packageKey);
     } else {
         $packageKey = $formValues['site'];
     }
     if ($packageKey !== '') {
         try {
             $contentContext = new \TYPO3\TYPO3\Domain\Service\ContentContext('live');
             $this->nodeRepository->setContext($contentContext);
             $this->siteImportService->importFromPackage($packageKey);
         } catch (\Exception $exception) {
             $finisherContext->cancel();
             $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error(sprintf('Error: During the import of the "Sites.xml" from the package "%s" an exception occurred: %s', $packageKey, $exception->getMessage())));
         }
     }
 }
 /**
  * Display a list of available sites
  *
  * @return void
  */
 public function listCommand()
 {
     $sites = $this->siteRepository->findAll();
     if ($sites->count() === 0) {
         $this->outputLine('No sites available');
         $this->quit(0);
     }
     $longestSiteName = 4;
     $longestNodeName = 9;
     $longestSiteResource = 17;
     $availableSites = array();
     foreach ($sites as $site) {
         array_push($availableSites, array('name' => $site->getName(), 'nodeName' => $site->getNodeName(), 'siteResourcesPackageKey' => $site->getSiteResourcesPackageKey()));
         if (strlen($site->getName()) > $longestSiteName) {
             $longestSiteName = strlen($site->getName());
         }
         if (strlen($site->getNodeName()) > $longestNodeName) {
             $longestNodeName = strlen($site->getNodeName());
         }
         if (strlen($site->getSiteResourcesPackageKey()) > $longestSiteResource) {
             $longestSiteResource = strlen($site->getSiteResourcesPackageKey());
         }
     }
     $this->outputLine();
     $this->outputLine(' ' . str_pad('Name', $longestSiteName + 15) . str_pad('Node name', $longestNodeName + 15) . 'Resources package');
     $this->outputLine(str_repeat('-', $longestSiteName + $longestNodeName + $longestSiteResource + 15 + 15 + 2));
     foreach ($availableSites as $site) {
         $this->outputLine(' ' . str_pad($site['name'], $longestSiteName + 15) . str_pad($site['nodeName'], $longestNodeName + 15) . $site['siteResourcesPackageKey']);
     }
     $this->outputLine();
 }
 /**
  * Initializes the context after all dependencies have been injected.
  *
  * @return void
  */
 public function initializeObject()
 {
     $this->locale = new Locale('mul_ZZ');
     $activeRequestHandler = $this->bootstrap->getActiveRequestHandler();
     if ($activeRequestHandler instanceof \TYPO3\FLOW3\Http\HttpRequestHandlerInterface) {
         $matchingDomains = $this->domainRepository->findByHost($activeRequestHandler->getHttpRequest()->getUri()->getHost());
         if (count($matchingDomains) > 0) {
             $this->currentDomain = $matchingDomains[0];
             $this->currentSite = $matchingDomains[0]->getSite();
         } else {
             $this->currentSite = $this->siteRepository->findFirst();
         }
     } else {
         $this->currentSite = $this->siteRepository->findFirst();
     }
 }
 /**
  * Add a domain record
  *
  * @param string $siteNodeName The nodeName of the site rootNode, e.g. "phoenixtypo3org"
  * @param string $hostPattern The host pattern to match on, e.g. "phoenix.typo3.org"
  * @return void
  */
 public function addCommand($siteNodeName, $hostPattern)
 {
     $site = $this->siteRepository->findOneByNodeName($siteNodeName);
     if (!$site instanceof Site) {
         $this->outputLine('No site found with nodeName "%s".', array($siteNodeName));
         $this->quit(1);
     }
     $domains = $this->domainRepository->findByHostPattern($hostPattern);
     if ($domains->count() > 0) {
         $this->outputLine('The host pattern "%s" is not unique', array($hostPattern));
         $this->quit(1);
     }
     $domain = new Domain();
     $domain->setSite($site);
     $domain->setHostPattern($hostPattern);
     $this->domainRepository->add($domain);
     $this->outputLine('Domain created');
 }
 /**
  * @param string $workspaceName
  * @return void
  * @todo Pagination
  * @todo Tree filtering + level limit
  * @todo Search field
  * @todo Difference mechanism
  */
 public function indexAction($workspaceName = NULL)
 {
     if (is_null($workspaceName)) {
         $workspaceName = $this->securityContext->getParty()->getPreferences()->get('context.workspace');
     }
     $contentContext = new \TYPO3\TYPO3\Domain\Service\ContentContext($workspaceName);
     $contentContext->setInvisibleContentShown(TRUE);
     $contentContext->setRemovedContentShown(TRUE);
     $contentContext->setInaccessibleContentShown(TRUE);
     $this->nodeRepository->setContext($contentContext);
     $sites = array();
     foreach ($this->workspacesService->getUnpublishedNodes($workspaceName) as $node) {
         if (!$node->getContentType()->isOfType('TYPO3.Phoenix.ContentTypes:Section')) {
             $pathParts = explode('/', $node->getPath());
             if (count($pathParts) > 2) {
                 $siteNodeName = $pathParts[2];
                 $folder = $this->findFolderNode($node);
                 $folderPath = implode('/', array_slice(explode('/', $folder->getPath()), 3));
                 $relativePath = str_replace(sprintf('/sites/%s/%s', $siteNodeName, $folderPath), '', $node->getPath());
                 if (!isset($sites[$siteNodeName]['siteNode'])) {
                     $sites[$siteNodeName]['siteNode'] = $this->siteRepository->findOneByNodeName($siteNodeName);
                 }
                 $sites[$siteNodeName]['folders'][$folderPath]['folderNode'] = $folder;
                 $change = array('node' => $node);
                 if ($node->getContentType()->isOfType('TYPO3.Phoenix.ContentTypes:AbstractNode')) {
                     $change['configuration'] = $node->getContentType()->getConfiguration();
                 }
                 $sites[$siteNodeName]['folders'][$folderPath]['changes'][$relativePath] = $change;
             }
         }
     }
     $liveWorkspace = $this->workspacesService->getWorkspace('live');
     ksort($sites);
     foreach ($sites as $siteKey => $site) {
         foreach ($site['folders'] as $folderKey => $folder) {
             foreach ($folder['changes'] as $changeKey => $change) {
                 $liveNode = $this->nodeRepository->findOneByIdentifier($change['node']->getIdentifier(), $liveWorkspace);
                 $sites[$siteKey]['folders'][$folderKey]['changes'][$changeKey]['isNew'] = is_null($liveNode);
                 $sites[$siteKey]['folders'][$folderKey]['changes'][$changeKey]['isMoved'] = $liveNode && $change['node']->getPath() !== $liveNode->getPath();
             }
         }
         ksort($sites[$siteKey]['folders']);
     }
     $workspaces = array();
     foreach ($this->workspacesService->getWorkspaces() as $workspace) {
         array_push($workspaces, array('workspaceNode' => $workspace, 'unpublishedNodesCount' => $this->workspacesService->getUnpublishedNodesCount($workspace->getName())));
     }
     $this->view->assignMultiple(array('workspaceName' => $workspaceName, 'workspaces' => $workspaces, 'sites' => $sites));
 }
 /**
  * @param string $pathAndFilename
  * @return void
  * @throws \TYPO3\FLOW3\Package\Exception\UnknownPackageException
  * @throws \TYPO3\FLOW3\Package\Exception\InvalidPackageStateException
  */
 public function importSitesFromFile($pathAndFilename)
 {
     $contentContext = $this->nodeRepository->getContext();
     $contentContext->setInvisibleContentShown(TRUE);
     $contentContext->setInaccessibleContentShown(TRUE);
     // no file_get_contents here because it does not work on php://stdin
     $fp = fopen($pathAndFilename, 'rb');
     $xmlString = '';
     while (!feof($fp)) {
         $xmlString .= fread($fp, 4096);
     }
     fclose($fp);
     $xml = new \SimpleXMLElement($xmlString);
     foreach ($xml->site as $siteXml) {
         $site = $this->siteRepository->findOneByNodeName((string) $siteXml['nodeName']);
         if ($site === NULL) {
             $site = new \TYPO3\TYPO3\Domain\Model\Site((string) $siteXml['nodeName']);
             $this->siteRepository->add($site);
         } else {
             $this->siteRepository->update($site);
         }
         $site->setName((string) $siteXml->properties->name);
         $site->setState((int) $siteXml->properties->state);
         $siteResourcesPackageKey = (string) $siteXml->properties->siteResourcesPackageKey;
         if ($this->packageManager->isPackageAvailable($siteResourcesPackageKey) === FALSE) {
             throw new \TYPO3\FLOW3\Package\Exception\UnknownPackageException('Package "' . $siteResourcesPackageKey . '" specified in the XML as site resources package does not exist.', 1303891443);
         }
         if ($this->packageManager->isPackageActive($siteResourcesPackageKey) === FALSE) {
             throw new \TYPO3\FLOW3\Package\Exception\InvalidPackageStateException('Package "' . $siteResourcesPackageKey . '" specified in the XML as site resources package is not active.', 1303898135);
         }
         $site->setSiteResourcesPackageKey($siteResourcesPackageKey);
         $rootNode = $contentContext->getWorkspace()->getRootNode();
         if ($rootNode->getNode('/sites') === NULL) {
             $rootNode->createSingleNode('sites');
         }
         $siteNode = $rootNode->getNode('/sites/' . $site->getNodeName());
         if ($siteNode === NULL) {
             $siteNode = $rootNode->getNode('/sites')->createSingleNode($site->getNodeName());
         }
         $siteNode->setContentObject($site);
         $this->parseNodes($siteXml, $siteNode);
     }
 }
 /**
  * Imports the specified bundle into the configured "importRootNodePath".
  *
  * @param string $bundle
  * @return void
  */
 protected function importBundle($bundle)
 {
     $this->outputLine('Importing bundle "%s"', array($bundle));
     $renderedDocumentationRootPath = rtrim($this->bundleConfiguration['renderedDocumentationRootPath'], '/');
     $contentContext = new \TYPO3\TYPO3\Domain\Service\ContentContext('live');
     $contentContext->setInvisibleContentShown(TRUE);
     $siteNode = $contentContext->getCurrentSiteNode();
     $importRootNode = $siteNode->getNode($this->bundleConfiguration['importRootNodePath']);
     if ($importRootNode === NULL) {
         $this->output('ImportRootNode "%s" does not exist!', array($this->bundleConfiguration['importRootNodePath']));
         $this->quit(1);
     }
     if (!is_dir($renderedDocumentationRootPath)) {
         $this->outputLine('The folder "%s" does not exist. Did you render the documentation?', array($renderedDocumentationRootPath));
         $this->quit(1);
     }
     $unorderedJsonFileNames = \TYPO3\FLOW3\Utility\Files::readDirectoryRecursively($renderedDocumentationRootPath, '.fjson');
     if ($unorderedJsonFileNames === array()) {
         $this->outputLine('The folder "%s" contains no fjson files. Did you render the documentation?', array($renderedDocumentationRootPath));
         $this->quit(1);
     }
     $orderedNodePaths = array();
     foreach ($unorderedJsonFileNames as $jsonPathAndFileName) {
         if (basename($jsonPathAndFileName) === 'Index.fjson') {
             $chapterRelativeNodePath = substr($jsonPathAndFileName, strlen($renderedDocumentationRootPath), -12) . '/';
             #				$orderedNodePaths[] = $this->normalizeNodePath(substr($chapterRelativeNodePath, 0, -1));
             $indexArray = json_decode(file_get_contents($jsonPathAndFileName), TRUE);
             foreach (explode(chr(10), $indexArray['body']) as $tocHtmlLine) {
                 preg_match('!^\\<li class="toctree-l1"\\>\\<a class="reference internal" href="\\.\\./([a-zA-Z0-9]+)/.*$!', $tocHtmlLine, $matches);
                 if ($matches !== array()) {
                     $orderedNodePaths[] = $this->normalizeNodePath($chapterRelativeNodePath . $matches[1]);
                 }
             }
         }
     }
     foreach ($unorderedJsonFileNames as $jsonPathAndFileName) {
         $data = json_decode(file_get_contents($jsonPathAndFileName));
         if (!isset($data->body)) {
             continue;
         }
         $relativeNodePath = substr($jsonPathAndFileName, strlen($renderedDocumentationRootPath) + 1, -6);
         $relativeNodePath = $this->normalizeNodePath($relativeNodePath);
         $segments = explode('/', $relativeNodePath);
         $pageNode = $importRootNode;
         while ($segment = array_shift($segments)) {
             $nodeName = preg_replace('/[^a-z0-9\\-]/', '', $segment);
             $subPageNode = $pageNode->getNode($nodeName);
             if ($subPageNode === NULL) {
                 $this->outputLine('Creating page node "%s"', array($relativeNodePath));
                 $subPageNode = $pageNode->createNode($nodeName, 'TYPO3.TYPO3:Page');
                 if (!$subPageNode->hasProperty('title')) {
                     $subPageNode->setProperty('title', $nodeName);
                 }
             }
             $pageNode = $subPageNode;
         }
         $sectionNode = $pageNode->getNode('main');
         if ($sectionNode === NULL) {
             $this->outputLine('Creating section node "%s"', array($relativeNodePath . '/main'));
             $sectionNode = $pageNode->createNode('main', 'TYPO3.TYPO3:Section');
         }
         $textNode = $sectionNode->getNode('text1');
         if ($textNode === NULL) {
             $this->outputLine('Creating text node "%s"', array($relativeNodePath . '/main/text1'));
             $textNode = $sectionNode->createNode('text1', 'TYPO3.TYPO3:Text');
         }
         $pageNode->setProperty('title', $data->title);
         $this->outputLine('Setting page title of page "%s" to "%s"', array($relativeNodePath, $data->title));
         $bodyText = $this->prepareBodyText($data->body, $relativeNodePath);
         $textNode->setProperty('text', $bodyText);
     }
     $importRootNodePath = $importRootNode->getPath();
     $currentParentNodePath = '';
     foreach ($orderedNodePaths as $nodePath) {
         $node = $importRootNode->getNode($importRootNodePath . $nodePath);
         if ($node !== NULL) {
             if ($node->getParent()->getPath() !== $currentParentNodePath) {
                 $currentParentNodePath = $node->getParent()->getPath();
                 $previousNode = NULL;
             }
             if ($previousNode !== NULL) {
                 $this->outputLine('Moved node %s', array($node->getPath()));
                 $this->outputLine('after node %s', array($previousNode->getPath()));
                 $node->moveAfter($previousNode);
             } else {
                 // FIXME: Node->isFirst() or Node->moveFirst() would be needed here
             }
             $previousNode = $node;
         } else {
             $this->outputLine('Node %s does not exist.', array($importRootNodePath . $nodePath));
         }
     }
     $this->siteRepository->update($contentContext->getCurrentSite());
 }