| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cite\ReferencePreviews; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Config\Config; |
| 7 |
use MediaWiki\Extension\Gadgets\GadgetRepo; |
| 8 |
use MediaWiki\User\UserIdentity; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class ReferencePreviewsGadgetsIntegration { |
| 16 |
|
| 17 |
public const CONFIG_NAVIGATION_POPUPS_NAME = 'CiteReferencePreviewsConflictingNavPopupsGadgetName'; |
| 18 |
public const CONFIG_REFERENCE_TOOLTIPS_NAME = 'CiteReferencePreviewsConflictingRefTooltipsGadgetName'; |
| 19 |
|
| 20 |
private ?GadgetRepo $gadgetRepo; |
| 21 |
|
| 22 |
private string $navPopupsGadgetName; |
| 23 |
private string $refTooltipsGadgetName; |
| 24 |
|
| 25 |
public function __construct( Config $config, ?GadgetRepo $gadgetRepo ) { |
| 26 |
$this->navPopupsGadgetName = $this->sanitizeGadgetName( |
| 27 |
$config->get( self::CONFIG_NAVIGATION_POPUPS_NAME ) ); |
| 28 |
$this->refTooltipsGadgetName = $this->sanitizeGadgetName( |
| 29 |
$config->get( self::CONFIG_REFERENCE_TOOLTIPS_NAME ) ); |
| 30 |
|
| 31 |
$this->gadgetRepo = $gadgetRepo; |
| 32 |
} |
| 33 |
|
| 34 |
private function sanitizeGadgetName( string $gadgetName ): string { |
| 35 |
return str_replace( ' ', '_', trim( $gadgetName ) ); |
| 36 |
} |
| 37 |
|
| 38 |
private function isGadgetEnabled( UserIdentity $user, string $gadgetName ): bool { |
| 39 |
if ( $this->gadgetRepo ) { |
| 40 |
if ( in_array( $gadgetName, $this->gadgetRepo->getGadgetIds() ) ) { |
| 41 |
try { |
| 42 |
return $this->gadgetRepo->getGadget( $gadgetName ) |
| 43 |
->isEnabled( $user ); |
| 44 |
} catch ( InvalidArgumentException $e ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
public function isNavPopupsGadgetEnabled( UserIdentity $user ): bool { |
| 53 |
return $this->isGadgetEnabled( $user, $this->navPopupsGadgetName ); |
| 54 |
} |
| 55 |
|
| 56 |
public function isRefTooltipsGadgetEnabled( UserIdentity $user ): bool { |
| 57 |
return $this->isGadgetEnabled( $user, $this->refTooltipsGadgetName ); |
| 58 |
} |
| 59 |
|
| 60 |
} |