OSDN Git Service

1.4.5_1
[pukiwiki/pukiwiki.git] / plugin / navi.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: navi.inc.php,v 1.20 2004/12/30 13:26:43 henoheno Exp $
4 //
5 // Navi plugin: Show DocBook-like navigation bar and contents
6
7 /*
8  * Usage:
9  *   #navi(contents-page-name)   <for ALL child pages>
10  *   #navi([contents-page-name]) <for contents page>
11  *
12  * Parameter:
13  *   contents-page-name - Page name of home of the navigation (default:itself)
14  *
15  * Behaviour at contents page:
16  *   Always show child-page list like 'ls' plugin
17  *
18  * Behaviour at child pages:
19  *
20  *   The first plugin call - Show a navigation bar like a DocBook header
21  *
22  *     Prev  <contents-page-name>  Next
23  *     --------------------------------
24  *
25  *   The second call - Show a navigation bar like a DocBook footer
26  *
27  *     --------------------------------
28  *     Prev          Home          Next
29  *     <pagename>     Up     <pagename>
30  *
31  * Page-construction example:
32  *   foobar    - Contents page, includes '#navi' or '#navi(foobar)'
33  *   foobar/1  - One of child pages, includes one or two '#navi(foobar)'
34  *   foobar/2  - One of child pages, includes one or two '#navi(foobar)'
35  */
36
37 // Exclusive regex pattern of child pages
38 define('PLUGIN_NAVI_EXCLUSIVE_REGEX', '');
39 //define('PLUGIN_NAVI_EXCLUSIVE_REGEX', '#/_#'); // Ignore 'foobar/_memo' etc.
40
41 // Insert <link rel=... /> tags into XHTML <head></head>
42 define('PLUGIN_NAVI_LINK_TAGS', FALSE); // FALSE, TRUE
43
44 // ----
45
46 function plugin_navi_convert()
47 {
48         global $vars, $script, $head_tags;
49         global $_navi_prev, $_navi_next, $_navi_up, $_navi_home;
50         static $navi = array();
51
52         $current = $vars['page'];
53         if (func_num_args()) {
54                 list($home) = func_get_args();
55                 // strip_bracket() is not necessary but compatible
56                 $home    = get_fullname(strip_bracket($home), $current);
57                 $is_home = ($home == $current);
58                 if (! is_page($home)) {
59                         return '#navi(contents-page-name): No such page: ' .
60                                 htmlspecialchars($home) . '<br/>';
61                 } else if (! $is_home &&
62                     ! preg_match('|^' . preg_quote($home, '|') . '|', $current)) {
63                         return '#navi(' . htmlspecialchars($home) .
64                                 '): Not a child page like: ' .
65                                 htmlspecialchars($home . '/' . basename($current)) .
66                                 '<br/>';
67                 }
68         } else {
69                 $home    = $vars['page'];
70                 $is_home = TRUE; // $home == $current
71         }
72
73         $pages  = array();
74         $footer = isset($navi[$home]); // The first time: FALSE, the second: TRUE
75         if (! $footer) {
76                 $navi[$home] = array(
77                         'up'   =>'',
78                         'prev' =>'',
79                         'prev1'=>'',
80                         'next' =>'',
81                         'next1'=>'',
82                         'home' =>'',
83                         'home1'=>'',
84                 );
85
86                 $pages = preg_grep('/^' . preg_quote($home, '/') .
87                         '($|\/)/', get_existpages());
88                 if (PLUGIN_NAVI_EXCLUSIVE_REGEX != '') {
89                         // If old PHP could use preg_grep(,,PREG_GREP_INVERT)...
90                         $pages = array_diff($pages,
91                                 preg_grep(PLUGIN_NAVI_EXCLUSIVE_REGEX, $pages));
92                 }
93                 $pages[] = $current; // Sentinel :)
94                 $pages   = array_unique($pages);
95                 natcasesort($pages);
96                 $prev = $home;
97                 foreach ($pages as $page) {
98                         if ($page == $current) break;
99                         $prev = $page;
100                 }
101                 $next = current($pages);
102
103                 $pos = strrpos($current, '/');
104                 $up = '';
105                 if ($pos > 0) {
106                         $up = substr($current, 0, $pos);
107                         $navi[$home]['up']    = make_pagelink($up, $_navi_up);
108                 }
109                 if (! $is_home) {
110                         $navi[$home]['prev']  = make_pagelink($prev);
111                         $navi[$home]['prev1'] = make_pagelink($prev, $_navi_prev);
112                 }
113                 if ($next != '') {
114                         $navi[$home]['next']  = make_pagelink($next);
115                         $navi[$home]['next1'] = make_pagelink($next, $_navi_next);
116                 }
117                 $navi[$home]['home']  = make_pagelink($home);
118                 $navi[$home]['home1'] = make_pagelink($home, $_navi_home);
119
120                 // Generate <link> tag: start next prev(previous) parent(up)
121                 // Not implemented: contents(toc) search first(begin) last(end)
122                 if (PLUGIN_NAVI_LINK_TAGS) {
123                         foreach (array('start'=>$home, 'next'=>$next,
124                             'prev'=>$prev, 'up'=>$up) as $rel=>$_page) {
125                                 if ($_page != '') {
126                                         $s_page = htmlspecialchars($_page);
127                                         $r_page = rawurlencode($_page);
128                                         $head_tags[] = ' <link rel="' .
129                                                 $rel . '" href="' . $script .
130                                                 '?' . $r_page . '" title="' .
131                                                 $s_page . '" />';
132                                 }
133                         }
134                 }
135         }
136
137         $ret = '';
138
139         if ($is_home) {
140                 // Contents
141                 $count = count($pages);
142                 if ($count == 0) {
143                         return '#navi(contents-page-name): You already view the result<br/>';
144                 } else if ($count == 1) {
145                         // Sentinel only: Show usage and warning
146                         $home = htmlspecialchars($home);
147                         $ret .= '#navi(' . $home . '): No child page like: ' .
148                                 $home . '/Foo';
149                 } else {
150                         $ret .= '<ul>';
151                         foreach ($pages as $page)
152                                 if ($page != $home)
153                                         $ret .= ' <li>' . make_pagelink($page) . '</li>';
154                         $ret .= '</ul>';
155                 }
156
157         } else if (! $footer) {
158                 // Header
159                 $ret = <<<EOD
160 <ul class="navi">
161  <li class="navi_left">{$navi[$home]['prev1']}</li>
162  <li class="navi_right">{$navi[$home]['next1']}</li>
163  <li class="navi_none">{$navi[$home]['home']}</li>
164 </ul>
165 <hr class="full_hr" />
166 EOD;
167
168         } else {
169                 // Footer
170                 $ret = <<<EOD
171 <hr class="full_hr" />
172 <ul class="navi">
173  <li class="navi_left">{$navi[$home]['prev1']}<br />{$navi[$home]['prev']}</li>
174  <li class="navi_right">{$navi[$home]['next1']}<br />{$navi[$home]['next']}</li>
175  <li class="navi_none">{$navi[$home]['home1']}<br />{$navi[$home]['up']}</li>
176 </ul>
177 EOD;
178         }
179         return $ret;
180 }
181 ?>