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
7 // Parse a PukiWiki page as a configuration page
10 * $obj = new Config('plugin/plugin_name/')
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
21 // Fixed prefix of configuration-page's name
22 define('PKWK_CONFIG_PREFIX', ':config/');
24 // Configuration-page manager
27 var $name, $page; // Page name
30 function Config($name)
33 $this->page = PKWK_CONFIG_PREFIX . $name;
36 // Load the configuration-page
39 if (! is_page($this->page)) return FALSE;
41 $this->objs = array();
42 $obj = new ConfigTable('');
45 foreach (get_source($this->page) as $line) {
46 if ($line == '') continue;
48 $head = $line{0}; // The first letter
49 $level = strspn($line, $head);
52 $obj->add_line($line);
54 } else if ($head == '*') {
55 // Cut fixed-heading anchors
56 $line = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/', '$1$2', $line);
59 $this->objs[$obj->title] = $obj;
60 $obj = new ConfigTable($line);
62 if (! is_a($obj, 'ConfigTable_Direct'))
63 $obj = new ConfigTable_Direct('', $obj);
67 } else if ($head == '-' && $level > 1) {
68 if (! is_a($obj, 'ConfigTable_Direct'))
69 $obj = new ConfigTable_Direct('', $obj);
70 $obj->add_value($line);
72 } else if ($head == '|' && preg_match('/^\|(.+)\|\s*$/', $line, $matches)) {
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])));
79 $obj->add_line($line);
82 $this->objs[$obj->title] = $obj;
88 function & get($title)
90 $obj = & $this->get_object($title);
94 // Set an array (Override)
95 function put($title, $values)
97 $obj = & $this->get_object($title);
98 $obj->values = $values;
102 function add($title, $value)
104 $obj = & $this->get_object($title);
105 $obj->values[] = $value;
108 // Get an object (or create it)
109 function & get_object($title)
111 if (! isset($this->objs[$title]))
112 $this->objs[$title] = new ConfigTable('*' . trim($title) . "\n");
113 return $this->objs[$title];
118 page_write($this->page, $this->toString());
124 foreach ($this->objs as $title=>$obj)
125 $retval .= $obj->toString();
130 // Class holds array values
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
138 function ConfigTable($title, $obj = NULL)
141 $this->title = $obj->title;
142 $this->before = array_merge($obj->before, $obj->after);
144 $this->title = trim(substr($title, strspn($title, '*')));
145 $this->before[] = $title;
149 // Addi an explanation
150 function add_line($line)
152 $this->after[] = $line;
157 return join('', $this->before) . join('', $this->after);
161 class ConfigTable_Sequential extends ConfigTable
164 function add_value($value)
166 $this->values[] = (count($value) == 1) ? $value[0] : $value;
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";
178 $retval .= join('', $this->after);
183 class ConfigTable_Direct extends ConfigTable
185 var $_keys = array(); // Used at initialization phase
187 function set_key($line)
189 $level = strspn($line, '*');
190 $this->_keys[$level] = trim(substr($line, $level));
194 function add_value($line)
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));
203 function toString($values = NULL, $level = 2)
206 $root = ($values === NULL);
208 $retval = join('', $this->before);
209 $values = & $this->values;
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);
216 $retval .= str_repeat('-', $level - 1) . $value . "\n";
219 if ($root) $retval .= join('', $this->after);