2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: config.php,v 1.1 2004/08/01 01:54:35 henoheno Exp $
8 * ¥×¥é¥°¥¤¥ó¤ÎÀßÄê¤òPukiWiki¤Î¥Ú¡¼¥¸¤Ëµ½Ò¤¹¤ë
11 * $obj = new Config('plugin/¥×¥é¥°¥¤¥ó̾/')
15 * $array = &$obj->get($title);
17 * $array[] = array(4,5,6);
18 * // Äɲà - Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
19 * $obj->add($title,array(4,5,6));
21 * $array = array(1=>array(1,2,3));
22 * // ÃÖ´¹ - Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
23 * $obj->put($title,array(1=>array(1,2,3));
25 * $obj->put_values($title,NULL);
31 // ¥Ú¡¼¥¸Ì¾¤Î¥×¥ì¥Õ¥£¥¯¥¹
32 define('CONFIG_BASE',':config/');
42 function Config($name)
45 $this->page = CONFIG_BASE.$name;
50 if (!is_page($this->page))
54 $this->objs = array();
55 $obj = &new ConfigTable('');
56 foreach (get_source($this->page) as $line)
64 $level = strspn($line,$head);
68 $obj->add_line($line);
74 // ¸«½Ð¤·¤Î¸ÇÍIDÉô¤òºï½ü
75 $line = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/','$1$2',$line);
79 $this->objs[$obj->title] = $obj;
80 $obj = &new ConfigTable($line);
84 if (!is_a($obj,'ConfigTable_Direct'))
86 $obj = &new ConfigTable_Direct('',$obj);
91 else if ($head == '-' and $level > 1)
93 if (!is_a($obj,'ConfigTable_Direct'))
95 $obj = &new ConfigTable_Direct('',$obj);
97 $obj->add_value($line);
99 else if ($head == '|' and preg_match('/^\|(.+)\|\s*$/',$line,$matches))
101 if (!is_a($obj,'ConfigTable_Sequential'))
103 $obj = &new ConfigTable_Sequential('',$obj);
105 $obj->add_value(explode('|',$matches[1]));
109 $obj->add_line($line);
112 $this->objs[$obj->title] = $obj;
117 function &get($title)
119 $obj = &$this->get_object($title);
122 // ÇÛÎó¤òÀßÄꤹ¤ë(¾å½ñ¤)
123 function put($title,$values)
125 $obj = &$this->get_object($title);
126 $obj->values = $values;
129 function add($title,$value)
131 $obj = &$this->get_object($title);
132 $obj->values[] = $value;
134 // ¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤Ê¤¤¤È¤¤Ïºî¤ë)
135 function &get_object($title)
137 if (!array_key_exists($title,$this->objs))
139 $this->objs[$title] = &new ConfigTable('*'.trim($title)."\n");
141 return $this->objs[$title];
146 page_write($this->page, $this->toString());
152 foreach ($this->objs as $title=>$obj)
154 $retval .= $obj->toString();
159 //ÇÛÎóÃͤòÊÝ»ý¤¹¤ë¥¯¥é¥¹
164 // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
165 var $before = array();
167 var $values = array();
168 // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
169 var $after = array();
171 function ConfigTable($title,$obj=NULL)
175 $this->title = $obj->title;
176 $this->before = array_merge($obj->before,$obj->after);
180 $this->title = trim(substr($title,strspn($title,'*')));
181 $this->before[] = $title;
185 function add_line($line)
187 $this->after[] = $line;
191 return join('',$this->before).join('',$this->after);
194 class ConfigTable_Sequential extends ConfigTable
197 function add_value($value)
199 $this->values[] = (count($value) == 1) ? $value[0] : $value;
204 $retval = join('',$this->before);
205 if (is_array($this->values))
207 foreach ($this->values as $value)
209 $value = is_array($value) ? join('|',$value) : $value;
210 $retval .= "|$value|\n";
213 $retval .= join('',$this->after);
217 class ConfigTable_Direct extends ConfigTable
219 // ¼èÆÀ¤·¤¿¥¡¼¤ÎÇÛÎó¡£½é´ü²½»þ¤Ë»ÈÍѤ¹¤ë¡£
220 var $_keys = array();
223 function set_key($line)
225 $level = strspn($line,'*');
226 $this->_keys[$level] = trim(substr($line,$level));
229 function add_value($line)
231 $level = strspn($line,'-');
232 $arr = &$this->values;
233 for ($n = 2; $n <= $level; $n++)
235 $arr = &$arr[$this->_keys[$n]];
237 $arr[] = trim(substr($line,$level));
240 function toString($values = NULL,$level = 2)
243 if ($root = ($values == NULL))
245 $retval = join('',$this->before);
246 $values = &$this->values;
248 foreach ($values as $key=>$value)
250 if (is_array($value))
252 $retval .= str_repeat('*',$level).$key."\n";
253 $retval .= $this->toString($value,$level + 1);
257 $retval .= str_repeat('-',$level - 1).$value."\n";
262 $retval .= join('',$this->after);