Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [MOD] RSS article feed filtered by category for sNews (v.1.6)  (Read 7748 times)

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940

This MOD enables default RSS article feed to be called with filter on specific category and specifying max number of latest articles to be returned. Comes handy when articles from specific quality content categories are meant to be supplied as separate feeds. Results should be handled as any RSS feeds ;)

Updated on July 30, 2007 - r.1.1

0) Backup snews.php and all (or just say something like 'hells bells' if U are true macho)

1) Change original RSS articles feed providing function to support filtering by category and setting max number of articles returned. To achieve this replace beginning of rss_contents function:
Code: [Select]
// RSS FEED - ARTICLES/PAGES/COMMENTS
function rss_contents($rss_item, $artSEF=''){
    header('Content-type: text/xml; charset='.s('charset').'');
    $limit = s('rss_limit');
    switch($rss_item) {
        case 'rss-articles':
            $heading = l('articles');
            $query = "articles WHERE position = 1 AND published = 1 ORDER BY date";
        break;
        case 'rss-pages':
            $heading = l('pages');
            $query = "articles WHERE position = 3 AND published = 1 ORDER BY date";
        break;
        case 'rss-comments':
            $heading = l('comments');
            $artId = retrieve('id','articles','seftitle',$artSEF);
            $articleId = ($artId && is_numeric($artId)) ? "AND articleid = $artId" : '';
            $query = "comments WHERE approved = 'True' $articleId ORDER BY id";
        break;
    }
    $header = '<?xml version="1.0" encoding="'.s('charset').'"?>';
    $header .= '<rss version="2.0">';
    $header .= '<channel>';
    $header .= '<title><![CDATA['.s('website_title').']]></title>';
    $header .= '<description><![CDATA['.$heading.']]></description>';
    $header .= '<link>'.db('website').'</link>';
    $header .= '<copyright><![CDATA[Copyright '.s('website_title').']]></copyright>';
    $header .= '<generator>sNews CMS</generator>';
    $footer = '</channel>';
    $footer .= '</rss>';
    echo $header;
    $result = mysql_query("SELECT * FROM ".db('prefix')."$query DESC LIMIT $limit");
with following updated code:
Code: [Select]
// RSS FEED - ARTICLES/PAGES/COMMENTS
//function rss_contents( $rss_item, $artSEF='', $catSEF=false, $maxRec = 1 ) {
function rss_contents( $rss_item, $artSEF = '', $catSEF = false, $maxRec = false ) {

    header('Content-type: text/xml; charset='.s('charset').'');

    //$limit = s('rss_limit');
    $limit = (int) s('rss_limit');

    $strAscDesc = ' DESC ';
    $strLimit   = ' LIMIT ' . $limit;

    switch($rss_item) {
        case 'rss-articles':
            $heading = l('articles');
            $query = "articles WHERE position = 1 AND published = 1 ORDER BY date";
            if ( $catSEF && is_string($catSEF) ) {
               if ( $catSEF == s('home_sef')) {
                  // Pseudo category 'home'
                  $catId = 0;
               }
               else {
                  $catId = retrieve( 'id', 'categories', 'seftitle', $catSEF );
               }
               if ( $catId >= 0) {
                  $query = "articles WHERE position = 1 AND published = 1 AND category = $catId ORDER BY date DESC, id DESC";

                  $strAscDesc = ''; // Already added directly in query

                  //if ( is_integer($maxRec) && ($maxRec > 0) ) {
                  $maxRecLimit = (int) $maxRec;
                  if ( !( $maxRec === false ) && ( $maxRecLimit > 0 ) ) {

                     // Use $maxRecLimit
                     if ( $maxRecLimit < $limit ) {
                        $strLimit = " LIMIT $maxRecLimit"; // Adjust according to request
                     }
                  }
               }
               else {
                  // Category not found - query that returns no rows
                  $query = "articles WHERE position = 1 AND published = 1 AND category = -1";
                  $strAscDesc = ''; // Not needed
                  $strLimit   = ''; // Not needed
               }
            }
         break;
      case 'rss-pages':
         $heading = l('pages');
         $query = "articles WHERE position = 3 AND published = 1 ORDER BY date";
         break;
      case 'rss-comments':
         $heading = l('comments');
         $artId = retrieve('id','articles','seftitle',$artSEF);
         $articleId = ($artId && is_numeric($artId)) ? "AND articleid = $artId" : '';
         $query = "comments WHERE approved = 'True' $articleId ORDER BY id";
         break;
   }
   $header = '<?xml version="1.0" encoding="'.s('charset').'"?>';
   $header .= '<rss version="2.0">';
   $header .= '<channel>';
   $header .= '<title><![CDATA['.s('website_title').']]></title>';
   $header .= '<description><![CDATA['.$heading.']]></description>';
   $header .= '<link>'.db('website').'</link>';
   $header .= '<copyright><![CDATA[Copyright '.s('website_title').']]></copyright>';
   $header .= '<generator>sNews CMS</generator>';
   $footer = '</channel>';
   $footer .= '</rss>';
   echo $header;
   //$result = mysql_query("SELECT * FROM ".db('prefix')."$query DESC LIMIT $limit");
   $strSql = "SELECT * FROM ".db('prefix')."$query" . $strAscDesc . $strLimit;
   $result = mysql_query( $strSql );
If all works as expected You will be able with updated function to retrieve RSS feed for articles from specific category only with call like this:
Code: [Select]
rss_contents( 'rss-articles', '', '<put-real-category-sef-here>' );or like this:
Code: [Select]
rss_contents( 'rss-articles', '', '<put-real-category-sef-here>', <put-record-limit-here> );Current MOD version should work like this
 - if number of articles isn't specified then system limit for rss is used;
 - if number of articles is specified and is lower than system limit for rss then only specified number of articles will be returned;
 - if specified number of articles is larger than system rss limit then specified number will be ignored.

2) Now, a little bit on how to get it working in whole scheme of sNews 1.6. To enable handling of updated RSS articles feed call change the beginning of startup_snews() from this:
Code: [Select]
function snews_startup() {
connect_to_db();
$categorySEF = get_id('category');
$articleSEF = get_id('article');
if (false !== strpos($categorySEF, 'rss-')) {rss_contents($categorySEF, $articleSEF);}
to look like this one:
Code: [Select]
function snews_startup() {
connect_to_db();
$categorySEF = get_id('category');
$articleSEF = get_id('article');
        $filterCategorySEF = get_id('filter_category');
        $filterMaxRec = get_id('filter_maxrec');
if (false !== strpos($categorySEF, 'rss-')) {rss_contents($categorySEF, $articleSEF, $filterCategorySEF, $filterMaxRec);}
3) And in order to enable handling of newly introduced parameters change stock get_id() from this:
Code: [Select]
function get_id($parameter) {
$url = array();
$url = explode('/', clean(cleanXSS($_GET['category'])));
  $get_id = array();
$get_id['category'] = clean(cleanXSS($url['0']));
if (isset($url['1'])) {$get_id['article'] = clean(cleanXSS($url['1']));}
if (isset($url['2'])) {$get_id['commentspage'] = clean(cleanXSS($url['2']));}
if (isset($get_id[$parameter])) {return clean(cleanXSS($get_id[$parameter]));}
}
to look like this one:
Code: [Select]
function get_id($parameter) {
$url = array();
$url = explode('/', clean(cleanXSS($_GET['category'])));
  $get_id = array();
$get_id['category'] = clean(cleanXSS($url['0']));
if (isset($url['1'])) {$get_id['article'] = clean(cleanXSS($url['1']));}
if (isset($url['1'])) {$get_id['filter_category'] = clean(cleanXSS($url['1']));}
if (isset($url['2'])) {$get_id['commentspage'] = clean(cleanXSS($url['2']));}
if (isset($url['2'])) {$get_id['filter_maxrec'] = clean(cleanXSS($url['2']));}
if (isset($get_id[$parameter])) {return clean(cleanXSS($get_id[$parameter]));}
}
4) After all steps described above that RSS article feed for particular category should be accessible like this (will return just single record if any present):
Code: [Select]
/rss-articles/<put-real-category-sef-here>or like this (will return last three articles if present):
Code: [Select]
/rss-articles/<put-real-category-sef-here>/3Credits:
 - TheTick for bringing issue up and testing this MOD
 - agentsmith for testing WIP material
 - sibas for facilitating bug-hunt

History:
 - r.1.1 - 2007.07.30 - fixed bug in record limit handling
 - r.1.0 - 2007.07.12 - allowed filtering by category

Rights
Modification code (c) 2007 Valdis Ozols aka codetwist
Included 3rd parties code is (c) their respective authors.
Released under Creative Commons Attribution.

P.S. Here (link broken for now) is thread that initiated this MOD.
« Last Edit: September 29, 2007, 03:57:47 pm by codetwist »
Logged

TheTick

  • Full Member
  • ***
  • Karma: 6
  • Posts: 105
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #1 on: July 12, 2007, 03:56:23 pm »

Great!
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #2 on: July 26, 2007, 03:42:37 pm »

Hi codetwist
I try this mod in two different snews to make it work but I don’t have any luck
The first in normal snews1.6 and the other in snews enhanced
If I understand correctly when I type http://mysite/rss-articles/category-sef
display rss from that category?
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #3 on: July 26, 2007, 04:21:08 pm »

MOD is described against codebase of original sNews 1.6, should work fine. By default it returns single record only unless more are directly requested. If I understood right it works just fine for TheTick.

In enhanced (keyrocks package I presume) version the modded get_id() function is used; that should be updated to gather parameters needed for this extension.

Your calling example is right if You have category with following sef title value :   category-sef    ;)
Logged

TheTick

  • Full Member
  • ***
  • Karma: 6
  • Posts: 105
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #4 on: July 26, 2007, 04:25:04 pm »

Yeah I use it with a very slightly modified sNews 1.6 install (byline, database BU and Body ID mods) &  and it works great for me...
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #5 on: July 26, 2007, 05:20:16 pm »

you can see in http://i-am.gr
is enhanced snews by keyrocks and i have change only  your instruction above,
so the  get_id() function i change it to what you write
(i don't know if is anything else that i must change in enhanced snews)

and for "normal" snews with small modification's i can't make it work

in my site i have only 2 category's "welcome" and "test"
when i go to http://i-am.gr/rss-articles/test or
http://i-am.gr/rss-articles/welcome
i can't see rss from those category's

any idea?
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #6 on: July 26, 2007, 05:36:40 pm »

I haven't looked into enhanced snews source yet; so, I don't think I can help You much right now. And this MOD  instructions are for changing of original sNews 1.6 code. For example, if enhanced version uses mods that changes either snews_startup(), get_id() or rss_contents() then these should be adapted as well.
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #7 on: July 26, 2007, 07:32:32 pm »

Thank you for your help codetwist  
 I thing this mod is one of the most important mod,
with RSS you can sent far and away your site, blog, whatever..

I install a new snews1.6 from beginning, http://i-am.gr/sNews16/
but still I can’t make it work, I don’t see any error so I don’t know what else to do

here is snews1.6 with your instructions
Code: [Select]
// STARTUP
function snews_startup() {
    connect_to_db();
    $categorySEF = get_id('category');
    $articleSEF = get_id('article');
        $filterCategorySEF = get_id('filter_category');
        $filterMaxRec = get_id('filter_maxrec');
    if (false !== strpos($categorySEF, 'rss-')) {rss_contents($categorySEF, $articleSEF, $filterCategorySEF, $filterMaxRec);}
$homeSEF = l('home_sef');
$categoryID = $categorySEF == $homeSEF ? 0 : retrieve('id', 'categories', 'seftitle', $categorySEF);
$articleCatID = retrieve('category', 'articles', 'seftitle', $articleSEF);
if (!empty($categorySEF) && $categorySEF != '404') {
switch(true) {
case ((!$categoryID || !is_numeric($categoryID)) && check_category($categorySEF) == false):
case (!empty($articleSEF) && false === strpos($articleSEF,l('paginator')) && !is_numeric($articleCatID)):
header('Location: '.db('website').'404/'); exit;
    break;



// RSS FEED - ARTICLES/PAGES/COMMENTS
function rss_contents( $rss_item, $artSEF='', $catSEF=false, $maxRec = 1 ) {
    header('Content-type: text/xml; charset='.s('charset').'');
    $limit = s('rss_limit');
    switch($rss_item) {
        case 'rss-articles':
            $heading = l('articles');
            $query = "articles WHERE position = 1 AND published = 1 ORDER BY date";
            if ( $catSEF && is_string($catSEF) ) {
               if ( $catSEF == s('home_sef')) {
                  // Pseudo category 'home'
                  $catId = 0;
               }
               else {
                  $catId = retrieve( 'id', 'categories', 'seftitle', $catSEF );
               }
               if ( $catId >= 0) {
                  $query = "articles WHERE position = 1 AND published = 1 AND category = $catId ORDER BY date DESC, id DESC";
                  if ( is_integer($maxRec) && ($maxRec > 0) ) {
                     // Use $maxRec limit
                     $query .= " LIMIT $maxRec";
                  }
               }
               else {
                  // Category not found - query that returns no rows
                  $query = "articles WHERE position = 1 AND published = 1 AND category = -1";
               }
            }
        break;
case 'rss-pages':
$heading = l('pages');
$query = "articles WHERE position = 3 AND published = 1 ORDER BY date";
break;
case 'rss-comments':
$heading = l('comments');
$artId = retrieve('id','articles','seftitle',$artSEF);
$articleId = ($artId && is_numeric($artId)) ? "AND articleid = $artId" : '';
$query = "comments WHERE approved = 'True' $articleId ORDER BY id";
break;
....

// GET ID
function get_id($parameter) {
    $url = array();
    $url = explode('/', clean(cleanXSS($_GET['category'])));
      $get_id = array();
    $get_id['category'] = clean(cleanXSS($url['0']));
    if (isset($url['1'])) {$get_id['article'] = clean(cleanXSS($url['1']));}
    if (isset($url['1'])) {$get_id['filter_category'] = clean(cleanXSS($url['1']));}
    if (isset($url['2'])) {$get_id['commentspage'] = clean(cleanXSS($url['2']));}
    if (isset($url['2'])) {$get_id['filter_maxrec'] = clean(cleanXSS($url['2']));}
    if (isset($get_id[$parameter])) {return clean(cleanXSS($get_id[$parameter]));}
}

//CATEGORY CHECK
I really like the idea to have RSS for its category! :D
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #8 on: July 26, 2007, 08:59:47 pm »

@ sibas : Ok, there might be problems with it. I updated code changes for step (1) and now it should work like this - if number of articles isn't specified then system limit for rss is used; - if number of articles is specified and is lower than system limit for rss then only specified number of articles will be returned; - specified number larger than system rss limit will be ignored. Code follows, try it out, please ;)

Updated step (1)

Replace original code:
Code: [Select]
// RSS FEED - ARTICLES/PAGES/COMMENTS
function rss_contents($rss_item, $artSEF=''){
header('Content-type: text/xml; charset='.s('charset').'');
$limit = s('rss_limit');
switch($rss_item) {
case 'rss-articles':
$heading = l('articles');
$query = "articles WHERE position = 1 AND published = 1 ORDER BY date";
break;
case 'rss-pages':
$heading = l('pages');
$query = "articles WHERE position = 3 AND published = 1 ORDER BY date";
break;
case 'rss-comments':
$heading = l('comments');
$artId = retrieve('id','articles','seftitle',$artSEF);
$articleId = ($artId && is_numeric($artId)) ? "AND articleid = $artId" : '';
$query = "comments WHERE approved = 'True' $articleId ORDER BY id";
break;
}
$header = '';
$header .= '';
    $header .= '';
    $header .= '<![CDATA['.s('website_title').']]>';
    $header .= '';
    $header .= ''.db('website').'';
    $header .= '';
    $header .= 'sNews CMS';
    $footer = '
';
    $footer .= '
';
echo $header;
$result = mysql_query("SELECT * FROM ".db('prefix')."$query DESC LIMIT $limit");
with following updated one:
Code: [Select]
// RSS FEED - ARTICLES/PAGES/COMMENTS
//function rss_contents( $rss_item, $artSEF='', $catSEF=false, $maxRec = 1 ) {
function rss_contents( $rss_item, $artSEF = '', $catSEF = false, $maxRec = false ) {

    header('Content-type: text/xml; charset='.s('charset').'');

    //$limit = s('rss_limit');
    $limit = (int) s('rss_limit');

    $strAscDesc = ' DESC ';
    $strLimit   = ' LIMIT ' . $limit;

    switch($rss_item) {
        case 'rss-articles':
            $heading = l('articles');
            $query = "articles WHERE position = 1 AND published = 1 ORDER BY date";
            if ( $catSEF && is_string($catSEF) ) {
               if ( $catSEF == s('home_sef')) {
                  // Pseudo category 'home'
                  $catId = 0;
               }
               else {
                  $catId = retrieve( 'id', 'categories', 'seftitle', $catSEF );
               }
               if ( $catId >= 0) {
                  $query = "articles WHERE position = 1 AND published = 1 AND category = $catId ORDER BY date DESC, id DESC";

                  $strAscDesc = ''; // Already added directly in query

                  if ( is_integer($maxRec) && ($maxRec > 0) ) {
                     // Use $maxRec limit
                     //$query .= " LIMIT $maxRec";
                     if ( $maxRec < $limit ) {
                        $strLimit = " LIMIT $maxRec"; // Adjust according to request
                     }
                  }
               }
               else {
                  // Category not found - query that returns no rows
                  $query = "articles WHERE position = 1 AND published = 1 AND category = -1";
                  $strAscDesc = ''; // Not needed
                  $strLimit   = ''; // Not needed
               }
            }
         break;
      case 'rss-pages':
         $heading = l('pages');
         $query = "articles WHERE position = 3 AND published = 1 ORDER BY date";
         break;
      case 'rss-comments':
         $heading = l('comments');
         $artId = retrieve('id','articles','seftitle',$artSEF);
         $articleId = ($artId && is_numeric($artId)) ? "AND articleid = $artId" : '';
         $query = "comments WHERE approved = 'True' $articleId ORDER BY id";
         break;
   }
   $header = '';
   $header .= '';
   $header .= '';
   $header .= '<![CDATA['.s('website_title').']]>';
   $header .= '';
   $header .= ''.db('website').'';
   $header .= '';
   $header .= 'sNews CMS';
   $footer = '
';
   $footer .= '
';
   echo $header;
   //$result = mysql_query("SELECT * FROM ".db('prefix')."$query DESC LIMIT $limit");
   $strSql = "SELECT * FROM ".db('prefix')."$query" . $strAscDesc . $strLimit;
   $result = mysql_query( $strSql );
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #9 on: July 26, 2007, 11:01:54 pm »

Is working  :D
Thank you codetwist, is working and for enhanced snews.

Quote from: codetwist
@ sibas : Ok, there might be problems with it. I updated code changes for step (1) and now it should work like this - if number of articles isn't specified then system limit for rss is used; - if number of articles is specified and is lower than system limit for rss then only specified number of articles will be returned; - specified number larger than system rss limit will be ignored.
I lost you here

in http://i-am.gr/sNews16/
i have 3 articles in main,
in  category1 i have 3 articles
and when i try http://i-am.gr/sNews16/rss-articles/category1
i see RSS from this category
but when i try
http://i-am.gr/sNews16/rss-articles/category1/1
shows the same.

Is just fine for me like this, but if you can fix and the numbers i gonna make a big statue to my site for you and the rest dudes :D
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #10 on: July 26, 2007, 11:45:45 pm »

Regarding numbers try to replace:
Code: [Select]
                 if ( is_integer($maxRec) && ($maxRec > 0) ) {

                     // Use $maxRec limit
                     //$query .= " LIMIT $maxRec";
                     if ( $maxRec < $limit ) {
                        $strLimit = " LIMIT $maxRec"; // Adjust according to request
                     }
                  }
with this:
Code: [Select]
                 //if ( is_integer($maxRec) && ($maxRec > 0) ) {
                  $maxRecLimit = (int) $maxRec;
                  if ( !( $maxRec === false ) && ( $maxRecLimit > 0 ) ) {

                     // Use $maxRecLimit
                     if ( $maxRecLimit < $limit ) {
                        $strLimit = " LIMIT $maxRecLimit"; // Adjust according to request
                     }
                  }
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #11 on: July 27, 2007, 01:10:13 pm »

Thank YOU a lot codetwist
is working just fine  :D
i let you know what i make when i finish my site
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
[MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #12 on: July 30, 2007, 10:22:44 pm »

Updated MOD post with latest code fixes ;)
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: [MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #13 on: August 29, 2008, 09:46:21 am »

oh oh... that Mod is a great one.
I'm gonna try it this week end.
Does somebody still use it?
« Last Edit: August 29, 2008, 09:51:32 am by Sven »
Logged

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Re: [MOD] RSS article feed filtered by category for sNews (v.1.6)
« Reply #14 on: January 25, 2010, 11:32:43 am »

Mhm, this is something I would think should be default... Is there anyone having tested this on 1.7 ? I would like this in 1.7
Logged
"It's only dead fish that goes with the flow... "
Updated