[FEATURE] Add an option to rewrite links 78/25378/3
authorNicole Cordes <cordes@cps-it.de>
Wed, 13 Nov 2013 14:08:55 +0000 (15:08 +0100)
committerNicole Cordes <typo3@cordes.co>
Tue, 19 Nov 2013 00:37:47 +0000 (01:37 +0100)
Twitter introduces an own link shortner. This is used in all tweets
returned from the api. This patch adds a new plugin option to rewrite
all links to the entered one.

Releases: 1.1
Change-Id: I1d059772afa37914f07e3f4cfe648fdc3b1b2ca6
Reviewed-on: https://review.typo3.org/25378
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Classes/Domain/Repository/TweetRepository.php
Configuration/TypoScript/Main/constants.txt
Configuration/TypoScript/Main/setup.txt

index de6bd0e..66ee35e 100644 (file)
@@ -46,6 +46,13 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
        protected $flashMessageContainer = NULL;
 
        /**
+        * The plugin settings
+        *
+        * @var array
+        */
+       protected $settings = array();
+
+       /**
         * Constructor function
         */
        public function __construct() {
@@ -67,26 +74,27 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
         */
        public function getTweetsFromUserTimeline($settings, &$response = NULL) {
                $tweets = array();
+               $this->settings = $settings;
 
                if ($this->isTwitterSigned() && $this->isCurlActivated()) {
                        $parameter = array();
 
                        // Get screen name
-                       if (Tx_WtTwitter_Utility_Compatibility::testInt($settings['account'])) {
-                               $parameter['user_id'] = $settings['account'];
+                       if (Tx_WtTwitter_Utility_Compatibility::testInt($this->settings['account'])) {
+                               $parameter['user_id'] = $this->settings['account'];
                        } else {
-                               $parameter['screen_name'] = $settings['account'];
+                               $parameter['screen_name'] = $this->settings['account'];
                        }
 
                        // Enable retweets
-                       if ($settings['showRetweets']) {
+                       if ($this->settings['showRetweets']) {
                                $parameter['include_rts'] = 'true';
                        } else {
                                $parameter['include_rts'] = 'false';
                        }
 
                        // Exclude retweets
-                       if ($settings['excludeReplies']) {
+                       if ($this->settings['excludeReplies']) {
                                $parameter['exclude_replies'] = 'true';
                        } else {
                                $parameter['exclude_replies'] = 'false';
@@ -95,7 +103,11 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                        $tweets = $this->callApi(Tx_WtTwitter_Twitter_Api::getStatusesUserTimelineUrl(), 'GET', $parameter, $response);
                }
 
-               return $this->addOldUserInformation($this->sliceArray($tweets, $settings['limit']));
+               $tweets = $this->sliceArray($tweets, $this->settings['limit']);
+               $tweets = $this->addOldUserInformation($tweets);
+               $tweets = $this->rewriteIncludedLinks($tweets);
+
+               return $tweets;
        }
 
        /**
@@ -105,33 +117,39 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
         */
        public function getTweetsFromSearch($settings, &$response = NULL) {
                $tweets = array();
+               $this->settings = $settings;
 
                if ($this->isTwitterSigned()&& $this->isCurlActivated()) {
                        $parameter = array(
-                               'q' => $settings['hashtag']
+                               'q' => $this->settings['hashtag']
                        );
 
                        $result = $this->callApi(Tx_WtTwitter_Twitter_Api::getSearchTweetsUrl(), 'GET', $parameter, $response);
                        $tweets = $result->statuses;
                }
 
-               return $this->addOldUserInformation($this->sliceArray($tweets, $settings['limit']));
+               $tweets = $this->sliceArray($tweets, $this->settings['limit']);
+               $tweets = $this->addOldUserInformation($tweets);
+               $tweets = $this->rewriteIncludedLinks($tweets);
+
+               return $tweets;
        }
 
        public function getListsFromUser($settings, &$response = NULL) {
                $lists = array();
+               $this->settings = $settings;
 
                if ($this->isTwitterSigned()&& $this->isCurlActivated()) {
                        $parameter = array(
-                               'count' => ((int) $settings['limit'] > 0 ? $settings['limit'] : '1000'),
+                               'count' => ((int) $this->settings['limit'] > 0 ? $this->settings['limit'] : '1000'),
                                'cursor' => '-1'
                        );
 
                        // Get screen name
-                       if (Tx_WtTwitter_Utility_Compatibility::testInt($settings['account'])) {
-                               $parameter['user_id'] = $settings['account'];
+                       if (Tx_WtTwitter_Utility_Compatibility::testInt($this->settings['account'])) {
+                               $parameter['user_id'] = $this->settings['account'];
                        } else {
-                               $parameter['screen_name'] = $settings['account'];
+                               $parameter['screen_name'] = $this->settings['account'];
                        }
 
                        $result = $this->callApi(Tx_WtTwitter_Twitter_Api::getListsOwnershipsUrl(), 'GET', $parameter, $response);
@@ -172,19 +190,20 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
         */
        public function getListsForUser($settings, &$response = NULL) {
                $lists = array();
+               $this->settings = $settings;
                $cursor = -1;
 
                if ($this->isTwitterSigned()&& $this->isCurlActivated()) {
                        $parameter = array();
 
                        // Get screen name
-                       if (Tx_WtTwitter_Utility_Compatibility::testInt($settings['account'])) {
-                               $parameter['user_id'] = $settings['account'];
+                       if (Tx_WtTwitter_Utility_Compatibility::testInt($this->settings['account'])) {
+                               $parameter['user_id'] = $this->settings['account'];
                        } else {
-                               $parameter['screen_name'] = $settings['account'];
+                               $parameter['screen_name'] = $this->settings['account'];
                        }
 
-                       while (!empty($cursor) && ($settings['limit'] == 0 || $settings['limit'] > count($lists))) {
+                       while (!empty($cursor) && ($this->settings['limit'] == 0 || $this->settings['limit'] > count($lists))) {
                                $parameter['cursor'] = $cursor;
                                $result = $this->callApi(Tx_WtTwitter_Twitter_Api::getListsMembershipsUrl(), 'GET', $parameter, $response);
                                $lists = array_merge($lists, (array) $result->lists);
@@ -216,7 +235,7 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                        });
                }
 
-               return $this->sliceArray($lists, $settings['limit']);
+               return $this->sliceArray($lists, $this->settings['limit']);
        }
 
        /**
@@ -261,7 +280,7 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
         * @return array
         */
        protected function callApi($url, $method, $parameter, $response) {
-               return Tx_WtTwitter_Twitter_Api::processRequest(
+               $tweets = Tx_WtTwitter_Twitter_Api::processRequest(
                        $this->extensionConfiguration['oauth_token'],
                        $this->extensionConfiguration['oauth_token_secret'],
                        $url,
@@ -269,6 +288,8 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                        $parameter,
                        $response
                );
+
+               return $tweets;
        }
 
        /**
@@ -289,7 +310,7 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
         * @return array
         */
        protected function addOldUserInformation(array $tweets) {
-               foreach ($tweets as &$tweet) {
+               foreach ($tweets as $tweet) {
                        $tweet->profile_image_url = $tweet->user->profile_image_url;
                        $tweet->from_user = $tweet->user->screen_name;
                }
@@ -298,4 +319,24 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
                return $tweets;
        }
 
+       /**
+        * @param array $tweets
+        * @return array
+        */
+       protected function rewriteIncludedLinks(array $tweets) {
+               if (!empty($this->settings['rewriteLinks'])) {
+                       foreach ($tweets as $tweet) {
+                               if ($tweet->entities && $tweet->entities->urls && is_array($tweet->entities->urls)) {
+                                       foreach ($tweet->entities->urls as $url) {
+                                               $tweet->text = str_replace($url->url, $url->expanded_url, $tweet->text);
+                                       }
+                                       unset($url);
+                               }
+                       }
+                       unset($tweet);
+               }
+
+               return $tweets;
+       }
+
 }
\ No newline at end of file
index c370645..07f9ea2 100644 (file)
@@ -25,6 +25,9 @@ plugin.wttwitter {
        # cat=plugin.wt_twitter//0100; type=boolean; label= Exclude replies: Filter replies from tweets - only if mode is showOwn (e.g. 1)
        main.excludeReplies = 0
 
+       # 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//9999; type=boolean; label= Debug Mode: Enable or disable some debug output
        debug = 0
 
index 6b3cc5d..f851d80 100644 (file)
@@ -13,6 +13,7 @@ plugin.tx_wttwitter {
                        sortDESC = {$plugin.wttwitter.main.sortDESC}
                        showRetweets = {$plugin.wttwitter.main.showRetweets}
                        excludeReplies = {$plugin.wttwitter.main.excludeReplies}
+                       rewriteLinks = {$plugin.wttwitter.main.rewriteLinks}
                        debug = {$plugin.wttwitter.debug}
 
                        # Tweet Fields (Feed in a loop like text, link, etc..)