source: main/trunk/greenstone2/perllib/cgiactions/modmetadataaction.pm@ 33183

Last change on this file since 33183 was 33183, checked in by ak19, 5 years ago
  1. Added the untested erase_archives/index/live/import_metadata() subroutines to modmetadataaction, which will remove all metadata with matching metadata name from the specified docid. They generate no perl interpreter errors when running metadata-server.pl in authenticated mode, but otherwise they're untested. 2. Fixed an issue in baseaction.pm with running metadata-server.pl. 3. Commented out debug messages from metadataaction.pm. Second commit phase will need to introduce matching javascript methods to erase metadata.
File size: 105.7 KB
Line 
1##########################################################################
2#
3# modmetadataaction.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 2009 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# This is a submodule of metadataaction.pm. It is loaded conditionally
27# upon having the permissions to modify (not just get) metadata.
28# With sufficient permissions, these additional subroutines should be made
29# available to metadataaction.pm
30
31# See http://www.perlmonks.org/?node_id=881761 for splitting module into multiple files
32# and how variables declared with 'our' are used there.
33
34package metadataaction;
35
36use strict;
37
38use cgiactions::baseaction;
39
40use dbutil;
41use ghtml;
42
43use JSON;
44
45
46$metadataaction::modmeta_action_table = #OR: our $modmeta_action_table =
47{
48 #SET METHODS
49 "set-live-metadata" => {
50 'compulsory-args' => [ "d", "metaname", "metavalue" ],
51 'optional-args' => [ ] },
52
53 "set-metadata" => { # generic set-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
54 'compulsory-args' => [ "metaname", "metavalue" ],
55 'optional-args' => [ "where", "metapos", "metamode", "prevmetavalue", "d", "f" ] },
56
57 "set-index-metadata" => {
58 'compulsory-args' => [ "d", "metaname", "metavalue" ],
59 'optional-args' => [ "metapos", "metamode" ] },
60
61 "set-archives-metadata" => {
62 'compulsory-args' => [ "d", "metaname", "metavalue" ],
63 'optional-args' => [ "metapos", "metamode", "prevmetavalue" ] }, # metamode can be "accumulate", "override",
64
65 "set-import-metadata" => {
66 'compulsory-args' => [ "metaname", "metavalue" ],
67 'optional-args' => [ "d", "f", "metamode", "metapos", "prevmetavalue" ] }, # metamode can be "accumulate", "override", or "unique-id". Also need to add the ability to specify a previous metadata value to overwrite (because we can't use metapos). Metapos now supported, but assumes you are working with a Simple (instead of Complex) collection
68
69 #SET METHODS (ARRAY)
70 "set-metadata-array" => {
71 'compulsory-args' => [ "where", "json" ],
72 'optional-args' => [ ],
73 'help-string' => [
74 'A simple example: metadata-server.pl?a=set-metadata-array&where=archives|index|import&c=demo&json=[{"docid":"HASHc5bce2d6d3e5b04e470ec9","metaname":"Title","metavalue":"Tralalala","metamode":"accumulate"},{"docid":"HASHbe483fa4df4e096335d1c8","metaname":"Title","metavalue":"Lala was here","metapos":0, "metamode":"override"}]',
75
76 'A more complex example: metadata-server.pl?a=set-metadata-array&where=archives|index&c=demo&json=[{"docid":"HASHc5bce2d6d3e5b04e470ec9.1","metatable":[{"metaname":"Title","metavals":["Transformers","Robots in disguise","Autobots"]}],"metamode":"override"},{"docid":"HASHbe483fa4df4e096335d1c8.2","metaname":"Title","metavalue":"Pinky was here","metamode":"accumulate"}]' ] },
77
78# The same examples rewritten for when running the metadata-server.pl script from the commandline:
79
80# the simple example: metadata-server.pl a="set-metadata-array" where="archives|index|import" c="demo" json="[{\"docid\":\"HASHc5bce2d6d3e5b04e470ec9\",\"metaname\":\"Title\",\"metavalue\":\"Tralalala\",\"metamode\":\"accumulate\"},{\"docid\":\"HASHbe483fa4df4e096335d1c8\",\"metaname\":\"Title\",\"metavalue\":\"Lala was here\",\"metapos\":0, \"metamode\":\"override\"}]",
81
82# the more complex example: metadata-server.pl a="set-metadata-array" where="archives|index" c="demo" json="[{\"docid\":\"HASHc5bce2d6d3e5b04e470ec9.1\",\"metatable\":[{\"metaname\":\"Title\",\"metavals\":[\"Transformers\",\"Robots in disguise\",\"Autobots\"]}],\"metamode\":\"override\"},{\"docid\":\"HASHbe483fa4df4e096335d1c8.2\",\"metaname\":\"Title\",\"metavalue\":\"Pinky was here\",\"metamode\":\"accumulate\"}]"
83
84 "set-archives-metadata-array" => {
85 'compulsory-args' => [ "json" ],
86 'optional-args' => [ ] },
87
88 "set-import-metadata-array" => {
89 'compulsory-args' => [ "json" ],
90 'optional-args' => [ ] },
91
92 "set-index-metadata-array" => {
93 'compulsory-args' => [ "json" ],
94 'optional-args' => [ ] },
95
96 "set-live-metadata-array" => {
97 'compulsory-args' => [ "json" ],
98 'optional-args' => [ ] },
99
100 #REMOVE METHODS
101 "remove-import-metadata" => {
102 'compulsory-args' => [ "d", "metaname" ], #TODO: add f argument
103 'optional-args' => [ "metapos", "metavalue", "metamode" ] }, # only provide metapos arg for SIMPLE collections.
104# Metavalue is now an optional arg for remove_import_metadata() based on what the implementation did, which allowed metavalue to be undefined, and if so, used metapos.
105
106 "remove-archives-metadata" => {
107 'compulsory-args' => [ "d", "metaname" ], #TODO: add f argument
108 'optional-args' => [ "metapos", "metavalue", "metamode" ] },
109
110 "remove-live-metadata" => {
111 'compulsory-args' => [ "d", "metaname" ],
112 'optional-args' => [ ] },
113
114 "remove-index-metadata" => {
115 'compulsory-args' => [ "d", "metaname" ],
116 'optional-args' => [ "metapos", "metavalue" ] },
117
118 "remove-metadata" => { # generic remove-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
119 'compulsory-args' => [ "d", "metaname" ],
120 'optional-args' => [ "where", "metapos", "metavalue", "metamode" ] }, # metamode is optional since remove-metadata can call any of remove_import_meta and remove_archives_meta, remove_index_meta, of which the first two accept metamode as an optional param
121
122
123 #REMOVE MULTIPLE (REMOVE ALL MATCHING METANAME AT DOCID)
124 "erase-import-metadata" => {
125 'compulsory-args' => [ "d", "metaname" ] },
126
127 "erase-archives-metadata" => {
128 'compulsory-args' => [ "d", "metaname" ] },
129
130 "erase-index-metadata" => {
131 'compulsory-args' => [ "d", "metaname" ] },
132
133 "erase-live-metadata" => {
134 'compulsory-args' => [ "d", "metaname" ] },
135
136 "erase-metadata" => { # generic erase-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
137 'compulsory-args' => [ "d", "metaname" ],
138 'optional-args' => [ "where" ] },
139
140
141 #INSERT METHODS
142 "insert-metadata" => {
143 'compulsory-args' => [ "d", "metaname", "metavalue" ],
144 'optional-args' => [ ] }
145};
146
147
148sub _set_live_metadata
149{
150 my $self = shift @_;
151
152 my $collect = $self->{'collect'};
153 my $gsdl_cgi = $self->{'gsdl_cgi'};
154 #my $gsdlhome = $self->{'gsdlhome'};
155 my $infodbtype = $self->{'infodbtype'};
156
157 # Obtain the collect dir
158 my $site = $self->{'site'};
159 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
160 ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
161
162
163 # look up additional args
164 my $docid = $self->{'d'};
165 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
166 $gsdl_cgi->generate_error("No docid (d=...) specified."); # generates error and dies
167 }
168 my $metavalue = $self->{'metavalue'};
169
170 # Generate the dbkey
171 my $metaname = $self->{'metaname'};
172 my $dbkey = "$docid.$metaname";
173
174 # To people who know $collect_tail please add some comments
175 # Obtain path to the database
176 my $collect_tail = $collect;
177 $collect_tail =~ s/^.*[\/|\\]//;
178 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
179 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
180
181 # Set the new value
182 my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
183 my $status = system($cmd);
184 if ($status != 0) {
185 # Catch error if gdbmget failed
186 my $mess = "Failed to set metadata key: $dbkey\n";
187
188 $mess .= "PATH: $ENV{'PATH'}\n";
189 $mess .= "cmd = $cmd\n";
190 $mess .= "Exit status: $status\n";
191 $mess .= "System Error Message: $!\n";
192
193 $gsdl_cgi->generate_error($mess);
194 }
195 else {
196 $gsdl_cgi->generate_ok_message("set-live-metadata successful: Key[$metaname]=$metavalue");
197 }
198
199 #return $status; # in case calling functions have any further use for this
200}
201
202sub set_live_metadata
203{
204 my $self = shift @_;
205
206 my $username = $self->{'username'};
207 my $collect = $self->{'collect'};
208 my $gsdl_cgi = $self->{'gsdl_cgi'};
209
210 if ($baseaction::authentication_enabled) {
211 # Ensure the user is allowed to edit this collection
212 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
213 }
214
215 # Make sure the collection isn't locked by someone else
216 $self->lock_collection($username, $collect);
217
218 $self->_set_live_metadata(@_);
219
220 # Release the lock once it is done
221 $self->unlock_collection($username, $collect);
222}
223
224sub set_index_metadata_entry
225{
226 my $self = shift @_;
227 my ($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue) = @_;
228
229 # To people who know $collect_tail please add some comments
230 # Obtain path to the database
231 my $collect_tail = $collect;
232 $collect_tail =~ s/^.*[\/|\\]//;
233 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
234 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
235
236# print STDERR "**** infodb file path = $infodb_file_path\n";
237# print STDERR "***** infodb type = $infodbtype\n";
238
239 # Read the docid entry
240 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
241
242 # Set the metadata value
243 if (defined $metapos) {
244 # if metamode=accumulate AND metapos, warn user and then use metapos
245 if (defined $metamode && $metamode eq "accumulate") {
246 print STDERR "**** Warning: metamode is set to accumulate yet metapos is also provided for $docid\n";
247 print STDERR "**** Proceeding by using metapos\n";
248 }
249 $doc_rec->{$metaname}->[$metapos] = $metavalue;
250 }
251 elsif (defined $prevmetavalue) {
252 my $array = $doc_rec->{$metaname};
253 my $length = @$array;
254
255 my $found = 0;
256 for (my $i = 0; $i < $length; $i++){
257 if(defined $doc_rec->{$metaname}->[$i] && $doc_rec->{$metaname}->[$i] eq $prevmetavalue){
258 $doc_rec->{$metaname}->[$i] = $metavalue;
259 $found = 1;
260 last;
261 }
262 }
263
264 if($found == 0){
265 $doc_rec->{$metaname} = [ $metavalue ];
266 }
267 }
268 elsif (defined $metamode && $metamode eq "override") {
269 $doc_rec->{$metaname} = [ $metavalue ];
270 }
271 else { # default for index was to override, but because accumulate is less destructive,
272 # and because accumulate is the default for archives and import, that's the new default for index too
273 if(defined $doc_rec->{$metaname}) {
274 push(@{$doc_rec->{$metaname}}, $metavalue); # accumulate the value for that metaname
275 } else {
276 $doc_rec->{$metaname} = [ $metavalue ];
277 }
278 }
279
280 my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec);
281
282 return $status;
283
284}
285
286sub _set_import_metadata
287{
288 my $self = shift @_;
289
290 my $collect = $self->{'collect'};
291 my $gsdl_cgi = $self->{'gsdl_cgi'};
292 my $infodbtype = $self->{'infodbtype'};
293# my $gsdlhome = $self->{'gsdlhome'};
294
295 # Obtain the collect and archive dir
296 my $site = $self->{'site'};
297 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
298 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
299 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
300
301 # look up additional args
302 # want either d= or f=
303 my $docid = $self->{'d'};
304 my ($docid_root,$docid_secnum);
305 if(defined $docid) {
306 ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
307 # as yet no support for setting subsection metadata in metadata.xml
308 if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
309 $gsdl_cgi->generate_message("*** No support yet for setting import metadata at subsections level.\n");
310 return;
311 }
312 }
313
314 my $import_file = $self->{'f'};
315 if ((!defined $docid || $docid =~ m/^\s*$/) && (!defined $import_file || $import_file =~ m/^\s*$/)) {
316 $gsdl_cgi->generate_error("No docid (d=...) or import file (f=) specified."); # at least d or f must be specified
317 }
318
319 # Get the parameters and set default mode to "accumulate"
320 my $metaname = $self->{'metaname'};
321 my $metavalue = $self->{'metavalue'};
322## $metavalue =~ s/&amp;lt;(.*?)&amp;gt;/<$1>/g;
323 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
324
325 my $metamode = $self->{'metamode'};
326 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
327 # make "accumulate" the default (less destructive, as it won't actually
328 # delete any existing values)
329 $metamode = "accumulate";
330 }
331
332 # adding metapos and prevmetavalue support to import_metadata subroutines
333 my $metapos = $self->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
334 my $prevmetavalue = $self->{'prevmetavalue'};
335 $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
336 $prevmetavalue = undef if(defined $prevmetavalue && ($prevmetavalue =~ m/^\s*$/));
337
338 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
339 my $metadata_xml_filename = $self->set_import_metadata_entry($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid_root, $metaname, $metapos,$metavalue, $metamode,$prevmetavalue, $collect, $collect_dir); # at this point, docid_root = docid
340
341 my $mess = "set-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
342 $mess .= " $metaname";
343 $mess .= " = $metavalue";
344 $mess .= " ($metamode)\n";
345
346 $gsdl_cgi->generate_ok_message($mess);
347
348 #return $status; # in case calling functions have any further use for this
349}
350
351# the version of set_archives_meta that doesn't do authentication
352sub _set_archives_metadata
353{
354 my $self = shift @_;
355
356 my $collect = $self->{'collect'};
357 my $gsdl_cgi = $self->{'gsdl_cgi'};
358 my $infodbtype = $self->{'infodbtype'};
359
360 # Obtain the collect and archive dir
361 my $site = $self->{'site'};
362 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
363 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
364
365 # look up additional args
366 my $docid = $self->{'d'};
367 my $metaname = $self->{'metaname'};
368 my $metavalue = $self->{'metavalue'};
369 my $prevmetavalue = $self->{'prevmetavalue'};
370
371 my $metapos = $self->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
372 # Don't append "|| undef", since if metapos=0 it will then be set to undef
373
374 $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
375 $prevmetavalue = undef if(defined $prevmetavalue && ($prevmetavalue =~ m/^\s*$/));
376
377 my $metamode = $self->{'metamode'};
378 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
379 # make "accumulate" the default (less destructive, as it won't actually
380 # delete any existing values)
381 $metamode = "accumulate";
382 }
383
384 my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
385 $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
386
387 if ($status == 0) {
388 my $mess = "set-archives-metadata successful: Key[$docid]\n";
389 $mess .= " $metaname";
390 $mess .= "->[$metapos]" if (defined $metapos);
391 $mess .= " = $metavalue";
392 $mess .= " ($metamode)\n";
393
394 $gsdl_cgi->generate_ok_message($mess);
395 }
396 else {
397 my $mess .= "Failed to set archives metadata key: $docid\n";
398 $mess .= "Exit status: $status\n";
399 if(defined $self->{'error_msg'}) {
400 $mess .= "Error Message: $self->{'error_msg'}\n";
401 } else {
402 $mess .= "System Error Message: $!\n";
403 }
404 $mess .= "-" x 20 . "\n";
405
406 $gsdl_cgi->generate_error($mess);
407 }
408
409 #return $status; # in case calling functions have any further use for this
410}
411
412
413# the version of set_index_meta that doesn't do authentication
414sub _set_index_metadata
415{
416 print STDERR "START SET INDEX METADATA\n";
417 my $self = shift @_;
418
419 my $collect = $self->{'collect'};
420 my $gsdl_cgi = $self->{'gsdl_cgi'};
421
422 my $site = $self->{'site'};
423 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
424
425 # look up additional args
426 my $docid = $self->{'d'};
427 my $metaname = $self->{'metaname'};
428 my $metapos = $self->{'metapos'}; # undef has meaning
429 my $metavalue = $self->{'metavalue'};
430 my $infodbtype = $self->{'infodbtype'};
431 my $metamode = $self->{'metamode'};
432 my $prevmetavalue = $self->{'prevmetavalue'};
433
434 $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
435 $prevmetavalue = undef if(defined $prevmetavalue && ($prevmetavalue =~ m/^\s*$/));
436
437 print STDERR "SETTING INDEX METADATA ENTRY\n";
438 my $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
439 print STDERR "DONE SETTING INDEX METADATA ENTRY\n";
440 if ($status != 0) {
441 # Catch error if set infodb entry failed
442 my $mess = "Failed to set metadata key: $docid\n";
443
444 $mess .= "PATH: $ENV{'PATH'}\n";
445 $mess .= "Exit status: $status\n";
446 $mess .= "System Error Message: $!\n";
447
448 $gsdl_cgi->generate_error($mess);
449 }
450 else {
451 my $mess = "set-index-metadata successful: Key[$docid]\n";
452 $mess .= " $metaname";
453 $mess .= "->[$metapos]" if (defined $metapos);
454 $mess .= " = $metavalue\n";
455
456 $gsdl_cgi->generate_ok_message($mess);
457 }
458
459 print STDERR "END SET INDEX METADATA\n";
460 #return $status; # in case calling functions have any further use for this
461}
462
463sub set_index_metadata
464{
465 my $self = shift @_;
466
467 my $username = $self->{'username'};
468 my $collect = $self->{'collect'};
469 my $gsdl_cgi = $self->{'gsdl_cgi'};
470 #my $gsdlhome = $self->{'gsdlhome'};
471
472 if ($baseaction::authentication_enabled) {
473 # Ensure the user is allowed to edit this collection
474 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
475 }
476
477 my $site = $self->{'site'};
478 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
479
480 $gsdl_cgi->checked_chdir($collect_dir);
481
482 # Obtain the collect dir
483 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
484
485 # Make sure the collection isn't locked by someone else
486 $self->lock_collection($username, $collect);
487
488 $self->_set_index_metadata(@_);
489
490 # Release the lock once it is done
491 $self->unlock_collection($username, $collect);
492}
493
494# call this to set the metadata for a combination of dirs archives, import or index, or live
495# if none specified, defaults to index which was the original behaviour of set_metadata.
496sub set_metadata
497{
498 my $self = shift @_;
499
500 # Testing that not defining a variable, setting it to "" or to " " all return false
501 # >perl -e 'my $whichdirs=""; if($whichdirs) {print "$whichdirs\n"};'
502
503 my $where = $self->{'where'};
504 if(!$where || ($where =~ m/^\s*$/)) {
505 $self->set_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
506 return;
507 }
508
509 # authenticate and lock collection once, even if processing multiple dirs
510 my $username = $self->{'username'};
511 my $collect = $self->{'collect'};
512 my $gsdl_cgi = $self->{'gsdl_cgi'};
513
514 if ($baseaction::authentication_enabled) {
515 # Ensure the user is allowed to edit this collection
516 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
517 }
518
519 if($where =~ m/index/) {
520 my $site = $self->{'site'};
521 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
522 $gsdl_cgi->checked_chdir($collect_dir);
523 }
524
525 # Make sure the collection isn't locked by someone else
526 $self->lock_collection($username, $collect);
527
528
529 # now at last can set the metadata. $where can specify multiple
530 # $where is of the form: import|archives|index, or a subset thereof
531
532 #my @whichdirs = split('\|', $where);
533
534 # just check whether $where contains import/archives/index/live in turn, and
535 # for each case, process it accordingly
536 if($where =~ m/import/) {
537 $self->_set_import_metadata(@_);
538 }
539
540 if($where =~ m/archives/) {
541
542 # look up docID arg which is optional to set_metadata because it's optional
543 # to set_import, but which is compulsory to set_archives_metadata
544 my $docid = $self->{'d'};
545 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
546 $gsdl_cgi->generate_error("No docid (d=...) specified."); # generates error and dies
547 }
548 # we have a docid, so can set archives meta
549 $self->_set_archives_metadata(@_);
550 }
551
552 if($where =~ m/index/) {
553
554 # look up docID arg which is optional to set_metadata because it's optional
555 # to set_import, but which is compulsory to set_archives_metadata
556 my $docid = $self->{'d'};
557 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
558 $gsdl_cgi->generate_error("No docid (d=...) specified.");
559 }
560 # we have a docid, so can set index meta
561 $self->_set_index_metadata(@_);
562 }
563
564 if($where =~ m/live/) {
565 $self->_set_live_metadata(@_); # docid param, d, is compulsory, but is checked for in subroutine
566 }
567
568 # Release the lock once it is done
569 $self->unlock_collection($username, $collect);
570}
571
572sub set_metadata_array
573{
574 my $self = shift @_;
575
576 my $where = $self->{'where'};
577 if(!$where || ($where =~ m/^\s*$/)) {
578 $self->set_index_metadata_array(@_); # default behaviour is the full version of set_index_meta_array
579 return;
580 }
581
582 my $username = $self->{'username'};
583 my $collect = $self->{'collect'};
584 my $gsdl_cgi = $self->{'gsdl_cgi'};
585
586 if ($baseaction::authentication_enabled) {
587 # Ensure the user is allowed to edit this collection
588 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
589 }
590
591 # Not sure if the checked_chdir is necessary, since lock_collection also does a chdir
592 # But including the stmt during this code reorganisation to preserve as-is what used to happen
593 my $site = $self->{'site'};
594 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
595 $gsdl_cgi->checked_chdir($collect_dir);
596
597 # Make sure the collection isn't locked by someone else
598 $self->lock_collection($username, $collect);
599
600 if($where =~ m/import/) {
601 $self->_set_import_metadata_array(@_);
602 }
603 if($where =~ m/archives/) {
604 $self->_set_archives_metadata_array(@_);
605 }
606 if($where =~ m/index/) {
607 $self->_set_index_metadata_array(@_);
608 }
609 if($where =~ m/live/) {
610 $self->_set_live_metadata_array(@_);
611 }
612
613 # Release the lock once it is done
614 $self->unlock_collection($username, $collect);
615}
616
617sub _set_index_metadata_array
618{
619 my $self = shift @_;
620
621 my $collect = $self->{'collect'};
622 my $gsdl_cgi = $self->{'gsdl_cgi'};
623 my $site = $self->{'site'};
624 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
625
626
627 # look up additional args
628
629 my $infodbtype = $self->{'infodbtype'};
630
631 my $json_str = $self->{'json'};
632 my $doc_array = decode_json $json_str;
633
634
635 my $global_status = 0;
636 my $global_mess = "";
637
638 my @all_docids = ();
639
640 foreach my $doc_array_rec ( @$doc_array ) {
641
642 my $status = -1;
643 my $docid = $doc_array_rec->{'docid'};
644
645 push(@all_docids,$docid);
646
647 my $metaname = $doc_array_rec->{'metaname'};
648 if(defined $metaname) {
649 my $metapos = $doc_array_rec->{'metapos'}; # can legitimately be undef
650 my $metavalue = $doc_array_rec->{'metavalue'};
651 my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
652
653 $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode);
654 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
655 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
656
657 foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps
658 $metaname = $metatable_rec->{'metaname'};
659 my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
660 my $metapos = undef;
661 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
662
663 foreach my $metavalue ( @$metavals ) { # metavals is an array
664 $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode); # how do we use metamode in set_meta_entry?
665 if($metamode eq "override") { # now, having overridden the metavalue for the first,
666 # need to accumulate subsequent metavals for this metaname, else the just-assigned
667 # metavalue for this metaname will be lost
668 $metamode = "accumulate";
669 }
670 }
671 }
672 }
673
674 if ($status != 0) {
675 # Catch error if set infodb entry failed
676 $global_status = $status;
677 $global_mess .= "Failed to set metadata key: $docid\n";
678 $global_mess .= "Exit status: $status\n";
679 $global_mess .= "System Error Message: $!\n";
680 $global_mess .= "-" x 20;
681 }
682 }
683
684 if ($global_status != 0) {
685 $global_mess .= "PATH: $ENV{'PATH'}\n";
686 $gsdl_cgi->generate_error($global_mess);
687 }
688 else {
689 my $mess = "set-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
690 $gsdl_cgi->generate_ok_message($mess);
691 }
692}
693
694sub set_index_metadata_array
695{
696 my $self = shift @_;
697
698 my $username = $self->{'username'};
699 my $collect = $self->{'collect'};
700 my $gsdl_cgi = $self->{'gsdl_cgi'};
701# my $gsdlhome = $self->{'gsdlhome'};
702
703 if ($baseaction::authentication_enabled) {
704 # Ensure the user is allowed to edit this collection
705 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
706 }
707
708 my $site = $self->{'site'};
709 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
710
711 $gsdl_cgi->checked_chdir($collect_dir);
712
713 # Obtain the collect dir
714 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
715
716 # Make sure the collection isn't locked by someone else
717 $self->lock_collection($username, $collect);
718
719 $self->_set_index_metadata_array(@_);
720
721 # Release the lock once it is done
722 $self->unlock_collection($username, $collect);
723}
724
725# experimental, newly added in and untested
726sub _set_live_metadata_array
727{
728 my $self = shift @_;
729
730 my $collect = $self->{'collect'};
731 my $gsdl_cgi = $self->{'gsdl_cgi'};
732
733 my $site = $self->{'site'};
734 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
735
736
737 # look up additional args
738 my $infodbtype = $self->{'infodbtype'};
739 # To people who know $collect_tail please add some comments
740 # Obtain path to the database
741 my $collect_tail = $collect;
742 $collect_tail =~ s/^.*[\/|\\]//;
743 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
744 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
745
746
747 my $json_str = $self->{'json'};
748 my $doc_array = decode_json $json_str;
749
750
751 my $global_status = 0;
752 my $global_mess = "";
753
754 my @all_docids = ();
755
756
757 foreach my $doc_array_rec ( @$doc_array ) {
758
759 my $status = -1;
760 my $docid = $doc_array_rec->{'docid'};
761
762 push(@all_docids,$docid);
763
764 my $metaname = $doc_array_rec->{'metaname'};
765 if(defined $metaname) {
766 my $dbkey = "$docid.$metaname";
767 my $metavalue = $doc_array_rec->{'metavalue'};
768
769 # Set the new value
770 my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
771 $status = system($cmd);
772
773 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
774 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
775 foreach my $metatable_rec ( @$metatable ) {
776 $metaname = $metatable_rec->{'metaname'};
777 my $dbkey = "$docid.$metaname";
778
779 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
780 foreach my $metavalue ( @$metavals ) {
781 my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
782 $status = system($cmd);
783 }
784 }
785
786 }
787
788 if ($status != 0) {
789 # Catch error if gdbmget failed
790 $global_status = $status;
791 $global_mess .= "Failed to set metadata key: $docid\n"; # $dbkey
792 $global_mess .= "Exit status: $status\n";
793 $global_mess .= "System Error Message: $!\n";
794 $global_mess .= "-" x 20;
795 }
796 }
797
798 if ($global_status != 0) {
799 $global_mess .= "PATH: $ENV{'PATH'}\n";
800 $gsdl_cgi->generate_error($global_mess);
801 }
802 else {
803 my $mess = "set-live-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
804 $gsdl_cgi->generate_ok_message($mess);
805 }
806}
807
808sub set_live_metadata_array
809{
810 my $self = shift @_;
811
812 my $username = $self->{'username'};
813 my $collect = $self->{'collect'};
814 my $gsdl_cgi = $self->{'gsdl_cgi'};
815
816 if ($baseaction::authentication_enabled) {
817 # Ensure the user is allowed to edit this collection
818 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
819 }
820
821 my $site = $self->{'site'};
822 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
823
824 $gsdl_cgi->checked_chdir($collect_dir);
825
826 # Make sure the collection isn't locked by someone else
827 $self->lock_collection($username, $collect);
828
829 $self->_set_live_metadata_array(@_);
830
831 # Release the lock once it is done
832 $self->unlock_collection($username, $collect);
833}
834
835
836sub dxml_metadata
837{
838 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
839 my $metaname = $parser->{'parameters'}->{'metaname'};
840 my $metamode = $parser->{'parameters'}->{'metamode'};
841
842###!!! print STDERR "**** Processing closing </Metadata> tag\n";
843
844 my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
845
846 # Find the right metadata tag and checks if we are going to
847 # override it
848 #
849 # Note: This over writes the first metadata block it
850 # encountered. If there are multiple Sections in the doc.xml, it
851 # might not behave as you would expect
852
853 my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
854## print STDERR "**** checking $opt_doc_secnum <=> $curr_secnum\n" if (defined $opt_doc_secnum);
855## print STDERR "**** metamode = $metamode\n";
856
857 if ((!defined $opt_doc_secnum) || ($opt_doc_secnum eq $curr_secnum))
858 {
859 my $name_attr = $attrHash->{'name'};
860 # print STDOUT "*** testing: $name_attr eq $metaname ? and $metamode eq override ?\n";
861 if (($name_attr eq $metaname) && ($metamode eq "override"))
862 {
863 if (!defined $parser->{'parameters'}->{'poscount'})
864 {
865 $parser->{'parameters'}->{'poscount'} = 0;
866 }
867 else
868 {
869 $parser->{'parameters'}->{'poscount'}++;
870 }
871
872 if (defined $parser->{'parameters'}->{'metapos'}) { # if pos defined, ignore any prevmeta if also defined, and just wait for pos match
873
874 if ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}) {
875 ###print STDERR "#### got pos match!!\n";
876 # Get the value and override the current value
877 my $metavalue = $parser->{'parameters'}->{'metavalue'};
878 $attrHash->{'_content'} = $metavalue;
879
880 # until we have clearly determined the role of metamode in doc.xml files (we know what it means in import/metadata.xml files)
881 # we're not setting metamode to override here.
882
883 # Don't want it to wipe out any other pieces of metadata
884 $parser->{'parameters'}->{'metamode'} = "done";
885 }
886 }
887 elsif (defined $parser->{'parameters'}->{'prevmetavalue'}) { # if no pos defined but prevmeta defined, then wait for prevmeta match
888 if ($parser->{'parameters'}->{'prevmetavalue'} eq $attrHash->{'_content'}) {
889 ###print STDERR "### prev meta value matches\n";
890 my $metavalue = $parser->{'parameters'}->{'metavalue'};
891 $attrHash->{'_content'} = $metavalue;
892
893 # until we have clearly determined the role of metamode in doc.xml files (we know what it means in import/metadata.xml files)
894 # we're not setting metamode to override here.
895
896 $parser->{'parameters'}->{'metamode'} = "done";
897
898 }
899 }
900 # Note that before commit rev 32076, if neither metapos nor prevmeta defined, then code tried to set pos 0 for that metaname to the new value
901 # However that is not how the code works now (and couldn't get it to work that way yet after rev 32076).
902 # What it does now, and what Dr Bainbridge approved of is that:
903 # If no pos or prevmeta defined, then on override, the metavalue set for the specificed metaname becomes the ONLY instance of that metaname
904 # So any earlier instances of that metaname is removed and replaced by the single new instance.
905 }
906 }
907
908 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
909 return [$tagname => $attrHash];
910}
911
912# This method exists purely for catching invalid section numbers that the client
913# requested to edit. Once the parser has reached the end (the final </Archive> tag),
914# we've seen all the Sections in the doc.xml, and none of their section nums matched
915# if the metamode has not been set to 'done' by then.
916sub dxml_archive
917{
918 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
919 my $metamode = $parser->{'parameters'}->{'metamode'};
920
921 my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
922 my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
923
924 # print STDERR "@@@ $tagname Processing a closing </Archive> tag [$curr_secnum|$opt_doc_secnum]\n";
925
926 if ($metamode ne "done" && $curr_secnum ne $opt_doc_secnum) {
927 #print STDERR "@@@ $tagname Finished processing FINAL Section.\n";
928
929 my $metaname = $parser->{'parameters'}->{'metaname'};
930 my $metavalue = $parser->{'parameters'}->{'metavalue'};
931
932 print STDERR "@@@ Requested section number $opt_doc_secnum not found.\n";
933 print STDERR "\t(last seen section number in document was $curr_secnum)\n";
934 print STDERR "\tDiscarded metadata value '$metavalue' for meta '$metaname'\n";
935 print STDERR "\tin section $opt_doc_secnum.\n";
936 $parser->{'custom_err_msg'} = "Requested section number $opt_doc_secnum not found.";
937 }
938
939 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
940 return [$tagname => $attrHash];
941}
942
943sub dxml_description
944{
945 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
946 my $metamode = $parser->{'parameters'}->{'metamode'};
947
948 my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
949 my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'} || "";
950
951###!!! print STDERR "**** Processing a closing </Description> tag \n";
952 # print STDERR "@@@ $tagname Processing a closing </Description> tag [$curr_secnum|$opt_doc_secnum]\n";
953
954 # Accumulate the metadata
955
956 # We'll be accumulating metadata at this point if we haven't found and therefore
957 # haven't processed the metadata yet.
958 # For subsections, this means that if we're at a matching subsection, but haven't
959 # found the correct metaname to override in that subsection, we accumulate it as new
960 # meta in the subsection by adding it to the current description.
961 # If there's no subsection info for the metadata, it will accumulate at the top level
962 # section description if we hadn't found a matching metaname to override at this point.
963
964 # Both curr_secnum and opt_doc_secnum can be "". In the former case, it means we're now
965 # at the toplevel section. In the latter case, it means we want to process meta in the
966 # toplevel section. So the eq check between the values below will work in all cases.
967
968 # The only time this won't work is if an opt_doc_secnum beyond the section numbers of
969 # this document has been provided. In that case, the metadata for that opt_doc_secnum
970 # won't get attached/accumulated to any part of the doc, not even its top-level section.
971
972 if ($curr_secnum eq $opt_doc_secnum
973 && ($metamode eq "accumulate" || $metamode eq "override")) {
974 # note, metamode would have been set to done if a metadata operation had already succeeded.
975 # If we get here, we still have the metadata to append.
976 if ($metamode eq "override") {
977 print "Got to end of <Description> block. No metadata value to override. Switching 'metamode' to accumulate\n";
978 }
979
980 # If we get to here and metamode is override, this means there
981 # was no existing value to overide => treat as an append operation
982
983 # Tack a new metadata tag on to the end of the <Metadata>+ block
984 my $metaname = $parser->{'parameters'}->{'metaname'};
985 my $metavalue = $parser->{'parameters'}->{'metavalue'};
986
987 my $metadata_attr = {
988 '_content' => $metavalue,
989 'name' => $metaname,
990 'mode' => "accumulate"
991 };
992
993 my $append_metadata = [ "Metadata" => $metadata_attr ];
994 my $description_content = $attrHash->{'_content'};
995 print "Appending metadata to doc.xml\n";
996
997 if (ref($description_content)) {
998 # got some existing interesting nested content
999 push(@$description_content, " ", $append_metadata, "\n ");
1000 }
1001 else {
1002 #description_content is most likely a string such as "\n"
1003 $attrHash->{'_content'} = [$description_content, " ", $append_metadata ,"\n" ];
1004 }
1005
1006 $parser->{'parameters'}->{'metamode'} = "done";
1007 }
1008 else {
1009 # metamode most likely "done" signifying that it has already found a position to add the metadata to.
1010## print STDERR "**** NOT ACCUMULATE?!? \n";
1011 }
1012 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1013 return [$tagname => $attrHash];
1014}
1015
1016sub edit_xml_file
1017{
1018 my $self = shift @_;
1019 my ($gsdl_cgi, $filename, $start_rules, $rules, $options) = @_;
1020
1021 # use XML::Rules to add it in (read in and out again)
1022 my $parser = XML::Rules->new(start_rules => $start_rules,
1023 rules => $rules,
1024 style => 'filter',
1025 output_encoding => 'utf8' );
1026
1027 my $xml_in = "";
1028 if (!open(MIN,"<$filename")) {
1029 $gsdl_cgi->generate_error("Unable to read in $filename: $!");
1030 }
1031 else {
1032 # Read all the text in
1033 my $line;
1034 while (defined ($line=<MIN>)) {
1035 $xml_in .= $line if($line !~ m/^\s*$/); # preserve all but empty lines
1036 }
1037 close(MIN);
1038
1039 my $MOUT;
1040 if (!open($MOUT,">$filename")) {
1041 $gsdl_cgi->generate_error("Unable to write out to $filename: $!");
1042 }
1043 else {
1044 # Matched lines will get handled by the call backs
1045## my $xml_out = "";
1046
1047 binmode($MOUT,":utf8");
1048 $parser->filter($xml_in,$MOUT, $options);
1049
1050# binmode(MOUT,":utf8");
1051# print MOUT $xml_out;
1052 close($MOUT);
1053 }
1054 }
1055
1056 # copy across any custom error information that was stored during parsing
1057 $self->{'error_msg'} = $parser->{'custom_err_msg'} if(defined $parser->{'custom_err_msg'});
1058}
1059
1060sub edit_doc_xml
1061{
1062 my $self = shift @_;
1063 my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos, $metamode, $opt_secnum, $prevmetavalue) = @_;
1064
1065 my $info_mess = <<RAWEND;
1066****************************
1067 edit_doc_xml()
1068****************************
1069RAWEND
1070
1071 $info_mess .= " doc_xml_filename = $doc_xml_filename\n" if defined($doc_xml_filename);
1072 $info_mess .= " metaname = $metaname\n" if defined($metaname);
1073 $info_mess .= " metapos = $metapos\n" if defined($metapos);
1074 $info_mess .= " metavalue = $metavalue\n" if defined($metavalue);
1075 $info_mess .= " metamode = $metamode\n" if defined($metamode);
1076 $info_mess .= " opt_secnum = $opt_secnum\n" if defined($opt_secnum);
1077 $info_mess .= " prevmetaval = $prevmetavalue\n" if defined($prevmetavalue);
1078
1079 $info_mess .= "****************************\n";
1080
1081 $gsdl_cgi->generate_message($info_mess);
1082
1083 # To monitor which section/subsection number we are in
1084 my @start_rules =
1085 ( 'Section' => \&dxml_start_section );
1086
1087 # use XML::Rules to add it in (read in and out again)
1088 # Set the call back functions
1089 my @rules =
1090 ( _default => 'raw',
1091 'Metadata' => \&dxml_metadata,
1092 'Description' => \&dxml_description,
1093 'Archive' => \&dxml_archive); # just for catching errors at end
1094
1095 # Sets the parameters
1096 my $options = { 'metaname' => $metaname,
1097 'metapos' => $metapos,
1098 'metavalue' => $metavalue,
1099 'metamode' => $metamode,
1100 'prevmetavalue' => $prevmetavalue };
1101
1102 if (defined $opt_secnum) {
1103 $options->{'secnum'} = $opt_secnum;
1104 }
1105
1106 $self->edit_xml_file($gsdl_cgi,$doc_xml_filename,\@start_rules,\@rules,$options);
1107}
1108
1109sub set_archives_metadata_entry
1110{
1111 my $self = shift @_;
1112 my ($gsdl_cgi, $archive_dir, $collect_dir, $collect, $infodbtype, $docid, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue) = @_;
1113
1114 my $info_mess = <<RAWEND;
1115****************************
1116 set_archives_metadata_entry()
1117****************************
1118RAWEND
1119
1120 $info_mess .= " archive_dir = $archive_dir\n" if defined($archive_dir);
1121 $info_mess .= " collect_dir = $collect_dir\n" if defined($collect_dir);
1122 $info_mess .= " collect = $collect\n" if defined($collect);
1123 $info_mess .= " infodbtype = $infodbtype\n" if defined($infodbtype);
1124 $info_mess .= " docid = $docid\n" if defined($docid);
1125 $info_mess .= " metaname = $metaname\n" if defined($metaname);
1126 $info_mess .= " metapos = $metapos\n" if defined($metapos);
1127 $info_mess .= " metavalue = $metavalue\n" if defined($metavalue);
1128 $info_mess .= " metamode = $metamode\n" if defined($metamode);
1129 $info_mess .= " prevmetaval = $prevmetavalue\n" if defined($prevmetavalue);
1130
1131 $info_mess .= "****************************\n";
1132
1133 $gsdl_cgi->generate_message($info_mess);
1134
1135 # Obtain the doc.xml path for the specified docID
1136 my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
1137
1138 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
1139 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root);
1140 my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
1141
1142 # The $doc_xml_file is relative to the archives, and now let's get the full path
1143 my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");
1144 my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
1145
1146 # If we're overriding everything, then $metamode=override combined with $metapos=undefined and $prevmetavalue=undefined
1147 # in which case, we need to remove all metavalues for the metaname at the given (sub)section
1148 # Thereafter, we will finally be setting the overriding metavalue for this metaname
1149 if (!defined $prevmetavalue && !defined $metapos && $metamode eq "override") {
1150 print "override mode with no pos or prev value set. Removing all existing values for $metaname, and setting mode to accumulate for the new value.\n";
1151 # remove all values of $metaname metadata
1152 $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_xml_file), $metaname, undef, undef, $docid_secnum, $metamode);
1153 $metamode = "accumulate";
1154 }
1155 # Edit the doc.xml file with the specified metadata name, value and position.
1156 # TODO: there is a potential problem here as this edit_doc_xml function
1157 # is assuming the simple doc.xml situation where there is only one Section and no SubSections.
1158 # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml
1159
1160 # dxml_metadata method ignores metapos if metamode anything other than override
1161 $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
1162 $metaname,$metavalue,$metapos,$metamode,$docid_secnum,$prevmetavalue);
1163
1164 # return 0; # return 0 for now to indicate no error
1165 return (defined $self->{'error_msg'}) ? 1 : 0;
1166}
1167
1168
1169sub set_archives_metadata
1170{
1171 my $self = shift @_;
1172
1173 my $username = $self->{'username'};
1174 my $collect = $self->{'collect'};
1175 my $gsdl_cgi = $self->{'gsdl_cgi'};
1176
1177 if ($baseaction::authentication_enabled) {
1178 # Ensure the user is allowed to edit this collection
1179 $self->authenticate_user($username, $collect);
1180 }
1181
1182 # Make sure the collection isn't locked by someone else
1183 $self->lock_collection($username, $collect);
1184
1185 $self->_set_archives_metadata(@_);
1186
1187 # Release the lock once it is done
1188 $self->unlock_collection($username, $collect);
1189}
1190
1191sub _set_archives_metadata_array
1192{
1193 my $self = shift @_;
1194
1195 my $collect = $self->{'collect'};
1196 my $gsdl_cgi = $self->{'gsdl_cgi'};
1197 my $site = $self->{'site'};
1198 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1199
1200 # look up additional args
1201
1202 my $infodbtype = $self->{'infodbtype'};
1203
1204 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
1205
1206 my $json_str = $self->{'json'};
1207 my $doc_array = decode_json $json_str;
1208
1209
1210 my $global_status = 0;
1211 my $global_mess = "";
1212
1213 my @all_docids = ();
1214
1215 foreach my $doc_array_rec ( @$doc_array ) {
1216 my $status = -1;
1217 my $docid = $doc_array_rec->{'docid'};
1218
1219 push(@all_docids,$docid);
1220
1221 my $metaname = $doc_array_rec->{'metaname'};
1222 if(defined $metaname) {
1223
1224 my $metapos = $doc_array_rec->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
1225
1226 my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
1227 my $metavalue = $doc_array_rec->{'metavalue'};
1228 my $prevmetavalue = $self->{'prevmetavalue'}; # to make this sub behave as _set_archives_metadata
1229
1230
1231 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
1232 # make "accumulate" the default (less destructive, as it won't actually
1233 # delete any existing values)
1234 $metamode = "accumulate";
1235 }
1236
1237 $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
1238 $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
1239 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
1240 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
1241
1242 foreach my $metatable_rec ( @$metatable ) {
1243 $metaname = $metatable_rec->{'metaname'};
1244 my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
1245 my $metapos = undef;
1246 my $prevmetavalue = undef;
1247 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
1248
1249 foreach my $metavalue ( @$metavals ) {
1250 $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect,$infodbtype,
1251 $docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
1252
1253 if($metamode eq "override") { # now, having overridden the metavalue for the first,
1254 # need to accumulate subsequent metavals for this metaname, else the just-assigned
1255 # metavalue for this metaname will be lost
1256 $metamode = "accumulate";
1257 }
1258 }
1259 }
1260 }
1261
1262 if ($status != 0) {
1263 # Catch error if set infodb entry failed
1264 $global_status = $status;
1265 $global_mess .= "Failed to set metadata key: $docid\n";
1266 $global_mess .= "Exit status: $status\n";
1267 $global_mess .= "System Error Message: $!\n";
1268 $global_mess .= "-" x 20 . "\n";
1269 }
1270 }
1271
1272 if ($global_status != 0) {
1273 $global_mess .= "PATH: $ENV{'PATH'}\n";
1274 $gsdl_cgi->generate_error($global_mess);
1275 }
1276 else {
1277 my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
1278 $gsdl_cgi->generate_ok_message($mess);
1279 }
1280}
1281
1282sub set_archives_metadata_array
1283{
1284 my $self = shift @_;
1285
1286 my $username = $self->{'username'};
1287 my $collect = $self->{'collect'};
1288 my $gsdl_cgi = $self->{'gsdl_cgi'};
1289# my $gsdlhome = $self->{'gsdlhome'};
1290
1291 if ($baseaction::authentication_enabled) {
1292 # Ensure the user is allowed to edit this collection
1293 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1294 }
1295
1296 my $site = $self->{'site'};
1297 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1298
1299 $gsdl_cgi->checked_chdir($collect_dir);
1300
1301 # Obtain the collect dir
1302 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
1303
1304 # Make sure the collection isn't locked by someone else
1305 $self->lock_collection($username, $collect);
1306
1307 $self->_set_archives_metadata_array(@_);
1308
1309 # Release the lock once it is done
1310 $self->unlock_collection($username, $collect);
1311}
1312
1313sub _remove_archives_metadata
1314{
1315 my $self = shift @_;
1316
1317 my $collect = $self->{'collect'};
1318 my $gsdl_cgi = $self->{'gsdl_cgi'};
1319# my $gsdlhome = $self->{'gsdlhome'};
1320 my $infodbtype = $self->{'infodbtype'};
1321
1322 my $site = $self->{'site'};
1323
1324 # Obtain the collect and archive dir
1325 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1326
1327 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
1328
1329 # look up additional args
1330 my ($docid, $docid_secnum) = ($self->{'d'} =~ m/^(.*?)(\..*)?$/);
1331
1332 my $metaname = $self->{'metaname'};
1333 my $metapos = $self->{'metapos'};
1334 my $metavalue = $self->{'metavalue'};
1335
1336 $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
1337 $metavalue = undef if(defined $metavalue && ($metavalue =~ m/^\s*$/)); # necessary to force fallback to undef here
1338
1339 # if the user hasn't told us what to delete, not having given a metavalue or metapos,
1340 # default to deleting the first metavalue for the given metaname
1341 # Beware that if both metapos AND metavalue are defined, both matches (if any)
1342 # seem to get deleted in one single remove_archives_meta action invocation.
1343 # Similarly, if 2 identical metavalues for a metaname exist and that metavalue is being
1344 # deleted, both get deleted.
1345 if(!defined $metapos && !defined $metavalue) {
1346 $metapos = 0;
1347 }
1348
1349 my $metamode = $self->{'metamode'};
1350 $metamode = undef if(defined $metamode && ($metamode =~ m/^\s*$/));
1351
1352 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
1353 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
1354
1355 # This now stores the full pathname
1356 my $doc_filename = $doc_rec->{'doc-file'}->[0];
1357
1358 my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
1359# my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, undef, $docid_secnum);
1360
1361 if ($status == 0)
1362 {
1363 my $mess = "\nremove-archives-metadata successful: \nKey[$docid]\n";
1364 $mess .= " $metaname";
1365 $mess .= "->[$metapos]" if (defined $metapos);
1366 $mess .= " ($metavalue)" if (defined $metavalue);
1367 $gsdl_cgi->generate_ok_message($mess);
1368 }
1369 else
1370 {
1371 my $mess .= "Failed to remove archives metadata key: $docid\n";
1372 $mess .= "Exit status: $status\n";
1373 $mess .= "System Error Message: $!\n";
1374 $mess .= "-" x 20 . "\n";
1375
1376 $gsdl_cgi->generate_error($mess);
1377 }
1378
1379 #return $status; # in case calling functions have a use for this
1380}
1381
1382sub remove_archives_metadata
1383{
1384 my $self = shift @_;
1385
1386 my $username = $self->{'username'};
1387 my $collect = $self->{'collect'};
1388 my $gsdl_cgi = $self->{'gsdl_cgi'};
1389
1390 if ($baseaction::authentication_enabled)
1391 {
1392 # Ensure the user is allowed to edit this collection
1393 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1394 }
1395
1396 # Make sure the collection isn't locked by someone else
1397 $self->lock_collection($username, $collect);
1398
1399 $self->_remove_archives_metadata(@_);
1400
1401 # Release the lock once it is done
1402 $self->unlock_collection($username, $collect);
1403}
1404
1405sub remove_from_doc_xml
1406{
1407 my $self = shift @_;
1408 my ($gsdl_cgi, $doc_xml_filename, $metaname, $metapos, $metavalue, $secid, $metamode) = @_;
1409
1410 my @start_rules = ('Section' => \&dxml_start_section);
1411
1412 # Set the call-back functions for the metadata tags
1413 my @rules =
1414 (
1415 _default => 'raw',
1416 'Metadata' => \&rfdxml_metadata
1417 );
1418
1419 my $parser = XML::Rules->new
1420 (
1421 start_rules => \@start_rules,
1422 rules => \@rules,
1423 style => 'filter',
1424 output_encoding => 'utf8',
1425# normalisespaces => 1, # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
1426 stripspaces => 2|0|0 # ineffectual
1427 );
1428
1429 my $status = 0;
1430 my $xml_in = "";
1431 if (!open(MIN,"<$doc_xml_filename"))
1432 {
1433 $gsdl_cgi->generate_error("Unable to read in $doc_xml_filename: $!");
1434 $status = 1;
1435 }
1436 else
1437 {
1438 # Read them in
1439 my $line;
1440 while (defined ($line=<MIN>)) {
1441 $xml_in .= $line if($line !~ m/^\s*$/); # preserve all but empty lines
1442 }
1443 close(MIN);
1444
1445 # Filter with the call-back functions
1446 my $xml_out = "";
1447
1448 my $MOUT;
1449 if (!open($MOUT,">$doc_xml_filename")) {
1450 $gsdl_cgi->generate_error("Unable to write out to $doc_xml_filename: $!");
1451 $status = 1;
1452 }
1453 else {
1454 binmode($MOUT,":utf8");
1455 $parser->filter($xml_in, $MOUT, {metaname => $metaname, metapos => $metapos, metavalue => $metavalue, secid => $secid, metamode => $metamode});
1456 close($MOUT);
1457 }
1458 }
1459 return $status;
1460}
1461
1462sub rfdxml_metadata
1463{
1464 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1465
1466 # For comparisons, toplevel section is indicated by ""
1467 my $curr_sec_num = $parser->{'parameters'}->{'curr_section_num'} || "";
1468 my $secid = $parser->{'parameters'}->{'secid'} || "";
1469
1470 if (!($secid eq $curr_sec_num))
1471 {
1472 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1473 return [$tagname => $attrHash];
1474 }
1475
1476 if ($parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
1477 {
1478 if (!defined $parser->{'parameters'}->{'poscount'})
1479 {
1480 $parser->{'parameters'}->{'poscount'} = 0;
1481 }
1482 else
1483 {
1484 $parser->{'parameters'}->{'poscount'}++;
1485 }
1486
1487 # if overriding (for set-meta) but no metapos, then clear all the meta for this metaname
1488 if ((defined $parser->{'parameters'}->{'metamode'}) && ($parser->{'parameters'}->{'metamode'} eq "override") && (!defined $parser->{'parameters'}->{'metapos'}) &&(!defined $parser->{'parameters'}->{'metavalue'})) {
1489 return [];
1490 }
1491
1492 if ((defined $parser->{'parameters'}->{'metapos'}) && ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
1493 {
1494 return [];
1495 }
1496
1497 if ((defined $parser->{'parameters'}->{'metavalue'}) && ($parser->{'parameters'}->{'metavalue'} eq $attrHash->{'_content'}))
1498 {
1499 return [];
1500 }
1501 }
1502
1503 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1504 return [$tagname => $attrHash];
1505}
1506
1507sub mxml_metadata
1508{
1509 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1510 my $metaname = $parser->{'parameters'}->{'metaname'};
1511 my $metamode = $parser->{'parameters'}->{'metamode'};
1512
1513 # Report error if we don't see FileName tag before this
1514 die "Fatal Error: Unexpected metadata.xml structure. Undefined current_file, possibly encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
1515
1516 # Don't do anything if we are not in the right FileSet
1517 my $file_regexp = $parser->{'parameters'}->{'current_file'};
1518 if ($file_regexp =~ /\.\*/) {
1519 # Only interested in a file_regexp if it specifies precisely one
1520 # file.
1521 # So, skip anything with a .* in it as it is too general
1522## print STDERR "@@@@ Skipping entry in metadata.xml where FileName=.* as it is too general\n";
1523 return [$tagname => $attrHash];
1524 }
1525 my $src_file = $parser->{'parameters'}->{'src_file'};
1526 if (!($src_file =~ /$file_regexp/)) {
1527 return [$tagname => $attrHash];
1528 }
1529## print STDERR "*** mxl metamode = $metamode\n";
1530
1531 # Find the right metadata tag and checks if we are going to override it
1532 my $name_attr = $attrHash->{'name'};
1533 if (($name_attr eq $metaname) && ($metamode eq "override")) {
1534
1535 # now metadata.xml functions need to keep track of metapos
1536 if (!defined $parser->{'parameters'}->{'poscount'})
1537 {
1538 $parser->{'parameters'}->{'poscount'} = 0;
1539 }
1540 else
1541 {
1542 $parser->{'parameters'}->{'poscount'}++;
1543 }
1544
1545 # If either the metapos or prevmetavalue is set,
1546 # get the value and override the current value
1547 my $metavalue = $parser->{'parameters'}->{'metavalue'};
1548
1549 if(defined $parser->{'parameters'}->{'prevmetavalue'} && $parser->{'parameters'}->{'prevmetavalue'} eq $attrHash->{'_content'})
1550 {
1551 $attrHash->{'_content'} = $metavalue;
1552
1553 ## print STDERR "**** overriding metadata.xml\n";
1554
1555 # Don't want it to wipe out any other pieces of metadata
1556 $parser->{'parameters'}->{'metamode'} = "done";
1557 }
1558 elsif(defined $parser->{'parameters'}->{'metapos'} && $parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'})
1559 {
1560 $attrHash->{'_content'} = $metavalue;
1561 $parser->{'parameters'}->{'metamode'} = "done";
1562 }
1563 }
1564
1565 # mxml_description will process the metadata if metadata is accumulate,
1566 # or if we haven't found the metadata to override
1567
1568 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1569 return [$tagname => $attrHash];
1570}
1571
1572
1573sub mxml_description
1574{
1575 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1576 my $metamode = $parser->{'parameters'}->{'metamode'};
1577
1578 # Failed... Report error if we don't see FileName tag before this
1579 die "Fatal Error: Unexpected metadata.xml structure. Undefind current_file, possiblely encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
1580
1581 # Don't do anything if we are not in the right FileSet
1582 my $file_regexp = $parser->{'parameters'}->{'current_file'};
1583 if ($file_regexp =~ m/\.\*/) {
1584 # Only interested in a file_regexp if it specifies precisely one
1585 # file.
1586 # So, skip anything with a .* in it as it is too general
1587 return [$tagname => $attrHash];
1588 }
1589 my $src_file = $parser->{'parameters'}->{'src_file'};
1590
1591 if (!($src_file =~ m/$file_regexp/)) {
1592 return [$tagname => $attrHash];
1593 }
1594
1595 # Accumulate the metadata block to the end of the description block
1596 # Note: This adds metadata block to all description blocks, so if there are
1597 # multiple FileSets, it will add to all of them
1598 if (($metamode eq "accumulate") || ($metamode eq "override")) {
1599
1600 # if metamode was "override" but get to here then it failed to
1601 # find an item to override, in which case it should append its
1602 # value to the end, just like the "accumulate" mode
1603
1604 if ($metamode eq "override") {
1605 print "No metadata value to override. Switching 'metamode' to accumulate\n";
1606 }
1607
1608 # tack a new metadata tag on to the end of the <Metadata>+ block
1609 my $metaname = $parser->{'parameters'}->{'metaname'};
1610 my $metavalue = $parser->{'parameters'}->{'metavalue'};
1611
1612 my $metadata_attr = { '_content' => $metavalue,
1613 'name' => $metaname,
1614 'mode' => "accumulate" };
1615
1616 my $append_metadata = [ "Metadata" => $metadata_attr ];
1617 my $description_content = $attrHash->{'_content'};
1618
1619## print STDERR "*** appending to metadata.xml\n";
1620
1621 # append the new metadata element to the end of the current
1622 # content contained inside this tag
1623 if (ref($description_content) eq "") {
1624 # => string or numeric literal
1625 # this is caused by a <Description> block has no <Metadata> child elements
1626 # => set up an empty array in '_content'
1627 $attrHash->{'_content'} = [ "\n" ];
1628 $description_content = $attrHash->{'_content'};
1629 }
1630
1631 push(@$description_content, " ", $append_metadata, "\n ");
1632 $parser->{'parameters'}->{'metamode'} = "done";
1633 }
1634
1635 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1636 return [$tagname => $attrHash];
1637}
1638
1639
1640sub mxml_fileset
1641{
1642 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1643
1644 # Initilise the current_file
1645 # Note: According to http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd
1646 # FileName tag must come before Description tag
1647 $parser->{'parameters'}->{'current_file'} = "";
1648
1649 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1650 return [$tagname => $attrHash];
1651}
1652
1653sub mxml_directorymetadata
1654{
1655 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1656
1657 # if we haven't processed the metadata when we reach the end of metadata.xml
1658 # it's because there's no particular FileSet element whose FileName matched
1659 # In which case, add a new FileSet for this FileName
1660 my $metamode = $parser->{'parameters'}->{'metamode'};
1661 if($metamode ne "done") {
1662
1663 if ($metamode eq "override") {
1664 print "No metadata value to override. Switching 'metamode' to accumulate\n";
1665 }
1666
1667 # If we get to here and metamode is override, this means there
1668 # was no existing value to overide => treat as an append operation
1669
1670 # Create a new FileSet element and append to DirectoryMetadata
1671 # <FileSet>
1672 # <FileName>src_file</FileName>
1673 # <Description>
1674 # <Metadata mode="" name="">metavalue</Metadata>
1675 # </Description>
1676 # </FileSet>
1677 my $src_file = $parser->{'parameters'}->{'src_file'};
1678 my $metaname = $parser->{'parameters'}->{'metaname'};
1679 my $metavalue = $parser->{'parameters'}->{'metavalue'};
1680 my $metadata_attr = {
1681 '_content' => $metavalue,
1682 'name' => $metaname,
1683 'mode' => "accumulate"
1684 };
1685 my $append_metadata = [ "Metadata" => $metadata_attr ];
1686 my $description_attr = { '_content' => [ "\n\t\t ", $append_metadata, "\n\t\t"] };
1687 my $description_element = [ "Description" => $description_attr ];
1688
1689 #_content is not an attribute, it's special and holds the children of this element
1690 # including the textnode value embedded in this element if any.
1691 my $filename_attr = {'_content' => $src_file};
1692 my $filename_element = [ "FileName" => $filename_attr ];
1693
1694 my $fileset_attr = {};
1695 $fileset_attr->{'_content'} = [ "\n\t\t", $filename_element,"\n\t\t",$description_element ,"\n\t" ];
1696 my $fileset = [ "FileSet" => $fileset_attr ]; #my $fileset = [ "FileSet" => {} ];
1697
1698
1699 # get children of dirmeta, and push the new FileSet element onto it
1700 print "Appending metadata to metadata.xml\n";
1701 my $dirmeta_content = $attrHash->{'_content'};
1702 if (ref($dirmeta_content)) {
1703 # got some existing interesting nested content
1704 #push(@$dirmeta_content, " ", $fileset ,"\n ");
1705 push(@$dirmeta_content, "\t", $fileset ,"\n");
1706 }
1707 else {
1708 #description_content is most likely a string such as "\n"
1709 #$attrHash->{'_content'} = [$dirmeta_content, " ", $fileset ,"\n" ];
1710 $attrHash->{'_content'} = [$dirmeta_content, "\t", $fileset ,"\n" ];
1711 }
1712
1713 $parser->{'parameters'}->{'metamode'} = "done";
1714 }
1715 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1716 return [$tagname => $attrHash];
1717}
1718
1719
1720sub edit_metadata_xml
1721{
1722 my $self = shift @_;
1723 my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $metamode, $src_file, $prevmetavalue) = @_;
1724
1725 # Set the call-back functions for the metadata tags
1726 my @rules =
1727 ( _default => 'raw',
1728 'FileName' => \&mxml_filename,
1729 'Metadata' => \&mxml_metadata,
1730 'Description' => \&mxml_description,
1731 'FileSet' => \&mxml_fileset,
1732 'DirectoryMetadata' => \&mxml_directorymetadata);
1733
1734 # use XML::Rules to add it in (read in and out again)
1735 my $parser = XML::Rules->new(rules => \@rules,
1736 style => 'filter',
1737 output_encoding => 'utf8',
1738 stripspaces => 2|0|0); # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
1739
1740 if (!-e $metadata_xml_filename) {
1741
1742 if (open(MOUT,">$metadata_xml_filename")) {
1743
1744 my $src_file_re = &util::filename_to_regex($src_file);
1745 # shouldn't the following also be in the above utility routine??
1746 # $src_file_re =~ s/\./\\./g;
1747
1748 print MOUT "<?xml version=\"1.0\"?>\n";
1749 print MOUT "<DirectoryMetadata>\n";
1750 print MOUT " <FileSet>\n";
1751 print MOUT " <FileName>$src_file_re</FileName>\n";
1752 print MOUT " <Description>\n";
1753 print MOUT " </Description>\n";
1754 print MOUT " </FileSet>\n";
1755 print MOUT "</DirectoryMetadata>\n";
1756
1757 close(MOUT);
1758 }
1759 else {
1760 $gsdl_cgi->generate_error("Unable to create $metadata_xml_filename: $!");
1761 }
1762 }
1763
1764
1765 my $xml_in = "";
1766 if (!open(MIN,"<$metadata_xml_filename")) {
1767 $gsdl_cgi->generate_error("Unable to read in $metadata_xml_filename: $!");
1768 }
1769 else {
1770 # Read them in
1771 my $line;
1772 while (defined ($line=<MIN>)) {
1773 $xml_in .= $line if($line !~ m/^\s*$/); # preserve all but empty lines
1774 }
1775 close(MIN);
1776
1777 # Filter with the call-back functions
1778 my $xml_out = "";
1779
1780 my $MOUT;
1781 if (!open($MOUT,">$metadata_xml_filename")) {
1782 $gsdl_cgi->generate_error("Unable to write out to $metadata_xml_filename: $!");
1783 }
1784 else {
1785 binmode($MOUT,":utf8");
1786
1787 # Some wise person please find out how to keep the DTD and encode lines in after it gets filtered by this XML::Rules
1788 # At the moment, I will just hack it!
1789 #my $header_with_utf8_dtd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1790 #$header_with_utf8_dtd .= "<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">";
1791 #$xml_out =~ s/\<\?xml\sversion\=\"1.0\"\?\>/$header_with_utf8_dtd/;
1792 #print MOUT $xml_out;
1793
1794 $parser->filter($xml_in, $MOUT, { metaname => $metaname,
1795 metapos => $metapos,
1796 metavalue => $metavalue,
1797 metamode => $metamode,
1798 src_file => $src_file,
1799 prevmetavalue => $prevmetavalue,
1800 current_file => undef} );
1801 close($MOUT);
1802 }
1803 }
1804}
1805
1806
1807sub set_import_metadata
1808{
1809 my $self = shift @_;
1810
1811 my $username = $self->{'username'};
1812 my $collect = $self->{'collect'};
1813 my $gsdl_cgi = $self->{'gsdl_cgi'};
1814
1815 if ($baseaction::authentication_enabled) {
1816 # Ensure the user is allowed to edit this collection
1817 $self->authenticate_user($username, $collect);
1818 }
1819
1820 # Make sure the collection isn't locked by someone else
1821 $self->lock_collection($username, $collect);
1822
1823 $self->_set_import_metadata(@_);
1824
1825 # Release the lock once it is done
1826 $self->unlock_collection($username, $collect);
1827
1828}
1829
1830sub set_import_metadata_array
1831{
1832 my $self = shift @_;
1833
1834 my $username = $self->{'username'};
1835 my $collect = $self->{'collect'};
1836 my $gsdl_cgi = $self->{'gsdl_cgi'};
1837# my $gsdlhome = $self->{'gsdlhome'};
1838
1839 if ($baseaction::authentication_enabled) {
1840 # Ensure the user is allowed to edit this collection
1841 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1842 }
1843
1844 my $site = $self->{'site'};
1845 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1846
1847 $gsdl_cgi->checked_chdir($collect_dir);
1848
1849 # Make sure the collection isn't locked by someone else
1850 $self->lock_collection($username, $collect);
1851
1852 $self->_set_import_metadata_array(@_);
1853
1854 # Release the lock once it is done
1855 $self->unlock_collection($username, $collect);
1856
1857}
1858
1859
1860sub _set_import_metadata_array
1861{
1862 my $self = shift @_;
1863
1864 my $collect = $self->{'collect'};
1865 my $gsdl_cgi = $self->{'gsdl_cgi'};
1866
1867 my $site = $self->{'site'};
1868 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1869
1870 # look up additional args
1871
1872 my $infodbtype = $self->{'infodbtype'};
1873
1874 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
1875 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
1876
1877 my $json_str = $self->{'json'};
1878 my $doc_array = decode_json $json_str;
1879
1880 my $global_status = 0;
1881 my $global_mess = "";
1882
1883 my @all_docids = ();
1884
1885 foreach my $doc_array_rec ( @$doc_array )
1886 {
1887 my $status = -1;
1888 my $docid = $doc_array_rec->{'docid'};
1889
1890 my ($docid_root,$docid_secnum);
1891 if(defined $docid) {
1892 ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
1893 # as yet no support for setting subsection metadata in metadata.xml
1894 if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
1895 $gsdl_cgi->generate_message("*** docid: $docid. No support yet for setting import metadata at subsections level.\n");
1896 next; # skip this docid in for loop
1897 }
1898 }
1899
1900 push(@all_docids,$docid); # docid_root rather
1901
1902 my $metaname = $doc_array_rec->{'metaname'};
1903 if (defined $metaname) {
1904 my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
1905 my $metavalue = $doc_array_rec->{'metavalue'};
1906 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
1907
1908 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
1909 # make "accumulate" the default (less destructive, as it won't actually
1910 # delete any existing values)
1911 $metamode = "accumulate";
1912 }
1913
1914 # adding metapos and prevmetavalue support to import_metadata subroutines
1915 my $metapos = $doc_array_rec->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
1916 my $prevmetavalue = $self->{'prevmetavalue'};
1917
1918 $self->set_import_metadata_entry($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid_root, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue, $collect, $collect_dir); # at this point, docid_root = docid
1919
1920 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
1921 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
1922
1923 foreach my $metatable_rec ( @$metatable ) {
1924 $metaname = $metatable_rec->{'metaname'};
1925 my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
1926 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
1927 # make "accumulate" the default (less destructive, as it won't actually
1928 # delete any existing values)
1929 $metamode = "accumulate";
1930 }
1931
1932 # No support for metapos and prevmetavalue in the JSON metatable substructure
1933 my $metapos = undef;
1934 my $prevmetavalue = undef;
1935 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
1936
1937 foreach my $metavalue ( @$metavals ) {
1938 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
1939
1940 $self->set_import_metadata_entry($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid_root, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue, $collect, $collect_dir); # at this point, docid_root = docid
1941 if($metamode eq "override") { # now, having overridden the first metavalue of the metaname,
1942 # need to accumulate subsequent metavals for this metaname, else the just-assigned
1943 # metavalue for this metaname will be lost
1944 $metamode = "accumulate";
1945 }
1946 }
1947 }
1948 }
1949 }
1950
1951 # always a success message
1952 my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
1953 $gsdl_cgi->generate_ok_message($mess);
1954}
1955
1956# always returns true (1)
1957sub set_import_metadata_entry
1958{
1959 my $self = shift @_;
1960 my ($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue, $collect, $collect_dir) = @_;
1961
1962 my $info_mess = <<RAWEND;
1963****************************
1964 set_import_metadata_entry()
1965****************************
1966RAWEND
1967
1968 $info_mess .= " collect_dir = $collect_dir\n" if defined($collect_dir);
1969 $info_mess .= " collect = $collect\n" if defined($collect);
1970 $info_mess .= " infodbtype = $infodbtype\n" if defined($infodbtype);
1971 $info_mess .= " arcinfo_doc_filename = $arcinfo_doc_filename\n" if defined($arcinfo_doc_filename);
1972 $info_mess .= " docid = $docid\n" if defined($docid);
1973 $info_mess .= " metaname = $metaname\n" if defined($metaname);
1974 $info_mess .= " metapos = $metapos\n" if defined($metapos);
1975 $info_mess .= " metavalue = $metavalue\n" if defined($metavalue);
1976 $info_mess .= " metamode = $metamode\n" if defined($metamode);
1977 $info_mess .= " prevmetaval = $prevmetavalue\n" if defined($prevmetavalue);
1978
1979 $info_mess .= "****************************\n";
1980
1981 $gsdl_cgi->generate_message($info_mess);
1982
1983 # import works with metadata.xml which can have inherited metadata
1984 # so setting or removing at a metapos can have unintended effects for a COMPLEX collection
1985 # (a collection that has or can have inherited metadata). Metapos has expected behaviour for
1986 # a SIMPLE collection, which is one that doesn't have inherited metadata. Assume caller knows
1987 # what they're doing if they provide a metapos.
1988 if(defined $metapos) {
1989 print STDERR "@@@@ WARNING: metapos defined.\n";
1990 print STDERR "@@@@ Assuming SIMPLE collection and proceeding to modify the import meta at $metapos.\n";
1991 }
1992
1993 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
1994 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
1995 my $metadata_xml_file;
1996 my $import_filename = undef;
1997
1998 if (defined $docid) {
1999 # my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2000 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2001
2002 # This now stores the full pathname
2003 $import_filename = $doc_rec->{'src-file'}->[0];
2004 $import_filename = &util::placeholders_to_abspath($import_filename);
2005
2006 } else { # only for set_import_meta, not the case when calling method is set_import_metadata_array
2007 # as the array version of the method doesn't support the -f parameter yet
2008 my $import_file = $self->{'f'};
2009 $import_filename = &util::filename_cat($collect_dir,$collect,$import_file);
2010 }
2011
2012 # figure out correct metadata.xml file [?]
2013 # Assuming the metadata.xml file is next to the source file
2014 # Note: This will not work if it is using the inherited metadata from the parent folder
2015 my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
2016 my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
2017
2018 # If we're overriding everything, then $prevmetavalue=undefined and
2019 # $metamode=override combined with $metapos=undefined
2020 # in which case we need to remove all metavalues for the metaname at the given (sub)section
2021 # Thereafter, we will finally be able to set the overriding metavalue for this metaname
2022 if(!defined $prevmetavalue && !defined $metapos && $metamode eq "override") {
2023## print STDERR "@@@ REMOVING all import metadata for $metaname\n";
2024 $self->remove_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, undef, $import_tailname, $metamode); # we're removing all values, so metavalue=undef
2025
2026 }
2027
2028 # Edit the metadata.xml
2029 # Modified by Jeffrey from DL Consulting
2030 # Handle the case where there is one metadata.xml file for multiple FileSets
2031 # The XML filter needs to know whether it is in the right FileSet
2032 # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
2033 # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
2034 $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname,
2035 $metapos, $metavalue, $metamode, $import_tailname, $prevmetavalue);
2036 #return 0;
2037 return $metadata_xml_filename;
2038}
2039
2040sub _remove_import_metadata
2041{
2042 my $self = shift @_;
2043
2044 my $collect = $self->{'collect'};
2045 my $gsdl_cgi = $self->{'gsdl_cgi'};
2046# my $gsdlhome = $self->{'gsdlhome'};
2047 my $infodbtype = $self->{'infodbtype'};
2048
2049 # Obtain the collect dir
2050 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2051 my $site = $self->{'site'};
2052 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2053
2054 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2055 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
2056
2057 # look up additional args
2058 my $docid = $self->{'d'};
2059 if ((!defined $docid) || ($docid =~ m/^\s*$/))
2060 {
2061 $gsdl_cgi->generate_error("No docid (d=...) specified.\n");
2062 }
2063
2064 my $metaname = $self->{'metaname'};
2065 my $metapos = $self->{'metapos'};
2066 my $metavalue = $self->{'metavalue'};
2067
2068 $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
2069 $metavalue = undef if(defined $metavalue && ($metavalue =~ m/^\s*$/));
2070
2071 if(defined $metavalue) { # metavalue is now a compulsory arg for remove_import_metadata()
2072 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
2073 } elsif (!defined $metapos) { # if given no metavalue or metapos to delete, default to deleting the 1st
2074 $metapos = 0;
2075 }
2076 my $metamode = $self->{'metamode'};
2077 $metamode = undef if(defined $metamode && ($metamode =~ m/^\s*$/));
2078
2079 # import works with metadata.xml which can have inherited metadata
2080 # so setting or removing at a metapos can have unintended effects for a COMPLEX collection
2081 # (a collection that has or can have inherited metadata). Metapos has expected behaviour for
2082 # a SIMPLE collection, which is one that doesn't have inherited metadata. Assume caller knows
2083 # what they're doing if they provide a metapos.
2084 if(defined $metapos) {
2085 print STDERR "@@@@ WARNING: metapos defined.\n";
2086 print STDERR "@@@@ Assuming SIMPLE collection and proceeding to modify the import meta at $metapos.\n";
2087 }
2088
2089 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
2090 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
2091 my $metadata_xml_file;
2092 my $import_filename = undef;
2093 if (defined $docid)
2094 {
2095 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2096 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2097
2098 # This now stores the full pathname
2099 $import_filename = $doc_rec->{'src-file'}->[0];
2100 $import_filename = &util::placeholders_to_abspath($import_filename);
2101 }
2102
2103 if((!defined $import_filename) || ($import_filename =~ m/^\s*$/))
2104 {
2105 $gsdl_cgi->generate_error("There is no metadata\n");
2106 }
2107
2108 # figure out correct metadata.xml file [?]
2109 # Assuming the metadata.xml file is next to the source file
2110 # Note: This will not work if it is using the inherited metadata from the parent folder
2111 my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
2112 my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
2113
2114 $self->remove_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $import_tailname, $metamode); # metamode has no meaning for removing meta, but is used by set_meta when overriding All
2115
2116 my $mess = "remove-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
2117 $mess .= " $metaname";
2118 $mess .= " = $metavalue\n";
2119
2120 $gsdl_cgi->generate_ok_message($mess);
2121
2122 #return $status; # in case calling functions have a use for this
2123}
2124
2125sub remove_import_metadata
2126{
2127 my $self = shift @_;
2128
2129 my $username = $self->{'username'};
2130 my $collect = $self->{'collect'};
2131 my $gsdl_cgi = $self->{'gsdl_cgi'};
2132
2133 if ($baseaction::authentication_enabled) {
2134 # Ensure the user is allowed to edit this collection
2135 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2136 }
2137
2138 # Make sure the collection isn't locked by someone else
2139 $self->lock_collection($username, $collect);
2140
2141 $self->_remove_import_metadata(@_);
2142
2143 # Release the lock once it is done
2144 $self->unlock_collection($username, $collect);
2145
2146}
2147
2148sub remove_from_metadata_xml
2149{
2150 my $self = shift @_;
2151 my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $src_file, $metamode) = @_;
2152 # metamode generally has no meaning for removing meta, but is used by set_meta
2153 # when overriding all metavals for a metaname, in which case remove_meta is called with metamode
2154
2155 # Set the call-back functions for the metadata tags
2156 my @rules =
2157 (
2158 _default => 'raw',
2159 'Metadata' => \&rfmxml_metadata,
2160 'FileName' => \&mxml_filename
2161 );
2162
2163 my $parser = XML::Rules->new
2164 (
2165 rules => \@rules,
2166 style => 'filter',
2167 output_encoding => 'utf8',
2168 #normalisespaces => 1,
2169 stripspaces => 2|0|0 # ineffectual
2170 );
2171
2172 my $xml_in = "";
2173 if (!open(MIN,"<$metadata_xml_filename"))
2174 {
2175 $gsdl_cgi->generate_error("Unable to read in $metadata_xml_filename: $!");
2176 }
2177 else
2178 {
2179 # Read them in
2180 my $line;
2181 while (defined ($line=<MIN>)) {
2182 $xml_in .= $line if($line !~ m/^\s*$/); # preserve all but empty lines
2183 }
2184 close(MIN);
2185
2186 # Filter with the call-back functions
2187 my $xml_out = "";
2188
2189 my $MOUT;
2190 if (!open($MOUT,">$metadata_xml_filename")) {
2191 $gsdl_cgi->generate_error("Unable to write out to $metadata_xml_filename: $!");
2192 }
2193 else {
2194 binmode($MOUT,":utf8");
2195 $parser->filter($xml_in, $MOUT, {metaname => $metaname, metapos => $metapos, metavalue => $metavalue, src_file => $src_file, metamode => $metamode, current_file => undef});
2196 close($MOUT);
2197 }
2198 }
2199}
2200
2201sub rfmxml_metadata
2202{
2203 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2204
2205 # metadata.xml does not handle subsections
2206
2207 # since metadata.xml now has to deal with metapos, we keep track of the metadata position
2208 if (($parser->{'parameters'}->{'src_file'} eq $parser->{'parameters'}->{'current_file'})
2209 && $parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
2210 {
2211 if (!defined $parser->{'parameters'}->{'poscount'})
2212 {
2213 $parser->{'parameters'}->{'poscount'} = 0;
2214 }
2215 else
2216 {
2217 $parser->{'parameters'}->{'poscount'}++;
2218 }
2219
2220 # if overriding but no metapos, then clear all the meta for this metaname
2221 if ((defined $parser->{'parameters'}->{'metamode'}) && ($parser->{'parameters'}->{'metamode'} eq "override") && (!defined $parser->{'parameters'}->{'metapos'}) && (!defined $parser->{'parameters'}->{'metavalue'})) {
2222 return [];
2223 }
2224
2225 if ((defined $parser->{'parameters'}->{'metapos'}) && ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
2226 {
2227 return [];
2228 }
2229 if ((defined $parser->{'parameters'}->{'metavalue'}) && ($attrHash->{'_content'} eq $parser->{'parameters'}->{'metavalue'}))
2230 {
2231 return [];
2232 }
2233 }
2234
2235 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2236 return [$tagname => $attrHash];
2237}
2238
2239sub _remove_live_metadata
2240{
2241 my $self = shift @_;
2242
2243 my $collect = $self->{'collect'};
2244 my $gsdl_cgi = $self->{'gsdl_cgi'};
2245# my $gsdlhome = $self->{'gsdlhome'};
2246 my $infodbtype = $self->{'infodbtype'};
2247
2248 # Obtain the collect dir
2249 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2250 my $site = $self->{'site'};
2251 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2252
2253
2254 # look up additional args
2255 my $docid = $self->{'d'};
2256 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
2257 $gsdl_cgi->generate_error("No docid (d=...) specified.");
2258 }
2259
2260 # Generate the dbkey
2261 my $metaname = $self->{'metaname'};
2262 my $dbkey = "$docid.$metaname";
2263
2264 # To people who know $collect_tail please add some comments
2265 # Obtain the live gdbm_db path
2266 my $collect_tail = $collect;
2267 $collect_tail =~ s/^.*[\/|\\]//;
2268 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
2269 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
2270
2271 # Remove the key
2272 my $cmd = "gdbmdel \"$infodb_file_path\" \"$dbkey\"";
2273 my $status = system($cmd);
2274 if ($status != 0) {
2275 # Catch error if gdbmdel failed
2276 my $mess = "Failed to delete metadata key: $dbkey\n";
2277
2278 $mess .= "PATH: $ENV{'PATH'}\n";
2279 $mess .= "cmd = $cmd\n";
2280 $mess .= "Exit status: $status\n";
2281 $mess .= "System Error Message: $!\n";
2282
2283 $gsdl_cgi->generate_error($mess);
2284 }
2285 else {
2286 $gsdl_cgi->generate_ok_message("DB remove successful: Key[$metaname]");
2287 }
2288
2289}
2290
2291sub remove_live_metadata
2292{
2293 my $self = shift @_;
2294
2295 my $username = $self->{'username'};
2296 my $collect = $self->{'collect'};
2297 my $gsdl_cgi = $self->{'gsdl_cgi'};
2298 my $gsdlhome = $self->{'gsdlhome'};
2299
2300 if ($baseaction::authentication_enabled) {
2301 # Ensure the user is allowed to edit this collection
2302 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2303 }
2304
2305 # Make sure the collection isn't locked by someone else
2306 $self->lock_collection($username, $collect);
2307
2308 $self->_remove_live_metadata(@_);
2309
2310 $self->unlock_collection($username, $collect);
2311}
2312
2313sub remove_metadata
2314{
2315 my $self = shift @_;
2316
2317 my $where = $self->{'where'};
2318 if(!$where || ($where =~ m/^\s*$/)) {
2319 $self->remove_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
2320 return;
2321 }
2322
2323 my $username = $self->{'username'};
2324 my $collect = $self->{'collect'};
2325 my $gsdl_cgi = $self->{'gsdl_cgi'};
2326
2327 if ($baseaction::authentication_enabled) {
2328 # Ensure the user is allowed to edit this collection
2329 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2330 }
2331
2332 # Make sure the collection isn't locked by someone else
2333 $self->lock_collection($username, $collect);
2334
2335 # check which directories need to be processed, specified in $where as
2336 # any combination of import|archives|index|live
2337 if($where =~ m/import/) {
2338 $self->_remove_import_metadata(@_);
2339 }
2340 if($where =~ m/archives/) {
2341 $self->_remove_archives_metadata(@_);
2342 }
2343 if($where =~ m/index/) {
2344 $self->_remove_index_metadata(@_);
2345 }
2346
2347 # Release the lock once it is done
2348 $self->unlock_collection($username, $collect);
2349}
2350
2351# the internal version, without authentication
2352sub _remove_index_metadata
2353{
2354 my $self = shift @_;
2355
2356 my $collect = $self->{'collect'};
2357 my $gsdl_cgi = $self->{'gsdl_cgi'};
2358# my $gsdlhome = $self->{'gsdlhome'};
2359 my $infodbtype = $self->{'infodbtype'};
2360
2361 # Obtain the collect dir
2362 my $site = $self->{'site'};
2363 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2364 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2365
2366 # look up additional args
2367 my $docid = $self->{'d'};
2368 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
2369 $gsdl_cgi->generate_error("No docid (d=...) specified.");
2370 }
2371 my $metaname = $self->{'metaname'};
2372 my $metapos = $self->{'metapos'};
2373 my $metavalue = $self->{'metavalue'};
2374
2375 $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
2376 $metavalue = undef if(defined $metavalue && ($metavalue =~ m/^\s*$/)); # necessary to force fallback to undef here
2377
2378 # To people who know $collect_tail please add some comments
2379 # -> In collection groups, I think collect_tailname is the subcollection name,
2380 # e.g. colgroup-name/col-tail-name
2381 # Obtain the path to the database
2382 my $collect_tail = $collect;
2383 $collect_tail =~ s/^.*[\/|\\]//;
2384 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
2385 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
2386
2387 # Read the docid entry
2388 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
2389
2390 # Check to make sure the key does exist
2391 if (!defined ($doc_rec->{$metaname})) {
2392 $gsdl_cgi->generate_error("No metadata field \"" . $metaname . "\" in the specified document: [" . $docid . "]");
2393 }
2394
2395 # Obtain the specified metadata pos
2396 # if no metavalue or metapos to delete, default to deleting the 1st value for the metaname
2397 if(!defined $metapos && !defined $metavalue) {
2398 $metapos = 0;
2399 }
2400
2401
2402 # consider check key is defined before deleting?
2403 # Loop through the metadata array and ignore the specified position
2404 my $filtered_metadata = [];
2405 my $num_metadata_vals = scalar(@{$doc_rec->{$metaname}});
2406 for (my $i=0; $i<$num_metadata_vals; $i++) {
2407 my $metaval = shift(@{$doc_rec->{$metaname}});
2408
2409 if (!defined $metavalue && $i != $metapos) {
2410 push(@$filtered_metadata,$metaval);
2411 }
2412
2413 if(defined $metavalue && !($metavalue eq $metaval))
2414 {
2415 push(@$filtered_metadata,$metaval);
2416 }
2417 }
2418 $doc_rec->{$metaname} = $filtered_metadata;
2419
2420 ## Use the dbutil set_entry method instead of assuming the database is gdbm
2421 my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path, $docid, $doc_rec);
2422
2423 if ($status != 0) {
2424 my $mess = "Failed to set metadata key: $docid\n";
2425
2426 $mess .= "PATH: $ENV{'PATH'}\n";
2427 $mess .= "Exit status: $status\n";
2428 $mess .= "System Error Message: $!\n";
2429
2430 $gsdl_cgi->generate_error($mess);
2431 }
2432 else {
2433 my $mess = "DB set (with item deleted) successful: Key[$docid]\n";
2434 $mess .= " $metaname";
2435 $mess .= "->[$metapos]" if (defined $metapos);
2436 $mess .= " ($metavalue)" if (defined $metavalue);
2437
2438 $gsdl_cgi->generate_ok_message($mess);
2439 }
2440
2441 #return $status; # in case calling functions have a use for this
2442}
2443
2444sub remove_index_metadata
2445{
2446 my $self = shift @_;
2447
2448 my $username = $self->{'username'};
2449 my $collect = $self->{'collect'};
2450 my $gsdl_cgi = $self->{'gsdl_cgi'};
2451# my $gsdlhome = $self->{'gsdlhome'};
2452
2453 if ($baseaction::authentication_enabled) {
2454 # Ensure the user is allowed to edit this collection
2455 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2456 }
2457
2458 # Obtain the collect dir
2459 my $site = $self->{'site'};
2460 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2461 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2462
2463 # Make sure the collection isn't locked by someone else
2464 $self->lock_collection($username, $collect);
2465
2466 $self->_remove_index_metadata(@_);
2467
2468 # Release the lock once it is done
2469 $self->unlock_collection($username, $collect);
2470}
2471
2472#################################################################################
2473# ERASE META METHODS: ERASE ALL VALUES FOR MATCHING METANAME AT SPECIFIED DOCID #
2474#################################################################################
2475
2476sub erase_metadata
2477{
2478 my $self = shift @_;
2479
2480 my $where = $self->{'where'};
2481 # when $where is unspecified, following behaviour of sub remove_metadata by defaulting to erasing metadata from index
2482 if(!$where || ($where =~ m/^\s*$/)) {
2483 $self->erase_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
2484 return;
2485 }
2486
2487 my $username = $self->{'username'};
2488 my $collect = $self->{'collect'};
2489 my $gsdl_cgi = $self->{'gsdl_cgi'};
2490
2491 if ($baseaction::authentication_enabled) {
2492 # Ensure the user is allowed to edit this collection
2493 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2494 }
2495
2496 # Make sure the collection isn't locked by someone else
2497 $self->lock_collection($username, $collect);
2498
2499 # check which directories need to be processed, specified in $where as
2500 # any combination of import|archives|index|live
2501 if($where =~ m/import/) {
2502 $self->_erase_import_metadata(@_);
2503 }
2504 if($where =~ m/archives/) {
2505 $self->_erase_archives_metadata(@_);
2506 }
2507 if($where =~ m/index/) {
2508 $self->_erase_index_metadata(@_);
2509 }
2510
2511 # Release the lock once it is done
2512 $self->unlock_collection($username, $collect);
2513}
2514
2515# the internal version, without authentication
2516sub _erase_index_metadata
2517{
2518 my $self = shift @_;
2519
2520 my $collect = $self->{'collect'};
2521 my $gsdl_cgi = $self->{'gsdl_cgi'};
2522# my $gsdlhome = $self->{'gsdlhome'};
2523 my $infodbtype = $self->{'infodbtype'};
2524
2525 # Obtain the collect dir
2526 my $site = $self->{'site'};
2527 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2528 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2529
2530 # look up additional args
2531 my $docid = $self->{'d'};
2532 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
2533 $gsdl_cgi->generate_error("No docid (d=...) specified.");
2534 }
2535 my $metaname = $self->{'metaname'};
2536 my $metapos = undef;
2537 my $metavalue = undef;
2538
2539 # To people who know $collect_tail please add some comments
2540 # -> In collection groups, I think collect_tailname is the subcollection name,
2541 # e.g. colgroup-name/col-tail-name
2542 # Obtain the path to the database
2543 my $collect_tail = $collect;
2544 $collect_tail =~ s/^.*[\/|\\]//;
2545 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
2546 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
2547
2548 # Read the docid entry
2549 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
2550
2551 # Check to make sure the key does exist
2552 if (defined ($doc_rec->{$metaname})) {
2553
2554 $doc_rec->{$metaname} = [];
2555
2556 ## Use the dbutil set_entry method instead of assuming the database is gdbm
2557 my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path, $docid, $doc_rec);
2558
2559 if ($status != 0) {
2560 my $mess = "Failed to erase metadata key: $docid\n";
2561
2562 $mess .= "PATH: $ENV{'PATH'}\n";
2563 $mess .= "Exit status: $status\n";
2564 $mess .= "System Error Message: $!\n";
2565
2566 $gsdl_cgi->generate_error($mess);
2567 }
2568 else {
2569 my $mess = "DB set (with item deleted) successful: Key[$docid]\n";
2570 $mess .= " $metaname";
2571 $mess .= "->[$metapos]" if (defined $metapos);
2572 $mess .= " ($metavalue)" if (defined $metavalue);
2573
2574 $gsdl_cgi->generate_ok_message($mess);
2575 }
2576 }
2577 else { # no such metaname. Is it really an error?
2578 $gsdl_cgi->generate_ok_message("Can't erase. No metadata field \"" . $metaname . "\" in the specified document: [" . $docid . "]");
2579 }
2580 #return $status; # in case calling functions have a use for this
2581}
2582
2583sub erase_index_metadata
2584{
2585 my $self = shift @_;
2586
2587 my $username = $self->{'username'};
2588 my $collect = $self->{'collect'};
2589 my $gsdl_cgi = $self->{'gsdl_cgi'};
2590# my $gsdlhome = $self->{'gsdlhome'};
2591
2592 if ($baseaction::authentication_enabled) {
2593 # Ensure the user is allowed to edit this collection
2594 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2595 }
2596
2597 # Obtain the collect dir
2598 my $site = $self->{'site'};
2599 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2600 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2601
2602 # Make sure the collection isn't locked by someone else
2603 $self->lock_collection($username, $collect);
2604
2605 $self->_erase_index_metadata(@_);
2606
2607 # Release the lock once it is done
2608 $self->unlock_collection($username, $collect);
2609}
2610
2611
2612# Given a metaname, removes ALL metadata with that metaname at the provided docid
2613sub _erase_archives_metadata
2614{
2615 my $self = shift @_;
2616
2617 my $collect = $self->{'collect'};
2618 my $gsdl_cgi = $self->{'gsdl_cgi'};
2619# my $gsdlhome = $self->{'gsdlhome'};
2620 my $infodbtype = $self->{'infodbtype'};
2621
2622 my $site = $self->{'site'};
2623
2624 # Obtain the collect and archive dir
2625 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2626
2627 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
2628
2629 # look up additional args
2630 my ($docid, $docid_secnum) = ($self->{'d'} =~ m/^(.*?)(\..*)?$/);
2631
2632 my $metaname = $self->{'metaname'};
2633
2634 # metapos, metavalue and metamode are never provided and therefore all undefined for erase_archives_metadata
2635 my $metapos = undef;
2636 my $metavalue = undef;
2637 my $metamode = "override"; # when override and no metapos or metaval, all values for metaname will be affected
2638
2639 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2640 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2641
2642 # This now stores the full pathname
2643 my $doc_filename = $doc_rec->{'doc-file'}->[0];
2644
2645 my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
2646
2647 if ($status == 0)
2648 {
2649 my $mess = "\nerase-archives-metadata successful: \nKey[$docid]\n";
2650 $mess .= " $metaname";
2651 $gsdl_cgi->generate_ok_message($mess);
2652 }
2653 else
2654 {
2655 my $mess .= "Failed to erase archives metadata key: $docid\n";
2656 $mess .= "Exit status: $status\n";
2657 $mess .= "System Error Message: $!\n";
2658 $mess .= "-" x 20 . "\n";
2659
2660 $gsdl_cgi->generate_error($mess);
2661 }
2662
2663 #return $status; # in case calling functions have a use for this
2664}
2665
2666# practically identical to remove_archives_metadata
2667sub erase_archives_metadata
2668{
2669 my $self = shift @_;
2670
2671 my $username = $self->{'username'};
2672 my $collect = $self->{'collect'};
2673 my $gsdl_cgi = $self->{'gsdl_cgi'};
2674
2675 if ($baseaction::authentication_enabled)
2676 {
2677 # Ensure the user is allowed to edit this collection
2678 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2679 }
2680
2681 # Make sure the collection isn't locked by someone else
2682 $self->lock_collection($username, $collect);
2683
2684 $self->_erase_archives_metadata(@_);
2685
2686 # Release the lock once it is done
2687 $self->unlock_collection($username, $collect);
2688}
2689
2690sub _erase_import_metadata
2691{
2692 my $self = shift @_;
2693
2694 my $collect = $self->{'collect'};
2695 my $gsdl_cgi = $self->{'gsdl_cgi'};
2696# my $gsdlhome = $self->{'gsdlhome'};
2697 my $infodbtype = $self->{'infodbtype'};
2698
2699 # Obtain the collect dir
2700 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2701 my $site = $self->{'site'};
2702 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2703
2704 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2705 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
2706
2707 # look up additional args
2708 my $docid = $self->{'d'};
2709 if ((!defined $docid) || ($docid =~ m/^\s*$/))
2710 {
2711 $gsdl_cgi->generate_error("No docid (d=...) specified.\n");
2712 }
2713
2714 my $metaname = $self->{'metaname'};
2715 my $metapos = undef;
2716 my $metavalue = undef;
2717 my $metamode = "override";
2718
2719 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
2720 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
2721 my $metadata_xml_file;
2722 my $import_filename = undef;
2723 if (defined $docid)
2724 {
2725 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2726 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2727
2728 # This now stores the full pathname
2729 $import_filename = $doc_rec->{'src-file'}->[0];
2730 $import_filename = &util::placeholders_to_abspath($import_filename);
2731 }
2732
2733 if((!defined $import_filename) || ($import_filename =~ m/^\s*$/))
2734 {
2735 $gsdl_cgi->generate_error("There is no metadata\n");
2736 }
2737
2738 # figure out correct metadata.xml file [?]
2739 # Assuming the metadata.xml file is next to the source file
2740 # Note: This will not work if it is using the inherited metadata from the parent folder
2741 my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
2742 my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
2743
2744 $self->remove_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $import_tailname, $metamode); # metamode=override, means all values of metaname are affected
2745
2746 my $mess = "erase-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
2747 $mess .= " $metaname";
2748 $mess .= " = $metavalue\n";
2749
2750 $gsdl_cgi->generate_ok_message($mess);
2751
2752 #return $status; # in case calling functions have a use for this
2753}
2754
2755sub erase_import_metadata
2756{
2757 my $self = shift @_;
2758
2759 my $username = $self->{'username'};
2760 my $collect = $self->{'collect'};
2761 my $gsdl_cgi = $self->{'gsdl_cgi'};
2762
2763 if ($baseaction::authentication_enabled) {
2764 # Ensure the user is allowed to edit this collection
2765 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2766 }
2767
2768 # Make sure the collection isn't locked by someone else
2769 $self->lock_collection($username, $collect);
2770
2771 $self->_erase_import_metadata(@_);
2772
2773 # Release the lock once it is done
2774 $self->unlock_collection($username, $collect);
2775
2776}
2777
2778# removes all meta with matching metaname for the specified docid from the live metadata index
2779sub _erase_live_metadata
2780{
2781 my $self = shift @_;
2782 # At present, _remove_live_metadata doesn't take metaval or metapos either and therefore has the desired effect
2783 $self->_remove_live_metadata(@_);
2784
2785}
2786
2787sub erase_live_metadata
2788{
2789 my $self = shift @_;
2790
2791 my $username = $self->{'username'};
2792 my $collect = $self->{'collect'};
2793 my $gsdl_cgi = $self->{'gsdl_cgi'};
2794 my $gsdlhome = $self->{'gsdlhome'};
2795
2796 if ($baseaction::authentication_enabled) {
2797 # Ensure the user is allowed to edit this collection
2798 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2799 }
2800
2801 # Make sure the collection isn't locked by someone else
2802 $self->lock_collection($username, $collect);
2803
2804 $self->_erase_live_metadata(@_);
2805
2806 $self->unlock_collection($username, $collect);
2807}
2808
2809
2810# Was trying to reused the codes, but the functions need to be broken
2811# down more before they can be reused, otherwise there will be too
2812# much overhead and duplicate process...
2813sub insert_metadata
2814{
2815 my $self = shift @_;
2816
2817 my $username = $self->{'username'};
2818 my $collect = $self->{'collect'};
2819 my $gsdl_cgi = $self->{'gsdl_cgi'};
2820 my $gsdlhome = $self->{'gsdlhome'};
2821 my $infodbtype = $self->{'infodbtype'};
2822
2823 # If the import metadata and gdbm database have been updated, we
2824 # need to insert some notification to warn user that the the text
2825 # they see at the moment is not indexed and require a rebuild.
2826 my $rebuild_pending_macro = "_rebuildpendingmessage_";
2827
2828 if ($baseaction::authentication_enabled) {
2829 # Ensure the user is allowed to edit this collection
2830 $self->authenticate_user($username, $collect);
2831 }
2832
2833 # Obtain the collect and archive dir
2834 my $site = $self->{'site'};
2835 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2836 ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2837 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
2838
2839 # Make sure the collection isn't locked by someone else
2840 $self->lock_collection($username, $collect);
2841
2842 # Check additional args
2843 my $docid = $self->{'d'};
2844 if (!defined($docid) || ($docid =~ m/^\s*$/)) {
2845 $gsdl_cgi->generate_error("No document id is specified: d=...");
2846 }
2847 my $metaname = $self->{'metaname'};
2848 if (!defined($metaname) || ($metaname =~ m/^\s*$/)) {
2849 $gsdl_cgi->generate_error("No metaname is specified: metadataname=...");
2850 }
2851 my $metavalue = $self->{'metavalue'};
2852 if (!defined($metavalue) || ($metavalue =~ m/^\s*$/)) {
2853 $gsdl_cgi->generate_error("No metavalue or empty metavalue is specified: metadataname=...");
2854 }
2855 # make "accumulate" the default (less destructive, as it won't actually
2856 # delete any existing values)
2857 my $metamode = "accumulate";
2858
2859 # metapos/prevmetavalue were never before used in this subroutine, so set them to undefined
2860 my $metapos = undef;
2861 my $prevmetavalue = undef;
2862
2863 #=======================================================================#
2864 # set_import_metadata [START]
2865 #=======================================================================#
2866 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
2867 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
2868 my $metadata_xml_file;
2869 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2870 my $archive_doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2871
2872 # This now stores the full pathname
2873 my $import_filename = $archive_doc_rec->{'src-file'}->[0];
2874 $import_filename = &util::placeholders_to_abspath($import_filename);
2875
2876 # figure out correct metadata.xml file [?]
2877 # Assuming the metadata.xml file is next to the source file
2878 # Note: This will not work if it is using the inherited metadata from the parent folder
2879 my ($import_tailname, $import_dirname)
2880 = File::Basename::fileparse($import_filename);
2881 my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
2882
2883 # Shane's escape characters
2884 $metavalue = pack "U0C*", unpack "C*", $metavalue;
2885 $metavalue =~ s/\,/&#44;/g;
2886 $metavalue =~ s/\:/&#58;/g;
2887 $metavalue =~ s/\|/&#124;/g;
2888 $metavalue =~ s/\(/&#40;/g;
2889 $metavalue =~ s/\)/&#41;/g;
2890 $metavalue =~ s/\[/&#91;/g;
2891 $metavalue =~ s/\\/&#92;/g;
2892 $metavalue =~ s/\]/&#93;/g;
2893 $metavalue =~ s/\{/&#123;/g;
2894 $metavalue =~ s/\}/&#125;/g;
2895 $metavalue =~ s/\"/&#34;/g;
2896 $metavalue =~ s/\`/&#96;/g;
2897 $metavalue =~ s/\n/_newline_/g;
2898
2899 # Edit the metadata.xml
2900 # Modified by Jeffrey from DL Consulting
2901 # Handle the case where there is one metadata.xml file for multiple FileSets
2902 # The XML filter needs to know whether it is in the right FileSet
2903 # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
2904 # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
2905 $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname,
2906 $metapos, $metavalue, $metamode, $import_tailname, $prevmetavalue);
2907 #=======================================================================#
2908 # set_import_metadata [END]
2909 #=======================================================================#
2910
2911
2912 #=======================================================================#
2913 # set_metadata (accumulate version) [START]
2914 #=======================================================================#
2915 # To people who know $collect_tail please add some comments
2916 # Obtain path to the database
2917 my $collect_tail = $collect;
2918 $collect_tail =~ s/^.*[\/|\\]//;
2919 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
2920 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
2921
2922 # Read the docid entry
2923 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
2924
2925 # Protect the quotes
2926 $metavalue =~ s/\"/\\\"/g;
2927
2928 # Adds the pending macro
2929 my $macro_metavalue = $rebuild_pending_macro . $metavalue;
2930
2931 # If the metadata doesn't exist, create a new one
2932 if (!defined($doc_rec->{$metaname})){
2933 $doc_rec->{$metaname} = [ $macro_metavalue ];
2934 }
2935 # Else, let's acculumate the values
2936 else {
2937 push(@{$doc_rec->{$metaname}},$macro_metavalue);
2938 }
2939
2940 ## Use the dbutil set_entry method instead of assuming the database is gdbm
2941 my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path, $docid, $doc_rec);
2942
2943 if ($status != 0) {
2944 # Catch error if gdbmget failed
2945 my $mess = "Failed to set metadata key: $docid\n";
2946
2947 $mess .= "PATH: $ENV{'PATH'}\n";
2948 $mess .= "Exit status: $status\n";
2949 $mess .= "System Error Message: $!\n";
2950
2951 $gsdl_cgi->generate_error($mess);
2952 }
2953 else {
2954 my $mess = "insert-metadata successful: Key[$docid]\n";
2955 $mess .= " [In metadata.xml] $metaname";
2956 $mess .= " = $metavalue\n";
2957 $mess .= " [In database] $metaname";
2958 $mess .= " = $macro_metavalue\n";
2959 $mess .= " The new text has not been indexed, rebuilding collection is required\n";
2960 $gsdl_cgi->generate_ok_message($mess);
2961 }
2962 #=======================================================================#
2963 # set_metadata (accumulate version) [END]
2964 #=======================================================================#
2965
2966 # Release the lock once it is done
2967 $self->unlock_collection($username, $collect);
2968}
2969
2970# not returning 1; here since this file is conditionally included by metadataction.pm
2971# and not otherwise meant to be used on its own
2972
Note: See TracBrowser for help on using the repository browser.