Exlude from search modules that have no UI
[webmin.git] / webmin-search-lib.pl
1 # Functions for searching the webmin docs and UI
2
3 =head2 search_webmin(phrase, [callback-function])
4
5 Searches all Webmin help pages, UI text, module names and config.info files
6 for entries matching the given phrase or word. Returns them sorted by relevance
7 order, each as a hash ref with the following keys :
8
9 =item mod - A module hash reference for the module the search result was in
10
11 =item rank - A result ranking, higher being better
12
13 =item type - One of mod (for module name), dir (for module directory), config (configuration setting), help (help page) or text (UI text)
14
15 =item text - The text that matched
16
17 =items cgis - An array ref of pages on which the text appears, each formatted like module/script.cgi
18
19 =cut
20 sub search_webmin
21 {
22 my ($re, $cbfunc) = @_;
23
24 # Work out this Webmin's URL base
25 my $urlhost = $ENV{'HTTP_HOST'};
26 if ($urlhost !~ /:/) {
27         $urlhost .= ":".$ENV{'SERVER_PORT'};
28         }
29 my $urlbase = ($ENV{'HTTPS'} eq 'ON' ? 'https://' : 'http://').$urlhost;
30
31 # Search module names and add to results list
32 my @rv = ( );
33 my $pn = &get_product_name();
34 my @mods = sort { $b->{'longdesc'} cmp $a->{'longdesc'} }
35              grep { !$_->{'clone'} }
36                grep { !$_->{'noui'} && !$_->{$pn.'_noui'} }
37                  &get_available_module_infos();
38 foreach my $m (@mods) {
39         if ($m->{'desc'} =~ /\Q$re\E/i) {
40                 # Module description match
41                 push(@rv, { 'mod' => $m,
42                             'rank' => 10,
43                             'type' => 'mod',
44                             'link' => $m->{'dir'}.'/',
45                             'text' => $m->{'desc'} });
46                 }
47         elsif ($m->{'dir'} =~ /\Q$re\E/i) {
48                 # Module directory match
49                 push(@rv, { 'mod' => $m,
50                             'rank' => 9,
51                             'type' => 'dir',
52                             'link' => $m->{'dir'}.'/',
53                             'text' => $urlbase."/".$m->{'dir'}."/" });
54                 }
55         &$cbfunc() if ($cbfunc);
56         }
57
58 # Search module configs and their help pages
59 foreach my $m (@mods) {
60         my %access = &get_module_acl(undef, $m);
61         next if ($access{'noconfig'});
62         my $file = $prod eq 'webmin' ? "$m->{'dir'}/config.info"
63                                      : "$m->{'dir'}/uconfig.info";
64         my %info = ( );
65         my @info_order = ( );
66         &read_file($file, \%info, \@info_order);
67         foreach my $o (@lang_order_list) {
68                 &read_file("$file.$o", \%info);
69                 }
70         my $section = undef;
71         foreach my $c (@info_order) {
72                 my @p = split(/,/, $info{$c});
73                 if ($p[1] == 11) {
74                         $section = $c;
75                         }
76                 if ($p[0] =~ /\Q$re\E/i) {
77                         # Config description matches
78                         push(@rv, { 'mod' => $m,
79                                     'rank' => 8,
80                                     'type' => 'config',
81                                     'link' => "/config.cgi?module=$m->{'dir'}&".
82                                              "section=".&urlize($section)."#$c",
83                                     'text' => $p[0],
84                                   });
85                         }
86                 my $hfl = &help_file($mod->{'dir'}, "config_".$c);
87                 my ($title, $help) = &help_file_match($hfl);
88                 if ($help) {
89                         # Config help matches
90                         push(@rv, { 'mod' => $m,
91                                     'rank' => 6,
92                                     'type' => 'help',
93                                     'link' => "/help.cgi/$m->{'dir'}/config_".$c,
94                                     'desc' => &text('wsearch_helpfor', $p[0]),
95                                     'text' => $help,
96                                     'cgis' => [ "/config.cgi?".
97                                                 "module=$m->{'dir'}&section=".
98                                                 &urlize($section)."#$c" ],
99                                    });
100                         }
101                 }
102         &$cbfunc() if ($cbfunc);
103         }
104
105 # Search other help pages
106 my %lang_order_list = map { $_, 1 } @lang_order_list;
107 foreach my $m (@mods) {
108         my $helpdir = &module_root_directory($m->{'dir'})."/help";
109         my %donepage = ( );
110         opendir(DIR, $helpdir);
111         foreach my $f (sort { length($b) <=> length($a) } readdir(DIR)) {
112                 next if ($f =~ /^config_/);     # For config help, already done
113
114                 # Work out if we should grep this help page - don't do the same
115                 # page twice for different languages
116                 my $grep = 0;
117                 my ($page, $lang);
118                 if ($f =~ /^(\S+)\.([^\.]+)\.html$/) {
119                         ($page, $lang) = ($1, $2);
120                         if ($lang_order_list{$lang} && !$donepage{$page}++) {
121                                 $grep = 1;
122                                 }
123                         }
124                 elsif ($f =~ /^(\S+)\.html$/) {
125                         $page = $1;
126                         if (!$donepage{$page}++) {
127                                 $grep = 1;
128                                 }
129                         }
130
131                 # If yes, search it
132                 if ($grep) {
133                         my ($title, $help) = &help_file_match("$helpdir/$f");
134                         if ($title) {
135                                 my @cgis = &find_cgi_text(
136                                         [ "hlink\\(.*'$page'",
137                                           "hlink\\(.*\"$page\"",
138                                           "header\\([^,]+,[^,]+,[^,]+,\\s*\"$page\"",
139                                           "header\\([^,]+,[^,]+,[^,]+,\\s*'$page'",
140                                         ], $m, 1);
141                                 push(@rv, { 'mod' => $m,
142                                             'rank' => 6,
143                                             'type' => 'help',
144                                             'link' => "/help.cgi/$m->{'dir'}/$page",
145                                             'desc' => $title,
146                                             'text' => $help,
147                                             'cgis' => \@cgis });
148                                 }
149                         }
150                 &$cbfunc() if ($cbfunc);
151                 }
152         closedir(DIR);
153         }
154
155 # Then do text strings
156 my %gtext = &load_language("");
157 MODULE: foreach my $m (@mods) {
158         my %mtext = &load_language($m->{'dir'});
159         foreach my $k (keys %mtext) {
160                 next if ($gtext{$k});   # Skip repeated global strings
161                 $mtext{$k} =~ s/\$[0-9]//g;
162                 if ($mtext{$k} =~ /\Q$re\E/i) {
163                         # Find CGIs that use this text
164                         my @cgis = &find_cgi_text(
165                                 [ "\$text{'$k'}",
166                                   "\$text{\"$k\"}",
167                                   "\$text{$k}",
168                                   "&text('$k'",
169                                   "&text(\"$k\"" ], $m);
170                         if (@cgis) {
171                                 push(@rv, { 'mod' => $m,
172                                             'rank' => 4,
173                                             'type' => 'text',
174                                             'text' => $mtext{$k},
175                                             'cgis' => \@cgis });
176                                 }
177                         }
178                 }
179         &$cbfunc() if ($cbfunc);
180         }
181
182 # Sort results by relevancy
183 # XXX can do better?
184 @rv = sort { $b->{'rank'} <=> $a->{'rank'} ||
185              lc($a->{'mod'}->{'desc'}) cmp lc($b->{'mod'}->{'desc'}) } @rv;
186 return @rv;
187 }
188
189 # highlight_text(text, [length])
190 # Returns text with the search term bolded, and truncated to 50 characters
191 sub highlight_text
192 {
193 local ($str, $len) = @_;
194 $len ||= 50;
195 local $hlen = $len / 2;
196 $str =~ s/<[^>]*>//g;
197 if ($str =~ /(.*)(\Q$re\E)(.*)/i) {
198         local ($before, $match, $after) = ($1, $2, $3);
199         if (length($before) > $hlen) {
200                 $before = "...".substr($before, length($before)-$hlen);
201                 }
202         if (length($after) > $hlen) {
203                 $after = substr($after, 0, $hlen)."...";
204                 }
205         $str = $before."<b>".&html_escape($match)."</b>".$after;
206         }
207 return $str;
208 }
209
210 # find_cgi_text(&regexps, module, re-mode)
211 # Returns the relative URLs of CGIs that matches some regexps, in the given
212 # module. Does not include those that don't call some header function, as
213 # they cannot be linked to normally
214 sub find_cgi_text
215 {
216 local ($res, $m, $remode) = @_;
217 local $mdir = &module_root_directory($m);
218 local @rv;
219 foreach my $f (glob("$mdir/*.cgi")) {
220         local $found = 0;
221         local $header = 0;
222         open(CGI, $f);
223         LINE: while(my $line = <CGI>) {
224                 if ($line =~ /(header|ui_print_header|ui_print_unbuffered_header)\(/) {
225                         $header++;
226                         }
227                 foreach my $r (@$res) {
228                         if (!$remode && index($line, $r) >= 0 ||
229                             $remode && $line =~ /$r/) {
230                                 $found++;
231                                 last LINE;
232                                 }
233                         }
234                 }
235         close(CGI);
236         if ($found && $header) {
237                 local $url = $f;
238                 $url =~ s/^\Q$root_directory\E\///;
239                 push(@rv, $url);
240                 }
241         }
242 return @rv;
243 }
244
245 # help_file_match(file)
246 # Returns the title if some help file matches the current search
247 sub help_file_match
248 {
249 local ($f) = @_;
250 local $data = &read_file_contents($f);
251 local $title;
252 if ($data =~ /<header>([^<]*)<\/header>/) {
253         $title = $1;
254         }
255 $data =~ s/\s+/ /g;
256 $data =~ s/<p>/\n\n/gi;
257 $data =~ s/<br>/\n/gi;
258 $data =~ s/<[^>]+>//g;
259 if ($data =~ /\Q$re\E/i) {
260         return ($title, $data);
261         }
262 return ( );
263 }
264
265 # cgi_page_title(module, cgi)
266 # Given a CGI, return the text for its page title, if possible
267 sub cgi_page_title
268 {
269 local ($m, $cgi) = @_;
270 local $data = &read_file_contents(&module_root_directory($m)."/".$cgi);
271 local $rv;
272 if ($data =~ /(ui_print_header|ui_print_unbuffered_header)\([^,]+,[^,]*(\$text{'([^']+)'|\$text{"([^"]+)"|\&text\('([^']+)'|\&text\("([^"]+)")/) {
273         # New header function, with arg before title
274         local $msg = $3 || $4 || $5 || $6;
275         local %mtext = &load_language($m);
276         $rv = $mtext{$msg};
277         }
278 elsif ($data =~ /(^|\s)header\(\s*(\$text{'([^']+)'|\$text{"([^"]+)"|\&text\('([^']+)'|\&text\("([^"]+)")/) {
279         # Old header function
280         local $msg = $3 || $4 || $5 || $6;
281         local %mtext = &load_language($m);
282         $rv = $mtext{$msg};
283         }
284 if ($cgi eq "index.cgi" && !$rv) {
285         # If no title was found for an index.cgi, use module title
286         local %minfo = &get_module_info($m);
287         $rv = $minfo{'desc'};
288         }
289 return $rv;
290 }
291
292 # cgi_page_args(module, cgi)
293 # Given a module and CGI name, returns a string of URL parameters that can be
294 # used for linking to it. Returns "none" if parameters are needed, but cannot
295 # be determined.
296 sub cgi_page_args
297 {
298 local ($m, $cgi) = @_;
299 local $mroot = &module_root_directory($m);
300 if (-r "$mroot/cgi_args.pl") {
301         # Module can tell us what args to use
302         &foreign_require($m, "cgi_args.pl");
303         $args = &foreign_call($m, "cgi_args", $cgi);
304         if (defined($args)) {
305                 return $args;
306                 }
307         }
308 if ($cgi eq "index.cgi") {
309         # Index page is always safe to link to
310         return undef;
311         }
312 # Otherwise check if it appears to parse any args
313 local $data = &read_file_contents($mroot."/".$cgi);
314 if ($data =~ /(ReadParse|ReadParseMime)\(/) {
315         return "none";
316         }
317 return undef;
318 }
319
320 1;
321