r92510 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92509‎ | r92510 | r92511 >
Date:00:02, 19 July 2011
Author:kaldari
Status:ok
Tags:
Comment:
code clean-up: updating method names, adding comments, etc.
Modified paths:
  • /trunk/extensions/CentralNotice/CentralNotice.db.php (modified) (history)
  • /trunk/extensions/CentralNotice/special/SpecialCentralNotice.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/CentralNotice.db.php
@@ -5,6 +5,9 @@
66 exit( 1 );
77 }
88
 9+/**
 10+ * Static methods that retrieve information from the database.
 11+ */
912 class CentralNoticeDB {
1013
1114 /* Functions */
Index: trunk/extensions/CentralNotice/special/SpecialCentralNotice.php
@@ -58,7 +58,7 @@
5959 if ( $wgUser->matchEditToken( $wgRequest->getVal( 'authtoken' ) ) ) {
6060
6161 // Handle adding a campaign
62 - if ( $method == 'addNotice' ) {
 62+ if ( $method == 'addCampaign' ) {
6363
6464 $noticeName = $wgRequest->getVal( 'noticeName' );
6565 $start = $wgRequest->getArray( 'start' );
@@ -69,7 +69,7 @@
7070 if ( $noticeName == '' ) {
7171 $this->showError( 'centralnotice-null-string' );
7272 } else {
73 - $this->addNotice( $noticeName, '0', $start, $projects,
 73+ $this->addCampaign( $noticeName, '0', $start, $projects,
7474 $project_languages, $geotargeted, $geo_countries );
7575 }
7676
@@ -77,11 +77,11 @@
7878 } else {
7979
8080 // Handle removing campaigns
81 - $toRemove = $wgRequest->getArray( 'removeNotices' );
 81+ $toRemove = $wgRequest->getArray( 'removeCampaigns' );
8282 if ( $toRemove ) {
8383 // Remove campaigns in list
8484 foreach ( $toRemove as $notice ) {
85 - $this->removeNotice( $notice );
 85+ $this->removeCampaign( $notice );
8686 }
8787 }
8888
@@ -455,7 +455,7 @@
456456
457457 if ( $this->editable ) {
458458 // Remove
459 - $fields[] = Xml::check( 'removeNotices[]', false,
 459+ $fields[] = Xml::check( 'removeCampaigns[]', false,
460460 array( 'value' => $row->not_name, 'class' => 'noshiftselect' ) );
461461 }
462462
@@ -497,7 +497,7 @@
498498 if ( $this->editable ) {
499499
500500 // If there was an error, we'll need to restore the state of the form
501 - if ( $wgRequest->wasPosted() && ( $wgRequest->getVal( 'method' ) == 'addNotice' ) ) {
 501+ if ( $wgRequest->wasPosted() && ( $wgRequest->getVal( 'method' ) == 'addCampaign' ) ) {
502502 $startArray = $wgRequest->getArray( 'start' );
503503 $startTimestamp = $startArray['year'] .
504504 $startArray['month'] .
@@ -520,7 +520,7 @@
521521 $htmlOut .= Xml::openElement( 'form', array( 'method' => 'post' ) );
522522 $htmlOut .= Xml::element( 'h2', null, wfMsg( 'centralnotice-add-notice' ) );
523523 $htmlOut .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
524 - $htmlOut .= Html::hidden( 'method', 'addNotice' );
 524+ $htmlOut .= Html::hidden( 'method', 'addCampaign' );
525525
526526 $htmlOut .= Xml::openElement( 'table', array ( 'cellpadding' => 9 ) );
527527
@@ -608,7 +608,7 @@
609609
610610 // Handle removing campaign
611611 if ( $wgRequest->getVal( 'remove' ) ) {
612 - $this->removeNotice( $notice );
 612+ $this->removeCampaign( $notice );
613613 if ( !$this->centralNoticeError ) {
614614 // Leave campaign detail interface
615615 $wgOut->redirect( $this->getTitle()->getLocalUrl() );
@@ -1082,18 +1082,29 @@
10831083 return $htmlOut;
10841084 }
10851085
1086 - function addNotice( $noticeName, $enabled, $start, $projects,
 1086+ /**
 1087+ * Add a new campaign to the database
 1088+ * @param $noticeName string: Name of the campaign
 1089+ * @param $enabled int: Boolean setting, 0 or 1
 1090+ * @param $start array: Start date and time
 1091+ * @param $projects array: Targeted project types (wikipedia, wikibooks, etc.)
 1092+ * @param $project_languages array: Targeted project languages (en, de, etc.)
 1093+ * @param $geotargeted int: Boolean setting, 0 or 1
 1094+ * @param $geo_countries array: Targeted countries
 1095+ * @return true or null
 1096+ */
 1097+ function addCampaign( $noticeName, $enabled, $start, $projects,
10871098 $project_languages, $geotargeted, $geo_countries )
10881099 {
10891100 if ( $this->noticeExists( $noticeName ) ) {
10901101 $this->showError( 'centralnotice-notice-exists' );
1091 - return;
 1102+ return null;
10921103 } elseif ( empty( $projects ) ) {
10931104 $this->showError( 'centralnotice-no-project' );
1094 - return;
 1105+ return null;
10951106 } elseif ( empty( $project_languages ) ) {
10961107 $this->showError( 'centralnotice-no-language' );
1097 - return;
 1108+ return null;
10981109 } else {
10991110 if ( !$geo_countries ) $geo_countries = array();
11001111 $dbw = wfGetDB( DB_MASTER );
@@ -1129,82 +1140,93 @@
11301141 );
11311142 $not_id = $dbw->insertId();
11321143
1133 - // Do multi-row insert for campaign projects
1134 - $insertArray = array();
1135 - foreach( $projects as $project ) {
1136 - $insertArray[] = array( 'np_notice_id' => $not_id, 'np_project' => $project );
1137 - }
1138 - $res = $dbw->insert( 'cn_notice_projects', $insertArray,
1139 - __METHOD__, array( 'IGNORE' ) );
1140 -
1141 - // Do multi-row insert for campaign languages
1142 - $insertArray = array();
1143 - foreach( $project_languages as $code ) {
1144 - $insertArray[] = array( 'nl_notice_id' => $not_id, 'nl_language' => $code );
1145 - }
1146 - $res = $dbw->insert( 'cn_notice_languages', $insertArray,
1147 - __METHOD__, array( 'IGNORE' ) );
 1144+ if ( $not_id ) {
11481145
1149 - if ( $geotargeted ) {
1150 - // Do multi-row insert for campaign countries
 1146+ // Do multi-row insert for campaign projects
11511147 $insertArray = array();
1152 - foreach( $geo_countries as $code ) {
1153 - $insertArray[] = array( 'nc_notice_id' => $not_id, 'nc_country' => $code );
 1148+ foreach( $projects as $project ) {
 1149+ $insertArray[] = array( 'np_notice_id' => $not_id, 'np_project' => $project );
11541150 }
1155 - $res = $dbw->insert( 'cn_notice_countries', $insertArray,
 1151+ $res = $dbw->insert( 'cn_notice_projects', $insertArray,
11561152 __METHOD__, array( 'IGNORE' ) );
 1153+
 1154+ // Do multi-row insert for campaign languages
 1155+ $insertArray = array();
 1156+ foreach( $project_languages as $code ) {
 1157+ $insertArray[] = array( 'nl_notice_id' => $not_id, 'nl_language' => $code );
 1158+ }
 1159+ $res = $dbw->insert( 'cn_notice_languages', $insertArray,
 1160+ __METHOD__, array( 'IGNORE' ) );
 1161+
 1162+ if ( $geotargeted ) {
 1163+ // Do multi-row insert for campaign countries
 1164+ $insertArray = array();
 1165+ foreach( $geo_countries as $code ) {
 1166+ $insertArray[] = array( 'nc_notice_id' => $not_id, 'nc_country' => $code );
 1167+ }
 1168+ $res = $dbw->insert( 'cn_notice_countries', $insertArray,
 1169+ __METHOD__, array( 'IGNORE' ) );
 1170+ }
 1171+
 1172+ $dbw->commit();
 1173+
 1174+ // Log the creation of the campaign
 1175+ $beginSettings = array();
 1176+ $endSettings = array(
 1177+ 'projects' => implode( ", ", $projects ),
 1178+ 'languages' => implode( ", ", $project_languages ),
 1179+ 'countries' => implode( ", ", $geo_countries ),
 1180+ 'start' => $dbw->timestamp( $startTs ),
 1181+ 'end' => $dbw->timestamp( $endTs ),
 1182+ 'enabled' => $enabled,
 1183+ 'preferred' => 0,
 1184+ 'locked' => 0,
 1185+ 'geo' => $geotargeted
 1186+ );
 1187+ $this->logCampaignChange( 'created', $not_id, $beginSettings, $endSettings );
 1188+
 1189+ return true;
 1190+
 1191+ } else {
 1192+ return null;
11571193 }
1158 -
1159 - $dbw->commit();
1160 -
1161 - // Log the creation of the campaign
1162 - $beginSettings = array();
1163 - $endSettings = array(
1164 - 'projects' => implode( ", ", $projects ),
1165 - 'languages' => implode( ", ", $project_languages ),
1166 - 'countries' => implode( ", ", $geo_countries ),
1167 - 'start' => $dbw->timestamp( $startTs ),
1168 - 'end' => $dbw->timestamp( $endTs ),
1169 - 'enabled' => $enabled,
1170 - 'preferred' => 0,
1171 - 'locked' => 0,
1172 - 'geo' => $geotargeted
1173 - );
1174 - $this->logCampaignChange( 'created', $not_id, $beginSettings, $endSettings );
1175 -
1176 - return;
11771194 }
11781195 }
11791196
1180 - function removeNotice( $noticeName ) {
 1197+ /**
 1198+ * Remove a campaign from the database
 1199+ * @param $noticeName string: Name of the campaign
 1200+ * @return true or null
 1201+ */
 1202+ function removeCampaign( $campaignName ) {
11811203 $dbr = wfGetDB( DB_SLAVE );
11821204
11831205 $res = $dbr->select( 'cn_notices', 'not_name, not_locked',
1184 - array( 'not_name' => $noticeName )
 1206+ array( 'not_name' => $campaignName )
11851207 );
11861208 if ( $dbr->numRows( $res ) < 1 ) {
11871209 $this->showError( 'centralnotice-remove-notice-doesnt-exist' );
1188 - return;
 1210+ return null;
11891211 }
11901212 $row = $dbr->fetchObject( $res );
11911213 if ( $row->not_locked == '1' ) {
11921214 $this->showError( 'centralnotice-notice-is-locked' );
1193 - return;
 1215+ return null;
11941216 } else {
11951217 // Log the removal of the campaign
1196 - $noticeId = CentralNotice::getNoticeId( $noticeName );
1197 - $this->logCampaignChange( 'removed', $noticeId );
 1218+ $campaignId = CentralNotice::getNoticeId( $campaignName );
 1219+ $this->logCampaignChange( 'removed', $campaignId );
11981220
11991221 $dbw = wfGetDB( DB_MASTER );
12001222 $dbw->begin();
1201 - $res = $dbw->delete( 'cn_assignments', array ( 'not_id' => $noticeId ) );
1202 - $res = $dbw->delete( 'cn_notices', array ( 'not_name' => $noticeName ) );
1203 - $res = $dbw->delete( 'cn_notice_languages', array ( 'nl_notice_id' => $noticeId ) );
1204 - $res = $dbw->delete( 'cn_notice_projects', array ( 'np_notice_id' => $noticeId ) );
1205 - $res = $dbw->delete( 'cn_notice_countries', array ( 'nc_notice_id' => $noticeId ) );
 1223+ $res = $dbw->delete( 'cn_assignments', array ( 'not_id' => $campaignId ) );
 1224+ $res = $dbw->delete( 'cn_notices', array ( 'not_name' => $campaignName ) );
 1225+ $res = $dbw->delete( 'cn_notice_languages', array ( 'nl_notice_id' => $campaignId ) );
 1226+ $res = $dbw->delete( 'cn_notice_projects', array ( 'np_notice_id' => $campaignId ) );
 1227+ $res = $dbw->delete( 'cn_notice_countries', array ( 'nc_notice_id' => $campaignId ) );
12061228 $dbw->commit();
12071229
1208 - return;
 1230+ return true;
12091231 }
12101232 }
12111233
@@ -1360,7 +1382,7 @@
13611383 * Update a boolean setting on a campaign
13621384 * @param $noticeName string: Name of the campaign
13631385 * @param $settingName string: Name of a boolean setting (enabled, preferred, locked, or geo)
1364 - * @param $settingValue boolean: Value to use for the setting
 1386+ * @param $settingValue int: Value to use for the setting, 0 or 1
13651387 */
13661388 private function setBooleanCampaignSetting( $noticeName, $settingName, $settingValue ) {
13671389 if ( !$this->noticeExists( $noticeName ) ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r100100MFT r92510, r92676, r96496, r97304, r99160, r99165, r99169, r99176, r99178, r...awjrichards23:56, 17 October 2011

Status & tagging log