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';
|
|
|
|
|
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
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @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 ) {
|
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
|
|
|
/**
|
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 ) {
|
2019-01-16 16:27:57 +00:00
|
|
|
/** @var \Config $conf */
|
2017-07-21 17:06:08 +00:00
|
|
|
$conf = MediaWikiServices::getInstance()->getService( 'Popups.Config' );
|
2018-02-08 22:11:44 +00:00
|
|
|
$vars['wgPopupsVirtualPageViews'] = $conf->get( 'PopupsVirtualPageViews' );
|
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' );
|
2018-03-07 20:15:30 +00:00
|
|
|
$vars['wgPopupsRestGatewayEndpoint'] = $conf->get( 'PopupsRestGatewayEndpoint' );
|
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 ) {
|
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' );
|
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 ) {
|
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
|
|
|
}
|
2014-02-06 10:49:28 +00:00
|
|
|
}
|