OSDN Git Service

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