r87811 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87810‎ | r87811 | r87812 >
Date:08:56, 10 May 2011
Author:faurethomas
Status:deferred (Comments)
Tags:
Comment:
Add a function to convert a tweet timestamp in a countdown. Include the functions with the AutoLoader.
Modified paths:
  • /trunk/extensions/WikiTweet/WikiTweet.api.php (modified) (history)
  • /trunk/extensions/WikiTweet/WikiTweet.functions.php (modified) (history)
  • /trunk/extensions/WikiTweet/WikiTweet.i18n.php (modified) (history)
  • /trunk/extensions/WikiTweet/WikiTweet.php (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiTweet/WikiTweet.i18n.php
@@ -30,6 +30,15 @@
3131 'wikitweet-tweets-tagged' => 'Tweets tagged',
3232 'wikitweet-back-timeline' => 'Back to the timeline',
3333 'wikitweet-tweets-from-room' => 'Tweets from room',
 34+ 'wikitweet-timeago' => '%time% ago', // Do not translate the string "%time%", it is a keyword
 35+ 'wikitweet-hour' => 'hour',
 36+ 'wikitweet-hours' => 'hours',
 37+ 'wikitweet-minute' => 'minute',
 38+ 'wikitweet-minutes' => 'minutes',
 39+ 'wikitweet-second' => 'second',
 40+ 'wikitweet-seconds' => 'seconds',
 41+ 'wikitweet-inthefuture' => 'In the future !!',
 42+ 'wikitweet-fewsecondsago' => 'Few seconds ago'
3443 );
3544
3645 /** Message documentation (Message documentation)
@@ -92,6 +101,15 @@
93102 'wikitweet-tweets-tagged' => 'Tweets taggés',
94103 'wikitweet-back-timeline' => 'Retour au flux',
95104 'wikitweet-tweets-from-room' => 'Tweets de la room',
 105+ 'wikitweet-timeago' => 'Il y a %time%',
 106+ 'wikitweet-hour' => 'heure',
 107+ 'wikitweet-hours' => 'heures',
 108+ 'wikitweet-minute' => 'minute',
 109+ 'wikitweet-minutes' => 'minutes',
 110+ 'wikitweet-second' => 'seconde',
 111+ 'wikitweet-seconds' => 'secondes',
 112+ 'wikitweet-inthefuture' => 'Dans le futur !!',
 113+ 'wikitweet-fewsecondsago' => 'Il y a quelques secondes'
96114 );
97115
98116 /** Colognian (Ripoarisch)
Index: trunk/extensions/WikiTweet/WikiTweet.php
@@ -45,6 +45,8 @@
4646 $wgAutoloadClasses['ApiQueryWikiTweet'] = "$dir/WikiTweet.api.php";
4747 $wgAPIListModules['wikitweet'] = 'ApiQueryWikiTweet';
4848
 49+$wgAutoloadClasses['WikiTweetFunctions'] = "$dir/WikiTweet.functions.php";
 50+
4951 function wikiTweeter()
5052 {
5153 global $wgExtensionMessagesFiles, $wgParser, $wgMessageCache;
Index: trunk/extensions/WikiTweet/WikiTweet.functions.php
@@ -1,5 +1,8 @@
22 <?php
3 -function sendWithPear($mailer, $dest, $headers, $body)
 3+if ( !defined( 'MEDIAWIKI' ) )
 4+ die();
 5+class WikiTweetFunctions {
 6+ public static function sendWithPear($mailer, $dest, $headers, $body)
47 {
58 $mailResult = $mailer->send($dest, $headers, $body);
69 if( PEAR::isError( $mailResult ) ) {
@@ -8,11 +11,12 @@
912 return true;
1013 }
1114 }
12 -function send( $to, $from, $subject, $body, $replyto=null ) {
 15+ public static function send( $to, $from, $subject, $body, $replyto=null )
 16+ {
1317 $wgOutputEncoding = 'UTF-8';
1418 $wgEnotifImpersonal = false;
1519 $wgErrorString = '';
16 - $wgEnotifMaxRecips=500;
 20+ $wgEnotifMaxRecips = 500;
1721 include('WikiTweet.config.php');
1822 if (is_array( $wgWikiTweet )) {
1923 require_once( 'Mail.php' );
@@ -48,10 +52,85 @@
4953 }
5054 $chunks = array_chunk( (array)$dest, $wgEnotifMaxRecips );
5155 foreach ($chunks as $chunk) {
52 - $e = sendWithPear($mail_object, $chunk, $headers, $body);
 56+ $e = WikiTweetFunctions::sendWithPear($mail_object, $chunk, $headers, $body);
5357 if( $e != true)
5458 return $e;
5559 }
5660 }
5761 }
58 -?>
\ No newline at end of file
 62+
 63+ public static function str_global_replace( $i__string, $i__array )
 64+ {
 65+ $o__string = $i__string ;
 66+ foreach($i__array as $l__key => $l__item)
 67+ {
 68+ $o__string = str_replace($l__key, $l__value, $o__string );
 69+ }
 70+ return $o__string;
 71+ }
 72+ public static function Convert_Date ( $i__old_date )
 73+ {
 74+ // function to convert a date in a countdown
 75+ $l__nber_seconds = 0 ;
 76+ $l__nber_minutes = 0 ;
 77+ $l__nber_hours = 0 ;
 78+ $l__diff_date = 0 ;
 79+
 80+
 81+ $l__diff_date = time() - $i__old_date ;
 82+ if ( $l__diff_date < 0 )
 83+ {
 84+ // theorically impossible
 85+ $result = wfMsg ( 'wikitweet-inthefuture' ) ;
 86+ }
 87+ elseif ( $l__diff_date < 10 )
 88+ {
 89+ // less than 10 seconds is "few"
 90+ $result = wfMsg ( 'wikitweet-fewsecondsago' ) ;
 91+ }
 92+ elseif ( $l__diff_date >= 60 )
 93+ {
 94+ // real conversion
 95+ $l__nber_seconds = $l__diff_date % 60 ;
 96+ $l__new_diff = $l__diff_date - $l__nber_seconds ;
 97+ $l__nber_minutes = $l__new_diff / 60 ;
 98+ if ($l__nber_minutes >= 60)
 99+ {
 100+ $l__old_nber_minutes = $l__nber_minutes ;
 101+ $l__nber_minutes = $l__nber_minutes % 60 ;
 102+ $l__new_nber_minutes = $l__old_nber_minutes - $l__nber_minutes ;
 103+ $l__nber_hours = $l__new_nber_minutes / 60 ;
 104+ }
 105+ }
 106+ else
 107+ {
 108+ $l__nber_seconds = $l__diff_date ;
 109+ }
 110+ //plurals
 111+ $seconds = ( $l__nber_seconds == 1 ) ? wfMsg( 'wikitweet-second' ) : wfMsg( 'wikitweet-seconds' ) ;
 112+ $minutes = ( $l__nber_minutes == 1 ) ? wfMsg( 'wikitweet-minute' ) : wfMsg( 'wikitweet-minutes' ) ;
 113+ $hours = ( $l__nber_hours == 1 ) ? wfMsg( 'wikitweet-hour' ) : wfMsg( 'wikitweet-hours' ) ;
 114+
 115+ if ($l__nber_hours !=0 && $l__nber_minutes != 0 )
 116+ {
 117+ $result = str_replace( "%time%" , "$l__nber_hours $hours $l__nber_minutes $minutes" , wfMsg( 'wikitweet-timeago' ) ); // "%time% ago"
 118+ }
 119+ elseif (($l__nber_hours != 0 && $l__nber_minutes == 0 ) || ($l__nber_hours >= 5 ) )
 120+ {
 121+ $result = str_replace( "%time%" , "$l__nber_hours $hours" , wfMsg( 'wikitweet-timeago' ) ); // "%time% ago"
 122+ }
 123+ elseif (($l__nber_minutes >= 5) || ($l__nber_minutes != 0 && $l__nber_seconds == 0 ) )
 124+ {
 125+ $result = str_replace( "%time%" , "$l__nber_minutes $minutes" , wfMsg( 'wikitweet-timeago' ) ); // "%time% ago"
 126+ }
 127+ elseif ($l__nber_minutes != 0 && $l__nber_seconds != 0 )
 128+ {
 129+ $result = str_replace( "%time%" , "$l__nber_minutes $minutes $l__nber_seconds $seconds" , wfMsg( 'wikitweet-timeago' ) ); // "%time% ago"
 130+ }
 131+ elseif ($l__nber_seconds !=0 )
 132+ {
 133+ $result = str_replace( "%time%" , "$l__nber_seconds $seconds" , wfMsg( 'wikitweet-timeago' ) ); // "%time% ago"
 134+ }
 135+ return $result ; // a string
 136+ }
 137+}
Index: trunk/extensions/WikiTweet/WikiTweet.api.php
@@ -168,8 +168,8 @@
169169 }
170170
171171 $dateSrc = $date.' GMT';
172 - // TODO : "x hours ago, today, etc"
173 - $date_to_display = date('H:i, F jS', strtotime($dateSrc));
 172+ $date_to_display = WikiTweetFunctions::Convert_Date(strtotime($dateSrc));
 173+ //$date_to_display = date('H:i, F jS', strtotime($dateSrc));
174174
175175 $res2 = $dbr->select('wikitweet_avatar','avatar',"`user`='".mysql_real_escape_string($tweetuser)."' ",__METHOD__,false);
176176 $row2 = $dbr->fetchObject ( $res2 );
@@ -284,7 +284,6 @@
285285
286286 $dest=array();
287287 $user_email = $wgWikiTweet['wikimail'];
288 - include('WikiTweet.functions.php');
289288 if($tomail==1 or $tomail==2){
290289 $res = $dbr->select('user','user_email',"user_name = '$user' ");
291290 if ($dbr->numRows($res) > 0){
@@ -326,7 +325,7 @@
327326 $lenlist += strlen($destmail);
328327 }
329328 if($lenlist>0){
330 - send( $dest, $user_email, "A new tweet for or about you !", $status);
 329+ WikiTweetFunctions::send( $dest, $user_email, "A new tweet for or about you !", $status);
331330 $text .= 'mail sent';
332331 }
333332
@@ -443,4 +442,4 @@
444443 public function getVersion() {
445444 return __CLASS__ . ': $Id: WikiTweet.api.php xxxxx 2010-05-09 13:42:00Z Faure.thomas $';
446445 }
447 -}
\ No newline at end of file
 446+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r87815fix r87811 using wfMsgExtfaurethomas11:02, 10 May 2011
r87818Simplify WikiTweetFunctions::Convert_Date using native Language::formatTimePe...faurethomas13:46, 10 May 2011

Comments

#Comment by Siebrand (talk | contribs)   09:00, 10 May 2011

This is really, really bad i18n. Not all languages have only one singular and one plural form.

#Comment by Siebrand (talk | contribs)   09:03, 10 May 2011

(Nikerabbit IRC ping) Have a look at extension CentralAuth which has an implementation of the same concept. Given that at least two extensions use it, I think this could be something we'd like to see added to core.

#Comment by Nikerabbit (talk | contribs)   10:34, 10 May 2011

What concept?

#Comment by Faure.thomas (talk | contribs)   10:50, 10 May 2011

I don't really know, but I can correct my code replacing "%time% by "$1" and using wfMsgExt to print my messages in the code. Is it the problem ? Thx...

#Comment by Faure.thomas (talk | contribs)   11:03, 10 May 2011

Siedbrand, I fixed the problem in r87815 . Is it correct now ? Regards.

#Comment by Siebrand (talk | contribs)   12:07, 10 May 2011

I still see only two options for second(s), minute(s), hour(s). This should simply be one message key that uses plural: "{{PLURAL:$1|One minute|$1 minutes}}".

#Comment by Faure.thomas (talk | contribs)   10:27, 18 May 2011

Hello Siedbrand, could you change the status of r87811, I fixed it in r87818. regards.

#Comment by Faure.thomas (talk | contribs)   12:24, 10 May 2011

ah ok... I understand now.

#Comment by Nikerabbit (talk | contribs)   13:30, 10 May 2011

The functionality already exists as Language::formatTimePeriod(), but it uses abbreviations.

#Comment by Faure.thomas (talk | contribs)   13:47, 10 May 2011

fixed by r87818.

Status & tagging log