-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnoid
executable file
·2090 lines (1703 loc) · 75.7 KB
/
noid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -Tw -Ilib -I../lib
#
# noid - a Perl script that mints and binds nice opaque identifiers
# using the Noid.pm module. This script can be invoked additionally
# via a URL interface as "noidu...", which formats output for the web.
#
# Author: John A. Kunze, jak@ucop.edu, California Digital Library
# Orginally created Nov. 2002 at UCSF Center for Knowledge Management
#
# ---------
# Copyright (c) 2002-2006 UC Regents
#
# Permission to use, copy, modify, distribute, and sell this software and
# its documentation for any purpose is hereby granted without fee, provided
# that (i) the above copyright notices and this permission notice appear in
# all copies of the software and related documentation, and (ii) the names
# of the UC Regents and the University of California are not used in any
# advertising or publicity relating to the software without the specific,
# prior written permission of the University of California.
#
# THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
# EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
# WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY
# SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
# THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE
# OR PERFORMANCE OF THIS SOFTWARE.
# ---------
# XXX change 'NOID' as database name to 'dbnoid' (for case insensitive filesys)
# XXX fix To Do alphabets
# XXX document an example of how to set up a rewrite rule that responds
# to the ? and ?? at the end of an id, and convert to a CGI string
# XXX add java interface
# XXX fix env test to suggest that NFS and AFS filesystems not be used
# XXX why does dbopen fail when doing dbinfo from an account that can't
# write the file -- should be doing readonly open
# XXX record literal noid dbcreate (re)creation command used into README
use strict;
#sub get_untainted_PERL5LIB {
# my $key;
# my $perl_5_lib = $ENV{"PERL5LIB"};
# #my @lib_list = ('./Noid/lib');
# my @lib_list = ('./lib');
# ! defined($perl_5_lib) and
# return(@lib_list);
# if ($perl_5_lib =~ /^(\/\S+)$/) {
# push @lib_list, $1;
# return(@lib_list);
# }
# die(qq@Format of variable "PERL5LIB" ($perl_5_lib) invalid.\n@);
#}
#
#use lib get_untainted_PERL5LIB( );
use Config; # Now do Brian McCauley's perl5lib.pm trick for untainting.
use lib map { /(.*)/ } split /$Config{path_sep}/ => $ENV{PERL5LIB};
use Text::ParseWords;
use Getopt::Long;
use BerkeleyDB;
use Noid;
my $web = 0;
my ($dbdir, $dbname, $debug, $locktest, $ver, $help, $contact, $bulkcmd);
my ($template, $snaa, $total);
my (@valid_helptopics, %info); # purposely undefined for now
my @valid_commands = qw(
bind dbinfo dbcreate fetch get hello help hold
mint note peppermint queue validate
);
# yyy make a noidmail (email robot interface?)
# yyy location field for redirect should include a discriminant
# eg, ^c for client choice, ^i for ipaddr, ^f format, ^l language
# and ^b for browser type, ^xyz for any http header??
# yyy add "file" command, like bind, but stores a file, either as file or
# in a big concatenation stream (binding offset, length, checksum)?
# yyy figure out whether validate needs to open the database, and if not,
# what that means
# yyy for locking and transactions: (1) ask Paul Marquess about what
# Perl interface support for txn and locking really is (2) check into
# lock and/or transaction timeout, (3) if I use cursors exclusively for
# storage, that may solve everything(?) (eg, no more simple tied assigments,
# which are db_put's in disguise, (4) make sure Noid.pm exit block releases
# locks, aborts transactions, etc.
# main
{
my $line;
if ($0 =~ m|noidu[^/]*$|) { # if called with the URL interface
$web = 1; # orient output for HTTP
print "Content-Type: text/plain\n\n";
open(STDERR, ">&STDOUT")
or die("Can't combine stderr and stdout: $!\n");
! defined($ENV{'QUERY_STRING'}) and
die("No QUERY_STRING (hence no command) defined.\n");
($line = $ENV{'QUERY_STRING'}) =~ tr/+/ /;
@ARGV = shellwords($line);
#print "ARGV: " . join("|", @ARGV) . "\n";
}
if ($0 =~ m|noidr[^/]*$|) { # if called for RewriteMap resolving,
# see Apache Rewrite mod documentation
$| = 1; # very important to unbuffer the output
$bulkcmd = 1;
# yyy should we set a timeout to prevent hanging the server?
}
if (! ($contact = who_are_you($web))) {
print STDERR "Can't tell who you are: $!\n";
exit(1);
}
if (! GetOptions(
'debug' => \$debug, # flag
'locktest=i' => \$locktest, # flag
'f=s' => \$dbdir, # filesystem directory name
'version' => \$ver, # flag
'help' => \$help, # flag
)) {
print "error: GetOptions\n";
usage(1, 1, "intro");
exit(1);
}
if ($locktest) {
$locktest < 0 and
print("error: locktest value must be a positive "
. "number of seconds to sleep\n"),
exit(0);
Noid::locktest($locktest);
}
$web && $debug and
print "contact=$contact, pwd=", `pwd`;
# Handle -v or -h, and exit early.
if ($ver) {
# We take our version number from the Noid module version.
print qq@This is "noid" version $Noid::VERSION.\n@;
exit(0);
}
if ($help) {
# yyy should we encode help output? print "help:\n";
usage(0, 0, "intro");
exit(0);
}
# Now try to find a database directory string.
# In the special case of dbcreate, we may create
# and name the directory on behalf of the user.
#
if (! defined($dbdir)) {
defined($ENV{'NOID'}) and # is NOID env variable defined?
$dbdir = $ENV{'NOID'},
1 or
$0 =~ m|_([^/]+)$| and # executable link reveals dbdir?
$dbdir = $1,
1 or
$dbdir = '.', # else try current directory
;
if (! defined($dbdir) || $dbdir !~ /\S/) {
print "error: no Dbdir\n";
usage(1, 1, "intro");
exit(1);
}
}
elsif ($web) {
print qq@-f option not allowed in URL interface.\n@;
return(0);
}
# Now untaint $dbdir. yyy we can do better?
$dbdir =~ m|^(.*)$| and
$dbdir = $1
or
print("error: bad Dbdir\n"),
usage(1, 1, "intro"),
exit(1)
;
$dbname = "$dbdir/NOID/noid.bdb";
# Bulk command mode is signified by a single final argument of "-".
# If we're _not_ in bulk command mode, expect a single command
# represented by the remaining arguments; do it and exit.
#
$bulkcmd ||= ($#ARGV == 0 && $ARGV[0] eq "-");
if (! $bulkcmd) {
do_command(@ARGV);
exit(0);
}
# If we get here, we're in bulk command mode. Read, tokenize,
# and execute commands from the standard input. Test with
# curl --data-binary @cmd_file http://dot.ucop.edu/nd/noidu_kt5\?-
# where cmd_file contains newline-separated commands.
# XXX make sure to %-decode web QUERY_STRING, so we don't have
# to always put +'s for spaces
#
while (($line = <STDIN>)) {
do_command(shellwords($line));
}
exit(0);
}
sub do_command {
# Any remaining args should form a noid command.
# Look at the command part (if any) now, and complain about
# a non-existent database unless the command is "dbcreate".
#
my $command = shift;
if (! defined($command)) { # if no command arg
usage(1, 1, "intro");
return(0);
}
if (! -f $dbname
&& $command ne 'dbcreate' && $command ne 'help') {
# if the database doesn't exist when it needs to
bprint(*STDERR, "error: no database ($dbname) "
. "-- use dbcreate?\n\n");
usage(1, 1, "intro");
return(0)
}
if (grep(/^$command$/, @valid_commands) != 1) {
print "error: no such command: $command (",
join(" ", @_), ")\n";
usage(1, 1, "intro");
return(0);
}
# Perform extra checks in $web case.
if ($web && $command eq 'dbcreate') {
print qq@error: command "$command" not allowed in URL interface.\n@;
usage(1, 1, "intro");
return(0);
}
# It should now be safe to turn off strict 'refs' when we
# invoke a command via its subroutine name.
#if ($#_ < 0) {
# usage(1); # yyy say something senstive about $command
#usage(1, 1, "intro");
# return(0);
#}
no strict 'refs';
&$command(@_);
}
#
# --- begin almost alphabetic listing of functions ---
#
# yyy what is the sensible thing to do if (a) no element given,
# (b) if no value, or (c) if there are multiple values?
# yyy vbind(..., template, ...)? nvbind()?
#
# Returns number of elements successfully bound.
#
# yyy what about append at the list vs the string level?
sub bind { my( $how, $id, $elem, $value )=@_;
my $validate = 1;
my $noid = Noid::dbopen($dbname, 0);
if (! $noid) {
print STDERR Noid::errmsg($noid);
return(0);
}
my $report;
! defined($elem) and
$elem = "";
if ($elem eq ":") { # expect name/value pairs up to blank line
defined($value) and
print(STDERR "Why give a value ($value) with an "
. qq@element "$elem"?\n@),
Noid::dbclose($noid),
return(0);
# To slurp paragraph, apparently safest to use local $/, which
local $/; # disappears when scope exits.
$/ = "\n\n"; # Means paragraph mode.
my $para = <STDIN> || "";
chop $para; # yyy needed?
$para =~ s/^#.*\n//g; # remove comment lines
$para =~ s/\n\s+/ /g; # merge continuation lines
my @elemvals = split(/^([^:]+)\s*:\s*/m, $para);
shift @elemvals; # throw away first null
my ($bound, $total) = (0, 0);
while (1) {
($elem, $value) = (shift @elemvals, shift @elemvals);
! defined($elem) && ! defined($value) and
last;
$total++;
! defined($elem) and
Noid::addmsg($noid,
"error: $id: bad element associated "
. qq@with value "$value".@),
last;
! defined($value) and
$value = "",
1 or
chop $value
;
$report = Noid::bind($noid, $contact, $validate,
$how, $id, $elem, $value);
! defined($report) and
print(STDERR $report, "\n"),
usage(1, 1, "bind"),
# yyy how/who should log failures in "hard" case
or
$bound++,
print($report, "\n"),
;
}
# yyy summarize for log $total and $bound
Noid::dbclose($noid);
return(defined($report) ? 1 : 0);
}
elsif ($elem eq ":-") { # expect name/value to be rest of file
defined($value) and
print(STDERR "Why give a value ($value) with an "
. qq@element "$elem"?\n@),
Noid::dbclose($noid),
return(0);
# while (<STDIN>) {
# next if /^#/ || /^\s*\n/;
# last; # end at first non-blank, non-comment
# }
# chop;
# ! defined($_) || ! s/^(\w+)\s*:\s*// and
# Noid::addmsg($noid, "error: $id no element to bind."),
# Noid::dbclose($noid),
# return(0);
# $elem = $1;
# $value = $_;
# # To slurp file, apparently safest is to use local $/, which
# local $/; # disappears when scope exits.
# $value .= <STDIN>; # $/==undef means file mode.
# Read all of STDIN into array "@input_lines".
my @input_lines = <STDIN>;
# Remove all newlines.
foreach (@input_lines) {
chomp;
}
# Ignore any leading lines that start with a pound sign
# or contain nothing but white space.
while (scalar(@input_lines) > 0) {
if ((substr($input_lines[0], 0, 1) eq "#") ||
($input_lines[0] =~ /^\s*$/)) {
shift @input_lines;
next;
}
last;
}
# If we don't have any lines, there's a problem.
if (scalar(@input_lines) == 0) {
print STDERR "error: no non-blank, non-comment ",
"input.\n";
Noid::dbclose($noid);
return(0);
}
# There must be an element and a colon on the first line.
unless ($input_lines[0] =~ /^\s*(\w+)\s*:\s*(.*)$/) {
print STDERR "error: missing element or colon on ",
"first non-blank, non-comment line.\n";
Noid::dbclose($noid);
return(0);
}
# Save the element, and any part of the value that there
# might be on the first line.
$elem = $1;
$value = $2;
# Remove the first line from the array.
shift @input_lines;
# Append any additional lines to the value.
foreach (@input_lines) {
$value .= "\n" . $_;
}
# Put on the final newline.
$value .= "\n";
#
# Now drop through to end of if-elsif clause to real binding.
}
# yyy eg, :fragment:Offset:Length:Path
# yyy eg, :fragment:Offset:Length:Path
# yyy eg, :file:Path
# yyy eg, ":xml",
elsif ($elem =~ /^:/) {
print(STDERR qq@Binding to element syntax "$elem" @
. "not supported.\n");
Noid::dbclose($noid);
return(0);
}
$report = Noid::bind($noid, $contact, $validate,
$how, $id, $elem, $value);
! defined($report) and
print(STDERR Noid::errmsg($noid)),
usage(0, 1, "bind")
or
print($report, "\n")
;
# yyy make sure return(0)'s do dbclose...
Noid::dbclose($noid);
return(defined($report) ? 1 : 0);
}
# This routine may not make sense in the URL interface.
#
sub dbcreate { my( $template, $policy, $naan, $naa, $subnaa )=@_;
my $dbreport = Noid::dbcreate($dbdir, $contact, $template, $policy,
$naan, $naa, $subnaa);
if (! $dbreport) {
print Noid::errmsg(), "\n";
return(0);
}
print $dbreport, "\n";
return(1);
}
sub dbinfo { my( $level )=@_;
$level = "brief"
if (! defined($level));
my $noid = Noid::dbopen($dbname, DB_RDONLY);
if (! $noid) {
print Noid::errmsg($noid);
return(0);
}
Noid::dbinfo($noid, $level);
Noid::dbclose($noid);
return(1);
}
sub fetch { my( $id, @elems )=@_;
return(getfetch(1, $id, @elems));
}
sub get { my( $id, @elems )=@_;
return(getfetch(0, $id, @elems));
}
sub getfetch { my( $verbose, $id, @elems )=@_;
my $noid = Noid::dbopen($dbname, DB_RDONLY);
if (! $noid) {
print STDERR Noid::errmsg($noid);
return(0);
}
my $fetched = Noid::fetch($noid, $verbose, $id, @elems);
! defined($fetched) and
print(STDERR Noid::errmsg($noid))
or
print($fetched),
$verbose && print("\n")
;
Noid::dbclose($noid);
return(1);
}
sub hello {
print "Hello.\n";
}
sub help { my( $topic )=@_;
my $in_error = 0;
my $brief = 0;
return(usage($in_error, $brief, $topic));
}
# yyy what about a "winnow" routine that is either started
# from cron or is started when an exiting noid call notices
# that there's some harvesting/garbage collecting to do and
# schedules it for, say, 10 minutes hence (by not exiting,
# but sleeping for 10 minutes and then harvesting)?
sub hold { my( $on_off, @ids )=@_;
my $noid = Noid::dbopen($dbname, 0);
if (! $noid) {
print STDERR Noid::errmsg($noid);
return(0);
}
if (! Noid::hold($noid, $contact, $on_off, @ids)) {
print(STDERR Noid::errmsg($noid));
usage(1, 1, "hold");
Noid::dbclose($noid);
return(0);
}
print(Noid::errmsg($noid), "\n"); # no error message at all
Noid::dbclose($noid);
return(1);
}
sub peppermint { my( $n, $elem, $value )=@_;
mint($n, $elem, $value, 1);
}
sub mint { my( $n, $elem, $value, $pepper )=@_;
if (defined($pepper)) {
print STDERR
"The peppermint command is not implemented yet.\n";
return(0);
}
if (! defined($n) || $n !~ /^\d+$/) {
print STDERR "Argument error: expected positive integer, got ",
(defined($n) ? qq@"$n"@ : "nothing"), "\n";
usage(1, 1, "mint");
return(0);
}
my $noid = Noid::dbopen($dbname, 0);
if (! $noid) {
print Noid::errmsg($noid);
return(0);
}
my $id;
while ($n--) {
if (! defined($id = Noid::mint($noid, $contact, $pepper))) {
print STDERR Noid::errmsg($noid);
Noid::dbclose($noid);
return(0);
}
print "id: $id\n";
}
Noid::dbclose($noid);
print "\n";
return(1);
}
sub note { my( $key, $value )=@_;
if (! defined($key) || ! defined($value)) {
print STDERR
"You must supply a key and a value.\n";
usage(1, 1, "note");
return(0);
}
my $noid = Noid::dbopen($dbname, 0);
! Noid::note($noid, $contact, $key, $value)
and print Noid::errmsg($noid);
Noid::dbclose($noid);
return(1);
}
sub queue { my( $when, @ids )=@_;
my $noid = Noid::dbopen($dbname, 0);
if (! $noid) {
print STDERR Noid::errmsg($noid);
return(0);
}
my @queued = Noid::queue($noid, $contact, $when, @ids);
my $retval;
! @queued and
$retval = 0,
print(STDERR Noid::errmsg($noid), "\n"),
1 or
$retval = 1,
print(join("\n", @queued), "\n"),
;
my $n = scalar(grep(! /^error:/, @queued));
print("note: $n identifier", ($n == 1 ? "" : "s"), " queued\n");
Noid::dbclose($noid);
return($retval);
}
# Returns the number of valid ids.
sub validate { my( $template, @ids )=@_;
my $noid = Noid::dbopen($dbname, DB_RDONLY);
if (! $noid) {
print Noid::errmsg($noid);
return(0);
}
my @valids = Noid::validate($noid, $template, @ids);
! @valids and
print(STDERR Noid::errmsg($noid)),
Noid::dbclose($noid),
usage(1, 1, "validate"),
return(0);
my @iderrs = grep(/^error:/, @valids);
print($_, "\n")
for (@valids);
Noid::dbclose($noid);
return(scalar(@ids) - scalar(@iderrs));
}
# Print a blank (space) in front of every newline.
# First arg must be a filehandle.
#
sub bprint { my( $out, @args )=@_;
map {s/\n/\n /g} @args;
return print $out @args;
}
# Always returns 1 so it can be used in boolean blocks.
#
sub usage { my( $in_error, $brief, $topic )=@_;
! defined($in_error) and
$in_error = 1; # default is to treat as error
$in_error and
$| = 1; # flush any pending output
my $out = # where to send output
($in_error ? *STDERR : *STDOUT);
! defined($brief) and
$brief = 1; # default is to be brief
$topic ||= "intro";
$topic = lc($topic);
# Initialize info topics if need be.
#
! @valid_helptopics and
init_help();
my @blurbs = grep(/^$topic/, @valid_helptopics);
if (scalar(@blurbs) != 1) {
print $out (scalar(@blurbs) < 1
? qq@Sorry: nothing under "$topic".\n@
: "Help: Your request ($topic), matches more than one "
. "topic:\n\t(" . join(", ", @blurbs) . ").\n"
),
" You might try one of these topics:";
my @topics = @valid_helptopics;
my $n = 0;
my $topics_per_line = 8;
while (1) {
! @topics and
print("\n "),
last
or
$n++ % $topics_per_line == 0 and
print("\n\t")
or
print(" ", shift(@topics))
;
}
print "\n\n";
return(1);
}
# If we get here, @blurbs names one story.
my $blurb = shift @blurbs;
# Big if-elsif clause to switch on requested topic.
#
# Note that we try to make the output conform to ANVL syntax;
# in the case of help output, every line tries to be a continuation
# line for the value of an element called "Usage". To do this we
# pass all output through a routine that just adds a space after
# every newline. The end of the output should end the ANVL record,
# so we print "\n\n" at the end.
#
my ($t, $i);
if ($blurb eq "intro") {
bprint $out,
qq@Usage:
noid [-f Dbdir] [-v] [-h] Command Arguments@, ($brief ? qq@
noid -h (for help with a Command summary).@
: qq@
Dbdir defaults to "." if not found from -f or a NOID environment variable.
For more information try "perldoc noid" or "noid help Command". Summary:
@);
$brief and
print("\n\n"),
return(1);
for $t (@valid_commands) {
$i = $info{"$t/brief"};
! defined($i) || ! $i and
next;
bprint $out, $i;
}
bprint $out, qq@
If invoked as "noidu...", output is formatted for a web client. Give Command
as "-" to run a block of noid Commands read from stdin or from POST data.@;
print "\n\n";
return(1);
}
#elsif $blurb eq "dbcreate" and print $out $info{$blurb}
#or
#$blurb eq "bind" and print $out
$brief and
$blurb .= "/brief";
$t = $info{$blurb};
if (! defined($t) || ! $t) {
print $out qq@Sorry: no information on "$blurb".\n\n@;
return(1);
}
bprint $out, $t;
print "\n";
return(1);
# yyy fix these verbose messages
my $yyyy = qq@
Called as "noid", an id generator accompanies every COMMAND. Called as
"noi", the id generator is supplied implicitly by looking first for a
NOID environment variable and, failing that, for a file calld ".noid" in
the current directory. Examples show the explicit form. To create a
generator, use
noid ck8 dbcreate TPL SNAA
where you replace TPL with a template that defines the shape and number
of all identifiers to be minted by this generator. You replace SNAA with
the name (eg, the initials) of the sub NAA (Name Assigning Authority) that
will be responsible for this generator; for example, if the Online Archive
of California is the sub-authority for a template, SNAA could be "oac".
This example of generator intialization,
noid oac.noid dbcreate pd2.wwdwwdc oac
sets up the "oac.noid" identifier generator. It can create "nice opaque
identifiers", such as "pd2pq5dk9z", suitable for use as persistent
identifiers should the supporting organization wish to provide such a
level of commitment. This generator is also capable of holding a simple
sequential counter (starting with 1), which some callers may wish to use
as an internal number to keep track of minted external identifiers.
[ currently accessible only via the count() routine ]
In the example template, "pd2" is a constant prefix for an identifier
generator capable of producing 70,728,100 identifiers before it runs out.
A template has the form "prefix.mask", where 'prefix' is a literal string
prepended to each identifier and 'mask' specifies the form of the generated
identifier that will appear after the prefix (but with no '.' between).
Mask characters are 'd' (decimal digit), 'w' (limited alpha-numeric
digit), 'c' (a generated check character that may only appear in the
terminal position).
Alternatively, if the mask contains an 's' (and no other letters), dbcreate
initializes a generator of sequential numbers. Instead of seemingly random
creates sequentially generated number. Use '0s'
to indicate a constant width number padded on the left with zeroes.
@;
return(1);
}
sub init_help {
# For convenient maintenance, we store individual topics in separate
# array elements. So as not to slow down script start up, we don't
# pre-load anything. In this way only the requester of help info,
# who does not need speed for this purpose, pays for it.
#
@valid_helptopics = qw(
intro all templates
);
push(@valid_helptopics, @valid_commands);
%info = (
'bind/brief' =>
q@
noid bind How Id Element Value # to bind an Id's Element, where
How is set|add|insert|new|replace|mint|append|prepend|delete|purge.
Use an Id of :idmap/Idpattern, Value=PerlReplacementPattern so that
fetch returns variable values. Use ":" as Element to read Elements
and Values up to a blank line from stdin (up to EOF with ":-").
@,
'bind' =>
q@@,
'dbinfo/brief' =>
q@@,
'dbinfo' =>
q@@,
'dbcreate/brief' =>
q@
noid dbcreate [ Template (long|-|short) [ NAAN NAA SubNAA ] ]
where Template=prefix.Tmask, T=(r|s|z), and mask=string of (e|d|k)
@,
'dbcreate' =>
q|
To create an identifier minter governed by Template and Term ("long" or "-"),
noid dbcreate [ Template Term [ NAAN NAA SubNAA ] ]
The Template gives the number and form of generated identifiers. Examples:
.rddd minter of random 3-digit numbers that stops after the 1000th
.zd sequential numbers without limit, adding new digits as needed
bc.sdddd sequential 4-digit numbers with constant prefix "bc"
.rdedeede .7 billion random ids, extended-digits at chars 2, 4, 5 and 7
fk.rdeeek .24 million random ids with prefix "fk" and final check char
For persistent identifiers, use "long" for Term, and specify the NAAN, NAA,
and SubNAA. Otherwise, use "-" for Term or omit it. The NAAN is a globally
registered Name Assigning Authority Number; for identifiers conforming to the
ARK scheme, this is a 5-digit number registered with ark@cdlib.org, or 00000.
The NAA is the character string equivalent registered for the NAAN; for
example, the NAAN, 13030, corresponds to the NAA, "cdlib.org". The SubNAA
is also a character string, but it is a locally determined and possibly
structured subauthority string (e.g., "oac", "ucb/dpg", "practice_area") that
is not globally registered.
|,
'fetch/brief' =>
q@
noid fetch Id Element ... # fetch/map one or more Elements
@,
'fetch' =>
q@
To bind,
noid bind replace fk0wqkb myGoto http://www.cdlib.org/foobar.html
sets "myGoto" element of identifier "fk0wqkb" to a string (here a URL).
@,
'get/brief' =>
q@
noid get Id Element ... # fetch/map Elements without labels
@,
'get' =>
q@@,
'hello/brief' =>
q@@,
'hello' =>
q@@,
'hold/brief' =>
q@
noid hold (set|release) Id ... # place or remove a "hold" on Id(s)
@,
'hold' =>
q@@,
'mint/brief' =>
q@
noid mint N [ Elem Value ] # to mint N identifiers (optionally binding)
@,
'mint' =>
q@@,
'note/brief' =>
q@@,
'note' =>
q@@,
'peppermint/brief' =>
q@@,
'peppermint' =>
q@@,
'queue/brief' =>
q@
noid queue (now|first|lvf|Time) Id ... # queue (eg, recycle) Id(s)
Time is NU, meaning N units, where U= d(ays) | s(econds).
With "lvf" (Lowest Value First) lowest value of id will mint first.
@,
'queue' =>
q@@,
'validate/brief' =>
q@
noid validate Template Id ... # to check if Ids are valid
Use Template of "-" to use the minter's native template.
@,
'validate' =>
q@@,
);
return(1);
}
sub who_are_you { my( $web )=@_;
my $user;
if ($web) {
$user = $ENV{'REMOTE_USER'} || '';
my $host = $ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'} || '';
$user .= '@' . $host;
}
# Look up by REAL_USER_ID first.
my ($name, undef, undef, $gid) = getpwuid($<);
my $ugid = getlogin() || $name;
! $ugid and
return "";
$ugid .= "/" . ((getgrgid($gid))[0] || "");
# If EFFECTIVE_USER_ID differs from REAL_USER_ID, get its info too.
if ($> ne $<) {
($name, undef, undef, $gid) = getpwuid($>);
! $name and
return "";
$ugid .= " ($name/" . ((getgrgid($gid))[0] || "") . ")";
}
$user = ($user ? "$user $ugid" : $ugid);
return $user;
}
exit 0;
1;
# yyy Possible for 'c' mask char:
# ASCII 33 to 126 (no SPACE, no DEL) --> 94 (not prime)
# MINUS the 5 chars: / \ - % " --> 89 (prime)
# or MINUS the 5 chars: / \ - % . --> 89 (prime)
# or MINUS the 5 chars: / \ - % SPACE --> 89 (prime)
#
# Note: current (1/2004) restrictions on ARKs are alphanums plus
# = @ $ _ * + #
# with the following reserved for special purposes
# / . - %
# yyy noid example: shuffle play (as in random song list)
# yyy bind is pair-wise or triple-wise? (how to explain consistently)
# yyy add java class to distro
# yyy add pdf of doc to distro
__END__
=pod
=for roff
.nr PS 12p
.nr VS 14.4p
=head1 NAME
noid - nice opaque identifier generator commands
=head1 SYNOPSIS
B<noid> [ B<-f> I<Dbdir> ] [ B<-vh> ] I<Command> I<Arguments>
=head1 DESCRIPTION
The B<noid> utility creates minters (identifier generators) and accepts
commands that operate them. Once created, a minter can be used to produce
persistent, globally unique names for documents, databases, images,
vocabulary terms, etc. Properly managed, these identifiers can be used as
long term durable information object references within naming schemes such
as ARK, PURL, URN, DOI, and LSID. At the same time, alternative minters
can be set up to produce short-lived names for transaction identifiers,
compact web server session keys, and other ephemera.
A B<noid> minter is a lightweight database designed for efficiently
generating, tracking, and binding unique identifiers, which are produced
without replacement in random or
sequential order, and with or without a check character that can be used
for detecting transcription errors. A minter can bind identifiers to
arbitrary element names and element values that are either stored or
produced upon retrieval from rule-based transformations of requested
identifiers, the latter having application in identifier resolution. Noid
minters are very fast, scalable, easy to create and tear down, and have a
relatively small footprint. They use BerkeleyDB as the underlying database.
An identifier generated by a B<noid> minter is also known generically
as a "noid" (standing for nice opaque identifier and rhyming with void).
While a minter can record and bind any identifiers
that you bring to its attention, often it is used to generate, bringing
to your attention, identifier strings that carry no widely recognizable
meaning. This semantic opaqueness reduces their vulnerability to era-Z<>
and language-specific change, and helps persistence by making for
identifiers that can age and travel well.
The form, number, and intended longevity of a minter's identifiers are given
by a Template and a Term supplied when the generator database is created.
A supplied Term of "long" establishes extra restrictions and logging
appropriate for the support of persistent identifiers. Across successive
minting operations, the generator "uses up" its namespace (the pool of
identifiers it is capable of minting) such that no identifier will ever be
generated twice unless the supplied Term is "short" and the namespace is
finite and completely exhausted. The default Term is "medium".
The B<noid> utility parameters -- flags, I<Dbdir> (database location),
I<Command>, I<Arguments> -- are described later under COMMANDS AND MODES.
There are also sections covering persistence, templates, rule-based
mapping, URL interface, and name resolution.
=head1 TUTORIAL INTRODUCTION
Once the noid utility is installed, the command,
noid dbcreate s.zd
will create a minter for an unlimited number of identifiers.
It produces a generator for medium term identifiers (the default) with
the Template, C<s.zd>, governing the order, number, and form of minted
identifier strings. These identifiers will begin with the constant part
C<s> and end in a digit (the final C<d>), all within an unbounded sequential
(C<z>) namespace. The TEMPLATES section gives a full explanation.
This generator will mint the identifiers, in order,
s0, s1, s2, ..., s9, s10, ..., s99, s100, ...
and never run out. To mint the first ten identifiers,
noid mint 10