forked from jayallen/melody
-
Notifications
You must be signed in to change notification settings - Fork 35
/
AuthorDaily.pm
299 lines (252 loc) · 9.46 KB
/
AuthorDaily.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
# Movable Type (r) Open Source (C) 2001-2010 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::ArchiveType::AuthorDaily;
use strict;
use base qw( MT::ArchiveType::Author MT::ArchiveType::Daily );
use MT::Util qw( dirify start_end_day );
sub name {
return 'Author-Daily';
}
sub archive_label {
return MT->translate('AUTHOR-DAILY_ADV');
}
# Corrected the problem where author archive mappings incorrectly implied that Display Names would be used in generating URLs when there is no guarantee that Display Names are unique. Basenames must be used instead. The code actually does the right thing in that it uses Author Basenames already, but the information displayed to the blog administrators in the CMS was never corrected until now. BugzID: 86237.
sub default_archive_templates {
return [ {
label => 'author/author-basename/yyyy/mm/dd/index.html',
template => 'author/%-a/%y/%m/%d/%f',
default => 1
},
{
label => 'author/author_basename/yyyy/mm/dd/index.html',
template => 'author/%a/%y/%m/%d/%f'
},
];
}
sub dynamic_template {
return 'author/<$MTEntryAuthorID$>/<$MTArchiveDate format="%Y%m%d"$>';
}
sub template_params {
return {
archive_class => "author-daily-archive",
author_daily_archive => 1,
archive_template => 1,
archive_listing => 1,
datebased_archive => 1,
};
}
sub archive_title {
my $obj = shift;
my ( $ctx, $entry_or_ts ) = @_;
my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
my $start = start_end_day($stamp);
my $date = MT::Template::Context::_hdlr_date( $ctx,
{ ts => $start, 'format' => "%x" } );
my $author = $obj->display_name($ctx);
sprintf( "%s%s", $author, $date );
}
sub archive_file {
my $obj = shift;
my ( $ctx, %param ) = @_;
my $timestamp = $param{Timestamp};
my $file_tmpl = $param{Template};
my $author = $ctx->{__stash}{author};
my $entry = $ctx->{__stash}{entry};
my $file;
my $this_author = $author ? $author : ( $entry ? $entry->author : undef );
return "" unless $this_author;
my $name = dirify( $this_author->nickname );
if ( $name eq '' || !$file_tmpl ) {
return "" unless $this_author;
$name = "author" . $this_author->id if $name !~ /\w/;
my $start = start_end_day($timestamp);
my ( $year, $month, $day ) = unpack 'A4A2A2', $start;
$file
= sprintf( "%s/%04d/%02d/%02d/index", $name, $year, $month, $day );
}
else {
( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} )
= start_end_day($timestamp);
}
$file;
} ## end sub archive_file
sub archive_group_iter {
my $obj = shift;
my ( $ctx, $args ) = @_;
my $blog = $ctx->stash('blog');
my $sort_order
= ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
my $tmpl = $ctx->stash('template');
my @data = ();
my $count = 0;
my $ts = $ctx->{current_timestamp};
my $tsend = $ctx->{current_timestamp_end};
my $at = $ctx->{archive_type};
my $archiver = MT->publisher->archiver($at);
my $author;
# if (($tmpl && $tmpl->type ne 'index') &&
# ($archiver && $archiver->author_based))
# {
$author = $ctx->stash('author');
# }
require MT::Entry;
my $loop_sub = sub {
my $auth = shift;
my $count_iter =
MT::Entry->count_group_by( {
blog_id => $blog->id,
author_id => $auth->id,
status => MT::Entry::RELEASE(),
(
$ts && $tsend ? ( authored_on => [ $ts, $tsend ] )
: ()
),
},
{ (
$ts && $tsend
? ( range_incl => { authored_on => 1 } )
: ()
),
group => [
"extract(year from authored_on)",
"extract(month from authored_on)",
"extract(day from authored_on)"
],
'sort' => [ {
column => 'extract(year from authored_on)',
desc => $order
},
{
column => 'extract(month from authored_on)',
desc => $order
},
{
column => 'extract(day from authored_on)',
desc => $order
}
],
}
) or return $ctx->error("Couldn't get monthly archive list");
while ( my @row = $count_iter->() ) {
my $hash = {
year => $row[1],
month => $row[2],
day => $row[3],
author => $auth,
count => $row[0],
};
push( @data, $hash );
return $count + 1
if ( defined($limit) && ( $count + 1 ) == $limit );
$count++;
}
return $count;
};
# Count entry by author
if ($author) {
$loop_sub->($author);
}
else {
# load authors
require MT::Author;
my $iter;
$iter = MT::Author->load_iter(
undef,
{
sort => 'name',
direction => $auth_order,
join => [
'MT::Entry',
'author_id',
{
status =>
MT::Entry::RELEASE(),
blog_id => $blog->id
},
{ unique => 1 }
]
}
);
while ( my $a = $iter->() ) {
$loop_sub->($a);
last if ( defined($limit) && $count == $limit );
}
} ## end else [ if ($author) ]
my $loop = @data;
my $curr = 0;
return sub {
if ( $curr < $loop ) {
my $date = sprintf( "%04d%02d%02d000000",
$data[$curr]->{year},
$data[$curr]->{month},
$data[$curr]->{day} );
my ( $start, $end ) = start_end_day($date);
my $count = $data[$curr]->{count};
my %hash = (
author => $data[$curr]->{author},
year => $data[$curr]->{year},
month => $data[$curr]->{month},
day => $data[$curr]->{day},
start => $start,
end => $end
);
$curr++;
return ( $count, %hash );
}
undef;
}
} ## end sub archive_group_iter
sub archive_group_entries {
my $obj = shift;
my ( $ctx, %param ) = @_;
my $ts = $param{year}
? sprintf( "%04d%02d%02d000000",
$param{year}, $param{month}, $param{day} )
: $ctx->stash('current_timestamp');
my $author = $param{author} || $ctx->stash('author');
my $limit = $param{limit};
$obj->dated_author_entries( $ctx, 'Author-Daily', $author, $ts, $limit );
}
sub archive_entries_count {
my $obj = shift;
my ( $blog, $at, $entry ) = @_;
my $auth = $entry->author;
return
$obj->SUPER::archive_entries_count( {
Blog => $blog,
ArchiveType => $at,
Timestamp => $entry->authored_on,
Author => $auth
}
);
}
*date_range = \&MT::ArchiveType::Daily::date_range;
*next_archive_entry = \&MT::ArchiveType::Date::next_archive_entry;
*previous_archive_entry = \&MT::ArchiveType::Date::previous_archive_entry;
1;
__END__
=head1 NAME
MT::ArchiveType::AuthorDaily
=head1 METHODS
=head2 archive_entries_count
=head2 archive_file
=head2 archive_group_entries
=head2 archive_group_iter
=head2 archive_label
=head2 archive_title
=head2 date_range
=head2 default_archive_templates
=head2 dynamic_template
=head2 name
=head2 next_archive_entry
=head2 previous_archive_entry
=head2 template_params
=head1 AUTHOR & COPYRIGHT
Please see L<MT/AUTHOR & COPYRIGHT>.
=cut