r85990 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85989‎ | r85990 | r85991 >
Date:22:00, 13 April 2011
Author:tparscal
Status:ok
Tags:
Comment:
Moved count change calculation code (which was writen 2x) into it's own function. Fixed a bug where an initial rating of 0 would still get a count of 1.
Modified paths:
  • /trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php
@@ -91,24 +91,6 @@
9292 private function insertPageRating( $pageId, $ratingId, $updateAddition, $thisRating, $lastRating ) {
9393 $dbw = wfGetDB( DB_MASTER );
9494
95 - // 0 == No change in rating count
96 - // 1 == No rating last time (or new rating), and now there is
97 - // -1 == Rating last time, but abstained this time
98 - $countChange = 0;
99 - if ( $lastRating === false || $lastRating === 0 ) {
100 - if ( $thisRating === 0 ) {
101 - $countChange = 0;
102 - } else {
103 - $countChange = 1;
104 - }
105 - } else { // Last rating was > 0
106 - if ( $thisRating === 0 ) {
107 - $countChange = -1;
108 - } else {
109 - $countChange = 0;
110 - }
111 - }
112 -
11395 // Try to insert a new afp row for this page with zeroes in it
11496 // Try will silently fail if the row already exists
11597 $dbw->insert(
@@ -128,7 +110,7 @@
129111 'article_feedback_pages',
130112 array(
131113 'aap_total = aap_total + ' . $updateAddition,
132 - 'aap_count = aap_count + ' . $countChange,
 114+ 'aap_count = aap_count + ' . $this->getCountChange( $lastRating, $thisRating ),
133115 ),
134116 array(
135117 'aap_page_id' => $pageId,
@@ -192,7 +174,7 @@
193175 'article_feedback_revisions',
194176 array(
195177 'afr_total' => $thisRating,
196 - 'afr_count' => 1,
 178+ 'afr_count' => $thisRating ? 1 : 0,
197179 ),
198180 array(
199181 'afr_page_id' => $pageId,
@@ -202,31 +184,12 @@
203185 __METHOD__
204186 );
205187 } else {
206 - // Calculate the difference between the previous rating and this one
207 - // * -1 == Rating last time, but abstained this time
208 - // * 0 == No change in rating count
209 - // * 1 == No rating last time (or new rating), and now there is
210 - $countChange = 0;
211 - if ( $lastRating === false || $lastRating === 0 ) {
212 - if ( $thisRating === 0 ) {
213 - $countChange = 0;
214 - } else {
215 - $countChange = 1;
216 - }
217 - } else {
218 - // Last rating was > 0
219 - if ( $thisRating === 0 ) {
220 - $countChange = -1;
221 - } else {
222 - $countChange = 0;
223 - }
224 - }
225188 // Apply the difference between the previous and new ratings to the current "totals" row
226189 $dbw->update(
227190 'article_feedback_revisions',
228191 array(
229192 'afr_total = afr_total + ' . $updateAddition,
230 - 'afr_count = afr_count + ' . $countChange,
 193+ 'afr_count = afr_count + ' . $this->getCountChange( $lastRating, $thisRating ),
231194 ),
232195 array(
233196 'afr_page_id' => $pageId,
@@ -237,6 +200,19 @@
238201 );
239202 }
240203 }
 204+ /**
 205+ * Calculate the difference between the previous rating and this one
 206+ * -1 == Rating last time, but abstained this time
 207+ * 0 == No change in rating count
 208+ * 1 == No rating last time (or new rating), and now there is
 209+ */
 210+ protected function getCountChange( $lastRating, $thisRating ) {
 211+ if ( $lastRating === false || $lastRating === 0 ) {
 212+ return $thisRating === 0 ? 0 : 1;
 213+ }
 214+ // Last rating was > 0
 215+ return $thisRating === 0 ? -1 : 0;
 216+ }
241217
242218 /**
243219 * Inserts (or Updates, where appropriate) the users ratings for a specific revision

Status & tagging log