2016-12-16 02:47:52 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension Popups.
|
|
|
|
*
|
|
|
|
* Popups is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Popups is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Popups. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup extensions
|
|
|
|
*/
|
|
|
|
namespace Popups;
|
|
|
|
|
2017-01-11 21:52:07 +00:00
|
|
|
use Config;
|
2020-01-26 19:22:03 +00:00
|
|
|
use ExtensionRegistry;
|
2021-10-01 01:31:05 +00:00
|
|
|
use MediaWiki\Extension\BetaFeatures\BetaFeatures;
|
2020-01-26 19:22:03 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-06-06 13:07:08 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2017-07-28 18:58:13 +00:00
|
|
|
use Title;
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Popups Module
|
|
|
|
*
|
|
|
|
* @package Popups
|
|
|
|
*/
|
|
|
|
class PopupsContext {
|
|
|
|
|
2019-10-10 14:53:30 +00:00
|
|
|
public const EXTENSION_NAME = 'popups';
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2019-02-26 08:56:07 +00:00
|
|
|
* Logger channel name
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
public const LOGGER_CHANNEL = 'popups';
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* User preference value for enabled Page Previews
|
|
|
|
*/
|
2021-06-06 13:07:08 +00:00
|
|
|
public const PREVIEWS_ENABLED = true;
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* User preference value for disabled Page Previews
|
|
|
|
*/
|
2021-06-06 13:07:08 +00:00
|
|
|
public const PREVIEWS_DISABLED = false;
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2018-04-26 20:43:05 +00:00
|
|
|
* User preference key to enable/disable Page Previews
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
public const PREVIEWS_OPTIN_PREFERENCE_NAME = 'popups';
|
2019-02-20 16:43:15 +00:00
|
|
|
|
2021-04-27 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* User preference key to enable/disable Reference Previews. Named
|
|
|
|
* "mwe-popups-referencePreviews-enabled" in localStorage for anonymous users.
|
|
|
|
*/
|
|
|
|
public const REFERENCE_PREVIEWS_PREFERENCE_NAME_AFTER_BETA = 'popups-reference-previews';
|
|
|
|
|
2019-02-20 16:43:15 +00:00
|
|
|
/**
|
2021-03-19 11:23:40 +00:00
|
|
|
* User preference key to enable/disable Reference Previews
|
2019-02-20 16:43:15 +00:00
|
|
|
*/
|
2021-03-19 11:23:40 +00:00
|
|
|
public const REFERENCE_PREVIEWS_PREFERENCE_NAME = 'popupsreferencepreviews';
|
2019-02-20 16:43:15 +00:00
|
|
|
|
2021-03-09 17:21:48 +00:00
|
|
|
/**
|
|
|
|
* Flags passed on to JS representing preferences
|
|
|
|
*/
|
|
|
|
private const NAV_POPUPS_ENABLED = 1;
|
|
|
|
private const REF_TOOLTIPS_ENABLED = 2;
|
|
|
|
private const REFERENCE_PREVIEWS_ENABLED = 4;
|
|
|
|
private const REFERENCE_PREVIEWS_BETA = 8;
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* @var \Config
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @var PopupsContext
|
|
|
|
*/
|
|
|
|
protected static $instance;
|
2018-08-07 07:48:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ExtensionRegistry
|
|
|
|
*/
|
|
|
|
private $extensionRegistry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PopupsGadgetsIntegration
|
|
|
|
*/
|
|
|
|
private $gadgetsIntegration;
|
|
|
|
|
2021-06-06 13:07:08 +00:00
|
|
|
/**
|
|
|
|
* @var UserOptionsLookup
|
|
|
|
*/
|
|
|
|
private $userOptionsLookup;
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2017-07-12 20:16:55 +00:00
|
|
|
* @param Config $config Mediawiki configuration
|
|
|
|
* @param ExtensionRegistry $extensionRegistry MediaWiki extension registry
|
|
|
|
* @param PopupsGadgetsIntegration $gadgetsIntegration Gadgets integration helper
|
2021-06-06 13:07:08 +00:00
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
2017-07-12 20:16:55 +00:00
|
|
|
* events
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2021-06-06 13:07:08 +00:00
|
|
|
public function __construct(
|
|
|
|
Config $config,
|
|
|
|
ExtensionRegistry $extensionRegistry,
|
|
|
|
PopupsGadgetsIntegration $gadgetsIntegration,
|
|
|
|
UserOptionsLookup $userOptionsLookup
|
|
|
|
) {
|
2016-12-20 21:54:47 +00:00
|
|
|
$this->extensionRegistry = $extensionRegistry;
|
2017-01-04 16:33:40 +00:00
|
|
|
$this->gadgetsIntegration = $gadgetsIntegration;
|
2021-06-06 13:07:08 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2017-01-04 16:33:40 +00:00
|
|
|
|
2017-01-11 21:52:07 +00:00
|
|
|
$this->config = $config;
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
/**
|
2017-07-12 20:16:55 +00:00
|
|
|
* @param \User $user User whose gadgets settings are being checked
|
2017-01-04 16:33:40 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function conflictsWithNavPopupsGadget( \User $user ) {
|
|
|
|
return $this->gadgetsIntegration->conflictsWithNavPopupsGadget( $user );
|
|
|
|
}
|
2017-01-10 01:28:57 +00:00
|
|
|
|
2020-11-03 12:28:33 +00:00
|
|
|
/**
|
|
|
|
* @param \User $user User whose gadgets settings are being checked
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function conflictsWithRefTooltipsGadget( \User $user ) {
|
|
|
|
return $this->gadgetsIntegration->conflictsWithRefTooltipsGadget( $user );
|
|
|
|
}
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* Are Page previews visible on User Preferences Page
|
|
|
|
*
|
2017-07-12 20:29:34 +00:00
|
|
|
* @return bool
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function showPreviewsOptInOnPreferencesPage() {
|
2018-04-26 20:43:05 +00:00
|
|
|
return $this->config->get( 'PopupsHideOptInOnPreferencesPage' ) === false;
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 14:24:51 +00:00
|
|
|
/**
|
|
|
|
* @param \User $user User whose preferences are checked
|
|
|
|
* @return bool whether or not to show reference previews
|
|
|
|
*/
|
2021-03-11 10:32:30 +00:00
|
|
|
public function isReferencePreviewsEnabled( \User $user ) {
|
2021-04-16 11:06:13 +00:00
|
|
|
// TODO: Remove when the feature flag is not needed any more
|
2019-10-10 14:24:51 +00:00
|
|
|
if ( !$this->config->get( 'PopupsReferencePreviews' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove when not in Beta any more
|
2021-02-04 17:04:58 +00:00
|
|
|
if ( $this->isReferencePreviewsInBeta() ) {
|
2019-10-10 14:24:51 +00:00
|
|
|
return BetaFeatures::isFeatureEnabled(
|
|
|
|
$user,
|
2021-03-19 11:23:40 +00:00
|
|
|
self::REFERENCE_PREVIEWS_PREFERENCE_NAME
|
2019-10-10 14:24:51 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-05 14:35:03 +00:00
|
|
|
|
2021-06-06 13:07:08 +00:00
|
|
|
return !$user->isRegistered() || $this->userOptionsLookup->getBoolOption(
|
|
|
|
$user, self::REFERENCE_PREVIEWS_PREFERENCE_NAME_AFTER_BETA
|
|
|
|
);
|
2021-02-04 17:04:58 +00:00
|
|
|
}
|
2019-10-10 14:24:51 +00:00
|
|
|
|
2021-02-04 17:04:58 +00:00
|
|
|
/**
|
|
|
|
* @return bool whether or not reference previews are a beta feature
|
|
|
|
*/
|
|
|
|
public function isReferencePreviewsInBeta() {
|
|
|
|
// TODO: Remove when not in Beta any more
|
|
|
|
return $this->config->get( 'PopupsReferencePreviewsBetaFeature' ) &&
|
|
|
|
\ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' );
|
2019-10-10 14:24:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 17:21:48 +00:00
|
|
|
/**
|
|
|
|
* @param \User $user User whose preferences are checked
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getConfigBitmaskFromUser( \User $user ) {
|
|
|
|
return ( $this->conflictsWithNavPopupsGadget( $user ) ? self::NAV_POPUPS_ENABLED : 0 ) |
|
|
|
|
( $this->conflictsWithRefTooltipsGadget( $user ) ? self::REF_TOOLTIPS_ENABLED : 0 ) |
|
|
|
|
( $this->isReferencePreviewsEnabled( $user ) ? self::REFERENCE_PREVIEWS_ENABLED : 0 ) |
|
|
|
|
( $this->isReferencePreviewsInBeta() ? self::REFERENCE_PREVIEWS_BETA : 0 );
|
|
|
|
}
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2017-07-12 20:16:55 +00:00
|
|
|
* @param \User $user User whose preferences are checked
|
2016-12-16 02:47:52 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-02-10 00:27:05 +00:00
|
|
|
public function shouldSendModuleToUser( \User $user ) {
|
2021-05-06 14:44:41 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-06 13:07:08 +00:00
|
|
|
$shouldLoadPagePreviews = $this->userOptionsLookup->getBoolOption(
|
|
|
|
$user,
|
|
|
|
self::PREVIEWS_OPTIN_PREFERENCE_NAME
|
|
|
|
);
|
2021-05-06 14:44:41 +00:00
|
|
|
$shouldLoadReferencePreviews = $this->isReferencePreviewsEnabled( $user );
|
|
|
|
|
|
|
|
return $shouldLoadPagePreviews || $shouldLoadReferencePreviews;
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function areDependenciesMet() {
|
2018-03-28 22:53:34 +00:00
|
|
|
if ( $this->config->get( 'PopupsGateway' ) === 'mwApiPlain' ) {
|
2019-01-16 16:25:01 +00:00
|
|
|
return $this->extensionRegistry->isLoaded( 'TextExtracts' )
|
2016-12-20 21:54:47 +00:00
|
|
|
&& $this->extensionRegistry->isLoaded( 'PageImages' );
|
2018-03-28 22:53:34 +00:00
|
|
|
}
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2019-01-16 16:25:01 +00:00
|
|
|
return true;
|
2016-12-20 21:54:47 +00:00
|
|
|
}
|
2017-07-28 18:58:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether popups code should be shipped to $title
|
|
|
|
*
|
2020-06-09 06:52:42 +00:00
|
|
|
* For example, if 'Special:UserLogin' is excluded, and the user is on 'Special:UserLogin',
|
|
|
|
* then the title is considered excluded.
|
2017-07-28 18:58:13 +00:00
|
|
|
*
|
2020-06-09 06:52:42 +00:00
|
|
|
* A title is also considered excluded if its root matches one of the page names
|
|
|
|
* from the config variable. For example, if 'User:A' is excluded, and the
|
|
|
|
* title is 'User:A/b', then this title is considered excluded.
|
2017-07-28 18:58:13 +00:00
|
|
|
*
|
2020-06-09 06:52:42 +00:00
|
|
|
* Language specific excluded titles affect all languages. For example, if "Main_Page" is
|
|
|
|
* excluded, "Bosh_Sahifa" (which is "Main_Page" in Uzbek) is considered excluded too.
|
2017-07-28 18:58:13 +00:00
|
|
|
*
|
|
|
|
* @param Title $title title being tested
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-06-09 06:52:42 +00:00
|
|
|
public function isTitleExcluded( $title ) {
|
2020-07-02 22:09:22 +00:00
|
|
|
$excludedPages = $this->config->get( 'PopupsPageDisabled' );
|
|
|
|
|
2017-08-24 20:51:39 +00:00
|
|
|
$canonicalTitle = $title->getRootTitle();
|
|
|
|
|
|
|
|
if ( $title->isSpecialPage() ) {
|
|
|
|
// it's special page, translate it to canonical name
|
2018-09-05 19:00:38 +00:00
|
|
|
list( $name, $subpage ) = MediaWikiServices::getInstance()->getSpecialPageFactory()
|
|
|
|
->resolveAlias( $canonicalTitle->getText() );
|
2017-08-24 20:51:39 +00:00
|
|
|
|
|
|
|
if ( $name !== null ) {
|
|
|
|
$canonicalTitle = Title::newFromText( $name, NS_SPECIAL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 06:52:42 +00:00
|
|
|
foreach ( $excludedPages as $page ) {
|
|
|
|
$excludedTitle = Title::newFromText( $page );
|
2017-08-24 20:51:39 +00:00
|
|
|
|
2020-06-09 06:52:42 +00:00
|
|
|
if ( $canonicalTitle->equals( $excludedTitle ) ) {
|
2017-07-28 18:58:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-16 14:25:27 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* Get module logger
|
|
|
|
*
|
|
|
|
* @return \Psr\Log\LoggerInterface
|
|
|
|
*/
|
|
|
|
public function getLogger() {
|
2017-07-21 17:06:08 +00:00
|
|
|
return MediaWikiServices::getInstance()->getService( 'Popups.Logger' );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
}
|