OSDN Git Service

BugTrack/2367 CSS reformat
[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_inline()
19 {
20         global $vars;
21
22         $retval = '';
23         $args = func_get_args();
24         $date = strip_autolink(array_pop($args)); // {date} always exists
25         if($date !== '') {
26                 // Show 'New!' message by the time of the $date string
27                 if (func_num_args() > 2) return '&new([nodate]){date};';
28                 $timestamp = -1;
29                 $dm = null;
30                 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)) {
31                         $iso8601_date = $dm[1]
32                                 . '-' . substr('0' . $dm[2], -2)
33                                 . '-' . substr('0' . $dm[3], -2)
34                                 . ' ' . $dm[4];
35                         $timestamp = strtotime($iso8601_date);
36                 } else {
37                         $timestamp = strtotime($date);
38                 }
39                 if ($timestamp === -1 || $timestamp === FALSE) {
40                         return '&new([nodate]){date}: Invalid date string;';
41                 }
42                 $timestamp -= ZONETIME;
43
44                 $retval = in_array('nodate', $args) ? '' : htmlsc($date);
45         } else {
46                 // Show 'New!' message by the timestamp of the page
47                 if (func_num_args() > 3) return '&new(pagename[,nolink]);';
48
49                 $name = strip_bracket(! empty($args) ? array_shift($args) : $vars['page']);
50                 $page = get_fullname($name, $vars['page']);
51                 $nolink = in_array('nolink', $args);
52
53                 if (substr($page, -1) == '/') {
54                         // Check multiple pages started with "$page"
55                         $timestamp = 0;
56                         $regex = '/^' . preg_quote($page, '/') . '/';
57                         foreach (preg_grep($regex, get_existpages()) as $page) {
58                                 // Get the latest pagename and its timestamp
59                                 $_timestamp = get_filetime($page);
60                                 if ($timestamp < $_timestamp) {
61                                         $timestamp = $_timestamp;
62                                         $retval    = $nolink ? '' : make_pagelink($page);
63                                 }
64                         }
65                         if ($timestamp == 0)
66                                 return '&new(pagename/[,nolink]): No such pages;';
67                 } else {
68                         // Check a page
69                         if (is_page($page)) {
70                                 $timestamp = get_filetime($page);
71                                 $retval    = $nolink ? '' : make_pagelink($page, $name);
72                         } else {
73                                 return '&new(pagename[,nolink]): No such page;';
74                         }
75                 }
76         }
77         if($date !== '') {
78                 // 1 day hot: <span class="new1">New!</span>
79                 // 5 days hot: <span class="new5">New</span>
80                 $retval .= '<span class="__plugin_new" data-mtime="' . get_date_atom($timestamp + LOCALZONE) . '"></span>';
81                 // Show a date string
82                 return sprintf(PLUGIN_NEW_DATE_FORMAT, $retval);
83         } else {
84                 // Show a page name
85                 return $retval;
86         }
87 }