| | 287 | eval("use MT::XSearch;"); |
| | 288 | if (!$@) { |
| | 289 | MT::XSearch->add_search_plugin('AllKeywords', { |
| | 290 | label => 'Keyword Search', |
| | 291 | description => 'Keyword Search plugin for MT-XSearch', |
| | 292 | on_execute => \&xsearch_on_execute, |
| | 293 | on_stash => \&xsearch_on_stash }); |
| | 294 | } |
| | 295 | |
| | 296 | sub xsearch_on_stash { |
| | 297 | $_[0]->stash('entry', $_[1]); |
| | 298 | $_[0]->{current_timestamp} = $_[1]->created_on; |
| | 299 | $_[0]->{modification_timestamp} = $_[1]->modified_on; |
| | 300 | } |
| | 301 | |
| | 302 | sub xsearch_on_execute { |
| | 303 | my $args = shift; |
| | 304 | require MT::Entry; |
| | 305 | my $blog_id = $args->{blog_id} or |
| | 306 | MT->error('A Blog ID is required.'); |
| | 307 | my $sort_by = $args->{sort_by} || 'created_on'; |
| | 308 | my $sort_order = $args->{sort_order} || 'descend'; |
| | 309 | my $delimiter = $args->{delimiter} || ''; |
| | 310 | my $case_sensitive = $args->{case_sensitive} || 0; |
| | 311 | |
| | 312 | my @patterns = split_keywords($args->{search}, $delimiter, $case_sensitive); |
| | 313 | my $iter = MT::Entry->load_iter({ blog_id => $blog_id, |
| | 314 | status => MT::Entry::RELEASE() }, |
| | 315 | { sort => $sort_by, |
| | 316 | direction => $sort_order }); |
| | 317 | my @results; |
| | 318 | while (my $e = $iter->()) { |
| | 319 | next unless $e->keywords; |
| | 320 | my @keywords = split_keywords($e->keywords, $delimiter, $case_sensitive); |
| | 321 | my $check = 1; |
| | 322 | foreach my $pattern (@patterns) { |
| | 323 | unless (scalar grep { $_ eq $pattern } @keywords) { |
| | 324 | $check = 0; |
| | 325 | last; |
| | 326 | } |
| | 327 | } |
| | 328 | push @results, $e if $check; |
| | 329 | } |
| | 330 | \@results; |
| | 331 | } |
| | 332 | |