r72952 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72951‎ | r72952 | r72953 >
Date:01:44, 14 September 2010
Author:kaldari
Status:ok (Comments)
Tags:
Comment:
adding some functions for geotargeting, support for updating geo
Modified paths:
  • /trunk/extensions/CentralNotice/SpecialCentralNotice.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/SpecialCentralNotice.php
@@ -563,26 +563,37 @@
564564 }
565565
566566 // Handle locking/unlocking campaign
567 - if ( $wgRequest->getArray( 'locked' ) ) {
 567+ if ( $wgRequest->getCheck( 'locked' ) ) {
568568 $this->updateLock( $notice, '1' );
569569 } else {
570570 $this->updateLock( $notice, 0 );
571571 }
572572
573573 // Handle enabling/disabling campaign
574 - if ( $wgRequest->getArray( 'enabled' ) ) {
 574+ if ( $wgRequest->getCheck( 'enabled' ) ) {
575575 $this->updateEnabled( $notice, '1' );
576576 } else {
577577 $this->updateEnabled( $notice, 0 );
578578 }
579579
580580 // Handle setting campaign to preferred/not preferred
581 - if ( $wgRequest->getArray( 'preferred' ) ) {
 581+ if ( $wgRequest->getCheck( 'preferred' ) ) {
582582 $this->updatePreferred( $notice, '1' );
583583 } else {
584584 $this->updatePreferred( $notice, 0 );
585585 }
586586
 587+ // Handle updating geotargeting
 588+ if ( $wgRequest->getCheck( 'geotargeted' ) ) {
 589+ $this->updateGeotargeted( $notice, '1' );
 590+ $countries = $wgRequest->getArray( 'geo_countries' );
 591+ if ( $countries ) {
 592+ $this->updateCountries( $notice, $countries );
 593+ }
 594+ } else {
 595+ $this->updateGeotargeted( $notice, 0 );
 596+ }
 597+
587598 // Handle updating the start and end settings
588599 $start = $wgRequest->getArray( 'start' );
589600 $end = $wgRequest->getArray( 'end' );
@@ -736,7 +747,8 @@
737748 'not_enabled',
738749 'not_preferred',
739750 'not_project',
740 - 'not_locked'
 751+ 'not_locked',
 752+ 'not_geo'
741753 ),
742754 array( 'not_name' => $notice ),
743755 __METHOD__
@@ -765,6 +777,8 @@
766778 $isLocked = $wgRequest->getCheck( 'locked' );
767779 $projectSelected = $wgRequest->getVal( 'project_name' );
768780 $noticeLanguages = $wgRequest->getArray( 'project_languages', array() );
 781+ $isGeotargeted = $wgRequest->getCheck( 'geotargeted' );
 782+ $countries = $wgRequest->getArray( 'geo_countries', array() );
769783 } else { // Defaults
770784 $startTimestamp = $row->not_start;
771785 $endTimestamp = $row->not_end;
@@ -773,6 +787,8 @@
774788 $isLocked = ( $row->not_locked == '1' );
775789 $projectSelected = $row->not_project;
776790 $noticeLanguages = $this->getNoticeLanguages( $notice );
 791+ $isGeotargeted = ( $row->not_geo == '1' );
 792+ $countries = $this->getNoticeCountries( $notice );
777793 }
778794
779795 // Build Html
@@ -814,11 +830,11 @@
815831 // Countries
816832 $htmlOut .= Xml::openElement( 'tr' );
817833 $htmlOut .= Xml::tags( 'td', array(), Xml::label( wfMsgHtml( 'centralnotice-geotargeted' ), 'geotargeted' ) );
818 - $htmlOut .= Xml::tags( 'td', array(), Xml::check( 'geotargeted', false, wfArrayMerge( $readonly, array( 'value' => $row->not_name, 'id' => 'geotargeted' ) ) ) );
 834+ $htmlOut .= Xml::tags( 'td', array(), Xml::check( 'geotargeted', $isGeotargeted, wfArrayMerge( $readonly, array( 'value' => $row->not_name, 'id' => 'geotargeted' ) ) ) );
819835 $htmlOut .= Xml::closeElement( 'tr' );
820836 $htmlOut .= Xml::openElement( 'tr', array( 'id'=>'geoMultiSelector' ) );
821837 $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ), wfMsgHtml( 'centralnotice-countries' ) );
822 - $htmlOut .= Xml::tags( 'td', array(), $this->geoMultiSelector() );
 838+ $htmlOut .= Xml::tags( 'td', array(), $this->geoMultiSelector( $countries ) );
823839 $htmlOut .= Xml::closeElement( 'tr' );
824840 // Enabled
825841 $htmlOut .= Xml::openElement( 'tr' );
@@ -1173,6 +1189,20 @@
11741190 }
11751191 return $languages;
11761192 }
 1193+
 1194+ function getNoticeCountries( $noticeName ) {
 1195+ $dbr = wfGetDB( DB_SLAVE );
 1196+ $eNoticeName = htmlspecialchars( $noticeName );
 1197+ $row = $dbr->selectRow( 'cn_notices', 'not_id', array( 'not_name' => $eNoticeName ) );
 1198+ $countries = array();
 1199+ if ( $row ) {
 1200+ $res = $dbr->select( 'cn_notice_countries', 'nc_country', array( 'nc_notice_id' => $row->not_id ) );
 1201+ foreach ( $res as $countryRow ) {
 1202+ $countries[] = $countryRow->nc_country;
 1203+ }
 1204+ }
 1205+ return $countries;
 1206+ }
11771207
11781208 function getNoticeProjectName( $noticeName ) {
11791209 $dbr = wfGetDB( DB_SLAVE );
@@ -1246,7 +1276,7 @@
12471277 );
12481278 }
12491279 }
1250 -
 1280+
12511281 /**
12521282 * Update the preferred/not preferred state of a campaign
12531283 */
@@ -1265,6 +1295,23 @@
12661296 }
12671297
12681298 /**
 1299+ * Update the geotargeted/not geotargeted state of a campaign
 1300+ */
 1301+ function updateGeotargeted( $noticeName, $isGeotargeted ) {
 1302+ global $wgOut;
 1303+
 1304+ if ( !$this->noticeExists( $noticeName ) ) {
 1305+ $this->showError( 'centralnotice-doesnt-exist' );
 1306+ } else {
 1307+ $dbw = wfGetDB( DB_MASTER );
 1308+ $res = $dbw->update( 'cn_notices',
 1309+ array( 'not_geo' => $isGeotargeted ),
 1310+ array( 'not_name' => $noticeName )
 1311+ );
 1312+ }
 1313+ }
 1314+
 1315+ /**
12691316 * Update the locked/unlocked state of a campaign
12701317 */
12711318 function updateLock( $noticeName, $isLocked ) {
@@ -1404,6 +1451,36 @@
14051452 $dbw->commit();
14061453 }
14071454
 1455+ function updateCountries( $notice, $newCountries ) {
 1456+ $dbw = wfGetDB( DB_MASTER );
 1457+ $dbw->begin();
 1458+
 1459+ // Get the previously assigned languages
 1460+ $oldCountries = array();
 1461+ $oldCountries = $this->getNoticeCountries( $notice );
 1462+
 1463+ // Get the notice id
 1464+ $row = $dbw->selectRow( 'cn_notices', 'not_id', array( 'not_name' => $notice ) );
 1465+
 1466+ // Add newly assigned countries
 1467+ $addCountries = array_diff( $newCountries, $oldCountries );
 1468+ $insertArray = array();
 1469+ foreach( $addCountries as $code ) {
 1470+ $insertArray[] = array( 'nc_notice_id' => $row->not_id, 'nc_country' => $code );
 1471+ }
 1472+ $res = $dbw->insert( 'cn_notice_countries', $insertArray, __METHOD__, array( 'IGNORE' ) );
 1473+
 1474+ // Remove disassociated countries
 1475+ $removeCountries = array_diff( $oldCountries, $newCountries );
 1476+ if ( $removeCountries ) {
 1477+ $res = $dbw->delete( 'cn_notice_countries',
 1478+ array( 'nc_notice_id' => $row->not_id, 'nc_country' => $removeCountries )
 1479+ );
 1480+ }
 1481+
 1482+ $dbw->commit();
 1483+ }
 1484+
14081485 public static function noticeExists( $noticeName ) {
14091486 $dbr = wfGetDB( DB_SLAVE );
14101487 $eNoticeName = htmlspecialchars( $noticeName );

Follow-up revisions

RevisionCommit summaryAuthorDate
r73099changes per comments at r72952kaldari22:55, 15 September 2010

Comments

#Comment by Catrope (talk | contribs)   18:55, 14 September 2010
+						$this->updateGeotargeted( $notice, '1' );
[...]
+						$this->updateGeotargeted( $notice, 0 );

Why the discrepancy?

+		// Get the previously assigned languages
+		$oldCountries = array();
+		$oldCountries = $this->getNoticeCountries( $notice );

This just reads strange. The comment doesn't match the code and the same variable is assigned twice in a row.

+		$dbw->begin();
[...]
+		$dbw->commit();

Does this really need to be in its own transaction?

#Comment by Kaldari (talk | contribs)   22:55, 15 September 2010

All issues addressed in r73099.

Status & tagging log