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 @@ |
618 | 618 | break; |
619 | 619 | |
620 | 620 | 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; |
624 | 622 | break; |
625 | 623 | } |
626 | 624 | } |
— | — | @@ -630,7 +628,7 @@ |
631 | 629 | $res = $dbr->select( $table, array( |
632 | 630 | 'user_name', 'mbf_id', 'mbf_type', |
633 | 631 | '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' |
635 | 633 | ), |
636 | 634 | $conds, |
637 | 635 | __METHOD__, |
— | — | @@ -714,47 +712,32 @@ |
715 | 713 | |
716 | 714 | /** |
717 | 715 | * 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 |
719 | 717 | * @return array |
720 | 718 | */ |
721 | 719 | public static function getResponseSummary( $res ) { |
722 | 720 | $dbr = wfGetDB( DB_SLAVE ); |
| 721 | + |
| 722 | + $mbfrIds = array(); |
723 | 723 | |
724 | | - $feedback = array(); |
725 | | - |
726 | 724 | foreach ( $res as $row ) { |
727 | | - $feedback[] = $row->mbf_id; |
| 725 | + if ( $row->mbf_latest_response != 0 ) { |
| 726 | + $mbfrIds[] = $row->mbf_latest_response; |
| 727 | + } |
728 | 728 | } |
729 | 729 | |
730 | 730 | $response = array(); |
731 | 731 | |
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__ |
739 | 737 | ); |
740 | 738 | |
741 | | - $mbfrId = array(); |
742 | | - |
743 | 739 | foreach ( $res as $row ) { |
744 | | - $mbfrId[] = $row->latest_mbfr_id; |
| 740 | + $response[$row->mbfr_mbf_id] = $row; |
745 | 741 | } |
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 | | - } |
759 | 742 | } |
760 | 743 | |
761 | 744 | return $response; |
Index: trunk/extensions/MoodBar/FeedbackItem.php |
— | — | @@ -275,5 +275,24 @@ |
276 | 276 | public static function getValidTypes() { |
277 | 277 | return self::$validTypes; |
278 | 278 | } |
| 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 ) { |
279 | 286 | |
| 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 | + |
280 | 299 | } |
Index: trunk/extensions/MoodBar/MoodBar.hooks.php |
— | — | @@ -145,6 +145,8 @@ |
146 | 146 | $updater->addExtensionIndex( 'moodbar_feedback_response', 'mbfr_mbf_mbfr_id', "$dir/mbfr_mbf_mbfr_id_index.sql" ); |
147 | 147 | $updater->addExtensionField( 'moodbar_feedback_response', 'mbfr_enotif_sent', "$dir/mbfr_enotif_sent.sql" ); |
148 | 148 | $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" ); |
149 | 151 | |
150 | 152 | return true; |
151 | 153 | } |
Index: trunk/extensions/MoodBar/sql/MoodBar.sql |
— | — | @@ -15,6 +15,8 @@ |
16 | 16 | -- The feedback itself |
17 | 17 | mbf_comment varchar(255) binary, |
18 | 18 | |
| 19 | + -- Latest response id for this feedback |
| 20 | + mbf_latest_response int unsigned NOT NULL, |
19 | 21 | -- Options and context |
20 | 22 | -- Whether or not the feedback item is hidden |
21 | 23 | -- 0 = No; 255 = Yes (other values reserved for partial hiding) |
— | — | @@ -35,3 +37,4 @@ |
36 | 38 | CREATE INDEX /*i*/mbf_userid_ip_timestamp_id ON /*_*/moodbar_feedback (mbf_user_id, mbf_user_ip, mbf_timestamp, mbf_id); |
37 | 39 | CREATE INDEX /*i*/mbf_type_userid_ip_timestamp_id ON /*_*/moodbar_feedback (mbf_type, mbf_user_id, mbf_user_ip, mbf_timestamp, mbf_id); |
38 | 40 | 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 @@ |
14 | 14 | // Build the query |
15 | 15 | $this->addJoinConds( array( 'user' => array( 'LEFT JOIN', 'user_id=mbf_user_id' ) ) ); |
16 | 16 | $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' ) ); |
18 | 18 | if ( count( $params['type'] ) ) { |
19 | 19 | $this->addWhereFld( 'mbf_type', $params['type'] ); |
20 | 20 | } |
— | — | @@ -46,9 +46,7 @@ |
47 | 47 | $this->addOption( 'GROUP BY', 'mbf_id' ); |
48 | 48 | } |
49 | 49 | } 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 ) ); |
53 | 51 | } |
54 | 52 | |
55 | 53 | $this->addTables( array( 'moodbar_feedback', 'user' ) ); |
Index: trunk/extensions/MoodBar/FeedbackResponseItem.php |
— | — | @@ -239,6 +239,8 @@ |
240 | 240 | $row['mbfr_user_id'] = $user->getId(); |
241 | 241 | } |
242 | 242 | |
| 243 | + $dbw->begin(); |
| 244 | + |
243 | 245 | if ( $this->getProperty('id') ) { |
244 | 246 | $row['mbfr_id'] = $this->getProperty('id'); |
245 | 247 | $dbw->replace( 'moodbar_feedback_response', array('mbfr_id'), $row, __METHOD__ ); |
— | — | @@ -247,8 +249,15 @@ |
248 | 250 | $dbw->insert( 'moodbar_feedback_response', $row, __METHOD__ ); |
249 | 251 | $this->setProperty( 'id', $dbw->insertId() ); |
250 | 252 | } |
| 253 | + |
| 254 | + $id = $this->getProperty('id'); |
251 | 255 | |
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; |
253 | 262 | } |
254 | 263 | |
255 | 264 | /** |
— | — | @@ -293,7 +302,7 @@ |
294 | 303 | |
295 | 304 | $dbw->update( 'moodbar_feedback_response', |
296 | 305 | $values, |
297 | | - array( 'mbfr_id' => $mbfr_id ), |
| 306 | + array( 'mbfr_id' => intval( $mbfr_id ) ), |
298 | 307 | __METHOD__ ); |
299 | 308 | } |
300 | 309 | |