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;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-11-05 09:25:26 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2023-08-19 04:18:33 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-11-29 12:39:47 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
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.
|
|
|
|
*/
|
2023-11-24 11:51:41 +00:00
|
|
|
public const REFERENCE_PREVIEWS_PREFERENCE_NAME = 'popups-reference-previews';
|
2021-04-27 11:09:31 +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;
|
|
|
|
|
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;
|
|
|
|
|
2022-11-05 09:25:26 +00:00
|
|
|
/**
|
|
|
|
* @var SpecialPageFactory
|
|
|
|
*/
|
|
|
|
private $specialPageFactory;
|
|
|
|
|
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
|
2022-11-05 09:25:26 +00:00
|
|
|
* @param SpecialPageFactory $specialPageFactory
|
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,
|
2022-11-05 09:25:26 +00:00
|
|
|
SpecialPageFactory $specialPageFactory,
|
2021-06-06 13:07:08 +00:00
|
|
|
UserOptionsLookup $userOptionsLookup
|
|
|
|
) {
|
2016-12-20 21:54:47 +00:00
|
|
|
$this->extensionRegistry = $extensionRegistry;
|
2017-01-04 16:33:40 +00:00
|
|
|
$this->gadgetsIntegration = $gadgetsIntegration;
|
2022-11-05 09:25:26 +00:00
|
|
|
$this->specialPageFactory = $specialPageFactory;
|
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 ) {
|
2019-10-10 14:24:51 +00:00
|
|
|
if ( !$this->config->get( 'PopupsReferencePreviews' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:08:14 +00:00
|
|
|
return !$user->isNamed() || $this->userOptionsLookup->getBoolOption(
|
2023-11-24 11:51:41 +00:00
|
|
|
$user, self::REFERENCE_PREVIEWS_PREFERENCE_NAME
|
2021-06-06 13:07:08 +00:00
|
|
|
);
|
2021-02-04 17:04:58 +00:00
|
|
|
}
|
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 ) |
|
2023-11-06 08:48:29 +00:00
|
|
|
( $this->isReferencePreviewsEnabled( $user ) ? self::REFERENCE_PREVIEWS_ENABLED : 0 );
|
2021-03-09 17:21:48 +00:00
|
|
|
}
|
|
|
|
|
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 ) {
|
2023-06-08 14:08:14 +00:00
|
|
|
if ( !$user->isNamed() ) {
|
2021-05-06 14:44:41 +00:00
|
|
|
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
|
2022-11-05 09:25:26 +00:00
|
|
|
[ $name, $subpage ] = $this->specialPageFactory
|
2018-09-05 19:00:38 +00:00
|
|
|
->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
|
|
|
}
|
|
|
|
}
|