r111978 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111977‎ | r111978 | r111979 >
Date:23:13, 20 February 2012
Author:gregchiasson
Status:ok
Tags:
Comment:
AFT5 - fix up filter counts on flagging/unflagging feedback.
Modified paths:
  • /trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php (modified) (history)
  • /trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php (modified) (history)
  • /trunk/extensions/ArticleFeedbackv5/sql/filter_count.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedbackv5/sql/filter_count.sql
@@ -1,10 +1,12 @@
22 DELETE FROM aft_article_filter_count;
3 -INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'helpful', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND CONVERT(af_helpful_count, SIGNED) - CONVERT(af_unhelpful_count, SIGNED) > 0 GROUP BY af_page_id;
4 -INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'abusive', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND af_abuse_count > 0 GROUP BY af_page_id;
 3+
54 INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'all', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 GROUP BY af_page_id;
6 -INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'comment', COUNT(*) FROM aft_article_feedback, aft_article_answer WHERE af_bucket_id = 1 AND af_id = aa_feedback_id AND aa_response_text IS NOT NULL GROUP BY af_page_id;
7 -INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'unhelpful', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND CONVERT(af_helpful_count, SIGNED) - CONVERT(af_unhelpful_count, SIGNED) < 0 GROUP BY af_page_id;
85 INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'needsoversight', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND af_oversight_count > 0 GROUP BY af_page_id;
96 INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'invisible', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND af_is_hidden IS TRUE GROUP BY af_page_id;
107 INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'visible', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND af_is_hidden IS FALSE AND af_is_deleted IS FALSE GROUP BY af_page_id;
118 INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'deleted', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND af_is_deleted IS TRUE GROUP BY af_page_id;
 9+
 10+INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'abusive', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND af_abuse_count > 0 AND af_is_hidden IS FALSE AND af_is_deleted IS FALSE GROUP BY af_page_id;
 11+INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'helpful', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND CONVERT(af_helpful_count, SIGNED) - CONVERT(af_unhelpful_count, SIGNED) > 0 AND af_is_hidden IS FALSE AND af_is_deleted IS FALSE GROUP BY af_page_id;
 12+INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'unhelpful', COUNT(*) FROM aft_article_feedback WHERE af_bucket_id = 1 AND CONVERT(af_helpful_count, SIGNED) - CONVERT(af_unhelpful_count, SIGNED) < 0 AND af_is_hidden IS FALSE AND af_is_deleted IS FALSE GROUP BY af_page_id;
 13+INSERT INTO aft_article_filter_count(afc_page_id, afc_filter_name, afc_filter_count) SELECT af_page_id, 'comment', COUNT(*) FROM aft_article_feedback, aft_article_answer WHERE af_bucket_id = 1 AND af_id = aa_feedback_id AND aa_response_text IS NOT NULL AND af_is_hidden IS FALSE AND af_is_deleted IS FALSE GROUP BY af_page_id;
Index: trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php
@@ -22,19 +22,18 @@
2323 * Execute the API call: Pull the requested feedback
2424 */
2525 public function execute() {
26 - $params = $this->extractRequestParams();
27 - $pageId = $params['pageid'];
 26+ $params = $this->extractRequestParams();
 27+ $pageId = $params['pageid'];
2828 $feedbackId = $params['feedbackid'];
29 - $flag = $params['flagtype'];
30 - $notes = $params['note'];
31 - $direction = isset( $params['direction'] ) ? $params['direction'] : 'increase';
32 - $counts = array( 'increment' => array(), 'decrement' => array() );
33 - $counters = array( 'abuse', 'helpful', 'unhelpful', 'oversight' );
34 - $flags = array( 'hide', 'delete' );
35 - $results = array();
36 - $helpful = null;
37 - $error = null;
38 - $where = array( 'af_id' => $feedbackId );
 29+ $flag = $params['flagtype'];
 30+ $notes = $params['note'];
 31+ $direction = isset( $params['direction'] ) ? $params['direction'] : 'increase';
 32+ $counters = array( 'abuse', 'helpful', 'unhelpful', 'oversight' );
 33+ $flags = array( 'hide', 'delete' );
 34+ $results = array();
 35+ $helpful = null;
 36+ $error = null;
 37+ $where = array( 'af_id' => $feedbackId );
3938
4039 # load feedback record, bail if we don't have one
4140 $record = $this->fetchRecord( $feedbackId );
@@ -48,11 +47,9 @@
4948 switch( $flag ) {
5049 case 'hide':
5150 $field = 'af_is_hidden';
52 - $count = 'invisible';
5351 break;
5452 case 'delete':
5553 $field = 'af_is_deleted';
56 - $count = 'deleted';
5754 break;
5855 default: return; # return error, ideally.
5956 }
@@ -61,20 +58,6 @@
6259 } else {
6360 $update[] = "$field = FALSE";
6461 }
65 -
66 - // Increment or decrement whichever flag is being set.
67 - $countDirection = $direction == 'increase' ? 'increment' : 'decrement';
68 - $counts[$countDirection][] = $count;
69 - // If this is hiding/deleting, decrement the visible count.
70 - if( ( $flag == 'hide' || $flag == 'delete' )
71 - && $direction == 'increase' ) {
72 - $counts['decrement'][] = 'visible';
73 - }
74 - // If this is unhiding/undeleting, increment the visible count.
75 - if( ( $flag == 'hide' || $flag == 'delete' )
76 - && $direction == 'decrease' ) {
77 - $counts['increment'][] = 'visible';
78 - }
7962 } elseif( 'resetoversight' === $flag) {
8063 // special case, oversight request count becomes 0
8164 $update[] = "af_oversight_count = 0";
@@ -103,60 +86,6 @@
10487 // Decrementing from 0 is not allowed.
10588 $where[] = "$field > 0";
10689 }
107 -
108 - // Adding a new abuse flag: abusive++
109 - if( $flag == 'abuse' && $direction == 'increase'
110 - && $record->af_abuse_count == 0 ) {
111 - $counts['increment'][] = 'abusive';
112 - // Auto-hide after 5 abuse flags.
113 - if( $record->af_abuse_count > 4 ) {
114 - $counts['increment'][] = 'invisible';
115 - $counts['decrement'][] = 'visible';
116 - }
117 - }
118 - // Removing the last abuse flag: abusive--
119 - if( $flag == 'abuse' && $direction == 'decrease'
120 - && $record->af_abuse_count == 1 ) {
121 - $counts['decrement'][] = 'abusive';
122 - // Un-hide if we don't have 5 flags anymore
123 - if( $record->af_abuse_count == 5 ) {
124 - $counts['increment'][] = 'visible';
125 - $counts['decrement'][] = 'invisible';
126 - }
127 - }
128 -
129 - // note that a net helpfulness of 0 is neither helpful nor unhelpful
130 - $netHelpfulness = $record->af_net_helpfulness;
131 -
132 - // increase helpful OR decrease unhelpful
133 - if( ( ($flag == 'helpful' && $direction == 'increase' )
134 - || ($flag == 'unhelpful' && $direction == 'decrease' ) )
135 - ) {
136 - // net was -1: no longer unhelpful
137 - if( $netHelpfulness == -1 ) {
138 - $counts['decrement'][] = 'unhelpful';
139 - }
140 -
141 - // net was 0: now helpful
142 - if( $netHelpfulness == 0 ) {
143 - $counts['increment'][] = 'helpful';
144 - }
145 - }
146 -
147 - // increase unhelpful OR decrease unhelpful
148 - if( ( ($flag == 'unhelpful' && $direction == 'increase' )
149 - || ($flag == 'helpful' && $direction == 'decrease' ) )
150 - ) {
151 - // net was 1: no longer helpful
152 - if( $netHelpfulness == 1 ) {
153 - $counts['decrement'][] = 'helpful';
154 - }
155 -
156 - // net was 0: now unhelpful
157 - if( $netHelpfulness == 0 ) {
158 - $counts['increment'][] = 'unhelpful';
159 - }
160 - }
16190 } else {
16291 $error = 'articlefeedbackv5-invalid-feedback-flag';
16392 }
@@ -183,9 +112,7 @@
184113
185114 ApiArticleFeedbackv5Utils::logActivity( $activity , $pageId, $feedbackId, $notes );
186115
187 - // Update the filter count rollups.
188 - ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['increment'] );
189 - ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['decrement'] );
 116+ $this->updateCounts( $flag, $direction, $record, $pageId );
190117
191118 // Update helpful/unhelpful display count after submission.
192119 if ( $flag == 'helpful' || $flag == 'unhelpful' ) {
@@ -217,7 +144,6 @@
218145 __METHOD__
219146 );
220147 }
221 -
222148 }
223149
224150 // Conditional formatting for abuse flag
@@ -368,6 +294,126 @@
369295 }
370296
371297 /**
 298+ * Figures out which filter counts to increment/decrement based on what
 299+ * flag was clicked and which direction it was going.
 300+ *
 301+ * @param $action string type of flag sent to the form
 302+ * @param $direction string type of direction sent to the form
 303+ * @param $record object database object representing the feedback row
 304+ * @param $pageId integer ID of the page to be updated.
 305+ */
 306+ protected function updateCounts( $action, $direction, $record, $pageId ) {
 307+ $counts = array( 'increment' => array(), 'decrement' => array() );
 308+ $netHelpfulness = $record->af_net_helpfulness;
 309+
 310+ // Increment or decrement hide or delete filters.
 311+ if( $action == 'hide' || $action == 'delete' ) {
 312+ $count = $action == 'hide' ? 'invisible' : 'deleted';
 313+
 314+ // If this is hiding/deleting, increment the visible count and
 315+ // decrement the visible count.
 316+ if( $direction == 'increase' ) {
 317+ $counts['increment'][] = $count;
 318+ $counts['decrement'][] = 'visible';
 319+ }
 320+ // For unhiding/undeleting, do the opposite.
 321+ if( $direction == 'decrease' ) {
 322+ $counts['increment'][] = 'visible';
 323+ $counts['decrement'][] = $count;
 324+ }
 325+ }
 326+
 327+ // Adding a new abuse flag: abusive++
 328+ if( $action == 'abuse' && $direction == 'increase'
 329+ && $record->af_abuse_count == 0 ) {
 330+ $counts['increment'][] = 'abusive';
 331+ // Auto-hide after 5 abuse flags.
 332+ if( $record->af_abuse_count > 4 ) {
 333+ $counts['increment'][] = 'invisible';
 334+ $counts['decrement'][] = 'visible';
 335+ }
 336+ }
 337+
 338+ // Removing the last abuse flag: abusive--
 339+ if( $action == 'abuse' && $direction == 'decrease'
 340+ && $record->af_abuse_count == 1 ) {
 341+ $counts['decrement'][] = 'abusive';
 342+ // Un-hide if we don't have 5 flags anymore
 343+ if( $record->af_abuse_count == 5 ) {
 344+ $counts['increment'][] = 'visible';
 345+ $counts['decrement'][] = 'invisible';
 346+ }
 347+ }
 348+
 349+ // note that a net helpfulness of 0 is neither helpful nor unhelpful
 350+
 351+ // increase helpful OR decrease unhelpful
 352+ if( ( ($action == 'helpful' && $direction == 'increase' )
 353+ || ($action == 'unhelpful' && $direction == 'decrease' ) )
 354+ ) {
 355+ // net was -1: no longer unhelpful
 356+ if( $netHelpfulness == -1 ) {
 357+ $counts['decrement'][] = 'unhelpful';
 358+ }
 359+
 360+ // net was 0: now helpful
 361+ if( $netHelpfulness == 0 ) {
 362+ $counts['increment'][] = 'helpful';
 363+ }
 364+ }
 365+
 366+ // increase unhelpful OR decrease unhelpful
 367+ if( ( ($action == 'unhelpful' && $direction == 'increase' )
 368+ || ($action == 'helpful' && $direction == 'decrease' ) )
 369+ ) {
 370+ // net was 1: no longer helpful
 371+ if( $netHelpfulness == 1 ) {
 372+ $counts['decrement'][] = 'helpful';
 373+ }
 374+
 375+ // net was 0: now unhelpful
 376+ if( $netHelpfulness == 0 ) {
 377+ $counts['increment'][] = 'unhelpful';
 378+ }
 379+ }
 380+
 381+ // If someone hides or deletes an abusive/comment/helpful/unhelpful
 382+ // comment, decrement the relevent counter.
 383+ // If they unhide/undelete, increment that same counter.
 384+ if( $action == 'delete' || $action == 'hide' ) {
 385+ $dbr = wfGetDB( DB_SLAVE );
 386+ $dir = $direction == 'increase' ? 'decrement' : 'increment';
 387+ $comment = $dbr->select(
 388+ array( 'aft_article_answer', 'aft_article_field' ),
 389+ 'aa_response_text',
 390+ array(
 391+ 'aa_feedback_id' => $record->af_id,
 392+ 'afi_data_type' => 'text',
 393+ 'aa_field_id = afi_id'
 394+ ),
 395+ __METHOD__
 396+ );
 397+
 398+ if( $record->af_abuse_count > 1 ) {
 399+ $counts[$dir][] = 'abusive';
 400+ }
 401+ if( $comment->numRows() > 0 ) {
 402+ $counts[$dir][] = 'comment';
 403+ }
 404+ if( $netHelpfulness > 1 ) {
 405+ $counts[$dir][] = 'helpful';
 406+ }
 407+ if( $netHelpfulness < 1 ) {
 408+ $counts[$dir][] = 'unhelpful';
 409+ }
 410+ }
 411+
 412+ // Update the filter count rollups.
 413+ ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['increment'] );
 414+ ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['decrement'] );
 415+ }
 416+
 417+ /**
372418 * Figures out which activity happened so it can be logged correctly
373419 *
374420 * @param $flag string type of flag sent to the form
Index: trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php
@@ -175,11 +175,9 @@
176176 public function updateFilterCounts( $pageId, $filters, $decrement ) {
177177 // Don't do anything unless we have filters to process.
178178 if( !$filters ) {
179 -error_log("no filters");
180179 return;
181180 }
182181 if( !count( $filters ) ) {
183 -error_log("no count of filters");
184182 return;
185183 }
186184
@@ -187,7 +185,6 @@
188186 $dbw->begin();
189187
190188 foreach ( $filters as $filter ) {
191 -error_log("have filter $filter for page $pageId");
192189 $rows[] = array(
193190 'afc_page_id' => $pageId,
194191 'afc_filter_name' => $filter,
@@ -205,10 +202,8 @@
206203 );
207204
208205 $value = $decrement ? 'afc_filter_count - 1' : 'afc_filter_count + 1';
209 -error_log("new value is $value");
210206
211207 foreach ( $filters as $filter ) {
212 -error_log("setting value for page $pageId, filter $filter");
213208 # Update each row with the new count.
214209 $dbw->update(
215210 'aft_article_filter_count',

Status & tagging log