root/branches/dipper/php/lib/archive_lib.php @ 1748

Revision 1748, 63.3 kB (checked in by takayama, 20 months ago)

Fixed BugId:75148
* Fixed for nested category based archive count issue.

  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id$
7
8require_once("MTUtil.php");
9
10// Create default archivers
11global $_archivers;
12
13// Date-based archives
14register_archiver(new YearlyArchiver());
15register_archiver(new MonthlyArchiver());
16register_archiver(new WeeklyArchiver());
17register_archiver(new DailyArchiver());
18register_archiver(new MonthlyArchiver());
19
20// Indivudual archives
21register_archiver(new IndividualArchiver());
22register_archiver(new PageArchiver());
23
24// Author-based archives
25register_archiver(new AuthorBasedArchiver());
26register_archiver(new YearlyAuthorBasedArchiver());
27register_archiver(new MonthlyAuthorBasedArchiver());
28register_archiver(new WeeklyAuthorBasedArchiver());
29register_archiver(new DailyAuthorBasedArchiver());
30
31// Date-based category archives
32register_archiver(new YearlyCategoryArchiver());
33register_archiver(new MonthlyCategoryArchiver());
34register_archiver(new DailyCategoryArchiver());
35register_archiver(new WeeklyCategoryArchiver());
36
37function register_archiver($archiver) {
38    global $_archivers;
39    $_archivers[$archiver->get_archive_name()] = $archiver;
40}
41
42function _hdlr_archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
43    global $_archivers;
44    $at = $args['archive_type'];
45    $at or $at = $ctx->stash('current_archive_type');
46    if ($at == 'Category') {
47        require_once("block.mtcategorynext.php");
48        return smarty_block_mtcategorynext($args, $content, $ctx, $repeat);
49    }
50    $archiver = $_archivers[$at];
51    if (!isset($archiver)) {
52        $repeat = false;
53        return '';
54    }
55    return $archiver->archive_prev_next($args, $content, $ctx, $repeat, $tag, $at);
56}
57
58class BaseArchiver {
59    // Abstract Method (needs override)
60    function get_label($args, $ctx) { }
61    function get_title($args, $ctx) { }
62    function get_range(&$ctx, &$row) { }
63    function get_archive_name() { }
64    function &get_archive_list($ctx, $args) { }
65    function get_archive_link_sql($ctx, $ts, $at, $args) { }
66    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) { }
67    function prepare_list(&$ctx, &$row) { }
68    function setup_args($ctx, &$args) { }
69    function template_params(&$ctx) { }
70    function is_date_based() { return false; }
71}
72
73class PageArchiver extends BaseArchiver {
74    function get_label($args, $ctx) {
75        return $ctx->mt->translate("Page");
76    }
77
78    function get_archive_name() {
79        return 'Page';
80    }
81
82    function template_params(&$ctx) {
83        $vars =& $ctx->__stash['vars'];
84        $vars['main_template'] = 1;
85        $vars['archive_template'] = 1;
86        $vars['page_template'] = 1;
87        $vars['feedback_template'] = 1;
88        $vars['page_archive'] = 1;
89        $vars['archive_class'] = 'page-archive';
90    }
91}
92
93class IndividualArchiver extends BaseArchiver {
94    // Override Method
95    function get_label($args, $ctx) {
96        return $ctx->mt->translate("Individual");
97    }
98
99    function get_title($args, $ctx) {
100        return $ctx->tag('EntryTitle', $args);
101    }
102
103    function get_range(&$ctx, &$row) {
104        if (is_array($row)) {
105            $period_start = $row[1];
106        } else {
107            $period_start = $row;
108        }
109        $period_start = preg_replace('/[^0-9]/', '', $period_start);
110        return start_end_day($period_start, $ctx->stash('blog'));
111    }
112
113    function get_archive_name() {
114        return 'Individual';
115    }
116   
117    function &get_archive_list($ctx, $args) {
118        return $ctx->mt->db->get_archive_list($args);
119    }
120
121    function get_archive_link_sql($ctx, $ts, $at, $args) {
122        return '';
123    }
124
125    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
126        return $ctx->error(
127            "You used an <mt$tag> without a date context set up.");
128    }
129
130    function prepare_list(&$ctx, &$row) {
131        $entry_id = $row[0];
132        $entry = $ctx->mt->db->fetch_entry($entry_id);
133        $ctx->stash('entry', $entry);
134        $ctx->stash('entries', array());
135    }
136
137    function template_params(&$ctx) {
138        $vars =& $ctx->__stash['vars'];
139        $vars['main_template'] = 1;
140        $vars['archive_template'] = 1;
141        $vars['entry_template'] = 1;
142        $vars['feedback_template'] = 1;
143        $vars['archive_class'] = 'entry-archive';
144    }
145}
146
147class DateBasedArchiver extends BaseArchiver {
148
149    // Override Method
150    function is_date_based() { return true; }
151
152    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag, $at) {
153        $localvars = array('current_timestamp', 'current_timestamp_end', 'entries');
154        if (!isset($content)) {
155            $ctx->localize($localvars);
156            $is_prev = $tag == 'archiveprevious';
157            $ts = $ctx->stash('current_timestamp');
158            if (!$ts) {
159                return $ctx->error(
160                   "You used an <mt$tag> without a date context set up.");
161            }
162            $order = $is_prev ? 'previous' : 'next';
163            $helper = $this->get_helper($at);
164            if ($entry = $this->get_entry($ts, $ctx->stash('blog_id'), $at, $order)) {
165                $ctx->stash('entries', array( $entry ));
166                list($start, $end) = $helper($entry['entry_authored_on']);
167                $ctx->stash('current_timestamp', $start);
168                $ctx->stash('current_timestamp_end', $end);
169            } else {
170                $ctx->restore($localvars);
171                $repeat = false;
172            }
173        } else {
174            $ctx->restore($localvars);
175        }
176        return $content;
177    }
178
179    // Functions
180    function get_helper($at) {
181        if (strtoupper($at) == 'YEARLY') {
182            return 'start_end_year';
183        } elseif (strtoupper($at) == 'MONTHLY') {
184            return 'start_end_month';
185        } elseif (strtoupper($at) == 'WEEKLY') {
186            return 'start_end_week';
187        } elseif (strtoupper($at) == 'DAILY') {
188            return 'start_end_day';
189        } else {
190            return null;
191        }
192    }
193
194    function get_entry($ts, $blog_id, $at, $order) {
195        $helper = $this->get_helper($at);
196        list($start, $end) = $helper($ts);
197        $args = array();
198        if ($order == 'previous') {
199            $args['current_timestamp_end'] = $this->dec_ts($start);
200        } else {
201            $args['current_timestamp'] = $this->inc_ts($end);
202            $args['base_sort_order'] = 'ascend'; # ascending order
203        }
204        $args['lastn'] = 1;
205        $args['blog_id'] = $blog_id;
206        global $mt;
207        list($entry) = $mt->db->fetch_entries($args);
208        return $entry;
209    }
210
211    function dec_ts($ts) {
212        $y = substr($ts, 0, 4);
213        $mo = substr($ts, 4, 2);
214        $d = substr($ts, 6, 2);
215        $h = substr($ts, 8, 2);
216        $m = substr($ts, 10, 2);
217        $s = substr($ts, 12, 2);
218        $s--;
219        if ($s == -1) {
220            $s = 59; $m--;
221            if ($m == -1) {
222                $m = 59; $h--;
223                if ($h == -1) {
224                    $h = 23; $d--;
225                    if ($d == 0) {
226                        $mo--;
227                        if ($mo == 0) {
228                            $mo = 12; $y--;
229                        }
230                        $d = days_in($mo, $y);
231                    }
232                }
233            }
234        }
235        return sprintf("%04d%02d%02d%02d%02d%02d", $y, $mo, $d, $h, $m, $s);
236    }
237
238    function inc_ts($ts) {
239        $y = substr($ts, 0, 4);
240        $mo = substr($ts, 4, 2);
241        $d = substr($ts, 6, 2);
242        $h = substr($ts, 8, 2);
243        $m = substr($ts, 10, 2);
244        $s = substr($ts, 12, 2);
245        $s++;
246        if ($s == 60) {
247            $s = 0; $m++;
248            if ($m == 60) {
249                $m = 0; $h++;
250                if ($h == 24) {
251                    $h = 0; $d++;
252                    $days = days_in($mo, $y);
253                    if ($d > $days) {
254                        $d = 1; $mo++;
255                        if ($mo == 13) {
256                            $mo = 1; $y++;
257                        }
258                    }
259                }
260            }
261        }
262        return sprintf("%04d%02d%02d%02d%02d%02d", $y, $mo, $d, $h, $m, $s);
263    }
264
265    function template_params(&$ctx) {
266        $vars =& $ctx->__stash['vars'];
267        $vars['datebased_archive'] = 1;
268    }
269}
270
271class YearlyArchiver extends DateBasedArchiver {
272
273    function YearlyArchiver() { }
274
275    // Override Method
276    function get_label($args, $ctx) {
277        return $ctx->mt->translate('Yearly');
278    }
279    function get_archive_name() {
280        return 'Yearly';
281    }
282
283    function get_title($args, $ctx) {
284        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
285        list($start) = start_end_year($stamp, $ctx->stash('blog'));
286        $format = $args['format'];
287        $blog = $ctx->stash('blog');
288        global $mt;
289        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] :
290            $mt->config('DefaultLanguage'));
291            if (strtolower($lang) == 'jp' || strtolower($lang) == 'ja') {
292            $format or $format = "%Y&#24180;";
293        } else {
294            $format or $format = "%Y";
295        }
296       
297        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
298    }
299
300    function get_range(&$ctx, &$row) {
301        if (is_array($row)) {
302            $period_start = sprintf("%04d0101000000", $row[0]);
303        } else {
304            $period_start = $row;
305        }
306        return start_end_year($period_start, $ctx->stash('blog'));
307    }
308
309    function &get_archive_list($ctx, $args) {
310        return $ctx->mt->db->get_archive_list($args);
311    }
312   
313    function get_archive_link_sql($ctx, $ts, $at, $args) {
314        return '';
315    }
316
317    function template_params(&$ctx) {
318        parent::template_params($ctx);
319        $vars =& $ctx->__stash['vars'];
320        $vars['archive_template'] = 1;
321        $vars['main_template'] = 1;
322        $vars['datebased_only_archive'] = 1;
323        $vars['datebased_yearly_archive'] = 1;
324        $vars['archive_class'] = 'datebased-yearly-archive';
325        $vars['module_yearly_archives'] = 1;
326    }
327}
328
329class MonthlyArchiver extends DateBasedArchiver {
330
331    function MonthlyArchiver() { }
332
333    // Override Method
334    function get_label($args, $ctx) {
335        return $ctx->mt->translate('Monthly');
336    }
337    function get_archive_name() {
338        return 'Monthly';
339    }
340
341    function get_title($args, $ctx) {
342        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
343        list($start) = start_end_month($stamp, $ctx->stash('blog'));
344        $format = $args['format'];
345        $format or $format = "%B %Y";
346        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
347    }
348
349    function get_range(&$ctx, &$row) {
350        if (is_array($row)) {
351            $period_start = sprintf("%04d%02d01000000", $row[0], $row[1]);
352        } else {
353            $period_start = $row;
354        }
355        return start_end_month($period_start, $ctx->stash('blog'));
356    }
357
358    function &get_archive_list($ctx, $args) {
359        $inside = $ctx->stash('inside_archive_list');
360        if (!isset($inside)) {
361          $inside = false;
362        }
363        $args['inside_archive_list'] = $inside;
364        $args['current_timestamp'] = $ctx->stash('current_timestamp');
365        $args['current_timestamp_end'] = $ctx->stash('current_timestamp_end');
366        return $ctx->mt->db->get_archive_list($args);
367    }
368   
369    function get_archive_link_sql($ctx, $ts, $at, $args) {
370        return '';
371    }
372
373    function template_params(&$ctx) {
374        parent::template_params($ctx);
375        $vars =& $ctx->__stash['vars'];
376        $vars['archive_template'] = 1;
377        $vars['main_template'] = 1;
378        $vars['datebased_only_archive'] = 1;
379        $vars['datebased_monthly_archive'] = 1;
380        $vars['archive_class'] = 'datebased-monthly-archive';
381    }
382}
383
384class DailyArchiver extends DateBasedArchiver {
385
386    function DailyArchiver() { }
387
388    // Override Method
389    function get_label($args, $ctx) {
390        return $ctx->mt->translate('Daily');
391    }
392    function get_archive_name() {
393        return 'Daily';
394    }
395
396    function get_title($args, $ctx) {
397        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
398        list($start) = start_end_day($stamp, $ctx->stash('blog'));
399        $format = $args['format'];
400        $format or $format = "%x";
401        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
402    }
403
404    function get_range(&$ctx, &$row) {
405        if (is_array($row)) {
406            $period_start = sprintf("%04d%02d%02d000000", $row[0], $row[1], $row[2]);
407        } else {
408            $period_start = $row;
409        }
410        return start_end_day($period_start, $ctx->stash('blog'));
411    }
412
413    function &get_archive_list($ctx, $args) {
414        $inside = $ctx->stash('inside_archive_list');
415        if (!isset($inside)) {
416          $inside = false;
417        }
418        $args['inside_archive_list'] = $inside;
419        $args['current_timestamp'] = $ctx->stash('current_timestamp');
420        $args['current_timestamp_end'] = $ctx->stash('current_timestamp_end');
421        return $ctx->mt->db->get_archive_list($args);
422    }
423   
424    function get_archive_link_sql($ctx, $ts, $at, $args) {
425        return '';
426    }
427
428    function template_params(&$ctx) {
429        parent::template_params($ctx);
430        $vars =& $ctx->__stash['vars'];
431        $vars['archive_template'] = 1;
432        $vars['main_template'] = 1;
433        $vars['datebased_only_archive'] = 1;
434        $vars['datebased_daily_archive'] = 1;
435        $vars['archive_class'] = 'datebased-daily-archive';
436    }
437}
438
439class WeeklyArchiver extends DateBasedArchiver {
440
441    function WeeklyArchiver() { }
442
443    // Override Method
444    function get_label($args, $ctx) {
445        return $ctx->mt->translate('Weekly');
446    }
447    function get_archive_name() {
448        return 'Weekly';
449    }
450
451    function get_title($args, $ctx) {
452        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
453        list($start, $end) = start_end_week($stamp, $ctx->stash('blog'));
454        $format = $args['format'];
455        $format or $format = "%x";
456        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx)
457            . ' - ' .
458            $ctx->_hdlr_date(array('ts' => $end, 'format' => $format), $ctx);
459    }
460
461    function get_range(&$ctx, &$row) {
462        if (is_array($row)) {
463            $week_yr = substr($row[0], 0, 4);
464            $week_wk = substr($row[0], 4);
465            list($y, $m, $d) = week2ymd($week_yr, $week_wk);
466            $period_start = sprintf("%04d%02d%02d000000", $y, $m, $d);
467        } else {
468            $period_start = $row;
469        }
470        return start_end_week($period_start, $ctx->stash('blog'));
471    }
472
473    function &get_archive_list($ctx, $args) {
474        $inside = $ctx->stash('inside_archive_list');
475        if (!isset($inside)) {
476          $inside = false;
477        }
478        $args['inside_archive_list'] = $inside;
479        $args['current_timestamp'] = $ctx->stash('current_timestamp');
480        $args['current_timestamp_end'] = $ctx->stash('current_timestamp_end');
481        return $ctx->mt->db->get_archive_list($args);
482    }
483   
484    function get_archive_link_sql($ctx, $ts, $at, $args) {
485        return '';
486    }
487
488    function template_params(&$ctx) {
489        parent::template_params($ctx);
490        $vars =& $ctx->__stash['vars'];
491        $vars['archive_template'] = 1;
492        $vars['main_template'] = 1;
493        $vars['datebased_only_archive'] = 1;
494        $vars['datebased_weekly_archive'] = 1;
495        $vars['archive_class'] = 'datebased-weekly-archive';
496    }
497}
498
499class AuthorBasedArchiver extends BaseArchiver {
500    // Override Method
501    function get_label($args, $ctx) {
502        return $ctx->mt->translate('Author');
503    }
504    function get_title($args, $ctx) {
505        $author_name = '';
506        $author = $ctx->stash('archive_author');
507        if (!isset($archive_author)) {
508            $author = $ctx->stash('author');
509            if (isset($author)) {
510                $author_name = $author['author_nickname'];
511                $author_name or $author_name =
512                    $ctx->mt->translate('(Display Name not set)');
513            }
514        }
515        return $author_name;
516    }
517   
518    function get_archive_name() {
519        return 'Author';
520    }
521
522    function &get_archive_list($ctx, $args) {
523        global $mt;
524        list($results, $hi, $low) = $this->get_archive_list_data($args);
525        if(is_array($results)) {
526            $blog_id = $args['blog_id'];
527            if (isset($low) && isset($hi)) {
528                $range = "fileinfo_startdate between '$low' and '$hi' and";
529            }
530            $at = $ctx->stash('current_archive_type');
531            $link_cache_sql = "
532                select fileinfo_startdate, fileinfo_url, blog_site_url, blog_file_extension
533                  from mt_fileinfo, mt_templatemap, mt_blog
534                 where  $range
535                   fileinfo_archive_type = '$at'
536                   and blog_id = $blog_id
537                   and fileinfo_blog_id = blog_id
538                   and templatemap_id = fileinfo_templatemap_id
539                   and templatemap_is_preferred = 1
540            ";
541            $cache_results = $mt->db->get_results($link_cache_sql, ARRAY_N);
542            if (is_array($cache_results)) {
543                foreach ($cache_results as $row) {
544                    $date = $ctx->mt->db->db2ts($row[0]);
545                    $blog_url = $row[2];
546                    $blog_url = preg_replace('!(https?://(?:[^/]+))/.*!', '$1', $blog_url);
547                    $url = $blog_url . $row[1];
548                    $url = _strip_index($url, array('blog_file_extension' => $row[3]));
549                    $mt->db->_archive_link_cache[$date.';'.$at] = $url;
550                }
551            }
552        }
553        return $results;
554    }
555
556    function get_archive_link_sql($ctx, $ts, $at, $args) {
557        $blog_id = intval($args['blog_id']);
558        $author = $ctx->stash('author');
559        $auth_id = $author['author_id'];
560        $at or $at = $ctx->stash('current_archive_type');
561        $ts = $ctx->stash('current_timestamp');
562        if ($at == 'Author-Monthly') {
563            $ts = substr($ts, 0, 6) . '01000000';
564        } elseif ($at == 'Author-Daily') {
565            $ts = substr($ts, 0, 8) . '000000';
566        } elseif ($at == 'Author-Weekly') {
567            require_once("MTUtil.php");
568            list($ws, $we) = start_end_week($ts);
569            $ts = $ws;
570        } elseif ($at == 'Author-Yearly') {
571            $ts = substr($ts, 0, 4) . '0101000000';
572        } else {
573            $ts = '';
574        }
575
576        $sql = "select fileinfo_url
577                  from mt_fileinfo, mt_templatemap
578                 where " . ($ts ? "fileinfo_startdate = '$ts' and" : "") .
579                   " fileinfo_blog_id = $blog_id
580                   and fileinfo_archive_type = '".$ctx->mt->db->escape($at)."'
581                   and fileinfo_author_id = '$auth_id'
582                   and templatemap_id = fileinfo_templatemap_id
583                   and templatemap_is_preferred = 1";
584        return $sql;
585    }
586
587    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
588        if ($tag == 'archiveprevious') {
589            require_once('block.mtauthorprevious.php');
590            return smarty_block_mtauthorprevious($args, $content, $ctx, $repeat);
591        } elseif ($tag == 'archivenext') {
592            require_once('block.mtauthornext.php');
593            return smarty_block_mtauthornext($args, $content, $ctx, $repeat);
594        }
595        return $ctx->error("Error in tag: $tag");
596    }
597
598    function prepare_list(&$ctx, &$row) {
599        $author_id = $row[1];
600        $author = $ctx->mt->db->fetch_author($author_id);
601        $ctx->stash('author', $author);
602    }
603
604    function setup_args($ctx, &$args) {
605        if ($auth = $ctx->stash('author')) {
606            $args['author'] = $auth['author_name'];
607        }
608    }
609
610    // Functions
611    function get_archive_list_data($args) {
612        global $mt;
613        $blog_id = $args['blog_id'];
614        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
615        $sql = "
616            select count(*),
617                   entry_author_id
618              from mt_entry
619             where entry_blog_id = $blog_id
620               and entry_status = 2
621             group by
622                   entry_author_id
623             order by
624                   entry_author_id $order
625                   <LIMIT>";
626        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
627        $results = $mt->db->get_results($group_sql, ARRAY_N);
628        return array($results, null, null);;
629    }
630
631    function template_params(&$ctx) {
632        $vars =& $ctx->__stash['vars'];
633        $vars['archive_template'] = 1;
634        $vars['main_template'] = 1;
635        $vars['author_archive'] = 1;
636        $vars['archive_class'] = 'author-archive';
637        $vars['module_author-monthly_archives'] = 1;
638    }
639}
640
641class DateBasedAuthorArchiver extends DateBasedArchiver {
642    function setup_args($ctx, &$args) {
643        if ($auth = $ctx->stash('author')) {
644            $args['author'] = $auth['author_name'];
645        }
646    }
647
648    function get_archive_link_sql($ctx, $ts, $at, $args) {
649        $blog_id = intval($args['blog_id']);
650        $author = $ctx->stash('author');
651        $auth_id = $author['author_id'];
652        $at or $at = $ctx->stash('current_archive_type');
653        $ts = $ctx->stash('current_timestamp');
654        if ($at == 'Author-Monthly') {
655            $ts = substr($ts, 0, 6) . '01000000';
656        } elseif ($at == 'Author-Daily') {
657            $ts = substr($ts, 0, 8) . '000000';
658        } elseif ($at == 'Author-Weekly') {
659            require_once("MTUtil.php");
660            list($ws, $we) = start_end_week($ts);
661            $ts = $ws;
662        } elseif ($at == 'Author-Yearly') {
663            $ts = substr($ts, 0, 4) . '0101000000';
664        } else {
665            $ts = '';
666        }
667
668        $sql = "select fileinfo_url
669                  from mt_fileinfo, mt_templatemap
670                 where " . ($ts ? "fileinfo_startdate = '$ts' and" : "") .
671                   " fileinfo_blog_id = $blog_id
672                   and fileinfo_archive_type = '".$ctx->mt->db->escape($at)."'
673                   and fileinfo_author_id = '$auth_id'
674                   and templatemap_id = fileinfo_templatemap_id
675                   and templatemap_is_preferred = 1";
676        return $sql;
677    }
678
679    function &get_archive_list($ctx, $args) {
680        global $mt;
681        list($results, $hi, $low) = $this->get_archive_list_data($args);
682        if(is_array($results)) {
683            $blog_id = $args['blog_id'];
684            if (isset($low) && isset($hi)) {
685                $range = "fileinfo_startdate between '$low' and '$hi' and";
686            }
687            $at = $ctx->stash('current_archive_type');
688            $link_cache_sql = "
689                select fileinfo_startdate, fileinfo_url, blog_site_url, blog_file_extension
690                  from mt_fileinfo, mt_templatemap, mt_blog
691                 where  $range
692                   fileinfo_archive_type = '$at'
693                   and blog_id = $blog_id
694                   and fileinfo_blog_id = blog_id
695                   and templatemap_id = fileinfo_templatemap_id
696                   and templatemap_is_preferred = 1
697            ";
698            $cache_results = $mt->db->get_results($link_cache_sql, ARRAY_N);
699            if (is_array($cache_results)) {
700                foreach ($cache_results as $row) {
701                    $date = $ctx->mt->db->db2ts($row[0]);
702                    $blog_url = $row[2];
703                    $blog_url = preg_replace('!(https?://(?:[^/]+))/.*!', '$1', $blog_url);
704                    $url = $blog_url . $row[1];
705                    $url = _strip_index($url, array('blog_file_extension' => $row[3]));
706                    $mt->db->_archive_link_cache[$date.';'.$at] = $url;
707                }
708            }
709        }
710        return $results;
711    }
712
713    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
714        $localvars = array('current_timestamp', 'current_timestamp_end', 'entries');
715        if (!isset($content)) {
716            $ctx->localize($localvars);
717            $is_prev = $tag == 'archiveprevious';
718            $ts = $ctx->stash('current_timestamp');
719            $author = $ctx->stash('author');
720            if (!$ts || !$author) {
721                return $ctx->error(
722                   "You used an <mt$tag> without a date context set up.");
723            }
724            $order = $is_prev ? 'previous' : 'next';
725
726            $at = $ctx->stash('current_archive_type');
727            if ($at == 'Author-Monthly') {
728                $wide = 'MONTHLY';
729            } elseif ($at == 'Author-Daily') {
730                $wide = 'DAILY';
731            } elseif ($at == 'Author-Weekly') {
732                $wide = 'WEEKLY';
733            } elseif ($at == 'Author-Yearly') {
734                $wide = 'YEARLY';
735            }
736
737            if ($entry = $this->get_author_entry($ts, $ctx->stash('blog_id'), $author['author_name'], $wide, $order)) {
738                $helper = $this->get_helper($wide);
739                $ctx->stash('entries', array( $entry ));
740                list($start, $end) = $helper($entry['entry_authored_on']);
741                $ctx->stash('current_timestamp', $start);
742                $ctx->stash('current_timestamp_end', $end);
743                $ctx->stash('author', $author);
744            } else {
745                $ctx->restore($localvars);
746                $repeat = false;
747            }
748        } else {
749            $ctx->restore($localvars);
750        }
751        return $content;
752    }
753
754    function get_author_entry($ts, $blog_id, $auth_name, $at, $order) {
755        $helper = $this->get_helper($at);
756        list($start, $end) = $helper($ts);
757        $args = array();
758        if ($order == 'previous') {
759            $args['current_timestamp_end'] = $this->dec_ts($start);
760        } else {
761            $args['current_timestamp'] = $this->inc_ts($end);
762            $args['base_sort_order'] = 'ascend'; # ascending order
763        }
764        $args['lastn'] = 1;
765        $args['blog_id'] = $blog_id;
766        $args['author'] = $auth_name;
767        global $mt;
768        list($entry) = $mt->db->fetch_entries($args);
769        return $entry;
770    }
771
772    function template_params(&$ctx) {
773        parent::template_params($ctx);
774        $vars =& $ctx->__stash['vars'];
775        $vars['archive_template'] = 1;
776        $vars['main_template'] = 1;
777        $vars['author_archive'] = 1;
778    }
779
780    function get_author_name ($ctx) {
781        global $_archivers;
782        $curr_at = $ctx->stash('current_archive_type');
783        $archiver = $_archivers[$curr_at];
784        $auth = $ctx->stash('author');
785        $author_name = '';
786
787        if ($ctx->stash('index_archive')
788            || !isset($archiver)
789            || (isset($archiver) && !isset($auth))
790            || !$ctx->stash('inside_archive_list'))
791        {
792            $author = $ctx->stash('archive_author');
793            $author or $author = $ctx->stash('author');
794            if (isset($author)) {
795                $author_name = $author['author_nickname'];
796                $author_name or $author_name =
797                    'Author (#'.$author['author_id'].')';
798                $author_name .= ': ';
799            }
800        }
801        return $author_name;
802    }
803}
804
805class YearlyAuthorBasedArchiver extends DateBasedAuthorArchiver {
806
807    // Override Method
808    function get_label($args, $ctx) {
809        return $ctx->mt->translate('Author Yearly');
810    }
811    function get_title($args, $ctx) {
812        $author_name = parent::get_author_name($ctx);
813        $stamp = $ctx->stash('current_timestamp');
814        list($start) = start_end_year($stamp, $ctx->stash('blog'));
815        $format = $args['format'];
816        $blog = $ctx->stash('blog');
817        global $mt;
818        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] :
819            $mt->config('DefaultLanguage'));
820            if (strtolower($lang) == 'jp' || strtolower($lang) == 'ja') {
821            $format or $format = "%Y&#24180;";
822        } else {
823            $format or $format = "%Y";
824        }
825
826        return $author_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
827    }
828   
829    function get_archive_name() {
830        return 'Author-Yearly';
831    }
832
833    function get_range(&$ctx, &$row) {
834        if (is_array($row)) {
835            return start_end_year($row[0]);
836        } else {
837            return start_end_year($row);
838        }
839    }
840
841    // Functions
842    function get_archive_list_data($args) {
843        global $mt;
844        $blog_id = $args['blog_id'];
845        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
846        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
847            :  $args['sort_order'] == 'descend' ? 'desc'
848            : '';
849        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
850        $ctx = $mt->context();
851        $index = $ctx->stash('index_archive');
852        #if (!$index) {
853            $author = $ctx->stash('archive_author');
854            $author or $author = $ctx->stash('author');
855            if (isset($author)) {
856                $author_filter = " and entry_author_id=".$author['author_id'];
857            }
858        #}
859
860        $sql = "
861            select count(*),
862                   $year_ext as y,
863                   entry_author_id
864              from mt_entry
865                   join mt_author on entry_author_id = author_id
866             where entry_blog_id = $blog_id
867               and entry_status = 2
868               $author_filter
869             group by
870                   $year_ext,
871                   entry_author_id
872             order by
873                   $year_ext $order
874                   <LIMIT>";
875        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
876        $results = $mt->db->get_results($group_sql, ARRAY_N);
877        if (is_array($results)) {
878            $hi = sprintf("%04d0000000000", $results[0][1]);
879            $low = sprintf("%04d0000000000", $results[count($results)-1][1]);
880        }
881        return array($results, null, null);;
882    }
883
884    function prepare_list(&$ctx, &$row) {
885        $author_id = $row[2];
886        $author = $ctx->mt->db->fetch_author($author_id);
887        $ctx->stash('author', $author);
888    }
889
890    function template_params(&$ctx) {
891        parent::template_params($ctx);
892        $vars =& $ctx->__stash['vars'];
893        $vars['author_yearly_archive'] = 1;
894        $vars['archive_class'] = 'author-yearly-archive';
895    }
896}
897
898class MonthlyAuthorBasedArchiver extends DateBasedAuthorArchiver {
899
900    // Override Method
901    function get_label($args, $ctx) {
902        return $ctx->mt->translate('Author Monthly');
903    }
904    function get_title($args, $ctx) {
905        $author_name = parent::get_author_name($ctx);
906        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
907        list($start) = start_end_month($stamp, $ctx->stash('blog'));
908        $format = $args['format'];
909        $format or $format = "%B %Y";
910        return $author_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
911    }
912   
913    function get_archive_name() {
914        return 'Author-Monthly';
915    }
916
917    function get_range(&$ctx, &$row) {
918        if (is_array($row)) {
919            return start_end_month(sprintf('%04d%02d%02d', $row[0], $row[1], '01'));
920        } else {
921            return start_end_month($row);
922        }
923    }
924
925    // Functions
926    function get_archive_list_data($args) {
927        global $mt;
928        $blog_id = $args['blog_id'];
929        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
930        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
931            :  $args['sort_order'] == 'descend' ? 'desc'
932            : '';
933        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
934        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
935        $ctx = $mt->context(); 
936        $index = $ctx->stash('index_archive');
937
938        #if (!$index) {
939            $author = $ctx->stash('archive_author');
940            $author or $author = $ctx->stash('author');
941            if (isset($author)) {
942                $author_filter = " and entry_author_id=".$author['author_id'];
943            }
944        #}
945        $inside = $ctx->stash('inside_archive_list');
946        if (!isset($inside)) {
947          $inside = false;
948        }
949        if ($inside) {
950            $ts = $ctx->stash('current_timestamp');
951            $tsend = $ctx->stash('current_timestamp_end');
952            if ($ts && $tsend) {
953                $ts = $mt->db->ts2db($ts);
954                $tsend = $mt->db->ts2db($tsend);
955                $date_filter = "and (entry_authored_on between '$ts' and '$tsend')";
956            }
957        }
958
959        $sql = "
960            select count(*),
961                   $year_ext as y,
962                   $month_ext as m,
963                   entry_author_id
964              from mt_entry
965                   join mt_author on entry_author_id = author_id
966             where entry_blog_id = $blog_id
967               and entry_status = 2
968               $date_filter
969               $author_filter
970             group by
971                   $year_ext,
972                   $month_ext,
973                   entry_author_id
974             order by
975                   $year_ext $order,
976                   $month_ext $order
977                   <LIMIT>";
978        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
979        $results = $mt->db->get_results($group_sql, ARRAY_N);
980        if (is_array($results)) {
981            $hi = sprintf("%04d%02d00000000", $results[0][1], $results[0][2]);
982            $low = sprintf("%04d%02d00000000", $results[count($results)-1][1], $results[count($results)-1][2]);
983        }
984        return array($results, null, null);;
985    }
986
987    function prepare_list(&$ctx, &$row) {
988        $author_id = $row[3];
989        $author = $ctx->mt->db->fetch_author($author_id);
990        $ctx->stash('author', $author);
991    }
992
993    function template_params(&$ctx) {
994        parent::template_params($ctx);
995        $vars =& $ctx->__stash['vars'];
996        $vars['author_monthly_archive'] = 1;
997        $vars['archive_class'] = 'author-monthly-archive';
998        $vars['module_author-monthly_archives'] = 1;
999    }
1000}
1001
1002class DailyAuthorBasedArchiver extends DateBasedAuthorArchiver {
1003
1004    // Override Method
1005    function get_label($args, $ctx) {
1006        return $ctx->mt->translate('Author Daily');
1007    }
1008    function get_title($args, $ctx) {
1009        $author_name = parent::get_author_name($ctx);
1010        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1011        list($start) = start_end_day($stamp, $ctx->stash('blog'));
1012        $format = $args['format'];
1013        $format or $format = "%x";
1014        return $author_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1015    }
1016   
1017    function get_archive_name() {
1018        return 'Author-Daily';
1019    }
1020
1021    function get_range(&$ctx, &$row) {
1022        if (is_array($row)) {
1023            $d = sprintf("%04d%02d%02d000000", $row[0], $row[1], $row[2]);
1024            return start_end_day($d);
1025        } else {
1026            return start_end_day($row);
1027        }
1028    }
1029
1030    // Functions
1031    function get_archive_list_data($args) {
1032        global $mt;
1033        $blog_id = $args['blog_id'];
1034        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1035        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
1036            :  $args['sort_order'] == 'descend' ? 'desc'
1037            : '';
1038        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1039        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1040        $day_ext = $mt->db->apply_extract_date('day', 'entry_authored_on');
1041        $ctx = $mt->context();
1042        $index = $ctx->stash('index_archive');
1043        #if (!$index) {
1044            $author = $ctx->stash('archive_author');
1045            $author or $author = $ctx->stash('author');
1046            if (isset($author)) {
1047                $author_filter = " and entry_author_id=".$author['author_id'];
1048            }
1049        #}
1050        $inside = $ctx->stash('inside_archive_list');
1051        if (!isset($inside)) {
1052          $inside = false;
1053        }
1054        if ($inside) {
1055            $ts = $ctx->stash('current_timestamp');
1056            $tsend = $ctx->stash('current_timestamp_end');
1057            if ($ts && $tsend) {
1058                $ts = $mt->db->ts2db($ts);
1059                $tsend = $mt->db->ts2db($tsend);
1060                $date_filter = "and (entry_authored_on between '$ts' and '$tsend')";
1061            }
1062        }
1063
1064        $sql = "
1065            select count(*),
1066                   $year_ext as y,
1067                   $month_ext as m,
1068                   $day_ext as d,
1069                   entry_author_id
1070              from mt_entry
1071                   join mt_author on entry_author_id = author_id
1072             where entry_blog_id = $blog_id
1073               and entry_status = 2
1074               $date_filter
1075               $author_filter
1076             group by
1077                   $year_ext,
1078                   $month_ext,
1079                   $day_ext,
1080                   entry_author_id
1081             order by
1082                   $year_ext $order,
1083                   $month_ext $order,
1084                   $day_ext $order
1085                   <LIMIT>";
1086        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1087        $results = $mt->db->get_results($group_sql, ARRAY_N);
1088        if (is_array($results)) {
1089            $hi = sprintf("%04d%02d%02d000000", $results[0][1], $results[0][2], $results[0][3]);
1090            $low = sprintf("%04d%02d%02d000000", $results[count($results)-1][1], $results[count($results)-1][2], $results[count($results)-1][3]);
1091        }
1092        return array($results, null, null);;
1093    }
1094
1095    function prepare_list(&$ctx, &$row) {
1096        $author_id = $row[4];
1097        $author = $ctx->mt->db->fetch_author($author_id);
1098        $ctx->stash('author', $author);
1099    }
1100
1101    function template_params(&$ctx) {
1102        parent::template_params($ctx);
1103        $vars =& $ctx->__stash['vars'];
1104        $vars['author_daily_archive'] = 1;
1105        $vars['archive_class'] = 'author-daily-archive';
1106    }
1107}
1108
1109class WeeklyAuthorBasedArchiver extends DateBasedAuthorArchiver {
1110
1111    // Override Method
1112    function get_label($args, $ctx) {
1113        return $ctx->mt->translate('Author Weekly');
1114    }
1115    function get_title($args, $ctx) {
1116        $author_name = parent::get_author_name($ctx);
1117        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1118        list($start, $end) = start_end_week($stamp, $ctx->stash('blog'));
1119        $format = $args['format'];
1120        $format or $format = "%x";
1121        return $author_name
1122            .$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx)
1123            .' - '.$ctx->_hdlr_date(array('ts' => $end, 'format' => $format), $ctx);
1124    }
1125   
1126    function get_archive_name() {
1127        return 'Author-Weekly';
1128    }
1129
1130    function get_range(&$ctx, &$row) {
1131        if (is_array($row)) {
1132            $week_yr = substr($row[0], 0, 4);
1133            $week_wk = substr($row[0], 4);
1134            list($y, $m, $d) = week2ymd($week_yr, $week_wk);
1135            return start_end_week(sprintf("%04d%02d%02d000000", $y, $m, $d));
1136        } else {
1137            return start_end_week($row);
1138        }
1139    }
1140
1141    // Functions
1142    function get_archive_list_data($args) {
1143        global $mt;
1144        $blog_id = $args['blog_id'];
1145        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1146        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
1147            :  $args['sort_order'] == 'descend' ? 'desc'
1148            : '';
1149        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1150        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1151        $day_ext = $mt->db->apply_extract_date('day', 'entry_authored_on');
1152        $ctx = $mt->context();
1153        $index = $ctx->stash('index_archive');
1154        #if (!$index) {
1155            $author = $ctx->stash('archive_author');
1156            $author or $author = $ctx->stash('author');
1157            if (isset($author)) {
1158                $author_filter = " and entry_author_id=".$author['author_id'];
1159            }
1160        #}
1161        $inside = $ctx->stash('inside_archive_list');
1162        if (!isset($inside)) {
1163          $inside = false;
1164        }
1165        if ($inside) {
1166            $ts = $ctx->stash('current_timestamp');
1167            $tsend = $ctx->stash('current_timestamp_end');
1168            if ($ts && $tsend) {
1169                $ts = $mt->db->ts2db($ts);
1170                $tsend = $mt->db->ts2db($tsend);
1171                $date_filter = "and (entry_authored_on between '$ts' and '$tsend')";
1172            }
1173        }
1174
1175        $sql = "
1176            select count(*),
1177                   entry_week_number,
1178                   entry_author_id
1179              from mt_entry
1180                   join mt_author on entry_author_id = author_id
1181             where entry_blog_id = $blog_id
1182               and entry_status = 2
1183               $date_filter
1184               $author_filter
1185             group by
1186                   entry_week_number,
1187                   entry_author_id
1188             order by
1189                   entry_week_number $order
1190                   <LIMIT>";
1191        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1192        $results = $mt->db->get_results($group_sql, ARRAY_N);
1193        if (is_array($results)) {
1194            $week_yr = substr($results[0][1], 0, 4);
1195            $week_num = substr($results[0][1], 4);
1196            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1197            $hi = sprintf("%04d%02d%02d000000", $y, $m, $d);
1198            $week_yr = substr($results[count($result)-1][1], 0, 4);
1199            $week_num = substr($results[count($result)-1][1], 4);
1200            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1201            $low = sprintf("%04d%02d%02d000000", $y, $m, $d);
1202        }
1203        return array($results, null, null);;
1204    }
1205
1206    function prepare_list(&$ctx, &$row) {
1207        $author_id = $row[2];
1208        $author = $ctx->mt->db->fetch_author($author_id);
1209        $ctx->stash('author', $author);
1210    }
1211
1212    function template_params(&$ctx) {
1213        parent::template_params($ctx);
1214        $vars =& $ctx->__stash['vars'];
1215        $vars['author_weekly_archive'] = 1;
1216        $vars['archive_class'] = 'author-weekly-archive';
1217    }
1218}
1219
1220class DateBasedCategoryArchiver extends DateBasedArchiver {
1221    // Override method
1222    function &get_archive_list($ctx, $args) {
1223        global $mt;
1224        list($results, $hi, $low) = 
1225            $this->get_archive_list_data($args);
1226        if(is_array($results)) {
1227            $blog_id = $args['blog_id'];
1228            $range = "'$low' and '$hi'";
1229            $at = $ctx->stash('current_archive_type');
1230            $link_cache_sql = "
1231                select fileinfo_startdate, fileinfo_url, blog_site_url, blog_file_extension
1232                  from mt_fileinfo, mt_templatemap, mt_blog
1233                 where fileinfo_startdate between $range
1234                   and fileinfo_archive_type = '$at'
1235                   and blog_id = $blog_id
1236                   and fileinfo_blog_id = blog_id
1237                   and templatemap_id = fileinfo_templatemap_id
1238                   and templatemap_is_preferred = 1
1239            ";
1240            $cache_results = $mt->db->get_results($link_cache_sql, ARRAY_N);
1241            if (is_array($cache_results)) {
1242                foreach ($cache_results as $row) {
1243                    $date = $ctx->mt->db->db2ts($row[0]);
1244                    $blog_url = $row[2];
1245                    $blog_url = preg_replace('!(https?://(?:[^/]+))/.*!', '$1', $blog_url);
1246                    $url = $blog_url . $row[1];
1247                    $url = _strip_index($url, array('blog_file_extension' => $row[3]));
1248                    $mt->db->_archive_link_cache[$date.';'.$at] = $url;
1249                }
1250            }
1251        }
1252        return $results;
1253    }
1254
1255    function get_archive_link_sql($ctx, $ts, $at, $args) {
1256        $blog_id = intval($args['blog_id']);
1257        $blog_id or $blog_id = intval($ctx->stash('blog_id'));
1258        $cat = $ctx->stash('category');
1259        $cat_id = $cat['category_id'];
1260        if (isset($ts)) {
1261            if ($at == 'Category-Monthly') {
1262                $ts = substr($ts, 0, 6) . '01000000';
1263            } elseif ($at == 'Category-Daily') {
1264                $ts = substr($ts, 0, 8) . '000000';
1265            } elseif ($at == 'Category-Weekly') {
1266                require_once("MTUtil.php");
1267                list($ws, $we) = start_end_week($ts);
1268                $ts = $ws;
1269            } elseif ($at == 'Category-Yearly') {
1270                $ts = substr($ts, 0, 4) . '0101000000';
1271            }
1272            $start_filter = "and fileinfo_startdate = '$ts'";
1273        } else {
1274            // find a most oldest link when timestamp was not presented
1275            $order = "order by fileinfo_startdate asc";
1276        }
1277
1278        $sql = "select fileinfo_url
1279                  from mt_fileinfo, mt_templatemap
1280                 where fileinfo_blog_id = $blog_id
1281                   and fileinfo_archive_type = '".$ctx->mt->db->escape($at)."'
1282                   and fileinfo_category_id = '$cat_id'
1283                   and templatemap_id = fileinfo_templatemap_id
1284                   and templatemap_is_preferred = 1
1285                   $start_filter
1286                 $order";
1287        return $sql;
1288    }
1289
1290    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
1291        $localvars = array('current_timestamp', 'current_timestamp_end', 'entries');
1292        if (!isset($content)) {
1293            $ctx->localize($localvars);
1294            $is_prev = $tag == 'archiveprevious';
1295            $ts = $ctx->stash('current_timestamp');
1296            $category = $ctx->stash('category');
1297            if (!$ts || !$category) {
1298                return $ctx->error(
1299                   "You used an <mt$tag> without a date context set up.");
1300            }
1301            $order = $is_prev ? 'previous' : 'next';
1302
1303            $at = $ctx->stash('current_archive_type');
1304            if ($at == 'Category-Monthly') {
1305                $wide = 'MONTHLY';
1306            } elseif ($at == 'Category-Daily') {
1307                $wide = 'DAILY';
1308            } elseif ($at == 'Category-Weekly') {
1309                $wide = 'WEEKLY';
1310            } elseif ($at == 'Category-Yearly') {
1311                $wide = 'YEARLY';
1312            }
1313
1314            if ($entry = $this->get_categorized_entry($ts, $ctx->stash('blog_id'), $category['category_id'], $wide, $order)) {
1315                $helper = $this->get_helper($wide);
1316                $ctx->stash('entries', array( $entry ));
1317                list($start, $end) = $helper($entry['entry_authored_on']);
1318                $ctx->stash('current_timestamp', $start);
1319                $ctx->stash('current_timestamp_end', $end);
1320                $ctx->stash('category', $category);
1321            } else {
1322                $ctx->restore($localvars);
1323                $repeat = false;
1324            }
1325        } else {
1326            $ctx->restore($localvars);
1327        }
1328        return $content;
1329    }
1330
1331    function setup_args($ctx, &$args) {
1332        if ($cat = $ctx->stash('category')) {
1333            $args['category_id'] = $cat['category_id'];
1334        }
1335    }
1336
1337    function get_categorized_entry($ts, $blog_id, $cat_id, $at, $order) {
1338        $helper = $this->get_helper($at);
1339        list($start, $end) = $helper($ts);
1340        $args = array();
1341        if ($order == 'previous') {
1342            $args['current_timestamp_end'] = $this->dec_ts($start);
1343        } else {
1344            $args['current_timestamp'] = $this->inc_ts($end);
1345            $args['base_sort_order'] = 'ascend'; # ascending order
1346        }
1347        $args['lastn'] = 1;
1348        $args['blog_id'] = $blog_id;
1349        $args['category_id'] = $cat_id;
1350        global $mt;
1351        list($entry) = $mt->db->fetch_entries($args);
1352        return $entry;
1353    }
1354
1355    function template_params(&$ctx) {
1356        parent::template_params($ctx);
1357        $vars =& $ctx->__stash['vars'];
1358        $vars['archive_template'] = 1;
1359        $vars['main_template'] = 1;
1360        $vars['category_archive'] = 1;
1361    }
1362
1363    function get_category_name ($ctx) {
1364        global $_archivers;
1365        $curr_at = $ctx->stash('current_archive_type');
1366        $archiver = $_archivers[$curr_at];
1367        $cat = $ctx->stash('category');
1368        $cat_name = '';
1369
1370        if ($ctx->stash('index_archive')
1371            || !isset($archiver)
1372            || (isset($archiver) && !isset($cat))
1373            || !$ctx->stash('inside_archive_list'))
1374        {
1375            $cat = $ctx->stash('archive_category');
1376            $cat or $cat = $ctx->stash('category');
1377            if (isset($cat)) {
1378                $cat_name = $cat['category_label'].": ";
1379            }
1380        }
1381        return $cat_name;
1382    }
1383}
1384
1385class YearlyCategoryArchiver extends DateBasedCategoryArchiver {
1386    // Override method
1387    function get_label($args, $ctx) {
1388        return $ctx->mt->translate('Category Yearly');
1389    }
1390    function get_title($args, $ctx) {
1391        $cat_name = parent::get_category_name($ctx);
1392        $stamp = $ctx->stash('current_timestamp'); 
1393        list($start) = start_end_year($stamp, $ctx->stash('blog'));
1394        $format = $args['format'];
1395        $blog = $ctx->stash('blog');
1396        global $mt;
1397        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] :
1398            $mt->config('DefaultLanguage'));
1399            if (strtolower($lang) == 'jp' || strtolower($lang) == 'ja') {
1400            $format or $format = "%Y&#24180;";
1401        } else {
1402            $format or $format = "%Y";
1403        }
1404        return $cat_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1405    }
1406
1407    function get_range(&$ctx, &$row) {
1408        if (is_array($row)) {
1409            return start_end_year($row[0]);
1410        } else {
1411            return start_end_year($row);
1412        }
1413    }
1414
1415    function get_archive_name() {
1416        return 'Category-Yearly';
1417    }
1418
1419    function get_archive_list_data($args) {
1420        global $mt;
1421        $blog_id = $args['blog_id'];
1422        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1423        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1424            : $args['sort_order'] == 'descend' ? 'desc'
1425            : '';
1426        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1427        $ctx = $mt->context();
1428        $index = $ctx->stash('index_archive');
1429        $inside = $ctx->stash('inside_archive_list');
1430        if (!isset($inside)) {
1431          $inside = false;
1432        }
1433        if ($inside) {
1434            $ts = $ctx->stash('current_timestamp');
1435            $tsend = $ctx->stash('current_timestamp_end');
1436            if ($ts && $tsend) {
1437                $ts = $mt->db->ts2db($ts);
1438                $tsend = $mt->db->ts2db($tsend);
1439                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1440            }
1441        }
1442        #if (!$index) {
1443            $cat = $ctx->stash('archive_category');
1444            $cat or $cat = $ctx->stash('category');
1445            if (isset($cat)){
1446                $cat_filter = " and placement_category_id=".$cat['category_id'];
1447       
1448            }
1449        #}
1450        $sql = "
1451            select count(*),
1452                   $year_ext as y,
1453                   placement_category_id
1454              from mt_entry join mt_placement on entry_id = placement_entry_id
1455                   join mt_category on placement_category_id = category_id
1456             where entry_blog_id = $blog_id
1457               and entry_status = 2
1458               and entry_class = 'entry'
1459               $cat_filter
1460               $date_filter
1461             group by
1462                   $year_ext,
1463                   placement_category_id
1464             order by
1465                   $year_ext $order
1466                   <LIMIT>";
1467        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1468        $results = $mt->db->get_results($group_sql, ARRAY_N);
1469        if (is_array($results)) {
1470            $hi = sprintf("%04d0000000000", $results[0][1]);
1471            $low = sprintf("%04d0000000000", $results[count($results)-1][1]);
1472        }
1473        return array($results, $hi, $low);
1474    }
1475
1476    function prepare_list(&$ctx, &$row) {
1477        $category_id = $row[2];
1478        $category = $ctx->mt->db->fetch_category($category_id);
1479        $ctx->stash('category', $category);
1480    }
1481
1482    function template_params(&$ctx) {
1483        parent::template_params($ctx);
1484        $vars =& $ctx->__stash['vars'];
1485        $vars['category_yearly_archive'] = 1;
1486        $vars['archive_class'] = 'category-yearly-archive';
1487    }
1488}
1489
1490class MonthlyCategoryArchiver extends DateBasedCategoryArchiver {
1491    // Override method
1492    function get_label($args, $ctx) {
1493        return $ctx->mt->translate('Category Monthly');
1494    }
1495    function get_title($args, $ctx) {
1496        $cat_name = parent::get_category_name($ctx);
1497        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1498        list($start) = start_end_month($stamp, $ctx->stash('blog'));
1499        $format = $args['format'];
1500        $format or $format = "%B %Y";
1501        return $cat_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1502    }
1503
1504    function get_range(&$ctx, &$row) {
1505        if (is_array($row)) {
1506            return start_end_month(sprintf('%04d%02d%02d', $row[0], $row[1], "01"));
1507        } else {
1508            return start_end_month($row);
1509        }
1510    }
1511
1512    function get_archive_name() {
1513        return 'Category-Monthly';
1514    }
1515
1516    function get_archive_list_data($args) {
1517        global $mt;
1518        $blog_id = $args['blog_id'];
1519        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1520        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1521            : $args['sort_order'] == 'descend' ? 'desc'
1522            : '';
1523        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1524        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1525        $ctx = $mt->context();
1526        $index = $ctx->stash('index_archive');
1527        $inside = $ctx->stash('inside_archive_list');
1528        if (!isset($inside)) {
1529          $inside = false;
1530        }
1531        if ($inside) {
1532            $ts = $ctx->stash('current_timestamp');
1533            $tsend = $ctx->stash('current_timestamp_end');
1534            if ($ts && $tsend) {
1535                $ts = $mt->db->ts2db($ts);
1536                $tsend = $mt->db->ts2db($tsend);
1537                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1538            }
1539        }
1540        #if (!$index) {
1541            $cat = $ctx->stash('archive_category');
1542            $cat or $cat = $ctx->stash('category');
1543            if(isset($cat)) {
1544                $cat_filter = " and placement_category_id=".$cat['category_id'];
1545       
1546            }
1547        #}
1548        $sql = "
1549            select count(*),
1550                   $year_ext as y,
1551                   $month_ext as m,
1552                   placement_category_id
1553              from mt_entry join mt_placement on entry_id = placement_entry_id
1554                   join mt_category on placement_category_id = category_id
1555             where entry_blog_id = $blog_id
1556               and entry_status = 2
1557               and entry_class = 'entry'
1558               $cat_filter
1559               $date_filter
1560             group by
1561                   $year_ext,
1562                   $month_ext,
1563                   placement_category_id
1564             order by
1565                   $year_ext $order,
1566                   $month_ext $order
1567                   <LIMIT>";
1568        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1569        $results = $mt->db->get_results($group_sql, ARRAY_N);
1570        if (is_array($results)) {
1571            $hi = sprintf("%04d%02d00000000", $results[0][1], $results[0][2]);
1572            $low = sprintf("%04d%02d00000000", $results[count($results)-1][1], $results[count($results)-1][2]);
1573        }
1574        return array($results, $hi, $low);;
1575    }
1576
1577    function prepare_list(&$ctx, &$row) {
1578        $category_id = $row[3];
1579        $category = $ctx->mt->db->fetch_category($category_id);
1580        $ctx->stash('category', $category);
1581    }
1582
1583    function template_params(&$ctx) {
1584        parent::template_params($ctx);
1585        $vars =& $ctx->__stash['vars'];
1586        $vars['archive_class'] = 'category-monthlyl-archive';
1587        $vars['category-monthly_archive'] = 1;
1588        $vars['module_category-monthly_archives'] = 1;
1589    }
1590}
1591
1592class DailyCategoryArchiver extends DateBasedCategoryArchiver {
1593    // Override method
1594    function get_label($args, $ctx) {
1595        return $ctx->mt->translate('Category Daily');
1596    }
1597    function get_title($args, $ctx) {
1598        $cat_name = parent::get_category_name($ctx);
1599        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1600        list($start) = start_end_day($stamp, $ctx->stash('blog'));
1601        $format = $args['format'];
1602        $format or $format = "%x";
1603        return $cat_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1604    }
1605
1606    function get_range(&$ctx, &$row) {
1607        if (is_array($row)) {
1608            return start_end_day(sprintf('%04d%02d%02d', $row[0], $row[1], $row[2]));
1609        } else {
1610            return start_end_day($row);
1611        }
1612    }
1613
1614    function get_archive_name() {
1615        return 'Category-Daily';
1616    }
1617
1618    function get_archive_list_data($args) {
1619        global $mt;
1620        $blog_id = $args['blog_id'];
1621        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1622        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1623            : $args['sort_order'] == 'descend' ? 'desc'
1624            : '';
1625        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1626        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1627        $day_ext = $mt->db->apply_extract_date('day', 'entry_authored_on');
1628        $ctx = $mt->context();
1629        $index = $ctx->stash('index_archive');
1630        $inside = $ctx->stash('inside_archive_list');
1631        if (!isset($inside)) {
1632          $inside = false;
1633        }
1634        if ($inside) {
1635            $ts = $ctx->stash('current_timestamp');
1636            $tsend = $ctx->stash('current_timestamp_end');
1637            if ($ts && $tsend) {
1638                $ts = $mt->db->ts2db($ts);
1639                $tsend = $mt->db->ts2db($tsend);
1640                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1641            }
1642        }
1643        #if (!$index) {
1644            $cat = $ctx->stash('archive_category');
1645            $cat or $cat = $ctx->stash('category');
1646            if(isset($cat)) {
1647                $cat_filter = " and placement_category_id=".$cat['category_id'];
1648       
1649            }
1650        #}
1651        $sql = "
1652            select count(*),
1653                   $year_ext as y,
1654                   $month_ext as m,
1655                   $day_ext as d,
1656                   placement_category_id
1657              from mt_entry join mt_placement on entry_id = placement_entry_id
1658                   join mt_category on placement_category_id = category_id
1659             where entry_blog_id = $blog_id
1660               and entry_status = 2
1661               and entry_class = 'entry'
1662               $cat_filter
1663               $date_filter
1664             group by
1665                   placement_category_id,
1666                   $year_ext,
1667                   $month_ext,
1668                   $day_ext
1669             order by
1670                   $year_ext $order,
1671                   $month_ext $order,
1672                   $day_ext $order
1673                   <LIMIT>";
1674        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1675        $results = $mt->db->get_results($group_sql, ARRAY_N);
1676        if (is_array($results)) {
1677            $hi = sprintf("%04d%02d%02d000000", $results[0][1], $results[0][2], $results[0][3]);
1678            $low = sprintf("%04d%02d%02d000000", $results[count($results)-1][1], $results[count($results)-1][2], $results[count($results)-1][3]);
1679        }
1680        return array($results, $hi, $low);
1681    }
1682
1683    function prepare_list(&$ctx, &$row) {
1684        $category_id = $row[4];
1685        $category = $ctx->mt->db->fetch_category($category_id);
1686        $ctx->stash('category', $category);
1687    }
1688
1689    function template_params(&$ctx) {
1690        parent::template_params($ctx);
1691        $vars =& $ctx->__stash['vars'];
1692        $vars['category_daily_archive'] = 1;
1693        $vars['archive_class'] = 'category-daily-archive';
1694    }
1695}
1696
1697class WeeklyCategoryArchiver extends DateBasedCategoryArchiver {
1698    // Override method
1699    function get_label($args, $ctx) {
1700        return $ctx->mt->translate('Category Weekly');
1701    }
1702    function get_title($args, $ctx) {
1703        $cat_name = parent::get_category_name($ctx);
1704        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1705        list($start, $end) = start_end_week($stamp, $ctx->stash('blog'));
1706        $format = $args['format'];
1707        $format or $format = "%x";
1708        return $cat_name
1709            .$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx)
1710            ." - ".$ctx->_hdlr_date(array('ts' => $end, 'format' => $format), $ctx);
1711    }
1712
1713    function get_range(&$ctx, &$row) {
1714        if (is_array($row)) {
1715            $week_yr = substr($row[0], 0, 4);
1716            $week_wk = substr($row[0], 4);
1717            list($y, $m, $d) = week2ymd($week_yr, $week_wk);
1718            $period_start = sprintf("%04d%02d%02d000000", $y, $m, $d);
1719        } else {
1720            $period_start = $row;
1721        }
1722        return start_end_week($period_start, $ctx->stash('blog'));
1723    }
1724
1725    function get_archive_name() {
1726        return 'Category-Weekly';
1727    }
1728
1729    function get_archive_list_data($args) {
1730        global $mt;
1731        $blog_id = $args['blog_id'];
1732        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1733        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1734            : $args['sort_order'] == 'descend' ? 'desc'
1735            : '';
1736        $ctx = $mt->context();
1737        $index = $ctx->stash('index_archive');
1738        $inside = $ctx->stash('inside_archive_list');
1739        if (!isset($inside)) {
1740          $inside = false;
1741        }
1742        if ($inside) {
1743            $ts = $ctx->stash('current_timestamp');
1744            $tsend = $ctx->stash('current_timestamp_end');
1745            if ($ts && $tsend) {
1746                $ts = $mt->db->ts2db($ts);
1747                $tsend = $mt->db->ts2db($tsend);
1748                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1749            }
1750        }
1751        #if (!$index) {
1752            $cat = $ctx->stash('archive_category');
1753            $cat or $cat = $ctx->stash('category');
1754            if(isset($cat)) {
1755                $cat_filter = " and placement_category_id=".$cat['category_id'];
1756       
1757            }
1758        #}
1759        $sql = "
1760            select count(*),
1761                   entry_week_number,
1762                   placement_category_id
1763              from mt_entry join mt_placement on entry_id = placement_entry_id
1764                   join mt_category on placement_category_id = category_id
1765             where entry_blog_id = $blog_id
1766               and entry_status = 2
1767               and entry_class = 'entry'
1768               $cat_filter
1769               $date_filter
1770             group by
1771                   entry_week_number,
1772                   placement_category_id
1773             order by
1774                   entry_week_number $order
1775                   <LIMIT>";
1776        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1777        $results = $mt->db->get_results($group_sql, ARRAY_N);
1778        if (is_array($results)) {
1779            $week_yr = substr($results[0][1], 0, 4);
1780            $week_num = substr($results[0][1], 4);
1781            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1782            $hi = sprintf("%04d%02d%02d000000", $y, $m, $d);
1783            $week_yr = substr($results[count($result)-1][1], 0, 4);
1784            $week_num = substr($results[count($result)-1][1], 4);
1785            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1786            $low = sprintf("%04d%02d%02d000000", $y, $m, $d);
1787        }
1788        return array($results, $hi, $low);
1789    }
1790
1791    function prepare_list(&$ctx, &$row) {
1792        $category_id = $row[2];
1793        $category = $ctx->mt->db->fetch_category($category_id);
1794        $ctx->stash('category', $category);
1795    }
1796
1797    function template_params(&$ctx) {
1798        parent::template_params($ctx);
1799        $vars =& $ctx->__stash['vars'];
1800        $vars['category_weekly_archive'] = 1;
1801        $vars['archive_class'] = 'category-weekly-archive';
1802    }
1803}
1804?>
Note: See TracBrowser for help on using the browser.