forked from TYPO3-Documentation/t3docs-search-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TYPO3-Documentation#14 Aggregate only major versions and render label…
…s for highest versions This commit replaces the all versions aggregation by only major versions. There is also change for the search results list. Before labels for all versions assigned to the snippet were rendered. After the changes only the highest version in given branch will be rendered. For example, if snippet has versions `main`, `12.4`, `12.0`, `11.5`, `11.3`, `11.0`, `9.5`, only labels for `main`,`12.4`, `11.5`, `9.5` will be rendered. Resolves issue: TYPO3-Documentation#14
- Loading branch information
Marcin Sągol
committed
Feb 15, 2024
1 parent
99cf6a2
commit ca6a00e
Showing
8 changed files
with
185 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ snippet_content: | |
analyzer: typo3_analyzer | ||
content_hash: | ||
type: keyword | ||
major_versions: | ||
type: keyword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace App\Helper; | ||
|
||
class VersionFilter | ||
{ | ||
/** | ||
* Filters an array of version strings to return only the highest version for each major version. | ||
* | ||
* This function groups the provided versions by their major version number, then sorts and selects | ||
* the highest version within each major version group. It is assumed that the version strings are in | ||
* the format 'major.minor' or similar. The function returns an array of the highest versions for | ||
* each major version. | ||
* | ||
* @param array<string> $versions | ||
*/ | ||
public static function filterVersions(array $versions): array | ||
{ | ||
$groupedVersions = []; | ||
$nonNumericVersions = []; | ||
|
||
foreach ($versions as $version) { | ||
if (!is_numeric(str_replace('.', '', $version))) { | ||
$nonNumericVersions[] = $version; | ||
continue; | ||
} | ||
|
||
$majorVersion = explode('.', $version)[0]; | ||
$groupedVersions[$majorVersion][] = $version; | ||
} | ||
|
||
$highestVersions = []; | ||
|
||
foreach ($groupedVersions as $minorVersions) { | ||
usort($minorVersions, static function($a, $b) { | ||
return version_compare($b, $a); | ||
}); | ||
$highestVersions[] = $minorVersions[0]; | ||
} | ||
|
||
return array_merge($highestVersions, $nonNumericVersions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Helper; | ||
|
||
use App\Helper\VersionFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class VersionFilterTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function emptyArray(): void | ||
{ | ||
self::assertSame([], VersionFilter::filterVersions([])); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function singleVersion(): void | ||
{ | ||
self::assertSame(['12.1'], VersionFilter::filterVersions(['12.1'])); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function multipleVersionsOneMajor(): void | ||
{ | ||
$versions = ['12.1', '12.4', '12.2']; | ||
$expected = ['12.4']; | ||
|
||
self::assertSame($expected, VersionFilter::filterVersions($versions)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function multipleVersionsMultipleMajors(): void | ||
{ | ||
$versions = ['12.1', '12.4', '11.3', '11.5', '10.1']; | ||
$expected = ['12.4', '11.5', '10.1']; | ||
|
||
self::assertSame($expected, VersionFilter::filterVersions($versions)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function unorderedVersions(): void | ||
{ | ||
$versions = ['11.3', '12.1', '11.5', '12.4', '10.1']; | ||
$expected = ['11.5', '12.4', '10.1']; | ||
|
||
self::assertSame($expected, VersionFilter::filterVersions($versions)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function versionsWithSameMajorAndMinor(): void | ||
{ | ||
$versions = ['12.0', '12.0', '11.0', '11.1']; | ||
$expected = ['12.0', '11.1']; | ||
|
||
self::assertSame($expected, VersionFilter::filterVersions($versions)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function versionsWithOnlyMajor(): void | ||
{ | ||
$versions = ['12', '12', '11', '10']; | ||
$expected = ['12', '11', '10']; | ||
|
||
self::assertSame($expected, VersionFilter::filterVersions($versions)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function nonNumericVersions(): void | ||
{ | ||
$versions = ['12.1', 'main', '11.2', 'master', '11.3']; | ||
$expected = ['12.1', '11.3', 'main', 'master']; | ||
|
||
$this->assertSame($expected, VersionFilter::filterVersions($versions)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function onlyNonNumericVersions(): void | ||
{ | ||
$versions = ['main', 'master']; | ||
$expected = ['main', 'master']; | ||
|
||
$this->assertSame($expected, VersionFilter::filterVersions($versions)); | ||
} | ||
} |