Welcome to Geeklog, Anonymous Thursday, December 26 2024 @ 11:52 am EST

Geeklog Forums

RSS by Author


Status: offline

RickW

Forum User
Full Member
Registered: 01/28/04
Posts: 240
Location:United States
Okay this isn't really a "plugin", but it's more than a hack. Using some of the users.php code and bits from here and there (and some stuff I had to figure out myself), I have come up with a single file php script that generates (in realtime) a RSS 2.0 xml file for the Author specified.

Here is the code:

Text Formatted Code

<?php

// rss.php
// May 11, 2005 Rick Westmoreland
//
// Code borrowed from (geeklog):
// $Id: users.php,v 1.93 2004/10/23 10:23:01 dhaun Exp $
//
// Original code by:
// @author   Tony Bibbs <tony AT tonybibbs DOT com>
// @author   Mark Limburg <mlimburg AT users DOT sourceforge DOT net>
// @author   Jason Whittenburg
//

require_once ('lib-common.php');


function userarticles ($user)
{
    global $_CONF, $_TABLES, $_USER;

    $result = DB_query("SELECT username,fullname FROM {$_TABLES['userinfo']},{$_TABLES["users"]} WHERE {$_TABLES['userinfo']}.uid = {$_TABLES['users']}.uid AND {$_TABLES['users']}.uid = $user");
    $nrows = DB_numRows($result);
    if ($nrows == 0) { // no such user
        return COM_refresh ($_CONF['site_url'] . '/');
    }
    $A = DB_fetchArray($result);


    header ("Content-Type: application/xml");
    $displayRSS = '';
    //
    // change this for number of articles
    //
    $limit = '15';


    $displayRSS .= '<rss version="2.0">
';
    $displayRSS .= '    <channel>
';

    $result = DB_query ("SELECT tid FROM {$_TABLES['topics']}"
            . COM_getPermSQL ());
    $nrows = DB_numRows ($result);
    $tids = array ();
    for ($i = 0; $i < $nrows; $i++) {
        $T = DB_fetchArray ($result);
        $tids[] = $T['tid'];
    }
    $topics = "'" . implode ("','", $tids) . "'";


    // list of last 10 stories by this user (RSS)
    if (sizeof ($tids) > 0) {
        $sql = "SELECT sid,tid,title,introtext,UNIX_TIMESTAMP(date) AS unixdate FROM {$_TABLES['stories']} WHERE (uid = $user) AND (draft_flag = 0) AND (date <= NOW()) AND (tid IN ($topics))" . COM_getPermSQL ('AND');
        $sql .= " ORDER BY unixdate DESC LIMIT " . $limit;
        $result = DB_query($sql);
        $nrows = DB_numRows($result);
    } else {
        $nrows = 0;
    }

    $displayRSS .= '        <title>' . $_CONF['site_name'] . ' Articles by ' . $A['fullname'] . '</title>
';
    $displayRSS .= '        <link>' . $_CONF['site_url'] . '</link>
';
    $displayRSS .= '        <description>Last ' . $nrows . ' Articles written by ' . $A['fullname'] . '</description>
';
    $displayRSS .= '        <language>' . $_CONF['rdf_language'] . '</language>
';
    $displayRSS .= '        <generator>Geeklog Author RSS Script</generator>
';


    if ($nrows > 0) {
        for ($i = 1; $i <= $nrows; $i++) {
            $C = DB_fetchArray($result);

            $displayRSS .= '        <item>
';

            $C['title'] = str_replace ('$', '$', $C['title']);
            $displayRSS .= '            <title>' . stripslashes($C['title']) . '</title>
';

            $articleUrl = COM_buildUrl ($_CONF['site_url'] . '/article.php?story=' . $C['sid']);
            $displayRSS .= '            <link>' . $articleUrl . '</link>
';

            $introtext = stripslashes($C['introtext']);
            $introtext = trim($introtext);
            $introtext = preg_replace('/(\015)/', '', $introtext);
            $breaks = array('<br>', '<br/>', '', '</p>');
            $introtext = str_replace($breaks, ' ', $introtext);
            $introtext = strip_tags($introtext);
            $introtext = htmlspecialchars($introtext, ENT_QUOTES);
            $displayRSS .= '            <description>' . $introtext . '</description>
';

            $displayRSS .= '            <category>' . $C['tid'] . '</category>
';

            $datetime = date(r, $C['unixdate']);
            $displayRSS .= '            <pubDate>' . $datetime . '</pubDate>
';

            $displayRSS .= '            <guid isPermaLink="false">' . $C['sid'] . '</guid>
';

            $displayRSS .= '            <comments>' . $articleUrl . '#comments</comments>
';

            $displayRSS .= '        </item>
';
        }
    }

    $displayRSS .= '    </channel>
';
    $displayRSS .= '</rss>
';


    return $displayRSS;
}


// MAIN

$display = '';


$uid = COM_applyFilter ($HTTP_GET_VARS['uid'], true);

if (is_numeric ($uid) && ($uid > 0)) {
    $display .= userarticles ($uid);
} else {
    $display .= COM_refresh ($_CONF['site_url'] . '/');
}


echo $display;

?>


 


Here is an example of the coding running:

http://www.antisource.com/rss.php?uid=2

What I have also done, is add a link to the author-specific rss in the users/profile template:

Text Formatted Code

<a href="{site_url}/rss.php?uid={user_id}">RSS Feed for {username}</a>


 


I tried to keep it as site-independent as possible so you guys/gals could use it too.
www.antisource.com
 Quote

Status: offline

ronack

Forum User
Full Member
Registered: 05/27/03
Posts: 612
Very nice, I changed your profile code a bit though to this.
Text Formatted Code
 <tr valign="top">
        <td align="right"><b>RSS Feed for {username}:</b></td>
        <td><a href="{site_url}/rss.php?uid={user_id}"><img src="{site_url}/images/xmlicon.gif" border=0></a></td>
        </tr>


 

Of course you'll have to download the XML Icon if you don't already have it.
 Quote

Status: offline

RickW

Forum User
Full Member
Registered: 01/28/04
Posts: 240
Location:United States
Very nice - I might do the same in the near future...

If anybody else uses this, please post a link so I can check it out. Smile

www.antisource.com
 Quote

Status: offline

ronack

Forum User
Full Member
Registered: 05/27/03
Posts: 612
I ran across a bug.

An invalid character was found in text content. Error processing resource 'http://www.nc-emt.com/rss.php?uid=2'. Line 38, ...

take a look
 Quote

Status: offline

ronack

Forum User
Full Member
Registered: 05/27/03
Posts: 612
Maybe it doesn't like the image?
 Quote

Status: offline

RickW

Forum User
Full Member
Registered: 01/28/04
Posts: 240
Location:United States
Oops, forgot to mention...

The feed I'm generating is RSS version 2.0, which the specifications explain needs to have a character encoding of UTF-8. I ran into the same problem when testing it out myself. Geeklog (and I think php) by default uses a character encoding of ISO-8859-1, so when you write your articles it's possible to save non-conventional characters into the database. There are two ways around this - you can either set your character encoding to UTF-8 in geeklog, and replace those unrecognized characters (in my case someone had contributed an article that used a different kind of apostrophe), or you can declare a different character encoding in the feed:

Text Formatted Code

    header ("Content-Type: application/xml; charset=iso-8859-1");


 


Although the feed still validates, it isn't recommended because some readers might not recognize the character encoding.

I tried to use iconv to convert the default character encoding to UTF-8 and just transliterate the unrecognized characters, but my host does not have the iconv module installed.
www.antisource.com
 Quote

Status: offline

ronack

Forum User
Full Member
Registered: 05/27/03
Posts: 612
Ok I'll experiment a bit with it.
 Quote

Status: offline

ronack

Forum User
Full Member
Registered: 05/27/03
Posts: 612
Yeah unfortunately UTF-8 didn't work for me, I do have many special characters like copyright c and the r and the tm. And there was another one that I ran across.
 Quote

Status: offline

ronack

Forum User
Full Member
Registered: 05/27/03
Posts: 612
I'm not sure I want all of the story in the description. Either need title only or a very brief description.
 Quote

All times are EST. The time is now 11:52 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content