/
Entry.pm
2495 lines (1999 loc) · 73.6 KB
/
Entry.pm
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Movable Type (r) Open Source (C) 2001-2011 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::Template::Tags::Entry;
use strict;
use MT;
use MT::Util
qw( offset_time_list encode_html remove_html spam_protect asset_cleanup );
use MT::Entry;
use MT::I18N qw( first_n_text const );
# returns an iterator that supplies entries, in the order of last comment
# date (descending)
sub _rco_entries_iter {
my ( $entry_terms, $entry_args, $blog_terms, $blog_args ) = @_;
my $offset = 0;
my $limit = $entry_args->{limit} || 20;
my @entries;
delete $entry_args->{direction}
if exists $entry_args->{direction};
delete $entry_args->{sort}
if exists $entry_args->{sort};
my $rco_iter = sub {
if ( !@entries ) {
require MT::Comment;
my $iter = MT::Comment->max_group_by(
{ visible => 1,
%$blog_terms,
},
{ join => MT::Entry->join_on(
undef,
{ 'id' => \'=comment_entry_id',
%$entry_terms,
},
{%$entry_args}
),
%$blog_args,
group => ['entry_id'],
max => 'created_on',
offset => $offset,
limit => $limit,
}
);
my @ids;
my %order;
my $num = 0;
while ( my ( $max, $id ) = $iter->() ) {
push @ids, $id;
$order{$id} = $num++;
}
if (@ids) {
@entries = MT::Entry->load( { id => \@ids } );
@entries
= sort { $order{ $a->id } <=> $order{ $b->id } } @entries;
}
}
if (@entries) {
$offset++;
return shift @entries;
}
else {
return undef;
}
};
return Data::ObjectDriver::Iterator->new($rco_iter);
}
###########################################################################
=head2 Entries
The Entries tag is a workhorse of MT publishing. It is used for
publishing a selection of entries in a variety of situations. Typically,
the basic use (specified without any attributes) outputs the selection
of entries that are appropriate for the page being published. But you
can use this tag for publishing custom modules, index templates and
widgets to select content in many different ways.
B<Attributes:>
=over 4
=item * lastn (optional)
Allows you to limit the number of entries output. This attribute
always implies selection of entries based on their 'authored' date, in
reverse chronological order.
<mt:Entries lastn="5" sort_by="title" sort_order="ascend">
This would publish the 5 most recent entries, ordered by their titles.
=item * limit (optional)
Similar to the C<lastn> attribute, but limits output based on whichever
sort order is in use.
=item * sort_by (optional; default "authored_on")
Accepted values are: C<authored_on>, C<title>, C<ping_count>,
C<comment_count>, C<author_id>, C<excerpt>, C<status>, C<created_on>,
C<modified_on>, C<rate>, C<score> (both C<rate> and C<score> require a C<namespace> attribute to be present).
If you have the Professional Pack installed, with custom fields, you
may specify a custom field basename to sort the listing, by giving
a C<sort_by> value of C<field:I<basename>> (where 'basename' is the custom
field basename you wish to sort on).
=item * sort_order (optional)
Accepted values are 'ascend' and 'descend'. The default is the order
specified for publishing entries, set on the blog entry preferences
screen.
=item * field:I<basename>
Permits filtering entries based on a custom field defined (available
when the Commercial Pack is installed).
B<Example:>
<mt:Entries field:special="1" sort_by="authored_on"
sort_order="descend" limit="5">
This selects the last 5 entries that have a "special" custom field
(checkbox field) checked.
=item * namespace (optional)
The namespace attribute is used to specify which scoring namespace
to use when applying the C<sort_by="score"> attribute, or filtering
based on scoring (any of the C<min_*>, C<max_*> attributes require this).
The MT Community Pack provides a 'community_pack_recommend' namespace,
for instance, which can be used to select entries, sorting by number
of recommend/favorite votes that have been made.
=item * class_type (optional; default 'entry')
Accepted values are 'entry' and 'page'.
=item * offset (optional)
Accepted values are any non-zero positive integers, or the keyword
"auto" which is used under dynamic publishing to automatically
determine the offset based on the C<offset> query parameter for
the request.
=item * category or categories (optional)
This attribute allows you to filter the entries based on category
assignment. The simple case is to filter for a single category,
where the full category name is specified:
<mt:Entries category="Featured">
If you have multiple categories with the same name, you can give
their parent category names to be more explicit:
<mt:Entries category="News/Featured">
or
<mt:Entries category="Projects/Featured">
You can also use 'AND', 'OR' and 'NOT' operators to include or
exclude categories:
<mt:Entries categories="(Family OR Pets) AND Featured">
or
<mt:Entries categories="NOT Family">
=item * include_subcategories (optional)
If this attribute is specified in conjunction with the category (or
categories) attribute, it will cause any entries assigned to subcategories
of the identified category/categories to be included as well.
=item * tag or tags (optional)
This attribute functions similarly to the C<category> attribute, but
filters based on tag assignments. It also supports the logical operators
described for category selection.
=item * author (optional)
Accepts an author's username to filter entry selection.
B<Example:>
<mt:Entries author="Melody">
=item * id (optional)
If specified, selects a single entry matching the given entry ID.
<mt:Entries id="10">
=item * min_score (optional)
=item * max_score (optional)
=item * min_rate (optional)
=item * max_rate (optional)
=item * min_count (optional)
=item * max_count (optional)
Allows filtering of entries based on score, rating or count. Each of
the attributes require the C<namespace> attribute.
=item * scored_by (optional)
Allows filtering of entries that were scored by a particular user,
specified by username. Requires the C<namespace> attribute.
=item * days (optional)
Limits the selection of entries to a specified number of days,
based on the current date. For instance, if you specify:
<mt:Entries days="10">
only entries that were authored within the last 10 days will be
published.
=item * recently_commented_on (optional)
Selects the list of entries that have received published comments
recently. The value of this attribute is the number of days to use
to limit the selection. For instance:
<mt:Entries recently_commented_on="10">
will select entries that received published comments within the last
10 days. The order of the entries is the date of the most recently
received comment.
=item * unique
If specified, this flag will cause MT to keep track of which entries
are being published for a given page. It will also prevent the publishing
of entries already published.
For example, if you wish to publish the last 3 entries that are
tagged "@featured", but wish to exclude these entries from the set
of entries that follow, you can do this:
<mt:Entries tag="@featured" lastn="3">
...
</mt:Entries>
<mt:Entries lastn="7" unique="1">
...
</mt:Entries>
The second Entries tag will exclude any entries that were output
from the first Entries tag.
=item * glue (optional)
Specifies a string that is output inbetween published entries.
B<Example:>
<mt:Entries glue=","><$mt:EntryID$></mt:Entries>
outputs something like this:
10,9,8,7,6,5,4,3,2,1
=back
=for tags multiblog, loop, scoring
=cut
sub _hdlr_entries {
my ( $ctx, $args, $cond ) = @_;
return $ctx->error(
MT->translate(
'sort_by="score" must be used in combination with namespace.')
)
if ( ( exists $args->{sort_by} )
&& ( 'score' eq $args->{sort_by} )
&& ( !exists $args->{namespace} ) );
my $cfg = $ctx->{config};
my $at = $ctx->{current_archive_type} || $ctx->{archive_type};
my $blog_id = $ctx->stash('blog_id');
my $blog = $ctx->stash('blog');
my ( @filters, %blog_terms, %blog_args, %terms, %args );
# for the case that we want to use mt:Entries with mt-search
# send to MT::Template::Search if searh results are found
if ( $ctx->stash('results') && $args->{search_results} == 1 ) {
require MT::Template::Context::Search;
return MT::Template::Context::Search::_hdlr_results( $ctx, $args,
$cond );
}
$ctx->set_blog_load_context( $args, \%blog_terms, \%blog_args )
or return $ctx->error( $ctx->errstr );
%terms = %blog_terms;
%args = %blog_args;
my $class_type = $args->{class_type} || 'entry';
my $class = MT->model($class_type);
my $cat_class_type = $class->container_type();
my $cat_class = MT->model($cat_class_type);
my %fields;
foreach my $arg ( keys %$args ) {
if ( $arg =~ m/^field:(.+)$/ ) {
$fields{$1} = $args->{$arg};
}
}
my $use_stash = 1;
# For the stock Entries/Pages tags, clear any prepopulated
# entries list (placed by archive publishing) if we're invoked
# with any of the following attributes. A plugin tag may
# prepopulate the entries stash and then invoke this handler
# to permit further filtering of the entries.
my $tag = lc $ctx->stash('tag');
if ( ( $tag eq 'entries' ) || ( $tag eq 'pages' ) ) {
foreach
my $args_key ( 'category', 'categories', 'tag', 'tags', 'author' )
{
if ( exists( $args->{$args_key} ) ) {
$use_stash = 0;
last;
}
}
}
if ($use_stash) {
foreach my $args_key (
'id', 'days',
'recently_commented_on', 'include_subcategories',
'include_blogs', 'exclude_blogs',
'blog_ids'
)
{
if ( exists( $args->{$args_key} ) ) {
$use_stash = 0;
last;
}
}
}
if ( $use_stash && %fields ) {
$use_stash = 0;
}
my $entries;
if ($use_stash) {
$entries = $ctx->stash('entries');
if ( !$entries && $at ) {
my $archiver = MT->publisher->archiver($at);
if ( $archiver && $archiver->group_based ) {
$entries = $archiver->archive_group_entries( $ctx, %$args );
}
}
}
if ( $entries && scalar @$entries ) {
my $entry = @$entries[0];
if ( !$entry->isa($class) ) {
# class types do not match; we can't use stashed entries
undef $entries;
}
}
$entries = undef unless defined $entries && scalar @$entries;
local $ctx->{__stash}{entries};
# handle automatic offset based on 'offset' query parameter
# in case we're invoked through mt-view.cgi or some other
# app.
if ( ( $args->{offset} || '' ) eq 'auto' ) {
$args->{offset} = 0;
if ( ( $args->{lastn} || $args->{limit} )
&& ( my $app = MT->instance ) )
{
if ( $app->isa('MT::App') ) {
if ( my $offset = $app->param('offset') ) {
$args->{offset} = $offset;
}
}
}
}
if ( ( $args->{limit} || '' ) eq 'auto' ) {
my ( $days, $limit );
my $blog = $ctx->stash('blog');
if ( $blog && ( $days = $blog->days_on_index ) ) {
my @ago = offset_time_list( time - 3600 * 24 * $days, $blog_id );
my $ago = sprintf "%04d%02d%02d%02d%02d%02d",
$ago[5] + 1900, $ago[4] + 1, @ago[ 3, 2, 1, 0 ];
$terms{authored_on} = [$ago];
$args{range_incl}{authored_on} = 1;
}
elsif ( $blog && ( $limit = $blog->entries_on_index ) ) {
$args->{lastn} = $limit;
}
else {
delete $args->{limit};
}
}
elsif ( $args->{limit} && ( $args->{limit} > 0 ) ) {
$args->{lastn} = $args->{limit};
}
$terms{status} = MT::Entry::RELEASE();
if ( !$entries ) {
if ( $ctx->{inside_mt_categories} ) {
if ( my $cat = $ctx->stash('category') ) {
$args->{category} ||= [ 'OR', [$cat] ]
if $cat->class eq $cat_class_type;
}
}
elsif ( my $cat = $ctx->stash('archive_category') ) {
$args->{category} ||= [ 'OR', [$cat] ]
if $cat->class eq $cat_class_type;
}
}
# kinds of <MTEntries> uses...
# * from an index template
# * from an archive context-- entries are prepopulated
# Adds a category filter to the filters list.
if ( my $category_arg = $args->{category} || $args->{categories} ) {
my ( $cexpr, $cats );
if ( ref $category_arg ) {
my $is_and = ( shift @{$category_arg} ) eq 'AND';
$cats = [ @{ $category_arg->[0] } ];
$cexpr = $ctx->compile_category_filter(
undef, $cats,
{ 'and' => $is_and,
children => $cat_class_type eq 'category'
? ( $args->{include_subcategories} ? 1 : 0 )
: ( $args->{include_subfolders} ? 1 : 0 )
}
);
}
else {
if (( $category_arg !~ m/\b(AND|OR|NOT)\b|[(|&]/i )
&& (( $cat_class_type eq 'category'
&& !$args->{include_subcategories}
)
|| ( $cat_class_type ne 'category'
&& !$args->{include_subfolders} )
)
)
{
my @cats
= $ctx->cat_path_to_category( $category_arg,
[ \%blog_terms, \%blog_args ],
$cat_class_type );
if (@cats) {
$cats = \@cats;
}
$cexpr
= $ctx->compile_category_filter( $category_arg, $cats );
}
else {
my @cats = $cat_class->load( \%blog_terms, \%blog_args );
if (@cats) {
$cats = \@cats;
$cexpr = $ctx->compile_category_filter(
$category_arg,
$cats,
{ children => $cat_class_type eq 'category'
? ( $args->{include_subcategories} ? 1 : 0 )
: ( $args->{include_subfolders} ? 1 : 0 )
}
);
}
}
}
if ($cexpr) {
my %map;
require MT::Placement;
my @cat_ids = map { $_->id } @$cats;
my $preloader = sub {
my ($id) = @_;
my @c_ids = MT::Placement->load(
{ category_id => \@cat_ids, entry_id => $id },
{ fetchonly => ['category_id'], no_triggers => 1 }
);
my %map;
$map{ $_->category_id } = 1 for @c_ids;
\%map;
};
if ( !$entries ) {
if ( $category_arg !~ m/\bNOT\b/i ) {
$args{join} = MT::Placement->join_on(
'entry_id',
{ category_id => \@cat_ids,
%blog_terms
},
{ %blog_args, unique => 1 }
);
}
}
push @filters, sub { $cexpr->( $preloader->( $_[0]->id ) ) };
}
else {
return $ctx->error(
MT->translate(
"You have an error in your '[_2]' attribute: [_1]",
$category_arg, $cat_class_type
)
);
}
}
# Adds a tag filter to the filters list.
if ( my $tag_arg = $args->{tags} || $args->{tag} ) {
require MT::Tag;
require MT::ObjectTag;
my $terms;
if ( $tag_arg !~ m/\b(AND|OR|NOT)\b|\(|\)/i ) {
my @tags = MT::Tag->split( ',', $tag_arg );
$terms = { name => \@tags };
$tag_arg = join " or ", @tags;
}
my $tags = [
MT::Tag->load(
$terms,
{ ( $terms ? ( binary => { name => 1 } ) : () ),
join => MT::ObjectTag->join_on(
'tag_id',
{ object_datasource => $class->datasource,
%blog_terms,
},
{ %blog_args, unique => 1 }
),
}
)
];
my $cexpr = $ctx->compile_tag_filter( $tag_arg, $tags );
if ($cexpr) {
my @tag_ids
= map { $_->id, ( $_->n8d_id ? ( $_->n8d_id ) : () ) } @$tags;
my $preloader = sub {
my ($entry_id) = @_;
my %map;
return \%map unless @tag_ids;
my $terms = {
tag_id => \@tag_ids,
object_id => $entry_id,
object_datasource => $class->datasource,
%blog_terms,
};
my $args = {
%blog_args,
fetchonly => ['tag_id'],
no_triggers => 1
};
my @ot_ids = MT::ObjectTag->load( $terms, $args ) if @tag_ids;
$map{ $_->tag_id } = 1 for @ot_ids;
\%map;
};
if ( !$entries ) {
if ( $tag_arg !~ m/\bNOT\b/i ) {
return '' unless @tag_ids;
$args{join} = MT::ObjectTag->join_on(
'object_id',
{ tag_id => \@tag_ids,
object_datasource => 'entry',
%blog_terms
},
{ %blog_args, unique => 1 }
);
}
}
push @filters, sub { $cexpr->( $preloader->( $_[0]->id ) ) };
}
else {
return $ctx->error(
MT->translate(
"You have an error in your '[_2]' attribute: [_1]",
$tag_arg, 'tag'
)
);
}
}
# Adds an author filter to the filters list.
if ( my $author_name = $args->{author} ) {
require MT::Author;
my $author = MT::Author->load( { name => $author_name } )
or return $ctx->error(
MT->translate( "No such user '[_1]'", $author_name ) );
if ($entries) {
push @filters, sub { $_[0]->author_id == $author->id };
}
else {
$terms{author_id} = $author->id;
}
}
if ( my $f = MT::Component->registry( "tags", "filters", "Entries" ) ) {
foreach my $set (@$f) {
foreach my $fkey ( keys %$set ) {
if ( exists $args->{$fkey} ) {
my $h = $set->{$fkey}{code}
||= MT->handler_to_coderef( $set->{$fkey}{handler} );
next unless ref($h) eq 'CODE';
local $ctx->{filters} = \@filters;
local $ctx->{terms} = \%terms;
local $ctx->{args} = \%args;
$h->( $ctx, $args, $cond );
}
}
}
}
# Adds an ID filter to the filter list.
if ( ( my $target_id = $args->{id} )
&& ( ref( $args->{id} ) || ( $args->{id} =~ m/^\d+$/ ) ) )
{
if ($entries) {
if ( ref $target_id eq 'ARRAY' ) {
my %ids = map { $_ => 1 } @$target_id;
push @filters, sub { exists $ids{ $_[0]->id } };
}
else {
push @filters, sub { $_[0]->id == $target_id };
}
}
else {
$terms{id} = $target_id;
}
}
if ( $args->{namespace} ) {
my $namespace = $args->{namespace};
my $need_join = 0;
for my $f
qw( min_score max_score min_rate max_rate min_count max_count scored_by )
{
if ( $args->{$f} ) {
$need_join = 1;
last;
}
}
if ($need_join) {
my $scored_by = $args->{scored_by} || undef;
if ($scored_by) {
require MT::Author;
my $author = MT::Author->load( { name => $scored_by } )
or return $ctx->error(
MT->translate( "No such user '[_1]'", $scored_by ) );
$scored_by = $author;
}
$args{join} = MT->model('objectscore')->join_on(
undef,
{ object_id => \'=entry_id',
object_ds => 'entry',
namespace => $namespace,
( !$entries && $scored_by
? ( author_id => $scored_by->id )
: ()
),
},
{ unique => 1, }
);
if ( $entries && $scored_by ) {
push @filters,
sub { $_[0]->get_score( $namespace, $scored_by ) };
}
}
# Adds a rate or score filter to the filter list.
if ( $args->{min_score} ) {
push @filters,
sub { $_[0]->score_for($namespace) >= $args->{min_score}; };
}
if ( $args->{max_score} ) {
push @filters,
sub { $_[0]->score_for($namespace) <= $args->{max_score}; };
}
if ( $args->{min_rate} ) {
push @filters,
sub { $_[0]->score_avg($namespace) >= $args->{min_rate}; };
}
if ( $args->{max_rate} ) {
push @filters,
sub { $_[0]->score_avg($namespace) <= $args->{max_rate}; };
}
if ( $args->{min_count} ) {
push @filters,
sub { $_[0]->vote_for($namespace) >= $args->{min_count}; };
}
if ( $args->{max_count} ) {
push @filters,
sub { $_[0]->vote_for($namespace) <= $args->{max_count}; };
}
}
# Adds an count of comments filter to the filter list.
if ( exists $args->{min_comment} && exists $args->{max_comment} ) {
$terms{comment_count}
= { 'between' =>
[ int( $args->{min_comment} ), int( $args->{max_comment} ) ]
};
}
elsif ( exists $args->{min_comment} ) {
$terms{comment_count} = { '>=' => int( $args->{min_comment} ) };
}
elsif ( exists $args->{max_comment} ) {
$terms{comment_count} = { '<=' => int( $args->{max_comment} ) };
}
my $published = $ctx->{__stash}{entry_ids_published} ||= {};
if ( $args->{unique} ) {
push @filters, sub { !exists $published->{ $_[0]->id } }
}
my $namespace = $args->{namespace};
my $no_resort = 0;
my $post_sort_limit = 0;
my $post_sort_offset = 0;
my @entries;
if ( !$entries ) {
my ( $start, $end )
= ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} );
if ( $start && $end ) {
$terms{authored_on} = [ $start, $end ];
$args{range_incl}{authored_on} = 1;
}
if ( my $days = $args->{days} ) {
my @ago = offset_time_list( time - 3600 * 24 * $days, $blog_id );
my $ago = sprintf "%04d%02d%02d%02d%02d%02d",
$ago[5] + 1900, $ago[4] + 1, @ago[ 3, 2, 1, 0 ];
$terms{authored_on} = [$ago];
$args{range_incl}{authored_on} = 1;
}
else {
# Check attributes
my $found_valid_args = 0;
foreach my $valid_key (
'lastn', 'category', 'categories', 'tag',
'tags', 'author', 'days', 'min_score',
'max_score', 'min_rate', 'max_rate', 'min_count',
'max_count'
)
{
if ( exists( $args->{$valid_key} ) ) {
$found_valid_args = 1;
last;
}
}
if ( !$found_valid_args ) {
# Uses weblog settings
if ( my $days = $blog ? $blog->days_on_index : 10 ) {
my @ago = offset_time_list( time - 3600 * 24 * $days,
$blog_id );
my $ago = sprintf "%04d%02d%02d%02d%02d%02d",
$ago[5] + 1900, $ago[4] + 1, @ago[ 3, 2, 1, 0 ];
$terms{authored_on} = [$ago];
$args{range_incl}{authored_on} = 1;
}
elsif ( my $limit = $blog ? $blog->entries_on_index : 10 ) {
$args->{lastn} = $limit;
}
}
}
# Adds class_type
$terms{class} = $class_type;
$args{'sort'} = 'authored_on';
if ( $args->{sort_by} ) {
$args->{sort_by} =~ s/:/./; # for meta:name => meta.name
$args->{sort_by} = 'ping_count'
if $args->{sort_by} eq 'trackback_count';
if ( $class->is_meta_column( $args->{sort_by} ) ) {
$post_sort_limit = delete( $args->{limit} ) || 0;
$post_sort_offset = delete( $args->{offset} ) || 0;
if ( $post_sort_limit || $post_sort_offset ) {
delete $args->{lastn};
}
$no_resort = 0;
}
elsif ( $class->has_column( $args->{sort_by} ) ) {
$args{sort} = $args->{sort_by};
$no_resort = 1;
}
elsif (
$args->{limit}
&& ( 'score' eq $args->{sort_by}
|| 'rate' eq $args->{sort_by} )
)
{
$post_sort_limit = delete( $args->{limit} ) || 0;
$post_sort_offset = delete( $args->{offset} ) || 0;
if ( $post_sort_limit || $post_sort_offset ) {
delete $args->{lastn};
}
$no_resort = 0;
}
}
if (%fields) {
# specifies we need a join with entry_meta;
# for now, we support one join
my ( $col, $val ) = %fields;
my $type = MT::Meta->metadata_by_name( $class, 'field.' . $col );
$args{join} = [
$class->meta_pkg,
undef,
{ type => 'field.' . $col,
$type->{type} => $val,
'entry_id' => \'= entry_id'
}
];
}
if ( !@filters ) {
if ( ( my $last = $args->{lastn} ) && ( !exists $args->{limit} ) )
{
$args{direction} = 'descend';
$args{sort} = 'authored_on';
$args{limit} = $last;
$no_resort = 0 if $args->{sort_by};
}
else {
$args{direction} = $args->{sort_order} || 'descend'
if exists( $args{sort} );
$no_resort = 1 unless $args->{sort_by};
if ( ( my $last = $args->{lastn} )
&& ( exists $args->{limit} ) )
{
$args{limit} = $last;
}
}
$args{offset} = $args->{offset} if $args->{offset};
if ( $args->{recently_commented_on} ) {
my $entries_iter
= _rco_entries_iter( \%terms, \%args, \%blog_terms,
\%blog_args );
my $limit = $args->{recently_commented_on};
while ( my $e = $entries_iter->() ) {
push @entries, $e;
last unless --$limit;
}
$no_resort = $args->{sort_order} || $args->{sort_by} ? 0 : 1;
}
else {
@entries = $class->load( \%terms, \%args );
}
}
else {
if ( ( $args->{lastn} ) && ( !exists $args->{limit} ) ) {
$args{direction} = 'descend';
$args{sort} = 'authored_on';
$no_resort = 0 if $args->{sort_by};
}
else {
$args{direction} = $args->{sort_order} || 'descend';
$no_resort = 1 unless $args->{sort_by};
}
my $iter;
if ( $args->{recently_commented_on} ) {
$args->{lastn} = $args->{recently_commented_on};
$iter = _rco_entries_iter( \%terms, \%args, \%blog_terms,
\%blog_args );
$no_resort = $args->{sort_order} || $args->{sort_by} ? 0 : 1;
}
else {
$iter = $class->load_iter( \%terms, \%args );
}
my $i = 0;
my $j = 0;
my $off = $args->{offset} || 0;
my $n = $args->{lastn};
ENTRY: while ( my $e = $iter->() ) {
for (@filters) {
next ENTRY unless $_->($e);
}
next if $off && $j++ < $off;
push @entries, $e;
$i++;
$iter->end, last if $n && $i >= $n;
}
}
}
else {
# Don't resort a predefined list that's not in a published archive
# page when we didn't request sorting.
if ( $args->{sort_by} || $args->{sort_order} || $ctx->{archive_type} )
{
my $so
= $args->{sort_order}
|| ( $blog ? $blog->sort_order_posts : undef )
|| '';
my $col = $args->{sort_by} || 'authored_on';
if ( $col ne 'score' ) {
if ( my $def = $class->column_def($col) ) {
if ( $def->{type} =~ m/^integer|float$/ ) {
@$entries
= $so eq 'ascend'
? sort { $a->$col() <=> $b->$col() } @$entries
: sort { $b->$col() <=> $a->$col() } @$entries;
}
else {
@$entries
= $so eq 'ascend'
? sort { $a->$col() cmp $b->$col() } @$entries
: sort { $b->$col() cmp $a->$col() } @$entries;
}
$no_resort = 1;
}
else {
$col =~ s/(^field):(.*)/$1.$2/ig;
if ( $class->is_meta_column($col) ) {
my $type = MT::Meta->metadata_by_name( $class, $col );
no warnings;
if ( $type->{type} =~ m/integer|float/ ) {
@$entries
= $so eq 'ascend'
? sort { $a->$col() <=> $b->$col() } @$entries
: sort { $b->$col() <=> $a->$col() }
@$entries;
}
else {
@$entries
= $so eq 'ascend'
? sort { $a->$col() cmp $b->$col() } @$entries
: sort { $b->$col() cmp $a->$col() }
@$entries;
}
$no_resort = 1;
}
}
}
}
else {
$no_resort = 1;
}
if (@filters) {
my $i = 0;
my $j = 0;
my $off = $args->{offset} || 0;
my $n = $args->{lastn};
ENTRY2: foreach my $e (@$entries) {
for (@filters) {
next ENTRY2 unless $_->($e);
}
next if $off && $j++ < $off;
push @entries, $e;
$i++;
last if $n && $i >= $n;
}
}
else {
my $offset;
if ( $offset = $args->{offset} ) {
if ( $offset < scalar @$entries ) {
@entries = @$entries[ $offset .. $#$entries ];
}
else {
@entries = ();
}
}
else {
@entries = @$entries;
}
if ( my $last = $args->{lastn} ) {
if ( scalar @entries > $last ) {
@entries = @entries[ 0 .. $last - 1 ];
}
}