r102568 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102567‎ | r102568 | r102569 >
Date:20:39, 9 November 2011
Author:gregchiasson
Status:deferred
Tags:
Comment:
Revert changes to apiQuery module. AFTv5 should be just the unmodified aftv4 now.
Modified paths:
  • /trunk/extensions/ArticleFeedbackv5/api/ApiQueryArticleFeedback.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedbackv5/api/ApiQueryArticleFeedback.php
@@ -1,276 +1,214 @@
22 <?php
3 -# This file loads the data and all. The other one saves it.
43 class ApiQueryArticleFeedback extends ApiQueryBase {
54 public function __construct( $query, $moduleName ) {
65 parent::__construct( $query, $moduleName, 'af' );
76 }
8 -
9 - # split these two off into their own modules, instead of doing this.
 7+
108 public function execute() {
11 -error_log('hi');
12 - $params = $this->extractRequestParams();
139 global $wgArticleFeedbackRatingTypes;
 10+
 11+ $params = $this->extractRequestParams();
 12+ $result = $this->getResult();
 13+ $revisionLimit = $this->getRevisionLimit( $params['pageid'] );
 14+
 15+ $this->addTables( array( 'article_feedback_revisions' ) );
 16+ $this->addFields( array(
 17+ 'MAX(afr_revision) as afr_revision',
 18+ 'SUM(afr_total) as afr_total',
 19+ 'SUM(afr_count) as afr_count',
 20+ 'afr_rating_id',
 21+ ) );
 22+ $this->addWhereFld( 'afr_page_id', $params['pageid'] );
 23+ $this->addWhere( 'afr_revision >= ' . $revisionLimit );
 24+ $this->addWhereFld( 'afr_rating_id', array_keys( $wgArticleFeedbackRatingTypes ) );
 25+ $this->addOption( 'GROUP BY', 'afr_rating_id' );
 26+ $this->addOption( 'LIMIT', count( $wgArticleFeedbackRatingTypes ) );
1427
15 - if($params['subaction'] == 'showratings') {
16 - $this->executeFetchRatings();
17 - } else {
18 - $this->executeNewForm();
 28+ // Rating counts and totals
 29+ $res = $this->select( __METHOD__ );
 30+ $ratings = array( $params['pageid'] => array( 'pageid' => $params['pageid'] ) );
 31+ $historicCounts = $this->getHistoricCounts( $params );
 32+ foreach ( $res as $i => $row ) {
 33+ if ( !isset( $ratings[$params['pageid']]['revid'] ) ) {
 34+ $ratings[$params['pageid']]['revid'] = (int) $row->afr_revision;
 35+ }
 36+ if ( !isset( $ratings[$params['pageid']]['ratings'] ) ) {
 37+ $ratings[$params['pageid']]['ratings'] = array();
 38+ }
 39+ $ratings[$params['pageid']]['ratings'][] = array(
 40+ 'ratingid' => (int) $row->afr_rating_id,
 41+ 'ratingdesc' => $wgArticleFeedbackRatingTypes[$row->afr_rating_id],
 42+ 'total' => (int) $row->afr_total,
 43+ 'count' => (int) $row->afr_count,
 44+ 'countall' => isset( $historicCounts[$row->afr_rating_id] )
 45+ ? (int) $historicCounts[$row->afr_rating_id] : 0
 46+ );
1947 }
20 - }
2148
22 - # Initialize a brand new request
23 - protected function executeNewForm() {
24 - global $wgUser;
25 - $params = $this->extractRequestParams();
26 - $bucket = $this->getBucket();
27 - $result = $this->getResult();
 49+ // User-specific data
 50+ $ratings[$params['pageid']]['status'] = 'current';
 51+ if ( $params['userrating'] ) {
 52+ // User ratings
 53+ $userRatings = $this->getUserRatings( $params );
2854
29 - if(!$params['pageid'] || !$params['revid']) {
30 - return null;
31 - }
 55+ // If valid ratings already exist..
 56+ if ( isset( $ratings[$params['pageid']]['ratings'] ) ) {
 57+ foreach ( $ratings[$params['pageid']]['ratings'] as $i => $rating ) {
 58+ if ( isset( $userRatings[$rating['ratingid']] ) ) {
 59+ // Rating value
 60+ $ratings[$params['pageid']]['ratings'][$i]['userrating'] =
 61+ (int) $userRatings[$rating['ratingid']]['value'];
 62+ // Expiration
 63+ if ( $userRatings[$rating['ratingid']]['revision'] < $revisionLimit ) {
 64+ $ratings[$params['pageid']]['status'] = 'expired';
 65+ }
 66+ }
 67+ }
3268
33 - $result->addValue('form', 'pageId', $params['pageid']);
34 - $result->addValue('form', 'bucketId', $bucket);
35 - $this->logBucket($params['pageid'], $params['revid'], $bucket);
 69+ // Else, no valid ratings exist..
 70+ } else {
3671
37 - /* Commented out, because we're allowing unlimited comments.
38 - $userData = array();
39 - $tmp = $this->getFeedbackId($params, $bucket);
40 - $feedbackId = $tmp['feedbackId'];
41 - if($feedbackId && $tmp['userId'] == $wgUser->getId()) {
42 - $userData = $this->getUserRatings($feedbackId);
43 - }
44 - $result->addValue('form', 'feedbackId', $feedbackId);
45 - foreach($userData as $row) {
46 - $key = $val['key'];
47 - $val = $val['value'];
48 - $result->addValue(array('form', 'data'), $key, $val);
49 - }
50 - */
51 - }
 72+ if ( count( $userRatings ) ) {
 73+ $ratings[$params['pageid']]['status'] = 'expired';
 74+ }
5275
53 - protected function executeFetchRatings() {
54 - $params = $this->extractRequestParams();
55 - $result = $this->getResult();
56 - $revisionLimit = $this->getRevisionLimit( $params['pageid'] );
57 - $bucket = $this->getBucket();
58 - $pageId = $params['pageid'];
59 - $rows = $this->fetchRevisionRollup($pageId, $revisionLimit);
60 - $historical = $this->fetchPageRollup($pageId);
61 - $ratings = array(
62 - 'pageid' => $params['pageid'],
63 - 'ratings' => array(),
64 - 'status' => 'current'
65 - );
 76+ foreach ( $userRatings as $ratingId => $userRating ) {
 77+ // Revision
 78+ if ( !isset( $ratings[$params['pageid']]['revid'] ) ) {
 79+ $ratings[$params['pageid']]['revid'] = (int) $userRating['revision'];
 80+ }
 81+ // Ratings
 82+ if ( !isset( $ratings[$params['pageid']]['ratings'] ) ) {
 83+ $ratings[$params['pageid']]['ratings'] = array();
 84+ }
 85+ // Rating value
 86+ $ratings[$params['pageid']]['ratings'][] = array(
 87+ 'ratingid' => $ratingId,
 88+ 'ratingdesc' => $userRating['text'],
 89+ 'total' => 0,
 90+ 'count' => 0,
 91+ 'countall' => isset( $historicCounts[$row->afr_rating_id] )
 92+ ? (int) $historicCounts[$row->afr_rating_id] : 0,
 93+ 'userrating' => (int) $userRating['value'],
 94+ );
 95+ }
 96+ }
6697
67 - foreach ( $rows as $row ) {
68 - $overall = 0;
69 - foreach($historical as $ancient) {
70 - if($ancient->aaf_name == $row->aaf_name) {
71 - $overall = $ancient->reviews;
 98+ // Expertise
 99+ if ( isset( $ratings[$params['pageid']]['revid'] ) ) {
 100+ $expertise = $this->getExpertise( $params, $ratings[$params['pageid']]['revid'] );
 101+ if ( $expertise !== false ) {
 102+ $ratings[$params['pageid']]['expertise'] = $expertise;
72103 }
73104 }
74 - $ratings['ratings'][] = array(
75 - 'ratingdesc' => $row->field_name,
76 - 'ratingid' => (int) $row->field_id,
77 - 'total' => (int) $row->points,
78 - 'count' => (int) $row->reviews,
79 - 'countall' => (int) $overall
80 - );
81105 }
82106
83 - foreach ( $ratings as $r ) {
84 - $result->addValue(
85 - array('query', $this->getModuleName()), null, $r
86 - );
 107+ foreach ( $ratings as $rat ) {
 108+ if ( isset( $rat['ratings'] ) ) {
 109+ $result->setIndexedTagName( $rat['ratings'], 'r' );
 110+ }
 111+
 112+ $result->addValue( array( 'query', $this->getModuleName() ), null, $rat );
87113 }
88 -
 114+
89115 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'aa' );
90116 }
91117
92 - protected function getBucket() {
93 - #TODO base this on last 2 digits of IP address per requirements
94 - return 5;
95 - }
96 -
97 - private function logBucket($page, $revision, $bucket) {
98 - $dbw = wfGetDB( DB_MASTER );
99 - $dbr = wfGetDB( DB_SLAVE );
100 -
101 - # Select hit counter row
102 - $row = $dbr->select(
103 - 'aft_article_hits',
104 - 'aah_counter',
105 - array(
106 - 'aah_page_id' => $page,
107 - 'aah_revision_id' => $revision,
108 - 'aah_bucket_id' => $bucket,
109 - )
110 - );
111 -
112 - # If there's a row, update it.
113 - if($row) {
114 - $dbw->insert(
115 - 'aft_article_hits'
116 - array( 'aah_counter' => ($row['aah_counter'} + 1) ),
117 - array(
118 - 'aah_page_id' => $page,
119 - 'aah_revision_id' => $revision,
120 - 'aah_bucket_id' => $bucket,
121 - )
122 - );
 118+ protected function getHistoricCounts( $params ) {
 119+ global $wgArticleFeedbackRatingTypes;
123120
124 - }
125 -
126 - # Otherwise, there's no row, insert one.
127 - $dbw->insert('aft_article_hits' array(
128 - 'aah_page_id' => $page,
129 - 'aah_revision_id' => $revision,
130 - 'aah_bucket_id' => $bucket,
131 - 'aah_counter' => 1
132 - ));
133 - }
134 -
135 - public function fetchPageRollup($pageId, $revisionLimit = 0) {
136 - return $this->fetchRollup($pageId, $revisionLimit, 'page');
137 - }
138 -
139 - public function fetchRevisionRollup($pageId, $revisionLimit = 0) {
140 - return $this->fetchRollup($pageId, $revisionLimit, 'revision');
141 - }
142 -
143 - private function fetchRollup($pageId, $revisionLimit, $type) {
144 - $dbr = wfGetDB( DB_SLAVE );
145 - $where = array();
146 -
147 - if($type == 'page') {
148 - $table = 'article_feedback_ratings_rollup';
149 - $prefix = 'aap';
150 - } else {
151 - $table = 'article_revision_feedback_ratings_rollup';
152 - $prefix = 'afr';
153 - $where[] = 'afr_revision >= '.$revisionLimit; #lmao
154 - }
155 - $where[$prefix.'_page_id'] = $pageId;
156 - $where[] = $prefix.'_rating_id = aaf_id';
157 -
158 - $rows = $dbr->select(
159 - array( $table, 'article_field' ),
 121+ $res = $this->getDB()->select(
 122+ 'article_feedback_pages',
160123 array(
161 - 'aaf_name AS field_name',
162 - $prefix.'_rating_id AS field_id',
163 - 'SUM('.$prefix.'_total) AS points',
164 - 'SUM('.$prefix.'_count) AS reviews',
 124+ 'aap_rating_id',
 125+ 'aap_count',
165126 ),
166 - $where,
167 - __METHOD__,
168127 array(
169 - 'GROUP BY' => $prefix.'_rating_id, aaf_name'
170 - )
 128+ 'aap_page_id' => $params['pageid'],
 129+ 'aap_rating_id' => array_keys( $wgArticleFeedbackRatingTypes ),
 130+ ),
 131+ __METHOD__
171132 );
172 -
173 - return $rows;
 133+ $counts = array();
 134+ foreach ( $res as $row ) {
 135+ $counts[$row->aap_rating_id] = $row->aap_count;
 136+ }
 137+ return $counts;
174138 }
175139
176 - # Either returns this users feedbackID for this page/rev, or saves
177 - # a new one and returns that.
178 - protected function getFeedbackId($params, $bucket) {
 140+ protected function getAnonToken( $params ) {
179141 global $wgUser;
180 - $token = ApiArticleFeedbackUtils::getAnonToken($params);
181 - $dbr = wfGetDB( DB_SLAVE );
182 - $timestamp = $dbr->timestamp();
183 - $revId = $params['revid'];
 142+ $token = '';
 143+ if ( $wgUser->isAnon() && $params['userrating'] ) {
 144+ if ( !isset( $params['anontoken'] ) ) {
 145+ $this->dieUsageMsg( array( 'missingparam', 'anontoken' ) );
 146+ } elseif ( strlen( $params['anontoken'] ) != 32 ) {
 147+ $this->dieUsage( 'The anontoken is not 32 characters', 'invalidtoken' );
 148+ }
 149+ $token = $params['anontoken'];
 150+ }
 151+ return $token;
 152+ }
 153+
 154+ protected function getExpertise( $params, $revid ) {
 155+ global $wgUser;
184156
185 - # make sure we have the page/revision/user
186 - if(!$params['pageid'] || !$wgUser) { return array(); }
 157+ return $this->getDB()->selectField(
 158+ 'article_feedback_properties',
 159+ 'afp_value_text',
 160+ array(
 161+ 'afp_key' => 'expertise',
 162+ 'afp_user_text' => $wgUser->getName(),
 163+ 'afp_user_anon_token' => $this->getAnonToken( $params ),
 164+ 'afp_revision' => $revid,
 165+ ),
 166+ __METHOD__
 167+ );
 168+ }
 169+
 170+ protected function getUserRatings( $params ) {
 171+ global $wgUser, $wgArticleFeedbackRatingTypes;
187172
188 - # Fetch this if it wasn't passed in
189 - if(!$revId) {
190 - $revId = $dbr->selectField(
191 - 'revision', 'rev_id',
192 - array('rev_page' => $params['pageid']),
193 - __METHOD__,
194 - array(
195 - 'ORDER BY' => 'rev_id DESC',
196 - 'LIMIT' => 1
197 - )
198 - );
199 - }
200 -
201 - # check for existing feedback for this rev/page/user
202 - $feedbackId = $dbr->selectField(
203 - 'article_feedback', 'aa_id',
 173+ $res = $this->getDB()->select(
 174+ array( 'article_feedback' ),
204175 array(
205 - 'aa_page_id' => $params['pageid'],
206 - 'aa_revision' => $revId,
207 - 'aa_user_text' => $wgUser->getName()
 176+ 'aa_rating_id',
 177+ 'aa_revision',
 178+ 'aa_rating_value',
208179 ),
 180+ array(
 181+ 'aa_page_id' => $params['pageid'],
 182+ 'aa_rating_id' => array_keys( $wgArticleFeedbackRatingTypes ),
 183+ 'aa_user_text' => $wgUser->getName(),
 184+ 'aa_user_anon_token' => $this->getAnonToken( $params ),
 185+ ),
209186 __METHOD__,
210187 array(
 188+ 'LIMIT' => count( $wgArticleFeedbackRatingTypes ),
211189 'ORDER BY' => 'aa_id DESC',
212 - 'LIMIT' => 1
213190 )
214191 );
215 - if($feedbackId) {
216 - return array(
217 - 'feedbackId' => $feedbackId,
218 - 'userId' => $wgUser->getId()
219 - );
 192+ $ratings = array();
 193+ $revId = null;
 194+ foreach ( $res as $row ) {
 195+ if ( $revId === null ) {
 196+ $revId = $row->aa_revision;
 197+ }
 198+ // Prevent incomplete rating sets from making a mess
 199+ if ( $revId === $row->aa_revision ) {
 200+ $ratings[$row->aa_rating_id] = array(
 201+ 'value' => $row->aa_rating_value,
 202+ 'revision' => $row->aa_revision,
 203+ 'text' => $wgArticleFeedbackRatingTypes[$row->aa_rating_id],
 204+ );
 205+ }
220206 }
221 -
222 - # insert new row if we don't already have one
223 - $dbw = wfGetDB( DB_MASTER );
224 - $dbw->insert('article_feedback', array(
225 - 'aa_page_id' => $params['pageid'],
226 - 'aa_revision' => $revId,
227 - 'aa_created' => $timestamp,
228 - 'aa_user_id' => $wgUser->getId(),
229 - 'aa_user_text' => $wgUser->getName(),
230 - 'aa_user_anon_token' => $token,
231 - 'aa_design_bucket' => $bucket,
232 - ), __METHOD__);
233 - return array(
234 - 'feedbackId' => $dbw->insertID(),
235 - 'userId' => $wgUser->getId()
236 - );
 207+ return $ratings;
237208 }
238209
239 - # Gets the user's feedback for this page. Only works on userids,
240 - # NOT IP adderesses. Idea being that IPs can move, and we don't want
241 - # your comments being shown to a different person who took your IP.
242 - # ALSO take revision limit into account.
243 - protected function getUserRatings($feedbackId) {
244 - global $wgUser;
245 - $dbr = wfGetDB( DB_SLAVE );
246 - $feedback = array();
247 - $rows = $dbr->select(
248 - array('article_answer', 'article_field',
249 - 'article_feedback'),
250 - array('aaaa_response_rating', 'aaaa_response_text',
251 - 'aaaa_response_bool', 'aaaa_response_option_id',
252 - 'aaf_name', 'aaf_data_type'),
253 - array(
254 - 'aa_revision >= '.$this->getRevisionLimit(),
255 - 'aaaa_feedback_id' => $feedbackId,
256 - 'aa_user_id' => $wgUser->getId(),
257 - 'aa_is_submitted' => 1,
258 - ),
259 - __METHOD__
260 - );
261 -
262 - foreach($rows as $row) {
263 - $method = 'response_'.$row->aaf_data_type;
264 - $feeedback[] = array(
265 - 'name' => $row->aaf_name,
266 - 'value' => $row->$method
267 - );
268 - }
269 - return $feedback;
270 - }
271 -
272210 /**
273211 * Get the revision number of the oldest revision still being counted in totals.
274 - *
 212+ *
275213 * @param $pageId Integer: ID of page to check revisions for
276214 * @return Integer: Oldest valid revision number or 0 of all revisions are valid
277215 */
@@ -284,8 +222,8 @@
285223 __METHOD__,
286224 array(
287225 'ORDER BY' => 'rev_id DESC',
288 - 'LIMIT' => 1,
289 - 'OFFSET' => $wgArticleFeedbackRatingLifetime - 1
 226+ 'LIMIT' => 1,
 227+ 'OFFSET' => $wgArticleFeedbackRatingLifetime - 1
290228 )
291229 );
292230 if ( $revision ) {
@@ -293,7 +231,7 @@
294232 }
295233 return 0;
296234 }
297 -
 235+
298236 public function getCacheMode( $params ) {
299237 if ( $params['userrating'] ) {
300238 return 'anon-public-user-private';
@@ -304,23 +242,13 @@
305243
306244 public function getAllowedParams() {
307245 return array(
308 - 'userrating' => 0,
309 - 'anontoken' => null,
310 - 'subaction' => array(
311 - ApiBase::PARAM_REQUIRED => false,
312 - ApiBase::PARAM_ISMULTI => false,
313 - ApiBase::PARAM_TYPE => array('showratings','newform'),
314 - ),
315 - 'revid' => array(
316 - ApiBase::PARAM_REQUIRED => false,
317 - ApiBase::PARAM_ISMULTI => false,
318 - ApiBase::PARAM_TYPE => 'integer',
319 - ),
320 - 'pageid' => array(
 246+ 'pageid' => array(
321247 ApiBase::PARAM_REQUIRED => true,
322 - ApiBase::PARAM_ISMULTI => false,
323 - ApiBase::PARAM_TYPE => 'integer',
324 - )
 248+ ApiBase::PARAM_ISMULTI => false,
 249+ ApiBase::PARAM_TYPE => 'integer',
 250+ ),
 251+ 'userrating' => 0,
 252+ 'anontoken' => null,
325253 );
326254 }
327255
@@ -356,4 +284,4 @@
357285 public function getVersion() {
358286 return __CLASS__ . ': $Id$';
359287 }
360 -}
 288+}
\ No newline at end of file

Status & tagging log