‪TYPO3CMS  ‪main
CreateSiteConfiguration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Hooks;
19 
31 use ‪TYPO3\CMS\Core\SysLog\Action\Site as SiteAction;
32 use ‪TYPO3\CMS\Core\SysLog\Error as SystemLogErrorClassification;
36 
43 {
47  protected ‪$allowedPageTypes = [
51  ];
52 
53  public function ‪processDatamap_afterDatabaseOperations(string $status, string $table, $id, array $fieldValues, ‪DataHandler $dataHandler): void
54  {
67  if ($status !== 'new'
68  || $table !== 'pages'
69  || $dataHandler->BE_USER->workspace > 0
70  || !isset($dataHandler->substNEWwithIDs[$id])
71  || (int)$fieldValues['pid'] !== 0
72  || (int)$fieldValues['l10n_parent'] !== 0
73  || (isset($fieldValues['t3ver_oid']) && (int)$fieldValues['t3ver_oid'] > 0)
74  || !in_array((int)$fieldValues['doktype'], $this->allowedPageTypes, true)
75  || $dataHandler->isImporting
76  ) {
77  return;
78  }
79 
80  ‪$uid = (int)$dataHandler->substNEWwithIDs[$id];
81  $this->generateSiteConfigurationForRootPage(‪$uid, $dataHandler->BE_USER);
82  }
83 
84  protected function ‪generateSiteConfigurationForRootPage(int $pageId, ‪BackendUserAuthentication $backendUser): void
85  {
86  $entryPoint = 'autogenerated-' . $pageId;
87  ‪$siteIdentifier = $entryPoint . '-' . md5((string)$pageId);
88 
89  if (!$this->‪siteExistsByRootPageId($pageId)) {
90  $siteConfiguration = GeneralUtility::makeInstance(SiteConfiguration::class);
91  $normalizedParams = $this->‪getNormalizedParams();
92  $basePrefix = ‪Environment::isCli() ? $normalizedParams->getSitePath() : $normalizedParams->getSiteUrl();
93  try {
94  $siteConfiguration->createNewBasicSite(
96  $pageId,
97  $basePrefix . $entryPoint
98  );
99  $backendUser->‪writelog(‪Type::SITE, SiteAction::CREATE, SystemLogErrorClassification::MESSAGE, 0, 'Site configuration \'%s\' was automatically created for new root page (%s).', [‪$siteIdentifier, $pageId], 'site');
100  $this->‪updateSlugForPage($pageId);
102  $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $e->getMessage(), '', ContextualFeedbackSeverity::WARNING, true);
103  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
104  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
105  $defaultFlashMessageQueue->enqueue($flashMessage);
106  }
107  }
108  }
109 
110  protected function ‪getNormalizedParams(): ‪NormalizedParams
111  {
112  $normalizedParams = null;
113  $serverParams = ‪Environment::isCli() ? ['HTTP_HOST' => 'localhost'] : ‪$_SERVER;
114  if (isset(‪$GLOBALS['TYPO3_REQUEST'])) {
115  $normalizedParams = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams');
116  $serverParams = ‪$GLOBALS['TYPO3_REQUEST']->getServerParams();
117  }
118 
119  if (!$normalizedParams instanceof ‪NormalizedParams) {
120  $normalizedParams = ‪NormalizedParams::createFromServerParams($serverParams);
121  }
122 
123  return $normalizedParams;
124  }
125 
129  protected function ‪updateSlugForPage(int $pageId): void
130  {
131  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
132  $dataMap = [
133  'pages' => [
134  $pageId => [
135  'slug' => '',
136  ],
137  ],
138  ];
139  $dataHandler->start($dataMap, []);
140  $dataHandler->process_datamap();
141  }
142 
148  protected function ‪siteExistsByRootPageId(int $rootPageId): bool
149  {
150  try {
151  GeneralUtility::makeInstance(SiteFinder::class)->getSiteByRootPageId($rootPageId);
152  } catch (SiteNotFoundException $e) {
153  return false;
154  }
155  return true;
156  }
157 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Core\SysLog\Action\Site
Definition: Site.php:24
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\updateSlugForPage
‪updateSlugForPage(int $pageId)
Definition: CreateSiteConfiguration.php:128
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\siteExistsByRootPageId
‪siteExistsByRootPageId(int $rootPageId)
Definition: CreateSiteConfiguration.php:147
‪TYPO3\CMS\Core\Configuration\Exception\SiteConfigurationWriteException
Definition: SiteConfigurationWriteException.php:27
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_DEFAULT
‪const DOKTYPE_DEFAULT
Definition: PageRepository.php:98
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\generateSiteConfigurationForRootPage
‪generateSiteConfigurationForRootPage(int $pageId, BackendUserAuthentication $backendUser)
Definition: CreateSiteConfiguration.php:83
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_SHORTCUT
‪const DOKTYPE_SHORTCUT
Definition: PageRepository.php:100
‪TYPO3\CMS\Core\SysLog\Type\SITE
‪const SITE
Definition: Type.php:34
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_LINK
‪const DOKTYPE_LINK
Definition: PageRepository.php:99
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\$allowedPageTypes
‪int[] $allowedPageTypes
Definition: CreateSiteConfiguration.php:46
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration
Definition: CreateSiteConfiguration.php:43
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\processDatamap_afterDatabaseOperations
‪processDatamap_afterDatabaseOperations(string $status, string $table, $id, array $fieldValues, DataHandler $dataHandler)
Definition: CreateSiteConfiguration.php:52
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromServerParams
‪static static createFromServerParams(array $serverParams, array $systemConfiguration=null)
Definition: NormalizedParams.php:824
‪TYPO3\CMS\Core\SysLog\Error
Definition: Error.php:24
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\writelog
‪int writelog($type, $action, $error, $details_nr, $details, $data, $tablename='', $recuid='', $recpid='', $event_pid=-1, $NEWid='', $userId=0)
Definition: BackendUserAuthentication.php:1727
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Webhooks\Message\$siteIdentifier
‪identifier readonly int readonly array readonly string readonly string $siteIdentifier
Definition: PageModificationMessage.php:38
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:69
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\getNormalizedParams
‪getNormalizedParams()
Definition: CreateSiteConfiguration.php:109
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38
‪TYPO3\CMS\Core\SysLog\Type
Definition: Type.php:28
‪TYPO3\CMS\Core\Hooks
Definition: BackendUserGroupIntegrityCheck.php:18