root/branches/release-26/php/lib/archive_lib.php @ 1174

Revision 1174, 63.2 kB (checked in by bchoate, 23 months ago)

Updated copyright year for source.

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