Index: trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php |
— | — | @@ -22,45 +22,66 @@ |
23 | 23 | * Execute the API call: Pull the requested feedback |
24 | 24 | */ |
25 | 25 | 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; |
32 | 35 | |
33 | 36 | # 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'] ); |
39 | 38 | |
40 | | - $flags = array( 'abuse', 'hide', 'helpful', 'unhelpful', 'delete' ); |
41 | | - $flag = $params['flagtype']; |
42 | | - |
43 | 39 | if ( !$record->af_id ) { |
44 | 40 | // no-op, because this is already broken |
45 | 41 | $error = 'articlefeedbackv5-invalid-feedback-id'; |
46 | 42 | } 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 | + } |
49 | 50 | } 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 | + } |
52 | 58 | } 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 | + } |
56 | 68 | } 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 | + } |
59 | 76 | } elseif ( in_array( $params['flagtype'], $flags ) ) { |
60 | 77 | // Probably this doesn't need validation, since the API |
61 | 78 | // will handle it, but if it's getting interpolated into |
62 | 79 | // the SQL, I'm really wary not re-validating it. |
63 | 80 | $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 | + } |
65 | 86 | } else { |
66 | 87 | $error = 'articlefeedbackv5-invalid-feedback-flag'; |
67 | 88 | } |
— | — | @@ -119,50 +140,60 @@ |
120 | 141 | __METHOD__ |
121 | 142 | ); |
122 | 143 | |
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 | + } |
125 | 152 | |
| 153 | + // Update helpful/unhelpful count after submission. |
126 | 154 | // 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 |
128 | 156 | // of staying off of the write database. |
129 | 157 | // Better stale data than wail on the master, IMO, |
130 | 158 | // but I'm open to suggestion on that one. |
131 | | - |
132 | | - // Update helpful/unhelpful count after submission |
133 | 159 | 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'] ); |
140 | 161 | |
141 | 162 | $helpful = $record->af_helpful_count; |
142 | 163 | $unhelpful = $record->af_unhelpful_count; |
143 | 164 | |
144 | | - $helpful = wfMessage( 'articlefeedbackv5-form-helpful-votes', |
| 165 | + $results['helpful'] = wfMessage( 'articlefeedbackv5-form-helpful-votes', |
145 | 166 | $helpful, $unhelpful |
146 | 167 | )->escaped(); |
147 | 168 | } |
| 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 | + } |
148 | 188 | } |
149 | 189 | |
150 | 190 | if ( $error ) { |
151 | | - $result = 'Error'; |
152 | | - $reason = $error; |
| 191 | + $results['result'] = 'Error'; |
| 192 | + $results['reason'] = $error; |
153 | 193 | } else { |
154 | | - $result = 'Success'; |
155 | | - $reason = null; |
| 194 | + $results['result'] = 'Success'; |
| 195 | + $results['reason'] = null; |
156 | 196 | } |
157 | 197 | |
158 | | - $results = array( |
159 | | - 'result' => $result, |
160 | | - 'reason' => $reason, |
161 | | - ); |
162 | | - |
163 | | - if ( $helpful ) { |
164 | | - $results['helpful'] = $helpful; |
165 | | - } |
166 | | - |
167 | 198 | $this->getResult()->addValue( |
168 | 199 | null, |
169 | 200 | $this->getModuleName(), |
— | — | @@ -170,6 +201,16 @@ |
171 | 202 | ); |
172 | 203 | } |
173 | 204 | |
| 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 | + |
174 | 215 | /** |
175 | 216 | * Gets the allowed parameters |
176 | 217 | * |
— | — | @@ -193,6 +234,12 @@ |
194 | 235 | ApiBase::PARAM_TYPE => array( |
195 | 236 | 'abuse', 'hide', 'helpful', 'unhelpful', 'delete', 'undelete', 'unhide', 'oversight', 'unoversight' ) |
196 | 237 | ), |
| 238 | + 'direction' => array( |
| 239 | + ApiBase::PARAM_REQUIRED => false, |
| 240 | + ApiBase::PARAM_ISMULTI => false, |
| 241 | + ApiBase::PARAM_TYPE => array( |
| 242 | + 'increase', 'decrease' ) |
| 243 | + ) |
197 | 244 | ); |
198 | 245 | } |
199 | 246 | |
Index: trunk/extensions/ArticleFeedbackv5/api/ApiViewFeedbackArticleFeedbackv5.php |
— | — | @@ -94,6 +94,7 @@ |
95 | 95 | $continueSql = "CONVERT(af_helpful_count, SIGNED) - CONVERT(af_unhelpful_count, SIGNED) $continueDirection"; |
96 | 96 | break; |
97 | 97 | case 'rating': |
| 98 | +# TODO |
98 | 99 | # disable because it's broken |
99 | 100 | # $sortField = 'rating'; |
100 | 101 | # break; |
— | — | @@ -295,9 +296,9 @@ |
296 | 297 | // Taken from the Moodbar extension. |
297 | 298 | $now = wfTimestamp( TS_UNIX ); |
298 | 299 | $timestamp = wfTimestamp( TS_UNIX, $record[0]->af_created ); |
| 300 | + |
299 | 301 | // Relative dates for 48 hours, normal timestamps later. |
300 | 302 | if( $timestamp > ( $now - ( 86400 * 2 ) ) ) { |
301 | | - // TODO: relative dates. |
302 | 303 | $time = $wgLang->formatTimePeriod( |
303 | 304 | ( $now - $timestamp ), 'avoidseconds' |
304 | 305 | ); |