[BUGFIX] Use hashtags provided by Twitter api 35/26735/3
authorNicole Cordes <cordes@cps-it.de>
Fri, 10 Jan 2014 11:46:34 +0000 (12:46 +0100)
committerNicole Cordes <typo3@cordes.co>
Mon, 13 Jan 2014 09:02:09 +0000 (10:02 +0100)
Move the hashtag link generation to internal handling as Twitter api
provides all needed information.

Resolves: #53881
Change-Id: Ie56d942bfcef0aa5ff86bc5c625f3b305f5f2b49
Reviewed-on: https://review.typo3.org/26735
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Classes/Domain/Repository/TweetRepository.php
Classes/UserFunction/user_wttwitter_userfunction.php
Configuration/TypoScript/Main/constants.txt
Configuration/TypoScript/Main/setup.txt

index 5a3f582..aedd802 100644 (file)
 class Tx_WtTwitter_Domain_Repository_TweetRepository {
 
        /**
+        * The content object
+        *
+        * @var tslib_cObj
+        */
+       protected $contentObject = NULL;
+
+       /**
         * The extension configuration
         *
         * @var array
@@ -57,6 +64,7 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
         */
        public function __construct() {
                $this->extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['wt_twitter']);
+               $this->contentObject = t3lib_div::makeInstance('tslib_cObj');
        }
 
        /**
@@ -103,9 +111,7 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                        $tweets = $this->callApi(Tx_WtTwitter_Twitter_Api::getStatusesUserTimelineUrl(), 'GET', $parameter, $response);
                }
 
-               $tweets = $this->sliceArray($tweets, $this->settings['limit']);
-               $tweets = $this->addOldUserInformation($tweets);
-               $tweets = $this->rewriteIncludedLinks($tweets);
+               $this->postProcessTweets($tweets);
 
                return $tweets;
        }
@@ -128,9 +134,7 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                        $tweets = $result->statuses;
                }
 
-               $tweets = $this->sliceArray($tweets, $this->settings['limit']);
-               $tweets = $this->addOldUserInformation($tweets);
-               $tweets = $this->rewriteIncludedLinks($tweets);
+               $this->postProcessTweets($tweets);
 
                return $tweets;
        }
@@ -235,7 +239,9 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                        });
                }
 
-               return $this->sliceArray($lists, $this->settings['limit']);
+               $this->sliceArray($lists, $this->settings['limit']);
+
+               return $lists;
        }
 
        /**
@@ -293,35 +299,44 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
        }
 
        /**
+        * @param $tweets
+        * @return void
+        */
+       protected function postProcessTweets(&$tweets) {
+               $this->sliceArray($tweets, $this->settings['limit']);
+               $this->addOldUserInformation($tweets);
+               $this->rewriteIncludedLinks($tweets);
+               $this->linkHashtags($tweets);
+       }
+
+       /**
         * @param array $array
         * @param integer $count
-        * @return array
+        * @return void
         */
-       protected function sliceArray(array $array, $count) {
-               if (empty($count) || count($array) <= $count) {
-                       return $array;
+       protected function sliceArray(&$array, $count) {
+               if (!empty($count) && count($array) > $count) {
+                       $array = array_slice($array, 0, $count);
                }
-
-               return array_slice($array, 0, $count);
        }
 
        /**
         * @param array $tweets
-        * @return array
+        * @return void
         */
-       protected function addOldUserInformation(array $tweets) {
-               foreach ($tweets as $tweet) {
-                       $tweet->profile_image_url = $tweet->user->profile_image_url;
-                       $tweet->from_user = $tweet->user->screen_name;
+       protected function addOldUserInformation(&$tweets) {
+               if (is_array($tweets)) {
+                       foreach ($tweets as $tweet) {
+                               $tweet->profile_image_url = $tweet->user->profile_image_url;
+                               $tweet->from_user = $tweet->user->screen_name;
+                       }
+                       unset($tweet);
                }
-               unset($tweet);
-
-               return $tweets;
        }
 
        /**
         * @param array $tweets
-        * @return array
+        * @return void
         */
        protected function rewriteIncludedLinks(array $tweets) {
                if (!empty($this->settings['rewriteLinks'])) {
@@ -335,8 +350,28 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                        }
                        unset($tweet);
                }
+       }
 
-               return $tweets;
+       /**
+        * @param array $tweets
+        * @return void
+        */
+       protected function linkHashtags(&$tweets) {
+               if (is_array($tweets) && !empty($this->settings['linkHashtags'])) {
+                       foreach ($tweets as $tweet) {
+                               if ($tweet->entities && $tweet->entities->hashtags && is_array($tweet->entities->hashtags)) {
+                                       foreach ($tweet->entities->hashtags as $hashtag) {
+                                               $hastagText = '#' . $hashtag->text;
+                                               $typolinkConfiguration = array(
+                                                       'parameter' => 'https://twitter.com/search?q=%23' . rawurlencode($hashtag->text),
+                                               );
+                                               $tweet->text = str_replace($hastagText, $this->contentObject->typolink($hastagText, $typolinkConfiguration), $tweet->text);
+                                       }
+                                       unset($hashtag);
+                               }
+                       }
+                       unset($tweet);
+               }
        }
 
 }
\ No newline at end of file
index 563be71..54a06c8 100644 (file)
@@ -73,7 +73,7 @@ class user_wttwitteruserfunction {
                $string = $this->cObj->getCurrentVal(); // get date from typoscript
 
                // 1. rewrite URL with typolink
-               preg_match_all('/(https?:\/\/|www\.)\S*/i', $string, $arr_result); // get all links of the string
+               preg_match_all('/(^|\s)(https?:\/\/|www\.)\S*/i', $string, $arr_result); // get all links of the string
                foreach ((array)$arr_result[0] as $url) { // one loop for every link in the string
                        if (!empty($url)) { // if there is a URL
                                $typolinkconf = array('parameter' => $url); // typolink configuration
index 07f9ea2..ea0679e 100644 (file)
@@ -27,6 +27,8 @@ plugin.wttwitter {
 
        # cat=plugin.wt_twitter//0110; type=boolean; label= Rewrite links: Rewrites the twitter links with the original ones
        main.rewriteLinks = 0
+       # cat=plugin.wt_twitter//0130; type=boolean; label= Inserts links for used hashtags
+       main.linkHashtags = 1
 
        # cat=plugin.wt_twitter//9999; type=boolean; label= Debug Mode: Enable or disable some debug output
        debug = 0
index f851d80..b321666 100644 (file)
@@ -14,6 +14,7 @@ plugin.tx_wttwitter {
                        showRetweets = {$plugin.wttwitter.main.showRetweets}
                        excludeReplies = {$plugin.wttwitter.main.excludeReplies}
                        rewriteLinks = {$plugin.wttwitter.main.rewriteLinks}
+                       linkHashtags = {$plugin.wttwitter.main.linkHashtags}
                        debug = {$plugin.wttwitter.debug}
 
                        # Tweet Fields (Feed in a loop like text, link, etc..)