2015-07-27 17:18:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CookieWarningHooks {
|
2016-01-10 17:58:21 +00:00
|
|
|
/**
|
|
|
|
* SkinTemplateOutputPageBeforeExec hook handler.
|
|
|
|
*
|
|
|
|
* Adds the CookieWarning information bar to the output html.
|
|
|
|
*
|
|
|
|
* @param SkinTemplate $sk
|
|
|
|
* @param QuickTemplate $tpl
|
|
|
|
*/
|
|
|
|
public static function onSkinTemplateOutputPageBeforeExec(
|
|
|
|
SkinTemplate &$sk, QuickTemplate &$tpl
|
|
|
|
) {
|
|
|
|
// Config instance of CookieWarning
|
2015-08-16 10:00:33 +00:00
|
|
|
$conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' );
|
|
|
|
$moreLink = '';
|
2016-01-10 17:58:21 +00:00
|
|
|
// if a "more information" URL was configured, add a link to it in the cookiewarning
|
|
|
|
// information bar
|
2015-08-16 10:00:33 +00:00
|
|
|
if ( $conf->get( 'CookieWarningMoreUrl' ) ) {
|
|
|
|
$moreLink = Html::element(
|
|
|
|
'a',
|
|
|
|
array( 'href' => $conf->get( 'CookieWarningMoreUrl' ) ),
|
2016-01-10 17:58:21 +00:00
|
|
|
$sk->msg( 'cookiewarning-moreinfo-label' )->text()
|
2015-08-16 10:00:33 +00:00
|
|
|
);
|
|
|
|
}
|
2016-01-10 17:58:21 +00:00
|
|
|
// if the cookiewarning should be visible to the user, append the element to
|
|
|
|
// the head data.
|
|
|
|
if ( self::showWarning( $sk->getContext() ) ) {
|
2015-07-27 17:18:44 +00:00
|
|
|
$tpl->data['headelement'] .= Html::openElement(
|
|
|
|
'div',
|
|
|
|
array( 'class' => 'mw-cookiewarning-container' )
|
|
|
|
) .
|
|
|
|
Html::openElement(
|
|
|
|
'div',
|
|
|
|
array( 'class' => 'mw-cookiewarning-text' )
|
|
|
|
) .
|
|
|
|
Html::element(
|
|
|
|
'span',
|
|
|
|
array(),
|
|
|
|
$sk->msg( 'cookiewarning-info' )->text()
|
|
|
|
) .
|
2015-08-16 10:00:33 +00:00
|
|
|
$moreLink .
|
2015-07-27 17:18:44 +00:00
|
|
|
Html::element(
|
|
|
|
'a',
|
|
|
|
array( 'class' => 'mw-cookiewarning-dismiss' ),
|
|
|
|
'OK'
|
|
|
|
) .
|
|
|
|
Html::closeElement( 'div' ) .
|
|
|
|
Html::closeElement( 'div' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 17:58:21 +00:00
|
|
|
/**
|
|
|
|
* BeforePageDisplay hook handler.
|
|
|
|
*
|
|
|
|
* Adds the required style and JS module, if cookiewarning is enabled.
|
|
|
|
*
|
|
|
|
* @param OutputPage $out
|
|
|
|
*/
|
2015-07-27 17:18:44 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage $out ) {
|
2016-01-10 17:58:21 +00:00
|
|
|
if ( self::showWarning( $out->getContext() ) ) {
|
2015-07-27 17:18:44 +00:00
|
|
|
$out->addModuleStyles( array( 'ext.CookieWarning.styles' ) );
|
|
|
|
$out->addModules( array( 'ext.CookieWarning' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 17:58:21 +00:00
|
|
|
/**
|
|
|
|
* Checks, if the CookieWarning information bar should be visible to this user on
|
|
|
|
* this page.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return boolean Returns true, if the cookie warning should be visible, false otherwise.
|
|
|
|
*/
|
2015-07-27 17:18:44 +00:00
|
|
|
private static function showWarning( IContextSource $context ) {
|
2015-08-16 09:49:05 +00:00
|
|
|
$user = $context->getUser();
|
2015-07-27 17:18:44 +00:00
|
|
|
$conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' );
|
|
|
|
if (
|
2016-01-10 17:58:21 +00:00
|
|
|
// if enabled in LocalSettings.php
|
2015-07-27 17:18:44 +00:00
|
|
|
$conf->get( 'CookieWarningEnabled' ) &&
|
2016-01-10 17:58:21 +00:00
|
|
|
// if not already dismissed by this user (and saved in the user prefs)
|
2015-08-16 09:49:05 +00:00
|
|
|
!$user->getBoolOption( 'cookiewarning_dismissed', false ) &&
|
2016-01-10 17:58:21 +00:00
|
|
|
// if not already dismissed by this user (and saved in the browser cookies)
|
2015-07-27 17:18:44 +00:00
|
|
|
!$context->getRequest()->getCookie( 'cookiewarning_dismissed' )
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-16 09:49:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'] = array(
|
|
|
|
'type' => 'api',
|
|
|
|
'default' => '0',
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-27 17:18:44 +00:00
|
|
|
}
|