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;
|
|
|
|
|
2019-10-10 14:24:51 +00:00
|
|
|
use BetaFeatures;
|
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 {
|
|
|
|
|
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
|
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
public const PREVIEWS_ENABLED = '1';
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* User preference value for disabled Page Previews
|
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
public const PREVIEWS_DISABLED = '0';
|
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
|
|
|
|
|
|
|
/**
|
2019-02-26 08:56:07 +00:00
|
|
|
* User preference key to enable/disable Reference Previews as a Beta feature
|
2019-02-20 16:43:15 +00:00
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
public const REFERENCE_PREVIEWS_BETA_PREFERENCE_NAME = 'popupsreferencepreviews';
|
2019-02-20 16:43:15 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EventLogger
|
|
|
|
*/
|
|
|
|
private $eventLogger;
|
|
|
|
|
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
|
|
|
|
* @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 );
|
|
|
|
}
|
2017-01-10 01:28:57 +00:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function isReferencePreviewsEnabled( \User $user ) {
|
|
|
|
// TODO: Remove when the feature flag is ot needed any more
|
|
|
|
if ( !$this->config->get( 'PopupsReferencePreviews' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove when not in Beta any more
|
|
|
|
if ( $this->config->get( 'PopupsReferencePreviewsBetaFeature' ) &&
|
|
|
|
\ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' )
|
|
|
|
) {
|
|
|
|
return BetaFeatures::isFeatureEnabled(
|
|
|
|
$user,
|
|
|
|
self::REFERENCE_PREVIEWS_BETA_PREFERENCE_NAME
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user->getBoolOption( self::REFERENCE_PREVIEWS_BETA_PREFERENCE_NAME );
|
|
|
|
}
|
|
|
|
|
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 ) {
|
2019-10-10 14:24:51 +00:00
|
|
|
return $user->isAnon() ||
|
|
|
|
$user->getBoolOption( self::PREVIEWS_OPTIN_PREFERENCE_NAME ) ||
|
|
|
|
$this->isReferencePreviewsEnabled( $user );
|
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
|
|
|
|
*
|
|
|
|
* 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
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
}
|
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
|
|
|
}
|