Gossamer Forum
Home : Products : Links 2.0 : Customization :

Script for adding new positing in X position od db?

(Page 1 of 2)
> >
Quote Reply
Script for adding new positing in X position od db?
Ok, now that I understand the gist of adding a field to my database I was wondering if there is a script out there that someone has that adds a field to the database at the Xth position. Like if I wanted to add a new field between Name and Description.

The reason for this: I want to add fields to my database. I know how to add them at the end using the modified upgrade_beta.pl script. But I don't want to add them at the end, but instead someplace in the middle of the record. I want to view the fields of the record in a certain order, and it will not be intuitive if I have all these unrelated fields at the end of the record. Make sense?

I know this is possible and was wondering if anyone has done it (or can do it easily) before I pull out my "Programming in PERL" book.

Thanks Smile
crisco

[This message has been edited by crisco (edited March 29, 1999).]
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Try the following script. I haven't tested it, but it should work okay.

Code:
#!/usr/local/bin/perl
# -------------------------------------------
$ENV{'REQUEST_METHOD'} and (print "Content-type: text/plain\n\n");
open (DB, "<links.db") or
print "Unable to open links database 'links.db'. Reason: $!" and exit;
print "\tOpening output file . . .\n";
open (DBOUT, ">links2.db") or
print "Unable to open output database.
Make sure data dir is chmod 777 temporarily. Reason: $!" and exit;
print "\tProcessing records\n";
while (<DB> ) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp; # Remove trailing new line.
@rec_in = &split_decode($_);
# Copy ID, Title, URL, Date and Category
for $i (0 .. 4) {
$rec_out[$i] = $rec_in[$i];
}
# Add New Field here
$rec_out[5] = ""; # add something between quotes if you have a default value
# Copy the rest of old links.db to new links2.db
for $i (5 .. 13) {
$rec_out[$i + 1] = $rec_in[$i];
}
print DBOUT &join_encode(&array_to_hash(0, @rec_out));
}
print "\tDone.\n\n";
close DB;
close DBOUT;
print "Database saved it as links2.db.\n";
print "Change permissions back to 755 on the data directory.\n\n";
print "You must rename 'links2.db' to 'links.db before using with Links v2.\n";
print "It is recommend you save the original 'links.db' before renaming.";

You can adjust the range in the two "for" loops and the field being added if the field numbers are different in your case.

I hope this helps.

[This message has been edited by Bobsie (edited March 30, 1999).]
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Thanks!

Playing with it now.

[This message has been edited by crisco (edited March 30, 1999).]
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Success! Thanks Bobsie Smile

Code:
#!/usr/bin/perl
# -------------------------------------------
#You must EDIT YOUR links.def script BEFORE entering these variable and running this script!
$field_default = ""; #Put the default value of the new field here. Leave Blank for no default.
$new_field = "7"; #Put the new field number here.
$total_fields = "15"; #Put the last field number here.

#Change these value to match your settings
require "../db_utils.pl";
require "../links.def";


#You shouldn't have to edit below this line
#------------------------------------------
$nfm1 = $new_field - 1;


$ENV{'REQUEST_METHOD'} and (print "Content-type: text/plain\n\n");
open (DB, "<links.db") or print "Unable to open links database 'links.db'. Reason: $!" and exit;
print "\tOpening output file . . .\n";
open (DBOUT, ">links2.db") or print "Unable to open output database. Make sure data dir is chmod 777 temporarily. Reason: $!" and exit;
print "\n\n\tProcessing records\n";
while (<DB> ) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
chomp; # Remove trailing new line.
@rec_in = &split_decode($_);
# Copy ID, Title, URL, Date and Category
for $i (0 .. $nfm1) {
$rec_out[$i] = $rec_in[$i];
}
# Add New Field
$rec_out[$new_field] = "$field_default";
# Copy the rest of old links.db to new links2.db
for $i ($new_field .. $total_fields) {
$rec_out[$i + 1] = $rec_in[$i];
}
print DBOUT &join_encode(&array_to_hash(0, @rec_out));
}
print "\tDone.\n\n";
close DB;
close DBOUT;
print "Database saved it as links2.db.\n";
print "Change permissions back to 755 on the data directory.\n\n";
print "You must rename 'links2.db' to 'links.db before using with Links v2.\n";
print "It is recommend you save the original 'links.db' before renaming.\n\n";

See this post for info on modifying links.def
www.gossamer-threads.com/scripts/forum/resources/Forum3/HTML/000865.html


[This message has been edited by crisco (edited March 30, 1999).]
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Crisco,

Nice modification to the original! Ya done good!

One thing I noted, you might want to change the comment, "# Copy ID, Title, URL, Date and Category", to something else since it only applied to the script I put up. Maybe something like, "# Copy fields up to, but not including, new field."?

Also, if I may ask, why did you include this in the script:

Quote:
#You must EDIT YOUR links.def script BEFORE entering these variable and running this script!

and

Quote:
require "../links.def";

I don't see anything in the script that would require that file. I do agree that the links.def should be edited before running the script, but I don't know why it is a required file. What am I missing?

Again, good job. I am going to bookmark this thread.

[This message has been edited by Bobsie (edited March 30, 1999).]
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Thanks guys. I don't have a need for this now, but I made a copy of it. I am pretty sure I will need it later.

Question: I am assuming that the replacement field could be any field - beginning, middle, end...
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Bobsie,

For some reason it wasn't working until I required link.def

It was just filling the whole database with spaces.

I think it pulls some kind of field information from links.def (well alex's subroutines seem to).

At any rate that is the only way I could get it to work. I had to require links.def and make sure I added the new information to links.def before I ran the script. Of course I was up all night playing with it so I was pretty groggy by the time I got it to work. And I'm also not all that hot at Perl...

Try running it without requiring links.def and see what happens. If your links2.db is full of spaces (it won't be empty, just full of spaces) then it's the links.def file that does something. And it must be one of alex's subroutines that looks at it and... well does something Smile
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
crisco,

Well I'll be darned! I have a variety of these scripts that I use and everyone of them include those require lines. Now I wonder why I left them out of the script I posted. Like you, it must have been early in the groggy morning... Wink

socrates,

Yes, crisco's script can be used to add a field anywhere in links.db. If you have multiple fields to add, you would run it multiple times (after renaming links2.db that was created from the previous run to links.db). Still, it is a lot easier and a lot faster than doing it by hand! Smile
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
A variety of scripts bobsie... mmmmm, you'll have to let me peek in your goodie bag sometime Smile

crisco
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
i tried bobsie's script but it's not working for me, i'm getting a blank links2.db.
am i doing something wrong or bobsie script is not working.
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
You need to require the links.def as noted above.

on a different note:

A great modification for this would be to include the old links.def, then the new_links.def and write out the new_links.db file matching the fields from the original file to the new file, inserting blank fields where needed.

This is not a major rewrite, and I've really been meaning to do it ever since I saw Bobsie's first code.

Such a generalized program would work for any flat-file database if you could set "infile pattern" "outfile pattern" and "delimeter"

Pattern could be set by the first row of the database (headers), or by a field match where each field of the input file is assigned a consecutive integer, so if you wanted to switch fields 3 and 4 and insert a field after 4 you could do something like this:

_output_pattern = [1,2,4,3,,]

Where the default would be to append the rest of the fields if not explicitly stated, so that in a 7 field input file, the above line would be equivalent to:

_output_pattern = [1,2,4,3,,5,6,7]

Because of the number of flat files being used in links and it's newer mods, such a flexible re-organizer would be very useful.

If anyone is looking for something do one evening, this would be a really useful utility, even limited to the links.def in and new_links.def type of input .

Quote Reply
Re: Script for adding new positing in X position od db? In reply to
You need to require the links.def as noted above.

on a different note:

A great modification for this would be to include the old links.def, then the new_links.def and write out the new_links.db file matching the fields from the original file to the new file, inserting blank fields where needed.

This is not a major rewrite, and I've really been meaning to do it ever since I saw Bobsie's first code.

Such a generalized program would work for any flat-file database if you could set "infile pattern" "outfile pattern" and "delimeter"

Pattern could be set by the first row of the database (headers), or by a field match where each field of the input file is assigned a consecutive integer, so if you wanted to switch fields 3 and 4 and insert a field after 4 you could do something like this:

_output_pattern = [1,2,4,3,,]

Where the default would be to append the rest of the fields if not explicitly stated, so that in a 7 field input file, the above line would be equivalent to:

_output_pattern = [1,2,4,3,,5,6,7]

Because of the number of flat files being used in links and it's newer mods, such a flexible re-organizer would be very useful.

If anyone is looking for something do one evening, this would be a really useful utility, even limited to the links.def in and new_links.def type of input .

Quote Reply
Re: Script for adding new positing in X position od db? In reply to
i used the same script to add an extra field for altcategory (links2)
just try the code posted by crisco. it should work
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Greetings All!

Did anyone ever finish this script for one to use to add fields to LINKS2. If so where is it available.

Many thanks.

------------------
http://www.nzcid.godzone.net.nz
New Zealand Christian Internet Directory


Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Hi theguy!

I need to add lots of different fields - can see one can run it multiple times - Is it possible to add mulitple fields all at the sametime.


------------------
http://www.nzcid.godzone.net.nz
New Zealand Christian Internet Directory


Quote Reply
Re: Script for adding new positing in X position od db? In reply to
i guess you have to run it afew times Smile
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Just a note for novices who are trying to use this (like myself):

1) Upload to your data directory.
2) You have to chmod the script to 755.
3) Just run it from the browser window.




------------------
Kevin D
www.greeknet.net
webmaster@greeknet.net



Quote Reply
Re: Script for adding new positing in X position od db? In reply to
what you suggested may worked for you but it does not mean it would work for everyone.
you do not have to upload the script to your data directory, just make sure the path to the script & data file are correct.
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Hm, there's something not running right. I'm using Crisco's modified version of the upgrade.cgi script.

My cat.def:
Code:
%db_def = (
ID => [0, 'numer', 5, 8, 1, '', ''],
Name => [1, 'alpha', 40, 75, 1, '', '^[\w\d/_-]+$'],
Description => [2, 'alpha', '40x3', 500, 0, '', ''],
SubCatStyle => [3, 'alpha', 40, 100, 0, '(ALL)1', ''],
Related => [4, 'alpha', 0, 255, 0, '', ''],
'Meta Description' => [5, 'alpha', 40, 75, 0, '', ''],
'Meta Keywords' => [6, 'alpha', 40, 75, 0, '', ''],
Header => [7, 'alpha', 40, 75, 0, '', ''],
Footer => [8, 'alpha', 40, 75, 0, '', ''],
Nonenglish => [9, 'alpha', 40, 75, 0, '', '']

);

See the last field has been added long time ago, it's all fine. Now I'd like to add field 3 'subcatstyle'.

When I execute the script all's ok, the ALL(1) is added in the new field which is at the right place, except that to the last nonenglish field 7x | is added.
For example, the entry will then read "Private Homepages | &#0124; &#0124; &#0124; &#0124; &#0124; &#0124;".

What can this be caused by?

I set up the upgrade.cgi like this:
Code:
$field_default = "ALL(1)"; #Put the default value of the new field here. Leave Blank for no default.
$new_field = "3"; #Put the new field number here.
$total_fields = "9"; #Put the last field number here.

Thanks
Denis

[This message has been edited by Denis (edited February 29, 2000).]
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Denis,

The following codes are WRONG:

Code:
$total_fields = "9"; #Put the last field number here.

You have 10 fields, not 9 fields. Count your fields again...see 10 fields, not 9 fields.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
Sorry, I didn't mention it. Thought so before and was actually 'counting' the fields. But the result was the same as is now (gonna test it this minute once more anyway). And I thought I would be blamed if I say I my last field's no. 10, whereas anybody knows Perl starts always with 0...

Ok, will test it once again following your advise..

Denis
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
If you have access to telnet for your website, I find awk (or gawk, nawk whatever is installed) a lot easier to use for this type of coding. Here is a sample awk program that would do the same thing:

Code:
BEGIN{FS="|"
RS="\n"
OFS="|"
ORS="\n"}
{print $1, $2, $3, $4, $5, $6, "", $7, $8, $9, $10, $11, $12, $13, $14 , $15}

FS = field seperator
RS = record seperator
OFS = output field seperator
ORS = output record seperator
$1 - $n = 1st - nth input field

To run the script (assume the script is in a file called convert.awk) use the following:

Code:
awk -f convert.awk < links.db > new_links.db

You'll need to supply the full paths to convert.awk, links.db and new_links.db

Just my 2 cents,

Frank

------------------
Webmasters Resources
http://www.webmasters-resources.com/
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
It makes no difference.
This is what I get for the first category in my .db:
Code:
1|Private_Homepages|SUB1|ALL(1)|Clubs_und_Vereine~~Lifestyle~~Roller_und_Mopeds~~Schrauberseiten~~Sport~~Touren_und_Reisen~~Veranstalt ungen_und_Termine|Private Homepages für Motorradfahrer|Motorrad, Homepages, Internetseiten, Links, Webverzeichnis|Diese Kategorie enthält Private Homepages von und für Motorradfahrer.&#0124; &#0124;Private Homepages&#0124; &#0124;&#0124; &#0124;&#0124; &#0124;|
I'm referring to the &#0124; &#0124;&#0124; &#0124;| at the end of the entry. See, the ALL(1) is at the right position when I tell the script to add the new field at position 3. That's also why I thought I have to quote 9 total fields.

When I call admin.cgi the new field SubCatStyle is at the right 4th position (so I think I did nothing wrong with categories.def), only the last field nonenglish shows "Usenet\Foren und Chat&#0124; &#0124;&#0124; &#0124;&#0124; &#0124;|".

????

Denis
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
UH,UH,UH!

Shame on me!

Allthough I made all the changes to have a new categories.db instead links.db, I forgot to change also require "../links.def";
to require "../category.def";.

He,he, so sorry! Took me 2 days to see that...

Once again 'thanks Eliot', anyway!
Denis


[This message has been edited by Denis (edited March 01, 2000).]
Quote Reply
Re: Script for adding new positing in X position od db? In reply to
sorry please delete this I posted wrong section..

[This message has been edited by thefreebiesite (edited May 03, 2000).]
> >