r110098 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110097‎ | r110098 | r110099 >
Date:00:11, 27 January 2012
Author:gregchiasson
Status:resolved (Comments)
Tags:aft 
Comment:
AFT5 - Backend plumbing in FlagFeedback call to support toggle buttons and decreasing counts/unflagging. Adds new parameter, 'direction', which controls that (defaults to increase, which is the normal behavior)
Modified paths:
  • /trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php (modified) (history)
  • /trunk/extensions/ArticleFeedbackv5/api/ApiViewFeedbackArticleFeedbackv5.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php
@@ -22,45 +22,66 @@
2323 * Execute the API call: Pull the requested feedback
2424 */
2525 public function execute() {
26 - $params = $this->extractRequestParams();
27 - $pageId = $params['pageid'];
28 - $error = null;
29 - $dbr = wfGetDB( DB_SLAVE );
30 - $counts = array( 'increment' => array(), 'decrement' => array() );
31 - $helpful = null;
 26+ $params = $this->extractRequestParams();
 27+ $pageId = $params['pageid'];
 28+ $flag = $params['flagtype'];
 29+ $direction = isset( $params['direction'] ) ? $params['direction'] : 'increase';
 30+ $counts = array( 'increment' => array(), 'decrement' => array() );
 31+ $flags = array( 'abuse', 'hide', 'helpful', 'unhelpful', 'delete' );
 32+ $results = array();
 33+ $helpful = null;
 34+ $error = null;
3235
3336 # load feedback record, bail if we don't have one
34 - $record = $dbr->selectRow(
35 - 'aft_article_feedback',
36 - array( 'af_id', 'af_abuse_count', 'af_hide_count', 'af_helpful_count', 'af_unhelpful_count', 'af_delete_count' ),
37 - array( 'af_id' => $params['feedbackid'] )
38 - );
 37+ $record = $this->fetchRecord( $params['feedbackid'] );
3938
40 - $flags = array( 'abuse', 'hide', 'helpful', 'unhelpful', 'delete' );
41 - $flag = $params['flagtype'];
42 -
4339 if ( !$record->af_id ) {
4440 // no-op, because this is already broken
4541 $error = 'articlefeedbackv5-invalid-feedback-id';
4642 } elseif ( $params['flagtype'] == 'unhide' ) {
47 - // remove the hidden status
48 - $update[] = 'af_hide_count = 0';
 43+ if( $direction == 'increase' ) {
 44+ // remove the hidden status
 45+ $update[] = 'af_hide_count = 0';
 46+ } else {
 47+ // or set one
 48+ $update[] = 'af_hide_count = 1';
 49+ }
4950 } elseif ( $params['flagtype'] == 'unoversight' ) {
50 - // remove the oversight flag
51 - $update[] = 'af_needs_oversight = FALSE';
 51+ if( $direction == 'increase' ) {
 52+ // remove the oversight flag
 53+ $update[] = 'af_needs_oversight = FALSE';
 54+ } else {
 55+ // or set one
 56+ $update[] = 'af_needs_oversight = TRUE';
 57+ }
5258 } elseif ( $params['flagtype'] == 'undelete' ) {
53 - // remove the deleted status, and clear oversight flag
54 - $update[] = 'af_delete_count = 0';
55 - $update[] = 'af_needs_oversight = FALSE';
 59+ if( $direction == 'increase' ) {
 60+ // remove the deleted status, and clear oversight flag
 61+ $update[] = 'af_delete_count = 0';
 62+ $update[] = 'af_needs_oversight = FALSE';
 63+ } else {
 64+ // add deleted status and oversight flag
 65+ $update[] = 'af_delete_count = 1';
 66+ $update[] = 'af_needs_oversight = TRUE';
 67+ }
5668 } elseif ( $params['flagtype'] == 'oversight' ) {
57 - // flag for oversight
58 - $update[] = 'af_needs_oversight = TRUE';
 69+ if( $direction == 'increase' ) {
 70+ // flag for oversight
 71+ $update[] = 'af_needs_oversight = TRUE';
 72+ } else {
 73+ // remove flag for oversight
 74+ $update[] = 'af_needs_oversight = FALSE';
 75+ }
5976 } elseif ( in_array( $params['flagtype'], $flags ) ) {
6077 // Probably this doesn't need validation, since the API
6178 // will handle it, but if it's getting interpolated into
6279 // the SQL, I'm really wary not re-validating it.
6380 $field = 'af_' . $params['flagtype'] . '_count';
64 - $update[] = "$field = $field + 1";
 81+ if( $direction == 'increase' ) {
 82+ $update[] = "$field = $field + 1";
 83+ } else {
 84+ $update[] = "$field = $field - 1";
 85+ }
6586 } else {
6687 $error = 'articlefeedbackv5-invalid-feedback-flag';
6788 }
@@ -119,50 +140,60 @@
120141 __METHOD__
121142 );
122143
123 - ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['increment'] );
124 - ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['decrement'] );
 144+ if( $direction == 'decrease') {
 145+ // This is backwards to account for a users' unflagging something.
 146+ ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['decrement'] );
 147+ ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['increment'] );
 148+ } else {
 149+ ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['increment'] );
 150+ ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['decrement'] );
 151+ }
125152
 153+ // Update helpful/unhelpful count after submission.
126154 // This gets a potentially stale copy from the read
127 - // database assumes it's valid, in the interest
 155+ // database and assumes it's valid, in the interest
128156 // of staying off of the write database.
129157 // Better stale data than wail on the master, IMO,
130158 // but I'm open to suggestion on that one.
131 -
132 - // Update helpful/unhelpful count after submission
133159 if ( $params['flagtype'] == 'helpful' || $params['flagtype'] == 'unhelpful' ) {
134 - $record = $dbr->selectRow(
135 - 'aft_article_feedback',
136 - array( 'af_helpful_count', 'af_unhelpful_count' ),
137 - array( 'af_id' => $params['feedbackid'] ),
138 - __METHOD__
139 - );
 160+ $record = $this->fetchRecord( $params['feedbackid'] );
140161
141162 $helpful = $record->af_helpful_count;
142163 $unhelpful = $record->af_unhelpful_count;
143164
144 - $helpful = wfMessage( 'articlefeedbackv5-form-helpful-votes',
 165+ $results['helpful'] = wfMessage( 'articlefeedbackv5-form-helpful-votes',
145166 $helpful, $unhelpful
146167 )->escaped();
147168 }
 169+
 170+ // Conditional formatting for abuse flag
 171+ // Re-fetch record - as above, from read/slave DB.
 172+ // The record could have had it's falg increased or
 173+ // decreased, so load a fresh (as fresh as the read
 174+ // db is, anyway) copy of it.
 175+ $record = $this->fetchRecord( $params['feedbackid'] );
 176+ if( $record->af_abuse_count > 5 ) {
 177+ $dbw->update(
 178+ 'aft_article_feedback',
 179+ array( 'af_hide_count = af_hide_count + 1' ),
 180+ array( 'af_id' => $params['feedbackid'] ),
 181+ __METHOD__
 182+ );
 183+ }
 184+ if( $record->af_abuse_count > 3 ) {
 185+ // Return a flag in the JSON, that turns the link red.
 186+ $results['abusive'] = 1;
 187+ }
148188 }
149189
150190 if ( $error ) {
151 - $result = 'Error';
152 - $reason = $error;
 191+ $results['result'] = 'Error';
 192+ $results['reason'] = $error;
153193 } else {
154 - $result = 'Success';
155 - $reason = null;
 194+ $results['result'] = 'Success';
 195+ $results['reason'] = null;
156196 }
157197
158 - $results = array(
159 - 'result' => $result,
160 - 'reason' => $reason,
161 - );
162 -
163 - if ( $helpful ) {
164 - $results['helpful'] = $helpful;
165 - }
166 -
167198 $this->getResult()->addValue(
168199 null,
169200 $this->getModuleName(),
@@ -170,6 +201,16 @@
171202 );
172203 }
173204
 205+ private function fetchRecord( $id ) {
 206+ $dbr = wfGetDB( DB_SLAVE );
 207+ $record = $dbr->selectRow(
 208+ 'aft_article_feedback',
 209+ array( 'af_id', 'af_abuse_count', 'af_hide_count', 'af_helpful_count', 'af_unhelpful_count', 'af_delete_count' ),
 210+ array( 'af_id' => $id )
 211+ );
 212+ return $record;
 213+ }
 214+
174215 /**
175216 * Gets the allowed parameters
176217 *
@@ -193,6 +234,12 @@
194235 ApiBase::PARAM_TYPE => array(
195236 'abuse', 'hide', 'helpful', 'unhelpful', 'delete', 'undelete', 'unhide', 'oversight', 'unoversight' )
196237 ),
 238+ 'direction' => array(
 239+ ApiBase::PARAM_REQUIRED => false,
 240+ ApiBase::PARAM_ISMULTI => false,
 241+ ApiBase::PARAM_TYPE => array(
 242+ 'increase', 'decrease' )
 243+ )
197244 );
198245 }
199246
Index: trunk/extensions/ArticleFeedbackv5/api/ApiViewFeedbackArticleFeedbackv5.php
@@ -94,6 +94,7 @@
9595 $continueSql = "CONVERT(af_helpful_count, SIGNED) - CONVERT(af_unhelpful_count, SIGNED) $continueDirection";
9696 break;
9797 case 'rating':
 98+# TODO
9899 # disable because it's broken
99100 # $sortField = 'rating';
100101 # break;
@@ -295,9 +296,9 @@
296297 // Taken from the Moodbar extension.
297298 $now = wfTimestamp( TS_UNIX );
298299 $timestamp = wfTimestamp( TS_UNIX, $record[0]->af_created );
 300+
299301 // Relative dates for 48 hours, normal timestamps later.
300302 if( $timestamp > ( $now - ( 86400 * 2 ) ) ) {
301 - // TODO: relative dates.
302303 $time = $wgLang->formatTimePeriod(
303304 ( $now - $timestamp ), 'avoidseconds'
304305 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r110182AFT5:...gregchiasson22:58, 27 January 2012
r110397AFT5 - Fix the way auto-hiding abuse comments is done: Add conditional as a q...gregchiasson15:58, 31 January 2012

Comments

#Comment by Bsitu (talk | contribs)   19:56, 27 January 2012
+		$record = $this->fetchRecord( $params['feedbackid'] );
 
-		$flags  = array( 'abuse', 'hide', 'helpful', 'unhelpful', 'delete' );
-		$flag   = $params['flagtype'];
-
 		if ( !$record->af_id ) {

$dbr->selectRow may return false and so does fetchRecord, you should check if $record is false before using it as object

#Comment by Catrope (talk | contribs)   07:37, 31 January 2012
+			// Re-fetch record - as above, from read/slave DB.
+			// The record could have had it's falg increased or
+			// decreased, so load a fresh (as fresh as the read
+			// db is, anyway) copy of it.
+			$record =  $this->fetchRecord( $params['feedbackid'] );
+			if( $record->af_abuse_count > 5 ) {

Why would you do that? Re-reading from the slave makes very little sense. You either care about freshness, or you don't. If you do, load from the master, if you don't, load from the slave once, not twice. Given that you're conditionally issuing a query based on the outcome, you should read from the master, or -- even better -- put the condition in the WHERE clause of the UPDATE query.

Status & tagging log