Index: trunk/extensions/ArticleAssessmentPilot/api/ApiArticleAssessment.php |
— | — | @@ -5,11 +5,9 @@ |
6 | 6 | } |
7 | 7 | |
8 | 8 | public function execute() { |
9 | | - global $wgUser; |
| 9 | + global $wgUser, $wgArticleAssessmentRatingCount; |
10 | 10 | $params = $this->extractRequestParams(); |
11 | 11 | |
12 | | - $userName = $wgUser->getName(); |
13 | | - |
14 | 12 | $dbr = wfGetDB( DB_SLAVE ); |
15 | 13 | |
16 | 14 | // TODO:Refactor out...? |
— | — | @@ -24,21 +22,16 @@ |
25 | 23 | __METHOD__ |
26 | 24 | ); |
27 | 25 | |
28 | | - $res = $res->fetchRow(); |
29 | | - |
30 | 26 | $lastRatings = array(); |
31 | 27 | |
32 | 28 | foreach ( $res as $row ) { |
33 | 29 | $lastRatings[$row->aa_rating_id] = $row->aa_rating_value; |
34 | 30 | } |
35 | 31 | |
36 | | - // Do for each metric/dimension |
37 | | - |
38 | 32 | $pageId = $params['pageid']; |
39 | 33 | $revisionId = $params['revid']; |
40 | 34 | |
41 | 35 | // TODO: Fold for loop into foreach above? |
42 | | - global $wgArticleAssessmentRatingCount; |
43 | 36 | for ( $i = 1; $i <= $wgArticleAssessmentRatingCount; $i++ ) { |
44 | 37 | $lastRating = 0; |
45 | 38 | if ( isset( $lastRatings[$i] ) ) { |
— | — | @@ -50,74 +43,94 @@ |
51 | 44 | $thisRating = $params["r{i}"]; |
52 | 45 | } |
53 | 46 | |
54 | | - $this->insertOrUpdatePageRating( $pageId, $revisionId, $i, $thisRating, ( $thisRating - $lastRating ), |
| 47 | + $this->insertPageRating( $pageId, $i, ( $thisRating - $lastRating ), |
55 | 48 | ( $lastRating == 0 && $thisRating != 0 ) |
56 | 49 | ); |
57 | 50 | |
58 | | - $this->insertOrUpdateUserRatings( $pageId, $revisionId, $userName, $i, $thisRating ); |
| 51 | + $this->insertUserRatings( $pageId, $revisionId, $wgUser, $i, $thisRating ); |
59 | 52 | } |
60 | 53 | |
61 | | - // Insert (or update) a users rating for a revision |
62 | | - |
63 | 54 | $r = array(); |
64 | 55 | $r['result'] = 'Success'; |
65 | 56 | $this->getResult()->addValue( null, $this->getModuleName(), $r ); |
66 | 57 | } |
| 58 | + |
67 | 59 | /* |
68 | 60 | * |
69 | | - * |
70 | 61 | * @param $pageId Integer: |
71 | | - * @param $revisionId Integer: |
72 | | - * @param $dimension Integer: |
73 | | - * @param $insert Integer: Users rating |
| 62 | + * @param $ratingId Integer: |
74 | 63 | * @param $updateAddition Integer: Difference between users last rating (if applicable) |
75 | 64 | * @param $newRating Boolean: Whether this is a new rating (for update, whether this increases the count) |
76 | 65 | */ |
77 | | - private function insertOrUpdatePageRating( $pageId, $revisionId, $rating, $insert, $updateAddition, $newRating ) { |
| 66 | + private function insertPageRating( $pageId, $ratingId, $updateAddition, $newRating ) { |
78 | 67 | $dbw = wfGetDB( DB_MASTER ); |
79 | 68 | |
80 | | - $dbw->insertOrUpdate( 'article_assessment_pages', |
81 | | - array( |
| 69 | + $dbw->insert( |
| 70 | + 'article_assessment_pages', |
| 71 | + array( |
82 | 72 | 'aap_page_id' => $pageId, |
83 | | - 'aap_revision' => $revisionId, |
84 | | - 'aap_total' => $insert, |
85 | | - 'aap_count' => 1, |
86 | | - 'aap_rating' => $rating, |
| 73 | + 'aap_total' => 0, |
| 74 | + 'aap_count' => 0, |
| 75 | + 'aap_rating_id' => $ratingId, |
87 | 76 | ), |
88 | 77 | __METHOD__, |
| 78 | + array( 'IGNORE' ) |
| 79 | + ); |
| 80 | + |
| 81 | + $dbw->update( |
| 82 | + 'article_assessment_pages', |
89 | 83 | array( |
90 | 84 | 'aap_total' => 'aap_total + ' . $updateAddition, |
91 | 85 | 'aap_count' => 'aap_count + ' . ( $newRating ? 1 : 0 ), |
92 | | - ) |
| 86 | + ), |
| 87 | + array( |
| 88 | + 'aap_page_id' => $pageId, |
| 89 | + 'aap_rating_id' => $ratingId, |
| 90 | + ), |
| 91 | + __METHOD__ |
93 | 92 | ); |
94 | 93 | } |
| 94 | + |
95 | 95 | /* |
96 | 96 | * @param $pageId Integer: |
97 | 97 | * @param $revisionId Integer: |
98 | | - * @param $user String: |
| 98 | + * @param $user User: |
99 | 99 | * @param $ratingId Integer: |
100 | 100 | * @param $ratingValue Integer: |
101 | 101 | */ |
102 | | - private function insertOrUpdateUserRatings( $pageId, $revisionId, $user, $ratingId, $ratingValue ) { |
| 102 | + private function insertUserRatings( $pageId, $revisionId, $user, $ratingId, $ratingValue ) { |
103 | 103 | $dbw = wfGetDB( DB_MASTER ); |
104 | 104 | |
105 | | - $dbw->insertOrUpdate( 'article_assessment', |
| 105 | + $res = $dbw->insert( |
| 106 | + 'article_assessment', |
106 | 107 | array( |
107 | 108 | 'aa_page_id' => $pageId, |
108 | | - 'aa_user_text' => $user, |
| 109 | + 'aa_user_id' => $user->getId(), |
| 110 | + 'aa_user_text' => $user->getName(), |
109 | 111 | 'aa_revision' => $revisionId, |
110 | 112 | 'aa_timestamp' => wfTimestampNow(), |
111 | 113 | 'aa_rating_id' => $ratingId, |
112 | 114 | 'aa_rating_value' => $ratingValue, |
113 | 115 | ), |
114 | 116 | __METHOD__, |
| 117 | + array( 'IGNORE' ) |
| 118 | + ); |
| 119 | + |
| 120 | + //TODO: Don't do this if the insert was successful |
| 121 | + $dbw->update( |
| 122 | + 'article_assessment', |
115 | 123 | array( |
116 | 124 | 'aa_timestamp' => wfTimestampNow(), |
117 | 125 | 'aa_rating_id' => $ratingId, |
118 | 126 | 'aa_rating_value' => $ratingValue, |
119 | | - ) |
| 127 | + ), |
| 128 | + array( |
| 129 | + 'aa_page_id' => $pageId, |
| 130 | + 'aa_user_text' => $user->getName(), |
| 131 | + 'aa_revision' => $revisionId, |
| 132 | + ), |
| 133 | + __METHOD__ |
120 | 134 | ); |
121 | | - |
122 | 135 | } |
123 | 136 | |
124 | 137 | public function getAllowedParams() { |
— | — | @@ -126,15 +139,17 @@ |
127 | 140 | 'pageid' => array( |
128 | 141 | ApiBase::PARAM_TYPE => 'integer', |
129 | 142 | ApiBase::PARAM_REQUIRED => true, |
| 143 | + ApiBase::PARAM_ISMULTI => false, |
130 | 144 | ), |
131 | 145 | 'revid' => array( |
132 | 146 | ApiBase::PARAM_TYPE => 'integer', |
133 | 147 | ApiBase::PARAM_REQUIRED => true, |
| 148 | + ApiBase::PARAM_ISMULTI => false, |
134 | 149 | ) |
135 | 150 | ); |
136 | 151 | |
137 | 152 | for ( $i = 1; $i <= $wgArticleAssessmentRatingCount; $i++ ) { |
138 | | - $ret['r{$i}'] = array( |
| 153 | + $ret["r{$i}"] = array( |
139 | 154 | ApiBase::PARAM_TYPE => 'integer', |
140 | 155 | ApiBase::PARAM_DFLT => 0, |
141 | 156 | ApiBase::PARAM_MIN => 0, |
— | — | @@ -147,11 +162,11 @@ |
148 | 163 | public function getParamDescription() { |
149 | 164 | global $wgArticleAssessmentRatingCount; |
150 | 165 | $ret = array( |
151 | | - 'pageid' => '', |
152 | | - 'revid' => '' |
| 166 | + 'pageid' => 'Page ID to submit assessment for', |
| 167 | + 'revid' => 'Revision ID to submit assessment for' |
153 | 168 | ); |
154 | 169 | for ( $i = 1; $i <= $wgArticleAssessmentRatingCount; $i++ ) { |
155 | | - $ret['r{$i}'] = 'Rating {$i}'; |
| 170 | + $ret["r{$i}"] = "Rating {$i}"; |
156 | 171 | } |
157 | 172 | return $ret; |
158 | 173 | } |