Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onResourceLoaderGetConfigVars | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| getWikibaseInitData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| onBeforePageDisplay | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
| onGetPreferences | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Hooks for Citoid extension |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Extension\Citoid; |
| 10 | |
| 11 | use MediaWiki\Config\Config; |
| 12 | use MediaWiki\Config\ConfigFactory; |
| 13 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 14 | use MediaWiki\Output\OutputPage; |
| 15 | use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 16 | use MediaWiki\Registration\ExtensionRegistry; |
| 17 | use MediaWiki\ResourceLoader as RL; |
| 18 | use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook; |
| 19 | use MediaWiki\Skin\Skin; |
| 20 | use MediaWiki\User\User; |
| 21 | use MobileContext; |
| 22 | use Wikibase\Lib\Store\EntityNamespaceLookup; |
| 23 | |
| 24 | class Hooks implements |
| 25 | ResourceLoaderGetConfigVarsHook, |
| 26 | BeforePageDisplayHook, |
| 27 | GetPreferencesHook |
| 28 | { |
| 29 | |
| 30 | public function __construct( |
| 31 | private readonly ConfigFactory $configFactory, |
| 32 | private readonly ExtensionRegistry $extensionRegistry, |
| 33 | private readonly ?EntityNamespaceLookup $entityNamespaceLookup, |
| 34 | private readonly ?MobileContext $mobileContext, |
| 35 | ) { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Adds extra variables to the global config |
| 40 | * @param array &$vars |
| 41 | * @param string $skin |
| 42 | * @param Config $config |
| 43 | */ |
| 44 | public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void { |
| 45 | $citoidConfig = $this->configFactory->makeConfig( 'citoid' ); |
| 46 | |
| 47 | $vars['wgCitoidConfig'] = [ |
| 48 | 'citoidServiceUrl' => $citoidConfig->get( 'CitoidServiceUrl' ), |
| 49 | /** @deprecated since 1.46 */ |
| 50 | 'fullRestbaseUrl' => $citoidConfig->get( 'CitoidFullRestbaseURL' ), |
| 51 | 'isbnScannerEnabled' => $citoidConfig->get( 'CitoidIsbnScannerEnabled' ), |
| 52 | /** @deprecated since 1.46 */ |
| 53 | 'wbFullRestbaseUrl' => $citoidConfig->get( 'WBCitoidFullRestbaseURL' ), |
| 54 | ]; |
| 55 | |
| 56 | // Hard deprecate |
| 57 | if ( $vars['wgCitoidConfig']['wbFullRestbaseUrl'] ) { |
| 58 | wfDeprecated( '$wgWBCitoidFullRestbaseURL', '1.46' ); |
| 59 | } |
| 60 | |
| 61 | if ( $vars['wgCitoidConfig']['fullRestbaseUrl'] ) { |
| 62 | wfDeprecated( '$wgCitoidFullRestbaseURL', '1.46' ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Virtual data file of 'ext.citoid.wikibase.init' module. |
| 68 | * |
| 69 | * @param RL\Context $context |
| 70 | * @return string[] |
| 71 | */ |
| 72 | public static function getWikibaseInitData( RL\Context $context ) { |
| 73 | return [ |
| 74 | 'toolConfig' => $context->msg( 'citoid-wikibase-config.json' ) |
| 75 | ->inContentLanguage() |
| 76 | ->plain(), |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Loads front-end wikibase citoid module |
| 82 | * @param OutputPage $out |
| 83 | * @param Skin $skin |
| 84 | */ |
| 85 | public function onBeforePageDisplay( $out, $skin ): void { |
| 86 | if ( !$this->extensionRegistry->isLoaded( 'WikibaseRepository' ) ) { |
| 87 | return; |
| 88 | } |
| 89 | if ( $this->entityNamespaceLookup->isEntityNamespace( $out->getTitle()->getNamespace() ) ) { |
| 90 | $isMobileView = $this->extensionRegistry->isLoaded( 'MobileFrontend' ) && |
| 91 | $this->mobileContext->shouldDisplayMobileView(); |
| 92 | if ( !$isMobileView ) { |
| 93 | $out->addModules( 'ext.citoid.wikibase.init' ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param User $user |
| 100 | * @param array &$preferences |
| 101 | */ |
| 102 | public function onGetPreferences( $user, &$preferences ) { |
| 103 | $preferences['citoid-mode'] = [ |
| 104 | 'type' => 'api' |
| 105 | ]; |
| 106 | } |
| 107 | } |