Jump to content

User:Sysy~metawiki/Scott Elcomb's RSS hack

From Meta, a Wikimedia project coordination wiki

Cached from http://www.gtalug.org/wiki/User:Dl33t

Please note that in order to authenticate, the users' name and password need to be sent to the script in plaintext... Using HTTPS (SSL, GET or POST) to access the feed generator is highly recommended.

Interrupted during Customizable MediaWiki/RSS Engine test - Temp Storage - Current time is 28minutes

[edit]
#!/usr/bin/perl
                                                                                                                             
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use SAL::DBI;
                                                                                                                             
# // Configuration
my $mediawiki_name = 'MyWiki';
my $mediawiki_url = 'http://localhost/mediawiki/index.php';
my $mediawiki_description = 'This is MyWiki';
                                                                                                                             
# // Get parameters
my $q = new CGI;
my $self_url = $q->script_name();
my $user = $q->param('u');
my $pass = $q->param('p');
                                                                                                                             
# // Setup database interface
my $dbo_factory = new SAL::DBI;
my $dbo_data = $dbo_factory->spawn_mysql('localhost','wikidbuser','wikidbpass','wikidb');
my $dbo_data2 = $dbo_factory->spawn_mysql('localhost','wikidbuser','wikidbpass','wikidb');
                                                                                                                             
# // NOTE:  SAL stores SQL datasets as 2D arrays, accessible like so: $dbo_data->{data}->[$recordNum][$fieldNum]
                                                                                                                             
# // Authenticate the user and password
my $SQL_auth_query = qq[SELECT count(user_id) FROM user WHERE (user_name='$user') AND (user_password = md5(concat(user_id,'-',md5('$pass'))))];
                                                                                                                             
$dbo_data->execute($SQL_auth_query);
if ($dbo_data->{data}->[0][0] != 1) { die('Not Authorized!'); }
                                                                                                                             
# // Get the user's watchlist
my $SQL_watchlist_query = qq[SELECT a.wl_title, a.wl_namespace FROM watchlist AS a, user AS b WHERE (a.wl_user=b.user_id) AND (b.user_name='$user')];
                                                                                                                             
# // NOTE:  The SAL execute method returns the width and height of the dataset...
my ($w, $h) = $dbo_data->execute(qq[$SQL_watchlist_query]);
                                                                                                                             
 
# // Start the RSS document
my $canvas = qq[<rss version="0.91">
  <channel>
    <title>$mediawiki_name</title>
    <link>$mediawiki_url</link>
    <description>$mediawiki_description</description>
    <language>en-us</language>
];
 
# // Loop through the items
foreach my $i (0 .. $h) {
        my $mediawiki_item_title = $dbo_data->{data}->[$i][0];
        my $mediawiki_item_namespace = $dbo_data->{data}->[$i][1];
        my $SQL_item_query = '';
 
        if ($mediawiki_item_namespace == 0) {
                my $mediawiki_item_link = $mediawiki_url . '/' . $mediawiki_item_title;
 
                $SQL_item_query = qq[SELECT a.old_text FROM text AS a, page AS b WHERE (a.old_id = b.page_latest) AND (b.page_title='$mediawiki_item_title')];
 
                $dbo_data2->execute($SQL_item_query);
 
                my $mediawiki_item_description = $dbo_data2->{data}[0][0];
 
                $canvas .= qq[    <item>
      <title>$mediawiki_item_title</title>
      <link>$mediawiki_item_link</link>
      <description>$mediawiki_item_description</description>
    </item>
];
        }
}
 
# // Close the RSS document
$canvas .= qq[  </channel>
</rss>
];
 
# // And send it to the client
print "Content-type: text/xml\n\n" . $canvas;

Output:

<rss version="0.91">
  <channel>
    <title>MyWiki</title>
    <link>http://localhost/mediawiki/index.php</link>
    <description>This is MyWiki</description>
    <language>en-us</language>
    <item>
      <title>Main_Page</title>
      <link>http://localhost/mediawiki/index.php/Main_Page</link>
      <description>Wiki software successfully installed.
 
Please see [http://meta.wikipedia.org/wiki/MediaWiki_i18n documentation on customizing the interface]
and the [http://meta.wikipedia.org/wiki/MediaWiki_User%27s_Guide User's Guide] for usage and configuration help.
 
Yay!
 
Adding Edits</description>
    </item>
    <item>
      <title>Test_Page</title>
      <link>http://localhost/mediawiki/index.php/Test_Page</link>
      <description>This is a test
Now watching...</description>
    </item>
    <item>
      <title></title>
      <link>http://localhost/mediawiki/index.php/</link>
      <description></description>
    </item>
  </channel>
</rss>