Gossamer Forum
Home : Products : Links 2.0 : Customization :

External Text File Mod

Quote Reply
External Text File Mod
Cool
I have spent the last couple of weeks working on this, as I could not find anything similar that worked for Links. Using this mod will let you have lots of text for each link, and not have that text fill up your link.db file. How you use this is up to you; I developed it while converting Links2 into a Content Management System (CMS).

This mod does not require any changes to links.def, and no additions to links.db. It uses the link ID number as the name of the text file (ie: 35.txt). You can add or modify the text from the Admin page, and also using add.cgi and modify.cgi. When a user submits a new or modified link, the text input from the form is saved in a temprary file, awaiting your validation. When you validate a submission, the changes are made to the text file, and the temporary file is deleted. Also, if you delete a link from the Admin, the associated text file will be deleted.

Most of the intelligence behind this mod came from JPDeni, who made it for DBMan. I took that code and extended it to work with Links2. Using a <%text%> tag in the templates will present the contents of the text file as interpreted by the browser, so HTML will render correctly.

I spent a lot of time testing this, and it all works as it should. A few minor improvements can still be made, but I wanted to get this out so others could experiment with it. If you use it, please let me know how it works for you, and any suggestions for improvement. It is fully commented, so you can tell what bit of code does what.
Wink


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] External Text File Mod In reply to
Encountered problems when the text file contained a textarea: Links Admin and templates converts </textarea> to </text-area>, and not doing so to the text file would break the input before the files' </textarea> tag. I believe I fixed it using a simple regex, and the updated file is attached.

The changes are marked with...
# Fix the textarea display


Leonard
aka PerlFlunkie

Last edited by:

PerlFlunkie: Sep 15, 2006, 5:38 PM
Quote Reply
Re: [PerlFlunkie] External Text File Mod In reply to
Another correction, to make sure we only look for a text file for links, not categories. Note, this is NOT corrected on the previous attachments.

Also, this note is from the mod made by JPDeni:


Note that you will not be able to search for text within the .txt file, but only for text that is in the .db file itself. I would suggest that you add a field to your database for keywords that people might search for.


Code:
sub build_html_record_form {
# --------------------------------------------------------
# Builds a record form based on the config information.
#
my ($output, $field, $multiple, $name);
($_[0] eq "multiple") and ($multiple = 1) and shift;
my (%rec) = @_;

# >>> External Text File mod (next 26 lines)
if ($in{'db'} eq 'links') { # Make sure we're editing links, and not categories.
# If validating input from modify.cgi, get the temporary text file from the /text/modified directory.
if ($in{'validate_form'}) {
my $text_file = "$external_modified_text_dir/$rec{$db_key}.txt";
if (-e $text_file) { # Check if there's a file in the /text/modified directory...
open (TEXT, "<$external_modified_text_dir/$rec{$db_key}.txt") or &cgierr("Error in db_utils.pl, sub build_html_record_form (A). Unable to open text file $external_modified_text_dir/$rec{$db_key}.txt\nReason: $!");
@text = <TEXT>;
close TEXT;
$rec{'text'} = join "",@text;
}
# If validating input from add.cgi, get the new text file from the /text/validate directory.
else { # ...if not, it must be in the /text/validate directory.
open (TEXT, "<$external_validate_text_dir/$rec{$db_key}.txt") or &cgierr("Error in db_utils.pl, sub build_html_record_form (B). Unable to open text file $external_modified_text_dir/$rec{$db_key}.txt\nReason: $!");
@text = <TEXT>;
close TEXT;
$rec{'text'} = join "",@text;
}
}
# If modifying from the admin, get the current text file.
if ($in{'modify_form'}) {
open (TEXT, "<$external_text_dir/$rec{$db_key}.txt") or &cgierr("Error in db_utils.pl, sub build_html_record_form (C). Unable to open text file $external_text_dir/$rec{$db_key}.txt\nReason: $!");
@text = <TEXT>;
close TEXT;
$rec{'text'} = join "",@text;
}
}
# <<<
$output = "<p><table border=1>";
# Go through a little hoops to only load category list when absolutely neccessary.
if ($in{'db'} eq 'links') {
exists $db_select_fields{$db_cols[$db_category]}
or ($db_select_fields{$db_cols[$db_category]} = join (",", &category_list));
}
else {
$db_select_fields{'Related'} or
($db_select_fields{'Related'} = $db_select_fields{'Mult-Related'} = join ",", &category_list);
}
foreach $field (@db_cols) {
# Set the field name to field-key if we are doing multiple forms.
$multiple ? ($name = "$field-$rec{$db_key}") : ($name = $field);
if ($db_select_fields{"Mult-$field"}) { $output .= "<tr><td align=right valign=top width=20%><$font>$field:</font></td><td width=80%>" . &build_select_field($field, $rec{$field}, $name, "MULTIPLE SIZE=3") . "</td></tr>\n"; }
elsif ($db_select_fields{$field}) { $output .= "<tr><td align=right valign=top width=20%><$font>$field:</font></td><td width=80%>" . &build_select_field($field, $rec{$field}, $name) . "</td></tr>\n"; }
elsif ($db_radio_fields{$field}) { $output .= "<tr><td align=right valign=top width=20%><$font>$field:</font></td><td width=80%>" . &build_radio_field($field, $rec{$field}, $name) . "</td></tr>\n"; }
elsif ($db_checkbox_fields{$field}) { $output .= "<tr><td align=right valign=top width=20%><$font>$field:</font></td><td width=80%>" . &build_checkbox_field ($field, $rec{$field}, $name) . "</td></tr>\n"; }
elsif ($db_form_len{$field} =~
/(\d+)x(\d+)/) { $output .= qq~<tr><td align=right valign=top width=20%><$font>$field:</font></td><td width=80%><textarea wrap="virtual" name="$name" cols="$1" rows="$2">$rec{$field}</textarea></td></tr>\n~; }
elsif ($db_form_len{$field} == -1) { $output = qq~<input type=hidden name="$field" value="$rec{$field}">\n$output~; }
else { $output .= qq~<tr><td align=right valign=top width=20%><$font>$field:</font></td><td width=80%><input type=text name="$name" value="$rec{$field}" size="$db_form_len{$field}" maxlength="$db_lengths{$field}"></td></tr>\n~; }
}

# >>> External Text File mod (next 7 lines)
# Put the textarea in the admin forms.
# Fix the textarea display.
if ($in{'db'} eq 'links') { # Make sure we're editing links, and not categories.
$rec{'text'} =~ s,</textarea>,</text-area>,ig;
$output .= qq~<tr><td align=right valign=top width=20%><$font>Text:</font></td><td width=80%><textarea name="text" rows="10" cols="40" wrap="virtual" value="">$rec{'text'}</textarea></td></tr>\n~;
}
# <<<
$output .= "</table></p>\n";
return $output;
}



Leonard
aka PerlFlunkie

Last edited by:

PerlFlunkie: Jun 6, 2007, 1:59 PM
Quote Reply
Re: [PerlFlunkie] External Text File Mod In reply to
If we want to upgrade down the road to Links sql, would it be possible to import the additional text for each link ID with some other script - or all the additional text will be lost?

To import, we would need a perl script (is there one that can do this - open each file per ID, get the text and dump the text into a flatfile db 1 ID per line). Otherwise, when upgrading all additional text would be lost.

Thanks
Quote Reply
Re: [socrates] External Text File Mod In reply to
Don't know...
I have seen scripts that import flatfile databases into a mySQL database, I'm sure it can be done without too much trouble. You could always put the text file data into the flatfile database prior to converting to SQL.


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] External Text File Mod In reply to
There is an import script that comes with links sql so links.db could be imported.

The question was more about "putting the text data" in to links.db before upgrading. I would be interested in knowing if there is a script that will import the additional text data in to links.db (or another db so we can merge them together with excel) before upgrading to sql.

Thanks