Revision [20597]

This is an old revision of TwitterFeed made by PezHore on 2009-05-07 13:49:18.

 

Twitter Feed

This is my first attempt at an action... hopefully things will work out ok.

Note: This is mostly re-tooled from the default rss action. I added in some additional features as specified below

Features


Current Features

Source Code


Current Version: 0.8

  1. <?php
  2. /**
  3.  * Cache and display an Twitter RSS feed.
  4.  *
  5.  * Usage:
  6.  *      {{twitter http://twitter.com/statuses/user_timeline/[uid]}} or
  7.  *      {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30"}} or
  8.  *      {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies=true}} or
  9.  *      {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies="1" maxitems="5"}}
  10.  *       
  11.  * NOTE 1: in Onyx-RSS default is "debugMode" which results in all errors being printed
  12.  * this could be suppressed by turning debug mode off, but then we'd never have a
  13.  * clue about the cause of any error.
  14.  * A better (preliminary) approach seems to be to override the raiseError() method
  15.  * still providing the text of any error message, only within an HTML comment:
  16.  * that way normal display will look clean but you can look at the HTML source to
  17.  * find the cause of any problem.
  18.  * NOTE 2: no solution for timeout problems with non-existing feeds yet...
  19.  * NOTE 3: A major portion of this was taken from the rss.php action.
  20.  *
  21.  * Version 0.8
  22.  *
  23.  * @input       string  $url    mandatory: URL of the feed
  24.  * @input       integer $cachetime  optional: time in minutes to cache the feed
  25.  * @input   boolean $replies optional: True for displaying @username replies. Defaults to false.
  26.  * @input   integer $max_items optional: Maximum items to display
  27.  * @output      ... tbd @@@
  28.  * @uses        Wakka::cleanUrl()
  29.  * @uses        Wakka::ReturnSafeHTML()
  30.  * @todo        Add comments for readability
  31.  *
  32.  * Changelog
  33.  *  0.8     Added clickable #hashes
  34.  *  0.7     Fixed underscore nick bug not being clickable
  35.  *  0.6     Added clickable @Nicks
  36.  *  0.5     Fixed Nick Bug
  37.  *  0.4     Added comments
  38.  *  0.3     Incorporated replies and maxitems parameters
  39.  *  0.2     Added coding for parsing out username/identifying and click-ifying links
  40.  *  0.1     Copied over from rss action  
  41.  */
  42.  
  43.  
  44.  
  45. $caching = true;                    // change this to false to disable caching
  46. $twitter_cache_path = "/tmp";       // set this to a writable directory to store the cache files in
  47.  
  48. $lowest_cache_time_allowed = "5";   // set this to the lowest caching time allowed
  49.  
  50. // Begin dealing with action variables
  51.  
  52. // Maximum items to display
  53. $twitter_max_items = (int)trim($this->htmlspecialchars_ent($vars['maxitems']));
  54. if (!$twitter_max_items) {
  55.   $max_items = 5;                   // this is the default number of items to display  
  56. } else {
  57.   $max_items = $twitter_max_items;  // set max items to passed variable
  58. }
  59.  
  60. // Are we displaying replies
  61. $twitter_replies = (bool)trim($this->htmlspecialchars_ent($vars['replies']));
  62. if (!$twitter_replies) {
  63.   $replies = 0;                     // defaults to not showing replies
  64. } else {
  65.   $replies = $twitter_replies;      // set replies to passed variable
  66. }
  67.  
  68. // What cache time are we using
  69. $twitter_cache_time = (int)trim($this->htmlspecialchars_ent($vars['cachetime']));
  70. if (!$twitter_cache_time) {
  71.     $twitter_cache_time = 30;          // set this for default cache time
  72. } elseif ($twitter_cache_time < $lowest_cache_time_allowed) {
  73.     $twitter_cache_time = $lowest_cache_time_allowed;
  74. }
  75. $twitter_cache_file = "";           // initial value, no need to ever change
  76.  
  77. // End dealing with action variables
  78.  
  79. // Action configuration
  80. $twitter_path = $this->htmlspecialchars_ent($vars['url']);
  81. if ((!$twitter_path) && $wikka_vars) $twitter_path = $wikka_vars;
  82. $twitter_path = $this->cleanUrl(trim($twitter_path));
  83.  
  84. // override - Added by pezhore: not sure what this does...
  85. if (preg_match("/^(http|https):\/\/([^\\s\"<>]+)$/i", $twitter_path))
  86. {
  87.     $onyx_classpath = $this->GetConfigValue('onyx_path').DIRECTORY_SEPARATOR.'onyx-rss.php';
  88.     /**
  89.      * 3rdParty plugin; implements feed aggregation.
  90.      */
  91.     #include_once('3rdparty'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'onyx-rss'.DIRECTORY_SEPARATOR.'onyx-rss.php');
  92.     include_once $onyx_classpath;
  93.     if (!class_exists('Wikka_Onyx'))
  94.     {
  95.         /**
  96.          * Extension to plugin; implements error handling.
  97.          *
  98.          * @package     Actions
  99.          */
  100.         class Wikka_Onyx extends ONYX_RSS
  101.         {
  102.             //private function raiseError($line, $err)
  103.             function raiseError($line, $err)
  104.             {
  105.                 if ($this->debugMode)
  106.                 {
  107.                     $errortext = sprintf($this->error, $line, $err);
  108.                     echo '<!-- '.$errortext.' -->'."\n";
  109.                 }
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115. if (preg_match("/^(http|https):\/\/([^\\s\"<>]+)$/i", $twitter_path))
  116. {
  117.     if ($caching)
  118.     {
  119.         // Create unique cache file name based on URL
  120.         $twitter_cache_file = md5($twitter_path).".xml";
  121.     }
  122.  
  123.     //Load the RSS Feed: workaround to hide error messages within HTML comments:
  124.     #$twitter =& new Wikka_Onyx();
  125.     $twitter = instantiate('Wikka_Onyx');
  126.     $twitter->setCachePath($twitter_cache_path);
  127.     $twitter->parse($twitter_path, $twitter_cache_file, $twitter_cache_time);
  128.     $meta = $twitter->getData(ONYX_META);
  129.     $nick = ": ";
  130.    
  131.     //List the feed's items
  132.     $cached_output = "<ul>\n";
  133.     while ($max_items > 0 && ($item = $twitter->getNextItem()))
  134.     {
  135.  
  136.         $str_desc = $item['description'];     // Only using the description for our feed
  137.         $pos = strpos($str_desc, $nick);      // Find the location of the beginning of the tweet
  138.         $pos = $pos + 2;                      // Add two for the ':' and ' ' characters
  139.         $str_desc = substr($str_desc, $pos);  // Get everything after the "TwitterNick: "
  140.    
  141.         // Check to see if replies are to be included
  142.         if (strcspn($str_desc,"@") && $replies == 0)
  143.     {
  144.       // Replace http:// with valid links
  145.             $str_desc = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $str_desc);
  146.            
  147.       // Replace @username with valid link
  148.             $str_desc = preg_replace("/@([a-z,A-Z,0-9,_]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $str_desc);
  149.            
  150.             // Replace #topic with valid link to search.twitter.com
  151.       $str_desc = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=$1\">#$1</a>", $str_desc);
  152.      
  153.             $cached_output .= "<li>".$str_desc."</li>\n";
  154.             $max_items = $max_items - 1;
  155.         } elseif ($replies == 1)              // If replies are wanted, show them
  156.     {
  157.       // Replace http:// with valid links
  158.       $str_desc = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $str_desc);
  159.      
  160.       // Replace @username with valid link
  161.       $str_desc = preg_replace("/@([a-z,A-Z,0-9,_]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $str_desc);
  162.      
  163.       // Replace #topic with valid link to search.twitter.com
  164.       $str_desc = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=$1\">#$1</a>", $str_desc);
  165.      
  166.             $cached_output .= "<li>".$str_desc."</li>\n";
  167.             $max_items = $max_items - 1;
  168.     }
  169.     }
  170.    
  171.     $cached_output .= "</ul>\n";
  172.     echo $this->ReturnSafeHTML($cached_output);
  173. }
  174. else                                    // If this was called improperly, display usage
  175. {
  176.     echo '<em class="error">Error: Invalid Twitter action syntax. <br /> Proper usage: {{twitter http://twitter.com/statuses/user_timeline/[uid]}} or <br>
  177. {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30"}} or <br>
  178. {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies=true}} or <br>
  179. {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies="1" maxitems="5"}}}}</em>'; # i18n
  180. }
  181.  
  182. ?>


Installation



Known Bugs



Thoughts? Questions? Let me know. I'm on IRC as Pezhore.


CategoryUserContributions
There is one comment on this page. [Display comment]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki