| 1 | package Plagger::Plugin::Search::Estraier; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use Encode; | ||
| 6 | use Search::Estraier; | ||
| 7 | |||
| 8 | sub init { | ||
| 9 | my $self = shift; | ||
| 10 | $self->SUPER::init(@_); | ||
| 11 | |||
| 12 | $self->conf->{url} ||= "http://localhost:1978/node/plagger"; | ||
| 13 | $self->conf->{username} ||= "admin"; | ||
| 14 | $self->conf->{password} ||= "admin"; | ||
| 15 | $self->conf->{timeout} ||= 30; | ||
| 16 | |||
| 17 | $self->{node} = Search::Estraier::Node->new( | ||
| 18 | url => $self->conf->{url}, | ||
| 19 | debug => $self->conf->{debug}, | ||
| 20 | ); | ||
| 21 | $self->{node}->set_auth($self->conf->{username}, $self->conf->{password}); | ||
| 22 | $self->{node}->set_timeout($self->conf->{timeout}); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub register { | ||
| 26 | my($self, $context) = @_; | ||
| 27 | $context->register_hook( | ||
| 28 | $self, | ||
| 29 | 'publish.entry' => \&entry, | ||
| 30 | 'searcher.search' => \&search, | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub entry { | ||
| 35 | my($self, $context, $args) = @_; | ||
| 36 | |||
| 37 | return unless $args->{entry}->permalink; | ||
| 38 | |||
| 39 | my $id = $self->{node}->uri_to_id($args->{entry}->permalink); | ||
| 40 | $context->log(info => "Going to index entry " . $args->{entry}->permalink . ($id ? " with id=$id" : "")); | ||
| 41 | |||
| 42 | my $doc = Search::Estraier::Document->new; | ||
| 43 | $doc->add_attr('@uri' => $args->{entry}->permalink); | ||
| 44 | $doc->add_attr('@title' => $args->{entry}->title->utf8); | ||
| 45 | $doc->add_attr('@cdate' => $args->{entry}->date->format('W3CDTF')) if $args->{entry}->date; | ||
| 46 | $doc->add_attr('@author' => $args->{entry}->author->utf8) if $args->{entry}->author; | ||
| 47 | |||
| 48 | $doc->add_text($args->{entry}->body->utf8); | ||
| 49 | $doc->add_hidden_text($args->{entry}->title->utf8); | ||
| 50 | |||
| 51 | $doc->add_attr('@id' => $id) if $id; # update mode | ||
| 52 | |||
| 53 | $self->{node}->put_doc($doc) or $context->error("Put failure: " . $self->{node}->status); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub search { | ||
| 57 | my($self, $context, $args) = @_; | ||
| 58 | |||
| 59 | my $cond = Search::Estraier::Condition->new; | ||
| 60 | $cond->set_phrase( encode_utf8($args->{query}) ); | ||
| 61 | |||
| 62 | my $nres = $self->{node}->search($cond, 0); | ||
| 63 | defined $nres or $context->error("search failed: " . $self->{node}->status); | ||
| 64 | |||
| 65 | my $feed = Plagger::Feed->new; | ||
| 66 | $feed->type('search:Estraier'); | ||
| 67 | $feed->title("Search: $args->{query}"); | ||
| 68 | |||
| 69 | for my $i ( 0 .. $nres->doc_num - 1 ) { | ||
| 70 | my $doc = $nres->get_doc($i); | ||
| 71 | my $entry = Plagger::Entry->new; | ||
| 72 | |||
| 73 | $entry->link( $doc->attr('@uri') ); | ||
| 74 | $entry->title( decode_utf8($doc->attr('@title')) ); | ||
| 75 | $entry->date( $doc->attr('@cdate') ) if $doc->attr('@cdate'); | ||
| 76 | $entry->author( decode_utf8($doc->attr('@author')) ) if $doc->attr('@author'); | ||
| 77 | $entry->body( decode_utf8($doc->snippet) ); | ||
| 78 | |||
| 79 | $feed->add_entry($entry); | ||
| 80 | } | ||
| 81 | |||
| 82 | return $feed; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _u { | ||
| 86 | my $str = shift; | ||
| 87 | Encode::_utf8_off($str); | ||
| 88 | $str; | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ |