Mwalker has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/82804


Change subject: Allow Custom Banner Categories
......................................................................

Allow Custom Banner Categories

This allows banners to hide/show based on a completely user custom
category definition.

Also; modifications were made to Special:HideBanners to allow for
eventual cross site custom hiding in much the same way that fundraising
currently does it.

Change-Id: I26e53fef0ad083c031809a6981741580fab92eda
---
M CentralNotice.i18n.php
M CentralNoticeBannerLogPager.php
M includes/Banner.php
M modules/ext.centralNotice.bannerController/bannerController.js
M special/SpecialBannerLoader.php
M special/SpecialCentralNoticeBanners.php
M special/SpecialHideBanners.php
7 files changed, 92 insertions(+), 85 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice 
refs/changes/04/82804/1

diff --git a/CentralNotice.i18n.php b/CentralNotice.i18n.php
index fe0d7f9..ffa776b 100644
--- a/CentralNotice.i18n.php
+++ b/CentralNotice.i18n.php
@@ -208,10 +208,8 @@
        'centralnotice-user-role-anonymous' => 'Anonymous',
        'centralnotice-user-role-logged-in' => 'Logged-in',
        'centralnotice-banner-messages' => 'Translatable banner messages',
-       'centralnotice-banner-class' => 'Class',
-       'centralnotice-banner-class-desc' => 'Banners of a specific class share 
tracking variables and group settings (e.g. hide cookies and impression 
counts.)',
-       'centralnotice-banner-class-generic' => 'Generic',
-       'centralnotice-banner-class-fundraising' => 'Fundraising',
+       'centralnotice-banner-class' => 'Category',
+       'centralnotice-banner-class-desc' => 'Banners of a specific category 
share tracking variables and group settings (e.g. hide cookies and impression 
counts.) Category names can be magic words like {{{campaign}}} or {{{banner}}} 
which will be automatically expanded at display time. Custom values must be 
alphanumeric.',
        'centralnotice-templates-included' => 'Included templates',
 
        'centralnotice-archive-banner' => 'Archive',
@@ -635,24 +633,14 @@
        'centralnotice-user-role-anonymous' => 'Label for the anonymous user 
role.
 {{Identical|Anonymous}}',
        'centralnotice-user-role-logged-in' => 'Label for the logged-in user 
role',
-       'centralnotice-banner-class' => 'Used as label for the select box.
+       'centralnotice-banner-class' => 'Used as label for a select box that 
allows a user to choose or create a label for the banner.
 
-Followed by the following options:
-* {{msg-mw|Centralnotice-banner-class-generic}}
-* {{msg-mw|Centralnotice-banner-class-fundraising}}
-
-The help message for the select box is 
{{msg-mw|Centralnotice-banner-class-desc}}.
-{{Identical|Class}}',
+The help message for the select box is 
{{msg-mw|Centralnotice-banner-class-desc}}.',
        'centralnotice-banner-class-desc' => 'Used as help message for the 
{{msg-mw|Centralnotice-banner-class}} select box which has the following 
options:
-* {{msg-mw|Centralnotice-banner-class-generic}}
-* {{msg-mw|Centralnotice-banner-class-fundraising}}
 
 A "hide cookie" is a cookie that is created when a user asks to hide a banner.
 
 "Impression" is an instance of showing the banner to a reader. For example, 
the banner was shown to the site readers a hundred times, then the impression 
count is 100.',
-       'centralnotice-banner-class-generic' => 'Used as an option in the 
{{msg-mw|Centralnotice-banner-class}} select box.',
-       'centralnotice-banner-class-fundraising' => 'Used as an option in the 
{{msg-mw|Centralnotice-banner-class}} select box.
-{{Identical|Fundraising}}',
        'centralnotice-templates-included' => 'Used as label for the list of 
included templates.',
        'centralnotice-archive-banner' => 'Translate as a 
verb.{{Identical|Archive}}',
        'centralnotice-archive-banner-title' => 'Used as title for the dialog 
box.
diff --git a/CentralNoticeBannerLogPager.php b/CentralNoticeBannerLogPager.php
index c8e4a51..e6e93d5 100644
--- a/CentralNoticeBannerLogPager.php
+++ b/CentralNoticeBannerLogPager.php
@@ -151,8 +151,8 @@
                )->text() . "<br/>";
                $details .= $this->msg(
                        'centralnotice-log-label',
-                       $this->msg( 'centralnotice-fundraising' )->text(),
-                       ($row->tmplog_end_fundraising ? 'on' : 'off')
+                       $this->msg( 'centralnotice-category' )->text(),
+                       ($row->tmplog_end_category ? 'on' : 'off')
                )->text() . "<br/>";
                $details .= $this->msg(
                        'centralnotice-log-label',
@@ -177,7 +177,7 @@
        function showChanges( $row ) {
                $details = $this->testBooleanChange( 'anon', $row );
                $details .= $this->testBooleanChange( 'account', $row );
-               $details .= $this->testBooleanChange( 'fundraising', $row );
+               $details .= $this->testTextChange( 'category', $row );
                $details .= $this->testBooleanChange( 'autolink', $row );
                $details .= $this->testTextChange( 'landingpages', $row );
                $details .= $this->testTextChange( 'controller_mixin', $row );
diff --git a/includes/Banner.php b/includes/Banner.php
index a8568b4..a9c5a44 100644
--- a/includes/Banner.php
+++ b/includes/Banner.php
@@ -260,6 +260,39 @@
        }
 
        /**
+        * Obtain an array of all categories currently seen attached to banners
+        * @return string[]
+        */
+       public static function getAllUsedCategories() {
+               $db = CNDatabase::getDb();
+               $res = $db->select(
+                       'cn_templates',
+                       'tmp_category',
+                       '',
+                       __METHOD__,
+                       array( 'DISTINCT', 'ORDER BY tmp_category ASC' )
+               );
+
+               $categories = array();
+               foreach ( $res as $row ) {
+                       $categories[$row->tmp_category] = $row->tmp_category;
+               }
+               return $categories;
+       }
+
+       /**
+        * Remove invalid characters from a category string that has been magic
+        * word expanded.
+        * 
+        * @param $cat Category string to sanitize
+        *
+        * @return string
+        */
+       public static function sanitizeRenderedCategory( $cat ) {
+               return preg_replace( '/[^a-zA-Z0-9_]/', '', $cat );
+       }
+
+       /**
         * If true the banner renderer should replace the contents of the 
<a#cn-landingpage-link> href
         * with values from @see Banner->getAutoLinks()
         *
@@ -1242,7 +1275,8 @@
                $details = array(
                        'anon'             => (int)$banner->allocateToAnon(),
                        'account'          => 
(int)$banner->allocateToLoggedIn(),
-                       'fundraising'      => (int)($banner->getCategory() === 
'fundraising'),
+                       'fundraising'      => (int)($banner->getCategory() === 
'fundraising'), // TODO: Death to this!
+                       'category'         => $banner->getCategory(),
                        'autolink'         => (int)$banner->isAutoLinked(),
                        'landingpages'     => implode( ',', 
$banner->getAutoLinks() ),
                        'controller_mixin' => implode( ",", array_keys( 
$banner->getMixins() ) ),
@@ -1392,34 +1426,6 @@
                }
 
                $dbw->insert( 'cn_template_log', $log );
-       }
-
-       /**
-        * Update a banner
-        */
-       function editTemplate( $user, $body, $displayAnon, $displayAccount, 
$fundraising,
-               $autolink, $landingPages, $mixins, $priorityLangs, $devices
-       ) {
-               $banner = Banner::fromName( $this->getName() );
-               if ( !$banner->exists() ) {
-                       return;
-               }
-
-               $banner->setAllocation( $displayAnon, $displayAccount );
-               $banner->setCategory( ( $fundraising == 1 ) ? 'fundraising' : 
'{{{campaign}}}' );
-               $banner->setDevices( $devices );
-               $banner->setPriorityLanguages( $priorityLangs );
-               $banner->setBodyContent( $body );
-
-               $landingPages = explode( ',', $landingPages );
-               array_walk( $landingPages, function ( &$x ) { $x = trim( $x ); 
} );
-               $banner->setAutoLink( $autolink, $landingPages );
-
-               $mixins = explode( ",", $mixins );
-               array_walk( $mixins, function ( &$x ) { $x = trim( $x ); } );
-               $banner->setMixins( $mixins );
-
-               $banner->save( $user );
        }
        //</editor-fold>
 
diff --git a/modules/ext.centralNotice.bannerController/bannerController.js 
b/modules/ext.centralNotice.bannerController/bannerController.js
index 3a517fe..48a45f6 100644
--- a/modules/ext.centralNotice.bannerController/bannerController.js
+++ b/modules/ext.centralNotice.bannerController/bannerController.js
@@ -42,7 +42,7 @@
                 */
                data: {
                        getVars: {},
-                       bannerType: 'default',
+                       category: 'default',
                        bucket: null,
                        testing: false
                },
@@ -253,7 +253,7 @@
                        };
                } else {
                        // Ok, we have a banner! Get the banner type for more 
queryness
-                       mw.centralNotice.data.bannerType = ( 
bannerJson.fundraising ? 'fundraising' : 'default' );
+                       mw.centralNotice.data.category = encodeURIComponent( 
bannerJson.category );
 
                        if ( typeof mw.centralNotice.bannerData.preload === 
'function'
                                        && 
!mw.centralNotice.bannerData.preload() ) {
@@ -263,10 +263,14 @@
                                };
                        } else if (
                                bannerJson.priority < 3 && /* A priority of 3 
is Emergency and cannot be hidden */
-                               !mw.centralNotice.data.testing && /* And we 
want to see what we're testing! :) */
-                               $.cookie( 'centralnotice_' + 
encodeURIComponent( mw.centralNotice.data.bannerType ) ) === 'hide'
+                               mw.centralNotice.data.testing === false && /* 
And we want to see what we're testing! :) */
+                               (
+                                       $.cookie( 'centralnotice_hide_' + 
mw.centralNotice.data.category ) === 'hide' ||
+                                       /* Legacy for long duration fundraising 
cookies; remove after Oct 1, 2014 */
+                                       $.cookie( 'centralnotice_' + 
mw.centralNotice.data.category ) === 'hide'
+                               )
                        ) {
-                               // The banner was hidden by a bannertype hide 
cookie and we're not testing
+                               // The banner was hidden by a category hide 
cookie and we're not testing
                                impressionResultData = {
                                        result: 'hide',
                                        reason: 'cookie'
@@ -275,7 +279,7 @@
                                // All conditions fulfilled, inject the banner
                                mw.centralNotice.bannerData.bannerName = 
bannerJson.bannerName;
                                $( 'div#centralNotice' )
-                                       .attr( 'class', mw.html.escape( 'cn-' + 
mw.centralNotice.data.bannerType ) )
+                                       .attr( 'class', mw.html.escape( 'cn-' + 
mw.centralNotice.data.category ) )
                                        .prepend( bannerJson.bannerHtml );
 
                                // Create landing page links if required
@@ -331,18 +335,16 @@
        };
 
        // Function for hiding banners when the user clicks the close button
+       // TODO: Make it call up to the special page for cross project hiding
        window.hideBanner = function () {
                // Hide current banner
                $( '#centralNotice' ).hide();
-
-               // Get the type of the current banner (e.g. 'fundraising')
-               var bannerType = mw.centralNotice.data.bannerType || 'default';
 
                // Set the banner hiding cookie to hide future banners of the 
same type
                var d = new Date();
                d.setSeconds( d.getSeconds() + mw.config.get( 
'wgNoticeCookieShortExpiry' ) );
                $.cookie(
-                       'centralnotice_' + encodeURIComponent( bannerType ),
+                       'centralnotice_hide_' + mw.centralNotice.data.category,
                        'hide',
                        { expires: d, path: '/' }
                );
diff --git a/special/SpecialBannerLoader.php b/special/SpecialBannerLoader.php
index 38342dd..061b056 100644
--- a/special/SpecialBannerLoader.php
+++ b/special/SpecialBannerLoader.php
@@ -110,11 +110,14 @@
                // TODO: these are BannerRenderer duties:
                $settings = Banner::getBannerSettings( $bannerName, false );
 
+               $category = $bannerRenderer->substituteMagicWords( 
$settings['category'] );
+               $category = Banner::sanitizeRenderedCategory( $category );
+
                $bannerArray = array(
                        'bannerName' => $bannerName,
                        'bannerHtml' => $bannerHtml,
                        'campaign' => $this->campaignName,
-                       'fundraising' => $settings['fundraising'],
+                       'category' => $category,
                        'autolink' => $settings['autolink'],
                        'landingPages' => explode( ", ", 
$settings['landingpages'] ),
                );
diff --git a/special/SpecialCentralNoticeBanners.php 
b/special/SpecialCentralNoticeBanners.php
index a519ecb..ff7e065 100644
--- a/special/SpecialCentralNoticeBanners.php
+++ b/special/SpecialCentralNoticeBanners.php
@@ -357,20 +357,16 @@
                );
 
                /* --- Banner Settings --- */
-               // TODO: Make this far more flexible with the option for 
arbitrary banner classes
-               $class = ( $bannerSettings[ 'fundraising' ] === 1 ) ? 
'fundraising' : 'generic';
-               $formDescriptor[ 'banner-class' ] = array(
+               $formDescriptor['banner-class'] = array(
                        'section' => 'settings',
-                       'type' => 'select',
+                       'type' => 'selectorother',
                        'disabled' => !$this->editable,
                        'label-message' => 'centralnotice-banner-class',
                        'help-message' => 'centralnotice-banner-class-desc',
-                       'options' => array(
-                               $this->msg( 
'centralnotice-banner-class-generic' )->text() => 'generic',
-                               $this->msg( 
'centralnotice-banner-class-fundraising' )->text() => 'fundraising'
-                       ),
-                       'default' => $class,
-                       'cssclass' => 'separate-form-element',
+                       'options' => Banner::getAllUsedCategories(),
+                       'size' => 30,
+                       'maxlength'=> 255,
+                       'default' => $banner->getCategory(),
                );
 
                $selected = array();
@@ -735,18 +731,26 @@
                        $prioLang = array();
                }
 
-               $banner->editTemplate(
-                       $this->getUser(),
-                       $formData[ 'banner-body' ],
+               $banner->setAllocation(
                        in_array( 'anonymous', $formData[ 'display-to' ] ),
-                       in_array( 'registered', $formData[ 'display-to' ] ),
-                       $formData[ 'banner-class' ] === 'fundraising',
-                       $formData[ 'create-landingpage-link' ],
-                       $formData[ 'landing-pages' ],
-                       '',
-                       $prioLang,
-                       $formData[ 'device-classes' ]
+                       in_array( 'registered', $formData[ 'display-to' ] )
                );
+               $banner->setCategory( $formData[ 'banner-class' ] );
+               $banner->setDevices( $formData[ 'device-classes' ] );
+               $banner->setPriorityLanguages( $prioLang );
+               $banner->setBodyContent( $formData[ 'banner-body' ] );
+
+               $landingPages = explode( ',', $formData[ 'landing-pages' ] );
+               array_walk( $landingPages, function ( &$x ) { $x = trim( $x ); 
} );
+               $banner->setAutoLink( $formData[ 'create-landingpage-link' ], 
$landingPages );
+
+               /* TODO: Mixins
+               $mixins = explode( ",", $mixins );
+               array_walk( $mixins, function ( &$x ) { $x = trim( $x ); } );
+               $banner->setMixins( $mixins );
+               */
+
+               $banner->save( $this->getUser() );
 
                return null;
        }
diff --git a/special/SpecialHideBanners.php b/special/SpecialHideBanners.php
index 3adf525..f947876 100644
--- a/special/SpecialHideBanners.php
+++ b/special/SpecialHideBanners.php
@@ -11,7 +11,12 @@
        }
 
        function execute( $par ) {
-               $this->setHideCookie();
+               global $wgNoticeCookieLongExpiry;
+
+               $duration = $this->getRequest()->getInt( 'duration', 
$wgNoticeCookieLongExpiry );
+               $category = $this->getRequest()->getText( 'category', 
'fundraising' );
+               $category = Banner::sanitizeRenderedCategory( $category );
+               $this->setHideCookie( $category, $duration );
 
                $this->getOutput()->disable();
                wfResetOutputBuffers();
@@ -23,17 +28,16 @@
        /**
         * Set the cookie for hiding fundraising banners.
         */
-       function setHideCookie() {
-               global $wgNoticeCookieDomain, $wgNoticeCookieLongExpiry;
+       function setHideCookie( $category, $duration ) {
+               global $wgNoticeCookieDomain;
 
-               $exp = time() + $wgNoticeCookieLongExpiry;
+               $exp = time() + $duration;
 
                if ( is_callable( array( 'CentralAuthUser', 'getCookieDomain' ) 
) ) {
                        $cookieDomain = CentralAuthUser::getCookieDomain();
                } else {
                        $cookieDomain = $wgNoticeCookieDomain;
                }
-               // Hide fundraising banners for this domain
-               setcookie( 'centralnotice_fundraising', 'hide', $exp, '/', 
$cookieDomain, false, false );
+               setcookie( "centralnotice_hide_{$category}", 'hide', $exp, '/', 
$cookieDomain, false, false );
        }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/82804
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I26e53fef0ad083c031809a6981741580fab92eda
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Mwalker <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to