https://www.mediawiki.org/wiki/Special:Code/MediaWiki/111978

Revision: 111978
Author:   gregchiasson
Date:     2012-02-20 23:13:34 +0000 (Mon, 20 Feb 2012)
Log Message:
-----------
AFT5 - fix up filter counts on flagging/unflagging feedback.

Modified Paths:
--------------
    trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php
    trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php
    trunk/extensions/ArticleFeedbackv5/sql/filter_count.sql

Modified: trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php
===================================================================
--- trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php        
2012-02-20 23:12:45 UTC (rev 111977)
+++ trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php        
2012-02-20 23:13:34 UTC (rev 111978)
@@ -175,11 +175,9 @@
        public function updateFilterCounts( $pageId, $filters, $decrement ) {
                // Don't do anything unless we have filters to process.
                if( !$filters ) { 
-error_log("no filters");
                        return; 
                }
                if( !count( $filters ) ) { 
-error_log("no count of filters");
                        return; 
                }
 
@@ -187,7 +185,6 @@
                $dbw->begin();
 
                foreach ( $filters as $filter ) {
-error_log("have filter $filter for page $pageId");
                        $rows[] = array(
                                'afc_page_id'      => $pageId,
                                'afc_filter_name'  => $filter,
@@ -205,10 +202,8 @@
                );
 
                $value = $decrement ? 'afc_filter_count - 1' : 
'afc_filter_count + 1';
-error_log("new value is $value");
 
                foreach ( $filters as $filter ) {
-error_log("setting value for page $pageId, filter $filter");
                        # Update each row with the new count.
                        $dbw->update(
                                'aft_article_filter_count',

Modified: 
trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php
===================================================================
--- trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php 
2012-02-20 23:12:45 UTC (rev 111977)
+++ trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php 
2012-02-20 23:13:34 UTC (rev 111978)
@@ -22,19 +22,18 @@
         * Execute the API call: Pull the requested feedback
         */
        public function execute() {
-               $params    = $this->extractRequestParams();
-               $pageId    = $params['pageid'];
+               $params     = $this->extractRequestParams();
+               $pageId     = $params['pageid'];
                $feedbackId = $params['feedbackid'];
-               $flag      = $params['flagtype'];
-               $notes = $params['note'];
-               $direction = isset( $params['direction'] ) ? 
$params['direction'] : 'increase';
-               $counts    = array( 'increment' => array(), 'decrement' => 
array() );
-               $counters  = array( 'abuse', 'helpful', 'unhelpful', 
'oversight' );
-               $flags     = array( 'hide', 'delete' );
-               $results   = array();
-               $helpful   = null;
-               $error     = null;
-               $where     = array( 'af_id' => $feedbackId );
+               $flag       = $params['flagtype'];
+               $notes      = $params['note'];
+               $direction  = isset( $params['direction'] ) ? 
$params['direction'] : 'increase';
+               $counters   = array( 'abuse', 'helpful', 'unhelpful', 
'oversight' );
+               $flags      = array( 'hide', 'delete' );
+               $results    = array();
+               $helpful    = null;
+               $error      = null;
+               $where      = array( 'af_id' => $feedbackId );
 
                # load feedback record, bail if we don't have one
                $record = $this->fetchRecord( $feedbackId );
@@ -48,11 +47,9 @@
                        switch( $flag ) {
                                case 'hide':
                                        $field = 'af_is_hidden';
-                                       $count = 'invisible';
                                        break;
                                case 'delete':
                                        $field = 'af_is_deleted';
-                                       $count = 'deleted';
                                        break;
                                default: return; # return error, ideally.
                        }
@@ -61,20 +58,6 @@
                        } else {
                                $update[] = "$field = FALSE";
                        }
-
-                       // Increment or decrement whichever flag is being set.
-                       $countDirection = $direction == 'increase' ? 
'increment' : 'decrement';
-                       $counts[$countDirection][] = $count;
-                       // If this is hiding/deleting, decrement the visible 
count.
-                       if( ( $flag == 'hide' || $flag == 'delete' )
-                        && $direction == 'increase' ) {
-                               $counts['decrement'][] = 'visible';
-                       }
-                       // If this is unhiding/undeleting, increment the 
visible count.
-                       if( ( $flag == 'hide' || $flag == 'delete' )
-                        && $direction == 'decrease' ) {
-                               $counts['increment'][] = 'visible';
-                       }
                } elseif( 'resetoversight' === $flag) {
                        // special case, oversight request count becomes 0
                        $update[] = "af_oversight_count = 0";
@@ -103,60 +86,6 @@
                                // Decrementing from 0 is not allowed.
                                $where[] = "$field > 0";
                        }
-
-                       // Adding a new abuse flag: abusive++
-                       if( $flag == 'abuse' && $direction == 'increase'
-                        && $record->af_abuse_count == 0 ) {
-                               $counts['increment'][] = 'abusive';
-                               // Auto-hide after 5 abuse flags.
-                               if( $record->af_abuse_count > 4 ) {
-                                       $counts['increment'][] = 'invisible';
-                                       $counts['decrement'][] = 'visible';
-                               }
-                       }
-                       // Removing the last abuse flag: abusive--
-                       if( $flag == 'abuse' && $direction == 'decrease'
-                        && $record->af_abuse_count == 1 ) {
-                               $counts['decrement'][] = 'abusive';
-                               // Un-hide if we don't have 5 flags anymore
-                               if( $record->af_abuse_count == 5 ) {
-                                       $counts['increment'][] = 'visible';
-                                       $counts['decrement'][] = 'invisible';
-                               }
-                       }
-
-                       // note that a net helpfulness of 0 is neither helpful 
nor unhelpful
-                       $netHelpfulness = $record->af_net_helpfulness;
-
-                       // increase helpful OR decrease unhelpful
-                       if( ( ($flag == 'helpful' && $direction == 'increase' )
-                        || ($flag == 'unhelpful' && $direction == 'decrease' ) 
)
-                       ) {
-                               // net was -1: no longer unhelpful
-                               if( $netHelpfulness == -1 ) {
-                                       $counts['decrement'][] = 'unhelpful';
-                               }
-
-                               // net was 0: now helpful
-                               if( $netHelpfulness == 0 ) {
-                                       $counts['increment'][] = 'helpful';
-                               }
-                       }
-
-                       // increase unhelpful OR decrease unhelpful
-                       if( ( ($flag == 'unhelpful' && $direction == 'increase' 
)
-                        || ($flag == 'helpful' && $direction == 'decrease' ) )
-                       ) {
-                               // net was 1: no longer helpful
-                               if( $netHelpfulness == 1 ) {
-                                       $counts['decrement'][] = 'helpful';
-                               }
-
-                               // net was 0: now unhelpful
-                               if( $netHelpfulness == 0 ) {
-                                       $counts['increment'][] = 'unhelpful';
-                               }
-                       }
                } else {
                        $error = 'articlefeedbackv5-invalid-feedback-flag';
                }
@@ -183,9 +112,7 @@
 
                                ApiArticleFeedbackv5Utils::logActivity( 
$activity , $pageId, $feedbackId, $notes );
 
-                               // Update the filter count rollups.
-                               
ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['increment'] 
);
-                               
ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['decrement'] 
);
+                               $this->updateCounts( $flag, $direction, 
$record, $pageId );
 
                                // Update helpful/unhelpful display count after 
submission.
                                if ( $flag == 'helpful' || $flag == 'unhelpful' 
) {
@@ -217,7 +144,6 @@
                                                __METHOD__
                                        );
                                }
-
                        }
 
                        // Conditional formatting for abuse flag
@@ -368,6 +294,126 @@
        }
 
        /**
+        * Figures out which filter counts to increment/decrement based on what
+        * flag was clicked and which direction it was going.
+        *
+        * @param $action    string  type of flag sent to the form
+        * @param $direction string  type of direction sent to the form
+        * @param $record    object  database object representing the feedback 
row
+        * @param $pageId    integer ID of the page to be updated.
+        */
+       protected function updateCounts( $action, $direction, $record, $pageId 
) {
+               $counts = array( 'increment' => array(), 'decrement' => array() 
);
+               $netHelpfulness = $record->af_net_helpfulness;
+
+               // Increment or decrement hide or delete filters.
+               if( $action == 'hide' || $action == 'delete' ) {
+                       $count    = $action == 'hide' ? 'invisible' : 'deleted';
+
+                       // If this is hiding/deleting, increment the visible 
count and 
+                       // decrement the visible count.
+                       if( $direction == 'increase' ) {
+                               $counts['increment'][] = $count;
+                               $counts['decrement'][] = 'visible';
+                       }
+                       // For unhiding/undeleting, do the opposite.
+                       if( $direction == 'decrease' ) {
+                               $counts['increment'][] = 'visible';
+                               $counts['decrement'][] = $count;
+                       }
+               }
+
+               // Adding a new abuse flag: abusive++
+               if( $action == 'abuse' && $direction == 'increase'
+                && $record->af_abuse_count == 0 ) {
+                       $counts['increment'][] = 'abusive';
+                       // Auto-hide after 5 abuse flags.
+                       if( $record->af_abuse_count > 4 ) {
+                               $counts['increment'][] = 'invisible';
+                               $counts['decrement'][] = 'visible';
+                       }
+               }
+
+               // Removing the last abuse flag: abusive--
+               if( $action == 'abuse' && $direction == 'decrease'
+                && $record->af_abuse_count == 1 ) {
+                       $counts['decrement'][] = 'abusive';
+                       // Un-hide if we don't have 5 flags anymore
+                       if( $record->af_abuse_count == 5 ) {
+                               $counts['increment'][] = 'visible';
+                               $counts['decrement'][] = 'invisible';
+                       }
+               }
+
+               // note that a net helpfulness of 0 is neither helpful nor 
unhelpful
+
+               // increase helpful OR decrease unhelpful
+               if( ( ($action == 'helpful' && $direction == 'increase' )
+                || ($action == 'unhelpful' && $direction == 'decrease' ) )
+               ) {
+                       // net was -1: no longer unhelpful
+                       if( $netHelpfulness == -1 ) {
+                               $counts['decrement'][] = 'unhelpful';
+                       }
+
+                       // net was 0: now helpful
+                       if( $netHelpfulness == 0 ) {
+                               $counts['increment'][] = 'helpful';
+                       }
+               }
+
+               // increase unhelpful OR decrease unhelpful
+               if( ( ($action == 'unhelpful' && $direction == 'increase' )
+                || ($action == 'helpful' && $direction == 'decrease' ) )
+               ) {
+                       // net was 1: no longer helpful
+                       if( $netHelpfulness == 1 ) {
+                               $counts['decrement'][] = 'helpful';
+                       }
+
+                       // net was 0: now unhelpful
+                       if( $netHelpfulness == 0 ) {
+                               $counts['increment'][] = 'unhelpful';
+                       }
+               }
+
+               // If someone hides or deletes an 
abusive/comment/helpful/unhelpful
+               // comment, decrement the relevent counter.
+               // If they unhide/undelete, increment that same counter.
+               if( $action == 'delete' || $action == 'hide' ) {
+                       $dbr     = wfGetDB( DB_SLAVE );
+                       $dir     = $direction == 'increase' ? 'decrement' : 
'increment';
+                       $comment = $dbr->select(
+                               array( 'aft_article_answer', 
'aft_article_field' ),
+                               'aa_response_text',
+                               array(
+                                       'aa_feedback_id' => $record->af_id,
+                                       'afi_data_type'  => 'text',
+                                       'aa_field_id = afi_id' 
+                               ),
+                               __METHOD__
+                       );
+
+                       if( $record->af_abuse_count > 1 ) {
+                               $counts[$dir][] = 'abusive';
+                       }
+                       if( $comment->numRows() > 0 ) {
+                               $counts[$dir][] = 'comment';
+                       }
+                       if( $netHelpfulness > 1 ) {
+                               $counts[$dir][] = 'helpful';
+                       }
+                       if( $netHelpfulness < 1 ) {
+                               $counts[$dir][] = 'unhelpful';
+                       }
+               }
+
+               // Update the filter count rollups.
+               ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, 
$counts['increment'] );
+               ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, 
$counts['decrement'] );
+       }
+
+       /**
         * Figures out which activity happened so it can be logged correctly
         *
         * @param $flag      string type of flag sent to the form

Modified: trunk/extensions/ArticleFeedbackv5/sql/filter_count.sql
===================================================================
--- trunk/extensions/ArticleFeedbackv5/sql/filter_count.sql     2012-02-20 
23:12:45 UTC (rev 111977)
+++ trunk/extensions/ArticleFeedbackv5/sql/filter_count.sql     2012-02-20 
23:13:34 UTC (rev 111978)
@@ -1,10 +1,12 @@
 DELETE FROM aft_article_filter_count;
-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;
-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;
+
 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;
-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;
-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;
 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;
 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;
 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;
 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;
+
+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;
+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;
+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;
+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;


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to