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;
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use ExtensionRegistry;
|
2017-01-11 21:52:07 +00:00
|
|
|
use Config;
|
2017-07-06 18:16:05 +00:00
|
|
|
use Popups\EventLogging\EventLogger;
|
2017-07-28 18:58:13 +00:00
|
|
|
use Title;
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Popups Module
|
|
|
|
*
|
|
|
|
* @package Popups
|
|
|
|
*/
|
|
|
|
class PopupsContext {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension name
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const EXTENSION_NAME = 'popups';
|
|
|
|
/**
|
|
|
|
* Logger channel (name)
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const LOGGER_CHANNEL = 'popups';
|
|
|
|
/**
|
|
|
|
* User preference value for enabled Page Previews
|
2017-03-30 19:37:52 +00:00
|
|
|
* Identical to \HTMLFeatureField::OPTION_ENABLED in BetaFeatures
|
|
|
|
*
|
2016-12-16 02:47:52 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-03-30 19:37:52 +00:00
|
|
|
const PREVIEWS_ENABLED = '1';
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* User preference value for disabled Page Previews
|
2017-03-30 19:37:52 +00:00
|
|
|
* Identical to \HTMLFeatureField::OPTION_DISABLED in BetaFeatures
|
|
|
|
*
|
2016-12-16 02:47:52 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-03-30 19:37:52 +00:00
|
|
|
const PREVIEWS_DISABLED = '0';
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* User preference to enable/disable Page Previews
|
|
|
|
* Currently for BETA and regular opt in we use same preference name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const PREVIEWS_OPTIN_PREFERENCE_NAME = 'popups';
|
|
|
|
/**
|
|
|
|
* User preference to enable/disable Page Preivews as a beta feature
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const PREVIEWS_BETA_PREFERENCE_NAME = 'popups';
|
|
|
|
/**
|
|
|
|
* @var \Config
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @var PopupsContext
|
|
|
|
*/
|
|
|
|
protected static $instance;
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* Module constructor.
|
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
|
|
|
|
* @param EventLogger $eventLogger A logger capable of logging EventLogging
|
|
|
|
* events
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2017-07-21 17:06:08 +00:00
|
|
|
public function __construct( Config $config, ExtensionRegistry $extensionRegistry,
|
2017-07-06 18:16:05 +00:00
|
|
|
PopupsGadgetsIntegration $gadgetsIntegration, EventLogger $eventLogger ) {
|
2016-12-20 21:54:47 +00:00
|
|
|
$this->extensionRegistry = $extensionRegistry;
|
2017-01-04 16:33:40 +00:00
|
|
|
$this->gadgetsIntegration = $gadgetsIntegration;
|
2017-07-06 18:16:05 +00:00
|
|
|
$this->eventLogger = $eventLogger;
|
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 );
|
|
|
|
}
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* Is Beta Feature mode enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isBetaFeatureEnabled() {
|
|
|
|
return $this->config->get( 'PopupsBetaFeature' ) === true;
|
|
|
|
}
|
2017-01-10 01:28:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default Page previews state
|
|
|
|
*
|
|
|
|
* @see PopupsContext::PREVIEWS_ENABLED
|
|
|
|
* @see PopupsContext::PREVIEWS_DISABLED
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDefaultIsEnabledState() {
|
|
|
|
return $this->config->get( 'PopupsOptInDefaultState' );
|
|
|
|
}
|
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() {
|
2016-12-20 21:54:47 +00:00
|
|
|
return !$this->isBetaFeatureEnabled()
|
2016-12-16 02:47:52 +00:00
|
|
|
&& $this->config->get( 'PopupsHideOptInOnPreferencesPage' ) === false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 ) {
|
2016-12-20 21:54:47 +00:00
|
|
|
if ( $this->isBetaFeatureEnabled() ) {
|
2017-02-08 22:08:12 +00:00
|
|
|
return $user->isAnon() ? false :
|
|
|
|
\BetaFeatures::isFeatureEnabled( $user, self::PREVIEWS_BETA_PREFERENCE_NAME );
|
|
|
|
}
|
|
|
|
return $user->isAnon() ? true :
|
|
|
|
$user->getOption( self::PREVIEWS_OPTIN_PREFERENCE_NAME ) === self::PREVIEWS_ENABLED;
|
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
|
|
|
$areMet = true;
|
|
|
|
|
|
|
|
if ( $this->config->get( 'PopupsGateway' ) === 'mwApiPlain' ) {
|
|
|
|
$areMet = $areMet && $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
|
|
|
|
|
|
|
if ( $this->isBetaFeatureEnabled() ) {
|
|
|
|
$areMet = $areMet && $this->extensionRegistry->isLoaded( 'BetaFeatures' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $areMet;
|
|
|
|
}
|
2017-07-28 18:58:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether popups code should be shipped to $title
|
|
|
|
*
|
|
|
|
* For example, if 'Special:UserLogin' is blacklisted, and the user is on 'Special:UserLogin',
|
|
|
|
* then the title is considered blacklisted.
|
|
|
|
*
|
|
|
|
* A title is also considered blacklisted if its root matches one of the page names
|
|
|
|
* from the config variable. For example, if 'User:A' is blacklisted, and the
|
|
|
|
* title is 'User:A/b', then this title is considered blacklisted.
|
|
|
|
*
|
|
|
|
* Language specific blacklisted titles affect all languages. For example, if "Main_Page" is
|
|
|
|
* blacklisted, "Bosh_Sahifa" (which is "Main_Page" in Uzbek) is considered blacklisted
|
|
|
|
* too.
|
|
|
|
*
|
|
|
|
* @param Title $title title being tested
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isTitleBlacklisted( $title ) {
|
|
|
|
$blacklistedPages = $this->config->get( 'PopupsPageBlacklist' );
|
2017-08-24 20:51:39 +00:00
|
|
|
$canonicalTitle = $title->getRootTitle();
|
|
|
|
|
|
|
|
if ( $title->isSpecialPage() ) {
|
|
|
|
// it's special page, translate it to canonical name
|
|
|
|
list( $name, $subpage ) = \SpecialPageFactory::resolveAlias( $canonicalTitle->getText() );
|
|
|
|
|
|
|
|
if ( $name !== null ) {
|
|
|
|
$canonicalTitle = Title::newFromText( $name, NS_SPECIAL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 18:58:13 +00:00
|
|
|
foreach ( $blacklistedPages as $page ) {
|
|
|
|
$blacklistedTitle = Title::newFromText( $page );
|
2017-08-24 20:51:39 +00:00
|
|
|
|
|
|
|
if ( $canonicalTitle->equals( $blacklistedTitle ) ) {
|
2017-07-28 18:58:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
}
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2017-07-06 18:16:05 +00:00
|
|
|
/**
|
|
|
|
* Log disabled event
|
|
|
|
*/
|
|
|
|
public function logUserDisabledPagePreviewsEvent() {
|
|
|
|
// @see https://phabricator.wikimedia.org/T167365
|
|
|
|
$this->eventLogger->log( [
|
|
|
|
'pageTitleSource' => 'Special:Preferences',
|
|
|
|
'namespaceIdSource' => NS_SPECIAL,
|
|
|
|
'pageIdSource' => -1,
|
|
|
|
'hovercardsSuppressedByGadget' => false,
|
|
|
|
'pageToken' => wfRandomString(),
|
2017-07-12 20:44:12 +00:00
|
|
|
// we don't have access to mw.user.sessionId()
|
|
|
|
'sessionToken' => wfRandomString(),
|
2017-07-06 18:16:05 +00:00
|
|
|
'action' => 'disabled',
|
|
|
|
'isAnon' => false,
|
|
|
|
'popupEnabled' => false,
|
|
|
|
'previewCountBucket' => 'unknown'
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|