Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
TemplateDiscoveryConfig
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 1
 getConfig
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3namespace MediaWiki\Extension\TemplateData;
4
5use Exception;
6use MediaWiki\Config\Config;
7use MediaWiki\Registration\ExtensionRegistry;
8use MediaWiki\ResourceLoader\Context;
9use MediaWiki\Title\Title;
10use MediaWiki\WikiMap\WikiMap;
11use Wikibase\Client\WikibaseClient;
12use Wikibase\DataModel\Entity\Item;
13use Wikibase\DataModel\Entity\ItemId;
14
15/**
16 * @license GPL-2.0-or-later
17 */
18class TemplateDiscoveryConfig {
19
20    public static function getConfig( Context $context, Config $config ): array {
21        $extRegistry = ExtensionRegistry::getInstance();
22
23        $categoryRootCat = null;
24        if ( $config->get( 'TemplateDataEnableCategoryBrowser' ) ) {
25            $catBrowserItemId = $config->get( 'TemplateDataCategoryBrowserItem' );
26            if ( $catBrowserItemId && $extRegistry->isLoaded( 'WikibaseClient' ) ) {
27                $entityLookup = WikibaseClient::getStore()->getEntityLookup();
28                $entity = $entityLookup->getEntity( new ItemId( $catBrowserItemId ) );
29                if ( $entity instanceof Item ) {
30                    // If Wikibase is installed, and the item exists, try to get the sitelink's page title.
31                    try {
32                        $pageName = $entity->getSiteLink( WikiMap::getCurrentWikiId() )->getPageName();
33                        $page = Title::newFromText( $pageName );
34                        if ( $page && $page->inNamespaces( NS_CATEGORY ) ) {
35                            $categoryRootCat = $page->getBaseText();
36                        }
37                    } catch ( Exception ) {
38                        // e.g. if the wiki ID doesn't exist.
39                    }
40                }
41            }
42        }
43        if ( $categoryRootCat === null ) {
44            // If Wikibase isn't installed, or no root cat was configured or able to be retrieved.
45            $categoryRootCat = $context->msg( 'templatedata-category-rootcat' )->inContentLanguage()->text();
46        }
47
48        return [
49            'cirrusSearchLoaded' => $extRegistry->isLoaded( 'CirrusSearch' ),
50            'communityConfigurationLoaded' => $extRegistry->isLoaded( 'CommunityConfiguration' ),
51            'maxFavorites' => $config->get( 'TemplateDataMaxFavorites' ),
52            'categoryRootCat' => $categoryRootCat,
53        ];
54    }
55
56}