TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Controller\PageTsConfig;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Backend\Attribute\AsController;
|
||||
use TYPO3\CMS\Backend\Module\ModuleData;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
||||
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
|
||||
use TYPO3\CMS\Backend\Utility\BackendUtility;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\Http\RedirectResponse;
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
use TYPO3\CMS\Core\Site\Entity\Site;
|
||||
use TYPO3\CMS\Core\TypoScript\AST\Traverser\AstTraverser;
|
||||
use TYPO3\CMS\Core\TypoScript\AST\Visitor\AstSortChildrenVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\RootInclude;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\SiteInclude;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\TsConfigInclude;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Traverser\ConditionVerdictAwareIncludeTreeTraverser;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Traverser\IncludeTreeTraverser;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\TsConfigTreeBuilder;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeAstBuilderVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeCommentAwareAstBuilderVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeConditionAggregatorVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeConditionEnforcerVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSetupConditionConstantSubstitutionVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\Tokenizer\LosslessTokenizer;
|
||||
|
||||
/**
|
||||
* Page TSconfig > Active page TSconfig
|
||||
*
|
||||
* @internal This class is a specific Backend controller implementation and is not part of the TYPO3's Core API.
|
||||
*/
|
||||
#[AsController]
|
||||
final readonly class PageTsConfigActiveController
|
||||
{
|
||||
public function __construct(
|
||||
private ContainerInterface $container,
|
||||
private UriBuilder $uriBuilder,
|
||||
private ModuleTemplateFactory $moduleTemplateFactory,
|
||||
private TsConfigTreeBuilder $tsConfigTreeBuilder,
|
||||
) {}
|
||||
|
||||
public function handleRequest(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$backendUser = $this->getBackendUser();
|
||||
$languageService = $this->getLanguageService();
|
||||
|
||||
$queryParams = $request->getQueryParams();
|
||||
$parsedBody = $request->getParsedBody();
|
||||
|
||||
$currentModule = $request->getAttribute('module');
|
||||
$currentModuleIdentifier = $currentModule->getIdentifier();
|
||||
$moduleData = $request->getAttribute('moduleData');
|
||||
|
||||
$pageUid = (int)($parsedBody['id'] ?? $queryParams['id'] ?? 0);
|
||||
$pageRecord = BackendUtility::readPageAccess($pageUid, '1=1') ?: [];
|
||||
if (empty($pageRecord)) {
|
||||
// Redirect to overview if page could not be determined.
|
||||
// Edge case if page has been removed meanwhile.
|
||||
BackendUtility::setUpdateSignal('updatePageTree');
|
||||
return new RedirectResponse($this->uriBuilder->buildUriFromRoute('pagetsconfig_pages'));
|
||||
}
|
||||
|
||||
// Force boolean toggles to bool and init further get/post vars
|
||||
if ($moduleData->clean('displayConstantSubstitutions', [true, false])) {
|
||||
$backendUser->pushModuleData($currentModuleIdentifier, $moduleData->toArray());
|
||||
}
|
||||
$displayConstantSubstitutions = $moduleData->get('displayConstantSubstitutions');
|
||||
if ($moduleData->clean('displayComments', [true, false])) {
|
||||
$backendUser->pushModuleData($currentModuleIdentifier, $moduleData->toArray());
|
||||
}
|
||||
$displayComments = $moduleData->get('displayComments');
|
||||
if ($moduleData->clean('sortAlphabetically', [true, false])) {
|
||||
$backendUser->pushModuleData($currentModuleIdentifier, $moduleData->toArray());
|
||||
}
|
||||
$sortAlphabetically = $moduleData->get('sortAlphabetically');
|
||||
|
||||
// Prepare site constants if any
|
||||
$site = $request->getAttribute('site');
|
||||
$siteSettingsAst = null;
|
||||
$siteSettingsFlat = [];
|
||||
if ($site instanceof Site && !$site->getSettings()->isEmpty()) {
|
||||
$siteSettings = $site->getSettings()->getAllFlat();
|
||||
$siteConstants = '';
|
||||
foreach ($siteSettings as $nodeIdentifier => $value) {
|
||||
$siteConstants .= $nodeIdentifier . ' = ' . $value . LF;
|
||||
}
|
||||
$siteSettingsNode = new SiteInclude();
|
||||
$siteSettingsNode->setName('Site constants settings of site "' . $site->getIdentifier() . '"');
|
||||
$siteSettingsNode->setLineStream((new LosslessTokenizer())->tokenize($siteConstants));
|
||||
$siteSettingsTreeRoot = new RootInclude();
|
||||
$siteSettingsTreeRoot->addChild($siteSettingsNode);
|
||||
$astBuilderVisitor = $this->container->get(IncludeTreeAstBuilderVisitor::class);
|
||||
$includeTreeTraverser = new IncludeTreeTraverser();
|
||||
$includeTreeTraverser->traverse($siteSettingsTreeRoot, [$astBuilderVisitor]);
|
||||
$siteSettingsAst = $astBuilderVisitor->getAst();
|
||||
// Trigger unique identifier creation for entire tree
|
||||
$siteSettingsAst->setIdentifier('pageTsConfig-siteSettingsAst');
|
||||
$siteSettingsFlat = $siteSettingsAst->flatten();
|
||||
if ($sortAlphabetically) {
|
||||
// Traverse AST to sort if needed
|
||||
$astTraverser = new AstTraverser();
|
||||
$astTraverser->traverse($siteSettingsAst, [new AstSortChildrenVisitor()]);
|
||||
}
|
||||
}
|
||||
|
||||
// Base page TSconfig tree
|
||||
$rootLine = BackendUtility::BEgetRootLine($pageUid, '', true);
|
||||
ksort($rootLine);
|
||||
$pagesTsConfigTree = $this->tsConfigTreeBuilder->getPagesTsConfigTree($rootLine, new LosslessTokenizer());
|
||||
|
||||
// Overload tree with user TSconfig if any
|
||||
$userTsConfig = $backendUser->getUserTsConfig();
|
||||
if ($userTsConfig === null) {
|
||||
throw new \RuntimeException('User TSconfig not initialized', 1674609098);
|
||||
}
|
||||
$userTsConfigAst = $userTsConfig->getUserTsConfigTree();
|
||||
$userTsConfigPageOverrides = '';
|
||||
// @todo: Ugly, similar in PageTsConfigFactory.
|
||||
$userTsConfigFlat = $userTsConfigAst->flatten();
|
||||
foreach ($userTsConfigFlat as $userTsConfigIdentifier => $userTsConfigValue) {
|
||||
if (str_starts_with($userTsConfigIdentifier, 'page.')) {
|
||||
$userTsConfigPageOverrides .= substr($userTsConfigIdentifier, 5) . ' = ' . $userTsConfigValue . chr(10);
|
||||
}
|
||||
}
|
||||
if (!empty($userTsConfigPageOverrides)) {
|
||||
$includeNode = new TsConfigInclude();
|
||||
$includeNode->setName('pageTsConfig-overrides-by-userTsConfig');
|
||||
$includeNode->setLineStream((new LosslessTokenizer())->tokenize($userTsConfigPageOverrides));
|
||||
$pagesTsConfigTree->addChild($includeNode);
|
||||
}
|
||||
|
||||
// Set enabled conditions in page TSconfig include tree and let it handle constant substitutions in page TSconfig conditions.
|
||||
$pageTsConfigConditions = $this->handleToggledPageTsConfigConditions($pagesTsConfigTree, $moduleData, $parsedBody, $siteSettingsFlat);
|
||||
$conditionEnforcerVisitor = new IncludeTreeConditionEnforcerVisitor();
|
||||
$conditionEnforcerVisitor->setEnabledConditions(array_column(array_filter($pageTsConfigConditions, static fn(array $condition): bool => (bool)$condition['active']), 'value'));
|
||||
$treeTraverser = new IncludeTreeTraverser();
|
||||
$treeTraverser->traverse($pagesTsConfigTree, [$conditionEnforcerVisitor]);
|
||||
|
||||
// Create AST with constants from site and conditions
|
||||
$includeTreeTraverser = new ConditionVerdictAwareIncludeTreeTraverser();
|
||||
$astBuilderVisitor = $this->container->get(IncludeTreeCommentAwareAstBuilderVisitor::class);
|
||||
$astBuilderVisitor->setFlatConstants($siteSettingsFlat);
|
||||
$includeTreeTraverser->traverse($pagesTsConfigTree, [$astBuilderVisitor]);
|
||||
$pageTsConfigAst = $astBuilderVisitor->getAst();
|
||||
// Trigger unique identifier creation for entire tree
|
||||
$pageTsConfigAst->setIdentifier('pageTsConfig');
|
||||
if ($sortAlphabetically) {
|
||||
// Traverse AST to sort if needed
|
||||
$astTraverser = new AstTraverser();
|
||||
$astTraverser->traverse($pageTsConfigAst, [new AstSortChildrenVisitor()]);
|
||||
}
|
||||
|
||||
$view = $this->moduleTemplateFactory->create($request);
|
||||
$view->setTitle($languageService->sL($currentModule->getTitle()), $pageRecord['title'] ?? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] ?? '');
|
||||
$view->getDocHeaderComponent()->setPageBreadcrumb($pageRecord);
|
||||
$shortcutTitle = sprintf(
|
||||
'%s: %s [%d]',
|
||||
$languageService->translate('title', 'backend.modules.pagetsconfig_active'),
|
||||
BackendUtility::getRecordTitle('pages', $pageRecord),
|
||||
$pageUid
|
||||
);
|
||||
$view->getDocHeaderComponent()->setShortcutContext(
|
||||
$currentModuleIdentifier,
|
||||
$shortcutTitle,
|
||||
['id' => $pageUid]
|
||||
);
|
||||
$view->makeDocHeaderModuleMenu(['id' => $pageUid]);
|
||||
$view->assignMultiple([
|
||||
'pageUid' => $pageUid,
|
||||
'pageTitle' => $pageRecord['title'] ?? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] ?? '',
|
||||
'displayConstantSubstitutions' => $displayConstantSubstitutions,
|
||||
'displayComments' => $displayComments,
|
||||
'sortAlphabetically' => $sortAlphabetically,
|
||||
'siteSettingsAst' => $siteSettingsAst,
|
||||
'pageTsConfigAst' => $pageTsConfigAst,
|
||||
'pageTsConfigConditions' => $pageTsConfigConditions,
|
||||
'pageTsConfigConditionsActiveCount' => count(array_filter($pageTsConfigConditions, static fn(array $condition): bool => (bool)$condition['active'])),
|
||||
]);
|
||||
return $view->renderResponse('PageTsConfig/Active');
|
||||
}
|
||||
|
||||
/**
|
||||
* Align module data active page TSconfig conditions with toggled conditions from POST,
|
||||
* write updated active conditions to user's module data if needed and
|
||||
* prepare a list of active conditions for view.
|
||||
*/
|
||||
private function handleToggledPageTsConfigConditions(RootInclude $pageTsConfigTree, ModuleData $moduleData, ?array $parsedBody, array $flattenedConstants): array
|
||||
{
|
||||
$treeTraverser = new IncludeTreeTraverser();
|
||||
$treeTraverserVisitors = [];
|
||||
$setupConditionConstantSubstitutionVisitor = new IncludeTreeSetupConditionConstantSubstitutionVisitor();
|
||||
$setupConditionConstantSubstitutionVisitor->setFlattenedConstants($flattenedConstants);
|
||||
$treeTraverserVisitors[] = $setupConditionConstantSubstitutionVisitor;
|
||||
$conditionAggregatorVisitor = new IncludeTreeConditionAggregatorVisitor();
|
||||
$treeTraverserVisitors[] = $conditionAggregatorVisitor;
|
||||
$treeTraverser->traverse($pageTsConfigTree, $treeTraverserVisitors);
|
||||
$pageTsConfigConditions = $conditionAggregatorVisitor->getConditions();
|
||||
$conditionsFromPost = $parsedBody['pageTsConfigConditions'] ?? [];
|
||||
$conditionsFromModuleData = array_flip((array)$moduleData->get('pageTsConfigConditions'));
|
||||
$conditions = [];
|
||||
foreach ($pageTsConfigConditions as $condition) {
|
||||
$conditionHash = hash('xxh3', $condition['value']);
|
||||
$conditionActive = array_key_exists($conditionHash, $conditionsFromModuleData);
|
||||
// Note we're not feeding the post values directly to module data, but filter
|
||||
// them through available conditions to prevent polluting module data with
|
||||
// manipulated post values.
|
||||
if (($conditionsFromPost[$conditionHash] ?? null) === '0') {
|
||||
unset($conditionsFromModuleData[$conditionHash]);
|
||||
$conditionActive = false;
|
||||
} elseif (($conditionsFromPost[$conditionHash] ?? null) === '1') {
|
||||
$conditionsFromModuleData[$conditionHash] = true;
|
||||
$conditionActive = true;
|
||||
}
|
||||
$conditions[] = [
|
||||
'value' => $condition['value'],
|
||||
'originalValue' => $condition['originalValue'],
|
||||
'hash' => $conditionHash,
|
||||
'active' => $conditionActive,
|
||||
];
|
||||
}
|
||||
if ($conditionsFromPost) {
|
||||
$moduleData->set('pageTsConfigConditions', array_keys($conditionsFromModuleData));
|
||||
$this->getBackendUser()->pushModuleData($moduleData->getModuleIdentifier(), $moduleData->toArray());
|
||||
}
|
||||
return $conditions;
|
||||
}
|
||||
|
||||
private function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
|
||||
private function getBackendUser(): BackendUserAuthentication
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Controller\PageTsConfig;
|
||||
|
||||
use Psr\Http\Message\ResponseFactoryInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\StreamFactoryInterface;
|
||||
use TYPO3\CMS\Backend\Attribute\AsController;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
||||
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
|
||||
use TYPO3\CMS\Backend\Utility\BackendUtility;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\Http\RedirectResponse;
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
use TYPO3\CMS\Core\Site\Entity\Site;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\RootInclude;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\SiteInclude;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\TsConfigInclude;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Traverser\IncludeTreeTraverser;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\TsConfigTreeBuilder;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeNodeFinderVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSourceAggregatorVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSyntaxScannerVisitor;
|
||||
use TYPO3\CMS\Core\TypoScript\Tokenizer\LosslessTokenizer;
|
||||
|
||||
/**
|
||||
* Page TSconfig > Included page TSconfig
|
||||
*
|
||||
* @internal This class is a specific Backend controller implementation and is not part of the TYPO3's Core API.
|
||||
*/
|
||||
#[AsController]
|
||||
final readonly class PageTsConfigIncludesController
|
||||
{
|
||||
public function __construct(
|
||||
private UriBuilder $uriBuilder,
|
||||
private ModuleTemplateFactory $moduleTemplateFactory,
|
||||
private TsConfigTreeBuilder $tsConfigTreeBuilder,
|
||||
private ResponseFactoryInterface $responseFactory,
|
||||
private StreamFactoryInterface $streamFactory,
|
||||
) {}
|
||||
|
||||
public function indexAction(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$backendUser = $this->getBackendUser();
|
||||
$languageService = $this->getLanguageService();
|
||||
|
||||
$queryParams = $request->getQueryParams();
|
||||
|
||||
$currentModule = $request->getAttribute('module');
|
||||
$currentModuleIdentifier = $currentModule->getIdentifier();
|
||||
|
||||
$pageUid = (int)($queryParams['id'] ?? 0);
|
||||
$pageRecord = BackendUtility::readPageAccess($pageUid, '1=1') ?: [];
|
||||
if (empty($pageRecord)) {
|
||||
// Redirect to records overview if page could not be determined.
|
||||
// Edge case if page has been removed meanwhile.
|
||||
BackendUtility::setUpdateSignal('updatePageTree');
|
||||
return new RedirectResponse($this->uriBuilder->buildUriFromRoute('pagetsconfig_pages'));
|
||||
}
|
||||
|
||||
// Prepare site constants if any
|
||||
$site = $request->getAttribute('site');
|
||||
$siteSettingsTree = new RootInclude();
|
||||
if ($site instanceof Site && !$site->getSettings()->isEmpty()) {
|
||||
$siteSettings = $site->getSettings()->getAllFlat();
|
||||
$siteConstants = '';
|
||||
foreach ($siteSettings as $nodeIdentifier => $value) {
|
||||
$siteConstants .= $nodeIdentifier . ' = ' . $value . LF;
|
||||
}
|
||||
$siteSettingsNode = new SiteInclude();
|
||||
$siteSettingsNode->setName('Site constants settings of site "' . $site->getIdentifier() . '"');
|
||||
$siteSettingsNode->setLineStream((new LosslessTokenizer())->tokenize($siteConstants));
|
||||
$siteSettingsTree->addChild($siteSettingsNode);
|
||||
$siteSettingsTree->setIdentifier('pageTsConfig-siteSettingsTree');
|
||||
}
|
||||
|
||||
// Base page TSconfig tree
|
||||
$rootLine = BackendUtility::BEgetRootLine($pageUid, '', true);
|
||||
ksort($rootLine);
|
||||
$pageTsConfigTree = $this->tsConfigTreeBuilder->getPagesTsConfigTree($rootLine, new LosslessTokenizer());
|
||||
|
||||
// Overload tree with user TSconfig if any
|
||||
$userTsConfig = $backendUser->getUserTsConfig();
|
||||
if ($userTsConfig === null) {
|
||||
throw new \RuntimeException('User TSconfig not initialized', 1675535278);
|
||||
}
|
||||
$userTsConfigAst = $userTsConfig->getUserTsConfigTree();
|
||||
$userTsConfigPageOverrides = '';
|
||||
// @todo: Ugly, similar in PageTsConfigFactory.
|
||||
$userTsConfigFlat = $userTsConfigAst->flatten();
|
||||
foreach ($userTsConfigFlat as $userTsConfigIdentifier => $userTsConfigValue) {
|
||||
if (str_starts_with($userTsConfigIdentifier, 'page.')) {
|
||||
$userTsConfigPageOverrides .= substr($userTsConfigIdentifier, 5) . ' = ' . $userTsConfigValue . chr(10);
|
||||
}
|
||||
}
|
||||
if (!empty($userTsConfigPageOverrides)) {
|
||||
$includeNode = new TsConfigInclude();
|
||||
$includeNode->setName('pageTsConfig-overrides-by-userTsConfig');
|
||||
$includeNode->setLineStream((new LosslessTokenizer())->tokenize($userTsConfigPageOverrides));
|
||||
$pageTsConfigTree->addChild($includeNode);
|
||||
}
|
||||
$pageTsConfigTree->setIdentifier('pageTsConfig-pageTsConfigTree');
|
||||
|
||||
$treeTraverser = new IncludeTreeTraverser();
|
||||
$treeTraverserVisitors = [];
|
||||
$syntaxScannerVisitor = new IncludeTreeSyntaxScannerVisitor();
|
||||
$treeTraverserVisitors[] = $syntaxScannerVisitor;
|
||||
$treeTraverser->traverse($pageTsConfigTree, $treeTraverserVisitors);
|
||||
|
||||
$view = $this->moduleTemplateFactory->create($request);
|
||||
$view->setTitle($languageService->sL($currentModule->getTitle()), $pageRecord['title'] ?? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] ?? '');
|
||||
$view->getDocHeaderComponent()->setPageBreadcrumb($pageRecord);
|
||||
$shortcutTitle = sprintf(
|
||||
'%s: %s [%d]',
|
||||
$languageService->translate('title', 'backend.modules.pagetsconfig_includes'),
|
||||
BackendUtility::getRecordTitle('pages', $pageRecord),
|
||||
$pageUid
|
||||
);
|
||||
$view->getDocHeaderComponent()->setShortcutContext(
|
||||
$currentModuleIdentifier,
|
||||
$shortcutTitle,
|
||||
['id' => $pageUid]
|
||||
);
|
||||
$view->makeDocHeaderModuleMenu(['id' => $pageUid]);
|
||||
$view->assignMultiple([
|
||||
'pageUid' => $pageUid,
|
||||
'pageTitle' => $pageRecord['title'] ?? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] ?? '',
|
||||
'siteSettingsTree' => $siteSettingsTree,
|
||||
'pageTsConfigTree' => $pageTsConfigTree,
|
||||
'syntaxErrors' => $syntaxScannerVisitor->getErrors(),
|
||||
'syntaxErrorCount' => count($syntaxScannerVisitor->getErrors()),
|
||||
]);
|
||||
return $view->renderResponse('PageTsConfig/Includes');
|
||||
}
|
||||
|
||||
public function sourceAction(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$backendUser = $this->getBackendUser();
|
||||
$queryParams = $request->getQueryParams();
|
||||
$pageUid = (int)($queryParams['id'] ?? 0);
|
||||
$type = $queryParams['includeType'] ?? null;
|
||||
$includeIdentifier = $queryParams['identifier'] ?? null;
|
||||
if ($pageUid === 0 || $includeIdentifier === null || !in_array($type, ['constants', 'setup'], true)) {
|
||||
return $this->responseFactory->createResponse(400);
|
||||
}
|
||||
|
||||
if ($type === 'constants') {
|
||||
// Prepare site constants if any
|
||||
$site = $request->getAttribute('site');
|
||||
$includeTree = new RootInclude();
|
||||
if ($site instanceof Site && !$site->getSettings()->isEmpty()) {
|
||||
$siteSettings = $site->getSettings()->getAllFlat();
|
||||
$siteConstants = '';
|
||||
foreach ($siteSettings as $nodeIdentifier => $value) {
|
||||
$siteConstants .= $nodeIdentifier . ' = ' . $value . LF;
|
||||
}
|
||||
$siteSettingsNode = new SiteInclude();
|
||||
$siteSettingsNode->setName('Site constants settings of site "' . $site->getIdentifier() . '"');
|
||||
$siteSettingsNode->setLineStream((new LosslessTokenizer())->tokenize($siteConstants));
|
||||
$includeTree->addChild($siteSettingsNode);
|
||||
$includeTree->setIdentifier('pageTsConfig-siteSettingsTree');
|
||||
}
|
||||
} else {
|
||||
// Base page TSconfig tree
|
||||
$rootLine = BackendUtility::BEgetRootLine($pageUid, '', true);
|
||||
ksort($rootLine);
|
||||
$includeTree = $this->tsConfigTreeBuilder->getPagesTsConfigTree($rootLine, new LosslessTokenizer());
|
||||
|
||||
// Overload tree with user TSconfig if any
|
||||
$userTsConfig = $backendUser->getUserTsConfig();
|
||||
if ($userTsConfig === null) {
|
||||
throw new \RuntimeException('UserTsConfig not initialized', 1675535279);
|
||||
}
|
||||
$userTsConfigAst = $userTsConfig->getUserTsConfigTree();
|
||||
$userTsConfigPageOverrides = '';
|
||||
// @todo: Ugly, similar in PageTsConfigFactory.
|
||||
$userTsConfigFlat = $userTsConfigAst->flatten();
|
||||
foreach ($userTsConfigFlat as $userTsConfigIdentifier => $userTsConfigValue) {
|
||||
if (str_starts_with($userTsConfigIdentifier, 'page.')) {
|
||||
$userTsConfigPageOverrides .= substr($userTsConfigIdentifier, 5) . ' = ' . $userTsConfigValue . chr(10);
|
||||
}
|
||||
}
|
||||
if (!empty($userTsConfigPageOverrides)) {
|
||||
$includeNode = new TsConfigInclude();
|
||||
$includeNode->setName('pageTsConfig-overrides-by-userTsConfig');
|
||||
$includeNode->setLineStream((new LosslessTokenizer())->tokenize($userTsConfigPageOverrides));
|
||||
$includeTree->addChild($includeNode);
|
||||
}
|
||||
$includeTree->setIdentifier('pageTsConfig-pageTsConfigTree');
|
||||
}
|
||||
|
||||
$nodeFinderVisitor = new IncludeTreeNodeFinderVisitor();
|
||||
$nodeFinderVisitor->setNodeIdentifier($includeIdentifier);
|
||||
$treeTraverser = new IncludeTreeTraverser();
|
||||
$treeTraverser->traverse($includeTree, [$nodeFinderVisitor]);
|
||||
$lineStream = $nodeFinderVisitor->getFoundNode()?->getLineStream();
|
||||
if ($lineStream === null) {
|
||||
return $this->responseFactory->createResponse(400);
|
||||
}
|
||||
|
||||
return $this->responseFactory
|
||||
->createResponse()
|
||||
->withHeader('Content-Type', 'text/plain')
|
||||
->withBody($this->streamFactory->createStream((string)$lineStream));
|
||||
}
|
||||
|
||||
public function sourceWithIncludesAction(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$backendUser = $this->getBackendUser();
|
||||
$queryParams = $request->getQueryParams();
|
||||
$pageUid = (int)($queryParams['id'] ?? 0);
|
||||
$type = $queryParams['includeType'] ?? null;
|
||||
$includeIdentifier = $queryParams['identifier'] ?? null;
|
||||
if ($pageUid === 0 || $includeIdentifier === null || !in_array($type, ['constants', 'setup'], true)) {
|
||||
return $this->responseFactory->createResponse(400);
|
||||
}
|
||||
|
||||
if ($type === 'constants') {
|
||||
// Prepare site constants if any
|
||||
$site = $request->getAttribute('site');
|
||||
$includeTree = new RootInclude();
|
||||
if ($site instanceof Site && !$site->getSettings()->isEmpty()) {
|
||||
$siteSettings = $site->getSettings()->getAllFlat();
|
||||
$siteConstants = '';
|
||||
foreach ($siteSettings as $nodeIdentifier => $value) {
|
||||
$siteConstants .= $nodeIdentifier . ' = ' . $value . LF;
|
||||
}
|
||||
$siteSettingsNode = new SiteInclude();
|
||||
$siteSettingsNode->setName('Site constants settings of site "' . $site->getIdentifier() . '"');
|
||||
$siteSettingsNode->setLineStream((new LosslessTokenizer())->tokenize($siteConstants));
|
||||
$includeTree->addChild($siteSettingsNode);
|
||||
$includeTree->setIdentifier('pageTsConfig-siteSettingsTree');
|
||||
}
|
||||
} else {
|
||||
// Base page TSconfig tree
|
||||
$rootLine = BackendUtility::BEgetRootLine($pageUid, '', true);
|
||||
ksort($rootLine);
|
||||
$includeTree = $this->tsConfigTreeBuilder->getPagesTsConfigTree($rootLine, new LosslessTokenizer());
|
||||
|
||||
// Overload tree with user TSconfig if any
|
||||
$userTsConfig = $backendUser->getUserTsConfig();
|
||||
if ($userTsConfig === null) {
|
||||
throw new \RuntimeException('UserTsConfig not initialized', 1675535280);
|
||||
}
|
||||
$userTsConfigAst = $userTsConfig->getUserTsConfigTree();
|
||||
$userTsConfigPageOverrides = '';
|
||||
// @todo: Ugly, similar in PageTsConfigFactory.
|
||||
$userTsConfigFlat = $userTsConfigAst->flatten();
|
||||
foreach ($userTsConfigFlat as $userTsConfigIdentifier => $userTsConfigValue) {
|
||||
if (str_starts_with($userTsConfigIdentifier, 'page.')) {
|
||||
$userTsConfigPageOverrides .= substr($userTsConfigIdentifier, 5) . ' = ' . $userTsConfigValue . chr(10);
|
||||
}
|
||||
}
|
||||
if (!empty($userTsConfigPageOverrides)) {
|
||||
$includeNode = new TsConfigInclude();
|
||||
$includeNode->setName('pageTsConfig-overrides-by-userTsConfig');
|
||||
$includeNode->setLineStream((new LosslessTokenizer())->tokenize($userTsConfigPageOverrides));
|
||||
$includeTree->addChild($includeNode);
|
||||
}
|
||||
$includeTree->setIdentifier('pageTsConfig-pageTsConfigTree');
|
||||
}
|
||||
|
||||
$sourceAggregatorVisitor = new IncludeTreeSourceAggregatorVisitor();
|
||||
$sourceAggregatorVisitor->setStartNodeIdentifier($includeIdentifier);
|
||||
$treeTraverser = new IncludeTreeTraverser();
|
||||
$treeTraverser->traverse($includeTree, [$sourceAggregatorVisitor]);
|
||||
$source = $sourceAggregatorVisitor->getSource();
|
||||
|
||||
return $this->responseFactory
|
||||
->createResponse()
|
||||
->withHeader('Content-Type', 'text/plain')
|
||||
->withBody($this->streamFactory->createStream($source));
|
||||
}
|
||||
|
||||
private function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
|
||||
private function getBackendUser(): BackendUserAuthentication
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Controller\PageTsConfig;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Backend\Attribute\AsController;
|
||||
use TYPO3\CMS\Backend\Module\ModuleInterface;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
||||
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
|
||||
use TYPO3\CMS\Backend\Utility\BackendUtility;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\Database\Connection;
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
|
||||
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
|
||||
use TYPO3\CMS\Core\Imaging\IconFactory;
|
||||
use TYPO3\CMS\Core\Imaging\IconSize;
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
use TYPO3\CMS\Core\Type\Bitmask\Permission;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\MathUtility;
|
||||
|
||||
/**
|
||||
* Page TSconfig > Page TSconfig Configuration
|
||||
*
|
||||
* @internal This class is a specific Backend controller implementation and is not part of the TYPO3's Core API.
|
||||
*/
|
||||
#[AsController]
|
||||
final readonly class PageTsConfigRecordsOverviewController
|
||||
{
|
||||
public function __construct(
|
||||
private IconFactory $iconFactory,
|
||||
private UriBuilder $uriBuilder,
|
||||
private ModuleTemplateFactory $moduleTemplateFactory,
|
||||
) {}
|
||||
|
||||
public function handleRequest(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$backendUser = $this->getBackendUser();
|
||||
$currentModule = $request->getAttribute('module');
|
||||
$currentModuleIdentifier = $currentModule->getIdentifier();
|
||||
$pageId = (int)($request->getQueryParams()['id'] ?? 0);
|
||||
$pageRecord = BackendUtility::readPageAccess($pageId, $backendUser->getPagePermsClause(Permission::PAGE_SHOW)) ?: [];
|
||||
|
||||
$moduleData = $request->getAttribute('moduleData');
|
||||
if ($moduleData->cleanUp([])) {
|
||||
$backendUser->pushModuleData($currentModuleIdentifier, $moduleData->toArray());
|
||||
}
|
||||
|
||||
$view = $this->moduleTemplateFactory->create($request);
|
||||
|
||||
$view->setTitle(
|
||||
$this->getLanguageService()->sL($currentModule->getTitle()),
|
||||
$pageId !== 0 && isset($pageRecord['title']) ? $pageRecord['title'] : ''
|
||||
);
|
||||
|
||||
// The page will show only if there is a valid page and if this page may be viewed by the user.
|
||||
if ($pageRecord !== []) {
|
||||
$view->getDocHeaderComponent()->setPageBreadcrumb($pageRecord);
|
||||
}
|
||||
|
||||
$accessContent = false;
|
||||
if (($pageId && $pageRecord !== []) || ($backendUser->isAdmin() && !$pageId)) {
|
||||
$accessContent = true;
|
||||
if (!$pageId && $backendUser->isAdmin()) {
|
||||
$pageRecord = ['title' => '[root-level]', 'uid' => 0, 'pid' => 0];
|
||||
}
|
||||
$view->assign('id', $pageId);
|
||||
// Setting up the buttons and the module menu for the doc header
|
||||
$view->getDocHeaderComponent()->setShortcutContext(
|
||||
$currentModule->getIdentifier(),
|
||||
$this->getLanguageService()->sL($currentModule->getTitle()),
|
||||
['id' => $pageId]
|
||||
);
|
||||
}
|
||||
$view->assign('accessContent', $accessContent);
|
||||
$pagesUsingTSConfig = $this->getOverviewOfPagesUsingTSConfig($currentModule);
|
||||
if (count($pagesUsingTSConfig) > 0) {
|
||||
$view->assign('overviewOfPagesUsingTSConfig', $pagesUsingTSConfig);
|
||||
}
|
||||
|
||||
$view->makeDocHeaderModuleMenu(['id' => $pageId]);
|
||||
return $view->renderResponse('PageTsConfig/RecordsOverview');
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders table rows of all pages containing TSConfig together with its rootline
|
||||
*/
|
||||
private function getOverviewOfPagesUsingTSConfig(ModuleInterface $currentModule): array
|
||||
{
|
||||
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
|
||||
$queryBuilder->getRestrictions()
|
||||
->removeAll()
|
||||
->add(GeneralUtility::makeInstance(DeletedRestriction::class))
|
||||
->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, 0));
|
||||
|
||||
$res = $queryBuilder
|
||||
->select('uid', 'TSconfig')
|
||||
->from('pages')
|
||||
->where(
|
||||
$queryBuilder->expr()->neq(
|
||||
'TSconfig',
|
||||
$queryBuilder->createNamedParameter('')
|
||||
),
|
||||
$queryBuilder->expr()->eq(
|
||||
'sys_language_uid',
|
||||
$queryBuilder->createNamedParameter(0, Connection::PARAM_INT)
|
||||
)
|
||||
)
|
||||
->executeQuery();
|
||||
|
||||
$pageArray = [];
|
||||
|
||||
while ($row = $res->fetchAssociative()) {
|
||||
$this->setInPageArray($pageArray, BackendUtility::BEgetRootLine($row['uid'], 'AND 1=1'), $row);
|
||||
}
|
||||
return $this->getList($currentModule, $pageArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a multidimensional array that reflects the page hierarchy.
|
||||
*/
|
||||
private function setInPageArray(array &$hierarchicArray, array $rootlineArray, array $row): void
|
||||
{
|
||||
ksort($rootlineArray);
|
||||
if (!($rootlineArray[0]['uid'] ?? false)) {
|
||||
array_shift($rootlineArray);
|
||||
}
|
||||
$currentElement = current($rootlineArray);
|
||||
$hierarchicArray[$currentElement['uid']] = htmlspecialchars($currentElement['title']);
|
||||
array_shift($rootlineArray);
|
||||
if (!empty($rootlineArray)) {
|
||||
if (!is_array($hierarchicArray[$currentElement['uid'] . '.'] ?? null)) {
|
||||
$hierarchicArray[$currentElement['uid'] . '.'] = [];
|
||||
}
|
||||
$this->setInPageArray($hierarchicArray[$currentElement['uid'] . '.'], $rootlineArray, $row);
|
||||
} else {
|
||||
$hierarchicArray[$currentElement['uid'] . '_'] = $this->extractLinesFromTSConfig($row);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the lines of TSConfig from a given pages row.
|
||||
*/
|
||||
private function extractLinesFromTSConfig(array $row): array
|
||||
{
|
||||
$out = [];
|
||||
$out['uid'] = $row['uid'];
|
||||
$lines = GeneralUtility::trimExplode("\r\n", $row['TSconfig']);
|
||||
$out['writtenLines'] = count($lines);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive method to get the list of pages to show.
|
||||
*/
|
||||
private function getList(ModuleInterface $currentModule, array $pageArray, array $lines = [], int $pageDepth = 0): array
|
||||
{
|
||||
if ($pageArray === []) {
|
||||
return $lines;
|
||||
}
|
||||
|
||||
foreach ($pageArray as $identifier => $title) {
|
||||
if (!MathUtility::canBeInterpretedAsInteger($identifier)) {
|
||||
continue;
|
||||
}
|
||||
$line = [];
|
||||
$line['padding'] = ($pageDepth * 20);
|
||||
$line['title'] = $identifier;
|
||||
if (isset($pageArray[$identifier . '_'])) {
|
||||
$line['link'] = $this->uriBuilder->buildUriFromRoute($currentModule->getIdentifier(), ['id' => $identifier]);
|
||||
$line['icon'] = $this->iconFactory->getIconForRecord('pages', BackendUtility::getRecordWSOL('pages', $identifier), IconSize::SMALL)->render();
|
||||
$line['pageTitle'] = GeneralUtility::fixed_lgd_cs($title, 30);
|
||||
$line['lines'] = ($pageArray[$identifier . '_']['writtenLines'] === 0 ? '' : $pageArray[$identifier . '_']['writtenLines']);
|
||||
} else {
|
||||
$line['link'] = '';
|
||||
$line['icon'] = $this->iconFactory->getIconForRecord('pages', BackendUtility::getRecordWSOL('pages', $identifier), IconSize::SMALL)->render();
|
||||
$line['pageTitle'] = GeneralUtility::fixed_lgd_cs($title, 30);
|
||||
$line['lines'] = '';
|
||||
}
|
||||
$lines[] = $line;
|
||||
$lines = $this->getList($currentModule, $pageArray[$identifier . '.'] ?? [], $lines, $pageDepth + 1);
|
||||
}
|
||||
return $lines;
|
||||
}
|
||||
|
||||
private function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
|
||||
private function getBackendUser(): BackendUserAuthentication
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user