SitemapCommandController.php 5.3 KB
Newer Older
Dominique Feyer's avatar
Dominique Feyer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
<?php
namespace Ttree\Neos\Sitemap\Command;

use Tackk\Cartographer\ChangeFrequency;
use TYPO3\Eel\FlowQuery\FlowQuery;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Cli\CommandController;
use TYPO3\Flow\Http\Request;
use TYPO3\Flow\Http\Response;
use TYPO3\Flow\Http\Uri;
use TYPO3\Flow\Mvc\ActionRequest;
use TYPO3\Flow\Mvc\Controller\Arguments;
use TYPO3\Flow\Mvc\Controller\ControllerContext;
use TYPO3\Flow\Mvc\Routing\UriBuilder;
use TYPO3\Flow\Utility\Files;
use TYPO3\Neos\Domain\Repository\SiteRepository;
use TYPO3\Neos\Service\LinkingService;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\TYPO3CR\Domain\Service\ContextFactoryInterface;

class SitemapCommandController extends CommandController {

    /**
     * @var ContextFactoryInterface
     * @Flow\Inject
     */
    protected $contextFactory;

    /**
     * @var SiteRepository
     * @Flow\Inject
     */
    protected $siteRepository;

    /**
     * @Flow\Inject
     * @var LinkingService
     */
    protected $linkingService;

    /**
     * @Flow\Inject
     * @var UriBuilder
     */
    protected $uriBuilder;

    /**
     * @Flow\InjectConfiguration("presets")
     * @var array
     */
    protected $presets;

    /**
     * @param string $siteNodeName
     * @param string $baseUrl
     * @param string $preset
     */
    public function generateCommand($siteNodeName, $baseUrl, $preset = 'default') {
        if (!isset($this->presets[$preset])) {
            $this->outputLine('Preset "%s" not found ...', [$preset]);
            $this->sendAndExit(1);
        }
        $preset = $this->presets[$preset];
        $processedUri = [];
        $sitemap = new \Tackk\Cartographer\Sitemap();

        $context = $this->createContext('live', $siteNodeName);
        $controllerContext = $this->createControllerContext($baseUrl);

        foreach ($preset['include'] as $nodeType => $nodeTypeStatus) {
            if ($nodeTypeStatus !== TRUE) {
                continue;
            }
            $flowQuery = new FlowQuery(array($context->getRootNode()));
            /** @var NodeInterface $node */
            foreach ($flowQuery->find(sprintf('[instanceof %s]', $nodeType)) as $node) {
                if ($this->needToSkipNode($preset, $node)) {
                    continue;
                }
                if (trim($node->getProperty('uriPathSegment')) === '') {
                    $this->outputLine('Skip "%s" (%s), uriPathSegment empty', [$node->getLabel(), $node->getPath()]);
                    continue;
                }
                $uri = $this->linkingService->createNodeUri($controllerContext, $node, NULL, 'html', TRUE);
                $hash = md5($uri);
                if (isset($processedUri[$hash])) {
                    $this->outputLine('Skip "%s" (%s), duplicate URI (%s)', [$node->getLabel(), $node->getPath(), $uri]);
                    continue;
                }
                $lastPublicationDateTime = $node->getNodeData()->getLastPublicationDateTime() ?: $node->getNodeData()->getLastModificationDateTime();
                $sitemap->add($uri, $lastPublicationDateTime ? $lastPublicationDateTime->format('Y-m-d') : null, ChangeFrequency::WEEKLY, 1.0);
                $processedUri[$hash] = TRUE;
            }
        }

        $pathAndFilename = FLOW_PATH_DATA . '/Persistent/Sitemaps/sitemap.xml';
        Files::createDirectoryRecursively(dirname($pathAndFilename));
        file_put_contents($pathAndFilename, (string)$sitemap);
    }

    /**
     * @param array $preset
     * @param NodeInterface $node
     * @return boolean
     */
    protected function needToSkipNode(array $preset, NodeInterface $node) {
        if (isset($preset['skip']) && is_array($preset['skip'])) {
            foreach ($preset['skip'] as $nodeType => $nodeTypeStatus) {
                if ($nodeTypeStatus !== TRUE) {
                    continue;
                }
                if ($node->getNodeType()->isOfType($nodeType) === TRUE) {
                    return TRUE;
                }
            }
        }
        return FALSE;
    }

    /**
     * @param string $baseUrl
     * @return ControllerContext
     */
    protected function createControllerContext($baseUrl) {
        $httpRequest = Request::create(new Uri($baseUrl));
        $response = new Response();
        $arguments = new Arguments([]);
        return new ControllerContext(new ActionRequest($httpRequest), $response, $arguments, $this->uriBuilder);
    }

    /**
     * Creates a content context for given workspace and language identifiers
     *
     * @param string $workspaceName
     * @param string $siteNodeName
     * @param array $languageIdentifiers
     * @return \TYPO3\TYPO3CR\Domain\Service\Context
     */
    protected function createContext($workspaceName, $siteNodeName, array $languageIdentifiers = NULL) {
        $site = $this->siteRepository->findOneByNodeName($siteNodeName);
        $contextProperties = array(
            'workspaceName' => $workspaceName,
            'currentSite' => $site,
            'invisibleContentShown' => TRUE,
            'inaccessibleContentShown' => TRUE
        );
        if ($languageIdentifiers !== NULL) {
            $contextProperties = array_merge($contextProperties, array(
                'dimensions' => array('language' => $languageIdentifiers)
            ));
        }
        return $this->contextFactory->create($contextProperties);
    }


}