Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SearchableNamespaceListBuilder | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCuratedNamespaces | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace AdvancedSearch; |
| 4 | |
| 5 | use MediaWiki\Language\ILanguageConverter; |
| 6 | |
| 7 | /** |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | class SearchableNamespaceListBuilder { |
| 11 | |
| 12 | /** |
| 13 | * @param ILanguageConverter $languageConverter |
| 14 | * @param callable|null $pagesInNs A function like {@see SiteStats::pagesInNs} that returns true |
| 15 | * or an int > 0 when there are pages in the namespace, i.e. it's not empty |
| 16 | */ |
| 17 | public function __construct( |
| 18 | private readonly ILanguageConverter $languageConverter, |
| 19 | private $pagesInNs, |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Get a curated list of namespaces. Adds Main namespace and removes unnamed namespaces |
| 25 | * @param array<int,string> $configNamespaces Mapping namespace ids to localized names |
| 26 | * @return array<int,string> |
| 27 | */ |
| 28 | public function getCuratedNamespaces( array $configNamespaces ): array { |
| 29 | foreach ( $configNamespaces as $id => &$name ) { |
| 30 | $name = $this->languageConverter->convertNamespace( $id ); |
| 31 | } |
| 32 | |
| 33 | // Make sure the main namespace is listed with a non-empty name |
| 34 | if ( isset( $configNamespaces[NS_MAIN] ) && !$configNamespaces[NS_MAIN] ) { |
| 35 | $configNamespaces[NS_MAIN] = wfMessage( 'blanknamespace' )->text(); |
| 36 | } |
| 37 | |
| 38 | // Remove entries that still have an empty name |
| 39 | $configNamespaces = array_filter( $configNamespaces ); |
| 40 | |
| 41 | if ( $this->pagesInNs ) { |
| 42 | foreach ( $configNamespaces as $ns => $_ ) { |
| 43 | if ( !( $this->pagesInNs )( $ns ) ) { |
| 44 | unset( $configNamespaces[$ns] ); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | ksort( $configNamespaces ); |
| 50 | return $configNamespaces; |
| 51 | } |
| 52 | |
| 53 | } |