https://www.mediawiki.org/wiki/Special:Code/MediaWiki/110098
Revision: 110098 Author: gregchiasson Date: 2012-01-27 00:11:27 +0000 (Fri, 27 Jan 2012) Log Message: ----------- 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 trunk/extensions/ArticleFeedbackv5/api/ApiViewFeedbackArticleFeedbackv5.php Modified: trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php =================================================================== --- trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php 2012-01-27 00:11:20 UTC (rev 110097) +++ trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php 2012-01-27 00:11:27 UTC (rev 110098) @@ -22,45 +22,66 @@ * Execute the API call: Pull the requested feedback */ public function execute() { - $params = $this->extractRequestParams(); - $pageId = $params['pageid']; - $error = null; - $dbr = wfGetDB( DB_SLAVE ); - $counts = array( 'increment' => array(), 'decrement' => array() ); - $helpful = null; + $params = $this->extractRequestParams(); + $pageId = $params['pageid']; + $flag = $params['flagtype']; + $direction = isset( $params['direction'] ) ? $params['direction'] : 'increase'; + $counts = array( 'increment' => array(), 'decrement' => array() ); + $flags = array( 'abuse', 'hide', 'helpful', 'unhelpful', 'delete' ); + $results = array(); + $helpful = null; + $error = null; # load feedback record, bail if we don't have one - $record = $dbr->selectRow( - 'aft_article_feedback', - array( 'af_id', 'af_abuse_count', 'af_hide_count', 'af_helpful_count', 'af_unhelpful_count', 'af_delete_count' ), - array( 'af_id' => $params['feedbackid'] ) - ); + $record = $this->fetchRecord( $params['feedbackid'] ); - $flags = array( 'abuse', 'hide', 'helpful', 'unhelpful', 'delete' ); - $flag = $params['flagtype']; - if ( !$record->af_id ) { // no-op, because this is already broken $error = 'articlefeedbackv5-invalid-feedback-id'; } elseif ( $params['flagtype'] == 'unhide' ) { - // remove the hidden status - $update[] = 'af_hide_count = 0'; + if( $direction == 'increase' ) { + // remove the hidden status + $update[] = 'af_hide_count = 0'; + } else { + // or set one + $update[] = 'af_hide_count = 1'; + } } elseif ( $params['flagtype'] == 'unoversight' ) { - // remove the oversight flag - $update[] = 'af_needs_oversight = FALSE'; + if( $direction == 'increase' ) { + // remove the oversight flag + $update[] = 'af_needs_oversight = FALSE'; + } else { + // or set one + $update[] = 'af_needs_oversight = TRUE'; + } } elseif ( $params['flagtype'] == 'undelete' ) { - // remove the deleted status, and clear oversight flag - $update[] = 'af_delete_count = 0'; - $update[] = 'af_needs_oversight = FALSE'; + if( $direction == 'increase' ) { + // remove the deleted status, and clear oversight flag + $update[] = 'af_delete_count = 0'; + $update[] = 'af_needs_oversight = FALSE'; + } else { + // add deleted status and oversight flag + $update[] = 'af_delete_count = 1'; + $update[] = 'af_needs_oversight = TRUE'; + } } elseif ( $params['flagtype'] == 'oversight' ) { - // flag for oversight - $update[] = 'af_needs_oversight = TRUE'; + if( $direction == 'increase' ) { + // flag for oversight + $update[] = 'af_needs_oversight = TRUE'; + } else { + // remove flag for oversight + $update[] = 'af_needs_oversight = FALSE'; + } } elseif ( in_array( $params['flagtype'], $flags ) ) { // Probably this doesn't need validation, since the API // will handle it, but if it's getting interpolated into // the SQL, I'm really wary not re-validating it. $field = 'af_' . $params['flagtype'] . '_count'; - $update[] = "$field = $field + 1"; + if( $direction == 'increase' ) { + $update[] = "$field = $field + 1"; + } else { + $update[] = "$field = $field - 1"; + } } else { $error = 'articlefeedbackv5-invalid-feedback-flag'; } @@ -119,50 +140,60 @@ __METHOD__ ); - ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['increment'] ); - ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['decrement'] ); + if( $direction == 'decrease') { + // This is backwards to account for a users' unflagging something. + ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['decrement'] ); + ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['increment'] ); + } else { + ApiArticleFeedbackv5Utils::incrementFilterCounts( $pageId, $counts['increment'] ); + ApiArticleFeedbackv5Utils::decrementFilterCounts( $pageId, $counts['decrement'] ); + } + // Update helpful/unhelpful count after submission. // This gets a potentially stale copy from the read - // database assumes it's valid, in the interest + // database and assumes it's valid, in the interest // of staying off of the write database. // Better stale data than wail on the master, IMO, // but I'm open to suggestion on that one. - - // Update helpful/unhelpful count after submission if ( $params['flagtype'] == 'helpful' || $params['flagtype'] == 'unhelpful' ) { - $record = $dbr->selectRow( - 'aft_article_feedback', - array( 'af_helpful_count', 'af_unhelpful_count' ), - array( 'af_id' => $params['feedbackid'] ), - __METHOD__ - ); + $record = $this->fetchRecord( $params['feedbackid'] ); $helpful = $record->af_helpful_count; $unhelpful = $record->af_unhelpful_count; - $helpful = wfMessage( 'articlefeedbackv5-form-helpful-votes', + $results['helpful'] = wfMessage( 'articlefeedbackv5-form-helpful-votes', $helpful, $unhelpful )->escaped(); } + + // Conditional formatting for abuse flag + // 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 ) { + $dbw->update( + 'aft_article_feedback', + array( 'af_hide_count = af_hide_count + 1' ), + array( 'af_id' => $params['feedbackid'] ), + __METHOD__ + ); + } + if( $record->af_abuse_count > 3 ) { + // Return a flag in the JSON, that turns the link red. + $results['abusive'] = 1; + } } if ( $error ) { - $result = 'Error'; - $reason = $error; + $results['result'] = 'Error'; + $results['reason'] = $error; } else { - $result = 'Success'; - $reason = null; + $results['result'] = 'Success'; + $results['reason'] = null; } - $results = array( - 'result' => $result, - 'reason' => $reason, - ); - - if ( $helpful ) { - $results['helpful'] = $helpful; - } - $this->getResult()->addValue( null, $this->getModuleName(), @@ -170,6 +201,16 @@ ); } + private function fetchRecord( $id ) { + $dbr = wfGetDB( DB_SLAVE ); + $record = $dbr->selectRow( + 'aft_article_feedback', + array( 'af_id', 'af_abuse_count', 'af_hide_count', 'af_helpful_count', 'af_unhelpful_count', 'af_delete_count' ), + array( 'af_id' => $id ) + ); + return $record; + } + /** * Gets the allowed parameters * @@ -193,6 +234,12 @@ ApiBase::PARAM_TYPE => array( 'abuse', 'hide', 'helpful', 'unhelpful', 'delete', 'undelete', 'unhide', 'oversight', 'unoversight' ) ), + 'direction' => array( + ApiBase::PARAM_REQUIRED => false, + ApiBase::PARAM_ISMULTI => false, + ApiBase::PARAM_TYPE => array( + 'increase', 'decrease' ) + ) ); } Modified: trunk/extensions/ArticleFeedbackv5/api/ApiViewFeedbackArticleFeedbackv5.php =================================================================== --- trunk/extensions/ArticleFeedbackv5/api/ApiViewFeedbackArticleFeedbackv5.php 2012-01-27 00:11:20 UTC (rev 110097) +++ trunk/extensions/ArticleFeedbackv5/api/ApiViewFeedbackArticleFeedbackv5.php 2012-01-27 00:11:27 UTC (rev 110098) @@ -94,6 +94,7 @@ $continueSql = "CONVERT(af_helpful_count, SIGNED) - CONVERT(af_unhelpful_count, SIGNED) $continueDirection"; break; case 'rating': +# TODO # disable because it's broken # $sortField = 'rating'; # break; @@ -295,9 +296,9 @@ // Taken from the Moodbar extension. $now = wfTimestamp( TS_UNIX ); $timestamp = wfTimestamp( TS_UNIX, $record[0]->af_created ); + // Relative dates for 48 hours, normal timestamps later. if( $timestamp > ( $now - ( 86400 * 2 ) ) ) { - // TODO: relative dates. $time = $wgLang->formatTimePeriod( ( $now - $timestamp ), 'avoidseconds' ); _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs