/**
  * Generate JSON for the specified site
  */
 function getJsonList()
 {
     $banners = array();
     // See if we have any preferred campaigns for this language and project
     $campaigns = CentralNoticeDB::getCampaigns($this->project, $this->language, null, 1, 1, $this->location);
     // Quick short circuit to show preferred campaigns
     if ($campaigns) {
         // Pull banners
         $banners = CentralNoticeDB::getCampaignBanners($campaigns);
     }
     // Didn't find any preferred banners so do an old style lookup
     if (!$banners) {
         $banners = CentralNoticeDB::getBannersByTarget($this->project, $this->language, $this->location);
     }
     return FormatJson::encode($banners);
 }
 /**
  * Lookup function for active banners under a given language/project/location. This function is
  * called by SpecialBannerListLoader::getJsonList() in order to build the banner list JSON for
  * each project.
  * @param $project string
  * @param $language string
  * @param $location string
  * @return array a 2D array of running banners with associated weights and settings
  */
 static function getBannersByTarget($project, $language, $location = null)
 {
     global $wgCentralDBname;
     $campaigns = array();
     $dbr = wfGetDB(DB_SLAVE, array(), $wgCentralDBname);
     $encTimestamp = $dbr->addQuotes($dbr->timestamp());
     // Pull non-geotargeted campaigns
     $campaignResults1 = $dbr->select(array('cn_notices', 'cn_notice_projects', 'cn_notice_languages'), array('not_id'), array("not_start <= {$encTimestamp}", "not_end >= {$encTimestamp}", 'not_enabled = 1', 'not_geo = 0', 'np_notice_id = cn_notices.not_id', 'np_project' => $project, 'nl_notice_id = cn_notices.not_id', 'nl_language' => $language), __METHOD__);
     foreach ($campaignResults1 as $row) {
         $campaigns[] = $row->not_id;
     }
     if ($location) {
         // Normalize location parameter (should be an uppercase 2-letter country code)
         preg_match('/[a-zA-Z][a-zA-Z]/', $location, $matches);
         if ($matches) {
             $location = strtoupper($matches[0]);
             // Pull geotargeted campaigns
             $campaignResults2 = $dbr->select(array('cn_notices', 'cn_notice_projects', 'cn_notice_languages', 'cn_notice_countries'), array('not_id'), array("not_start <= {$encTimestamp}", "not_end >= {$encTimestamp}", 'not_enabled = 1', 'not_geo = 1', 'nc_notice_id = cn_notices.not_id', 'nc_country' => $location, 'np_notice_id = cn_notices.not_id', 'np_project' => $project, 'nl_notice_id = cn_notices.not_id', 'nl_language' => $language), __METHOD__);
             foreach ($campaignResults2 as $row) {
                 $campaigns[] = $row->not_id;
             }
         }
     }
     $banners = array();
     if ($campaigns) {
         // Pull all banners assigned to the campaigns
         $banners = CentralNoticeDB::getCampaignBanners($campaigns);
     }
     return $banners;
 }
 public function testGetCampaignBanners()
 {
     $campaignId = CentralNotice::getNoticeId('PHPUnitTestCampaign');
     $this->assertEquals('[{"name":"PHPUnitTestBanner","weight":25,"display_anon":1,"display_account":1,"fundraising":1,"landing_pages":"JA1, JA2","campaign":"PHPUnitTestCampaign"}]', json_encode(CentralNoticeDB::getCampaignBanners($campaignId)));
 }