/**
  * @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());
 }