r71812 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71811‎ | r71812 | r71813 >
Date:22:05, 27 August 2010
Author:reedy
Status:deferred
Tags:
Comment:
Overhaul insert code post removeal of insertOrUpdate

Few tweaks and updates :)
Modified paths:
  • /trunk/extensions/ArticleAssessmentPilot/api/ApiArticleAssessment.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleAssessmentPilot/api/ApiArticleAssessment.php
@@ -5,11 +5,9 @@
66 }
77
88 public function execute() {
9 - global $wgUser;
 9+ global $wgUser, $wgArticleAssessmentRatingCount;
1010 $params = $this->extractRequestParams();
1111
12 - $userName = $wgUser->getName();
13 -
1412 $dbr = wfGetDB( DB_SLAVE );
1513
1614 // TODO:Refactor out...?
@@ -24,21 +22,16 @@
2523 __METHOD__
2624 );
2725
28 - $res = $res->fetchRow();
29 -
3026 $lastRatings = array();
3127
3228 foreach ( $res as $row ) {
3329 $lastRatings[$row->aa_rating_id] = $row->aa_rating_value;
3430 }
3531
36 - // Do for each metric/dimension
37 -
3832 $pageId = $params['pageid'];
3933 $revisionId = $params['revid'];
4034
4135 // TODO: Fold for loop into foreach above?
42 - global $wgArticleAssessmentRatingCount;
4336 for ( $i = 1; $i <= $wgArticleAssessmentRatingCount; $i++ ) {
4437 $lastRating = 0;
4538 if ( isset( $lastRatings[$i] ) ) {
@@ -50,74 +43,94 @@
5144 $thisRating = $params["r{i}"];
5245 }
5346
54 - $this->insertOrUpdatePageRating( $pageId, $revisionId, $i, $thisRating, ( $thisRating - $lastRating ),
 47+ $this->insertPageRating( $pageId, $i, ( $thisRating - $lastRating ),
5548 ( $lastRating == 0 && $thisRating != 0 )
5649 );
5750
58 - $this->insertOrUpdateUserRatings( $pageId, $revisionId, $userName, $i, $thisRating );
 51+ $this->insertUserRatings( $pageId, $revisionId, $wgUser, $i, $thisRating );
5952 }
6053
61 - // Insert (or update) a users rating for a revision
62 -
6354 $r = array();
6455 $r['result'] = 'Success';
6556 $this->getResult()->addValue( null, $this->getModuleName(), $r );
6657 }
 58+
6759 /*
6860 *
69 - *
7061 * @param $pageId Integer:
71 - * @param $revisionId Integer:
72 - * @param $dimension Integer:
73 - * @param $insert Integer: Users rating
 62+ * @param $ratingId Integer:
7463 * @param $updateAddition Integer: Difference between users last rating (if applicable)
7564 * @param $newRating Boolean: Whether this is a new rating (for update, whether this increases the count)
7665 */
77 - private function insertOrUpdatePageRating( $pageId, $revisionId, $rating, $insert, $updateAddition, $newRating ) {
 66+ private function insertPageRating( $pageId, $ratingId, $updateAddition, $newRating ) {
7867 $dbw = wfGetDB( DB_MASTER );
7968
80 - $dbw->insertOrUpdate( 'article_assessment_pages',
81 - array(
 69+ $dbw->insert(
 70+ 'article_assessment_pages',
 71+ array(
8272 '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,
8776 ),
8877 __METHOD__,
 78+ array( 'IGNORE' )
 79+ );
 80+
 81+ $dbw->update(
 82+ 'article_assessment_pages',
8983 array(
9084 'aap_total' => 'aap_total + ' . $updateAddition,
9185 'aap_count' => 'aap_count + ' . ( $newRating ? 1 : 0 ),
92 - )
 86+ ),
 87+ array(
 88+ 'aap_page_id' => $pageId,
 89+ 'aap_rating_id' => $ratingId,
 90+ ),
 91+ __METHOD__
9392 );
9493 }
 94+
9595 /*
9696 * @param $pageId Integer:
9797 * @param $revisionId Integer:
98 - * @param $user String:
 98+ * @param $user User:
9999 * @param $ratingId Integer:
100100 * @param $ratingValue Integer:
101101 */
102 - private function insertOrUpdateUserRatings( $pageId, $revisionId, $user, $ratingId, $ratingValue ) {
 102+ private function insertUserRatings( $pageId, $revisionId, $user, $ratingId, $ratingValue ) {
103103 $dbw = wfGetDB( DB_MASTER );
104104
105 - $dbw->insertOrUpdate( 'article_assessment',
 105+ $res = $dbw->insert(
 106+ 'article_assessment',
106107 array(
107108 'aa_page_id' => $pageId,
108 - 'aa_user_text' => $user,
 109+ 'aa_user_id' => $user->getId(),
 110+ 'aa_user_text' => $user->getName(),
109111 'aa_revision' => $revisionId,
110112 'aa_timestamp' => wfTimestampNow(),
111113 'aa_rating_id' => $ratingId,
112114 'aa_rating_value' => $ratingValue,
113115 ),
114116 __METHOD__,
 117+ array( 'IGNORE' )
 118+ );
 119+
 120+ //TODO: Don't do this if the insert was successful
 121+ $dbw->update(
 122+ 'article_assessment',
115123 array(
116124 'aa_timestamp' => wfTimestampNow(),
117125 'aa_rating_id' => $ratingId,
118126 '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__
120134 );
121 -
122135 }
123136
124137 public function getAllowedParams() {
@@ -126,15 +139,17 @@
127140 'pageid' => array(
128141 ApiBase::PARAM_TYPE => 'integer',
129142 ApiBase::PARAM_REQUIRED => true,
 143+ ApiBase::PARAM_ISMULTI => false,
130144 ),
131145 'revid' => array(
132146 ApiBase::PARAM_TYPE => 'integer',
133147 ApiBase::PARAM_REQUIRED => true,
 148+ ApiBase::PARAM_ISMULTI => false,
134149 )
135150 );
136151
137152 for ( $i = 1; $i <= $wgArticleAssessmentRatingCount; $i++ ) {
138 - $ret['r{$i}'] = array(
 153+ $ret["r{$i}"] = array(
139154 ApiBase::PARAM_TYPE => 'integer',
140155 ApiBase::PARAM_DFLT => 0,
141156 ApiBase::PARAM_MIN => 0,
@@ -147,11 +162,11 @@
148163 public function getParamDescription() {
149164 global $wgArticleAssessmentRatingCount;
150165 $ret = array(
151 - 'pageid' => '',
152 - 'revid' => ''
 166+ 'pageid' => 'Page ID to submit assessment for',
 167+ 'revid' => 'Revision ID to submit assessment for'
153168 );
154169 for ( $i = 1; $i <= $wgArticleAssessmentRatingCount; $i++ ) {
155 - $ret['r{$i}'] = 'Rating {$i}';
 170+ $ret["r{$i}"] = "Rating {$i}";
156171 }
157172 return $ret;
158173 }

Status & tagging log