OSDN Git Service

Moved libraries from root to lib/
[pukiwiki/pukiwiki.git] / lib / config.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: config.php,v 1.1 2004/08/01 01:54:35 henoheno Exp $
6 //
7 /*
8  * ¥×¥é¥°¥¤¥ó¤ÎÀßÄê¤òPukiWiki¤Î¥Ú¡¼¥¸¤Ëµ­½Ò¤¹¤ë
9  *
10  * // ¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®
11  * $obj = new Config('plugin/¥×¥é¥°¥¤¥ó̾/')
12  * // Æɤ߽Ф·
13  * $obj->read();
14  * // ÇÛÎó¼èÆÀ
15  * $array = &$obj->get($title);
16  * // Äɲà- Ä¾ÀÜ
17  * $array[] = array(4,5,6);
18  * // Äɲà- Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
19  * $obj->add($title,array(4,5,6));
20  * // ÃÖ´¹ - Ä¾ÀÜ
21  * $array = array(1=>array(1,2,3));
22  * // ÃÖ´¹ - Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
23  * $obj->put($title,array(1=>array(1,2,3));
24  * // ¾Ãµî
25  * $obj->put_values($title,NULL);
26  * // ½ñ¤­¹þ¤ß
27  * $obj->write();
28  *
29  */
30
31 // ¥Ú¡¼¥¸Ì¾¤Î¥×¥ì¥Õ¥£¥¯¥¹
32 define('CONFIG_BASE',':config/');
33
34 // ÀßÄê¥Ú¡¼¥¸´ÉÍý
35 class Config
36 {
37         // ¥Ú¡¼¥¸Ì¾
38         var $name,$page;
39         // Í×ÁÇ
40         var $objs = array();
41
42         function Config($name)
43         {
44                 $this->name = $name;
45                 $this->page = CONFIG_BASE.$name;
46         }
47         // ¥Ú¡¼¥¸¤òÆɤ߹þ¤à
48         function read()
49         {
50                 if (!is_page($this->page))
51                 {
52                         return FALSE;
53                 }
54                 $this->objs = array();
55                 $obj = &new ConfigTable('');
56                 foreach (get_source($this->page) as $line)
57                 {
58                         if ($line == '')
59                         {
60                                 continue;
61                         }
62
63                         $head = $line{0};
64                         $level = strspn($line,$head);
65
66                         if ($level > 3)
67                         {
68                                 $obj->add_line($line);
69                                 continue;
70                         }
71
72                         if ($head == '*')
73                         {
74                                 // ¸«½Ð¤·¤Î¸ÇÍ­IDÉô¤òºï½ü
75                                 $line = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/','$1$2',$line);
76
77                                 if ($level == 1)
78                                 {
79                                         $this->objs[$obj->title] = $obj;
80                                         $obj = &new ConfigTable($line);
81                                 }
82                                 else
83                                 {
84                                         if (!is_a($obj,'ConfigTable_Direct'))
85                                         {
86                                                 $obj = &new ConfigTable_Direct('',$obj);
87                                         }
88                                         $obj->set_key($line);
89                                 }
90                         }
91                         else if ($head == '-' and $level > 1)
92                         {
93                                 if (!is_a($obj,'ConfigTable_Direct'))
94                                 {
95                                         $obj = &new ConfigTable_Direct('',$obj);
96                                 }
97                                 $obj->add_value($line);
98                         }
99                         else if ($head == '|' and preg_match('/^\|(.+)\|\s*$/',$line,$matches))
100                         {
101                                 if (!is_a($obj,'ConfigTable_Sequential'))
102                                 {
103                                         $obj = &new ConfigTable_Sequential('',$obj);
104                                 }
105                                 $obj->add_value(explode('|',$matches[1]));
106                         }
107                         else
108                         {
109                                 $obj->add_line($line);
110                         }
111                 }
112                 $this->objs[$obj->title] = $obj;
113
114                 return TRUE;
115         }
116         // ÇÛÎó¤ò¼èÆÀ¤¹¤ë
117         function &get($title)
118         {
119                 $obj = &$this->get_object($title);
120                 return $obj->values;
121         }
122         // ÇÛÎó¤òÀßÄꤹ¤ë(¾å½ñ¤­)
123         function put($title,$values)
124         {
125                 $obj = &$this->get_object($title);
126                 $obj->values = $values;
127         }
128         // ¹Ô¤òÄɲ乤ë
129         function add($title,$value)
130         {
131                 $obj = &$this->get_object($title);
132                 $obj->values[] = $value;
133         }
134         // ¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤Ê¤¤¤È¤­¤Ïºî¤ë)
135         function &get_object($title)
136         {
137                 if (!array_key_exists($title,$this->objs))
138                 {
139                         $this->objs[$title] = &new ConfigTable('*'.trim($title)."\n");
140                 }
141                 return $this->objs[$title];
142         }
143         // ¥Ú¡¼¥¸¤Ë½ñ¤­¹þ¤à
144         function write()
145         {
146                 page_write($this->page, $this->toString());
147         }
148         // ½ñ¼°²½
149         function toString()
150         {
151                 $retval = '';
152                 foreach ($this->objs as $title=>$obj)
153                 {
154                         $retval .= $obj->toString();
155                 }
156                 return $retval;
157         }
158 }
159 //ÇÛÎóÃͤòÊÝ»ý¤¹¤ë¥¯¥é¥¹
160 class ConfigTable
161 {
162         // ¥Æ¡¼¥Ö¥ë¤Î̾Á°
163         var $title = '';
164         // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
165         var $before = array();
166         // ¼èÆÀ¤·¤¿ÃͤÎÇÛÎó
167         var $values = array();
168         // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
169         var $after = array();
170
171         function ConfigTable($title,$obj=NULL)
172         {
173                 if ($obj !== NULL)
174                 {
175                         $this->title = $obj->title;
176                         $this->before = array_merge($obj->before,$obj->after);
177                 }
178                 else
179                 {
180                         $this->title = trim(substr($title,strspn($title,'*')));
181                         $this->before[] = $title;
182                 }
183         }
184         // ÀâÌÀ¤ÎÄɲÃ
185         function add_line($line)
186         {
187                 $this->after[] = $line;
188         }
189         function toString()
190         {
191                 return join('',$this->before).join('',$this->after);
192         }
193 }
194 class ConfigTable_Sequential extends ConfigTable
195 {
196         // ¹Ô¤ÎÄɲÃ
197         function add_value($value)
198         {
199                 $this->values[] = (count($value) == 1) ? $value[0] : $value;
200         }
201         // ½ñ¼°²½
202         function toString()
203         {
204                 $retval = join('',$this->before);
205                 if (is_array($this->values))
206                 {
207                         foreach ($this->values as $value)
208                         {
209                                 $value = is_array($value) ? join('|',$value) : $value;
210                                 $retval .= "|$value|\n";
211                         }
212                 }
213                 $retval .= join('',$this->after);
214                 return $retval;
215         }
216 }
217 class ConfigTable_Direct extends ConfigTable
218 {
219         // ¼èÆÀ¤·¤¿¥­¡¼¤ÎÇÛÎó¡£½é´ü²½»þ¤Ë»ÈÍѤ¹¤ë¡£
220         var $_keys = array();
221
222         // ¥­¡¼¤ÎÀßÄê
223         function set_key($line)
224         {
225                 $level = strspn($line,'*');
226                 $this->_keys[$level] = trim(substr($line,$level));
227         }
228         // ¹Ô¤ÎÄɲÃ
229         function add_value($line)
230         {
231                 $level = strspn($line,'-');
232                 $arr = &$this->values;
233                 for ($n = 2; $n <= $level; $n++)
234                 {
235                         $arr = &$arr[$this->_keys[$n]];
236                 }
237                 $arr[] = trim(substr($line,$level));
238         }
239         // ½ñ¼°²½
240         function toString($values = NULL,$level = 2)
241         {
242                 $retval = '';
243                 if ($root = ($values == NULL))
244                 {
245                         $retval = join('',$this->before);
246                         $values = &$this->values;
247                 }
248                 foreach ($values as $key=>$value)
249                 {
250                         if (is_array($value))
251                         {
252                                 $retval .= str_repeat('*',$level).$key."\n";
253                                 $retval .= $this->toString($value,$level + 1);
254                         }
255                         else
256                         {
257                                 $retval .= str_repeat('-',$level - 1).$value."\n";
258                         }
259                 }
260                 if ($root)
261                 {
262                         $retval .= join('',$this->after);
263                 }
264                 return $retval;
265         }
266 }
267 ?>