From 07f8ef395a4d4c6312ded96a039ef58e245e70b0 Mon Sep 17 00:00:00 2001 From: Karl 'vollmerk' Vollmer Date: Sun, 7 Dec 2008 14:02:12 +0000 Subject: finished up the rss feed stuff for now playing, it is currently the only type that works, should conform to rss standards now --- lib/class/ampacherss.class.php | 167 +++++++++++++++++++++++++++++++++ lib/class/rss.class.php | 79 ---------------- lib/class/xmldata.class.php | 17 ++-- lib/stream.lib.php | 3 +- rss.php | 19 +--- templates/show_now_playing.inc.php | 2 +- templates/show_recently_played.inc.php | 2 +- 7 files changed, 184 insertions(+), 105 deletions(-) create mode 100644 lib/class/ampacherss.class.php delete mode 100644 lib/class/rss.class.php diff --git a/lib/class/ampacherss.class.php b/lib/class/ampacherss.class.php new file mode 100644 index 00000000..69684f82 --- /dev/null +++ b/lib/class/ampacherss.class.php @@ -0,0 +1,167 @@ +type = self::validate_type($type); + + } // constructor + + /** + * get_xml + * This returns the xmldocument for the current rss type, it calls a sub function that gathers the data + * and then uses the xmlDATA class to build the document + */ + public function get_xml() { + + // Function call name + $data_function = 'load_' . $this->type; + $pub_date_function = 'pubdate_' . $this->type; + + $data = call_user_func(array('AmpacheRSS',$data_function)); + $pub_date = call_user_func(array('AmpacheRSS',$pub_date_function)); + + xmlData::set_type('rss'); + $xml_document = xmlData::rss_feed($data,$this->get_title(),$this->get_description(),$pub_date); + + return $xml_document; + + } // get_xml + + /** + * get_title + * This returns the standardized title for the rss feed based on this->type + */ + public function get_title() { + + $titles = array('now_playing'=>_('Now Playing'), + 'recently_played'=>_('Recently Played'), + 'latest_album'=>_('Newest Albums'), + 'latest_artist'=>_('Newest Artists')); + + return $titles[$this->type]; + + } // get_title + + /** + * get_description + * This returns the standardized description for the rss feed based on this->type + */ + public function get_description() { + + //FIXME: For now don't do any kind of translating + return 'Ampache RSS Feeds'; + + } // get_description + + /** + * validate_type + * this returns a valid type for an rss feed, if the specified type is invalid it returns a default value + */ + public static function validate_type($type) { + + $valid_types = array('now_playing','recently_played','latest_album','latest_artist','latest_song', + 'popular_song','popular_album','popular_artist'); + + if (!in_array($type,$valid_types)) { + return 'now_playing'; + } + + return $type; + + } // validate_type + + /** + * get_display + * This dumps out some html and an icon for the type of rss that we specify + */ + public static function get_display($type='nowplaying') { + + // Default to now playing + $type = self::validate_type($type); + + $string = '' . get_user_icon('feed',_('RSS Feed')) . ''; + + return $string; + + } // get_display + + // type specific functions below, these are called semi-dynamically based on the current type // + + /** + * load_now_playing + * This loads in the now playing information. This is just the raw data with key=>value pairs that could be turned + * into an xml document if we so wished + */ + public static function load_now_playing() { + + $data = get_now_playing(); + + $results = array(); + + foreach ($data as $element) { + $song = $element['song']; + $client = $element['user']; + $xml_array = array('title'=>$song->f_title . ' - ' . $song->f_artist . ' - ' . $song->f_album, + 'link'=>$song->link, + 'description'=>$song->title . ' - ' . $song->f_artist_full . ' - ' . $song->f_album_full, + 'comments'=>$client->fullname . ' - ' . $element['agent'], + 'pubDate'=>date("r",$element['expire']) + ); + $results[] = $xml_array; + } // end foreach + + return $results; + + } // load_now_playing + + /** + * pubdate_now_playing + * this is the pub date we should use for the now playing information, + * this is a little specific as it uses the 'newest' expire we can find + */ + public static function pubdate_now_playing() { + + // Little redundent, should be fixed by an improvement in the get_now_playing stuff + $data = get_now_playing(); + + $element = array_shift($data); + + return $element['expire']; + + } // pubdate_now_playing + +} // end AmpacheRSS class diff --git a/lib/class/rss.class.php b/lib/class/rss.class.php deleted file mode 100644 index 21362131..00000000 --- a/lib/class/rss.class.php +++ /dev/null @@ -1,79 +0,0 @@ -' . get_user_icon('feed',_('RSS Feed')) . ''; - - return $string; - - } // get_display - -} // end RSS class diff --git a/lib/class/xmldata.class.php b/lib/class/xmldata.class.php index cae0fa04..1b478df7 100644 --- a/lib/class/xmldata.class.php +++ b/lib/class/xmldata.class.php @@ -299,10 +299,13 @@ class xmlData { public static function rss_feed($data,$title,$description,$date) { $string = "\t$title\n\t" . Config::get('web_path') . "\n\t" . - "$date\n"; + "" . date("r",$date) . "\n"; // Pass it to the keyed array xml function - $string .= self::keyed_array($data); + foreach ($data as $item) { + // We need to enclose it in an item tag + $string .= self::keyed_array(array('item'=>$item),1); + } $final = self::_header() . $string . self::_footer(); @@ -315,7 +318,7 @@ class xmlData { * this returns a standard header, there are a few types * so we allow them to pass a type if they want to */ - private static function _header($type='') { + private static function _header() { switch (self::$type) { case 'xspf': @@ -326,8 +329,8 @@ class xmlData { break; case 'rss': $header = "\n " . - "\n" . + "\n\n"; break; default: $header = "\n\n"; @@ -342,11 +345,11 @@ class xmlData { * _footer * this returns the footer for this document, these are pretty boring */ - private static function _footer($type='') { + private static function _footer() { switch (self::$type) { case 'rss': - $footer = "\n\t\n\n"; + $footer = "\n\n\n"; break; default: $footer = "\n\n"; diff --git a/lib/stream.lib.php b/lib/stream.lib.php index b0f4d609..8cc92d50 100644 --- a/lib/stream.lib.php +++ b/lib/stream.lib.php @@ -41,7 +41,8 @@ function show_now_playing() { */ function get_now_playing($filter='') { - $sql = "SELECT `session_stream`.`agent`,`now_playing`.`song_id`,`now_playing`.`user` FROM `now_playing` " . + $sql = "SELECT `session_stream`.`agent`,`now_playing`.`song_id`,`now_playing`.`user`,`now_playing`.`expire` " . + "FROM `now_playing` " . "LEFT JOIN `session_stream` ON `session_stream`.`id`=`now_playing`.`id` " . "ORDER BY `now_playing`.`expire` DESC"; $db_results = Dba::query($sql); diff --git a/rss.php b/rss.php index 6ccace13..329b5282 100644 --- a/rss.php +++ b/rss.php @@ -30,23 +30,10 @@ if (!Config::get('use_rss') || Config::get('demo_mode')) { // Add in our base hearder defining the content type header("Content-Type: application/xml; charset=" . Config::get('site_charset')); -header("Content-Disposition: attachment; filename=rss.xml"); -// This is always going to be an rss feed, so make sure our header and footers are correct -xmlData::set_type('rss'); +$rss = new AmpacheRSS($_REQUEST['type']); +echo $rss->get_xml(); -switch ($_REQUEST['action']) { - case 'user': - - break; - case 'catalog_add': - - default: - - - break; -} // end data collection - -show_RSS($_REQUEST['type'],$_REQUEST['username']); +//show_RSS($_REQUEST['type'],$_REQUEST['username']); ?> diff --git a/templates/show_now_playing.inc.php b/templates/show_now_playing.inc.php index d489c274..a1ee87a7 100644 --- a/templates/show_now_playing.inc.php +++ b/templates/show_now_playing.inc.php @@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ if (count($results)) { -$link = Config::get('use_rss') ? ' ' . RSS::get_display('nowplaying') : ''; +$link = Config::get('use_rss') ? ' ' . AmpacheRSS::get_display('nowplaying') : ''; ?>
diff --git a/templates/show_recently_played.inc.php b/templates/show_recently_played.inc.php index 25ca1396..a638a24d 100644 --- a/templates/show_recently_played.inc.php +++ b/templates/show_recently_played.inc.php @@ -21,7 +21,7 @@ /* Define the time places starting at 0 */ $time_unit = array('',_('seconds ago'),_('minutes ago'),_('hours ago'),_('days ago'),_('weeks ago'),_('months ago'),_('years ago')); -$link = Config::get('use_rss') ? ' ' . RSS::get_display('recentlyplayed') : ''; +$link = Config::get('use_rss') ? ' ' . AmpacheRSS::get_display('recentlyplayed') : ''; show_box_top(_('Recently Played') . $link); ?> -- cgit