r104644 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104643‎ | r104644 | r104645 >
Date:01:42, 30 November 2011
Author:bsitu
Status:resolved (Comments)
Tags:
Comment:
followup to r104642 - Moodbar Phase 2 server side change
Modified paths:
  • /trunk/extensions/MoodBar/ApiFeedbackDashboardResponse.php (modified) (history)
  • /trunk/extensions/MoodBar/ApiQueryMoodBarComments.php (modified) (history)
  • /trunk/extensions/MoodBar/MoodBar.i18n.php (modified) (history)
  • /trunk/extensions/MoodBar/SpecialFeedbackDashboard.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/SpecialFeedbackDashboard.php
@@ -128,9 +128,10 @@
129129 * @param $params An array of flags. Valid flags:
130130 * * admin (user can show/hide feedback items)
131131 * * show-anyway (user has asked to see this hidden item)
 132+ * @param $response An array of response for feedback
132133 * @return string HTML
133134 */
134 - public static function formatListItem( $row, $params = array() ) {
 135+ public static function formatListItem( $row, $params = array(), $response = array() ) {
135136 global $wgLang, $wgUser;
136137
137138 $classes = array('fbd-item');
@@ -142,6 +143,7 @@
143144 $feedbackItem = MBFeedbackItem::load( $row );
144145 }
145146 catch (Exception $e) {
 147+ $classes = Sanitizer::encodeAttribute( implode(' ', $classes) );
146148 $error_message = wfMessage('moodbar-feedback-load-record-error')->escaped();
147149 return <<<HTML
148150 <li class="$classes">
@@ -200,22 +202,10 @@
201203
202204 }
203205
204 - //only show response elements if feedback is not hidden, and user is logged in
205 - if ($feedbackItem->getProperty('hidden-state') == false
206 - && !$wgUser->isAnon() ) {
207 - $respondToThis = "<span>".wfMessage('moodbar-respond-collapsed')->escaped().'</span> '.wfMessage("moodbar-respond-text")->escaped();
208 - $responseElements = <<<HTML
209 - <div class="fbd-item-response">
210 - <a class="fbd-respond-link">$respondToThis</a>
211 - </div>
212 -HTML;
213 - }
 206+ $responseElements = self::buildResponseElement( $feedbackItem, $response );
214207
215208 $classes = Sanitizer::encodeAttribute( implode(' ', $classes) );
216209 $toolLinks = implode("\n", $toolLinks );
217 - if (!isset($responseElements)) {
218 - $responseElements = "";
219 - }
220210
221211 return <<<HTML
222212 <li class="$classes" data-mbccontinue="$continueData">
@@ -232,6 +222,63 @@
233223 HTML;
234224 }
235225
 226+ protected static function buildResponseElement( $feedbackItem, $response ) {
 227+ global $wgLang, $wgUser;
 228+
 229+ $responseElements = '';
 230+
 231+ $id = $feedbackItem->getProperty('id');
 232+
 233+ $showResponseBox = true;
 234+
 235+ //Do not show response box if there is a response already
 236+ if ( isset( $response[$id] ) ) {
 237+ //for now we only display the latest response
 238+ foreach ( $response[$id] AS $response_detail ) {
 239+ if ( $responder = User::newFromId( $response_detail->mbfr_user_id ) ) {
 240+
 241+ $now = wfTimestamp( TS_UNIX );
 242+ $responsetimestamp = wfTimestamp( TS_UNIX, $response_detail->mbfr_timestamp );
 243+
 244+ $responsetime = $wgLang->formatTimePeriod( $now - $responsetimestamp,
 245+ array( 'avoid' => 'avoidminutes', 'noabbrevs' => true )
 246+ );
 247+
 248+ $permalinkTitle = $feedbackItem->getProperty('user')->getTalkPage()->getFullText();
 249+
 250+ $individual_response = wfMsgExt('moodbar-feedback-response-summary', array('parse'),
 251+ $responder->getUserPage()->getFullText(),
 252+ htmlspecialchars($responder->getName()),
 253+ $permalinkTitle . '#feedback-dashboard-response-' . $response_detail->mbfr_id,
 254+ $responsetime);
 255+ $showResponseBox = false;
 256+
 257+ $responseElements = <<<HTML
 258+ <div class="fbd-item-response">
 259+ $individual_response
 260+ </div>
 261+HTML;
 262+ break;
 263+
 264+ }
 265+ }
 266+
 267+ }
 268+ //only show response elements if feedback is not hidden, and user is logged in
 269+ else if ( $showResponseBox && $feedbackItem->getProperty('hidden-state') == false
 270+ && !$wgUser->isAnon() ) {
 271+ $respondToThis = "<span>".wfMessage('moodbar-respond-collapsed')->escaped().'</span> '.wfMessage("moodbar-respond-text")->escaped();
 272+ $responseElements = <<<HTML
 273+ <div class="fbd-item-response">
 274+ <a class="fbd-respond-link">$respondToThis</a>
 275+ </div>
 276+HTML;
 277+ }
 278+
 279+ return $responseElements;
 280+
 281+ }
 282+
236283 /**
237284 * Build the "user information" part of an item on the feedback dashboard.
238285 * @param $feedbackItem MBFeedbackItem representing the feedback to show
@@ -350,8 +397,10 @@
351398 }
352399 }
353400
 401+ $response = self::getResponseSummary( $res['rows'] );
 402+
354403 foreach ( $res['rows'] as $row ) {
355 - $list .= self::formatListItem( $row, $params );
 404+ $list .= self::formatListItem( $row, $params, $response );
356405 }
357406
358407 if ( $list === '' ) {
@@ -555,4 +604,37 @@
556605 );
557606 }
558607
 608+ /**
 609+ * Get the response summary for a set of feedback
 610+ * @param $res Iterator of Db row with index mbf_id for feedback
 611+ * @return array
 612+ */
 613+ public static function getResponseSummary( $res ) {
 614+ $dbr = wfGetDB( DB_SLAVE );
 615+
 616+ $feedback = array();
 617+
 618+ foreach ( $res as $row ) {
 619+ $feedback[] = $row->mbf_id;
 620+ }
 621+
 622+ $response = array();
 623+
 624+ if ( $feedback = implode( ',', $feedback ) ) {
 625+ $res = $dbr->select( array( 'moodbar_feedback_response' ),
 626+ array( 'mbfr_id', 'mbfr_mbf_id', 'mbfr_user_id', 'mbfr_timestamp' ),
 627+ array( 'mbfr_mbf_id IN (' . $feedback . ') AND mbfr_user_id != 0' ),
 628+ __METHOD__,
 629+ array( 'ORDER BY' => "mbfr_timestamp DESC, mbfr_id DESC" )
 630+ );
 631+
 632+ foreach ( $res AS $row ) {
 633+ $response[$row->mbfr_mbf_id][] = $row;
 634+ }
 635+ }
 636+
 637+
 638+ return $response;
 639+ }
 640+
559641 }
Index: trunk/extensions/MoodBar/MoodBar.i18n.php
@@ -80,7 +80,7 @@
8181 'moodbar-header-own-talk' => 'Own talk page',
8282 // Special:MoodBarFeedback
8383 'moodbar-feedback-title' => 'Feedback dashboard',
84 - 'moodbar-feedback-response-title' => 'Feedback dashboard response',
 84+ 'moodbar-feedback-response-title' => '==In response to your [[$1|feedback]]==',
8585 'moodbar-feedback-view-link' => '(View the feedback)',
8686 'moodbar-feedback-filters' => 'Filters',
8787 'moodbar-feedback-filters-type' => 'Mood:',
@@ -116,6 +116,7 @@
117117 'moodbar-restore-intro' => '',
118118 'moodbar-invalid-item' => 'The system was unable to find the correct feedback item.',
119119 'moodbar-feedback-action-error' => 'An error occurred when trying to perform this action.',
 120+ 'moodbar-feedback-response-summary' => '[[$1|$2]] [[$3|responded]] this comment $4 ago',
120121 // Mood types
121122 'moodbar-type-happy' => '{{GENDER:$1|Happy}}',
122123 'moodbar-type-sad' => '{{GENDER:$1|Sad}}',
@@ -225,7 +226,7 @@
226227 'moodbar-header-user' => '{{Identical|User}}',
227228 'moodbar-header-comment' => '{{Identical|Comment}}',
228229 'moodbar-header-namespace' => '{{Identical|Namespace}}',
229 - 'moodbar-feedback-response-title' => 'The title for appending feedback response text to a user talk page',
 230+ 'moodbar-feedback-response-title' => 'The title for appending feedback response text to a user talk page, $1 is the dashboard page',
230231 'moodbar-feedback-view-link' => 'link to an individual feedback',
231232 'moodbar-feedback-filters' => '{{Identical|Filter}}',
232233 'moodbar-feedback-filters-type' => '{{Identical|Mood}}',
@@ -248,6 +249,7 @@
249250 'moodbar-hidden-footer-without-log' => '* $1 is a link to restore the item displaying {{msg-mw|moodbar-feedback-restore}}',
250251 'moodbar-action-reason' => 'Text for Admin action reason',
251252 'moodbar-action-reason-required' => 'Text explaining admin action reason is required',
 253+ 'moodbar-feedback-response-summary' => 'Text providing a summary of a user response, $1 is user page, $2 is user name, $3 is user talk page, $4 is time',
252254 'moodbar-type-happy' => '$1 is the username that can be used for GENDER',
253255 'moodbar-type-sad' => '$1 is the username that can be used for GENDER',
254256 'moodbar-type-confused' => '$1 is the username that can be used for GENDER',
Index: trunk/extensions/MoodBar/ApiFeedbackDashboardResponse.php
@@ -3,7 +3,7 @@
44 class ApiFeedbackDashboardResponse extends ApiBase {
55
66 public function execute() {
7 - global $wgRequest, $wgUser, $wgContLang;
 7+ global $wgRequest, $wgUser, $wgContLang, $wgParser;
88
99 if ( $wgUser->isAnon() ) {
1010 $this->dieUsage( "You don't have permission to do that", 'permission-denied' );
@@ -28,16 +28,17 @@
2929
3030 if ( $commenter !== null && $commenter->isAnon() == false ) {
3131 $talkPage = $commenter->getTalkPage();
 32+
 33+ $feedback_link = wfMessage('moodbar-feedback-response-title')->rawParams($wgContLang->getNsText( NS_SPECIAL ) .
 34+ ':FeedbackDashboard/' . $item->getProperty('feedback'))->escaped();
3235
3336 $api = new ApiMain( new FauxRequest( array(
3437 'action' => 'edit',
3538 'title' => $talkPage->getFullText(),
3639 'appendtext' => ( $talkPage->exists() ? "\n\n" : '' ) .
37 - '==' . wfMessage('moodbar-feedback-response-title')->escaped() . '==' . "\n\n" .
38 - '[[' . $wgContLang->getNsText( NS_SPECIAL ) . ':FeedbackDashboard/' . $item->getProperty('feedback') . '|' .
39 - wfMessage('moodbar-feedback-view-link')->escaped() . ']]' . "\n\n".
40 - '[['. $wgUser->getTalkPage()->getFullText() . '|' . $wgUser->getName() . ']] ' .
41 - '<nowiki>' . $params['response'] . '</nowiki>',
 40+ $feedback_link . "\n" .
 41+ '<span id="feedback-dashboard-response-' . $item->getProperty('id') . '"></span>' . "\n\n" .
 42+ $wgParser->cleanSigInSig($params['response']) . "\n\n~~~~",
4243 'token' => $params['token'],
4344 'summary' => '',
4445 'notminor' => true,
Index: trunk/extensions/MoodBar/ApiQueryMoodBarComments.php
@@ -45,6 +45,9 @@
4646 $res = $this->select( __METHOD__ );
4747 $result = $this->getResult();
4848 $count = 0;
 49+
 50+ $response = SpecialFeedbackDashboard::getResponseSummary( $res );
 51+
4952 foreach ( $res as $row ) {
5053 if ( ++$count > $params['limit'] ) {
5154 // We've reached the one extra which shows that there are additional rows. Stop here
@@ -52,7 +55,7 @@
5356 break;
5457 }
5558
56 - $vals = $this->extractRowInfo( $row, $prop );
 59+ $vals = $this->extractRowInfo( $row, $prop, $response );
5760 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
5861 if ( !$fit ) {
5962 $this->setContinueEnumParameter( 'continue', $this->getContinue( $row ) );
@@ -85,7 +88,7 @@
8689 );
8790 }
8891
89 - protected function extractRowInfo( $row, $prop ) {
 92+ protected function extractRowInfo( $row, $prop, $response = array() ) {
9093 global $wgUser;
9194
9295 $r = array();
@@ -114,7 +117,7 @@
115118 $params[] = 'show-anyway';
116119 }
117120
118 - $r['formatted'] = SpecialFeedbackDashboard::formatListItem( $row, $params );
 121+ $r['formatted'] = SpecialFeedbackDashboard::formatListItem( $row, $params, $response );
119122 }
120123
121124 if ( $isHidden && !$showHidden ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r104705followup to r104644 - update the language filebsitu17:20, 30 November 2011
r104757followup to r104644 - change for code reviewbsitu21:47, 30 November 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r104642added preview mode for responses, allow wikitext, add response icons / langua...rmoen01:37, 30 November 2011

Comments

#Comment by Catrope (talk | contribs)   21:10, 30 November 2011
+				if ( $responder =  User::newFromId( $response_detail->mbfr_user_id ) ) {
[...]
+		if ( $feedback = implode( ',', $feedback ) ) {

Don't use assignments in if conditions.

+					$individual_response = wfMsgExt('moodbar-feedback-response-summary', array('parse'),  
+						                         $responder->getUserPage()->getFullText(), 
+						                         htmlspecialchars($responder->getName()), 
+						                         $permalinkTitle . '#feedback-dashboard-response-' . $response_detail->mbfr_id,  
+						                         $responsetime);

You shouldn't be passing an escaped responder name into wfMsgExt in parse mode. The parser will take care of escaping. Also, it would be nice if new code used wfMessage().

+					     array( 'mbfr_mbf_id IN (' . $feedback . ') AND mbfr_user_id != 0' ),

We have a quite rich database API. You should be using something like array( 'mbfr_mbf_id' => $feedback, 'mbfr_user_id != 0' ) where $feedback is an array of IDs.

+					     array( 'ORDER BY' => "mbfr_timestamp DESC, mbfr_id DESC" )

This ordering is not indexed. You should

  1. change this to ORDER BY mbfr_mbf_id DESC, mbfr_timestamp DESC, mbfr_id DESC
  2. add an index on (mbfr_mbf_id, mbfr_timestamp, mbfr_id). You can extend the existing index for this
+			$feedback_link = wfMessage('moodbar-feedback-response-title')->rawParams($wgContLang->getNsText( NS_SPECIAL ) . 
+				         ':FeedbackDashboard/' . $item->getProperty('feedback'))->escaped();

You should use params() here instead of rawParams(). The arguments to rawParams() are potentially interpreted as HTML (because they bypass escaping), and what you're passing in is not HTML so it should be escaped.

Status & tagging log