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

Last change on this file since 37218 was 37218, checked in by davidb, 15 months ago

set-archives-assocfile can now pass data directly using 'filedata' (rather than a full-blown POST with file-update). This argument will be more conventient for transmitted CSV and JSON data interactively created within the browser

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