24b16fe2a78c4607f89788cd78ea1d7e5a1ac137
2 # xapian-omega search engine plugin
3 package IkiWiki
::Plugin
::search
;
10 hook
(type
=> "getsetup", id
=> "search", call
=> \
&getsetup
);
11 hook
(type
=> "checkconfig", id
=> "search", call
=> \
&checkconfig
);
12 hook
(type
=> "pagetemplate", id
=> "search", call
=> \
&pagetemplate
);
13 hook
(type
=> "indexhtml", id
=> "search", call
=> \
&indexhtml
);
14 hook
(type
=> "delete", id
=> "search", call
=> \
&delete);
15 hook
(type
=> "cgi", id
=> "search", call
=> \
&cgi
);
16 hook
(type
=> "disable", id
=> "search", call
=> \
&disable
);
17 hook
(type
=> "needsbuild", id
=> "search", call
=> \
&needsbuild
);
19 eval q{ use Search::Xapian }; # load early to work around #622591
31 example
=> "/usr/lib/cgi-bin/omega/omega",
32 description
=> "path to the omega cgi program",
33 safe
=> 0, # external program
39 description
=> "use google site search rather than internal xapian index?",
46 foreach my $required (qw(url cgiurl)) {
47 if (! length $config{$required}) {
48 error
(sprintf(gettext
("Must specify %s when using the %s plugin"), $required, 'search'));
52 if (! defined $config{omega_cgi
}) {
53 $config{omega_cgi
}="/usr/lib/cgi-bin/omega/omega";
56 # This is a mass dependency, so if the search form template
57 # changes, every page is rebuilt.
58 add_depends
("", "templates/searchform.tmpl");
62 sub pagetemplate
(@
) {
64 my $page=$params{page
};
65 my $template=$params{template
};
67 # Add search box to page header.
68 if ($template->query(name
=> "searchform")) {
69 if (! defined $form) {
70 my $searchform = template
("searchform.tmpl", blind_cache
=> 1);
71 $searchform->param(searchaction
=> IkiWiki
::cgiurl
());
72 $searchform->param(html5
=> $config{html5
});
73 $form=$searchform->output;
76 $template->param(searchform
=> $form);
85 return if $config{google_search
};
89 # A unique pageterm is used to identify the document for a page.
90 my $pageterm=pageterm
($params{page
});
91 return unless defined $pageterm;
94 my $doc=Search
::Xapian
::Document
->new();
95 my $caption=pagetitle
($params{page
});
97 if (exists $pagestate{$params{page
}}{meta
} &&
98 exists $pagestate{$params{page
}}{meta
}{title
}) {
99 $title=$pagestate{$params{page
}}{meta
}{title
};
105 # Remove html from text to be indexed.
106 if (! defined $scrubber) {
107 eval q{use HTML::Scrubber};
109 $scrubber=HTML
::Scrubber
->new(allow
=> []);
112 my $toindex = defined $scrubber ?
$scrubber->scrub($params{content
}) : $params{content
};
114 # Take 512 characters for a sample, then extend it out
115 # if it stopped in the middle of a word.
117 my ($sample)=substr($toindex, 0, $size);
118 if (length($sample) == $size) {
119 my $max=length($toindex);
121 while ($size < $max &&
122 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
128 my $url=urlto
($params{destpage
}, "");
129 if (defined $pagestate{$params{page
}}{meta
}{permalink
}) {
130 $url=$pagestate{$params{page
}}{meta
}{permalink
}
134 # Decode html entities in it, since omega re-encodes them.
135 eval q{use HTML::Entities};
139 "sample=".decode_entities
($sample)."\n".
140 "caption=".decode_entities
($caption)."\n".
141 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
142 "size=".length($params{content
})."\n"
145 # Index document and add terms for other metadata.
146 my $tg = Search
::Xapian
::TermGenerator
->new();
148 my $langcode=$ENV{LANG
} || "en";
151 # This whitelist is here to work around a xapian bug (#486138)
152 my @whitelist=qw{da de en es fi fr hu it
no pt ru ro sv tr
};
154 if (grep { $_ eq $langcode } @whitelist) {
155 $stemmer=Search
::Xapian
::Stem
->new($langcode);
158 $stemmer=Search
::Xapian
::Stem
->new("english");
161 $tg->set_stemmer($stemmer);
162 $tg->set_document($doc);
163 $tg->index_text($params{page
}, 2);
164 $tg->index_text($caption, 2);
165 $tg->index_text($title, 2) if $title ne $caption;
166 $tg->index_text($toindex);
167 $tg->index_text(lc($title), 1, "S"); # for title:foo
168 foreach my $link (@
{$links{$params{page
}}}) {
169 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
172 $doc->add_term($pageterm);
173 $db->replace_document_by_term($pageterm, $doc);
177 return if $config{google_search
};
180 foreach my $page (@_) {
181 my $pageterm=pageterm
(pagename
($page));
182 $db->delete_document_by_term($pageterm) if defined $pageterm;
189 if (defined $cgi->param('P')) {
190 if ($config{google_search
}) {
191 print $cgi->redirect("https://www.google.com/search?sitesearch=$config{url}&q=".$cgi->param('P'));
195 # only works for GET requests
196 chdir("$config{wikistatedir}/xapian") || error
("chdir: $!");
197 $ENV{OMEGA_CONFIG_FILE
}="./omega.conf";
198 $ENV{CGIURL
}=IkiWiki
::cgiurl
();
199 IkiWiki
::loadindex
();
200 $ENV{HELPLINK
}=htmllink
("", "", "ikiwiki/searching",
201 noimageinline
=> 1, linktext
=> "Help");
202 exec($config{omega_cgi
}) || error
("$config{omega_cgi} failed: $!");
210 # 240 is the number used by omindex to decide when to hash an
211 # overlong term. This does not use a compatible hash method though.
213 if (length encode_utf8
($page) > 240) {
214 eval q{use Digest::SHA};
216 debug
("search: ".sprintf(gettext
("need Digest::SHA to index %s"), $page)) if $@
;
220 # Note no colon, therefore it's guaranteed to not overlap
221 # with a page with the same name as the hash..
222 return "U".lc(Digest
::SHA
::sha1_hex
($page));
234 use Search::Xapian::WritableDatabase;
237 $db=Search
::Xapian
::WritableDatabase
->new($config{wikistatedir
}."/xapian/default",
238 Search
::Xapian
::DB_CREATE_OR_OPEN
());
246 if (! $setup and (! -e
$config{wikistatedir
}."/xapian" || $config{rebuild
})) {
247 writefile
("omega.conf", $config{wikistatedir
}."/xapian",
249 "template_dir ./templates\n");
259 $_ eq "templates/page.tmpl" ||
260 $_ eq "templates/searchquery.tmpl"
267 # Avoid omega interpreting anything in the cgitemplate
268 # as an omegascript command.
269 eval q{use IkiWiki::CGI};
270 my $template=IkiWiki
::cgitemplate
(undef, gettext
("search"), "\0",
271 searchform
=> "", # avoid showing the small search form
273 eval q{use HTML::Entities};
275 $template=encode_entities
($template, '\$');
277 my $querytemplate=readfile
(IkiWiki
::template_file
("searchquery.tmpl"));
278 $template=~s/\0/$querytemplate/;
279 writefile
("query", $config{wikistatedir
}."/xapian/templates",
284 if (-d
$config{wikistatedir
}."/xapian") {
285 system("rm", "-rf", $config{wikistatedir
}."/xapian");