File: Search.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (454 lines) | stat: -rw-r--r-- 12,623 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT::Template::Context::Search;

use strict;
use base qw( MT::Template::Context );
use MT::Util qw( encode_url decode_html );

sub load_core_tags {
    require MT::Template::Context;
    return {
        function => {
            SearchString         => \&_hdlr_search_string,
            SearchResultCount    => \&_hdlr_result_count,
            MaxResults           => \&_hdlr_max_results,
            SearchIncludeBlogs   => \&_hdlr_include_blogs,
            SearchTemplateID     => \&_hdlr_template_id,
            SearchTemplateBlogID => \&_hdlr_template_blog_id,
        },
        block => {
            SearchResults       => \&_hdlr_results,
            'IfTagSearch?'      => sub { MT->instance->mode eq 'tag' },
            'IfStraightSearch?' => sub { MT->instance->mode eq 'default' },
            'NoSearchResults?'  => sub {
                (          $_[0]->stash('search_string')
                        && $_[0]->stash('search_string') =~ /\S/
                        && !$_[0]->stash('count') ) ? 1 : 0;
            },
            'NoSearch?' => sub {
                (          $_[0]->stash('search_string')
                        && $_[0]->stash('search_string') =~ /\S/ ) ? 0 : 1;
            },
            SearchResultsHeader => \&MT::Template::Context::_hdlr_pass_tokens,
            SearchResultsFooter => \&MT::Template::Context::_hdlr_pass_tokens,
            BlogResultHeader    => \&MT::Template::Context::_hdlr_pass_tokens,
            BlogResultFooter    => \&MT::Template::Context::_hdlr_pass_tokens,
            'IfMaxResultsCutoff?' =>
                \&MT::Template::Context::_hdlr_pass_tokens,
        },
    };
}

###########################################################################

=head2 IfStraightSearch

A conditional block which outputs its contents if the search in progress
is a regular (or "straight") search.

=for tags search

=cut

###########################################################################

=head2 IfTagSearch

A conditional block which outputs its contents if the search in progress
is a search of entries by tag.

=for tags search

=cut

###########################################################################

=head2 NoSearch

A container tag whose contents are displayed only if there is no search
performed.

This tag is only recognized in search templates.

=for tags search

=cut

###########################################################################

=head2 NoSearchResults

A container tag whose contents are displayed if a search is performed
but no results are found.

This tag is only recognized in search templates.

=for tags search

=cut

###########################################################################

=head2 SearchResultsHeader

The content of this block tag is rendered when the item in context
from search results are the first item of the result set.  You can
use the block to render headings and titles of the result table,
for example.

This tag is only recognized in SearchResults block.

B<Example:>

    <mt:SearchResultsHeader>
    <h3>Look what we found!</h3>
    </mt:SearchResultsHeader>

=for tags search

=cut

###########################################################################

=head2 SearchResultsFooter

The content of this block tag is rendered when the item in context
from search results are the last item of the result set.  You can
use the block to render closeing tags of a HTML element, for example.

This tag is only recognized in SearchResults block.

B<Example:>

    <mt:SearchResultsFooter>
    <p>If you didn't find what you were looking for, you can also peruse
    the <a href="<mt:Link identifier="archive_index">">site archives</a>.</p>
    </mt:SearchResultsFooter>

=for tags search

=cut

###########################################################################

=head2 BlogResultHeader

The contents of this container tag will be displayed when the blog id of
the entry in context from search results differs from the previous entry's
blog id.

This tag is only recognized in search templates.

=for tags search

=cut

###########################################################################

=head2 BlogResultFooter

The contents of this container tag will be displayed when the blog id of
the entry in context from search results differs from the next entry's
blog id.

This tag is only recognized in search templates.

=for tags search

=cut

###########################################################################

=head2 IfMaxResultsCutoff

NOTE: this tag only applies if you are using older Movable Type than
version 4.15, or you set up your search script so it instantiates
MT::App::Search::Legacy, the older search script.  Under the default
search script in Movable Type, this tag will never be evaluated as true and
therefore the contents will never be rendered.

A conditional tag that returns true when the number of search results
per blog exceeds the maximium limit specified in MaxResults configuration
directive.

This tag is only recognized in search templates.

=for tags search

=cut

###########################################################################

=head2 MaxResults

Returns the value of SearchMaxResults, specified either in configuration
(via C<SearchMaxResults> configuration directive) or in the search query
parameter in the URL.

This tag is only recognized in search templates.

=for tags search

=cut

###########################################################################

=head2 SearchIncludeBlogs

Used in the search result template to pass the IncludeBlogs parameters
through from the search form keeping the context of any followup search
the same as the initial search.

B<Example:>

    <input type="hidden" name="IncludeBlogs" value="<$mt:SearchIncludeBlogs$>" />

=for tags search

=cut

sub _hdlr_include_blogs { $_[0]->stash('include_blogs') || '' }

###########################################################################

=head2 SearchString

An HTML-encoded search query. This tag is only recognized in search templates.

B<Example:>

    <$mt:SearchString$>

=for tags search

=cut

sub _hdlr_search_string { $_[0]->stash('search_string') || '' }

###########################################################################

=head2 SearchTemplateID

Returns the identifier of the search template (ie, "feed" or
"nomorepizzaplease").

B<Example:>

    <$mt:SearchTemplateID$>

=for tags search

=cut

sub _hdlr_template_id { $_[0]->stash('template_id') || '' }

###########################################################################

=head2 SearchTemplateBlogID

Returns the ID of the blog that loaded the template.

B<Example:>

    <$mt:SearchTemplateBlogID$>

=for tags search

=cut

sub _hdlr_template_blog_id { $_[0]->stash('blog_id') || '' }

sub _hdlr_max_results { $_[0]->stash('maxresults') || '' }

###########################################################################

=head2 SearchResultCount

The number of results found across all of the blogs searched. This tag
is only recognized in search templates.

B<Example:>

    <$mt:SearchResultCount$>

=for tags search, count

=cut

sub _hdlr_result_count {
    my $results = $_[0]->stash('count');
    $results ? $results : 0;
}

###########################################################################

=head2 SearchResults

A container tag that creates a list of search results. This tag
creates an entry and blog context that all Entry* and Blog* tags
can be used.

This tag is only recognized in search templates.

=for tags search

=cut

sub _hdlr_results {
    my ( $ctx, $args, $cond ) = @_;

    my $orig_blog    = $ctx->stash('blog');
    my $orig_blog_id = $ctx->stash('blog_id');

    ## If there are no results, return the empty string, knowing
    ## that the handler for <MTNoSearchResults> will fill in the
    ## no results message.
    my $iter  = $ctx->stash('results') or return '';
    my $count = $ctx->stash('count')   or return '';
    my $max   = $ctx->stash('maxresults');
    my $stash_key = $ctx->stash('stash_key') || 'entry';

    my $output         = '';
    my $build          = $ctx->stash('builder');
    my $tokens         = $ctx->stash('tokens');
    my $blog_header    = 1;
    my $blog_footer    = 0;
    my $footer         = 0;
    my $count_per_blog = 0;
    my $max_reached    = 0;
    my ( $this_object, $next_object );
    $this_object = $iter->();
    return '' unless $this_object;

    for ( my $i = 0; $i < $count; $i++ ) {
        $count_per_blog++;
        local $ctx->{__stash}{$stash_key} = $this_object;
        local $ctx->{__stash}{blog} = $this_object->blog
            if $this_object->can('blog');
        my $ts;
        if ( $this_object->isa('MT::Entry') ) {
            $ts = $this_object->authored_on;
        }
        elsif ( $this_object->properties->{audit} ) {
            $ts = $this_object->created_on;
        }
        local $ctx->{current_timestamp} = $ts;

   # TODO: per blog max objects?
   #if ( $count_per_blog >= $max ) {
   #    while (1) {
   #        if ( $count > $i + 1 ) {
   #            $next_object = $results->[$i + 1];
   #            if ( $next_object->blog_id ne $this_object->blog_id ) {
   #                $blog_footer = 1;
   #                last;
   #            }
   #            else {
   #                $max_reached = 1;
   #            }
   #        }
   #        else {
   #            $next_object = undef;
   #            $blog_footer = 1;
   #            last;
   #        }
   #        $i++;
   #    }
   #}
   #elsif ( $count > $i + 1 ) {
   #    $next_object = $results->[$i + 1];
   #    $blog_footer = $next_object->blog_id ne $this_object->blog_id ? 1 : 0;
   #}
   #else {
   #    $blog_footer = 1;
   #}
        if ( $next_object = $iter->() ) {
            if ( $next_object->can('blog') ) {
                $blog_footer
                    = $next_object->blog_id ne $this_object->blog_id ? 1 : 0;
            }
        }
        else {
            $blog_footer = 1;
            $footer      = 1;
        }

        defined(
            my $out = $build->build(
                $ctx, $tokens,
                {   %$cond,
                    SearchResultsHeader => $i == 0,
                    SearchResultsFooter => $footer,
                    BlogResultHeader    => $blog_header,
                    BlogResultFooter    => $blog_footer,
                    IfMaxResultsCutoff  => $max_reached,
                }
            )
        ) or return $ctx->error( $build->errstr );
        $output .= $out;

        $this_object = $next_object;
        last unless $this_object;
        $blog_header = $blog_footer ? 1 : 0;
    }

    $ctx->stash( 'blog',    $orig_blog );
    $ctx->stash( 'blog_id', $orig_blog_id );
    $output;
}

sub context_script {
    my ( $ctx, $args, $cond ) = @_;

    my $search_string = decode_html( $ctx->stash('search_string') );
    my $cgipath       = $ctx->invoke_handler( 'cgipath', $args );
    my $script        = $ctx->{config}->SearchScript;
    my $link = $cgipath . $script . '?search=' . encode_url($search_string);
    if ( my $mode = $ctx->stash('mode') ) {
        $mode = encode_url($mode);
        $link .= "&__mode=$mode";
    }
    if ( my $type = $ctx->stash('type') ) {
        $link .= "&type=" . encode_url($type);
    }
    if ( my $include_blogs = $ctx->stash('include_blogs') ) {
        $link .= "&IncludeBlogs=" . encode_url($include_blogs);
    }
    elsif ( my $blog_id = $ctx->stash('blog_id') ) {
        $link .= "&blog_id=" . encode_url($blog_id);
    }

    my $category = $ctx->stash('search_category');
    $link .= "&category=" . encode_url($category) if $category;

    my $author = $ctx->stash('search_author');
    $link .= "&author=" . encode_url($author) if $author;

    my $year = $ctx->stash('search_year');
    $link .= "&year=" . encode_url($year) if $year;

    my $month = $ctx->stash('search_month');
    $link .= "&month=" . encode_url($month) if $month;

    my $day = $ctx->stash('search_day');
    $link .= "&day=" . encode_url($day) if $day;

    my $archive_type = $ctx->stash('search_archive_type');
    $link .= "&archive_type=" . encode_url($archive_type) if $archive_type;

    my $template_id = $ctx->stash('search_template_id');
    $link .= "&template_id=" . encode_url($template_id) if $template_id;

    if ( my $format = $ctx->stash('format') ) {
        $link .= '&format=' . encode_url($format);
    }
    $link;
}

1;
__END__