r92682 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92681‎ | r92682 | r92683 >
Date:21:05, 20 July 2011
Author:werdna
Status:deferred
Tags:
Comment:
MoodBar:
* Refactoring of the display/export class formatting logic.
* User bucketing (based on a modulo of user ID). Requires EditPageTracking extension.
* Add type and own-talk parameters to output of formatter.
* Formatting for type and user type parameters.
* Allow only showing for users past a certain cutoff.
Modified paths:
  • /trunk/extensions/MoodBar/ApiMoodBar.php (modified) (history)
  • /trunk/extensions/MoodBar/MoodBar.hooks.php (modified) (history)
  • /trunk/extensions/MoodBar/MoodBar.i18n.php (modified) (history)
  • /trunk/extensions/MoodBar/MoodBar.php (modified) (history)
  • /trunk/extensions/MoodBar/SpecialMoodBar.php (modified) (history)
  • /trunk/extensions/MoodBar/sql/MoodBar.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/sql/MoodBar.sql
@@ -28,4 +28,3 @@
2929 -- A little overboard with the indexes perhaps, but we want to be able to dice this data a lot!
3030 CREATE INDEX /*i*/type_timestamp ON /*_*/moodbar_feedback (mbf_type,mbf_timestamp);
3131 CREATE INDEX /*i*/title_type ON /*_*/moodbar_feedback (mbf_namespace,mbf_title,mbf_type,mbf_timestamp);
32 -
Index: trunk/extensions/MoodBar/ApiMoodBar.php
@@ -88,6 +88,11 @@
8989 'useragent' => 'The User-Agent header of the browser',
9090 'system' => 'The operating system being used',
9191 'locale' => 'The locale in use',
 92+ 'token' => 'An edit token',
9293 );
9394 }
 95+
 96+ public function getDescription() {
 97+ return 'Allows users to submit feedback about their experiences on the site';
 98+ }
9499 }
Index: trunk/extensions/MoodBar/MoodBar.i18n.php
@@ -47,6 +47,16 @@
4848 'moodbar-header-useragent' => 'User-Agent',
4949 'moodbar-header-comment' => 'Comments',
5050 'moodbar-header-user-editcount' => 'User edit count',
 51+ 'moodbar-header-namespace' => 'Namespace',
 52+ 'moodbar-header-own-talk' => 'Own talk page',
 53+ // Mood types
 54+ 'moodbar-type-happy' => 'Happy',
 55+ 'moodbar-type-sad' => 'Sad',
 56+ 'moodbar-type-confused' => 'Confused',
 57+ // User types
 58+ 'moodbar-user-anonymized' => 'Anonymized',
 59+ 'moodbar-user-ip' => 'IP Address',
 60+ 'moodbar-user-user' => 'Registered user',
5161 );
5262
5363 /** Message documentation (Message documentation)
Index: trunk/extensions/MoodBar/MoodBar.php
@@ -85,3 +85,8 @@
8686 'jquery.client',
8787 ),
8888 );
 89+
 90+
 91+/** Configuration **/
 92+/** The registration time after which users will be shown the MoodBar **/
 93+$wgMoodBarCutoffTime = null;
Index: trunk/extensions/MoodBar/SpecialMoodBar.php
@@ -5,7 +5,9 @@
66 'id',
77 'timestamp',
88 'type',
 9+ 'namespace',
910 'page',
 11+ 'own-talk',
1012 'usertype',
1113 'user',
1214 'user-editcount',
@@ -84,31 +86,7 @@
8587 $outData = array();
8688
8789 foreach( $this->fields as $field ) {
88 - switch( $field ) {
89 - case 'usertype':
90 - $user = $data->getProperty('user');
91 - if ( $data->getProperty('anonymize') ) {
92 - $outData[] = 'anonymized';
93 - } else if ( $user->isAnon() ) {
94 - $outData[] = 'ip';
95 - } else {
96 - $outData[] = 'user';
97 - }
98 - break;
99 - case 'user':
100 - $user = $data->getProperty('user');
101 - if ( $data->getProperty('anonymize') ) {
102 - $outData[] = '';
103 - } else {
104 - $outData[] = $user->getName();
105 - }
106 - break;
107 - case 'page':
108 - $outData[] = $data->getProperty('page')->getPrefixedText();
109 - break;
110 - default:
111 - $outData[] = $data->getProperty($field);
112 - }
 90+ $outData[] = $this->getInternalRepresentation( $data, $field );
11391 }
11492
11593 fputcsv( $fh, $outData );
@@ -141,41 +119,7 @@
142120 $outData = null;
143121
144122 foreach( $this->fields as $field ) {
145 - switch( $field ) {
146 - case 'usertype':
147 - $user = $data->getProperty('user');
148 - if ( $data->getProperty('anonymize') ) {
149 - $outData = 'anonymized';
150 - } else if ( $user->isAnon() ) {
151 - $outData = 'ip';
152 - } else {
153 - $outData = 'user';
154 - }
155 - break;
156 - case 'user':
157 - $user = $data->getProperty('user');
158 - if ( $data->getProperty('anonymize') ) {
159 - $outData = '';
160 - } else {
161 - $outData = $user->getName();
162 - }
163 - break;
164 - case 'page':
165 - $title = $data->getProperty('page');
166 -
167 - global $wgUser;
168 - $linker = $wgUser->getSkin();
169 - $outData = $linker->link( $title );
170 - break;
171 - case 'timestamp':
172 - global $wgLang;
173 - $outData = $wgLang->timeanddate( $data->getProperty('timestamp') );
174 - break;
175 - default:
176 - $outData = $data->getProperty($field);
177 - break;
178 - }
179 -
 123+ $outData = $this->getHTMLRepresentation( $data, $field );
180124 $out .= Xml::tags( 'td', null, $outData );
181125 }
182126
@@ -183,5 +127,89 @@
184128
185129 return $out;
186130 }
 131+
 132+ /**
 133+ * Gets the viewer's representation of $field from $data.
 134+ * @param $data MBFeedbackItem to retrieve the data from.
 135+ * @param $field String name of the field to fill. Valid values in $this->fields
 136+ * @return String HTML for putting in the table.
 137+ */
 138+ public function getHTMLRepresentation( $data, $field ) {
 139+ switch( $field ) {
 140+ case 'page':
 141+ $title = $data->getProperty('page');
 142+
 143+ global $wgUser;
 144+ $linker = $wgUser->getSkin();
 145+ $outData = $linker->link( $title );
 146+ break;
 147+ case 'timestamp':
 148+ global $wgLang;
 149+ $outData = $wgLang->timeanddate( $data->getProperty('timestamp') );
 150+ $outData = htmlspecialchars($outData);
 151+ break;
 152+ case 'type':
 153+ $internal = $this->getInternalRepresentation( $data, $field );
 154+ $outData = wfMessage("moodbar-type-$internal")->parse();
 155+ break;
 156+ case 'usertype':
 157+ $internal = $this->getInternalRepresentation( $data, $field );
 158+ $outData = wfMessage("moodbar-user-$internal")->parse();
 159+ break;
 160+ default:
 161+ $outData = $this->getInternalRepresentation($data, $field);
 162+ $outData = htmlspecialchars( $outData );
 163+ break;
 164+ }
 165+
 166+ return $outData;
 167+ }
 168+
 169+ /**
 170+ * Gets an internal representation of $field from $data.
 171+ * @param $data MBFeedbackItem to retrieve the data from.
 172+ * @param $field String name of the field to fill. Valid values in $this->fields
 173+ * @return String value appropriate for putting in CSV
 174+ */
 175+ public function getInternalRepresentation( $data, $field ) {
 176+ $outData = null;
 177+ switch( $field ) {
 178+ case 'namespace':
 179+ $page = $data->getProperty('page');
 180+ $outData = $page->getNsText();
 181+ break;
 182+ case 'own-talk':
 183+ $page = $data->getProperty('page');
 184+ $user = $data->getProperty('user');
 185+ $userTalk = $user->getUserPage()->getTalkPage();
 186+ $outData = $page->equals( $userTalk );
 187+ break;
 188+ case 'usertype':
 189+ $user = $data->getProperty('user');
 190+ if ( $data->getProperty('anonymize') ) {
 191+ $outData = 'anonymized';
 192+ } else if ( $user->isAnon() ) {
 193+ $outData = 'ip';
 194+ } else {
 195+ $outData = 'user';
 196+ }
 197+ break;
 198+ case 'user':
 199+ $user = $data->getProperty('user');
 200+ if ( $data->getProperty('anonymize') ) {
 201+ $outData = '';
 202+ } else {
 203+ $outData = $user->getName();
 204+ }
 205+ break;
 206+ case 'page':
 207+ $outData = $data->getProperty('page')->getPrefixedText();
 208+ break;
 209+ default:
 210+ $outData = $data->getProperty($field);
 211+ }
 212+
 213+ return $outData;
 214+ }
187215 }
188216
Index: trunk/extensions/MoodBar/MoodBar.hooks.php
@@ -16,19 +16,44 @@
1717 * Determines whether or not we should show the MoodBar.
1818 */
1919 public static function shouldShowMoodbar( &$output, &$skin ) {
20 - if (
21 - // Front-end appends to header elements, which have different
22 - // locations and IDs in every skin.
23 - // Untill there is some kind of central registry of element-ids
24 - // that skins implement, or a fixed name for each of them, just
25 - // show it in Vector for now.
26 - $skin->getSkinName() == 'vector' &&
27 -
28 - // Only for logged-in users
29 - $output->getUser()->isLoggedIn()
30 - ) {
31 - return true;
 20+ // Front-end appends to header elements, which have different
 21+ // locations and IDs in every skin.
 22+ // Untill there is some kind of central registry of element-ids
 23+ // that skins implement, or a fixed name for each of them, just
 24+ // show it in Vector for now.
 25+ if ( $skin->getSkinName() !== 'vector' ) {
 26+ return false;
3227 }
 28+ $user = $output->getUser();
 29+
 30+ if ( $user->isAnon() ) {
 31+ return false;
 32+ }
 33+
 34+ // Only show MoodBar for users registered after a certain time
 35+ global $wgMoodBarCutoffTime;
 36+ if ( $wgMoodBarCutoffTime &&
 37+ $user->getRegistration() < $wgMoodBarCutoffTime )
 38+ {
 39+ return false;
 40+ }
 41+
 42+ $buckets = self::getUserBuckets( $user );
 43+
 44+ if ( in_array( 'moodbar-always', $buckets ) ) {
 45+ return true;
 46+ } elseif( in_array( 'no-moodbar', $buckets ) ) {
 47+ return false;
 48+ } elseif ( in_array( 'moodbar-on-submit', $buckets ) ) {
 49+ // MoodBar is shown when a user has submitted an edit
 50+ return ( $user->getEditCount() > 0 );
 51+ } elseif ( in_array( 'moodbar-on-edit', $buckets ) &&
 52+ class_exists('EditPageTracking') )
 53+ {
 54+ // MoodBar is shown when a user has previously loaded the edit form
 55+ return ((bool)EditPageTracking::getFirstEditPage($user));
 56+ }
 57+
3358 return false;
3459 }
3560
@@ -36,8 +61,10 @@
3762 * ResourceLoaderGetConfigVars hook
3863 */
3964 public static function resourceLoaderGetConfigVars( &$vars ) {
 65+ global $wgUser;
4066 $vars['mbConfig'] = array(
4167 'validTypes' => MBFeedbackItem::getValidTypes(),
 68+ 'userBuckets' => MoodBarHooks::getUserBuckets( $wgUser ),
4269 );
4370 return true;
4471 }
@@ -55,4 +82,29 @@
5683
5784 return true;
5885 }
 86+
 87+ /**
 88+ * Gets the MoodBar testing bucket that a user is in.
 89+ * @param $user The user to check
 90+ * @return array of bucket names
 91+ */
 92+ public static function getUserBuckets( $user ) {
 93+ $id = $user->getID();
 94+
 95+ // 60 is divisible by 2, 3, 4, 5, 6 and 10
 96+ $bucketId = $id % 60;
 97+ $buckets = array();
 98+
 99+ if ( $bucketId < 15 ) {
 100+ $buckets[] = 'no-moodbar';
 101+ } elseif ( $bucketId < 30 ) {
 102+ $buckets[] = 'moodbar-on-edit';
 103+ } elseif ( $bucketId < 45 ) {
 104+ $buckets[] = 'moodbar-on-submit';
 105+ } elseif ( $bucektId < 60 ) {
 106+ $buckets[] = 'moodbar-always';
 107+ }
 108+
 109+ return $buckets;
 110+ }
59111 }

Status & tagging log