Index: trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php |
— | — | @@ -64,9 +64,11 @@ |
65 | 65 | $thisRating = intval( $params["r{$rating}"] ); |
66 | 66 | } |
67 | 67 | |
68 | | - $this->insertPageRating( $pageId, $revisionId, $lastRevision, $rating, ( $thisRating - $lastRating ), |
| 68 | + $this->insertRevisionRating( $pageId, $revisionId, $lastRevision, $rating, ( $thisRating - $lastRating ), |
69 | 69 | $thisRating, $lastRating |
70 | 70 | ); |
| 71 | + |
| 72 | + $this->insertPageRating( $pageId, $rating, ( $thisRating - $lastRating ), $thisRating, $lastRating ); |
71 | 73 | |
72 | 74 | $this->insertUserRatings( $pageId, $revisionId, $wgUser, $token, $rating, $thisRating, $params['bucket'] ); |
73 | 75 | } |
— | — | @@ -76,11 +78,70 @@ |
77 | 79 | $r = array( 'result' => 'Success' ); |
78 | 80 | $this->getResult()->addValue( null, $this->getModuleName(), $r ); |
79 | 81 | } |
80 | | - |
| 82 | + |
81 | 83 | /** |
82 | 84 | * Inserts (or Updates, where appropriate) the aggregate page rating |
83 | 85 | * |
84 | 86 | * @param $pageId Integer: Page Id |
| 87 | + * @param $ratingId Integer: Rating Id |
| 88 | + * @param $updateAddition Integer: Difference between user's last rating (if applicable) |
| 89 | + * @param $thisRating Integer|Boolean: Value of the Rating |
| 90 | + * @param $lastRating Integer|Boolean: Value of the last Rating |
| 91 | + */ |
| 92 | + private function insertPageRating( $pageId, $ratingId, $updateAddition, $thisRating, $lastRating ) { |
| 93 | + $dbw = wfGetDB( DB_MASTER ); |
| 94 | + |
| 95 | + // 0 == No change in rating count |
| 96 | + // 1 == No rating last time (or new rating), and now there is |
| 97 | + // -1 == Rating last time, but abstained this time |
| 98 | + $countChange = 0; |
| 99 | + if ( $lastRating === false || $lastRating === 0 ) { |
| 100 | + if ( $thisRating === 0 ) { |
| 101 | + $countChange = 0; |
| 102 | + } else { |
| 103 | + $countChange = 1; |
| 104 | + } |
| 105 | + } else { // Last rating was > 0 |
| 106 | + if ( $thisRating === 0 ) { |
| 107 | + $countChange = -1; |
| 108 | + } else { |
| 109 | + $countChange = 0; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Try to insert a new afp row for this page with zeroes in it |
| 114 | + // Try will silently fail if the row already exists |
| 115 | + $dbw->insert( |
| 116 | + 'article_feedback_pages', |
| 117 | + array( |
| 118 | + 'aap_page_id' => $pageId, |
| 119 | + 'aap_total' => 0, |
| 120 | + 'aap_count' => 0, |
| 121 | + 'aap_rating_id' => $ratingId, |
| 122 | + ), |
| 123 | + __METHOD__, |
| 124 | + array( 'IGNORE' ) |
| 125 | + ); |
| 126 | + |
| 127 | + // We now know the row exists, so increment it |
| 128 | + $dbw->update( |
| 129 | + 'article_feedback_pages', |
| 130 | + array( |
| 131 | + 'aap_total = aap_total + ' . $updateAddition, |
| 132 | + 'aap_count = aap_count + ' . $countChange, |
| 133 | + ), |
| 134 | + array( |
| 135 | + 'aap_page_id' => $pageId, |
| 136 | + 'aap_rating_id' => $ratingId, |
| 137 | + ), |
| 138 | + __METHOD__ |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Inserts (or Updates, where appropriate) the aggregate revision rating |
| 144 | + * |
| 145 | + * @param $pageId Integer: Page Id |
85 | 146 | * @param $revisionId Integer: Revision Id |
86 | 147 | * @param $lastRevision Integer: Revision Id of last rating |
87 | 148 | * @param $ratingId Integer: Rating Id |
— | — | @@ -88,7 +149,7 @@ |
89 | 150 | * @param $thisRating Integer|Boolean: Value of the Rating |
90 | 151 | * @param $lastRating Integer|Boolean: Value of the last Rating |
91 | 152 | */ |
92 | | - private function insertPageRating( $pageId, $revisionId, $lastRevision, $ratingId, $updateAddition, $thisRating, $lastRating ) { |
| 153 | + private function insertRevisionRating( $pageId, $revisionId, $lastRevision, $ratingId, $updateAddition, $thisRating, $lastRating ) { |
93 | 154 | $dbw = wfGetDB( DB_MASTER ); |
94 | 155 | |
95 | 156 | // Try to insert a new "totals" row for this page,rev,rating set |