Revision [20593]

This is an old revision of TwitterFeed made by PezHore on 2009-05-07 09:46:41.

 

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


  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.6
  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.6     Added clickable @Nicks
  34.  *  0.5     Fixed Nick Bug
  35.  *  0.4     Added comments
  36.  *  0.3     Incorporated replies and maxitems parameters
  37.  *  0.2     Added coding for parsing out username/identifying and click-ifying links
  38.  *  0.1     Copied over from rss action  
  39.  */
  40.  
  41.  
  42.  
  43. $caching = true;                    // change this to false to disable caching
  44. $twitter_cache_path = "/tmp";       // set this to a writable directory to store the cache files in
  45.  
  46. $lowest_cache_time_allowed = "5";   // set this to the lowest caching time allowed
  47.  
  48. // Begin dealing with action variables
  49.  
  50. // Maximum items to display
  51. $twitter_max_items = (int)trim($this->htmlspecialchars_ent($vars['maxitems']));
  52. if (!$twitter_max_items) {
  53.   $max_items = 5;                   // this is the default number of items to display  
  54. } else {
  55.   $max_items = $twitter_max_items;  // set max items to passed variable
  56. }
  57.  
  58. // Are we displaying replies
  59. $twitter_replies = (bool)trim($this->htmlspecialchars_ent($vars['replies']));
  60. if (!$twitter_replies) {
  61.   $replies = 0;                     // defaults to not showing replies
  62. } else {
  63.   $replies = $twitter_replies;      // set replies to passed variable
  64. }
  65.  
  66. // What cache time are we using
  67. $twitter_cache_time = (int)trim($this->htmlspecialchars_ent($vars['cachetime']));
  68. if (!$twitter_cache_time) {
  69.     $twitter_cache_time = 30;          // set this for default cache time
  70. } elseif ($twitter_cache_time < $lowest_cache_time_allowed) {
  71.     $twitter_cache_time = $lowest_cache_time_allowed;
  72. }
  73. $twitter_cache_file = "";           // initial value, no need to ever change
  74.  
  75. // End dealing with action variables
  76.  
  77. // Action configuration
  78. $twitter_path = $this->htmlspecialchars_ent($vars['url']);
  79. if ((!$twitter_path) && $wikka_vars) $twitter_path = $wikka_vars;
  80. $twitter_path = $this->cleanUrl(trim($twitter_path));
  81.  
  82. // override - Added by pezhore: not sure what this does...
  83. if (preg_match("/^(http|https):\/\/([^\\s\"<>]+)$/i", $twitter_path))
  84. {
  85.     $onyx_classpath = $this->GetConfigValue('onyx_path').DIRECTORY_SEPARATOR.'onyx-rss.php';
  86.     /**
  87.      * 3rdParty plugin; implements feed aggregation.
  88.      */
  89.     #include_once('3rdparty'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'onyx-rss'.DIRECTORY_SEPARATOR.'onyx-rss.php');
  90.     include_once $onyx_classpath;
  91.     if (!class_exists('Wikka_Onyx'))
  92.     {
  93.         /**
  94.          * Extension to plugin; implements error handling.
  95.          *
  96.          * @package     Actions
  97.          */
  98.         class Wikka_Onyx extends ONYX_RSS
  99.         {
  100.             //private function raiseError($line, $err)
  101.             function raiseError($line, $err)
  102.             {
  103.                 if ($this->debugMode)
  104.                 {
  105.                     $errortext = sprintf($this->error, $line, $err);
  106.                     echo '<!-- '.$errortext.' -->'."\n";
  107.                 }
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113. if (preg_match("/^(http|https):\/\/([^\\s\"<>]+)$/i", $twitter_path))
  114. {
  115.     if ($caching)
  116.     {
  117.         // Create unique cache file name based on URL
  118.         $twitter_cache_file = md5($twitter_path).".xml";
  119.     }
  120.  
  121.     //Load the RSS Feed: workaround to hide error messages within HTML comments:
  122.     #$twitter =& new Wikka_Onyx();
  123.     $twitter = instantiate('Wikka_Onyx');
  124.     $twitter->setCachePath($twitter_cache_path);
  125.     $twitter->parse($twitter_path, $twitter_cache_file, $twitter_cache_time);
  126.     $meta = $twitter->getData(ONYX_META);
  127.     $nick = ": ";
  128.    
  129.     //List the feed's items
  130.     $cached_output = "<ul>\n";
  131.     while ($max_items > 0 && ($item = $twitter->getNextItem()))
  132.     {
  133.  
  134.         $str_desc = $item['description'];     // Only using the description for our feed
  135.         $pos = strpos($str_desc, $nick);      // Find the location of the beginning of the tweet
  136.         $pos = $pos + 2;                      // Add two for the ':' and ' ' characters
  137.         $str_desc = substr($str_desc, $pos);  // Get everything after the "TwitterNick: "
  138.    
  139.         // Check to see if replies are to be included
  140.         if (strcspn($str_desc,"@") && $replies == 0)
  141.     {
  142.       // Replace http:// with valid links
  143.             $str_desc = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $str_desc);
  144.            
  145.       // Replace @username with valid link
  146.             $str_desc = preg_replace("/@([a-z,A-Z,0-9]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $str_desc);
  147.  
  148.             $cached_output .= "<li>".$str_desc."</li>\n";
  149.             $max_items = $max_items - 1;
  150.         } elseif ($replies == 1)              // If replies are wanted, show them
  151.     {
  152.       // Replace http:// with valid links
  153.       $str_desc = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $str_desc);
  154.      
  155.       // Replace @username with valid link
  156.       $str_desc = preg_replace("/@([a-z,A-Z,0-9]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $str_desc);
  157.      
  158.             $cached_output .= "<li>".$str_desc."</li>\n";
  159.             $max_items = $max_items - 1;
  160.     }
  161.     }
  162.    
  163.     $cached_output .= "</ul>\n";
  164.     echo $this->ReturnSafeHTML($cached_output);
  165. }
  166. else                                    // If this was called improperly, display usage
  167. {
  168.     echo '<em class="error">Error: Invalid Twitter action syntax. <br /> Proper usage: {{twitter http://twitter.com/statuses/user_timeline/[uid]}} or <br>
  169. {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30"}} or <br>
  170. {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies=true}} or <br>
  171. {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies="1" maxitems="5"}}}}</em>'; # i18n
  172. }
  173.  
  174. ?>


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