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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks definitions for Popups extension
|
|
|
|
*
|
|
|
|
* @package Popups
|
|
|
|
*/
|
2014-02-06 10:49:28 +00:00
|
|
|
class PopupsHooks {
|
2016-12-13 20:38:26 +00:00
|
|
|
const PREVIEWS_PREFERENCES_SECTION = 'rendering/reading';
|
|
|
|
|
2017-07-12 20:41:53 +00:00
|
|
|
/**
|
|
|
|
* Hook executed on retrieving User beta preferences
|
|
|
|
* @param User $user User whose beta preferences are retrieved
|
|
|
|
* @param array &$prefs An associative array of all beta preferences
|
|
|
|
*/
|
2016-12-16 02:47:52 +00:00
|
|
|
static function onGetBetaPreferences( User $user, array &$prefs ) {
|
2014-02-07 11:52:40 +00:00
|
|
|
global $wgExtensionAssetsPath;
|
2017-07-21 17:06:08 +00:00
|
|
|
/** @var PopupsContext $context */
|
|
|
|
$context = MediaWikiServices::getInstance()->getService( 'Popups.Context' );
|
|
|
|
if ( $context->isBetaFeatureEnabled() !== true ) {
|
2015-03-16 13:02:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
$prefs[PopupsContext::PREVIEWS_BETA_PREFERENCE_NAME] = [
|
2014-02-07 11:52:40 +00:00
|
|
|
'label-message' => 'popups-message',
|
|
|
|
'desc-message' => 'popups-desc',
|
2016-12-13 20:38:26 +00:00
|
|
|
'screenshot' => [
|
2016-10-17 18:54:49 +00:00
|
|
|
'ltr' => "$wgExtensionAssetsPath/Popups/images/popups-ltr.svg",
|
|
|
|
'rtl' => "$wgExtensionAssetsPath/Popups/images/popups-rtl.svg",
|
2016-12-13 20:38:26 +00:00
|
|
|
],
|
2017-08-26 20:05:41 +00:00
|
|
|
'info-link' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/Beta_Features/Hovercards',
|
2014-02-07 11:52:40 +00:00
|
|
|
'discussion-link' => 'https://www.mediawiki.org/wiki/Talk:Beta_Features/Hovercards',
|
2016-12-13 20:38:26 +00:00
|
|
|
'requirements' => [
|
2014-08-21 00:46:59 +00:00
|
|
|
'javascript' => true,
|
2016-12-13 20:38:26 +00:00
|
|
|
],
|
|
|
|
];
|
2015-03-16 13:02:38 +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
|
2017-07-12 20:27:44 +00:00
|
|
|
* @param array &$prefs Preferences description array, to be fed to a HTMLForm object
|
2014-10-11 18:19:12 +00:00
|
|
|
*/
|
2016-12-13 20:38:26 +00:00
|
|
|
static function onGetPreferences( User $user, array &$prefs ) {
|
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' => [
|
2016-12-16 02:47:52 +00:00
|
|
|
wfMessage( 'popups-prefs-optin-enabled-label' )->text()
|
2016-12-20 22:49:01 +00:00
|
|
|
=> PopupsContext::PREVIEWS_ENABLED,
|
2016-12-16 02:47:52 +00:00
|
|
|
wfMessage( 'popups-prefs-optin-disabled-label' )->text()
|
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.
|
|
|
|
*
|
|
|
|
* @param OutputPage &$out The Output page object
|
|
|
|
* @param Skin &$skin &Skin object that will be used to generate the page
|
|
|
|
*/
|
2016-12-13 20:38:26 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
|
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();
|
2016-12-20 21:54:47 +00:00
|
|
|
$logger->error( 'Popups requires the PageImages and TextExtracts extensions. '
|
|
|
|
. 'If Beta mode is on it requires also BetaFeatures extension' );
|
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();
|
2017-07-21 17:06:08 +00:00
|
|
|
if ( !$context->isBetaFeatureEnabled() || $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
|
|
|
/**
|
2017-07-12 20:27:44 +00:00
|
|
|
* @param array &$vars Array of variables to be added into the output of the startup module
|
2014-11-11 05:49:07 +00:00
|
|
|
*/
|
|
|
|
public static function onResourceLoaderGetConfigVars( array &$vars ) {
|
2017-07-21 17:06:08 +00:00
|
|
|
$conf = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
2017-01-15 18:59:37 +00:00
|
|
|
$vars['wgPopupsBetaFeature'] = $conf->get( 'PopupsBetaFeature' );
|
2017-06-08 13:29:57 +00:00
|
|
|
$vars['wgPopupsGateway'] = $conf->get( 'PopupsGateway' );
|
2017-08-10 17:55:26 +00:00
|
|
|
$vars['wgPopupsEventLogging'] = $conf->get( 'PopupsEventLogging' );
|
|
|
|
$vars['wgPopupsAnonsExperimentalGroupSize'] = $conf->get( 'PopupsAnonsExperimentalGroupSize' );
|
2017-03-07 00:27:38 +00:00
|
|
|
$vars['wgPopupsStatsvSamplingRate'] = $conf->get( 'PopupsStatsvSamplingRate' );
|
2016-05-16 09:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MakeGlobalVariablesScript hook handler.
|
|
|
|
*
|
2016-12-21 12:35:35 +00:00
|
|
|
* Variables added:
|
2017-02-10 00:27:05 +00:00
|
|
|
* * `wgPopupsShouldSendModuleToUser' - The server's notion of whether or not the
|
|
|
|
* user has enabled Page Previews (see `\Popups\PopupsContext#shouldSendModuleToUser`).
|
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
|
2017-07-12 20:16:55 +00:00
|
|
|
* @param OutputPage $out OutputPage instance calling the hook
|
2016-05-16 09:03:52 +00:00
|
|
|
*/
|
|
|
|
public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) {
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = MediaWikiServices::getInstance()->getService( 'Popups.Context' );
|
2016-05-16 09:03:52 +00:00
|
|
|
$user = $out->getUser();
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$vars['wgPopupsShouldSendModuleToUser'] = $context->shouldSendModuleToUser( $user );
|
|
|
|
$vars['wgPopupsConflictsWithNavPopupGadget'] = $context->conflictsWithNavPopupsGadget(
|
2017-01-23 22:00:27 +00:00
|
|
|
$user );
|
2016-12-21 12:35:35 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2017-01-10 01:28:57 +00:00
|
|
|
public static function onUserGetDefaultOptions( &$wgDefaultUserOptions ) {
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = MediaWikiServices::getInstance()->getService( 'Popups.Context' );
|
2016-12-16 02:47:52 +00:00
|
|
|
$wgDefaultUserOptions[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ] =
|
2017-07-21 17:06:08 +00:00
|
|
|
$context->getDefaultIsEnabledState();
|
2014-11-11 05:49:07 +00:00
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
|
2014-02-06 10:49:28 +00:00
|
|
|
}
|