mediawiki-extensions-Cookie.../includes/Hooks.php
Ostrzyciel 4f3981580d Move the warning outside sitenotice, tidy up code
There's a lot of things going on here, so a quick summary:
* Moved the warning outside sitenotice to avoid reflows and all
  kinds of other issues, including those with dismissable notices.
* Placed the warning on the bottom of the page, on all skins, for
  consistency and to avoid obstructing vital UI elements such as
  site name, user tools, search box.
* Changed the UI to use OOUI for, again, consistency and to avoid
  reinventing the wheel with button styling.
* Removed the cookie emoji as it was only taking up valuable space
  on mobile and making designing a sensible layout much harder.
* Merged the mobile and desktop RL modules into a single, responsive
  one. The warning will work correctly for any skin and screen width.
* The integration tests are truly horrible, I tried to at least
  make them work. They deserve a proper rewrite, but that should be
  done after the extension switches to the new hook system.

I will post screenshots of this in action in the first of the linked
tasks.

Bug: T271047
Bug: T173335
Change-Id: I6e2a3d7aeccc0d4df1b3238e52c67e72099d27d8
(cherry picked from commit edd0a7d949)
2021-02-01 17:56:50 +00:00

227 lines
5.9 KiB
PHP

<?php
namespace CookieWarning;
use Config;
use ConfigException;
use Html;
use MediaWiki;
use MediaWiki\MediaWikiServices;
use MWException;
use OOUI\ButtonInputWidget;
use OOUI\ButtonWidget;
use OOUI\HorizontalLayout;
use OutputPage;
use Skin;
use Title;
use User;
use WebRequest;
class Hooks {
/**
* BeforeInitialize hook handler.
*
* If the disablecookiewarning POST data is send, disables the cookiewarning bar with a
* cookie or a user preference, if the user is logged in.
*
* @param Title &$title
* @param null &$unused
* @param OutputPage &$output
* @param User &$user
* @param WebRequest $request
* @param MediaWiki $mediawiki
* @throws MWException
*/
public static function onBeforeInitialize( Title &$title, &$unused, OutputPage &$output,
User &$user, WebRequest $request, MediaWiki $mediawiki
) {
if ( !$request->wasPosted() || !$request->getVal( 'disablecookiewarning' ) ) {
return;
}
if ( $user->isRegistered() ) {
$user->setOption( 'cookiewarning_dismissed', 1 );
$user->saveSettings();
} else {
$request->response()->setCookie( 'cookiewarning_dismissed', true );
}
$output->redirect( $request->getRequestURL() );
}
/**
* SkinAfterContent hook handler.
*
* Adds the CookieWarning information bar to the output html.
*
* @param string &$data
* @param Skin $skin
*
* @throws MWException
*/
public static function onSkinAfterContent( string &$data, Skin $skin ) {
/** @var Decisions $cookieWarningDecisions */
$cookieWarningDecisions = MediaWikiServices::getInstance()
->getService( 'CookieWarning.Decisions' );
if ( !$cookieWarningDecisions->shouldShowCookieWarning( $skin->getContext() ) ) {
return;
}
$data .= self::generateElements( $skin );
}
/**
* Generates the elements for the banner.
*
* @param Skin $skin
* @return string|null The html for cookie notice.
*/
private static function generateElements( Skin $skin ) : ?string {
$moreLink = self::getMoreLink();
$buttons = [];
if ( $moreLink ) {
$buttons[] = new ButtonWidget( [
'href' => $moreLink,
'label' => $skin->msg( 'cookiewarning-moreinfo-label' )->text(),
'flags' => [ 'progressive' ]
] );
}
$buttons[] = new ButtonInputWidget( [
'type' => 'submit',
'label' => $skin->msg( 'cookiewarning-ok-label' )->text(),
'name' => 'disablecookiewarning',
'value' => 'OK',
'flags' => [ 'primary', 'progressive' ]
] );
$form = Html::rawElement(
'form',
[ 'method' => 'POST' ],
new HorizontalLayout( [ 'items' => $buttons ] )
);
return Html::openElement(
'div',
[ 'class' => 'mw-cookiewarning-container' ]
) .
Html::openElement(
'div',
[ 'class' => 'mw-cookiewarning-text' ]
) .
Html::element(
'span',
[],
$skin->msg( 'cookiewarning-info' )->text()
) .
Html::closeElement( 'div' ) .
$form .
Html::closeElement( 'div' );
}
/**
* Returns the target for the "More information" link of the cookie warning bar, if one is set.
* The link can be set by either (checked in this order):
* - the configuration variable $wgCookieWarningMoreUrl
* - the interface message MediaWiki:Cookiewarning-more-link
* - the interface message MediaWiki:Cookie-policy-link (bc T145781)
*
* @return string|null The url or null if none set
* @throws ConfigException
*/
private static function getMoreLink() : ?string {
$conf = self::getConfig();
if ( $conf->get( 'CookieWarningMoreUrl' ) ) {
return $conf->get( 'CookieWarningMoreUrl' );
}
$cookieWarningMessage = wfMessage( 'cookiewarning-more-link' );
if ( $cookieWarningMessage->exists() && !$cookieWarningMessage->isDisabled() ) {
return $cookieWarningMessage->text();
}
$cookiePolicyMessage = wfMessage( 'cookie-policy-link' );
if ( $cookiePolicyMessage->exists() && !$cookiePolicyMessage->isDisabled() ) {
return $cookiePolicyMessage->text();
}
return null;
}
/**
* BeforePageDisplay hook handler.
*
* Adds the required style and JS module, if cookiewarning is enabled.
*
* @param OutputPage $out
* @throws ConfigException
* @throws MWException
*/
public static function onBeforePageDisplay( OutputPage $out ) {
/** @var Decisions $cookieWarningDecisions */
$cookieWarningDecisions = MediaWikiServices::getInstance()
->getService( 'CookieWarning.Decisions' );
if ( !$cookieWarningDecisions->shouldShowCookieWarning( $out->getContext() ) ) {
return;
}
$modules = [ 'ext.CookieWarning' ];
$moduleStyles = [ 'ext.CookieWarning.styles' ];
if ( $cookieWarningDecisions->shouldAddResourceLoaderComponents() ) {
$modules[] = 'ext.CookieWarning.geolocation';
$moduleStyles[] = 'ext.CookieWarning.geolocation.styles';
}
$out->addModules( $modules );
$out->addModuleStyles( $moduleStyles );
$out->enableOOUI();
}
/**
* ResourceLoaderGetConfigVars hook handler.
*
* @param array &$vars
* @throws ConfigException
*/
public static function onResourceLoaderGetConfigVars( array &$vars ) {
/** @var Decisions $cookieWarningDecisions */
$cookieWarningDecisions = MediaWikiServices::getInstance()
->getService( 'CookieWarning.Decisions' );
$conf = self::getConfig();
if ( $cookieWarningDecisions->shouldAddResourceLoaderComponents() ) {
$vars += [
'wgCookieWarningGeoIPServiceURL' => $conf->get( 'CookieWarningGeoIPServiceURL' ),
'wgCookieWarningForCountryCodes' => $conf->get( 'CookieWarningForCountryCodes' ),
];
}
}
/**
* Returns the Config object for the CookieWarning extension.
*
* @return Config
*/
private static function getConfig() {
return MediaWikiServices::getInstance()->getService( 'CookieWarning.Config' );
}
/**
* GetPreferences hook handler
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
*
* @param User $user
* @param array &$defaultPreferences
* @return bool
*/
public static function onGetPreferences( User $user, &$defaultPreferences ) {
$defaultPreferences['cookiewarning_dismissed'] = [
'type' => 'api',
'default' => '0',
];
return true;
}
}