PK       ! î¶Û}˜  ˜    ReferencePreviewsContext.phpnu ÕIw¶´        <?php

namespace Cite\ReferencePreviews;

use MediaWiki\Config\Config;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\User;
use Skin;

/**
 * @license GPL-2.0-or-later
 */
class ReferencePreviewsContext {

	private Config $config;
	private ReferencePreviewsGadgetsIntegration $gadgetsIntegration;
	private UserOptionsLookup $userOptionsLookup;

	public function __construct(
		Config $config,
		ReferencePreviewsGadgetsIntegration $gadgetsIntegration,
		UserOptionsLookup $userOptionsLookup
	) {
		$this->config = $config;
		$this->gadgetsIntegration = $gadgetsIntegration;
		$this->userOptionsLookup = $userOptionsLookup;
	}

	/**
	 * User preference key to enable/disable Reference Previews. Named
	 * "mwe-popups-referencePreviews-enabled" in localStorage for anonymous users.
	 */
	public const REFERENCE_PREVIEWS_PREFERENCE_NAME = 'popups-reference-previews';

	/**
	 * If the client-side code for Reference Previews should continue loading
	 * (see isReferencePreviewsEnabled.js), incorporating decisions we can only make after the
	 * ResourceLoader module was registered via {@see CiteHooks::onResourceLoaderRegisterModules}.
	 */
	public function isReferencePreviewsEnabled( User $user, Skin $skin ): bool {
		if (
			// T243822: Temporarily disabled in the mobile skin
			$skin->getSkinName() === 'minerva' ||
			// The feature flag is also checked in the ResourceLoaderRegisterModules hook handler
			// and technically redundant here, but it's cheap; better safe than sorry
			!$this->config->get( 'CiteReferencePreviews' ) ||
			$this->gadgetsIntegration->isRefToolTipsGadgetEnabled( $user ) ||
			$this->gadgetsIntegration->isNavPopupsGadgetEnabled( $user )
		) {
			return false;
		}

		// Anonymous users can (de)activate the feature via a cookie at runtime, hence it must load
		return !$user->isNamed() || $this->userOptionsLookup->getBoolOption(
			$user, self::REFERENCE_PREVIEWS_PREFERENCE_NAME
		);
	}
}
PK       ! Uo—à  à  '  ReferencePreviewsGadgetsIntegration.phpnu ÕIw¶´        <?php

namespace Cite\ReferencePreviews;

use InvalidArgumentException;
use MediaWiki\Config\Config;
use MediaWiki\Extension\Gadgets\GadgetRepo;
use MediaWiki\User\UserIdentity;

/**
 * Gadgets integration
 *
 * @license GPL-2.0-or-later
 */
class ReferencePreviewsGadgetsIntegration {

	public const CONFIG_NAVIGATION_POPUPS_NAME = 'CiteReferencePreviewsConflictingNavPopupsGadgetName';
	public const CONFIG_REFERENCE_TOOLTIPS_NAME = 'CiteReferencePreviewsConflictingRefTooltipsGadgetName';

	private ?GadgetRepo $gadgetRepo;

	private string $navPopupsGadgetName;
	private string $refTooltipsGadgetName;

	public function __construct( Config $config, ?GadgetRepo $gadgetRepo ) {
		$this->navPopupsGadgetName = $this->sanitizeGadgetName(
			$config->get( self::CONFIG_NAVIGATION_POPUPS_NAME ) );
		$this->refTooltipsGadgetName = $this->sanitizeGadgetName(
			$config->get( self::CONFIG_REFERENCE_TOOLTIPS_NAME ) );

		$this->gadgetRepo = $gadgetRepo;
	}

	private function sanitizeGadgetName( string $gadgetName ): string {
		return str_replace( ' ', '_', trim( $gadgetName ) );
	}

	private function isGadgetEnabled( UserIdentity $user, string $gadgetName ): bool {
		if ( $this->gadgetRepo ) {
			if ( in_array( $gadgetName, $this->gadgetRepo->getGadgetIds() ) ) {
				try {
					return $this->gadgetRepo->getGadget( $gadgetName )
						->isEnabled( $user );
				} catch ( InvalidArgumentException $e ) {
					return false;
				}
			}
		}
		return false;
	}

	public function isNavPopupsGadgetEnabled( UserIdentity $user ): bool {
		return $this->isGadgetEnabled( $user, $this->navPopupsGadgetName );
	}

	public function isRefTooltipsGadgetEnabled( UserIdentity $user ): bool {
		return $this->isGadgetEnabled( $user, $this->refTooltipsGadgetName );
	}

}
PK         ! î¶Û}˜  ˜                  ReferencePreviewsContext.phpnu ÕIw¶´        PK         ! Uo—à  à  '            ä  ReferencePreviewsGadgetsIntegration.phpnu ÕIw¶´        PK      Ã       