RSS is an essential part of every site with rich content. We'd like to show how easy it is to create a RSS feed with Limb and WACT.
Say, we'd like to have the RSS feed of the latest news objects and documents.
Let's create a simple last-news-feed.php script, it's going to accomplish the following tasks:
We think the following code should be self explanatory:
<?php require_once('setup.php'); require_once(LIMB_DIR . '/core/template/template.class.php'); require_once(LIMB_DIR . '/core/fetcher.class.php'); require_once(LIMB_DIR . '/core/lib/http/uri.class.php'); require_once(LIMB_DIR . '/core/lib/util/array_dataset.class.php'); require_once(LIMB_DIR . '/core/lib/system/sys.class.php'); define('CONTENT_LOCALE_ID', DEFAULT_CONTENT_LOCALE_ID); //sorting callback used in uasort function sorter($a, $b) { $date1 = $a['modified_date']; $date2 = $b['modified_date']; if ($date1 == $date2) { if($a['node_id'] > $b['node_id']) return -1; else return 1; } return ($date1 > $date2) ? -1 : 1; } //function that normalizes hrefs in $content using $base_path to resolve relative links function fix_hrefs($base_path, $content) { $GLOBALS['base_path'] = $base_path; return preg_replace_callback('~href=("|\')?([^"\'>\s]+)("|\')?~', '_replace_callback', $content); } //preg_replace_callback function, does the dirty job of regular expression replacing function _replace_callback($matches) { if(preg_match('~(https?|ftp)://~', $matches[2])) return $matches[0]; $url = get_qualified_url($GLOBALS['base_path'], $matches[2]); return "href='{$url}'"; } //small helper function that actually normalizes the href function get_qualified_url($base_path, $raw_path) { $path = $raw_path; if($path{0} != '/') $path = uri :: resolve_path($base_path . '/' . $path); return 'http://' . sys :: hostname() . $path; } $counter = 0; $params = array( 'limit' => 3, 'order' => array('modified_date' => 'DESC') ); $arr_news = fetch('news_object', $counter, $params, 'fetch_accessible'); $params = array( 'limit' => 3, 'order' => array('modified_date' => 'DESC') ); $arr_documents = fetch('document', $counter, $params, 'fetch_accessible'); $arr = array_merge($arr_documents, $arr_news); uasort($arr, 'sorter'); foreach($arr as $key => $data) { $arr[$key]['annotation'] = fix_hrefs($data['path'], $data['annotation']); $arr[$key]['path'] = get_qualified_url($data['path'], $data['path']); } $ds = new array_dataset($arr); $template = new template('/rss/last_news.rss'); $template->set('xml', '<!--l version="1.0" encoding="utf-8-->'); $news =& $template->find_child('last_news'); $news->register_dataset($ds); header("Content-Type: application/xml"); $template->display(); ?>
And here goes the template itself:
{$xml} <!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent">]> <rss version="0.92" xml:base="http://limb-project.com"> <channel> <title>YourDomain.com last news</title> <link>http://YourDomain.com</link> <description>YourDomain.com site</description> <language>en</language> <grid:LIST id='last_news'> <grid:ITERATOR> <item> <title>{$title}</title> <link>{$path}</link> <description> <core:HTMLSPECIALCHARS hash_id='annotation'> </description> <pubDate> <locale:DATE_FORMAT hash_id='modified_date' locale_format='short_date_time' type='stamp'> </pubDate> </item> </grid:ITERATOR> </grid:LIST> </channel> </rss>
Note that we use <core:HTMLSPECIALCHARS> tag in order to sanitize the html content and we use <locale:DATE_FORMAT> tag to display properly formatted date.
Now place the script to PROJECT_DIR/last-news-feed.php and template to PROJECT_DIR/design/main/templates/rss/last_news.rss. Make a neat link to your RSS feed somewhere in the layout, sort like
<a href='/last-news-feed.php'><img src='/design/main/images/xml.gif'>]]
and…that really should be it!
Обсуждение