1 |
#! /usr/local/bin/ruby -w |
2 |
# :title:estcall |
3 |
#= Java interface of Hyper Estraier |
4 |
# Copyright (C) 2004-2005 Mikio Hirabayashi |
5 |
# All rights reserved. |
6 |
# This file is part of Hyper Estraier. |
7 |
# Redistribution and use in source and binary forms, with or without modification, are |
8 |
# permitted provided that the following conditions are met: |
9 |
# |
10 |
# * Redistributions of source code must retain the above copyright notice, this list of |
11 |
# conditions and the following disclaimer. |
12 |
# * Redistributions in binary form must reproduce the above copyright notice, this list of |
13 |
# conditions and the following disclaimer in the documentation and/or other materials |
14 |
# provided with the distribution. |
15 |
# * Neither the name of Mikio Hirabayashi nor the names of its contributors may be used to |
16 |
# endorse or promote products derived from this software without specific prior written |
17 |
# permission. |
18 |
# |
19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
20 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
21 |
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
22 |
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 |
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
24 |
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25 |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 |
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
27 |
# OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
|
29 |
|
30 |
require "estraier" |
31 |
|
32 |
include Estraier |
33 |
|
34 |
|
35 |
# global constants |
36 |
SEARCHMAX = 10 |
37 |
|
38 |
# global variables |
39 |
$pxhost = nil |
40 |
$pxport = 0 |
41 |
$timeout = -1 |
42 |
$authname = nil |
43 |
$authpass = nil |
44 |
$condopts = 0 |
45 |
|
46 |
|
47 |
# main routine |
48 |
def main(args) |
49 |
usage() if args.length < 1 |
50 |
rv = 0 |
51 |
case args[0] |
52 |
when "put" |
53 |
rv = runput(args) |
54 |
when "out" |
55 |
rv = runout(args) |
56 |
when "get" |
57 |
rv = runget(args) |
58 |
when "uriid" |
59 |
rv = runuriid(args) |
60 |
when "inform" |
61 |
rv = runinform(args) |
62 |
when "search" |
63 |
rv = runsearch(args) |
64 |
when "setuser" |
65 |
rv = runsetuser(args) |
66 |
when "setlink" |
67 |
rv = runsetlink(args) |
68 |
else |
69 |
usage() |
70 |
end |
71 |
return rv |
72 |
end |
73 |
|
74 |
|
75 |
# print usage and exit |
76 |
def usage() |
77 |
printf("%s: command line utility for the node API of Hyper Estraier\n", $0) |
78 |
printf("\n") |
79 |
printf("usage:\n") |
80 |
printf(" %s put [-proxy host port] [-tout num] [-auth user pass] nurl [file]\n", $0) |
81 |
printf(" %s out [-proxy host port] [-tout num] [-auth user pass] nurl expr\n", $0) |
82 |
printf(" %s get [-proxy host port] [-tout num] [-auth user pass] nurl expr [attr]\n", $0) |
83 |
printf(" %s uriid [-proxy host port] [-tout num] [-auth user pass] nurl uri\n", $0) |
84 |
printf(" %s inform [-proxy host port] [-tout num] [-auth user pass] nurl\n", $0) |
85 |
printf(" %s search [-proxy host port] [-tout num] [-auth user pass] [-sf]" \ |
86 |
" [-attr expr] [-ord expr] [-max num] [-dpt num] nurl [phrase]\n", $0) |
87 |
printf(" %s setuser [-proxy host port] [-tout num] [-auth user pass] nurl name mode\n", $0) |
88 |
printf(" %s setlink [-proxy host port] [-tout num] [-auth user pass]" \ |
89 |
" nurl url label credit\n", $0) |
90 |
printf("\n") |
91 |
exit(1) |
92 |
end |
93 |
|
94 |
|
95 |
# print error string and flush the buffer */ |
96 |
def printerror(msg) |
97 |
STDERR.printf("%s: ERROR: %s\n", $0, msg) |
98 |
STDERR.flush |
99 |
end |
100 |
|
101 |
|
102 |
# parse arguments of the put command |
103 |
def runput(args) |
104 |
nurl = nil |
105 |
file = nil |
106 |
i = 1 |
107 |
while i < args.length |
108 |
if !nurl && args[i] =~ /^-/ |
109 |
if args[i] == "-proxy" |
110 |
usage if (i += 1) >= args.length |
111 |
$pxhost = args[i] |
112 |
usage if (i += 1) >= args.length |
113 |
$pxport = args[i].to_i |
114 |
elsif args[i] == "-tout" |
115 |
usage if (i += 1) >= args.length |
116 |
$timeout = args[i].to_i |
117 |
elsif args[i] == "-auth" |
118 |
usage if (i += 1) >= args.length |
119 |
$authname = args[i] |
120 |
usage if (i += 1) >= args.length |
121 |
$authpass = args[i] |
122 |
else |
123 |
usage |
124 |
end |
125 |
elsif !nurl |
126 |
nurl = args[i] |
127 |
elsif !file |
128 |
file = args[i] |
129 |
else |
130 |
usage |
131 |
end |
132 |
i += 1 |
133 |
end |
134 |
usage if !nurl |
135 |
procput(nurl, file) |
136 |
end |
137 |
|
138 |
|
139 |
# parse arguments of the out command |
140 |
def runout(args) |
141 |
nurl = nil |
142 |
expr = nil |
143 |
i = 1 |
144 |
while i < args.length |
145 |
if !nurl && args[i] =~ /^-/ |
146 |
if args[i] == "-proxy" |
147 |
usage if (i += 1) >= args.length |
148 |
$pxhost = args[i] |
149 |
usage if (i += 1) >= args.length |
150 |
$pxport = args[i].to_i |
151 |
elsif args[i] == "-tout" |
152 |
usage if (i += 1) >= args.length |
153 |
$timeout = args[i].to_i |
154 |
elsif args[i] == "-auth" |
155 |
usage if (i += 1) >= args.length |
156 |
$authname = args[i] |
157 |
usage if (i += 1) >= args.length |
158 |
$authpass = args[i] |
159 |
else |
160 |
usage |
161 |
end |
162 |
elsif !nurl |
163 |
nurl = args[i] |
164 |
elsif !expr |
165 |
expr = args[i] |
166 |
else |
167 |
usage |
168 |
end |
169 |
i += 1 |
170 |
end |
171 |
usage if !nurl || !expr |
172 |
procout(nurl, expr) |
173 |
end |
174 |
|
175 |
|
176 |
# parse arguments of the get command |
177 |
def runget(args) |
178 |
nurl = nil |
179 |
expr = nil |
180 |
attr = nil |
181 |
i = 1 |
182 |
while i < args.length |
183 |
if !nurl && args[i] =~ /^-/ |
184 |
if args[i] == "-proxy" |
185 |
usage if (i += 1) >= args.length |
186 |
$pxhost = args[i] |
187 |
usage if (i += 1) >= args.length |
188 |
$pxport = args[i].to_i |
189 |
elsif args[i] == "-tout" |
190 |
usage if (i += 1) >= args.length |
191 |
$timeout = args[i].to_i |
192 |
elsif args[i] == "-auth" |
193 |
usage if (i += 1) >= args.length |
194 |
$authname = args[i] |
195 |
usage if (i += 1) >= args.length |
196 |
$authpass = args[i] |
197 |
else |
198 |
usage |
199 |
end |
200 |
elsif !nurl |
201 |
nurl = args[i] |
202 |
elsif !expr |
203 |
expr = args[i] |
204 |
elsif !attr |
205 |
attr = args[i] |
206 |
else |
207 |
usage |
208 |
end |
209 |
i += 1 |
210 |
end |
211 |
usage if !nurl || !expr |
212 |
procget(nurl, expr, attr) |
213 |
end |
214 |
|
215 |
|
216 |
# parse arguments of the uriid command |
217 |
def runuriid(args) |
218 |
nurl = nil |
219 |
uri = nil |
220 |
i = 1 |
221 |
while i < args.length |
222 |
if !nurl && args[i] =~ /^-/ |
223 |
if args[i] == "-proxy" |
224 |
usage if (i += 1) >= args.length |
225 |
$pxhost = args[i] |
226 |
usage if (i += 1) >= args.length |
227 |
$pxport = args[i].to_i |
228 |
elsif args[i] == "-tout" |
229 |
usage if (i += 1) >= args.length |
230 |
$timeout = args[i].to_i |
231 |
elsif args[i] == "-auth" |
232 |
usage if (i += 1) >= args.length |
233 |
$authname = args[i] |
234 |
usage if (i += 1) >= args.length |
235 |
$authpass = args[i] |
236 |
else |
237 |
usage |
238 |
end |
239 |
elsif !nurl |
240 |
nurl = args[i] |
241 |
elsif !uri |
242 |
uri = args[i] |
243 |
else |
244 |
usage |
245 |
end |
246 |
i += 1 |
247 |
end |
248 |
usage if !nurl || !uri |
249 |
procuriid(nurl, uri) |
250 |
end |
251 |
|
252 |
|
253 |
# parse arguments of the inform command |
254 |
def runinform(args) |
255 |
nurl = nil |
256 |
i = 1 |
257 |
while i < args.length |
258 |
if !nurl && args[i] =~ /^-/ |
259 |
if args[i] == "-proxy" |
260 |
usage if (i += 1) >= args.length |
261 |
$pxhost = args[i] |
262 |
usage if (i += 1) >= args.length |
263 |
$pxport = args[i].to_i |
264 |
elsif args[i] == "-tout" |
265 |
usage if (i += 1) >= args.length |
266 |
$timeout = args[i].to_i |
267 |
elsif args[i] == "-auth" |
268 |
usage if (i += 1) >= args.length |
269 |
$authname = args[i] |
270 |
usage if (i += 1) >= args.length |
271 |
$authpass = args[i] |
272 |
else |
273 |
usage |
274 |
end |
275 |
elsif !nurl |
276 |
nurl = args[i] |
277 |
else |
278 |
usage |
279 |
end |
280 |
i += 1 |
281 |
end |
282 |
usage if !nurl |
283 |
procinform(nurl) |
284 |
end |
285 |
|
286 |
|
287 |
# parse arguments of the search command |
288 |
def runsearch(args) |
289 |
nurl = nil |
290 |
phrase = nil |
291 |
attrs = [] |
292 |
ord = nil |
293 |
max = SEARCHMAX |
294 |
opts = 0 |
295 |
depth = 0 |
296 |
i = 1 |
297 |
while i < args.length |
298 |
if !nurl && args[i] =~ /^-/ |
299 |
if args[i] == "-proxy" |
300 |
usage if (i += 1) >= args.length |
301 |
$pxhost = args[i] |
302 |
usage if (i += 1) >= args.length |
303 |
$pxport = args[i].to_i |
304 |
elsif args[i] == "-tout" |
305 |
usage if (i += 1) >= args.length |
306 |
$timeout = args[i].to_i |
307 |
elsif args[i] == "-auth" |
308 |
usage if (i += 1) >= args.length |
309 |
$authname = args[i] |
310 |
usage if (i += 1) >= args.length |
311 |
$authpass = args[i] |
312 |
elsif args[i] == "-sf" |
313 |
opts |= Condition::CONDSIMPLE |
314 |
elsif args[i] == "-attr" |
315 |
usage if (i += 1) >= args.length |
316 |
attrs.push(args[i]) |
317 |
elsif args[i] == "-ord" |
318 |
usage if (i += 1) >= args.length |
319 |
ord = args[i] |
320 |
elsif args[i] == "-max" |
321 |
usage if (i += 1) >= args.length |
322 |
max = args[i].to_i |
323 |
elsif args[i] == "-dpt" |
324 |
usage if (i += 1) >= args.length |
325 |
depth = args[i].to_i |
326 |
else |
327 |
usage |
328 |
end |
329 |
elsif !nurl |
330 |
nurl = args[i] |
331 |
elsif !phrase |
332 |
phrase = args[i] |
333 |
else |
334 |
phrase += " " + args[i] |
335 |
end |
336 |
i += 1 |
337 |
end |
338 |
usage if !nurl || depth < 0 |
339 |
procsearch(nurl, phrase, attrs, ord, max, opts, depth) |
340 |
end |
341 |
|
342 |
|
343 |
# parse arguments of the setuser command |
344 |
def runsetuser(args) |
345 |
nurl = nil |
346 |
name = nil |
347 |
mode = nil |
348 |
i = 1 |
349 |
while i < args.length |
350 |
if !nurl && args[i] =~ /^-/ |
351 |
if args[i] == "-proxy" |
352 |
usage if (i += 1) >= args.length |
353 |
$pxhost = args[i] |
354 |
usage if (i += 1) >= args.length |
355 |
$pxport = args[i].to_i |
356 |
elsif args[i] == "-tout" |
357 |
usage if (i += 1) >= args.length |
358 |
$timeout = args[i].to_i |
359 |
elsif args[i] == "-auth" |
360 |
usage if (i += 1) >= args.length |
361 |
$authname = args[i] |
362 |
usage if (i += 1) >= args.length |
363 |
$authpass = args[i] |
364 |
else |
365 |
usage |
366 |
end |
367 |
elsif !nurl |
368 |
nurl = args[i] |
369 |
elsif !name |
370 |
name = args[i] |
371 |
elsif !mode |
372 |
mode = args[i].to_i |
373 |
else |
374 |
usage |
375 |
end |
376 |
i += 1 |
377 |
end |
378 |
usage if !nurl || !name || !mode || mode < 0 |
379 |
procsetuser(nurl, name, mode) |
380 |
end |
381 |
|
382 |
|
383 |
# parse arguments of the setlink command |
384 |
def runsetlink(args) |
385 |
nurl = nil |
386 |
url = nil |
387 |
label = nil |
388 |
credit = nil |
389 |
i = 1 |
390 |
while i < args.length |
391 |
if !nurl && args[i] =~ /^-/ |
392 |
if args[i] == "-proxy" |
393 |
usage if (i += 1) >= args.length |
394 |
$pxhost = args[i] |
395 |
usage if (i += 1) >= args.length |
396 |
$pxport = args[i].to_i |
397 |
elsif args[i] == "-tout" |
398 |
usage if (i += 1) >= args.length |
399 |
$timeout = args[i].to_i |
400 |
elsif args[i] == "-auth" |
401 |
usage if (i += 1) >= args.length |
402 |
$authname = args[i] |
403 |
usage if (i += 1) >= args.length |
404 |
$authpass = args[i] |
405 |
else |
406 |
usage |
407 |
end |
408 |
elsif !nurl |
409 |
nurl = args[i] |
410 |
elsif !url |
411 |
url = args[i] |
412 |
elsif !label |
413 |
label = args[i] |
414 |
elsif !credit |
415 |
credit = args[i].to_i |
416 |
else |
417 |
usage |
418 |
end |
419 |
i += 1 |
420 |
end |
421 |
usage if !nurl || !url || !label || !credit |
422 |
procsetlink(nurl, url, label, credit) |
423 |
end |
424 |
|
425 |
|
426 |
# perform the put command |
427 |
def procput(nurl, file) |
428 |
if file |
429 |
begin |
430 |
ifp = open(file, "rb") |
431 |
draft = ifp.read |
432 |
ensure |
433 |
ifp.close if ifp |
434 |
end |
435 |
else |
436 |
STDIN.binmode |
437 |
draft = STDIN.read |
438 |
end |
439 |
doc = Document.new(draft) |
440 |
node = Node.new() |
441 |
node.set_url(nurl) |
442 |
node.set_proxy($pxhost, $pxport) if $pxhost |
443 |
node.set_timeout($timeout) if $timeout > 0 |
444 |
node.set_auth($authname, $authpass) if $authname |
445 |
if !node.put_doc(doc) |
446 |
printerror("failed: " + node.status.to_s) |
447 |
return 1 |
448 |
end |
449 |
return 0 |
450 |
end |
451 |
|
452 |
|
453 |
# perform the out command |
454 |
def procout(nurl, expr) |
455 |
node = Node.new() |
456 |
node.set_url(nurl) |
457 |
node.set_proxy($pxhost, $pxport) if $pxhost |
458 |
node.set_timeout($timeout) if $timeout > 0 |
459 |
node.set_auth($authname, $authpass) if $authname |
460 |
id = expr.to_i |
461 |
if id > 0 |
462 |
if !node.out_doc(id) |
463 |
printerror("failed: " + node.status.to_s) |
464 |
return 1 |
465 |
end |
466 |
else |
467 |
if !node.out_doc_by_uri(expr) |
468 |
printerror("failed: " + node.status.to_s) |
469 |
return 1 |
470 |
end |
471 |
end |
472 |
return 0 |
473 |
end |
474 |
|
475 |
|
476 |
# perform the get command |
477 |
def procget(nurl, expr, attr) |
478 |
node = Node.new() |
479 |
node.set_url(nurl) |
480 |
node.set_proxy($pxhost, $pxport) if $pxhost |
481 |
node.set_timeout($timeout) if $timeout > 0 |
482 |
node.set_auth($authname, $authpass) if $authname |
483 |
id = expr.to_i |
484 |
if attr |
485 |
value = id > 0 ? node.get_doc_attr(id, attr) : node.get_doc_attr_by_uri(expr, attr) |
486 |
if !value |
487 |
printerror("failed: " + node.status.to_s) |
488 |
return 1 |
489 |
end |
490 |
printf("%s\n", value) |
491 |
else |
492 |
doc = id > 0 ? node.get_doc(id) : node.get_doc_by_uri(expr) |
493 |
if !doc |
494 |
printerror("failed: " + node.status.to_s) |
495 |
return 1 |
496 |
end |
497 |
printf("%s", doc.dump_draft) |
498 |
end |
499 |
return 0 |
500 |
end |
501 |
|
502 |
|
503 |
# perform the uriid command |
504 |
def procuriid(nurl, uri) |
505 |
node = Node.new() |
506 |
node.set_url(nurl) |
507 |
node.set_proxy($pxhost, $pxport) if $pxhost |
508 |
node.set_timeout($timeout) if $timeout > 0 |
509 |
node.set_auth($authname, $authpass) if $authname |
510 |
value = node.uri_to_id(uri) |
511 |
if !value |
512 |
printerror("failed: " + node.status.to_s) |
513 |
return 1 |
514 |
end |
515 |
printf("%s\n", value) |
516 |
return 0 |
517 |
end |
518 |
|
519 |
|
520 |
# perform the inform command |
521 |
def procinform(nurl) |
522 |
node = Node.new() |
523 |
node.set_url(nurl) |
524 |
node.set_proxy($pxhost, $pxport) if $pxhost |
525 |
node.set_timeout($timeout) if $timeout > 0 |
526 |
node.set_auth($authname, $authpass) if $authname |
527 |
name = node.name |
528 |
label = node.label |
529 |
dnum = node.doc_num |
530 |
wnum = node.word_num |
531 |
size = node.size |
532 |
if !name || !label || dnum < 0 || wnum < 0 || size < 0.0 |
533 |
printerror("failed: " + node.status.to_s) |
534 |
return 1 |
535 |
end |
536 |
printf("%s\t%s\t%d\t%d\t%.0f\n", name, label, dnum, wnum, size) |
537 |
return 0 |
538 |
end |
539 |
|
540 |
|
541 |
# perform the search command |
542 |
def procsearch(nurl, phrase, attrs, ord, max, opts, depth) |
543 |
node = Node.new() |
544 |
node.set_url(nurl) |
545 |
node.set_proxy($pxhost, $pxport) if $pxhost |
546 |
node.set_timeout($timeout) if $timeout > 0 |
547 |
node.set_auth($authname, $authpass) if $authname |
548 |
cond = Condition.new() |
549 |
cond.set_phrase(phrase) if phrase |
550 |
for i in 0...attrs.length |
551 |
cond.add_attr(attrs[i]) |
552 |
end |
553 |
cond.set_order(ord) if ord |
554 |
cond.set_max(max) if max >= 0 |
555 |
cond.set_options(opts) |
556 |
nres = node.search(cond, depth) |
557 |
if !nres |
558 |
printerror("failed: " + node.status.to_s) |
559 |
return 1 |
560 |
end |
561 |
border = "--------[" + Time.now.to_f.to_s.gsub(/\./, "") + "]--------" |
562 |
printf("%s\n", border) |
563 |
value = nres.hint("VERSION") |
564 |
printf("VERSION\t%s\n", value) if value |
565 |
value = nres.hint("NODE") |
566 |
printf("NODE\t%s\n", value) if value |
567 |
value = nres.hint("HIT") |
568 |
printf("HIT\t%s\n", value) if value |
569 |
for i in 1...1024 |
570 |
key = "HINT#" + i.to_s |
571 |
value = nres.hint(key) |
572 |
if value |
573 |
printf("%s\t%s\n", key, value) |
574 |
else |
575 |
break |
576 |
end |
577 |
end |
578 |
value = nres.hint("DOCNUM") |
579 |
printf("DOCNUM\t%s\n", value) if value |
580 |
value = nres.hint("WORDNUM") |
581 |
printf("WORDNUM\t%s\n", value) if value |
582 |
value = nres.hint("TIME") |
583 |
printf("TIME\t%s\n", value) if value |
584 |
for i in 0...1024 |
585 |
key = "LINK#" + i.to_s |
586 |
value = nres.hint(key) |
587 |
if value |
588 |
printf("%s\t%s\n", key, value) |
589 |
else |
590 |
break |
591 |
end |
592 |
end |
593 |
value = nres.hint("VIEW") |
594 |
printf("VIEW\t%s\n", value) if value |
595 |
printf("\n") |
596 |
for i in 0...nres.doc_num |
597 |
printf("%s\n", border) |
598 |
rdoc = nres.get_doc(i) |
599 |
names = rdoc.attr_names |
600 |
for j in 0...names.length |
601 |
printf("%s=%s\n", names[j], rdoc.attr(names[j])) |
602 |
end |
603 |
printf("\n") |
604 |
printf("%s", rdoc.snippet) |
605 |
end |
606 |
printf("%s:END\n", border) |
607 |
return 0 |
608 |
end |
609 |
|
610 |
|
611 |
# perform the setuser command |
612 |
def procsetuser(nurl, name, mode) |
613 |
node = Node.new() |
614 |
node.set_url(nurl) |
615 |
node.set_proxy($pxhost, $pxport) if $pxhost |
616 |
node.set_timeout($timeout) if $timeout > 0 |
617 |
node.set_auth($authname, $authpass) if $authname |
618 |
if !node.set_user(name, mode) |
619 |
printerror("failed: " + node.status.to_s) |
620 |
return 1 |
621 |
end |
622 |
return 0 |
623 |
end |
624 |
|
625 |
|
626 |
# perform the setlink command |
627 |
def procsetlink(nurl, url, label, credit) |
628 |
node = Node.new() |
629 |
node.set_url(nurl) |
630 |
node.set_proxy($pxhost, $pxport) if $pxhost |
631 |
node.set_timeout($timeout) if $timeout > 0 |
632 |
node.set_auth($authname, $authpass) if $authname |
633 |
if !node.set_link(url, label, credit) |
634 |
printerror("failed: " + node.status.to_s) |
635 |
return 1 |
636 |
end |
637 |
return 0 |
638 |
end |
639 |
|
640 |
|
641 |
# perform the main routine |
642 |
exit(main(ARGV)) |
643 |
|
644 |
|
645 |
|
646 |
# END OF FILE |