Difference between revisions of "Import CSV data into a wiki"
($::record not used yet) |
|||
Line 27: | Line 27: | ||
if (/^\*?\s*title\s*:\s*(.+?)\s*$/i) { $::title = $1 } | if (/^\*?\s*title\s*:\s*(.+?)\s*$/i) { $::title = $1 } | ||
if (/^\*?\s*template\s*:\s*(.+?)\s*$/i) { $::template = $1 } | if (/^\*?\s*template\s*:\s*(.+?)\s*$/i) { $::template = $1 } | ||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
close JOB; | close JOB; | ||
} else { die "Couldn't parse job file!" } | } else { die "Couldn't parse job file!" } | ||
− | + | ||
+ | |||
+ | if (open CSV,'<',$::csv) { | ||
+ | $_ = <CSV>; | ||
+ | /^\s*(.+?)\s*$/; | ||
+ | @headings = split /\s*$sep\s*/i, $1; | ||
+ | close CSV; | ||
+ | } else { die "Could not open CSV file!" } | ||
# Log in to the wiki | # Log in to the wiki | ||
Line 62: | Line 52: | ||
# Process the records | # Process the records | ||
+ | $n = 1; | ||
while (<CSV>) { | while (<CSV>) { | ||
/^\s*(.+?)\s*$/; | /^\s*(.+?)\s*$/; | ||
Line 68: | Line 59: | ||
$text .= "|$headings[$_] = $record[$_]\n" for 0..$#headings; | $text .= "|$headings[$_] = $record[$_]\n" for 0..$#headings; | ||
$text .= "}}\n"; | $text .= "}}\n"; | ||
− | print "\n | + | print "Processing record ".$n++."\n"; |
# Update the record | # Update the record | ||
Line 76: | Line 67: | ||
#} | #} | ||
#else { $text = $template } | #else { $text = $template } | ||
+ | #print decode('UTF-8',$record[$::title]); | ||
my $updated = wikiPageEdit($::wiki,$record[$::title],$text,"$template updated by csv2wiki.pl"); | my $updated = wikiPageEdit($::wiki,$record[$::title],$text,"$template updated by csv2wiki.pl"); | ||
− | + | ||
# log a row error if any | # log a row error if any | ||
} | } | ||
close CSV; | close CSV; |
Revision as of 00:00, 27 March 2008
- !/usr/bin/perl
- Our Perl scripts.{{#security:*|sysop}}
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: http://www.organicdesign.co.nz/nad
- - Source: http://www.organicdesign.co.nz/scraper.pl
- - Started: 2008-03-21
require('wiki.pl');
- Job, log and error files
$ARGV[0] or die "No job file specified!"; $ARGV[0] =~ /^(.+?)(\..+?)?$/; $::log = "$1.log"; $::err = "$1.err"; $::sep = ','; $::title = 0; $::template = 'Record';
- Parse the job file
if (open JOB,'<',$ARGV[0]) { for (<JOB>) { if (/^\*?\s*csv\s*:\s*(.+?)\s*$/i) { $::csv = $1 } if (/^\*?\s*wiki\s*:\s*(.+?)\s*$/i) { $::wiki = $1 } if (/^\*?\s*user\s*:\s*(.+?)\s*$/i) { $::user = $1 } if (/^\*?\s*pass\s*:\s*(.+?)\s*$/i) { $::pass = $1 } if (/^\*?\s*separator\s*:\s*(.+?)\s*$/i) { $::sep = $1 } if (/^\*?\s*title\s*:\s*(.+?)\s*$/i) { $::title = $1 } if (/^\*?\s*template\s*:\s*(.+?)\s*$/i) { $::template = $1 } } close JOB; } else { die "Couldn't parse job file!" }
if (open CSV,'<',$::csv) {
$_ = <CSV>;
/^\s*(.+?)\s*$/;
@headings = split /\s*$sep\s*/i, $1;
close CSV;
} else { die "Could not open CSV file!" }
- Log in to the wiki
wikiLogin($::wiki,$::user,$::pass) or exit;
- Open the CSV
if (open CSV,'<',$::csv) { <CSV> if defined @headings; } else { die "Could not open CSV file!" }
- Get batch size and current number (also later account for n-bots)
- todo: log batch start
- Process the records
$n = 1; while (<CSV>) { /^\s*(.+?)\s*$/; @record = split /\s*$sep\s*/, $1; $text = "{{$template\n"; $text .= "|$headings[$_] = $record[$_]\n" for 0..$#headings; $text .= "}}\n"; print "Processing record ".$n++."\n";
# Update the record #my $text = wikiRawPage($::wiki,0); #if ($text) { # replace the template with new values or append #} #else { $text = $template } #print decode('UTF-8',$record[$::title]); my $updated = wikiPageEdit($::wiki,$record[$::title],$text,"$template updated by csv2wiki.pl");
# log a row error if any }
close CSV;