Changeset 8119


Ignore:
Timestamp:
2004-09-14T14:55:21+12:00 (20 years ago)
Author:
jrm21
Message:

allow multiple callbacks, one for each metadata field (using the
metadata name that greenstone sees, rather than the column name in the DB).

Updated the example file to show how to do this.

Location:
trunk/gsdl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/etc/packages/example.dbi

    r7684 r8119  
    6767
    6868
    69 # finally, you can declare a subroutine here to process the "text"
     69# finally, you can declare subroutines here to post-process the fields
    7070# in case you want to do any mark up or processing of it.
    7171# For example, you might want to do HTML formatting.
     72# The subroutine's name should be the greenstone metadata field name,
     73# followed by "_callback", and must end with "}" in the first column.
    7274#
    7375#sub text_callback {
     
    7678#    return $text;
    7779#}
    78          
     80#
     81#sub Title_callback {
     82#    my $title=shift;
     83#    return ucfirst($title); # uppercase first letter
     84#}
     85
  • trunk/gsdl/perllib/plugins/DBPlug.pm

    r7701 r8119  
    135135
    136136    my %db_to_greenstone_fields=();
     137    my %callbacks=();
    137138
    138139    # read in config file.
     
    146147    while (defined($line=<CONF>)) {
    147148    chomp $line;
    148     $line =~ s/\s*#.*$//mg; # remove comments
     149    $line =~ s/\s*\#.*$//mg; # remove comments
    149150    $statement .= $line;
    150     if ($statement =~ m/;\s*$/) { # ends with ";"
     151
     152    if ($line =~ /^\}\s*$/ && $callback) { # ends the callback
     153        $callback .= $statement ; $statement = "";
     154        # try to check that the function is "safe"
     155        if ($callback  =~ /\b(?:system|open|pipe|readpipe|qx|kill|eval|do|use|require|exec|fork)\b/ ||
     156        $callback =~ /[\`]|\|\-/) {
     157        # no backticks or functions that start new processes allowed
     158        print $outhandle "DBPlug: bad function in callback\n";
     159        return 0;
     160        }
     161        $callback =~ s/sub (\w+?)_callback/sub/;
     162        my $fieldname=$1;
     163        eval "\$callbacks{'$fieldname'} = $callback ; 1";
     164        $callback="";
     165    } elsif ($callback) {
     166        # add this line to the callback function
     167        $callback .= $statement;
     168        $statement = "";
     169    } elsif ($statement =~ m/;\s*$/) { # ends with ";"
    151170        # check that it is safe
    152171        # assignment
    153         if ($statement =~ m~\$(\w+)\s* = \s*
     172        if ($statement =~ m~(\$\w+)\s* = \s*
    154173        (\d     # digits
    155174         | ".*?(?<!\\)" # " up to the next " not preceded by a \
    156175         | '.*?(?<!\\)' # ' up to the next ' not preceded by a \
    157         )\s*;~x) {      # /x means ignore comments and whitespace in rx
    158         # evaluate the assignment
    159         if (!eval "\$$1=$2;") {
     176        )\s*;~x ||      # /x means ignore comments and whitespace in rx
     177        $statement =~ m~(\%\w+)\s*=\s*(\([\w\s\"\',:=>]+\))\s*;~ ) {   
     178        # evaluate the assignment, return 1 on success "
     179        if (!eval "$1=$2; 1") {
    160180            my $err=$@;
    161181            chomp $err;
    162182            $err =~ s/\.$//; # remove a trailing .
    163             print $outhandle "$err (in $filename)\n";
     183            print $outhandle "DBPlug: error evaluating `$statement'\n";
     184            print $outhandle " $err (in $filename)\n";
    164185            return 0; # there was an error reading the config file
    165186        }
    166         } elsif ($callback) {
    167         # add this line to the callback function
    168         $callback .= $statement;
    169         } elsif ($statement =~ /sub text_callback/) {
    170         # this is the start of the callback function definition
     187        } elsif ($statement =~ /sub \w+_callback/) {
     188        # this is the start of a callback function definition
    171189        $callback = $statement;
     190        $statement = "";
     191        } else {
     192        print $outhandle "DBPlug: skipping statement `$statement'\n";
    172193        }
    173194        $statement = "";
    174     } elsif ($statement =~ /\}\s*$/ && $callback) { # ends with {
    175         $callback .= $statement ; $statement = "";
    176195    }
    177196    }
    178197    close CONF;
    179     if ($callback) {
    180     # try to check that the function is "safe"
    181     if ($callback =~ /\b(?:system|open|pipe|readpipe|qx|kill|eval|do|use|require|exec|fork)\b/ ||
    182         $callback =~ /[\`]|\|\-/) {
    183         # no backticks or functions that start new processes allowed
    184         print $outhandle "DBPlug: bad function in callback\n";
    185         return 0;
    186     }
    187     }
    188198
    189199    if (!defined($db)) {
     
    222232    }
    223233    }
    224 
    225234
    226235#    print "DBPlug: names: " . join (", ", @field_names) . ".\n";
     
    280289                           );
    281290        }
     291        # see if we have a ****_callback() function defined
     292        if (exists $callbacks{$fieldname}) {
     293        my $funcptr = $callbacks{$fieldname};
     294        $fielddata = &$funcptr($fielddata);
     295        }
     296
    282297        if ($fieldname eq "text") {
    283         # see if we have a text_callback() function defined
    284         if (defined(&text_callback)) {
    285             $fielddata=text_callback($fielddata);
    286         }
    287298        # add as document text
    288299        $fielddata=~s@<@&lt;@g;
     
    339350    $self->{'num_processed'}++;
    340351
    341     return $count;
     352    if (defined($dbplug_debug) && $dbplug_debug==1) {
     353        print $outhandle "DBPlug: imported $count DB records as documents.\n";
     354    }
     355    $count;
    342356}
    343357
Note: See TracChangeset for help on using the changeset viewer.