Changeset 8119
- Timestamp:
- 2004-09-14T14:55:21+12:00 (20 years ago)
- Location:
- trunk/gsdl
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/etc/packages/example.dbi
r7684 r8119 67 67 68 68 69 # finally, you can declare a subroutine here to process the "text"69 # finally, you can declare subroutines here to post-process the fields 70 70 # in case you want to do any mark up or processing of it. 71 71 # 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. 72 74 # 73 75 #sub text_callback { … … 76 78 # return $text; 77 79 #} 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 135 135 136 136 my %db_to_greenstone_fields=(); 137 my %callbacks=(); 137 138 138 139 # read in config file. … … 146 147 while (defined($line=<CONF>)) { 147 148 chomp $line; 148 $line =~ s/\s* #.*$//mg; # remove comments149 $line =~ s/\s*\#.*$//mg; # remove comments 149 150 $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 ";" 151 170 # check that it is safe 152 171 # assignment 153 if ($statement =~ m~ \$(\w+)\s* = \s*172 if ($statement =~ m~(\$\w+)\s* = \s* 154 173 (\d # digits 155 174 | ".*?(?<!\\)" # " up to the next " not preceded by a \ 156 175 | '.*?(?<!\\)' # ' 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") { 160 180 my $err=$@; 161 181 chomp $err; 162 182 $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"; 164 185 return 0; # there was an error reading the config file 165 186 } 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 171 189 $callback = $statement; 190 $statement = ""; 191 } else { 192 print $outhandle "DBPlug: skipping statement `$statement'\n"; 172 193 } 173 194 $statement = ""; 174 } elsif ($statement =~ /\}\s*$/ && $callback) { # ends with {175 $callback .= $statement ; $statement = "";176 195 } 177 196 } 178 197 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 allowed184 print $outhandle "DBPlug: bad function in callback\n";185 return 0;186 }187 }188 198 189 199 if (!defined($db)) { … … 222 232 } 223 233 } 224 225 234 226 235 # print "DBPlug: names: " . join (", ", @field_names) . ".\n"; … … 280 289 ); 281 290 } 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 282 297 if ($fieldname eq "text") { 283 # see if we have a text_callback() function defined284 if (defined(&text_callback)) {285 $fielddata=text_callback($fielddata);286 }287 298 # add as document text 288 299 $fielddata=~s@<@<@g; … … 339 350 $self->{'num_processed'}++; 340 351 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; 342 356 } 343 357
Note:
See TracChangeset
for help on using the changeset viewer.