Revision [20592]

This is an old revision of TwitterFeed made by PezHore on 2009-05-07 08:07:59.

 

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

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


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