https://bugzilla.wikimedia.org/show_bug.cgi?id=60227

       Web browser: ---
            Bug ID: 60227
           Summary: Extensions:TweetANew patch to support multibyte
                    character (e.g. CJK) and t.co changing
           Product: MediaWiki extensions
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: Unprioritized
         Component: [other]
          Assignee: wikibugs-l@lists.wikimedia.org
          Reporter: is...@cory.jp
    Classification: Unclassified
   Mobile Platform: ---

[1]
Twitter accept a tweet in 140 characters or below.
Extension:TweetANew has a process to truncate max 140 bytes as a tweet.

But TweetANew truncate a tweet too shorten on some languages (such as Japanese
or Chinese) which have multibyte characher.
Because it is using PHP string class which process string as bytes array.
I rewrite it with PHP mbstring class which process string by character.

[2]
Now Twitter wrap up all URLs in "t.co", and changed length from 20 to 22 (on
https, 21 to 23) characters.
https://dev.twitter.com/docs/tco-url-wrapper
https://dev.twitter.com/blog/upcoming-tco-changes

Following patch is working in my wiki. http://kimagurenote.net/kn/TweetANew
Please test it.


--- TweetANew.body.php.head    2013-12-07 10:54:04.000000000 +0900
+++ TweetANew.body.php    2014-01-15 22:57:20.000000000 +0900
@@ -231,17 +231,19 @@
     */
    public static function makeSendTweet( $tweet_text, $finalurl ) {
        global $wgTweetANewTwitter, $wgLang;
-
-        # Calculate length of tweet factoring in longURL
-        if ( strlen( $finalurl ) > 20 ) {
-            $tweet_text_count = ( strlen( $finalurl ) - 20 ) + 140;
+
+        # Calculate length of tweet factoring in t.co
+        if ( stripos( $finalurl, 'https:' ) !== false ) {
+            $tweet_text_count = 140 - 23 + mb_strlen( $finalurl );
+        } elseif ( stripos( $finalurl, 'http:' ) !== false ) {
+            $tweet_text_count = 140 - 22 + mb_strlen( $finalurl );
        } else {
            $tweet_text_count = 140;
        }

        # Check if length of tweet is beyond 140 characters and shorten if
necessary
-        if ( strlen( $tweet_text ) > $tweet_text_count ) {
-            $tweet_text = $wgLang->truncate( $tweet_text, $tweet_text_count );
+        if ( mb_strlen( $tweet_text ) > $tweet_text_count ) {
+            $tweet_text = mb_substr( $tweet_text, 0, $tweet_text_count - 3 ) .
'...';
        }



I'm sorry about my broken English;-)

-- 
You are receiving this mail because:
You are the assignee for the bug.
You are on the CC list for the bug.
_______________________________________________
Wikibugs-l mailing list
Wikibugs-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikibugs-l

Reply via email to