/**
  * Generate the HTML for the requested banner
  * @throws SpecialBannerLoaderException
  */
 function getHtmlNotice($bannerName)
 {
     // Make sure the banner exists
     if (CentralNoticeDB::bannerExists($bannerName)) {
         $this->bannerName = $bannerName;
         $bannerHtml = '';
         $bannerHtml .= preg_replace_callback('/{{{(.*?)}}}/', array($this, 'getNoticeField'), $this->getNoticeTemplate());
         return $bannerHtml;
     }
     return '';
 }
 /**
  * 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;
 }
 /**
  * Handle different types of page requests
  */
 function execute($sub)
 {
     global $wgOut, $wgLang, $wgRequest, $wgNoticeProjects, $wgLanguageCode, $wgNoticeProject;
     $this->project = $wgRequest->getText('project', $wgNoticeProject);
     $this->language = $wgRequest->getText('language', $wgLanguageCode);
     // If the form has been submitted, the country code should be passed along.
     $locationSubmitted = $wgRequest->getVal('country');
     $this->location = $locationSubmitted ? $locationSubmitted : $this->location;
     // Convert submitted location to boolean value. If it true, showList() will be called.
     $locationSubmitted = (bool) $locationSubmitted;
     // Begin output
     $this->setHeaders();
     // Output ResourceLoader module for styling and javascript functions
     $wgOut->addModules(array('ext.centralNotice.interface', 'ext.centralNotice.bannerStats'));
     // Initialize error variable
     $this->centralNoticeError = false;
     // Show summary
     $wgOut->addWikiMsg('centralnotice-summary');
     // Show header
     CentralNotice::printHeader();
     // Begin Banners tab content
     $wgOut->addHTML(Html::openElement('div', array('id' => 'preferences')));
     $htmlOut = '';
     // Begin Allocation selection fieldset
     $htmlOut .= Html::openElement('fieldset', array('class' => 'prefsection'));
     $htmlOut .= Html::openElement('form', array('method' => 'get'));
     $htmlOut .= Html::element('h2', null, wfMsg('centralnotice-view-allocation'));
     $htmlOut .= Xml::tags('p', null, wfMsg('centralnotice-allocation-instructions'));
     $htmlOut .= Html::openElement('table', array('id' => 'envpicker', 'cellpadding' => 7));
     $htmlOut .= Html::openElement('tr');
     $htmlOut .= Xml::tags('td', array('style' => 'width: 20%;'), wfMsg('centralnotice-project-name'));
     $htmlOut .= Html::openElement('td');
     $htmlOut .= Html::openElement('select', array('name' => 'project'));
     foreach ($wgNoticeProjects as $value) {
         $htmlOut .= Xml::option($value, $value, $value === $this->project);
     }
     $htmlOut .= Html::closeElement('select');
     $htmlOut .= Html::closeElement('td');
     $htmlOut .= Html::closeElement('tr');
     $htmlOut .= Html::openElement('tr');
     $htmlOut .= Xml::tags('td', array('valign' => 'top'), wfMsg('centralnotice-project-lang'));
     $htmlOut .= Html::openElement('td');
     // Make sure the site language is in the list; a custom language code
     // might not have a defined name...
     $languages = Language::getLanguageNames(true);
     if (!array_key_exists($wgLanguageCode, $languages)) {
         $languages[$wgLanguageCode] = $wgLanguageCode;
     }
     ksort($languages);
     $htmlOut .= Html::openElement('select', array('name' => 'language'));
     foreach ($languages as $code => $name) {
         $htmlOut .= Xml::option(wfMsg('centralnotice-language-listing', $code, $name), $code, $code === $this->language);
     }
     $htmlOut .= Html::closeElement('select');
     $htmlOut .= Html::closeElement('td');
     $htmlOut .= Html::closeElement('tr');
     $htmlOut .= Html::openElement('tr');
     $htmlOut .= Xml::tags('td', array(), wfMsg('centralnotice-country'));
     $htmlOut .= Html::openElement('td');
     $userLanguageCode = $wgLang->getCode();
     $countries = CentralNoticeDB::getCountriesList($userLanguageCode);
     $htmlOut .= Html::openElement('select', array('name' => 'country'));
     foreach ($countries as $code => $name) {
         $htmlOut .= Xml::option($name, $code, $code === $this->location);
     }
     $htmlOut .= Html::closeElement('select');
     $htmlOut .= Html::closeElement('td');
     $htmlOut .= Html::closeElement('tr');
     $htmlOut .= Html::closeElement('table');
     $htmlOut .= Xml::tags('div', array('class' => 'cn-buttons'), Xml::submitButton(wfMsg('centralnotice-view')));
     $htmlOut .= Html::closeElement('form');
     // End Allocation selection fieldset
     $htmlOut .= Html::closeElement('fieldset');
     $wgOut->addHTML($htmlOut);
     // Handle form submissions
     if ($locationSubmitted) {
         $this->showList();
     }
     // End Banners tab content
     $wgOut->addHTML(Html::closeElement('div'));
 }
 /**
  * Generates a multiple select list of all countries.
  * @param $selected The country codes of the selected countries
  * @return multiple select list
  */
 function geoMultiSelector($selected = array())
 {
     global $wgLang;
     $userLanguageCode = $wgLang->getCode();
     $countries = CentralNoticeDB::getCountriesList($userLanguageCode);
     $options = "\n";
     foreach ($countries as $code => $name) {
         $options .= Xml::option($name, $code, in_array($code, $selected)) . "\n";
     }
     $htmlOut = '';
     if ($this->editable) {
         $htmlOut .= Xml::tags('select', array('multiple' => 'multiple', 'size' => 6, 'id' => 'geo_countries[]', 'name' => 'geo_countries[]'), $options);
     } else {
         $htmlOut .= Xml::tags('select', array('multiple' => 'multiple', 'size' => 6, 'id' => 'geo_countries[]', 'name' => 'geo_countries[]', 'disabled' => 'disabled'), $options);
     }
     return $htmlOut;
 }
 /**
  * Update a banner
  */
 private function editTemplate($name, $body, $displayAnon, $displayAccount, $fundraising, $autolink, $landingPages)
 {
     if ($body == '' || $name == '') {
         $this->showError('centralnotice-null-string');
         return;
     }
     $initialBannerSettings = CentralNoticeDB::getBannerSettings($name, true);
     $dbr = wfGetDB(DB_SLAVE);
     $res = $dbr->select('cn_templates', 'tmp_name', array('tmp_name' => $name), __METHOD__);
     if ($dbr->numRows($res) == 1) {
         $dbw = wfGetDB(DB_MASTER);
         $res = $dbw->update('cn_templates', array('tmp_display_anon' => $displayAnon, 'tmp_display_account' => $displayAccount, 'tmp_fundraising' => $fundraising, 'tmp_autolink' => $autolink, 'tmp_landing_pages' => $landingPages), array('tmp_name' => $name));
         // Perhaps these should move into the db as blob
         $article = new Article(Title::newFromText("centralnotice-template-{$name}", NS_MEDIAWIKI));
         $article->doEdit($body, '', EDIT_FORCE_BOT);
         $bannerId = SpecialNoticeTemplate::getTemplateId($name);
         $finalBannerSettings = CentralNoticeDB::getBannerSettings($name, true);
         // If there are any difference between the old settings and the new settings, log them.
         $diffs = array_diff_assoc($initialBannerSettings, $finalBannerSettings);
         if ($diffs) {
             $this->logBannerChange('modified', $bannerId, $initialBannerSettings, $finalBannerSettings);
         }
         return;
     }
 }
 public function testGetCampaignSettings()
 {
     $campaignArray = array('enabled' => 0, 'end' => 20110818235500.0, 'geo' => 1, 'locked' => 0, 'preferred' => 0, 'start' => 20110718235500.0);
     $this->assertEquals($campaignArray, CentralNoticeDB::getCampaignSettings('PHPUnitTestCampaign', false));
 }