Revision [20595]

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


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