Adamw has uploaded a new change for review.

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


Change subject: Refactor to facilitate historical allocations
......................................................................

Refactor to facilitate historical allocations

BannerChooser filter and get operations are explicit

Change-Id: I2f3a0d89b30517294565231e60657bb25a456572
---
M api/ApiCentralNoticeAllocations.php
M includes/Banner.php
M includes/BannerChooser.php
M includes/Campaign.php
M special/SpecialBannerAllocation.php
M special/SpecialBannerRandom.php
M special/SpecialGlobalAllocation.php
7 files changed, 123 insertions(+), 66 deletions(-)


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

diff --git a/api/ApiCentralNoticeAllocations.php 
b/api/ApiCentralNoticeAllocations.php
index e37dcfc..e61fc86 100644
--- a/api/ApiCentralNoticeAllocations.php
+++ b/api/ApiCentralNoticeAllocations.php
@@ -45,7 +45,7 @@
                // Get our language/project/country
                $params = $this->extractRequestParams();
 
-               $bannerList = static::getAllocationInformation(
+               $bannerList = static::getBannerAllocation(
                        $params['project'],
                        $params['country'],
                        $params['language'],
@@ -138,29 +138,44 @@
         *
         * @return array
         */
-       public static function getAllocationInformation( $project, $country, 
$language, $anonymous, $device, $bucket = null, $minimize = false ) {
+       public static function getBannerAllocation( $project, $country, 
$language, $anonymous, $device, $bucket = null, $minimize = false ) {
+               self::sanitizeParams( $project, $country, $language, 
$anonymous, $device, $bucket, $minimize );
+
+               $chooser = new BannerChooser();
+               $chooser->filter( $project, $language, $country, $anonymous, 
$device, $bucket );
+               $banners = $chooser->getBanners();
+
+               if ( $minimize ) {
+                       $banners = static::minimizeBanners( $banners );
+               }
+
+               return $banners;
+       }
+
+       // FIXME: this function is steeoopid
+       protected static function sanitizeParams( &$project, &$country, 
&$language, &$anonymous, &$device, &$bucket, &$minimize ) {
                $project = ApiCentralNoticeAllocations::sanitizeText(
                        $project,
-                       static::PROJECT_FILTER,
-                       static::DEFAULT_PROJECT
+                       self::PROJECT_FILTER,
+                       self::DEFAULT_PROJECT
                );
 
                $country = ApiCentralNoticeAllocations::sanitizeText(
                        $country,
-                       static::LOCATION_FILTER,
-                       static::DEFAULT_COUNTRY
+                       self::LOCATION_FILTER,
+                       self::DEFAULT_COUNTRY
                );
 
                $language = ApiCentralNoticeAllocations::sanitizeText(
                        $language,
-                       static::LANG_FILTER,
-                       static::DEFAULT_LANGUAGE
+                       self::LANG_FILTER,
+                       self::DEFAULT_LANGUAGE
                );
 
                $anonymous = ApiCentralNoticeAllocations::sanitizeText(
                        $anonymous,
-                       static::ANONYMOUS_FILTER,
-                       static::DEFAULT_ANONYMOUS
+                       self::ANONYMOUS_FILTER,
+                       self::DEFAULT_ANONYMOUS
                );
                $anonymous = ( $anonymous == 'true' );
 
@@ -172,20 +187,11 @@
 
                $bucket = ApiCentralNoticeAllocations::sanitizeText(
                        $bucket,
-                       static::BUCKET_FILTER,
-                       static::DEFAULT_BUCKET
+                       self::BUCKET_FILTER,
+                       self::DEFAULT_BUCKET
                );
 
                $minimize = (boolean) $minimize;
-
-               $chooser = new BannerChooser( $project, $language, $country, 
$anonymous, $device, $bucket );
-               $banners = $chooser->banners;
-
-               if ( $minimize ) {
-                       $banners = static::minimizeBanners( $banners );
-               }
-
-               return $banners;
        }
 
        /**
diff --git a/includes/Banner.php b/includes/Banner.php
index 51d4ec7..bab30a4 100644
--- a/includes/Banner.php
+++ b/includes/Banner.php
@@ -341,13 +341,15 @@
                        ),
                        __METHOD__
                );
-               $banner['display_anon'] = intval( $row->display_anon );
-               $banner['display_account'] = intval( $row->display_account );
+               $banner['display_anon'] = (int) $row->display_anon;
+               $banner['display_account'] = (int) $row->display_account;
 
-               $banner['fundraising'] = intval( $row->fundraising );
-               $banner['autolink'] = intval( $row->autolink );
+               $banner['fundraising'] = (int) $row->fundraising;
+               $banner['autolink'] = (int) $row->autolink;
                $banner['landing_pages'] = explode( ", ", $row->landing_pages );
 
+               //XXX
+               $banner['device'] = "desktop";
                return $banner;
        }
 
diff --git a/includes/BannerChooser.php b/includes/BannerChooser.php
index 9ca5dd0..22a0f7c 100644
--- a/includes/BannerChooser.php
+++ b/includes/BannerChooser.php
@@ -5,11 +5,30 @@
        const ALLOCATION_KEY = 'allocation';
        const RAND_MAX = 30;
 
-       var $banners = array();
+       protected $campaigns;
+       protected $banners;
 
-       function __construct( $project, $language, $country, $anonymous, 
$device, $bucket ) {
-               $campaigns = Campaign::getCampaigns( $project, $language, 
$country );
-               $this->banners = Banner::getCampaignBanners( $campaigns );
+       /**
+        * @param $campaigns array of structs of the type returned by 
getHistoricalCampaigns
+        */
+       function __construct( $campaigns = null ) {
+               $this->campaigns = $campaigns;
+       }
+
+       function filter( $project, $language, $country, $anonymous, $device, 
$bucket ) {
+               if ( $this->campaigns === null ) {
+                       //FIXME: a little sketchy that we are leaving 
$this->campaigns NULL
+                       $campaigns = Campaign::getCampaigns( $project, 
$language, $country );
+                       $this->banners = Banner::getCampaignBanners( $campaigns 
);
+               } else {
+                       $this->banners = array();
+                       $this->filterCampaigns( $project, $language, $country );
+                       foreach ( $this->campaigns as $campaign ) {
+                               foreach ( $campaign['banners'] as $name => 
$banner ) {
+                                       $this->banners[] = $banner;
+                               }
+                       }
+               }
 
                $this->filterBanners( $anonymous, $device, $bucket );
 
@@ -40,6 +59,19 @@
                if ( count( $this->banners ) ) {
                        return $this->banners[ count( $this->banners ) - 1 ];
                }
+       }
+
+       protected function filterCampaigns( $project, $language, $country ) {
+               $filtered = array();
+               foreach ( $this->campaigns as $campaign ) {
+                       if ( ( $project === null or in_array( $project, 
$campaign['projects'] ) )
+                               or ( $language === null or in_array( $language, 
$campaign['languages'] ) )
+                               or ( $country === null or in_array( $country, 
$campaign['countries'] ) )
+                       ) {
+                               $filtered[] = $campaign;
+                       }
+               }
+               $this->campaigns = $filtered;
        }
 
        /**
@@ -143,4 +175,12 @@
                        $banner[ self::ALLOCATION_KEY ] = $banner[ 
self::SLOTS_KEY ] / self::RAND_MAX;
                }
        }
+
+       function getCampaigns() {
+               return $this->campaigns;
+       }
+
+       function getBanners() {
+               return $this->banners;
+       }
 }
diff --git a/includes/Campaign.php b/includes/Campaign.php
index a5422c9..443506d 100644
--- a/includes/Campaign.php
+++ b/includes/Campaign.php
@@ -216,6 +216,7 @@
                        $singleRes = $dbr->select(
                                "cn_notice_log",
                                array(
+                                       "id" => "notlog_not_id",
                                        "name" => "notlog_not_name",
                                        "projects" => "notlog_end_projects",
                                        "languages" => "notlog_end_languages",
@@ -232,6 +233,9 @@
                        );
 
                        $campaign = $singleRes->fetchRow();
+                       $campaign['projects'] = explode( ", ", 
$campaign['projects'] );
+                       $campaign['languages'] = explode( ", ", 
$campaign['languages'] );
+                       $campaign['countries'] = explode( ", ", 
$campaign['countries'] );
                        $campaign['banners'] = FormatJson::decode( 
$campaign['banners'], true );
                        foreach ( $campaign['banners'] as $name => &$banner ) {
                                $historical_banner = 
Banner::getHistoricalBanner( $name, $ts );
diff --git a/special/SpecialBannerAllocation.php 
b/special/SpecialBannerAllocation.php
index 6bc6ee4..5e3d1eb 100644
--- a/special/SpecialBannerAllocation.php
+++ b/special/SpecialBannerAllocation.php
@@ -36,7 +36,7 @@
         *
         * This should always be a lowercase language code.
         *
-        * @var string $project
+        * @var string $language
         */
        public $language = 'en';
 
@@ -85,7 +85,6 @@
                // Output ResourceLoader module for styling and javascript 
functions
                $out->addModules( array(
                        'ext.centralNotice.interface',
-                       'ext.centralNotice.bannerStats'
                ) );
 
                // Initialize error variable
@@ -247,7 +246,7 @@
                        }
 
                        foreach ( $matrix as $target ) {
-                               $banners = 
ApiCentralNoticeAllocations::getAllocationInformation(
+                               $banners = 
ApiCentralNoticeAllocations::getBannerAllocation(
                                        $project,
                                        $country,
                                        $language,
diff --git a/special/SpecialBannerRandom.php b/special/SpecialBannerRandom.php
index 1588f58..4b55892 100644
--- a/special/SpecialBannerRandom.php
+++ b/special/SpecialBannerRandom.php
@@ -24,7 +24,8 @@
        }
 
        protected function chooseBanner() {
-               $chooser = new BannerChooser(
+               $chooser = new BannerChooser();
+               $chooser->filter(
                        $this->project,
                        $this->language,
                        $this->country,
diff --git a/special/SpecialGlobalAllocation.php 
b/special/SpecialGlobalAllocation.php
index fca0787..d94a155 100644
--- a/special/SpecialGlobalAllocation.php
+++ b/special/SpecialGlobalAllocation.php
@@ -1,29 +1,10 @@
 <?php
 /**
- * Wikimedia Foundation
- *
- * LICENSE
- *
- * This program 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.
- *
- * This program 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.
- *
- */
-
-/**
- * SpecialGlobalAllocation
- *
  * Display worldwide allocation
  */
 class SpecialGlobalAllocation extends CentralNotice {
        /**
-        * The project being used for banner allocation.
+        * Optional project filter
         *
         * @see $wgNoticeProjects
         *
@@ -32,22 +13,20 @@
        public $project = null;
 
        /**
-        * The language being used for banner allocation
-        *
         * This should always be a lowercase language code.
         *
-        * @var string $project
+        * @var string $language
         */
        public $language = null;
 
        /**
-        * The location being used for banner allocation.
-        *
         * This should always be an uppercase country code.
         *
         * @var string $location
         */
        public $location = null;
+
+       public $device = null;
 
        /**
         * Constructor
@@ -61,17 +40,41 @@
                return false;
        }
 
+       protected function getRequestParams() {
+               $sanitize = function( $param, $regex ) {
+                       return filter_var( $param, FILTER_VALIDATE_REGEXP, 
array( 'options' => array( 'regexp' => $regex ) ) );
+               };
+
+               $this->project = $sanitize(
+                       $this->getRequest()->getText( 'project', $this->project 
),
+                       ApiCentralNoticeAllocations::PROJECT_FILTER
+               );
+               $this->language = $sanitize(
+                       $this->getRequest()->getText( 'language', 
$this->language ),
+                       ApiCentralNoticeAllocations::LANG_FILTER
+               );
+               $this->location = $sanitize(
+                       $this->getRequest()->getText( 'country', 
$this->location ),
+                       ApiCentralNoticeAllocations::LOCATION_FILTER
+               );
+               $this->device = $sanitize(
+                       $this->getRequest()->getText( 'device', $this->device ),
+                       ApiCentralNoticeAllocations::DEVICE_NAME_FILTER
+               );
+
+               $this->timestamp = wfTimestamp( TS_UNIX, $this->getDateTime( 
'filter' ) );
+       }
+
        /**
-        * Handle different types of page requests
+        * Handle page requests
+        *
+        * Without filters, this will display the allocation of all active 
banners.
         */
        public function execute( $sub ) {
                global $wgNoticeProjects, $wgLanguageCode, $wgNoticeProject;
                $out = $this->getOutput();
-               $request = $this->getRequest();
 
-               $this->project = $request->getText( 'project', $this->project );
-               $this->language = $request->getText( 'language', 
$this->language );
-               $this->location = $request->getVal( 'country', $this->location 
);
+               $this->getRequestParams();
 
                // Begin output
                $this->setHeaders();
@@ -79,7 +82,6 @@
                // Output ResourceLoader module for styling and javascript 
functions
                $out->addModules( array(
                        'ext.centralNotice.interface',
-                       'ext.centralNotice.bannerStats'
                ) );
 
                // Initialize error variable
@@ -201,13 +203,16 @@
                        htmlspecialchars( $this->project ) : $this->msg( 
'centralnotice-all' )->text();
                $countryLabel = $this->location ?
                        htmlspecialchars( $this->location ) : $this->msg( 
'centralnotice-all' )->text();
+               $deviceLabel = $this->device ?
+                       htmlspecialchars( $this->device ) : $this->msg( 
'centralnotice-all' )->text();
 
                $htmlOut .= Xml::tags( 'p', null,
                        $this->msg(
                                'centralnotice-allocation-description',
                                $languageLabel,
                                $projectLabel,
-                               $countryLabel
+                               $countryLabel,
+                               $deviceLabel
                        )->text()
                );
 
@@ -413,7 +418,7 @@
                // are identical because of e.g. z-index
                foreach ( array( true, false ) as $isAnon ) {
                        for ( $bucket = 0; $bucket < $numBuckets; $bucket++ ) {
-                               $variations[$isAnon][$bucket] = 
ApiCentralNoticeAllocations::getAllocationInformation(
+                               $variations[$isAnon][$bucket] = 
ApiCentralNoticeAllocations::getBannerAllocation(
                                        $project, $country, $language,
                                        $isAnon ? 'true' : 'false',
                                        $bucket

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

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

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

Reply via email to