Import CSV data into a wiki

From Organic Design wiki
Revision as of 20:41, 26 March 2008 by Nad (talk | contribs) ($::record not used yet)
  1. !/usr/bin/perl
  2. Our Perl scripts.{{#security:*|sysop}}
  3. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  4. - Author: http://www.organicdesign.co.nz/nad
  5. - Source: http://www.organicdesign.co.nz/scraper.pl
  6. - Started: 2008-03-21

require('wiki.pl');

  1. 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';

  1. 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 } if (/^\*?\s*record\s*:\s*(.+?)\s*$/i) { $::record = $1 } if (/^\*?\s*field\s*:\s*(\w+)\s*=\s*(.+?)\s*$/i) { # named field } elsif (/^\*?\s*field\s*:\s*(.+?)\s*$/) { # unnamed field (get from @headings) if (!defined(@headings)) { die "Please specify CSV file first" if !defined $::csv; if (open CSV,'<',$::csv) { $_ = <CSV>; /^\s*(.+?)\s*$/; @headings = split /\s*$sep\s*/i, $1; close CSV; } else { die "Could not open CSV file!" } die "No headings found!" if !defined @headings; } } } close JOB; } else { die "Couldn't parse job file!" }

  1. $::record or die "No record pattern specified!";
  1. Log in to the wiki

wikiLogin($::wiki,$::user,$::pass) or exit;

  1. Open the CSV

if (open CSV,'<',$::csv) { <CSV> if defined @headings; } else { die "Could not open CSV file!" }

  1. Get batch size and current number (also later account for n-bots)
  1. todo: log batch start
  1. Process the records

while (<CSV>) { /^\s*(.+?)\s*$/; @record = split /\s*$sep\s*/, $1; $text = "{{$template\n"; $text .= "|$headings[$_] = $record[$_]\n" for 0..$#headings; $text .= "}}\n"; print "\n$text";

# Update the record #my $text = wikiRawPage($::wiki,0); #if ($text) { # replace the template with new values or append #} #else { $text = $template } my $updated = wikiPageEdit($::wiki,$record[$::title],$text,"$template updated by csv2wiki.pl");

# log a row error if any }

close CSV;