OSDN Git Service

file_head() with non-lock option
[pukiwiki/pukiwiki.git] / lib / config.php
index 1f1efe4..3cac328 100644 (file)
 <?php
-/////////////////////////////////////////////////
 // PukiWiki - Yet another WikiWikiWeb clone.
+// $Id: config.php,v 1.6 2005/04/29 11:24:20 henoheno Exp $
+// Copyright (C) 2003-2005 PukiWiki Developers Team
+// License: GPL v2 or (at your option) any later version
 //
-// $Id: config.php,v 1.3 2004/12/23 02:04:14 henoheno Exp $
-//
+// Parse a PukiWiki page as a configuration page
+
 /*
- * ¥×¥é¥°¥¤¥ó¤ÎÀßÄê¤òPukiWiki¤Î¥Ú¡¼¥¸¤Ëµ­½Ò¤¹¤ë
- *
- * // ¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®
- * $obj = new Config('plugin/¥×¥é¥°¥¤¥ó̾/')
- *
- * // Æɤ߽Ф·
+ * $obj = new Config('plugin/plugin_name/')
  * $obj->read();
- *
- * // ÇÛÎó¼èÆÀ
  * $array = & $obj->get($title);
- *
- * // Äɲà- Ä¾ÀÜ
- * $array[] = array(4, 5, 6);
- *
- * // Äɲà- Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
- * $obj->add($title, array(4, 5, 6));
- *
- * // ÃÖ´¹ - Ä¾ÀÜ
- * $array = array(1=>array(1, 2, 3));
- *
- * // ÃÖ´¹ - Config¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥½¥Ã¥É
- * $obj->put($title, array(1=>array(1, 2, 3));
- *
- * // ¾Ãµî
- * $obj->put_values($title, NULL);
- *
- * // ½ñ¤­¹þ¤ß
+ * $array[] = array(4, 5, 6);          // Add - directly
+ * $obj->add($title, array(4, 5, 6));  // Add - method of Config object
+ * $array = array(1=>array(1, 2, 3));          // Replace - directly
+ * $obj->put($title, array(1=>array(1, 2, 3)); // Replace - method of Config object
+ * $obj->put_values($title, NULL);     // Delete
  * $obj->write();
- *
  */
 
-// ¥Ú¡¼¥¸Ì¾¤Î¥×¥ì¥Õ¥£¥¯¥¹
-define('CONFIG_BASE', ':config/');
+// Fixed prefix of configuration-page's name
+define('PKWK_CONFIG_PREFIX', ':config/');
 
-// ÀßÄê¥Ú¡¼¥¸´ÉÍý
+// Configuration-page manager
 class Config
 {
-       // ¥Ú¡¼¥¸Ì¾
-       var $name, $page;
-
-       // Í×ÁÇ
+       var $name, $page; // Page name
        var $objs = array();
 
        function Config($name)
        {
                $this->name = $name;
-               $this->page = CONFIG_BASE . $name;
+               $this->page = PKWK_CONFIG_PREFIX . $name;
        }
 
-       // ¥Ú¡¼¥¸¤òÆɤ߹þ¤à
+       // Load the configuration-page
        function read()
        {
                if (! is_page($this->page)) return FALSE;
 
                $this->objs = array();
                $obj        = & new ConfigTable('');
+               $matches = array();
+
                foreach (get_source($this->page) as $line) {
                        if ($line == '') continue;
 
-                       $head  = $line{0};
+                       $head  = $line{0};      // The first letter
                        $level = strspn($line, $head);
 
                        if ($level > 3) {
                                $obj->add_line($line);
-                               continue;
-                       }
 
-                       if ($head == '*') {
-                               // ¸«½Ð¤·¤Î¸ÇÍ­IDÉô¤òºï½ü
+                       } else if ($head == '*') {
+                               // Cut fixed-heading anchors
                                $line = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/', '$1$2', $line);
 
                                if ($level == 1) {
                                        $this->objs[$obj->title] = $obj;
                                        $obj = & new ConfigTable($line);
                                } else {
-                                       if (! is_a($obj, 'ConfigTable_Direct')) {
+                                       if (! is_a($obj, 'ConfigTable_Direct'))
                                                $obj = & new ConfigTable_Direct('', $obj);
-                                       }
                                        $obj->set_key($line);
                                }
                                
                        } else if ($head == '-' && $level > 1) {
-                               if (! is_a($obj, 'ConfigTable_Direct')) {
+                               if (! is_a($obj, 'ConfigTable_Direct'))
                                        $obj = & new ConfigTable_Direct('', $obj);
-                               }
                                $obj->add_value($line);
 
                        } else if ($head == '|' && preg_match('/^\|(.+)\|\s*$/', $line, $matches)) {
-                               if (! is_a($obj, 'ConfigTable_Sequential')) {
+                               // Table row
+                               if (! is_a($obj, 'ConfigTable_Sequential'))
                                        $obj = & new ConfigTable_Sequential('', $obj);
-                               }
                                // Trim() each table cell
                                $obj->add_value(array_map('trim', explode('|', $matches[1])));
                        } else {
@@ -107,34 +84,32 @@ class Config
                return TRUE;
        }
 
-       // ÇÛÎó¤ò¼èÆÀ¤¹¤ë
+       // Get an array
        function & get($title)
        {
                $obj = & $this->get_object($title);
                return $obj->values;
        }
 
-       // ÇÛÎó¤òÀßÄꤹ¤ë(¾å½ñ¤­)
+       // Set an array (Override)
        function put($title, $values)
        {
                $obj         = & $this->get_object($title);
                $obj->values = $values;
        }
 
-       // ¹Ô¤òÄɲ乤ë
+       // Add a line
        function add($title, $value)
        {
                $obj = & $this->get_object($title);
                $obj->values[] = $value;
        }
 
-       // ¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤Ê¤¤¤È¤­¤Ïºî¤ë)
+       // Get an object (or create it)
        function & get_object($title)
        {
                if (! isset($this->objs[$title]))
-               {
                        $this->objs[$title] = & new ConfigTable('*' . trim($title) . "\n");
-               }
                return $this->objs[$title];
        }
 
@@ -146,27 +121,19 @@ class Config
        function toString()
        {
                $retval = '';
-               foreach ($this->objs as $title=>$obj) {
+               foreach ($this->objs as $title=>$obj)
                        $retval .= $obj->toString();
-               }
                return $retval;
        }
 }
 
-//ÇÛÎóÃͤòÊÝ»ý¤¹¤ë¥¯¥é¥¹
+// Class holds array values
 class ConfigTable
 {
-       // ¥Æ¡¼¥Ö¥ë¤Î̾Á°
-       var $title = '';
-
-       // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
-       var $before = array();
-
-       // ¼èÆÀ¤·¤¿ÃͤÎÇÛÎó
-       var $values = array();
-
-       // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
-       var $after = array();
+       var $title  = '';       // Table title
+       var $before = array();  // Page contents (except table ones)
+       var $after  = array();  // Page contents (except table ones)
+       var $values = array();  // Table contents
 
        function ConfigTable($title, $obj = NULL)
        {
@@ -179,7 +146,7 @@ class ConfigTable
                }
        }
 
-       // ÀâÌÀ¤ÎÄɲÃ
+       // Addi an  explanation
        function add_line($line)
        {
                $this->after[] = $line;
@@ -193,7 +160,7 @@ class ConfigTable
 
 class ConfigTable_Sequential extends ConfigTable
 {
-       // ¹Ô¤ÎÄɲÃ
+       // Add a line
        function add_value($value)
        {
                $this->values[] = (count($value) == 1) ? $value[0] : $value;
@@ -205,7 +172,7 @@ class ConfigTable_Sequential extends ConfigTable
                if (is_array($this->values)) {
                        foreach ($this->values as $value) {
                                $value   = is_array($value) ? join('|', $value) : $value;
-                               $retval .= "|$value|\n";
+                               $retval .= '|' . $value . '|' . "\n";
                        }
                }
                $retval .= join('', $this->after);
@@ -215,17 +182,15 @@ class ConfigTable_Sequential extends ConfigTable
 
 class ConfigTable_Direct extends ConfigTable
 {
-       // ¼èÆÀ¤·¤¿¥­¡¼¤ÎÇÛÎó¡£½é´ü²½»þ¤Ë»ÈÍѤ¹¤ë¡£
-       var $_keys = array();
+       var $_keys = array();   // Used at initialization phase
 
-       // ¥­¡¼¤ÎÀßÄê
        function set_key($line)
        {
                $level = strspn($line, '*');
                $this->_keys[$level] = trim(substr($line, $level));
        }
 
-       // ¹Ô¤ÎÄɲÃ
+       // Add a line
        function add_value($line)
        {
                $level = strspn($line, '-');
@@ -238,7 +203,7 @@ class ConfigTable_Direct extends ConfigTable
        function toString($values = NULL, $level = 2)
        {
                $retval = '';
-               $root = ($values === NULL);
+               $root   = ($values === NULL);
                if ($root) {
                        $retval = join('', $this->before);
                        $values = & $this->values;