summaryrefslogtreecommitdiffstats
path: root/khelpcenter/searchhandlers/khc_beagle_search.pl
blob: d7a1f44b86304761d98be57ceeb17d6ca7cc62a6 (plain)
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
#!/usr/bin/perl -w
# vim:sw=4:et

use warnings;
use strict;
use Getopt::Long;

sub isBeagleRunning()
{
    open(IN, "-|") || exec "beagle-ping";
    while(<IN>) {
        if (/^Daemon version:/) {
            close(IN);
            return 1;
        }
    }
    close(IN);
    return 0;
}

sub formatHTML($$)
{
    my ($query, $hits) = @_;

    print "<html>\n<body\n<ul>\n";

    foreach my $hit(@$hits) {
        print "<li>$hit</li>\n";
    }
    print "</ul>\n</body>\n</html>\n";
}

sub beagleQuery($$$)
{
    my ($words, $method, $maxnum) = @_;

    my @hits = ();

    open(IN, "-|") || exec "beagle-query", "--type", "DocbookEntry", "--type", "File", "--max-hits", $maxnum, @$words, "ext:docbook";
    while(<IN>) {
        chop;
        next if (/^Debug:/);

        my $uri = $_;
        $uri = $1 if ($uri =~ /^file:\/\/(.*)$/);

        print "uri: $uri\n";
        my $helpLink = &makeHelpLink($uri);

        push(@hits, $helpLink) if (!grep { /^$helpLink$/ } @hits);
    }
    close(IN);
    return @hits;
}

sub makeHelpLink($)
{
    # Try to figure out the name of the application from the path to its index.docbook file

    my ($path) = @_;
    my @pathcomponents = split '/', $path;

    my $appName = $pathcomponents[-2];
    my $appName2 = $pathcomponents[-3];

    if ($appName eq $appName2 or $appName2 eq "doc" 
        or (-d "/usr/share/locale/$appName2")) {
        return "<a href=\"help:/$appName\">$appName</a>";
    }
    return "<a href=\"help:/$appName2/$appName\">$appName ($appName2)</a>";
}

my $method = "and";
my $maxnum = 100;

GetOptions("method=s", \$method, "maxnum=i", \$maxnum);

my @hits = ("The Beagle daemon is not running, search is not available");

my @words = @ARGV;

if (isBeagleRunning()) {
    @hits = beagleQuery(\@words, $method, $maxnum);
}

@hits = ("There are no search results") if ($#hits < 0);

formatHTML(\@words, \@hits);