OSDN Git Service

BugTrack2/369 Support PHP7 - Remove '& new Class' notation
[pukiwiki/pukiwiki.git] / lib / config.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: config.php,v 1.6 2005/04/29 11:24:20 henoheno Exp $
4 // Copyright (C) 2003-2005 PukiWiki Developers Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // Parse a PukiWiki page as a configuration page
8
9 /*
10  * $obj = new Config('plugin/plugin_name/')
11  * $obj->read();
12  * $array = & $obj->get($title);
13  * $array[] = array(4, 5, 6);           // Add - directly
14  * $obj->add($title, array(4, 5, 6));   // Add - method of Config object
15  * $array = array(1=>array(1, 2, 3));           // Replace - directly
16  * $obj->put($title, array(1=>array(1, 2, 3));  // Replace - method of Config object
17  * $obj->put_values($title, NULL);      // Delete
18  * $obj->write();
19  */
20
21 // Fixed prefix of configuration-page's name
22 define('PKWK_CONFIG_PREFIX', ':config/');
23
24 // Configuration-page manager
25 class Config
26 {
27         var $name, $page; // Page name
28         var $objs = array();
29
30         function Config($name)
31         {
32                 $this->name = $name;
33                 $this->page = PKWK_CONFIG_PREFIX . $name;
34         }
35
36         // Load the configuration-page
37         function read()
38         {
39                 if (! is_page($this->page)) return FALSE;
40
41                 $this->objs = array();
42                 $obj        = new ConfigTable('');
43                 $matches = array();
44
45                 foreach (get_source($this->page) as $line) {
46                         if ($line == '') continue;
47
48                         $head  = $line{0};      // The first letter
49                         $level = strspn($line, $head);
50
51                         if ($level > 3) {
52                                 $obj->add_line($line);
53
54                         } else if ($head == '*') {
55                                 // Cut fixed-heading anchors
56                                 $line = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/', '$1$2', $line);
57
58                                 if ($level == 1) {
59                                         $this->objs[$obj->title] = $obj;
60                                         $obj = new ConfigTable($line);
61                                 } else {
62                                         if (! is_a($obj, 'ConfigTable_Direct'))
63                                                 $obj = new ConfigTable_Direct('', $obj);
64                                         $obj->set_key($line);
65                                 }
66                                 
67                         } else if ($head == '-' && $level > 1) {
68                                 if (! is_a($obj, 'ConfigTable_Direct'))
69                                         $obj = new ConfigTable_Direct('', $obj);
70                                 $obj->add_value($line);
71
72                         } else if ($head == '|' && preg_match('/^\|(.+)\|\s*$/', $line, $matches)) {
73                                 // Table row
74                                 if (! is_a($obj, 'ConfigTable_Sequential'))
75                                         $obj = new ConfigTable_Sequential('', $obj);
76                                 // Trim() each table cell
77                                 $obj->add_value(array_map('trim', explode('|', $matches[1])));
78                         } else {
79                                 $obj->add_line($line);
80                         }
81                 }
82                 $this->objs[$obj->title] = $obj;
83
84                 return TRUE;
85         }
86
87         // Get an array
88         function & get($title)
89         {
90                 $obj = & $this->get_object($title);
91                 return $obj->values;
92         }
93
94         // Set an array (Override)
95         function put($title, $values)
96         {
97                 $obj         = & $this->get_object($title);
98                 $obj->values = $values;
99         }
100
101         // Add a line
102         function add($title, $value)
103         {
104                 $obj = & $this->get_object($title);
105                 $obj->values[] = $value;
106         }
107
108         // Get an object (or create it)
109         function & get_object($title)
110         {
111                 if (! isset($this->objs[$title]))
112                         $this->objs[$title] = new ConfigTable('*' . trim($title) . "\n");
113                 return $this->objs[$title];
114         }
115
116         function write()
117         {
118                 page_write($this->page, $this->toString());
119         }
120
121         function toString()
122         {
123                 $retval = '';
124                 foreach ($this->objs as $title=>$obj)
125                         $retval .= $obj->toString();
126                 return $retval;
127         }
128 }
129
130 // Class holds array values
131 class ConfigTable
132 {
133         var $title  = '';       // Table title
134         var $before = array();  // Page contents (except table ones)
135         var $after  = array();  // Page contents (except table ones)
136         var $values = array();  // Table contents
137
138         function ConfigTable($title, $obj = NULL)
139         {
140                 if ($obj !== NULL) {
141                         $this->title  = $obj->title;
142                         $this->before = array_merge($obj->before, $obj->after);
143                 } else {
144                         $this->title  = trim(substr($title, strspn($title, '*')));
145                         $this->before[] = $title;
146                 }
147         }
148
149         // Addi an  explanation
150         function add_line($line)
151         {
152                 $this->after[] = $line;
153         }
154
155         function toString()
156         {
157                 return join('', $this->before) . join('', $this->after);
158         }
159 }
160
161 class ConfigTable_Sequential extends ConfigTable
162 {
163         // Add a line
164         function add_value($value)
165         {
166                 $this->values[] = (count($value) == 1) ? $value[0] : $value;
167         }
168
169         function toString()
170         {
171                 $retval = join('', $this->before);
172                 if (is_array($this->values)) {
173                         foreach ($this->values as $value) {
174                                 $value   = is_array($value) ? join('|', $value) : $value;
175                                 $retval .= '|' . $value . '|' . "\n";
176                         }
177                 }
178                 $retval .= join('', $this->after);
179                 return $retval;
180         }
181 }
182
183 class ConfigTable_Direct extends ConfigTable
184 {
185         var $_keys = array();   // Used at initialization phase
186
187         function set_key($line)
188         {
189                 $level = strspn($line, '*');
190                 $this->_keys[$level] = trim(substr($line, $level));
191         }
192
193         // Add a line
194         function add_value($line)
195         {
196                 $level = strspn($line, '-');
197                 $arr   = & $this->values;
198                 for ($n = 2; $n <= $level; $n++)
199                         $arr = & $arr[$this->_keys[$n]];
200                 $arr[] = trim(substr($line, $level));
201         }
202
203         function toString($values = NULL, $level = 2)
204         {
205                 $retval = '';
206                 $root   = ($values === NULL);
207                 if ($root) {
208                         $retval = join('', $this->before);
209                         $values = & $this->values;
210                 }
211                 foreach ($values as $key=>$value) {
212                         if (is_array($value)) {
213                                 $retval .= str_repeat('*', $level) . $key . "\n";
214                                 $retval .= $this->toString($value, $level + 1);
215                         } else {
216                                 $retval .= str_repeat('-', $level - 1) . $value . "\n";
217                         }
218                 }
219                 if ($root) $retval .= join('', $this->after);
220
221                 return $retval;
222         }
223 }
224