r108556 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108555‎ | r108556 | r108557 >
Date:22:42, 10 January 2012
Author:bsitu
Status:resolved (Comments)
Tags:
Comment:
followup to -r108187 - Introducing new field mbf_latest_response for latest feedback response and feedback without responses
Modified paths:
  • /trunk/extensions/MoodBar/ApiQueryMoodBarComments.php (modified) (history)
  • /trunk/extensions/MoodBar/FeedbackItem.php (modified) (history)
  • /trunk/extensions/MoodBar/FeedbackResponseItem.php (modified) (history)
  • /trunk/extensions/MoodBar/MoodBar.hooks.php (modified) (history)
  • /trunk/extensions/MoodBar/SpecialFeedbackDashboard.php (modified) (history)
  • /trunk/extensions/MoodBar/sql/MoodBar.sql (modified) (history)
  • /trunk/extensions/MoodBar/sql/mbf_latest_response.sql (added) (history)
  • /trunk/extensions/MoodBar/updateMoodBarFeedback.php (added) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/updateMoodBarFeedback.php
@@ -0,0 +1,58 @@
 2+<?php
 3+/**
 4+ * Update moodbar_feedback.mbf_latest_response with the latest response id
 5+ *
 6+ * @ingroup Maintenance
 7+ */
 8+
 9+$IP = getenv( 'MW_INSTALL_PATH' );
 10+if ( $IP === false ) {
 11+ $IP = dirname( __FILE__ ) . '/../..';
 12+}
 13+require_once( "$IP/maintenance/Maintenance.php" );
 14+
 15+class UpdateMoodBarFeedback extends LoggedUpdateMaintenance {
 16+ public function __construct() {
 17+ parent::__construct();
 18+ $this->mDescription = "Update moodbar_feedback.mbf_latest_response with the corresponding latest moodbar_feedback_response.mbfr_id";
 19+ }
 20+
 21+ protected function getUpdateKey() {
 22+ return 'update moodbar_feedback.mbf_latest_response';
 23+ }
 24+
 25+ protected function updateSkippedMessage() {
 26+ return 'mbf_latest_response in moodbar_feedback table is already updated.';
 27+ }
 28+
 29+ protected function doDBUpdates() {
 30+ $db = wfGetDB( DB_MASTER );
 31+
 32+ $this->output( "Updating mbf_latest_response in moodbar_feedback table...\n" );
 33+
 34+ // Grab the feedback record with mbf_latest_response = 0
 35+ $res = $db->select( array( 'moodbar_feedback', 'moodbar_feedback_response' ),
 36+ array( 'MAX(mbfr_id) AS latest_mbfr_id', 'mbf_id' ),
 37+ array( 'mbf_id=mbfr_mbf_id', 'mbf_latest_response' => 0 ),
 38+ __METHOD__,
 39+ array( 'GROUP BY' => 'mbfr_mbf_id' )
 40+ );
 41+ $count = 0;
 42+ foreach ( $res as $row ) {
 43+ $count++;
 44+ if ( $count % 100 == 0 ) {
 45+ $this->output( $count . "\n" );
 46+ wfWaitForSlaves();
 47+ }
 48+ $db->update( 'moodbar_feedback',
 49+ array( 'mbf_latest_response' => intval( $row->latest_mbfr_id ) ),
 50+ array( 'mbf_id' => intval( $row->mbf_id ) ),
 51+ __METHOD__ );
 52+ }
 53+ $this->output( "Done, $count rows updated.\n" );
 54+ return true;
 55+ }
 56+}
 57+
 58+$maintClass = "UpdateMoodBarFeedback";
 59+require_once( RUN_MAINTENANCE_IF_MAIN );
Index: trunk/extensions/MoodBar/SpecialFeedbackDashboard.php
@@ -617,9 +617,7 @@
618618 break;
619619
620620 case 'showunanswered':
621 - $table[] = 'moodbar_feedback_response';
622 - $tableJoin['moodbar_feedback_response'] = array( 'LEFT JOIN', 'mbf_id=mbfr_mbf_id' );
623 - $conds['mbfr_id'] = null;
 621+ $conds['mbf_latest_response'] = 0;
624622 break;
625623 }
626624 }
@@ -630,7 +628,7 @@
631629 $res = $dbr->select( $table, array(
632630 'user_name', 'mbf_id', 'mbf_type',
633631 'mbf_timestamp', 'mbf_user_id', 'mbf_user_ip', 'mbf_comment',
634 - 'mbf_anonymous', 'mbf_hidden_state',
 632+ 'mbf_anonymous', 'mbf_hidden_state', 'mbf_latest_response'
635633 ),
636634 $conds,
637635 __METHOD__,
@@ -714,47 +712,32 @@
715713
716714 /**
717715 * Get the latest response summary for a set of feedback,
718 - * @param $res Iterator of Db row with index mbf_id for feedback
 716+ * @param $res Iterator of Db row with index mbf_latest_response for feedback
719717 * @return array
720718 */
721719 public static function getResponseSummary( $res ) {
722720 $dbr = wfGetDB( DB_SLAVE );
 721+
 722+ $mbfrIds = array();
723723
724 - $feedback = array();
725 -
726724 foreach ( $res as $row ) {
727 - $feedback[] = $row->mbf_id;
 725+ if ( $row->mbf_latest_response != 0 ) {
 726+ $mbfrIds[] = $row->mbf_latest_response;
 727+ }
728728 }
729729
730730 $response = array();
731731
732 - if ( count( $feedback ) > 0 ) {
733 - // query to get the latest mbfr_id for each mbfr_mbf_id
734 - $res = $dbr->select( array( 'moodbar_feedback_response' ),
735 - array( 'MAX(mbfr_id) AS latest_mbfr_id' ),
736 - array( 'mbfr_mbf_id' => $feedback, 'mbfr_user_id != 0' ),
737 - __METHOD__,
738 - array( 'GROUP BY' => "mbfr_mbf_id" )
 732+ if ( count( $mbfrIds ) > 0 ) {
 733+ $res = $dbr->select( array( 'moodbar_feedback_response', 'user' ),
 734+ array( 'mbfr_id', 'mbfr_mbf_id', 'mbfr_timestamp', 'user_id', 'user_name', 'user_real_name' ),
 735+ array( 'mbfr_id' => $mbfrIds, 'mbfr_user_id = user_id' ),
 736+ __METHOD__
739737 );
740738
741 - $mbfrId = array();
742 -
743739 foreach ( $res as $row ) {
744 - $mbfrId[] = $row->latest_mbfr_id;
 740+ $response[$row->mbfr_mbf_id] = $row;
745741 }
746 -
747 - // get the detail for each mbfr_id
748 - if ( count( $mbfrId ) > 0 ) {
749 - $res = $dbr->select( array( 'moodbar_feedback_response', 'user' ),
750 - array( 'mbfr_id', 'mbfr_mbf_id', 'mbfr_timestamp', 'user_id', 'user_name', 'user_real_name' ),
751 - array( 'mbfr_id' => $mbfrId, 'mbfr_user_id = user_id' ),
752 - __METHOD__
753 - );
754 -
755 - foreach ( $res as $row ) {
756 - $response[$row->mbfr_mbf_id] = $row;
757 - }
758 - }
759742 }
760743
761744 return $response;
Index: trunk/extensions/MoodBar/FeedbackItem.php
@@ -275,5 +275,24 @@
276276 public static function getValidTypes() {
277277 return self::$validTypes;
278278 }
 279+
 280+ /**
 281+ * Update feedback item based on the primary key $mbf_id
 282+ * @param $mbf_id int - id representing a unique feedback item
 283+ * @param $values array - key -> database field, value -> the new value
 284+ */
 285+ public static function update( $mbf_id, $values ) {
279286
 287+ if ( !$values ) {
 288+ return;
 289+ }
 290+
 291+ $dbw = wfGetDB( DB_MASTER );
 292+
 293+ $dbw->update( 'moodbar_feedback',
 294+ $values,
 295+ array( 'mbf_id' => intval( $mbf_id ) ),
 296+ __METHOD__ );
 297+ }
 298+
280299 }
Index: trunk/extensions/MoodBar/MoodBar.hooks.php
@@ -145,6 +145,8 @@
146146 $updater->addExtensionIndex( 'moodbar_feedback_response', 'mbfr_mbf_mbfr_id', "$dir/mbfr_mbf_mbfr_id_index.sql" );
147147 $updater->addExtensionField( 'moodbar_feedback_response', 'mbfr_enotif_sent', "$dir/mbfr_enotif_sent.sql" );
148148 $updater->addExtensionIndex( 'moodbar_feedback_response', 'mbfr_user_id', "$dir/mbfr_user_id_index.sql" );
 149+ $updater->addExtensionField( 'moodbar_feedback', 'mbf_latest_response', "$dir/mbf_latest_response.sql" );
 150+ $updater->addExtensionIndex( 'moodbar_feedback', 'mbf_latest_response', "$dir/mbf_latest_response.sql" );
149151
150152 return true;
151153 }
Index: trunk/extensions/MoodBar/sql/MoodBar.sql
@@ -15,6 +15,8 @@
1616 -- The feedback itself
1717 mbf_comment varchar(255) binary,
1818
 19+ -- Latest response id for this feedback
 20+ mbf_latest_response int unsigned NOT NULL,
1921 -- Options and context
2022 -- Whether or not the feedback item is hidden
2123 -- 0 = No; 255 = Yes (other values reserved for partial hiding)
@@ -35,3 +37,4 @@
3638 CREATE INDEX /*i*/mbf_userid_ip_timestamp_id ON /*_*/moodbar_feedback (mbf_user_id, mbf_user_ip, mbf_timestamp, mbf_id);
3739 CREATE INDEX /*i*/mbf_type_userid_ip_timestamp_id ON /*_*/moodbar_feedback (mbf_type, mbf_user_id, mbf_user_ip, mbf_timestamp, mbf_id);
3840 CREATE INDEX /*i*/mbf_timestamp_id ON /*_*/moodbar_feedback (mbf_timestamp, mbf_id);
 41+CREATE INDEX /*i*/mbf_latest_response ON /*_*/moodbar_feedback (mbf_latest_response);
Index: trunk/extensions/MoodBar/sql/mbf_latest_response.sql
@@ -0,0 +1,2 @@
 2+ALTER TABLE /*_*/moodbar_feedback ADD COLUMN mbf_latest_response int unsigned NOT NULL;
 3+CREATE INDEX /*i*/mbf_latest_response ON /*_*/moodbar_feedback (mbf_latest_response);
Index: trunk/extensions/MoodBar/ApiQueryMoodBarComments.php
@@ -13,7 +13,7 @@
1414 // Build the query
1515 $this->addJoinConds( array( 'user' => array( 'LEFT JOIN', 'user_id=mbf_user_id' ) ) );
1616 $this->addFields( array( 'user_name', 'mbf_id', 'mbf_type', 'mbf_timestamp', 'mbf_user_id', 'mbf_user_ip',
17 - 'mbf_comment', 'mbf_hidden_state' ) );
 17+ 'mbf_comment', 'mbf_hidden_state', 'mbf_latest_response' ) );
1818 if ( count( $params['type'] ) ) {
1919 $this->addWhereFld( 'mbf_type', $params['type'] );
2020 }
@@ -46,9 +46,7 @@
4747 $this->addOption( 'GROUP BY', 'mbf_id' );
4848 }
4949 } elseif ( $params['showunanswered'] ) {
50 - $this->addTables( array( 'moodbar_feedback_response' ) );
51 - $this->addJoinConds( array( 'moodbar_feedback_response' => array( 'LEFT JOIN', 'mbf_id=mbfr_mbf_id' ) ) );
52 - $this->addWhere( array( 'mbfr_id' => null ) );
 50+ $this->addWhere( array( 'mbf_latest_response' => 0 ) );
5351 }
5452
5553 $this->addTables( array( 'moodbar_feedback', 'user' ) );
Index: trunk/extensions/MoodBar/FeedbackResponseItem.php
@@ -239,6 +239,8 @@
240240 $row['mbfr_user_id'] = $user->getId();
241241 }
242242
 243+ $dbw->begin();
 244+
243245 if ( $this->getProperty('id') ) {
244246 $row['mbfr_id'] = $this->getProperty('id');
245247 $dbw->replace( 'moodbar_feedback_response', array('mbfr_id'), $row, __METHOD__ );
@@ -247,8 +249,15 @@
248250 $dbw->insert( 'moodbar_feedback_response', $row, __METHOD__ );
249251 $this->setProperty( 'id', $dbw->insertId() );
250252 }
 253+
 254+ $id = $this->getProperty('id');
251255
252 - return $this->getProperty('id');
 256+ // Update feedback with the latest response id
 257+ MBFeedbackItem::update( $this->getProperty('feedback'), array( 'mbf_latest_response' => $id ) );
 258+
 259+ $dbw->commit();
 260+
 261+ return $id;
253262 }
254263
255264 /**
@@ -293,7 +302,7 @@
294303
295304 $dbw->update( 'moodbar_feedback_response',
296305 $values,
297 - array( 'mbfr_id' => $mbfr_id ),
 306+ array( 'mbfr_id' => intval( $mbfr_id ) ),
298307 __METHOD__ );
299308 }
300309

Follow-up revisions

RevisionCommit summaryAuthorDate
r108561followup to -r108556 - make the maintenance script to process in batchsize at...bsitu23:31, 10 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r108187Adding "Show unanswered" filter to feedback dashboardbsitu00:05, 6 January 2012

Comments

#Comment by Catrope (talk | contribs)   23:09, 10 January 2012
+		$res = $db->select( array( 'moodbar_feedback', 'moodbar_feedback_response' ), 
+					array( 'MAX(mbfr_id) AS latest_mbfr_id', 'mbf_id' ),
+					array( 'mbf_id=mbfr_mbf_id', 'mbf_latest_response' => 0 ),
+					__METHOD__,
+					array( 'GROUP BY' => 'mbfr_mbf_id' )
+		);

Per IRC convo, this query needs to be batched rather than fetching all rows at once.

+					array( 'mbf_id=mbfr_mbf_id', 'mbf_latest_response' => 0 ),

You're assuming unfilled rows will have 0 here. I would feel safer if the column definition included an explicit DEFAULT 0 .

OK otherwise.

#Comment by Catrope (talk | contribs)   23:09, 10 January 2012
+		$res = $db->select( array( 'moodbar_feedback', 'moodbar_feedback_response' ), 
+					array( 'MAX(mbfr_id) AS latest_mbfr_id', 'mbf_id' ),
+					array( 'mbf_id=mbfr_mbf_id', 'mbf_latest_response' => 0 ),
+					__METHOD__,
+					array( 'GROUP BY' => 'mbfr_mbf_id' )
+		);

Per IRC convo, this query needs to be batched rather than fetching all rows at once.

+					array( 'mbf_id=mbfr_mbf_id', 'mbf_latest_response' => 0 ),

You're assuming unfilled rows will have 0 here. I would feel safer if the column definition included an explicit DEFAULT 0 .

OK otherwise.

Status & tagging log