OSDN Git Service

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