Gossamer Forum
Home : Products : Gossamer Links : Discussions :

MyLinks.cgi for NG

Quote Reply
MyLinks.cgi for NG
OK...I used Jerry's code for MyLinks for Links SQL 1.13 and have tried to modify it to work with NG.

Can Jerry/Pugdog/Anthro or other perl "gurus" please take a look at this code and try to erradicate afew of the errors. I have done "perl mylinks.cgi" when I was logged into my SSH account and it produced no syntax errors or any other errors, but when I execute the script is gives me an internal server error so the code must be incorrect still to work with NG.

The code is likely to be totally wrong but I am trying my best :)

Here is the code I have so far:

#!/usr/bin/perl


# Load required modules.
# ---------------------------------------------------
use strict;
use CGI ();
use CGI::Carp qw/fatalsToBrowser/;
use lib '/MY/FULL/PATH/TO/ADMIN/admin';
use Links;
use Links::SQL;
use Links::Tools;
use Links::SiteHTML;
use Links::Link;
use vars qw!$DB $USER $IN!;

&main ();

sub main {
# --------------------------------------------------------------
# Determine what to do.
#
my $in = new CGI;
my $dynamic = $in->param('d') ? $in : undef;

$DB = new Links::SQL "$Links::CFG->{admin_root_path}/defs/Links.def";

my $s = $in->param('s') || $in->cookie('s');

$USER->{Referer} = $ENV{HTTP_REFERER};

print $in->header ();
if ($in->param('add')) {
&add ($in, $dynamic);
}
elsif ($in->param('delete')) {
&delete ($in, $dynamic);
}
else {
&display ($in, $dynamic);
}
}


sub add {
# --------------------------------------------------------
# Adds an ID into the users links.
#
my ($in, $dynamic) = @_;
my $id = $in->param('add');
my @c = split /\|/, $USER->{MyLinks};
my $args = $IN->get_hash;


# Make sure we have a valid link.
my $rec = $DB->get_record ($id, $args);
$rec or &site_html_my_add_error ({ error => "Unkown Link ID: '$id'." }, $dynamic) and return;

# Check to see if the ID is in the database.
my %c = map { $_ => 1 } @c;
$c{$id} and &site_html_my_add_error ({ error => "Link Already Added '$id'." }, $dynamic) and return;

# Update the database.
push (@c, $id);
@c = sort { $a <=> $b } @c;
$" = $Links::CFG->{MYLINKS_DELIM};
$DB->do ("UPDATE Users SET MyLinks = '@c' WHERE Username = '$$USER{Username}'");

# Display the add page.
&site_html_my_add ($rec, $dynamic);
}

sub delete {
# --------------------------------------------------------
# Deletes an ID from the users cookie.
#
my ($in, $dynamic) = @_;
my $id = $in->param('delete');
my @c = split /\|/, $USER->{MyLinks};
my $args = $IN->get_hash;


if ($id =~ /all/i) {
$DB->do ("UPDATE Users SET MyLinks = '' WHERE Username = '$$USER{Username}'");
&site_html_my_delete_all ($USER, $dynamic);
return;
}

# Make sure we have a valid ID.
my $rec = $DB->get_record ($id, $args);
$rec or &site_html_my_delete_error ({ error => "Unkown Link ID: '$id'." }, $dynamic) and return;

# Check to see if the ID is in the database.
my %c = map { $_ => 1 } @c;
$c{$id} or &site_html_my_delete_error ({ error => "Link Not in Database '$id'." }, $dynamic) and return;

# Update the database.
@c = grep {!/^$id$/} @c;
$" = $Links::CFG->{MYLINKS_DELIM};
$DB->do ("UPDATE Users SET MyLinks = '@c' WHERE Username = '$$USER{Username}'");

# Display the delete page.
&site_html_my_delete ($rec, $dynamic);
}

sub display {
my ($in, $dynamic) = @_;
my @c = split /\|/, $USER->{MyLinks};

# Get total of links.
$USER->{Total} = @c || 0;
if (@c) {
$USER->{Pages} = int $$USER{Total}/$Links::CFG->{MYLINKS_PERPAGE} (($$USER{Total} % $Links::CFG->{MYLINKS_PERPAGE}) ? 1 : 0);
$USER->{Page} = $in->param('p') || 1;
$USER->{Page} = (($$USER{Page} > $$USER{Pages}) ? 1 : $$USER{Page});
$USER->{Start} = ($$USER{Page}-1) * $Links::CFG->{MYLINKS_PERPAGE};
$USER->{Finish} = $USER->{Start};

# Build span pages.
if ($USER->{Pages} > 1) {
my $prev = $USER->{Page} - 1;
my $next = $USER->{Page} 1;

if ($$USER{Page} > 1) {
$USER->{Span} = qq~<a href="$ENV{SCRIPT_NAME}~;
$USER->{Span} .= qq~?p=$prev~ if ($prev > 1);
$USER->{Span} .= qq~"><b>< Previous</b></a> <a href="$ENV{SCRIPT_NAME}">1</a> ~;
if ($prev > 1) {
for (2 .. $prev) { $USER->{Span} .= qq~<a href="$ENV{SCRIPT_NAME}?p=$_">$_</a> ~; }
}
}
else { $USER->{Span} = qq~< Previous ~; }
$USER->{Span} .= qq~<b>$$USER{Page}</b> ~;
if ($$USER{Page} < $$USER{Pages}) {
for ($next .. $$USER{Pages}) { $USER->{Span} .= qq~<a href="$ENV{SCRIPT_NAME}?p=$_">$_</a> ~; }
$USER->{Span} .= qq~<a href="$ENV{SCRIPT_NAME}?p=$next"><b>Next ></b></a>~;
}
else { $USER->{Span} .= qq~Next >~; }
}

# Get the links to display.
$" = ",";
my $sth = $DB->prepare ("SELECT * FROM Links WHERE ID IN (@c) ORDER BY $Links::CFG->{MYLINKS_ORDER} LIMIT $$USER{Start}, $Links::CFG->{MYLINKS_PERPAGE}");
$sth->execute() or die $DBI::errstr;
while (my $rec = $sth->fetchrow_hashref) {
$USER->{Links} .= &site_html_my_link ($rec, $dynamic);
$USER->{Finish} ;
}
$USER->{Start} ;
}

# Display the home page.
&site_html_my_home ($USER, $dynamic);
}


I have got all the templates in my default templates directory and have added the following code for each template into SiteHTML.pm...

sub site_html_my_home {
# --------------------------------------------------------
# Return parsed category page.
#
my $tags = shift;

my $output = Links::load_template ('my_home.html', $tags, 1);
return $output;
}


(Not sure if im meant to to that or not)[/red]

I would be really grateful if someone could tell me what ive done wrong (If youve got a few hours spare to fix my code)...lol

This bit is bothering me $" = $Links::CFG->{MYLINKS_DELIM}; and this bit $Links::CFG->{MYLINKS_ORDER};...oh and also....$Links::CFG->{MYLINKS_PERPAGE};.

Do I have to add some code to /Links/Config.pm or /Links/ConfigData.pm ???

THANKS

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: MyLinks.cgi for NG In reply to
Oh...by the way...Jerry used...

use Links::DB_Utils;

...but I couldnt find that file with NG so I used...

use Links::Tools;

...so that will be wrong too I guess!

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: MyLinks.cgi for NG In reply to
You don't need to "use DB_Utils" since once the $DB object is intialized, the access methods to it exist. Look at how the other scripts -- jump.cgi and add.cgi and search.cgi handle it.

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: MyLinks.cgi for NG In reply to
Thanks Pugdog....I had a good look through Search.cgi and thats how I came up with...

my $args = $IN->HASH; (Not sure if that is correct)

Jerry used ($id, 'HASH')...so I switched it to ($id, $args);


Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: MyLinks.cgi for NG In reply to
You can access $IN directly $IN->param('key'), or you can convert $IN to a hash via:

$IN->get_hash

that returns a hash_ref



PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: MyLinks.cgi for NG In reply to
I used...

my $args = $IN->get_hash;

and then...

($id, $args)

...in the code in the first post....so you are saying that is correct?




Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: MyLinks.cgi for NG In reply to
Am I calling the templates correctly using...

&site_html_my_home ($USER, $dynamic);


or should that be...

print $IN->header();
print Links::SiteHTML::display ('my_home', $results);

??




Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: MyLinks.cgi for NG In reply to
No,

>> Jerry used ($id, 'HASH')...so I switched it to ($id, $args);

What that is doing is passing a parameter $id and the 'HASH' is telling the subroutine what type of parameter it is ie: 'HASH' rather than 'SCALER' or 'ARRAY'

I didn't see where that was originally used, and in what context.

But, $args returned as above _would_ be a 'HASH'-type since it's a hash_ref

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: MyLinks.cgi for NG In reply to
This is the code I was referring to...
(This is what is being used at the moment)

my $args = $IN->get_hash;


# Make sure we have a valid link.
my $rec = $DB->get_record ($id, $args);
$rec or &site_html_my_add_error ({ error => "Unkown Link ID: '$id'." }, $dynamic) and return;


Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: MyLinks.cgi for NG In reply to
>> # Make sure we have a valid link.
>> my $rec = $DB->get_record ($id, $args);
>> $rec or &site_html_my_add_error ({ error => "Unkown Link ID: '$id'." }, $dynamic) and return;

You just need to do:

$rec = $DB->get ($id);

You don't need to pass in $args. All 'HASH' was doing was requesting that the routine return a hash_ref instead of an array_ref



PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: MyLinks.cgi for NG In reply to
I have a lot to learn!..... :)

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: MyLinks.cgi for NG In reply to
I think I am almost there....

Could you just help me with one more thing...

When I delete all the mylinks templates from the templates directory and go to mylinks.cgi, the Internal server error disappears and it just says "invalid template my_home.html"....which means that the code seems to be working as it is trying to load the homepage.

However when I re-upload the templates, the internal server error comes back and my server log says....

[Wed Jan 3 22:03:29 2001] [error]
[client 152.163.189.73] malformed header from script. Bad header=<html><head><title>MyLinks -> : /home/audio/audio-grabber-www/cgi-bin/mylinks.cgi


This code....

=<html><head><title>MyLinks ->

...is the beginning of the my_home.html template so it seems to be trying to load the template but then encountering an error.

What could be causing this?




Paul Wilson. Shocked
(Dont blame me if I'm wrong!)