r100269 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100268‎ | r100269 | r100270 >
Date:20:16, 19 October 2011
Author:ashley
Status:deferred (Comments)
Tags:socialprofile 
Comment:
SocialProfile: allow specifying and showing the user's year of birth on social profile pages. Patch by Rinat Silnov.

To use this feature, go to Special:UpdateProfile/preferences, toggle the new "Show year of birth" option, save preferences, then add your birthday on Special:UpdateProfile, save again and now your birthday will be shown on your profile.

Do note that because of the mw.loader.using() call in UserProfile/UpdateProfile.js, this breaks backwards compatibility with pre-ResourceLoader MediaWikis (that is, 1.16 and older). I don't know if this works with 1.17, but I tested this against the current 1.18 branch.
Modified paths:
  • /trunk/extensions/SocialProfile/UserProfile/SpecialEditProfile.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserProfile/SpecialUpdateProfile.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserProfile/UpdateProfile.js (modified) (history)
  • /trunk/extensions/SocialProfile/UserProfile/UserProfile.i18n.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserProfile/UserProfileClass.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/UserProfile/SpecialUpdateProfile.php
@@ -86,7 +86,7 @@
8787 $wgOut->addScriptFile( $wgUserProfileScripts . '/UpdateProfile.js' );
8888 }
8989
90 - if ( $wgRequest->wasPosted() ) {
 90+ if ( $wgRequest->wasPosted() ) {
9191 if ( !$section ) {
9292 $section = 'basic';
9393 }
@@ -194,6 +194,7 @@
195195 $notify_challenge = $wgRequest->getVal( 'notify_challenge' );
196196 $notify_honorifics = $wgRequest->getVal( 'notify_honorifics' );
197197 $notify_message = $wgRequest->getVal( 'notify_message' );
 198+ $show_year_of_birth = $wgRequest->getVal( 'show_year_of_birth', 0 );
198199 if ( $notify_friend == '' ) {
199200 $notify_friend = 0;
200201 }
@@ -214,6 +215,7 @@
215216 $wgUser->setOption( 'notifychallenge', $notify_challenge );
216217 $wgUser->setOption( 'notifyhonorifics', $notify_honorifics );
217218 $wgUser->setOption( 'notifymessage', $notify_message );
 219+ $wgUser->setOption( 'showyearofbirth', $show_year_of_birth );
218220 $wgUser->saveSettings();
219221
220222 // Allow extensions like UserMailingList do their magic here
@@ -222,8 +224,8 @@
223225
224226 function formatBirthdayDB( $birthday ) {
225227 $dob = explode( '/', $birthday );
226 - if ( count( $dob ) == 2 ) {
227 - $year = 2007;
 228+ if ( count( $dob ) == 2 || count( $dob ) == 3 ) {
 229+ $year = isset( $dob[2] ) ? $dob[2] : 2007;
228230 $month = $dob[0];
229231 $day = $dob[1];
230232 $birthday_date = $year . '-' . $month . '-' . $day;
@@ -233,13 +235,16 @@
234236 return ( $birthday_date );
235237 }
236238
237 - function formatBirthday( $birthday ) {
 239+ function formatBirthday( $birthday, $showYOB = false ) {
238240 $dob = explode( '-', $birthday );
239241 if ( count( $dob ) == 3 ) {
240 - $year = 0000;
241242 $month = $dob[1];
242243 $day = $dob[2];
243 - $birthday_date = $month . '/' . $day; // . '/' . $year;
 244+ $birthday_date = $month . '/' . $day;
 245+ if ( $showYOB ) {
 246+ $year = $dob[0];
 247+ $birthday_date .= '/' . $year;
 248+ }
244249 } else {
245250 $birthday_date = '';
246251 }
@@ -382,6 +387,7 @@
383388 __METHOD__
384389 );
385390
 391+ $showYOB = true;
386392 if ( $s !== false ) {
387393 $location_city = $s->up_location_city;
388394 $location_state = $s->up_location_state;
@@ -391,7 +397,8 @@
392398 $hometown_city = $s->up_hometown_city;
393399 $hometown_state = $s->up_hometown_state;
394400 $hometown_country = $s->up_hometown_country;
395 - $birthday = $this->formatBirthday( $s->up_birthday );
 401+ $showYOB = $wgUser->getIntOption( 'showyearofbirth', !isset( $s->up_birthday ) ) == 1;
 402+ $birthday = $this->formatBirthday( $s->up_birthday, $showYOB );
396403 $schools = $s->up_schools;
397404 $places = $s->up_places_lived;
398405 $websites = $s->up_websites;
@@ -498,8 +505,13 @@
499506
500507 $form .= '<div class="profile-update">
501508 <p class="profile-update-title">' . wfMsg( 'user-profile-personal-birthday' ) . '</p>
502 - <p class="profile-update-unit-left">' . wfMsg( 'user-profile-personal-birthdate' ) . '</p>
503 - <p class="profile-update-unit"><input type="text" size="25" name="birthday" id="birthday" value="' . ( isset( $birthday ) ? $birthday : '' ) . '" /></p>
 509+ <p class="profile-update-unit-left" id="birthday-format">' .
 510+ wfMsg( $showYOB ? 'user-profile-personal-birthdate-with-year' : 'user-profile-personal-birthdate' ) .
 511+ '</p>
 512+ <p class="profile-update-unit"><input type="text"' .
 513+ ( $showYOB ? ' class="long-birthday"' : null ) .
 514+ ' size="25" name="birthday" id="birthday" value="' .
 515+ ( isset( $birthday ) ? $birthday : '' ) . '" /></p>
504516 <div class="cleared"></div>
505517 </div><div class="cleared"></div>';
506518
@@ -662,8 +674,18 @@
663675 * @return HTML
664676 */
665677 function displayPreferencesForm() {
666 - global $wgUser, $wgOut;
 678+ global $wgRequest, $wgUser, $wgOut;
667679
 680+ $dbr = wfGetDB( DB_MASTER );
 681+ $s = $dbr->selectRow(
 682+ 'user_profile',
 683+ array( 'up_birthday' ),
 684+ array( 'up_user_id' => $wgUser->getID() ),
 685+ __METHOD__
 686+ );
 687+
 688+ $showYOB = isset( $s, $s->up_birthday ) ? false : true;
 689+
668690 // @todo If the checkboxes are in front of the option, this would look more like Special:Preferences
669691 $wgOut->setPageTitle( wfMsg( 'user-profile-section-preferences' ) );
670692 $form = UserProfile::getEditProfileNav( wfMsg( 'user-profile-section-preferences' ) );
@@ -689,6 +711,14 @@
690712 ' <input type="checkbox" size="25" name="notify_honorifics" id="notify_honorifics" value="1"' . ( ( $wgUser->getIntOption( 'notifyhonorifics', 1 ) == 1 ) ? 'checked' : '' ) . '/>
691713 </p>';
692714
 715+ $form .= '<p class="profile-update-title">' .
 716+ wfMsg( 'user-profile-preferences-miscellaneous' ) .
 717+ '</p>
 718+ <p class="profile-update-row">' .
 719+ wfMsg( 'user-profile-preferences-miscellaneous-show-year-of-birth' ) .
 720+ ' <input type="checkbox" size="25" name="show_year_of_birth" id="show_year_of_birth" value="1"' . ( ( $wgUser->getIntOption( 'showyearofbirth', $showYOB ) == 1 ) ? 'checked' : '' ) . '/>
 721+ </p>';
 722+
693723 // Allow extensions (like UserMailingList) to add new checkboxes
694724 wfRunHooks( 'SpecialUpdateProfile::displayPreferencesForm', array( $this, &$form ) );
695725
@@ -735,7 +765,7 @@
736766 $form = '<h1>' . wfMsg( 'user-profile-tidbits-title' ) . '</h1>';
737767 $form .= UserProfile::getEditProfileNav( wfMsg( 'user-profile-section-custom' ) );
738768 $form .= '<form action="" method="post" enctype="multipart/form-data" name="profile">
739 - <div class="profile-info clearfix">
 769+ <div class="profile-info clearfix">
740770 <div class="profile-update">
741771 <p class="profile-update-title">' . wfMsgForContent( 'user-profile-tidbits-title' ) . '</p>
742772 <div id="profile-update-custom1">
Index: trunk/extensions/SocialProfile/UserProfile/SpecialEditProfile.php
@@ -179,7 +179,7 @@
180180 $hometown_city = $s->up_hometown_city;
181181 $hometown_state = $s->up_hometown_state;
182182 $hometown_country = $s->up_hometown_country;
183 - $birthday = $this->formatBirthday( $s->up_birthday );
 183+ $birthday = $this->formatBirthday( $s->up_birthday, true );
184184 $schools = $s->up_schools;
185185 $places = $s->up_places_lived;
186186 $websites = $s->up_websites;
@@ -286,8 +286,8 @@
287287
288288 $form .= '<div class="profile-update">
289289 <p class="profile-update-title">' . wfMsg( 'user-profile-personal-birthday' ) . '</p>
290 - <p class="profile-update-unit-left">' . wfMsg( 'user-profile-personal-birthdate' ) . '</p>
291 - <p class="profile-update-unit"><input type="text" size="25" name="birthday" id="birthday" value="' . ( isset( $birthday ) ? $birthday : '' ) . '" /></p>
 290+ <p class="profile-update-unit-left">' . wfMsg( 'user-profile-personal-birthdate-with-year' ) . '</p>
 291+ <p class="profile-update-unit"><input type="text" class="long-birthday" size="25" name="birthday" id="birthday" value="' . ( isset( $birthday ) ? $birthday : '' ) . '" /></p>
292292 <div class="cleared"></div>
293293 </div><div class="cleared"></div>';
294294
Index: trunk/extensions/SocialProfile/UserProfile/UserProfile.i18n.php
@@ -117,6 +117,7 @@
118118 'user-profile-personal-hometown' => 'Hometown',
119119 'user-profile-personal-birthday' => 'Birthday',
120120 'user-profile-personal-birthdate' => 'Date (MM/DD)',
 121+ 'user-profile-personal-birthdate-with-year' => 'Date (MM/DD/YYYY)',
121122 'user-profile-personal-aboutme' => 'About me',
122123 'user-profile-personal-work' => 'Work',
123124 'user-profile-personal-occupation' => 'Occupation',
@@ -142,6 +143,8 @@
143144 'user-profile-preferences-emails-gift' => 'When you receive a gift',
144145 'user-profile-preferences-emails-level' => 'When advancing a level',
145146 'user-profile-preferences-emails-weekly' => 'Receive weekly email updates',
 147+ 'user-profile-preferences-miscellaneous' => 'Miscellaneous',
 148+ 'user-profile-preferences-miscellaneous-show-year-of-birth' => 'Show year of birth',
146149 'user-profile-update-button' => 'Update',
147150 'user-profile-tidbits-title' => 'Tidbits',
148151 'user-profile-tidbits-favmoment' => 'Favorite sports moment',
@@ -9284,6 +9287,7 @@
92859288 'user-profile-personal-hometown' => 'Родной город',
92869289 'user-profile-personal-birthday' => 'День рождения',
92879290 'user-profile-personal-birthdate' => 'Дата (ММ/ДД)',
 9291+ 'user-profile-personal-birthdate-with-year' => 'Дата (ММ/ДД/ГГГГ)',
92889292 'user-profile-personal-aboutme' => 'Обо мне',
92899293 'user-profile-personal-work' => 'Работа',
92909294 'user-profile-personal-occupation' => 'Занятие',
@@ -9309,6 +9313,8 @@
93109314 'user-profile-preferences-emails-gift' => 'Когда вы получаете подарок',
93119315 'user-profile-preferences-emails-level' => 'Когда повышается уровень',
93129316 'user-profile-preferences-emails-weekly' => 'Получать еженедельные обновления по почте',
 9317+ 'user-profile-preferences-miscellaneous' => 'Разное',
 9318+ 'user-profile-preferences-miscellaneous-show-year-of-birth' => 'Показывать год рождения',
93139319 'user-profile-update-button' => 'Обновить',
93149320 'user-profile-tidbits-title' => 'Интересные подробности',
93159321 'user-profile-tidbits-favmoment' => 'Любимые спортивный момент',
Index: trunk/extensions/SocialProfile/UserProfile/UserProfileClass.php
@@ -84,6 +84,9 @@
8585 public function getProfile() {
8686 global $wgMemc;
8787
 88+ $user = User::newFromId( $this->user_id );
 89+ $user->loadFromId();
 90+
8891 // Try cache first
8992 $key = wfMemcKey( 'user', 'profile', 'info', $this->user_id );
9093 $data = $wgMemc->get( $key );
@@ -107,13 +110,15 @@
108111 $profile['user_page_type'] = 1;
109112 $profile['user_id'] = 0;
110113 }
 114+ $showYOB = $user->getIntOption( 'showyearofbirth', !isset( $row->up_birthday ) ) == 1;
 115+ $issetUpBirthday = isset( $row->up_birthday ) ? $row->up_birthday : '';
111116 $profile['location_city'] = isset( $row->up_location_city ) ? $row->up_location_city : '';
112117 $profile['location_state'] = isset( $row->up_location_state ) ? $row->up_location_state : '';
113118 $profile['location_country'] = isset( $row->up_location_country ) ? $row->up_location_country : '';
114119 $profile['hometown_city'] = isset( $row->up_hometown_city ) ? $row->up_hometown_city : '';
115120 $profile['hometown_state'] = isset( $row->up_hometown_state ) ? $row->up_hometown_state : '';
116121 $profile['hometown_country'] = isset( $row->up_hometown_country ) ? $row->up_hometown_country : '';
117 - $profile['birthday'] = $this->formatBirthday( isset( $row->up_birthday ) ? $row->up_birthday : '' );
 122+ $profile['birthday'] = $this->formatBirthday( $issetUpBirthday, $showYOB);
118123
119124 $profile['about'] = isset( $row->up_about ) ? $row->up_about : '';
120125 $profile['places_lived'] = isset( $row->up_places_lived ) ? $row->up_places_lived : '';
@@ -138,8 +143,6 @@
139144 $wgMemc->set( $key, $profile );
140145 }
141146
142 - $user = User::newFromId( $this->user_id );
143 - $user->loadFromId();
144147 $profile['real_name'] = $user->getRealName();
145148 $profile['email'] = $user->getEmail();
146149
@@ -152,12 +155,16 @@
153156 * @param $birthday String: birthday in YYYY-MM-DD format
154157 * @return String: formatted birthday
155158 */
156 - function formatBirthday( $birthday ) {
 159+ function formatBirthday( $birthday, $showYear = true ) {
157160 $dob = explode( '-', $birthday );
158161 if ( count( $dob ) == 3 ) {
159162 $month = $dob[1];
160163 $day = $dob[2];
161 - return date( 'F jS', mktime( 0, 0, 0, $month, $day ) );
 164+ if ( !$showYear ) {
 165+ return date( 'F jS', mktime( 0, 0, 0, $month, $day ) );
 166+ }
 167+ $year = $dob[0];
 168+ return date( 'F jS, Y', mktime( 0, 0, 0, $month, $day, $year ) );
162169 //return $day . ' ' . $wgLang->getMonthNameGen( $month );
163170 }
164171 return $birthday;
Index: trunk/extensions/SocialProfile/UserProfile/UpdateProfile.js
@@ -40,4 +40,14 @@
4141 }
4242
4343 document.getElementById( id + '_form' ).innerHTML = section_select;
44 -}
\ No newline at end of file
 44+}
 45+
 46+mw.loader.using( 'jquery.ui.datepicker', function() {
 47+ jQuery( function( jQuery ) {
 48+ jQuery( '#birthday' ).datepicker({
 49+ changeYear: true,
 50+ yearRange: '1930:c',
 51+ dateFormat: jQuery( '#birthday' ).hasClass( 'long-birthday' ) ? 'mm/dd/yy' : 'mm/dd'
 52+ });
 53+ });
 54+});
\ No newline at end of file

Comments

#Comment by Siebrand (talk | contribs)   20:19, 19 October 2011

Please add message documentation for the newly added messages. Thanks.

#Comment by Siebrand (talk | contribs)   20:21, 19 October 2011
+	'user-profile-personal-birthdate-with-year' => 'Date (MM/DD/YYYY)',

Can this be changed based on the content language somehow? Month Day Year notation is very American, and confuses the heck out of most Europeans I know (including me).

#Comment by Jack Phoenix (talk | contribs)   19:39, 23 October 2011

I agree that it's not the best possible format for most of the world, but thankfully it isn't such a big issue, due to the jQuery datepicker. Maybe Rinat would be able to comment more on that, as this is his code.

Special:Code/MediaWiki/status states about the FIXME status: "Revision introduced a bug or is broken. It should be fixed or reverted." — this clearly is not the case with this revision, unless there's a regression or a bug that hasn't been reported yet. Message documentation or the lack of it is at most a to-do, not a FIXME. Resetting status back to new.

#Comment by Katkov Yury (talk | contribs)   19:46, 28 November 2011

The datepicker doesn't seem to be working on MW 1.17 . Are the any manuals of MW1.17.0-friendly use of ResourseLoader?

#Comment by Jack Phoenix (talk | contribs)   19:52, 28 November 2011

I'm not too worried about 1.17. While it's the latest stable for a while (until 1.18 is out), SocialProfile doesn't support it (see the infobox) and same goes for most/all of the other social tools; 1.17 is just too broken.

Status & tagging log