OSDN Git Service

BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
[pukiwiki/pukiwiki.git] / plugin / ls2.inc.php
index e4c5829..6c4e01b 100644 (file)
 <?php
-// $Id: ls2.inc.php,v 1.1 2002/12/05 05:02:27 panda Exp $
+// PukiWiki - Yet another WikiWikiWeb clone.
+//
+// $Id: ls2.inc.php,v 1.23 2004/12/05 11:37:37 henoheno Exp $
+//
+// List plugin 2
+
 /*
-Last-Update:2002-10-29 rev.8
-
-*¥×¥é¥°¥¤¥ó ls2
-ÇÛ²¼¤Î¥Ú¡¼¥¸¤Î¸«½Ð¤·(*,**,***)¤Î°ìÍ÷¤òɽ¼¨¤¹¤ë
-
-*Usage
- #ls2(¥Ñ¥¿¡¼¥ó[,¥Ñ¥é¥á¡¼¥¿])
-
-*¥Ñ¥é¥á¡¼¥¿
--¥Ñ¥¿¡¼¥ó(ºÇ½é¤Ë»ØÄê)~
-¾Êά¤¹¤ë¤È¤­¤â¥«¥ó¥Þ¤¬É¬Í×
--title~
- ¸«½Ð¤·¤Î°ìÍ÷¤òɽ¼¨¤¹¤ë
--include~
- ¥¤¥ó¥¯¥ë¡¼¥É¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤Î¸«½Ð¤·¤òºÆµ¢Åª¤ËÎóµó¤¹¤ë
--link~
- action¥×¥é¥°¥¤¥ó¤ò¸Æ¤Ó½Ð¤¹¥ê¥ó¥¯¤òɽ¼¨
--reverse~
- ¥Ú¡¼¥¸¤Îʤӽç¤òȿž¤·¡¢¹ß½ç¤Ë¤¹¤ë
-
-*/
-
-//¸«½Ð¤·¥¢¥ó¥«¡¼¤Î½ñ¼°
-//define('LS2_CONTENT_HEAD','#content:'); // html.php 1.35°ÊÁ°
-define('LS2_CONTENT_HEAD','#content_1_'); // html.php 1.36°Ê¹ß
-
-//¸«½Ð¤·¥¢¥ó¥«¡¼¤Î³«»ÏÈÖ¹æ
-//define('LS2_ANCHOR_ORIGIN',''); // html.php 1.35°ÊÁ°
-define('LS2_ANCHOR_ORIGIN',0); // html.php 1.36°Ê¹ß
-
-function plugin_ls2_init() {
-       global $_ls2_anchor;
-       if (!isset($_ls2_anchor)) { $_ls2_anchor = 0; }
-       $messages = array('_ls2_messages'=>array(
-               'err_nopages' => '<p>\'$1\' ¤Ë¤Ï¡¢²¼°ÌÁؤΥڡ¼¥¸¤¬¤¢¤ê¤Þ¤»¤ó¡£</p>',
-               'msg_title' => '\'$1\'¤Ç»Ï¤Þ¤ë¥Ú¡¼¥¸¤Î°ìÍ÷',
-               'msg_go' => '<span class="small">...</span>',
-       ));
-  set_plugin_messages($messages);
-}
-function plugin_ls2_action() {
-       global $vars;
-       global $_ls2_messages;
+ * ÇÛ²¼¤Î¥Ú¡¼¥¸¤ä¡¢¤½¤Î¸«½Ð¤·(*,**,***)¤Î°ìÍ÷¤òɽ¼¨¤¹¤ë
+ * Usage
+ *  #ls2(pattern[,title|include|link|reverse|compact, ...],heading title)
+ *
+ * pattern  : ¾Êά¤¹¤ë¤È¤­¤â¥«¥ó¥Þ¤¬É¬Í×
+ * 'title'  : ¸«½Ð¤·¤Î°ìÍ÷¤òɽ¼¨¤¹¤ë
+ * 'include': ¥¤¥ó¥¯¥ë¡¼¥É¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤Î¸«½Ð¤·¤òºÆµ¢Åª¤ËÎóµó¤¹¤ë
+ * 'link   ': action¥×¥é¥°¥¤¥ó¤ò¸Æ¤Ó½Ð¤¹¥ê¥ó¥¯¤òɽ¼¨
+ * 'reverse': ¥Ú¡¼¥¸¤Îʤӽç¤òȿž¤·¡¢¹ß½ç¤Ë¤¹¤ë
+ * 'compact': ¸«½Ð¤·¥ì¥Ù¥ë¤òÄ´À°¤¹¤ë
+ *     PLUGIN_LS2_LIST_COMPACT¤¬TRUE¤Î»þ¤Ï̵¸ú(ÊѲ½¤·¤Ê¤¤)
+ * heading title: ¸«½Ð¤·¤Î¥¿¥¤¥È¥ë¤ò»ØÄꤹ¤ë (link¤ò»ØÄꤷ¤¿»þ¤Î¤ß)
+ */
+
+// ¸«½Ð¤·¥¢¥ó¥«¡¼¤Î½ñ¼°
+define('PLUGIN_LS2_ANCHOR_PREFIX', '#content_1_');
+
+// ¸«½Ð¤·¥¢¥ó¥«¡¼¤Î³«»ÏÈÖ¹æ
+define('PLUGIN_LS2_ANCHOR_ORIGIN', 0);
+
+// ¸«½Ð¤·¥ì¥Ù¥ë¤òÄ´À°¤¹¤ë(¥Ç¥Õ¥©¥ë¥ÈÃÍ)
+define('PLUGIN_LS2_LIST_COMPACT', FALSE);
+
+function plugin_ls2_action()
+{
+       global $vars, $_ls2_msg_title;
 
        $params = array();
-       foreach (array('title','include','reverse') as $key)
-               $params[$key] = array_key_exists($key,$vars);
-       $prefix = array_key_exists('prefix',$vars) ? $vars['prefix'] : '';
-       $body = ls2_show_lists($prefix,$params);
-
-       return array(
-               'body'=>$body,
-               'msg'=>str_replace('$1',htmlspecialchars($prefix),$_ls2_messages['msg_title'])
-       );
+       foreach (array('title', 'include', 'reverse') as $key)
+               $params[$key] = isset($vars[$key]);
+
+       $prefix = isset($vars['prefix']) ? $vars['prefix'] : '';
+       $body = plugin_ls2_show_lists($prefix, $params);
+
+       return array('body'=>$body,
+               'msg'=>str_replace('$1', htmlspecialchars($prefix), $_ls2_msg_title));
 }
 
-function plugin_ls2_convert() {
-       global $script,$vars;
-       global $_ls2_messages;
+function plugin_ls2_convert()
+{
+       global $script, $vars, $_ls2_msg_title;
+
+       $params = array(
+               'link'    => FALSE,
+               'title'   => FALSE,
+               'include' => FALSE,
+               'reverse' => FALSE,
+               'compact' => PLUGIN_LS2_LIST_COMPACT,
+               '_args'   => array(),
+               '_done'   => FALSE
+       );
 
+       $args = array();
        $prefix = '';
        if (func_num_args()) {
-               $args = func_get_args();
+               $args   = func_get_args();
                $prefix = array_shift($args);
+       }
+       if ($prefix == '') $prefix = strip_bracket($vars['page']) . '/';
+
+       array_walk($args, 'plugin_ls2_check_arg', & $params);
+
+       $title = (! empty($params['_args'])) ? join(',', $params['_args']) :   // Manual
+               str_replace('$1', htmlspecialchars($prefix), $_ls2_msg_title); // Auto
+
+       if (! $params['link'])
+               return plugin_ls2_show_lists($prefix, $params);
+
+       $tmp = array();
+       $tmp[] = 'plugin=ls2&amp;prefix=' . rawurlencode($prefix);
+       if (isset($params['title']))   $tmp[] = 'title=1';
+       if (isset($params['include'])) $tmp[] = 'include=1';
+
+       return '<p><a href="' . $script . '?' . join('&amp;', $tmp) . '">' .
+               $title . '</a></p>' . "\n";
+}
+
+function plugin_ls2_show_lists($prefix, & $params)
+{
+       global $_ls2_err_nopages;
+
+       $pages = array();
+       if ($prefix != '') {
+               foreach (get_existpages() as $_page)
+                       if (strpos($_page, $prefix) === 0)
+                               $pages[] = $_page;
        } else {
-               $args = array();
+               $pages = get_existpages();
        }
-       if ($prefix == '')
-               $prefix = strip_bracket($vars['page']).'/';
-
-       $params = array('link'=>FALSE,'title'=>FALSE,'include'=>FALSE,'reverse'=>FALSE,'_args'=>array(),'_done'=>FALSE);
-       array_walk($args, 'ls2_check_arg', &$params);
-       $title = (count($params['_args']) > 0) ?
-               join(',', $params['_args']) :
-               str_replace('$1',htmlspecialchars($prefix),$_ls2_messages['msg_title']);
-
-       if ($params['link']) {
-               $tmp = array();
-               $tmp[] = 'plugin=ls2&amp;prefix='.$prefix;
-               if (isset($params['title'])) { $tmp[] = 'title=1'; }
-               if (isset($params['include'])) { $tmp[] = 'include=1'; }
-               return '<p><a href="'.$script.'?'.join('&amp;',$tmp).'">'.$title.'</a></p>'."\n";
+
+       natcasesort($pages);
+       if ($params['reverse']) $pages = array_reverse($pages);
+
+       foreach ($pages as $page) $params["page_$page"] = 0;
+
+       if (empty($pages)) {
+               return str_replace('$1', htmlspecialchars($prefix), $_ls2_err_nopages);
+       } else {
+               $params['result'] = $params['saved'] = array();
+               foreach ($pages as $page)
+                       plugin_ls2_get_headings($page, $params, 1);
+               return join("\n", $params['result']) . join("\n", $params['saved']);
        }
-       return ls2_show_lists($prefix,$params);
 }
-function ls2_show_lists($prefix,&$params) {
-       global $_ls2_messages;
 
-       $pages = ls2_get_child_pages($prefix);
-       if ($params['reverse']) $pages = array_reverse($pages);
+function plugin_ls2_get_headings($page, & $params, $level, $include = FALSE)
+{
+       global $script;
+       static $_ls2_anchor = 0;
 
-       foreach ($pages as $page) { $params[$page] = 0; }
+       // ¥Ú¡¼¥¸¤¬Ì¤É½¼¨¤Î¤È¤­
+       $is_done = (isset($params["page_$page"]) && $params["page_$page"] > 0);
+       if (! $is_done) $params["page_$page"] = ++$_ls2_anchor;
 
-       if (count($pages) == 0) { return str_replace('$1',htmlspecialchars($prefix),$_ls2_messages['err_nopages']); }
+       $r_page = rawurlencode($page);
+       $s_page = htmlspecialchars($page);
+       $title  = $s_page . ' ' . get_pg_passage($page, FALSE);
+       $href   = $script . '?cmd=read&amp;page=' . $r_page;
 
-       $ret = '<ul>';
-       foreach ($pages as $page) { $ret .= ls2_show_headings($page,$params); }
-       $ret .= '</ul>'."\n";
-       return $ret;
-}
+       plugin_ls2_list_push($params, $level);
+       $ret = $include ? '<li>include ' : '<li>';
 
-function ls2_show_headings($page,&$params,$include = FALSE) {
-       global $script, $user_rules;
-       global $_ls2_anchor, $_ls2_messages;
-       
-       $ret = '';
-       $rules = '/\(\(((?:(?!\)\)).)*)\)\)/';
-       $is_done = (isset($params[$page]) and $params[$page] > 0); //¥Ú¡¼¥¸¤¬É½¼¨ºÑ¤ß¤Î¤È¤­True
-       if (!$is_done) { $params[$page] = ++$_ls2_anchor; }
-
-       $name = strip_bracket($page);
-       $title = $name.' '.get_pg_passage($page,FALSE);
-       $href = $script.'?cmd=read&amp;page='.rawurlencode($page);
-       $ret .= '<li>';
-       if ($include) { $ret .= 'include '; }
-       $ret .= '<a id="list_'.$params[$page].'" href="'.$href.'" title="'.$title.'">'.$name.'</a>';
-       if ($params['title'] and $is_done) {
-               $ret .= '<a href="#list_'.$params[$page].'">+</a></li>'."\n";
-               return $ret;
+       if ($params['title'] && $is_done) {
+               $ret .= '<a href="' . $href . '" title="' . $title . '">' . $s_page . '</a> ';
+               $ret .= '<a href="#list_' . $params["page_$page"] . '"><sup>&uarr;</sup></a>';
+               array_push($params['result'], $ret);
+               return;
        }
-       $anchor = LS2_ANCHOR_ORIGIN;
-       $_ret = '';
+
+       $ret .= '<a id="list_' . $params["page_$page"] . '" href="' . $href .
+               '" title="' . $title . '">' . $s_page . '</a>';
+       array_push($params['result'], $ret);
+
+       $anchor = PLUGIN_LS2_ANCHOR_ORIGIN;
+       $matches = array();
        foreach (get_source($page) as $line) {
-               if ($params['title'] and preg_match('/^(\*+)(.*)$/',$line,$matches)) {
-                       list($special) = inline2(array(preg_replace($rules,'',htmlspecialchars($matches[2]))));
-                       $left = (strlen($matches[1]) - 1) * 16;
-                       $_ret .= '<li style="margin-left:'.$left.'px">'.$special.
-                               '<a href="'.$href.LS2_CONTENT_HEAD.$anchor.'">'.$_ls2_messages['msg_go'].'</a></li>'."\n";
-                       $anchor++;
+               if ($params['title'] && preg_match('/^(\*{1,3})/', $line, $matches)) {
+                       $id    = make_heading($line);
+                       $level = strlen($matches[1]);
+                       $id    = PLUGIN_LS2_ANCHOR_PREFIX . $anchor++;
+                       plugin_ls2_list_push($params, $level + strlen($level));
+                       array_push($params['result'],
+                               '<li><a href="' . $href . $id . '">' . $line . '</a>');
+               } else if ($params['include'] &&
+                       preg_match('/^#include\((.+)\)/', $line, $matches) &&
+                       is_page($matches[1]))
+               {
+                       plugin_ls2_get_headings($matches[1], $params, $level + 1, TRUE);
                }
-               else if ($params['include'] and preg_match('/^#include\((.+)\)/',$line,$matches) and is_page($matches[1]))
-                       $_ret .= ls2_show_headings($matches[1],$params,TRUE);
        }
-       if ($_ret != '') { $ret .= "<ul>$_ret</ul>\n"; }
-       $ret .= '</li>'."\n";
-       return $ret;
 }
-function ls2_get_child_pages($prefix) {
-       global $vars;
-       
-       $pattern = '[['.$prefix;
-       
-       $pages = array();
-       foreach (get_existpages() as $_page)
-               if (strpos($_page,$pattern) === 0)
-                       $pages[$_page] = strip_bracket($_page);
-       natcasesort($pages);
 
-       return array_keys($pages);
+//¥ê¥¹¥È¹½Â¤¤ò¹½ÃÛ¤¹¤ë
+function plugin_ls2_list_push(& $params, $level)
+{
+       global $_ul_left_margin, $_ul_margin, $_list_pad_str;
+
+       $result = & $params['result'];
+       $saved  = & $params['saved'];
+       $cont   = TRUE;
+       $open   = '<ul%s>';
+       $close  = '</li></ul>';
+
+       while (count($saved) > $level || (! empty($saved) && $saved[0] != $close))
+               array_push($result, array_shift($saved));
+
+       $margin = $level - count($saved);
+
+       // count($saved)¤òÁý¤ä¤¹
+       while (count($saved) < ($level - 1)) array_unshift($saved, '');
+
+       if (count($saved) < $level) {
+               $cont = FALSE;
+               array_unshift($saved, $close);
+
+               $left = ($level == $margin) ? $_ul_left_margin : 0;
+               if ($params['compact']) {
+                       $left  += $_ul_margin;   // ¥Þ¡¼¥¸¥ó¤ò¸ÇÄê
+                       $level -= ($margin - 1); // ¥ì¥Ù¥ë¤ò½¤Àµ
+               } else {
+                       $left += $margin * $_ul_margin;
+               }
+               $str = sprintf($_list_pad_str, $level, $left, $left);
+               array_push($result, sprintf($open, $str));
+       }
+
+       if ($cont) array_push($result, '</li>');
 }
-//¥ª¥×¥·¥ç¥ó¤ò²òÀϤ¹¤ë
-function ls2_check_arg($val, $key, &$params) {
-       if ($val == '') { $params['_done'] = TRUE; return; }
-       if (!$params['_done']) {
-               foreach (array_keys($params) as $key) {
-                       if (strpos($key, strtolower($val)) === 0) {
-                               $params[$key] = TRUE;
+
+// ¥ª¥×¥·¥ç¥ó¤ò²òÀϤ¹¤ë
+function plugin_ls2_check_arg($value, $key, & $params)
+{
+       if ($value == '') {
+               $params['_done'] = TRUE;
+               return;
+       }
+
+       if (! $params['_done']) {
+               foreach (array_keys($params) as $param) {
+                       if (strtolower($value)  == $param &&
+                           preg_match('/^[a-z]/', $param)) {
+                               $params[$param] = TRUE;
                                return;
                        }
                }
                $params['_done'] = TRUE;
        }
-       $params['_args'][] = $val;
-}
 
+       $params['_args'][] = htmlspecialchars($value); // Link title
+}
 ?>