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
|
|
|
|
2019-12-17 10:58:36 +00:00
|
|
|
use Config;
|
2017-07-21 17:06:08 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-14 19:22:55 +00:00
|
|
|
use OutputPage;
|
|
|
|
use Skin;
|
2019-12-17 10:58:36 +00:00
|
|
|
use User;
|
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
|
|
|
|
2019-10-10 14:53:30 +00:00
|
|
|
private const PREVIEWS_PREFERENCES_SECTION = 'rendering/reading';
|
2016-12-13 20:38:26 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2019-10-10 15:21:31 +00:00
|
|
|
|
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 ) );
|
2019-10-10 15:21:31 +00:00
|
|
|
$readingOptions = [
|
|
|
|
PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME => $option,
|
|
|
|
];
|
2016-01-29 17:06:33 +00:00
|
|
|
|
2019-10-10 14:24:51 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
|
|
|
if ( $config->get( 'PopupsReferencePreviews' ) &&
|
|
|
|
!$config->get( 'PopupsReferencePreviewsBetaFeature' )
|
|
|
|
) {
|
2019-10-10 15:33:09 +00:00
|
|
|
$readingOptions[PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME] = [
|
2019-10-10 14:24:51 +00:00
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'popups-refpreview-user-preference-label',
|
|
|
|
'section' => self::PREVIEWS_PREFERENCES_SECTION,
|
|
|
|
'disabled' => $option['disabled'] ?? false,
|
|
|
|
];
|
2019-12-17 10:58:36 +00:00
|
|
|
}
|
2019-10-10 14:24:51 +00:00
|
|
|
|
2016-12-20 22:49:01 +00:00
|
|
|
if ( $skinPosition !== false ) {
|
|
|
|
$injectIntoIndex = $skinPosition + 1;
|
|
|
|
$prefs = array_slice( $prefs, 0, $injectIntoIndex, true )
|
2019-10-10 15:21:31 +00:00
|
|
|
+ $readingOptions
|
|
|
|
+ array_slice( $prefs, $injectIntoIndex, null, true );
|
2016-12-20 22:49:01 +00:00
|
|
|
} else {
|
2019-10-10 15:21:31 +00:00
|
|
|
$prefs += $readingOptions;
|
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-12-17 10:58:36 +00:00
|
|
|
/** @var Config $config */
|
2019-02-11 12:33:37 +00:00
|
|
|
$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-10-10 15:21:31 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2019-01-16 16:27:57 +00:00
|
|
|
/** @var PopupsContext $context */
|
2019-10-10 15:21:31 +00:00
|
|
|
$context = $services->getService( 'Popups.Context' );
|
2016-05-16 09:03:52 +00:00
|
|
|
$user = $out->getUser();
|
|
|
|
|
2019-10-10 14:24:51 +00:00
|
|
|
// TODO: Remove all references to wgPopupsReferencePreviews when not in Beta any more, and
|
|
|
|
// the temporary feature flag is not needed any more.
|
|
|
|
$vars['wgPopupsReferencePreviews'] = $context->isReferencePreviewsEnabled( $user );
|
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
|
|
|
}
|
|
|
|
|
2016-12-13 20:38:26 +00:00
|
|
|
/**
|
2019-12-17 10:58:36 +00:00
|
|
|
* Called whenever a user wants to reset their preferences.
|
2017-01-10 01:28:57 +00:00
|
|
|
*
|
2019-12-17 10:58:36 +00:00
|
|
|
* @param array &$defaultOptions
|
2016-12-13 20:38:26 +00:00
|
|
|
*/
|
2019-12-17 10:58:36 +00:00
|
|
|
public static function onUserGetDefaultOptions( array &$defaultOptions ) {
|
|
|
|
/** @var Config $config */
|
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
|
|
|
$default = $config->get( 'PopupsOptInDefaultState' );
|
|
|
|
$defaultOptions[PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME] = $default;
|
2018-05-09 21:21:30 +00:00
|
|
|
|
2019-12-17 15:27:26 +00:00
|
|
|
// As long as in Beta, don't set a default for Reference Previews. Rely on it either being
|
|
|
|
// null (= disabled), or follow what the "betafeatures-auto-enroll" flag says.
|
2019-12-17 10:58:36 +00:00
|
|
|
if ( $config->get( 'PopupsReferencePreviews' ) &&
|
|
|
|
!$config->get( 'PopupsReferencePreviewsBetaFeature' )
|
|
|
|
) {
|
|
|
|
$defaultOptions[PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME] = $default;
|
|
|
|
}
|
2014-11-11 05:49:07 +00:00
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
|
2018-05-09 21:21:30 +00:00
|
|
|
/**
|
2019-12-17 10:58:36 +00:00
|
|
|
* Called one time when initializing a users preferences for a newly created account.
|
2018-05-09 21:21:30 +00:00
|
|
|
*
|
|
|
|
* @param User $user Newly created user object
|
2019-12-17 10:58:36 +00:00
|
|
|
* @param bool $isAutoCreated
|
2018-05-09 21:21:30 +00:00
|
|
|
*/
|
2019-12-17 10:58:36 +00:00
|
|
|
public static function onLocalUserCreated( User $user, $isAutoCreated ) {
|
|
|
|
/** @var Config $config */
|
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
|
|
|
$default = $config->get( 'PopupsOptInStateForNewAccounts' );
|
2019-10-10 15:21:31 +00:00
|
|
|
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $default );
|
2019-12-17 10:58:36 +00:00
|
|
|
|
2019-12-17 15:27:26 +00:00
|
|
|
// As long as in Beta, don't set a default for Reference Previews. Rely on it either being
|
|
|
|
// null (= disabled), or follow what the "betafeatures-auto-enroll" flag says.
|
2019-12-17 10:58:36 +00:00
|
|
|
if ( $config->get( 'PopupsReferencePreviews' ) &&
|
|
|
|
!$config->get( 'PopupsReferencePreviewsBetaFeature' )
|
|
|
|
) {
|
|
|
|
$user->setOption( PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME, $default );
|
|
|
|
}
|
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 ) {
|
2019-12-17 10:58:36 +00:00
|
|
|
/** @var Config $config */
|
2019-02-20 16:43:15 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
|
|
|
$extensionAssetsPath = $config->get( 'ExtensionAssetsPath' );
|
|
|
|
|
|
|
|
if ( $config->get( 'PopupsReferencePreviewsBetaFeature' ) &&
|
|
|
|
$config->get( 'PopupsReferencePreviews' )
|
|
|
|
) {
|
2019-10-10 15:33:09 +00:00
|
|
|
$prefs[PopupsContext::REFERENCE_PREVIEWS_PREFERENCE_NAME] = [
|
2019-02-20 16:43:15 +00:00
|
|
|
'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
|
|
|
}
|