2014-02-06 10:49:28 +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
|
|
|
|
*/
|
2017-02-14 19:22:55 +00:00
|
|
|
namespace Popups;
|
2014-02-06 10:49:28 +00:00
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-14 19:22:55 +00:00
|
|
|
use User;
|
|
|
|
use OutputPage;
|
|
|
|
use Skin;
|
2019-02-20 16:43:15 +00:00
|
|
|
use BetaFeatures;
|
2017-02-14 19:22:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks definitions for Popups extension
|
|
|
|
*
|
|
|
|
* @package Popups
|
|
|
|
*/
|
2014-02-06 10:49:28 +00:00
|
|
|
class PopupsHooks {
|
2019-02-26 10:55:57 +00:00
|
|
|
|
2016-12-13 20:38:26 +00:00
|
|
|
const PREVIEWS_PREFERENCES_SECTION = 'rendering/reading';
|
|
|
|
|
2014-10-11 18:19:12 +00:00
|
|
|
/**
|
2016-12-13 20:38:26 +00:00
|
|
|
* Add Page Previews options to user Preferences page
|
|
|
|
*
|
2017-07-12 20:16:55 +00:00
|
|
|
* @param User $user User whose preferences are being modified
|
2019-01-24 14:44:26 +00:00
|
|
|
* @param array[] &$prefs Preferences description array, to be fed to a HTMLForm object
|
2014-10-11 18:19:12 +00:00
|
|
|
*/
|
2018-05-20 19:13:36 +00:00
|
|
|
public static function onGetPreferences( User $user, array &$prefs ) {
|
2019-01-16 16:27:57 +00:00
|
|
|
/** @var PopupsContext $context */
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = MediaWikiServices::getInstance()->getService( 'Popups.Context' );
|
2014-10-11 18:19:12 +00:00
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
if ( !$context->showPreviewsOptInOnPreferencesPage() ) {
|
2016-12-13 20:38:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-12-20 22:49:01 +00:00
|
|
|
$option = [
|
2016-12-13 20:38:26 +00:00
|
|
|
'type' => 'radio',
|
|
|
|
'label-message' => 'popups-prefs-optin-title',
|
2017-09-12 16:53:40 +00:00
|
|
|
'help-message' => 'popups-prefs-conflicting-gadgets-info',
|
2016-12-13 20:38:26 +00:00
|
|
|
'options' => [
|
2018-05-20 20:41:33 +00:00
|
|
|
wfMessage( 'popups-prefs-optin-enabled-label' )->escaped()
|
2016-12-20 22:49:01 +00:00
|
|
|
=> PopupsContext::PREVIEWS_ENABLED,
|
2018-05-20 20:41:33 +00:00
|
|
|
wfMessage( 'popups-prefs-optin-disabled-label' )->escaped()
|
2016-12-20 22:49:01 +00:00
|
|
|
=> PopupsContext::PREVIEWS_DISABLED
|
2016-12-13 20:38:26 +00:00
|
|
|
],
|
|
|
|
'section' => self::PREVIEWS_PREFERENCES_SECTION
|
2016-05-12 22:37:19 +00:00
|
|
|
];
|
2017-01-04 16:33:40 +00:00
|
|
|
if ( $context->conflictsWithNavPopupsGadget( $user ) ) {
|
|
|
|
$option[ 'disabled' ] = true;
|
2017-09-12 16:53:40 +00:00
|
|
|
$option[ 'help-message' ] = [ 'popups-prefs-disable-nav-gadgets-info',
|
|
|
|
'Special:Preferences#mw-prefsection-gadgets' ];
|
2014-10-11 18:19:12 +00:00
|
|
|
}
|
2016-01-29 17:06:33 +00:00
|
|
|
|
2016-12-20 22:49:01 +00:00
|
|
|
$skinPosition = array_search( 'skin', array_keys( $prefs ) );
|
2016-01-29 17:06:33 +00:00
|
|
|
|
2016-12-20 22:49:01 +00:00
|
|
|
if ( $skinPosition !== false ) {
|
|
|
|
$injectIntoIndex = $skinPosition + 1;
|
|
|
|
$prefs = array_slice( $prefs, 0, $injectIntoIndex, true )
|
|
|
|
+ [ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME => $option ]
|
|
|
|
+ array_slice( $prefs, $injectIntoIndex, count( $prefs ) - 1, true );
|
|
|
|
} else {
|
|
|
|
$prefs[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ] = $option;
|
2016-01-29 17:06:33 +00:00
|
|
|
}
|
2014-10-11 18:19:12 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 20:41:53 +00:00
|
|
|
/**
|
|
|
|
* Allows last minute changes to the output page, e.g. adding of CSS or JavaScript by extensions.
|
|
|
|
*
|
2019-02-26 11:15:54 +00:00
|
|
|
* @param OutputPage $out The Output page object
|
|
|
|
* @param Skin $skin Skin object that will be used to generate the page
|
2017-07-12 20:41:53 +00:00
|
|
|
*/
|
2019-02-26 11:15:54 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
|
2019-01-16 16:27:57 +00:00
|
|
|
/** @var PopupsContext $context */
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = MediaWikiServices::getInstance()->getService( 'Popups.Context' );
|
2017-07-28 18:58:13 +00:00
|
|
|
if ( $context->isTitleBlacklisted( $out->getTitle() ) ) {
|
2017-08-18 23:38:42 +00:00
|
|
|
return;
|
2017-07-28 18:58:13 +00:00
|
|
|
}
|
2015-03-16 13:02:38 +00:00
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
if ( !$context->areDependenciesMet() ) {
|
|
|
|
$logger = $context->getLogger();
|
2018-04-26 20:43:05 +00:00
|
|
|
$logger->error( 'Popups requires the PageImages extensions.
|
|
|
|
TextExtracts extension is required when using mwApiPlain gateway.' );
|
2017-08-18 23:38:42 +00:00
|
|
|
return;
|
2015-03-16 13:02:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 18:58:13 +00:00
|
|
|
$user = $out->getUser();
|
2018-04-26 20:43:05 +00:00
|
|
|
if ( $context->shouldSendModuleToUser( $user ) ) {
|
2017-02-08 22:08:12 +00:00
|
|
|
$out->addModules( [ 'ext.popups' ] );
|
2014-02-06 10:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-11 04:34:49 +00:00
|
|
|
|
2014-11-11 05:49:07 +00:00
|
|
|
/**
|
2019-02-26 11:15:54 +00:00
|
|
|
* Hook handler for the ResourceLoaderStartUpModule that makes static configuration visible to
|
|
|
|
* the frontend. These variables end in the only "startup" ResourceLoader module that is loaded
|
|
|
|
* before all others.
|
|
|
|
*
|
|
|
|
* Dynamic configuration that depends on the context needs to be published via the
|
|
|
|
* MakeGlobalVariablesScript hook.
|
|
|
|
*
|
2017-07-12 20:27:44 +00:00
|
|
|
* @param array &$vars Array of variables to be added into the output of the startup module
|
2019-02-26 11:15:54 +00:00
|
|
|
* @param string $skin
|
2014-11-11 05:49:07 +00:00
|
|
|
*/
|
2019-02-26 11:15:54 +00:00
|
|
|
public static function onResourceLoaderGetConfigVars( array &$vars, $skin ) {
|
2019-02-11 12:33:37 +00:00
|
|
|
/** @var \Config $config */
|
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
2019-02-26 11:15:54 +00:00
|
|
|
|
2019-02-11 12:33:37 +00:00
|
|
|
$vars['wgPopupsVirtualPageViews'] = $config->get( 'PopupsVirtualPageViews' );
|
|
|
|
$vars['wgPopupsGateway'] = $config->get( 'PopupsGateway' );
|
|
|
|
$vars['wgPopupsEventLogging'] = $config->get( 'PopupsEventLogging' );
|
|
|
|
$vars['wgPopupsRestGatewayEndpoint'] = $config->get( 'PopupsRestGatewayEndpoint' );
|
|
|
|
$vars['wgPopupsStatsvSamplingRate'] = $config->get( 'PopupsStatsvSamplingRate' );
|
2016-05-16 09:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-26 11:15:54 +00:00
|
|
|
* Hook handler publishing dynamic configuration that depends on the context, e.g. the page or
|
|
|
|
* the users settings. These variables end in an inline <script> in the documents head.
|
2016-05-16 09:03:52 +00:00
|
|
|
*
|
2016-12-21 12:35:35 +00:00
|
|
|
* Variables added:
|
2019-02-20 16:43:15 +00:00
|
|
|
* * `wgPopupsReferencePreviews' - The server's notion of whether or not the reference
|
|
|
|
* previews should be enabled. Depending on the general setting done on the wiki and
|
|
|
|
* - in cases where the feature is used as BetaFeature - of the user's BetaFeature
|
|
|
|
* setting.
|
2017-01-23 22:00:27 +00:00
|
|
|
* * `wgPopupsConflictsWithNavPopupGadget' - The server's notion of whether or not the
|
|
|
|
* user has enabled conflicting Navigational Popups Gadget.
|
2016-05-16 09:03:52 +00:00
|
|
|
*
|
2017-07-12 20:27:44 +00:00
|
|
|
* @param array &$vars variables to be added into the output of OutputPage::headElement
|
2019-02-26 11:15:54 +00:00
|
|
|
* @param \IContextSource $out OutputPage instance calling the hook
|
2016-05-16 09:03:52 +00:00
|
|
|
*/
|
2019-02-26 11:15:54 +00:00
|
|
|
public static function onMakeGlobalVariablesScript( array &$vars, \IContextSource $out ) {
|
2019-01-16 16:27:57 +00:00
|
|
|
/** @var PopupsContext $context */
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = MediaWikiServices::getInstance()->getService( 'Popups.Context' );
|
2019-02-20 16:43:15 +00:00
|
|
|
/** @var \Config $config */
|
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
2016-05-16 09:03:52 +00:00
|
|
|
$user = $out->getUser();
|
|
|
|
|
2019-02-20 16:43:15 +00:00
|
|
|
$vars['wgPopupsReferencePreviews'] = self::isReferencePreviewsEnabled( $user, $config );
|
2017-07-21 17:06:08 +00:00
|
|
|
$vars['wgPopupsConflictsWithNavPopupGadget'] = $context->conflictsWithNavPopupsGadget(
|
2019-02-26 11:15:54 +00:00
|
|
|
$user
|
|
|
|
);
|
2016-12-21 12:35:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 16:43:15 +00:00
|
|
|
/**
|
|
|
|
* @param User $user User whose preferences are checked
|
|
|
|
* @param \Config $config Popups config
|
|
|
|
* @return bool whether or not to show reference previews
|
|
|
|
*/
|
|
|
|
private static function isReferencePreviewsEnabled( User $user, \Config $config ) {
|
|
|
|
return $config->get( 'PopupsReferencePreviews' ) &&
|
|
|
|
( !$config->get( 'PopupsReferencePreviewsBetaFeature' ) ||
|
|
|
|
!\ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) ||
|
|
|
|
BetaFeatures::isFeatureEnabled(
|
|
|
|
$user,
|
|
|
|
PopupsContext::REFERENCE_PREVIEWS_BETA_PREFERENCE_NAME
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:38:26 +00:00
|
|
|
/**
|
|
|
|
* Register default preferences for popups
|
2017-01-10 01:28:57 +00:00
|
|
|
*
|
2017-07-12 20:27:44 +00:00
|
|
|
* @param array &$wgDefaultUserOptions Reference to default options array
|
2016-12-13 20:38:26 +00:00
|
|
|
*/
|
2019-02-26 11:15:54 +00:00
|
|
|
public static function onUserGetDefaultOptions( array &$wgDefaultUserOptions ) {
|
2019-01-16 16:27:57 +00:00
|
|
|
/** @var \Config $config */
|
2018-05-09 21:21:30 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
$wgDefaultUserOptions[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ] =
|
2018-05-09 21:21:30 +00:00
|
|
|
$config->get( 'PopupsOptInDefaultState' );
|
2014-11-11 05:49:07 +00:00
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
|
2018-05-09 21:21:30 +00:00
|
|
|
/**
|
|
|
|
* Change the default PagePreviews visibility state for newly created accounts
|
|
|
|
*
|
|
|
|
* @param User $user Newly created user object
|
|
|
|
* @param bool $autocreated Is user autocreated
|
|
|
|
*/
|
|
|
|
public static function onLocalUserCreated( User $user, $autocreated ) {
|
2018-06-11 19:10:32 +00:00
|
|
|
// ignore the $autocreated flag, we always want to set PagePreviews visibility
|
2019-01-16 16:27:57 +00:00
|
|
|
/** @var \Config $config */
|
2018-06-11 19:10:32 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
2018-05-09 21:21:30 +00:00
|
|
|
|
2018-06-11 19:10:32 +00:00
|
|
|
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
|
|
|
|
$config->get( 'PopupsOptInStateForNewAccounts' ) );
|
2018-05-09 21:21:30 +00:00
|
|
|
}
|
2019-02-20 16:43:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register preferences that enable experimental features.
|
|
|
|
*
|
|
|
|
* @param User $user User whose preferences are being modified
|
|
|
|
* @param array[] &$prefs Array of beta features
|
|
|
|
*/
|
|
|
|
public static function onGetBetaFeaturePreferences( User $user, array &$prefs ) {
|
|
|
|
/** @var \Config $config */
|
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
|
|
|
$extensionAssetsPath = $config->get( 'ExtensionAssetsPath' );
|
|
|
|
|
|
|
|
if ( $config->get( 'PopupsReferencePreviewsBetaFeature' ) &&
|
|
|
|
$config->get( 'PopupsReferencePreviews' )
|
|
|
|
) {
|
|
|
|
$prefs[ PopupsContext::REFERENCE_PREVIEWS_BETA_PREFERENCE_NAME ] = [
|
|
|
|
'label-message' => 'popups-refpreview-beta-feature-message',
|
|
|
|
'desc-message' => 'popups-refpreview-beta-feature-description',
|
|
|
|
'screenshot' => [
|
|
|
|
'ltr' => "$extensionAssetsPath/Popups/resources/ext.popups.images/refpreview-beta-ltr.svg",
|
|
|
|
'rtl' => "$extensionAssetsPath/Popups/resources/ext.popups.images/refpreview-beta-rtl.svg",
|
|
|
|
],
|
2019-04-19 00:02:20 +00:00
|
|
|
'info-link' => 'https://mediawiki.org/wiki/Help:Reference_Previews',
|
|
|
|
'discussion-link' => 'https://mediawiki.org/wiki/Help_Talk:Reference_Previews',
|
2019-02-20 16:43:15 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2019-02-26 10:55:57 +00:00
|
|
|
|
2014-02-06 10:49:28 +00:00
|
|
|
}
|