Index: trunk/extensions/CentralNotice/CentralNotice.db.php |
— | — | @@ -5,6 +5,9 @@ |
6 | 6 | exit( 1 ); |
7 | 7 | } |
8 | 8 | |
| 9 | +/** |
| 10 | + * Static methods that retrieve information from the database. |
| 11 | + */ |
9 | 12 | class CentralNoticeDB { |
10 | 13 | |
11 | 14 | /* Functions */ |
Index: trunk/extensions/CentralNotice/special/SpecialCentralNotice.php |
— | — | @@ -58,7 +58,7 @@ |
59 | 59 | if ( $wgUser->matchEditToken( $wgRequest->getVal( 'authtoken' ) ) ) { |
60 | 60 | |
61 | 61 | // Handle adding a campaign |
62 | | - if ( $method == 'addNotice' ) { |
| 62 | + if ( $method == 'addCampaign' ) { |
63 | 63 | |
64 | 64 | $noticeName = $wgRequest->getVal( 'noticeName' ); |
65 | 65 | $start = $wgRequest->getArray( 'start' ); |
— | — | @@ -69,7 +69,7 @@ |
70 | 70 | if ( $noticeName == '' ) { |
71 | 71 | $this->showError( 'centralnotice-null-string' ); |
72 | 72 | } else { |
73 | | - $this->addNotice( $noticeName, '0', $start, $projects, |
| 73 | + $this->addCampaign( $noticeName, '0', $start, $projects, |
74 | 74 | $project_languages, $geotargeted, $geo_countries ); |
75 | 75 | } |
76 | 76 | |
— | — | @@ -77,11 +77,11 @@ |
78 | 78 | } else { |
79 | 79 | |
80 | 80 | // Handle removing campaigns |
81 | | - $toRemove = $wgRequest->getArray( 'removeNotices' ); |
| 81 | + $toRemove = $wgRequest->getArray( 'removeCampaigns' ); |
82 | 82 | if ( $toRemove ) { |
83 | 83 | // Remove campaigns in list |
84 | 84 | foreach ( $toRemove as $notice ) { |
85 | | - $this->removeNotice( $notice ); |
| 85 | + $this->removeCampaign( $notice ); |
86 | 86 | } |
87 | 87 | } |
88 | 88 | |
— | — | @@ -455,7 +455,7 @@ |
456 | 456 | |
457 | 457 | if ( $this->editable ) { |
458 | 458 | // Remove |
459 | | - $fields[] = Xml::check( 'removeNotices[]', false, |
| 459 | + $fields[] = Xml::check( 'removeCampaigns[]', false, |
460 | 460 | array( 'value' => $row->not_name, 'class' => 'noshiftselect' ) ); |
461 | 461 | } |
462 | 462 | |
— | — | @@ -497,7 +497,7 @@ |
498 | 498 | if ( $this->editable ) { |
499 | 499 | |
500 | 500 | // 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' ) ) { |
502 | 502 | $startArray = $wgRequest->getArray( 'start' ); |
503 | 503 | $startTimestamp = $startArray['year'] . |
504 | 504 | $startArray['month'] . |
— | — | @@ -520,7 +520,7 @@ |
521 | 521 | $htmlOut .= Xml::openElement( 'form', array( 'method' => 'post' ) ); |
522 | 522 | $htmlOut .= Xml::element( 'h2', null, wfMsg( 'centralnotice-add-notice' ) ); |
523 | 523 | $htmlOut .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ); |
524 | | - $htmlOut .= Html::hidden( 'method', 'addNotice' ); |
| 524 | + $htmlOut .= Html::hidden( 'method', 'addCampaign' ); |
525 | 525 | |
526 | 526 | $htmlOut .= Xml::openElement( 'table', array ( 'cellpadding' => 9 ) ); |
527 | 527 | |
— | — | @@ -608,7 +608,7 @@ |
609 | 609 | |
610 | 610 | // Handle removing campaign |
611 | 611 | if ( $wgRequest->getVal( 'remove' ) ) { |
612 | | - $this->removeNotice( $notice ); |
| 612 | + $this->removeCampaign( $notice ); |
613 | 613 | if ( !$this->centralNoticeError ) { |
614 | 614 | // Leave campaign detail interface |
615 | 615 | $wgOut->redirect( $this->getTitle()->getLocalUrl() ); |
— | — | @@ -1082,18 +1082,29 @@ |
1083 | 1083 | return $htmlOut; |
1084 | 1084 | } |
1085 | 1085 | |
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, |
1087 | 1098 | $project_languages, $geotargeted, $geo_countries ) |
1088 | 1099 | { |
1089 | 1100 | if ( $this->noticeExists( $noticeName ) ) { |
1090 | 1101 | $this->showError( 'centralnotice-notice-exists' ); |
1091 | | - return; |
| 1102 | + return null; |
1092 | 1103 | } elseif ( empty( $projects ) ) { |
1093 | 1104 | $this->showError( 'centralnotice-no-project' ); |
1094 | | - return; |
| 1105 | + return null; |
1095 | 1106 | } elseif ( empty( $project_languages ) ) { |
1096 | 1107 | $this->showError( 'centralnotice-no-language' ); |
1097 | | - return; |
| 1108 | + return null; |
1098 | 1109 | } else { |
1099 | 1110 | if ( !$geo_countries ) $geo_countries = array(); |
1100 | 1111 | $dbw = wfGetDB( DB_MASTER ); |
— | — | @@ -1129,82 +1140,93 @@ |
1130 | 1141 | ); |
1131 | 1142 | $not_id = $dbw->insertId(); |
1132 | 1143 | |
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 ) { |
1148 | 1145 | |
1149 | | - if ( $geotargeted ) { |
1150 | | - // Do multi-row insert for campaign countries |
| 1146 | + // Do multi-row insert for campaign projects |
1151 | 1147 | $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 ); |
1154 | 1150 | } |
1155 | | - $res = $dbw->insert( 'cn_notice_countries', $insertArray, |
| 1151 | + $res = $dbw->insert( 'cn_notice_projects', $insertArray, |
1156 | 1152 | __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; |
1157 | 1193 | } |
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; |
1177 | 1194 | } |
1178 | 1195 | } |
1179 | 1196 | |
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 ) { |
1181 | 1203 | $dbr = wfGetDB( DB_SLAVE ); |
1182 | 1204 | |
1183 | 1205 | $res = $dbr->select( 'cn_notices', 'not_name, not_locked', |
1184 | | - array( 'not_name' => $noticeName ) |
| 1206 | + array( 'not_name' => $campaignName ) |
1185 | 1207 | ); |
1186 | 1208 | if ( $dbr->numRows( $res ) < 1 ) { |
1187 | 1209 | $this->showError( 'centralnotice-remove-notice-doesnt-exist' ); |
1188 | | - return; |
| 1210 | + return null; |
1189 | 1211 | } |
1190 | 1212 | $row = $dbr->fetchObject( $res ); |
1191 | 1213 | if ( $row->not_locked == '1' ) { |
1192 | 1214 | $this->showError( 'centralnotice-notice-is-locked' ); |
1193 | | - return; |
| 1215 | + return null; |
1194 | 1216 | } else { |
1195 | 1217 | // 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 ); |
1198 | 1220 | |
1199 | 1221 | $dbw = wfGetDB( DB_MASTER ); |
1200 | 1222 | $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 ) ); |
1206 | 1228 | $dbw->commit(); |
1207 | 1229 | |
1208 | | - return; |
| 1230 | + return true; |
1209 | 1231 | } |
1210 | 1232 | } |
1211 | 1233 | |
— | — | @@ -1360,7 +1382,7 @@ |
1361 | 1383 | * Update a boolean setting on a campaign |
1362 | 1384 | * @param $noticeName string: Name of the campaign |
1363 | 1385 | * @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 |
1365 | 1387 | */ |
1366 | 1388 | private function setBooleanCampaignSetting( $noticeName, $settingName, $settingValue ) { |
1367 | 1389 | if ( !$this->noticeExists( $noticeName ) ) { |