Commit 5ddb8ebd authored by Sybille Peters's avatar Sybille Peters 🙋 Committed by Benni Mack
Browse files

[TASK] Always handle linktypes in linkvalidator as list

The TSconfig of linkvalidator contains 'linktypes' which is a
list of link types which should be checked
(e.g. 'external','db' ...).

This list of links can have 2 different formats inside internal code:

['external', 'db']
or

[
   'external' => 1,
   'db' => 1
]

Converting this in various places made the code hard to read and error
prone.

Only the first format is now used, which also makes it possible to
simplify the tests.

Resolves: #92715
Releases: master
Change-Id: I7451f707da15a20b21b5fefe5df50ed455a5b733
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/66319

Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Tymoteusz Motylewski's avatarTymoteusz Motylewski <t.motylewski@gmail.com>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 48fd54c4
......@@ -119,19 +119,19 @@ class LinkAnalyzer
/**
* Find all supported broken links and store them in tx_linkvalidator_link
*
* @param array $checkOptions List of hook object to activate
* @param array<int,string> $linkTypes List of hook object to activate
* @param bool $considerHidden Defines whether to look into hidden fields
*/
public function getLinkStatistics($checkOptions = [], $considerHidden = false)
public function getLinkStatistics(array $linkTypes = [], $considerHidden = false)
{
$results = [];
if (empty($checkOptions) || empty($this->pids)) {
if (empty($linkTypes) || empty($this->pids)) {
return;
}
$this->brokenLinkRepository->removeAllBrokenLinksOfRecordsOnPageIds(
$this->pids,
array_keys($checkOptions)
$linkTypes
);
// Traverse all configured tables
......@@ -176,13 +176,17 @@ class LinkAnalyzer
$this->analyzeRecord($results, $table, $fields, $row);
}
}
$this->checkLinks($results, $checkOptions);
$this->checkLinks($results, $linkTypes);
}
protected function checkLinks(array $links, array $checkOptions)
/**
* @param array $links
* @param array<int,string> $linkTypes
*/
protected function checkLinks(array $links, array $linkTypes)
{
foreach ($this->hookObjectsArr as $key => $hookObj) {
if (!is_array($links[$key]) || (!empty($checkOptions) && !$checkOptions[$key])) {
if (!is_array($links[$key]) || (!in_array($key, $linkTypes, true))) {
continue;
}
......
......@@ -386,7 +386,14 @@ class LinkValidatorReport
*/
protected function updateBrokenLinks()
{
$this->linkAnalyzer->getLinkStatistics($this->checkOpt['check'], $this->modTS['checkhidden']);
// convert ['external' => 1, 'db' => 0, ...] into ['external']
$linkTypes = [];
foreach ($this->checkOpt['check'] as $linkType => $value) {
if ($value) {
$linkTypes[] = $linkType;
}
}
$this->linkAnalyzer->getLinkStatistics($linkTypes, $this->modTS['checkhidden']);
}
/**
......
......@@ -152,6 +152,10 @@ class BrokenLinkRepository
->execute();
}
/**
* @param array $pageIds
* @param array<int,string> $linkTypes
*/
public function removeAllBrokenLinksOfRecordsOnPageIds(array $pageIds, array $linkTypes): void
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
......
......@@ -161,7 +161,7 @@ class LinkAnalyzerResult
$this->brokenLinks = $this->brokenLinkRepository->getAllBrokenLinksForPages(
$pageIds,
array_keys($linkTypes),
$linkTypes,
$searchFields,
$languageIds
);
......
......@@ -406,7 +406,7 @@ class ValidatorTask extends AbstractTask
$typesTmp = GeneralUtility::trimExplode(',', $this->modTSconfig['linktypes'], true);
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] ?? [] as $type => $value) {
if (in_array($type, $typesTmp, true)) {
$linkTypes[$type] = 1;
$linkTypes[] = (string)$type;
}
}
return $linkTypes;
......
......@@ -88,13 +88,6 @@ class LinkAnalyzerTest extends FunctionalTestCase
*/
public function getLinkStatisticsFindAllBrokenLinks(string $inputFile, string $pidList, string $expectedOutputFile)
{
$config = [
'db' => '1',
'file' => '1',
'external' => '1'
];
$tsConfig = [
'searchFields' => [
'pages' => ['media', 'url', 'canonical_link'],
......@@ -103,6 +96,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
'linktypes' => 'db,file,external',
'checkhidden' => '0',
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields'];
......@@ -113,7 +107,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
new BrokenLinkRepository()
);
$linkAnalyzer->init($searchFields, $pidList, $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$this->assertCSVDataSet($expectedOutputFile);
}
......@@ -149,13 +143,6 @@ class LinkAnalyzerTest extends FunctionalTestCase
*/
public function getLinkStatisticsFindOnlyFileBrokenLinks(string $inputFile, string $pidList, string $expectedOutputFile)
{
$config = [
'db' => '0',
'file' => '1',
'external' => '0'
];
$tsConfig = [
'searchFields' => [
'pages' => ['media', 'url'],
......@@ -164,6 +151,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
'linktypes' => 'file',
'checkhidden' => '0',
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields'];
......@@ -174,7 +162,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
new BrokenLinkRepository()
);
$linkAnalyzer->init($searchFields, $pidList, $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$this->assertCSVDataSet($expectedOutputFile);
}
......@@ -210,13 +198,6 @@ class LinkAnalyzerTest extends FunctionalTestCase
*/
public function getLinkStatisticsFindOnlyPageBrokenLinks(string $inputFile, string $pidList, string $expectedOutputFile)
{
$config = [
'db' => '1',
'file' => '0',
'external' => '0'
];
$tsConfig = [
'searchFields' => [
'pages' => ['media', 'url'],
......@@ -225,6 +206,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
'linktypes' => 'db',
'checkhidden' => '0',
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields'];
......@@ -235,7 +217,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
new BrokenLinkRepository()
);
$linkAnalyzer->init($searchFields, $pidList, $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$this->assertCSVDataSet($expectedOutputFile);
}
......@@ -271,13 +253,6 @@ class LinkAnalyzerTest extends FunctionalTestCase
*/
public function getLinkStatisticsFindOnlyExternalBrokenLinksInBodytext(string $inputFile, string $pidList, string $expectedOutputFile)
{
$config = [
'db' => '0',
'file' => '0',
'external' => '1'
];
$tsConfig = [
'searchFields' => [
'tt_content' => ['bodytext']
......@@ -285,6 +260,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
'linktypes' => 'external',
'checkhidden' => '0',
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields'];
......@@ -295,7 +271,7 @@ class LinkAnalyzerTest extends FunctionalTestCase
new BrokenLinkRepository()
);
$linkAnalyzer->init($searchFields, $pidList, $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$this->assertCSVDataSet($expectedOutputFile);
}
......
......@@ -203,12 +203,6 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
array $pidList,
array $expectedOutput
) {
$config = [
'db' => '1',
'file' => '1',
'external' => '1',
];
$tsConfig = [
'searchFields.' => [
'pages' => 'media,url,canonical_link',
......@@ -217,6 +211,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
'linktypes' => 'db,file,external',
'checkhidden' => '0',
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields.'];
foreach ($searchFields as $table => $fields) {
......@@ -232,7 +227,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
$this->brokenLinksRepository
);
$linkAnalyzer->init($searchFields, implode(',', $pidList), $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$result = $this->brokenLinksRepository->getNumberOfBrokenLinksForRecordsOnPages(
$pidList,
$searchFields
......@@ -334,18 +329,6 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
array $pidList,
int $expectedCount
) {
$config = [
'db' => '1',
'file' => '1',
'external' => '1',
];
$linkTypes = [
'db',
'file',
'external'
];
$tsConfig = [
'searchFields.' => [
'pages' => 'media,url,canonical_link',
......@@ -354,6 +337,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
'linktypes' => 'db,file,external',
'checkhidden' => '0',
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields.'];
foreach ($searchFields as $table => $fields) {
......@@ -369,7 +353,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
$this->brokenLinksRepository
);
$linkAnalyzer->init($searchFields, implode(',', $pidList), $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$results = $this->brokenLinksRepository->getAllBrokenLinksForPages(
$pidList,
......@@ -647,18 +631,6 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
array $pidList,
array $expectedResult
) {
$config = [
'db' => '1',
'file' => '1',
'external' => '1',
];
$linkTypes = [
'db',
'file',
'external'
];
$tsConfig = [
'searchFields.' => [
'pages' => 'media,url,canonical_link',
......@@ -667,6 +639,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
'linktypes' => 'db,file,external',
'checkhidden' => '0'
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields.'];
foreach ($searchFields as $table => $fields) {
......@@ -682,7 +655,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
$this->brokenLinksRepository
);
$linkAnalyzer->init($searchFields, implode(',', $pidList), $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$results = $this->brokenLinksRepository->getAllBrokenLinksForPages(
$pidList,
......@@ -802,8 +775,6 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
array $languages,
array $expectedResult
): void {
$config = ['external' => '1'];
$linkTypes = ['external'];
$tsConfig = [
'searchFields.' => [
'tt_content' => 'bodytext'
......@@ -811,6 +782,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
'linktypes' => 'external',
'checkhidden' => '0'
];
$linkTypes = explode(',', $tsConfig['linktypes']);
$searchFields = $tsConfig['searchFields.'];
foreach ($searchFields as $table => $fields) {
......@@ -825,7 +797,7 @@ class BrokenLinkRepositoryTest extends FunctionalTestCase
$this->brokenLinksRepository
);
$linkAnalyzer->init($searchFields, implode(',', $pidList), $tsConfig);
$linkAnalyzer->getLinkStatistics($config);
$linkAnalyzer->getLinkStatistics($linkTypes);
$results = $this->brokenLinksRepository->getAllBrokenLinksForPages(
$pidList,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment