OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / plugin / navi.inc.php
1 <?php
2 /**
3  * naviプラグイン
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2008 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: navi.inc.php 1088 2008-10-19 07:42:08Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 /*
17  * Usage:
18  *   #navi(contents-page-name)   <for ALL child pages>
19  *   #navi([contents-page-name][,reverse]) <for contents page>
20  *
21  * Parameter:
22  *   contents-page-name - Page name of home of the navigation (default:itself)
23  *   reverse            - Show contents revese
24  *
25  * Behaviour at contents page:
26  *   Always show child-page list like 'ls' plugin
27  *
28  * Behaviour at child pages:
29  *
30  *   The first plugin call - Show a navigation bar like a DocBook header
31  *
32  *     Prev  <contents-page-name>  Next
33  *     --------------------------------
34  *
35  *   The second call - Show a navigation bar like a DocBook footer
36  *
37  *     --------------------------------
38  *     Prev          Home          Next
39  *     <pagename>     Up     <pagename>
40  *
41  * Page-construction example:
42  *   foobar    - Contents page, includes '#navi' or '#navi(foobar)'
43  *   foobar/1  - One of child pages, includes one or two '#navi(foobar)'
44  *   foobar/2  - One of child pages, includes one or two '#navi(foobar)'
45  */
46
47 // Exclusive regex pattern of child pages
48 define('PLUGIN_NAVI_EXCLUSIVE_REGEX', '');
49 //define('PLUGIN_NAVI_EXCLUSIVE_REGEX', '#/_#'); // Ignore 'foobar/_memo' etc.
50
51 // Insert <link rel=... /> tags into XHTML <head></head>
52 define('PLUGIN_NAVI_LINK_TAGS', FALSE); // FALSE, TRUE
53
54 // ----
55
56 function plugin_navi_convert()
57 {
58         //global $vars, $script, $head_tags;
59         global $script, $head_tags;
60         global $_navi_prev, $_navi_next, $_navi_up, $_navi_home;
61         static $navi = array();
62
63         //$current = $vars['page'];
64         $current = WikiParam::getPage();
65         $reverse = FALSE;
66         if (func_num_args()) {
67                 list($home, $reverse) = array_pad(func_get_args(), 2, '');
68                 // strip_bracket() is not necessary but compatible
69                 $home    = get_fullname(strip_bracket($home), $current);
70                 $is_home = ($home == $current);
71                 if (! is_page($home)) {
72                         return '#navi(contents-page-name): No such page: ' .
73                                 htmlspecialchars($home) . '<br />';
74                 } else if (! $is_home &&
75                     ! preg_match('/^' . preg_quote($home, '/') . '/', $current)) {
76                         return '#navi(' . htmlspecialchars($home) .
77                                 '): Not a child page like: ' .
78                                 htmlspecialchars($home . '/' . basename($current)) .
79                                 '<br />';
80                 }
81                 $reverse = (strtolower($reverse) == 'reverse');
82         } else {
83                 //$home    = $vars['page'];
84                 $home    = WikiParam::getPage();
85                 $is_home = TRUE; // $home == $current
86         }
87
88         $pages  = array();
89         $footer = isset($navi[$home]); // The first time: FALSE, the second: TRUE
90         if (! $footer) {
91                 $navi[$home] = array(
92                         'up'   =>'',
93                         'prev' =>'',
94                         'prev1'=>'',
95                         'next' =>'',
96                         'next1'=>'',
97                         'home' =>'',
98                         'home1'=>'',
99                 );
100
101                 $pages = preg_grep('/^' . preg_quote($home, '/') .
102                         '($|\/)/', get_existpages());
103                 if (PLUGIN_NAVI_EXCLUSIVE_REGEX != '') {
104                         // If old PHP could use preg_grep(,,PREG_GREP_INVERT)...
105                         $pages = array_diff($pages,
106                                 preg_grep(PLUGIN_NAVI_EXCLUSIVE_REGEX, $pages));
107                 }
108                 $pages[] = $current; // Sentinel :)
109                 $pages   = array_unique($pages);
110                 natcasesort($pages);
111                 if ($reverse) $pages = array_reverse($pages);
112
113                 $prev = $home;
114                 foreach ($pages as $page) {
115                         if ($page == $current) break;
116                         $prev = $page;
117                 }
118                 $next = current($pages);
119
120                 $pos = strrpos($current, '/');
121                 $up = '';
122                 if ($pos > 0) {
123                         $up = substr($current, 0, $pos);
124                         $navi[$home]['up']    = make_pagelink($up, $_navi_up);
125                 }
126                 if (! $is_home) {
127                         $navi[$home]['prev']  = make_pagelink($prev);
128                         $navi[$home]['prev1'] = make_pagelink($prev, $_navi_prev);
129                 }
130                 if ($next != '') {
131                         $navi[$home]['next']  = make_pagelink($next);
132                         $navi[$home]['next1'] = make_pagelink($next, $_navi_next);
133                 }
134                 $navi[$home]['home']  = make_pagelink($home);
135                 $navi[$home]['home1'] = make_pagelink($home, $_navi_home);
136
137                 // Generate <link> tag: start next prev(previous) parent(up)
138                 // Not implemented: contents(toc) search first(begin) last(end)
139                 if (PLUGIN_NAVI_LINK_TAGS) {
140                         foreach (array('start'=>$home, 'next'=>$next,
141                             'prev'=>$prev, 'up'=>$up) as $rel=>$_page) {
142                                 if ($_page != '') {
143                                         $s_page = htmlspecialchars($_page);
144                                         $r_page = rawurlencode($_page);
145                                         $head_tags[] = ' <link rel="' .
146                                                 $rel . '" href="' . $script .
147                                                 '?' . $r_page . '" title="' .
148                                                 $s_page . '" />';
149                                 }
150                         }
151                 }
152         }
153
154         $ret = '';
155
156         if ($is_home) {
157                 // Show contents
158                 $count = count($pages);
159                 if ($count == 0) {
160                         return '#navi(contents-page-name): You already view the result<br />';
161                 } else if ($count == 1) {
162                         // Sentinel only: Show usage and warning
163                         $home = htmlspecialchars($home);
164                         $ret .= '#navi(' . $home . '): No child page like: ' .
165                                 $home . '/Foo';
166                 } else {
167                         $ret .= '<ul>';
168                         foreach ($pages as $page)
169                                 if ($page != $home)
170                                         $ret .= ' <li>' . make_pagelink($page) . '</li>';
171                         $ret .= '</ul>';
172                 }
173
174         } else if (! $footer) {
175                 // Header
176                 $ret = <<<EOD
177 <ul class="navi">
178  <li class="navi_left">{$navi[$home]['prev1']}</li>
179  <li class="navi_right">{$navi[$home]['next1']}</li>
180  <li class="navi_none">{$navi[$home]['home']}</li>
181 </ul>
182 <hr class="full_hr" />
183 EOD;
184
185         } else {
186                 // Footer
187                 $ret = <<<EOD
188 <hr class="full_hr" />
189 <ul class="navi">
190  <li class="navi_left">{$navi[$home]['prev1']}<br />{$navi[$home]['prev']}</li>
191  <li class="navi_right">{$navi[$home]['next1']}<br />{$navi[$home]['next']}</li>
192  <li class="navi_none">{$navi[$home]['home1']}<br />{$navi[$home]['up']}</li>
193 </ul>
194 EOD;
195         }
196         return $ret;
197 }
198 ?>