OSDN Git Service

BugTrack/684 Don't show MenuBar right after unfreezing a page
[pukiwiki/pukiwiki.git] / plugin / new.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // new.inc.php
4 // Copyright  2003-2017 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // New! plugin
8 //
9 // Usage:
10 //      &new([nodate]){date};     // Check the date string
11 //      &new(pagename[,nolink]);  // Check the pages's timestamp
12 //      &new(pagename/[,nolink]);
13 //              // Check multiple pages started with 'pagename/',
14 //              // and show the latest one
15
16 define('PLUGIN_NEW_DATE_FORMAT', '<span class="comment_date">%s</span>');
17
18 function plugin_new_init()
19 {
20         // Elapsed time => New! message with CSS
21         $messages['_plugin_new_elapses'] = array(
22                 60 * 60 * 24 * 1 => ' <span class="new1" title="%s">New!</span>',  // 1day
23                 60 * 60 * 24 * 5 => ' <span class="new5" title="%s">New</span>');  // 5days
24         set_plugin_messages($messages);
25 }
26
27 function plugin_new_inline()
28 {
29         global $vars, $_plugin_new_elapses;
30
31         $retval = '';
32         $args = func_get_args();
33         $date = strip_autolink(array_pop($args)); // {date} always exists
34         if($date !== '') {
35                 // Show 'New!' message by the time of the $date string
36                 if (func_num_args() > 2) return '&new([nodate]){date};';
37                 $timestamp = -1;
38                 $dm = null;
39                 if (preg_match('/^\D*(\d{4})\D+(\d{1,2})\D+(\d{1,2})\D+(\d{1,2}:\d{2}:\d{2})\D*$/', $date, $dm)) {
40                         $iso8601_date = $dm[1]
41                                 . '-' . substr('0' . $dm[2], -2)
42                                 . '-' . substr('0' . $dm[3], -2)
43                                 . ' ' . $dm[4];
44                         $timestamp = strtotime($iso8601_date);
45                 } else {
46                         $timestamp = strtotime($date);
47                 }
48                 if ($timestamp === -1 || $timestamp === FALSE) {
49                         return '&new([nodate]){date}: Invalid date string;';
50                 }
51                 $timestamp -= ZONETIME;
52
53                 $retval = in_array('nodate', $args) ? '' : htmlsc($date);
54         } else {
55                 // Show 'New!' message by the timestamp of the page
56                 if (func_num_args() > 3) return '&new(pagename[,nolink]);';
57
58                 $name = strip_bracket(! empty($args) ? array_shift($args) : $vars['page']);
59                 $page = get_fullname($name, $vars['page']);
60                 $nolink = in_array('nolink', $args);
61
62                 if (substr($page, -1) == '/') {
63                         // Check multiple pages started with "$page"
64                         $timestamp = 0;
65                         $regex = '/^' . preg_quote($page, '/') . '/';
66                         foreach (preg_grep($regex, get_existpages()) as $page) {
67                                 // Get the latest pagename and its timestamp
68                                 $_timestamp = get_filetime($page);
69                                 if ($timestamp < $_timestamp) {
70                                         $timestamp = $_timestamp;
71                                         $retval    = $nolink ? '' : make_pagelink($page);
72                                 }
73                         }
74                         if ($timestamp == 0)
75                                 return '&new(pagename/[,nolink]): No such pages;';
76                 } else {
77                         // Check a page
78                         if (is_page($page)) {
79                                 $timestamp = get_filetime($page);
80                                 $retval    = $nolink ? '' : make_pagelink($page, $name);
81                         } else {
82                                 return '&new(pagename[,nolink]): No such page;';
83                         }
84                 }
85         }
86
87         // Add 'New!' string by the elapsed time
88         $erapse = UTIME - $timestamp;
89         foreach ($_plugin_new_elapses as $limit=>$tag) {
90                 if ($erapse <= $limit) {
91                         $retval .= sprintf($tag, get_passage($timestamp));
92                         break;
93                 }
94         }
95
96         if($date !== '') {
97                 // Show a date string
98                 return sprintf(PLUGIN_NEW_DATE_FORMAT, $retval);
99         } else {
100                 // Show a page name
101                 return $retval;
102         }
103 }