root/branches/athena/php/lib/archive_lib.php @ 1092

Revision 1092, 60.3 kB (checked in by hachi, 2 years ago)

Merging release-15 to athena branch. svn merge -r59987:60375 http://svn.sixapart.com/repos/eng/movabletype/branches/release-15 .

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