OSDN Git Service

BugTrack/2420 AutoTicketLink - Improve regex and JSON encode
[pukiwiki/pukiwiki.git] / plugin / topicpath.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // topicpath.inc.php
4 // Copyright
5 //   2004-2018 PukiWiki Development Team
6 //   2003      reimy       (Some bug fix)
7 //   2003      t.m         (Migrate to 1.3)
8 //   2003      Nibun-no-ni (Originally written for PukiWiki 1.4.x)
9 // License: GPL v2 or (at your option) any later version
10 //
11 // 'topicpath' plugin for PukiWiki
12
13 // Show a link to $defaultpage or not
14 define('PLUGIN_TOPICPATH_TOP_DISPLAY', 1);
15
16 // Label for $defaultpage
17 define('PLUGIN_TOPICPATH_TOP_LABEL', 'Top');
18
19 // Separetor / of / topic / path
20 define('PLUGIN_TOPICPATH_TOP_SEPARATOR', '<span class="topicpath-slash">/</span>');
21
22 // Show the page itself or not
23 define('PLUGIN_TOPICPATH_THIS_PAGE_DISPLAY', 1);
24
25 // If PLUGIN_TOPICPATH_THIS_PAGE_DISPLAY, add a link to itself
26 define('PLUGIN_TOPICPATH_THIS_PAGE_LINK', 0);
27
28 function plugin_topicpath_convert()
29 {
30         return '<div>' . plugin_topicpath_inline() . '</div>';
31 }
32
33 function plugin_topicpath_parent_links($page)
34 {
35         $parts = explode('/', $page);
36         $parents = array();
37         for ($i = 0, $pos = 0; $pos = strpos($page, '/', $i); $i = $pos + 1) {
38                 $p = substr($page, 0, $pos);
39                 $parents[] = array(
40                         'page' => $p,
41                         'leaf' => substr($p, $i),
42                         'uri' => get_page_uri($p),
43                 );
44         }
45         return $parents;
46 }
47
48 function plugin_topicpath_inline()
49 {
50         global $vars, $defaultpage;
51         $page = isset($vars['page']) ? $vars['page'] : '';
52         if ($page == '' || $page == $defaultpage) return '';
53         $parents = plugin_topicpath_parent_links($page);
54         $topic_path = array();
55         foreach ($parents as $p) {
56                 if (PKWK_READONLY && !is_page($p['page'])) {
57                         // Page not exists
58                         $topic_path[] = htmlsc($p['leaf']);
59                 } else {
60                         // Page exists or not exists
61                         $topic_path[] = '<a href="' . $p['uri'] . '">' .
62                                 $p['leaf'] . '</a>';
63                 }
64         }
65         // This page
66         if (PLUGIN_TOPICPATH_THIS_PAGE_DISPLAY) {
67                 $leaf_name = preg_replace('#^.*/#', '', $page);
68                 if (PLUGIN_TOPICPATH_THIS_PAGE_LINK) {
69                         $topic_path[] = '<a href="' . get_page_uri($page) . '">' .
70                                 $leaf_name . '</a>';
71                 } else {
72                         $topic_path[] = htmlsc($leaf_name);
73                 }
74         }
75         $s = join(PLUGIN_TOPICPATH_TOP_SEPARATOR, $topic_path);
76         if (PLUGIN_TOPICPATH_TOP_DISPLAY) {
77                 $s = '<span class="topicpath-top">' .
78                         make_pagelink($defaultpage, PLUGIN_TOPICPATH_TOP_LABEL) .
79                         PLUGIN_TOPICPATH_TOP_SEPARATOR . '</span>' . $s;
80         }
81         return $s;
82 }