|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Lolli\Dbdoctor\HealthCheck; |
| 6 | + |
| 7 | +/* |
| 8 | + * This file is part of the TYPO3 CMS project. |
| 9 | + * |
| 10 | + * It is free software; you can redistribute it and/or modify it under |
| 11 | + * the terms of the GNU General Public License, either version 2 |
| 12 | + * of the License, or any later version. |
| 13 | + * |
| 14 | + * For the full copyright and license information, please read the |
| 15 | + * LICENSE.txt file that was distributed with this source code. |
| 16 | + * |
| 17 | + * The TYPO3 project - inspiring people to share! |
| 18 | + */ |
| 19 | +use Lolli\Dbdoctor\Helper\TableHelper; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 | +use TYPO3\CMS\Core\Database\Connection; |
| 22 | +use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; |
| 23 | +use TYPO3\CMS\Core\Exception\SiteNotFoundException; |
| 24 | +use TYPO3\CMS\Core\Site\Entity\Site; |
| 25 | +use TYPO3\CMS\Core\Site\SiteFinder; |
| 26 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 27 | + |
| 28 | +/** |
| 29 | + * Translated records (except pages) need a corresponding page translation to exist. |
| 30 | + * A record translated to a language for which no page translation exists on the same |
| 31 | + * pid is orphaned and should be removed. This check only considers languages that are |
| 32 | + * configured in the site configuration. |
| 33 | + */ |
| 34 | +final class TcaTablesTranslatedLanguagePageTranslationMissing extends AbstractHealthCheck implements HealthCheckInterface |
| 35 | +{ |
| 36 | + private SiteFinder $siteFinder; |
| 37 | + |
| 38 | + public function __construct(SiteFinder $siteFinder) |
| 39 | + { |
| 40 | + $this->siteFinder = $siteFinder; |
| 41 | + } |
| 42 | + |
| 43 | + public function header(SymfonyStyle $io): void |
| 44 | + { |
| 45 | + $io->section('Scan for translated records without corresponding page translation'); |
| 46 | + $this->outputClass($io); |
| 47 | + $this->outputTags($io, self::TAG_SOFT_DELETE, self::TAG_REMOVE, self::TAG_WORKSPACE_REMOVE); |
| 48 | + $io->text([ |
| 49 | + 'Translated records need a corresponding page translation on their pid to be valid.', |
| 50 | + 'When a page translation for a given language does not exist, content records translated', |
| 51 | + 'to that language are orphaned. This check finds and removes such records. Only languages', |
| 52 | + 'configured in the site configuration are considered.', |
| 53 | + ]); |
| 54 | + } |
| 55 | + |
| 56 | + protected function getAffectedRecords(): array |
| 57 | + { |
| 58 | + /** @var TableHelper $tableHelper */ |
| 59 | + $tableHelper = $this->container->get(TableHelper::class); |
| 60 | + |
| 61 | + if (!$tableHelper->tableExistsInDatabase('pages')) { |
| 62 | + // pages table is required for page translation lookups. |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + /** @var array<int, Site|false> $siteCache */ |
| 67 | + $siteCache = []; |
| 68 | + /** @var array<string, array<int, true>> $siteLanguageCache */ |
| 69 | + $siteLanguageCache = []; |
| 70 | + /** @var array<int, array<int, true>> $pageTranslationCache */ |
| 71 | + $pageTranslationCache = []; |
| 72 | + |
| 73 | + $affectedRows = []; |
| 74 | + foreach ($this->tcaHelper->getNextLanguageAwareTcaTable(['pages']) as $tableName) { |
| 75 | + if (!$tableHelper->tableExistsInDatabase($tableName)) { |
| 76 | + // TCA may define tables not yet present in database schema. |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + /** @var string $languageField */ |
| 81 | + $languageField = $this->tcaHelper->getLanguageField($tableName); |
| 82 | + $workspaceIdField = $this->tcaHelper->getWorkspaceIdField($tableName); |
| 83 | + $isTableWorkspaceAware = !empty($workspaceIdField); |
| 84 | + |
| 85 | + $selectFields = [ |
| 86 | + 'uid', |
| 87 | + 'pid', |
| 88 | + $languageField, |
| 89 | + ]; |
| 90 | + if ($isTableWorkspaceAware) { |
| 91 | + $selectFields[] = $workspaceIdField; |
| 92 | + $selectFields[] = 't3ver_state'; |
| 93 | + } |
| 94 | + |
| 95 | + $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName); |
| 96 | + // Do not consider already deleted records: Those are not visible and will not cause |
| 97 | + // issues. Reducing the number of affected records avoids unnecessary noise. |
| 98 | + $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
| 99 | + $queryBuilder |
| 100 | + ->select(...$selectFields) |
| 101 | + ->from($tableName) |
| 102 | + ->where( |
| 103 | + $queryBuilder->expr()->gt($languageField, $queryBuilder->createNamedParameter(0, Connection::PARAM_INT)), |
| 104 | + $queryBuilder->expr()->neq($languageField, $queryBuilder->createNamedParameter(-1, Connection::PARAM_INT)) |
| 105 | + ) |
| 106 | + ->orderBy('uid'); |
| 107 | + |
| 108 | + if ($isTableWorkspaceAware) { |
| 109 | + // Skip DELETE_PLACEHOLDER records (t3ver_state = 2), those are workspace internals. |
| 110 | + $queryBuilder->andWhere( |
| 111 | + $queryBuilder->expr()->neq('t3ver_state', $queryBuilder->createNamedParameter(2, Connection::PARAM_INT)) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + $result = $queryBuilder->executeQuery(); |
| 116 | + while ($row = $result->fetchAssociative()) { |
| 117 | + /** @var array<string, int|string> $row */ |
| 118 | + $pid = (int)$row['pid']; |
| 119 | + $langId = (int)$row[$languageField]; |
| 120 | + |
| 121 | + // Resolve site for this pid, cached. Records on pages without site config |
| 122 | + // (e.g. pid 0 or pages not below a site root) are skipped: No site means |
| 123 | + // no language configuration to validate against. |
| 124 | + if (!array_key_exists($pid, $siteCache)) { |
| 125 | + try { |
| 126 | + $siteCache[$pid] = $this->siteFinder->getSiteByPageId($pid); |
| 127 | + } catch (SiteNotFoundException) { |
| 128 | + $siteCache[$pid] = false; |
| 129 | + } |
| 130 | + } |
| 131 | + if ($siteCache[$pid] === false) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + $site = $siteCache[$pid]; |
| 135 | + |
| 136 | + // Only check languages that are configured in the site. Records with a language |
| 137 | + // not in site config are handled by TcaTablesTranslatedLanguageNotInSiteConfiguration. |
| 138 | + $siteIdentifier = $site->getIdentifier(); |
| 139 | + if (!isset($siteLanguageCache[$siteIdentifier])) { |
| 140 | + $siteLanguageCache[$siteIdentifier] = []; |
| 141 | + foreach ($site->getAllLanguages() as $siteLanguage) { |
| 142 | + $siteLanguageCache[$siteIdentifier][$siteLanguage->getLanguageId()] = true; |
| 143 | + } |
| 144 | + } |
| 145 | + if (!isset($siteLanguageCache[$siteIdentifier][$langId])) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + // Check if a page translation exists for this pid and language, cached. |
| 150 | + // Do not consider deleted page translations: A deleted page translation means |
| 151 | + // content translations on that page are orphaned and should be removed. |
| 152 | + if (!isset($pageTranslationCache[$pid])) { |
| 153 | + $pageTranslationCache[$pid] = []; |
| 154 | + $pageQueryBuilder = $this->connectionPool->getQueryBuilderForTable('pages'); |
| 155 | + $pageQueryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
| 156 | + $pageResult = $pageQueryBuilder |
| 157 | + ->select('sys_language_uid') |
| 158 | + ->from('pages') |
| 159 | + ->where( |
| 160 | + $pageQueryBuilder->expr()->eq('l10n_parent', $pageQueryBuilder->createNamedParameter($pid, Connection::PARAM_INT)), |
| 161 | + $pageQueryBuilder->expr()->gt('sys_language_uid', $pageQueryBuilder->createNamedParameter(0, Connection::PARAM_INT)) |
| 162 | + ) |
| 163 | + ->executeQuery(); |
| 164 | + while ($pageRow = $pageResult->fetchAssociative()) { |
| 165 | + $pageTranslationCache[$pid][(int)$pageRow['sys_language_uid']] = true; |
| 166 | + } |
| 167 | + } |
| 168 | + if (!isset($pageTranslationCache[$pid][$langId])) { |
| 169 | + $row['_reasonBroken'] = 'MissingPageTranslation'; |
| 170 | + $affectedRows[$tableName][(int)$row['uid']] = $row; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + return $affectedRows; |
| 175 | + } |
| 176 | + |
| 177 | + protected function processRecords(SymfonyStyle $io, bool $simulate, array $affectedRecords): void |
| 178 | + { |
| 179 | + $this->softOrHardDeleteRecords($io, $simulate, $affectedRecords); |
| 180 | + } |
| 181 | + |
| 182 | + protected function recordDetails(SymfonyStyle $io, array $affectedRecords): void |
| 183 | + { |
| 184 | + $this->outputRecordDetails($io, $affectedRecords, '_reasonBroken'); |
| 185 | + } |
| 186 | +} |
0 commit comments