2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: config.php,v 1.2 2004/10/10 02:13:25 henoheno Exp $
8 * ¥×¥é¥°¥¤¥ó¤ÎÀßÄê¤òPukiWiki¤Î¥Ú¡¼¥¸¤Ëµ½Ò¤¹¤ë
11 * $obj = new Config('plugin/¥×¥é¥°¥¤¥ó̾/')
17 * $array = & $obj->get($title);
20 * $array[] = array(4, 5, 6);
22 * // Äɲà - Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
23 * $obj->add($title, array(4, 5, 6));
26 * $array = array(1=>array(1, 2, 3));
28 * // ÃÖ´¹ - Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
29 * $obj->put($title, array(1=>array(1, 2, 3));
32 * $obj->put_values($title, NULL);
39 // ¥Ú¡¼¥¸Ì¾¤Î¥×¥ì¥Õ¥£¥¯¥¹
40 define('CONFIG_BASE', ':config/');
51 function Config($name)
54 $this->page = CONFIG_BASE . $name;
60 if (! is_page($this->page)) return FALSE;
62 $this->objs = array();
63 $obj = & new ConfigTable('');
64 foreach (get_source($this->page) as $line) {
65 if ($line == '') continue;
68 $level = strspn($line, $head);
71 $obj->add_line($line);
76 // ¸«½Ð¤·¤Î¸ÇÍIDÉô¤òºï½ü
77 $line = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/', '$1$2', $line);
80 $this->objs[$obj->title] = $obj;
81 $obj = & new ConfigTable($line);
83 if (! is_a($obj, 'ConfigTable_Direct')) {
84 $obj = & new ConfigTable_Direct('', $obj);
89 } else if ($head == '-' && $level > 1) {
90 if (! is_a($obj, 'ConfigTable_Direct')) {
91 $obj = & new ConfigTable_Direct('', $obj);
93 $obj->add_value($line);
95 } else if ($head == '|' && preg_match('/^\|(.+)\|\s*$/', $line, $matches)) {
96 if (! is_a($obj, 'ConfigTable_Sequential')) {
97 $obj = & new ConfigTable_Sequential('', $obj);
99 $obj->add_value(explode('|', $matches[1]));
101 $obj->add_line($line);
104 $this->objs[$obj->title] = $obj;
110 function & get($title)
112 $obj = & $this->get_object($title);
116 // ÇÛÎó¤òÀßÄꤹ¤ë(¾å½ñ¤)
117 function put($title, $values)
119 $obj = & $this->get_object($title);
120 $obj->values = $values;
124 function add($title, $value)
126 $obj = & $this->get_object($title);
127 $obj->values[] = $value;
130 // ¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤Ê¤¤¤È¤¤Ïºî¤ë)
131 function & get_object($title)
133 if (! isset($this->objs[$title]))
135 $this->objs[$title] = & new ConfigTable('*' . trim($title) . "\n");
137 return $this->objs[$title];
142 page_write($this->page, $this->toString());
148 foreach ($this->objs as $title=>$obj) {
149 $retval .= $obj->toString();
155 //ÇÛÎóÃͤòÊÝ»ý¤¹¤ë¥¯¥é¥¹
161 // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
162 var $before = array();
165 var $values = array();
167 // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
168 var $after = array();
170 function ConfigTable($title, $obj = NULL)
173 $this->title = $obj->title;
174 $this->before = array_merge($obj->before, $obj->after);
176 $this->title = trim(substr($title, strspn($title, '*')));
177 $this->before[] = $title;
182 function add_line($line)
184 $this->after[] = $line;
189 return join('', $this->before) . join('', $this->after);
193 class ConfigTable_Sequential extends ConfigTable
196 function add_value($value)
198 $this->values[] = (count($value) == 1) ? $value[0] : $value;
203 $retval = join('', $this->before);
204 if (is_array($this->values)) {
205 foreach ($this->values as $value) {
206 $value = is_array($value) ? join('|', $value) : $value;
207 $retval .= "|$value|\n";
210 $retval .= join('', $this->after);
215 class ConfigTable_Direct extends ConfigTable
217 // ¼èÆÀ¤·¤¿¥¡¼¤ÎÇÛÎó¡£½é´ü²½»þ¤Ë»ÈÍѤ¹¤ë¡£
218 var $_keys = array();
221 function set_key($line)
223 $level = strspn($line, '*');
224 $this->_keys[$level] = trim(substr($line, $level));
228 function add_value($line)
230 $level = strspn($line, '-');
231 $arr = & $this->values;
232 for ($n = 2; $n <= $level; $n++)
233 $arr = & $arr[$this->_keys[$n]];
234 $arr[] = trim(substr($line, $level));
237 function toString($values = NULL, $level = 2)
240 $root = ($values === NULL);
242 $retval = join('', $this->before);
243 $values = & $this->values;
245 foreach ($values as $key=>$value) {
246 if (is_array($value)) {
247 $retval .= str_repeat('*', $level) . $key . "\n";
248 $retval .= $this->toString($value, $level + 1);
250 $retval .= str_repeat('-', $level - 1) . $value . "\n";
253 if ($root) $retval .= join('', $this->after);