r106018 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106017‎ | r106018 | r106019 >
Date:13:06, 13 December 2011
Author:rsterbin
Status:resolved (Comments)
Tags:
Comment:
Fix for r105252: Not checking the result of Title::newFromID could cause a fatal error
Modified paths:
  • /trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5.php
@@ -81,7 +81,7 @@
8282 }
8383 }
8484
85 - if($error) {
 85+ if ( $error ) {
8686 $this->getResult()->addValue(
8787 null, 'error', $error
8888 );
@@ -91,7 +91,7 @@
9292 $ctaId = $this->saveUserRatings( $user_answers, $feedbackId, $bucket );
9393 $this->updateRollupTables( $pageId, $revisionId, $user_answers );
9494
95 - if( $params['email'] ) {
 95+ if ( $params['email'] ) {
9696 $this->captureEmail ( $params['email'], FormatJson::encode(
9797 $email_data
9898 ) );
@@ -193,10 +193,10 @@
194194
195195 /**
196196 * Cache result of ApiArticleFeedbackv5Utils::getRevisionLimit to avoid
197 - * multiple fetches.
198 -
199 - * @param $pageID int the page id
200 - * @return int the oldest revision to still count
 197+ * multiple fetches.
 198+ *
 199+ * @param $pageID int the page id
 200+ * @return int the oldest revision to still count
201201 */
202202 public function getRevisionLimit( $pageId ) {
203203 if( $this->revision_limit === null ) {
@@ -212,19 +212,19 @@
213213 * @param $revision int the revision id
214214 * @param $type string the type (rating, select, or boolean)
215215 * @param $raw_data array the user's validated feedback answers
216 -
 216+ *
217217 * This should:
218218 * 0. Attempt to insert a blank revision rollup row for each $data of type $type, based on revId, fieldId.
219219 * 1. Increment said revision rollup for each $data of type $type, based on revId, fieldId, and value
220220 * 2. Re-calculate the page value, across the last [X] revisions (an old revision, or more, may have moved outside of the wgArticleFeedbackv5RatingLifetime window, so we can't just increment the page level rollups - revision-level, absolutely)
221 -
 221+ *
222222 */
223223 private function updateRollup( $pageId, $revId, $type, $raw_data ) {
224224 # sanity check
225225 if ( $type != 'rating' && $type != 'option_id' && $type != 'boolean' ) {
226226 return 0;
227227 }
228 -
 228+
229229 // Strip out the data not of this type.
230230 foreach ( $raw_data as $row ) {
231231 if ( $row["aa_response_$type"] != null ) {
@@ -240,12 +240,12 @@
241241 * @param $revision int the revision id
242242 * @param $type string the type (rating, select, or boolean)
243243 * @param $row array a user's validated feedback answer
244 -
 244+ *
245245 * This should:
246246 * 0. Attempt to insert a blank revision rollup row, based on revId, fieldId.
247247 * 1. Increment said revision rollup, based on revId, fieldId, and value
248248 * 2. Re-calculate the page rolup value, across the last [X] revisions (an old revision, or more, may have moved outside of the wgArticleFeedbackv5RatingLifetime window, so we can't just increment the page level rollups - revision-level, absolutely)
249 -
 249+ *
250250 */
251251 private function updateRollupRow( $pageId, $revId, $type, $row ) {
252252 $dbr = wfGetDB( DB_SLAVE );
@@ -258,10 +258,10 @@
259259 // Selects are kind of a odd bird. We store one row
260260 // per option per field, and each one has the number
261261 // of times that option was chosen, and the number of
262 - // times the question was shown in total. So, you'd
 262+ // times the question was shown in total. So, you'd
263263 // have 1/10, 2/10, 7/10, eg. We increment the times
264264 // chosen on the one that was chosen, and the times
265 - // shown on all of them.
 265+ // shown on all of them.
266266
267267 // Fetch all the options for this field.
268268 $options = $dbr->select(
@@ -271,7 +271,7 @@
272272 __METHOD__
273273 );
274274
275 - // For each option this field has, make sure we have
 275+ // For each option this field has, make sure we have
276276 // a row by inserting one - will fail silently if the
277277 // row already exists.
278278 foreach( $options as $option ) {
@@ -353,7 +353,7 @@
354354 }
355355
356356 // Revision rollups being done, we update the page rollups.
357 - // These are built off of the revision rollups, and only
 357+ // These are built off of the revision rollups, and only
358358 // count revisions back to the user-specified limit, so
359359 // they need to be recalculated every time, since we don't
360360 // know what revision we're dealing with, or how many times
@@ -367,8 +367,8 @@
368368 $rows = $dbr->select(
369369 'aft_article_revision_feedback_select_rollup',
370370 array(
371 - 'arfsr_option_id',
372 - 'SUM(arfsr_total) AS total',
 371+ 'arfsr_option_id',
 372+ 'SUM(arfsr_total) AS total',
373373 'SUM(arfsr_count) AS count'
374374 ),
375375 array(
@@ -387,7 +387,7 @@
388388 'afsr_field_id' => $field,
389389 'afsr_option_id' => $row->arfsr_option_id,
390390 'afsr_total' => $row->total,
391 - 'afsr_count' => $row->count
 391+ 'afsr_count' => $row->count
392392 );
393393 }
394394 } else {
@@ -396,7 +396,7 @@
397397 $row = $dbr->selectRow(
398398 'aft_article_revision_feedback_ratings_rollup',
399399 array(
400 - 'SUM(afrr_total) AS total',
 400+ 'SUM(afrr_total) AS total',
401401 'SUM(afrr_count) AS count'
402402 ),
403403 array(
@@ -412,10 +412,10 @@
413413 'arr_page_id' => $pageId,
414414 'arr_field_id' => $field,
415415 'arr_total' => $row->total,
416 - 'arr_count' => $row->count
 416+ 'arr_count' => $row->count
417417 );
418418 }
419 -
 419+
420420 $dbw->begin();
421421 // Delete the existing page rollup rows.
422422 $dbw->delete( $table, array(
@@ -428,7 +428,7 @@
429429 $dbw->commit();
430430
431431 // One way to speed this up would be to purge old rows from
432 - // the revision_rollup tables, as soon as they're out of the
 432+ // the revision_rollup tables, as soon as they're out of the
433433 // window in which we count them. 30 revisions per page is still
434434 // a lot, but it'd be better than this, which has no limit and
435435 // will only get larger over time.
@@ -451,18 +451,23 @@
452452 $ip = null;
453453
454454 // Only save IP address if the user isn't logged in.
455 - if( !$wgUser->isLoggedIn() ) {
456 - $ip = wfGetIP();
 455+ if ( !$wgUser->isLoggedIn() ) {
 456+ $ip = wfGetIP();
457457 }
458458
459459 # make sure we have a page/user
460460 if ( !$params['pageid'] || !$wgUser) {
461 - return null;
 461+ if ( !$feedbackId ) {
 462+ $this->dieUsage( 'Saving feedback requires a page ID', 'invalidpageid' );
 463+ }
462464 }
463465
464466 # Fetch this if it wasn't passed in
465467 if ( !$revId ) {
466468 $title = Title::newFromID( $params['pageid'] );
 469+ if ( !$title ) {
 470+ $this->dieUsage( 'Page ID is invalid', 'invalidpageid' );
 471+ }
467472 $revId = $title->getLatestRevID();
468473 }
469474
@@ -520,7 +525,7 @@
521526 * Gets the anonymous token from the params
522527 *
523528 * @param $params array the params
524 - * @return string the token, or null if the user is not anonymous
 529+ * @return string the token, or null if the user is not anonymous
525530 */
526531 public function getAnonToken( $params ) {
527532 global $wgUser;
@@ -626,6 +631,7 @@
627632 array( 'missingparam', 'anontoken' ),
628633 array( 'code' => 'invalidtoken', 'info' => 'The anontoken is not 32 characters' ),
629634 array( 'code' => 'invalidpage', 'info' => 'ArticleFeedback is not enabled on this page' ),
 635+ array( 'code' => 'invalidpageid', 'info' => 'Page ID is missing or invalid' ),
630636 ) );
631637 }
632638

Follow-up revisions

RevisionCommit summaryAuthorDate
r106448Error messages now match; fixes r106018rsterbin17:42, 16 December 2011
r106450Broke out missing user into its own error; fixes r106018rsterbin17:47, 16 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r105252Tweaks and bug fixes for API uils class, as per Roan's review:...rsterbin23:56, 5 December 2011

Comments

#Comment by Catrope (talk | contribs)   22:05, 13 December 2011
+			if ( !$feedbackId ) {

It looks like $feedbackId is undefined at this point.

+				$this->dieUsage( 'Page ID is invalid', 'invalidpageid' );
[...]
+			array( 'code' => 'invalidpageid', 'info' => 'Page ID is missing or invalid' ),

These two should match, but they don't. I prefer the "missing or invalid" one.

Status & tagging log