Revision: 48587
Author:   ialex
Date:     2009-03-19 18:19:23 +0000 (Thu, 19 Mar 2009)

Log Message:
-----------
Added the possibility to pass some definitions to load_txt_def.
This adds a thrid parameter to load_txt_def, which is an array with the 
following keys (all optionals):
* array: list of keys that will always be arrays, even if they are not defined 
with "[]" in the file
* allowed: list of allowed keys, if a key is used but not in this array, an 
error will be emitted
* key: set the "Group" name in this key instead of using associative array for 
the "top level" one

Users who compiled an older version of load_txt_def must recompile it.

Modified Paths:
--------------
    trunk/extensions/Configure/CHANGELOG
    trunk/extensions/Configure/Configure.php
    trunk/extensions/Configure/Configure.settings.php
    trunk/extensions/Configure/TxtDef.php
    trunk/extensions/Configure/load_txt_def/load_txt_def.c
    trunk/extensions/Configure/load_txt_def/php_load_txt_def.h
    trunk/extensions/Configure/load_txt_def/tests/load_txt_def.phpt
    trunk/extensions/Configure/load_txt_def/tests/sample.txt

Modified: trunk/extensions/Configure/CHANGELOG
===================================================================
--- trunk/extensions/Configure/CHANGELOG        2009-03-19 18:07:50 UTC (rev 
48586)
+++ trunk/extensions/Configure/CHANGELOG        2009-03-19 18:19:23 UTC (rev 
48587)
@@ -1,6 +1,10 @@
 This file lists changes on this extension.
 Localisation updates are done on betawiki and aren't listed here.
 
+0.13.4 - 19 March 2009
+  Added the possibility to pass some definitions to load_txt_def.
+  Users who compiled an older version of load_txt_def must recompile it.
+
 0.13.3 - 17 March 2009
   Added $wgTiffThumbnailType.
 

Modified: trunk/extensions/Configure/Configure.php
===================================================================
--- trunk/extensions/Configure/Configure.php    2009-03-19 18:07:50 UTC (rev 
48586)
+++ trunk/extensions/Configure/Configure.php    2009-03-19 18:19:23 UTC (rev 
48587)
@@ -17,7 +17,7 @@
        'url' => 'http://www.mediawiki.org/wiki/Extension:Configure',
        'description' => 'Allow authorised users to configure the wiki via a 
web-based interface',
        'descriptionmsg' => 'configure-desc',
-       'version' => '0.13.3',
+       'version' => '0.13.4',
 );
 
 # Configuration part

Modified: trunk/extensions/Configure/Configure.settings.php
===================================================================
--- trunk/extensions/Configure/Configure.settings.php   2009-03-19 18:07:50 UTC 
(rev 48586)
+++ trunk/extensions/Configure/Configure.settings.php   2009-03-19 18:19:23 UTC 
(rev 48587)
@@ -60,7 +60,7 @@
                wfProfileIn( __METHOD__ );
 
                global $wgConfigureAdditionalExtensions;
-               $coreExtensions = TxtDef::remapForConfigure( 
TxtDef::loadFromFile( dirname( __FILE__ ) . '/Configure.settings-ext.txt' ) );
+               $coreExtensions = TxtDef::loadFromFile( dirname( __FILE__ ) . 
'/Configure.settings-ext.txt', array( 'key' => 'name' ) );
                $extensions = array_merge( $coreExtensions, 
$wgConfigureAdditionalExtensions );
                usort( $extensions, array( __CLASS__, 'compExt' ) );
                $list = array();

Modified: trunk/extensions/Configure/TxtDef.php
===================================================================
--- trunk/extensions/Configure/TxtDef.php       2009-03-19 18:07:50 UTC (rev 
48586)
+++ trunk/extensions/Configure/TxtDef.php       2009-03-19 18:19:23 UTC (rev 
48587)
@@ -3,27 +3,29 @@
 class TxtDef {
        private static $arrayReplacements = array();
 
-       public static function loadFromFile( $file, $forceLocal = false ) {
+       public static function loadFromFile( $file, $definitions = array(), 
$forceLocal = false ) {
                if( function_exists( 'load_txt_def' ) && !$forceLocal ) {
-                       return load_txt_def( $file );
+                       return load_txt_def( $file, false, $definitions );
                } else {
-                       return self::phpParser( $file );
+                       return self::phpParser( $file, $definitions );
                }
        }
 
-       public static function remapForConfigure( $arr ) {
-               $out = array();
-               foreach( $arr as $name => $defs ) {
-                       $out[] = array( 'name' => $name ) + $defs;
-               }
-               return $out;
-       }
-
-       private static function phpParser( $file ) {
+       private static function phpParser( $file, $definitions ) {
                $lines = file( $file );
                $out = array();
                $head = false;
 
+               if ( isset( $definitions['array'] ) )
+                       $arrayKeys = (array)$definitions['array'];
+               else
+                       $arrayKeys = array();
+
+               if ( isset( $definitions['allowed'] ) )
+                       $allowedKeys = (array)$definitions['allowed'];
+               else
+                       $allowedKeys = array();
+
                foreach( $lines as $line ) {
                        $line = trim( preg_replace( '/#.*$/', '', $line ) );
                        if( $line == '' )
@@ -39,8 +41,22 @@
                        }
 
                        list( $key, $value ) = array_map( 'trim', explode( '=', 
$line, 2 ) );
-                               if( substr( $key, -2 ) == '[]' ) {
+
+                       if( substr( $key, -2 ) == '[]' ) {
                                $key = substr( $key, 0, -2 );
+                               $isArray = true;
+                       } elseif( in_array( $key, $arrayKeys ) ) {
+                               $isArray = true;
+                       } else {
+                               $isArray = false;
+                       }
+
+                       if( count( $allowedKeys ) && !in_array( $key, 
$allowedKeys ) ) {
+                               trigger_error( "The key {$key} is not in the 
allowed keys in {$file}", E_USER_WARNING );
+                               return false;
+                       }
+
+                       if( $isArray ) {
                                if( !isset( $out[$head][$key] ) )
                                        $out[$head][$key] = array();
 
@@ -50,9 +66,22 @@
                                $out[$head][$key] = self::maybeTransformValue( 
$value );
                        }
                }
+
+               if ( isset( $definitions['key'] ) ) {
+                       $out = self::remapKeys( $out, $definitions['key'] );
+               }
+
                return $out;
        }
 
+       public static function remapKeys( $arr, $key ) {
+               $out = array();
+               foreach( $arr as $name => $defs ) {
+                       $out[] = array( $key => $name ) + $defs;
+               }
+               return $out;
+       }
+
        private static function processArray( &$ret, $string ) {
                $string = self::escapeSubArrays( $string );
                $parts = array_map( 'trim', explode( ',', $string ) );

Modified: trunk/extensions/Configure/load_txt_def/load_txt_def.c
===================================================================
--- trunk/extensions/Configure/load_txt_def/load_txt_def.c      2009-03-19 
18:07:50 UTC (rev 48586)
+++ trunk/extensions/Configure/load_txt_def/load_txt_def.c      2009-03-19 
18:19:23 UTC (rev 48587)
@@ -41,7 +41,7 @@
  * Every user visible function must have an entry in load_txt_def_functions[].
  */
 zend_function_entry load_txt_def_functions[] = {
-       PHP_FE(load_txt_def,    NULL)
+       PHP_FE( load_txt_def, NULL )
        {NULL, NULL, NULL}
 };
 /* }}} */
@@ -80,7 +80,7 @@
 
 
 /**
- * {{{ proto array load_txt_def( string filename [, bool use_include_path] )
+ * {{{ proto array load_txt_def( string filename [, bool use_include_path[, 
array definitions]] )
  */
 PHP_FUNCTION( load_txt_def ) {
        char *filename = NULL, *contents = NULL;
@@ -93,15 +93,21 @@
        char **lines = NULL, *line, *new_line, *trim_line, *comment, *pos, 
*start, *end;
        int  lines_len = 0, line_len, new_line_len, trim_line_len;
 
+       zval *entry = NULL, *values = NULL, **oldvalues = NULL;
+
        char *delim, *arg, *trim_arg, *end_arg, *keyval;
-       int delim_len, arg_len, end_arg_len, keyval_len;
+       int delim_len, arg_len, end_arg_len, keyval_len, is_array;
 
+       zval *definitions = NULL, **def_key, **def_value;
+       HashTable *def_key_hash;
+       HashPosition def_pointer;
+       char **k_array = NULL, **k_allowed = NULL, *k_key = NULL;
+       int k_array_len = 0, k_allowed_len = 0, k_key_len = 0;
+
        int i = 0, retval = SUCCESS;
 
-       zval *entry = NULL, *values = NULL, **oldvalues = NULL;
-
-       if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s|b", 
&filename, &filename_len, &use_include_path ) == FAILURE ) {
-               return;
+       if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s|ba", 
&filename, &filename_len, &use_include_path, &definitions ) == FAILURE ) {
+               RETURN_FALSE;
        }
 
        /* Open the file */
@@ -187,6 +193,47 @@
                efree( contents );
                php_stream_close( def_file );
 
+               /* parse definitions */
+               if ( definitions ) {
+                       if ( zend_hash_find( Z_ARRVAL_P( definitions ), 
"array", sizeof( "array" ), (void**)&def_key ) == SUCCESS ) {
+                               if ( Z_TYPE_PP( def_key ) != IS_ARRAY ) {
+                                       php_error( E_WARNING, "The \"array\" 
key of the third parameter must be an array!" );
+                                       goto free;
+                               }
+                               def_key_hash = Z_ARRVAL_PP( def_key );
+                               for ( zend_hash_internal_pointer_reset_ex( 
def_key_hash, &def_pointer );
+                                       zend_hash_get_current_data_ex( 
def_key_hash, (void**)&def_value, &def_pointer ) == SUCCESS;
+                                       zend_hash_move_forward_ex( 
def_key_hash, &def_pointer ) ) {
+
+                                       convert_to_string( *def_value );
+                                       k_array_len++;
+                                       k_array = ( char** ) erealloc( k_array, 
sizeof( char* ) * k_array_len );
+                                       k_array[k_array_len-1] = Z_STRVAL_PP( 
def_value );
+                               }
+                       }
+                       if ( zend_hash_find( Z_ARRVAL_P( definitions ), 
"allowed", sizeof( "allowed" ), (void**)&def_key ) == SUCCESS ) {
+                               if ( Z_TYPE_PP( def_key ) != IS_ARRAY ) {
+                                       php_error( E_WARNING, "The \"allowed\" 
key of the third parameter must be an array!" );
+                                       goto free;
+                               }
+                               def_key_hash = Z_ARRVAL_PP( def_key );
+                               for ( zend_hash_internal_pointer_reset_ex( 
def_key_hash, &def_pointer );
+                                       zend_hash_get_current_data_ex( 
def_key_hash, (void**)&def_value, &def_pointer ) == SUCCESS;
+                                       zend_hash_move_forward_ex( 
def_key_hash, &def_pointer ) ) {
+
+                                       convert_to_string( *def_value );
+                                       k_allowed_len++;
+                                       k_allowed = ( char** ) erealloc( 
k_allowed, sizeof( char* ) * k_allowed_len );
+                                       k_allowed[k_allowed_len-1] = 
Z_STRVAL_PP( def_value );
+                               }
+                       }
+                       if ( zend_hash_find( Z_ARRVAL_P( definitions ), "key", 
sizeof( "key" ), (void**)&def_key ) == SUCCESS ) {
+                               convert_to_string( *def_key );
+                               k_key = Z_STRVAL_PP( def_key );
+                               k_key_len = Z_STRLEN_PP( def_key );
+                       }
+               }
+
                /* real stuff */
                for ( i = 0; i < lines_len; i++ ) {
                        line = lines[i];
@@ -205,6 +252,7 @@
                                ltd_trim( arg, delim_len, &trim_arg, &arg_len );
 
                                line += delim_len + 1;
+                               is_array = 0;
 
                                if ( arg_len > 2 ) {
                                        end_arg = trim_arg + arg_len - 2;
@@ -215,7 +263,29 @@
                                if ( end_arg[0] == '[' && end_arg[1] == ']' ) {
                                        arg_len -= 2;
                                        trim_arg[arg_len] = '\0';
-                                       
+                                       trim_arg[arg_len+1] = '\0';
+                                       is_array = 1;
+                               }
+
+                               /* Check for allowed keys */
+                               if ( k_allowed_len > 0 ) {
+                                       if ( !ltd_in_array( k_allowed, 
k_allowed_len, trim_arg ) ) {
+                                               php_error( E_WARNING, "The key 
'%s' is not in the allowed keys in '%s' on line %i", trim_arg, filename, i + 1 
);
+                                               efree( arg );
+                                               if ( entry )
+                                                       zval_dtor( entry );
+                                               retval = FAILURE;
+                                               break;
+                                       }
+                               }
+
+                               /* Only check if it's not an array */
+                               if ( !is_array && k_array_len > 0 ) {
+                                       if ( ltd_in_array( k_array, 
k_array_len, trim_arg ) )
+                                               is_array = 1;
+                               }
+
+                               if ( is_array ) {
                                        if ( zend_hash_find( Z_ARRVAL_P( entry 
), trim_arg, arg_len + 1, (void**)&oldvalues ) == SUCCESS ) {
                                                values = *oldvalues;
                                        } else {
@@ -242,7 +312,12 @@
                        } else {
                                /* next item, add the current if we have one 
and initialse the next one */
                                if ( head ) {
-                                       add_assoc_zval( return_value, head, 
entry );
+                                       if ( k_key_len > 0 ) {
+                                               add_assoc_string_ex( entry, 
k_key, k_key_len + 1, head, 1 );
+                                               add_next_index_zval( 
return_value, entry );
+                                       } else {
+                                               add_assoc_zval( return_value, 
head, entry );
+                                       }
                                        entry = NULL;
                                }
                                MAKE_STD_ZVAL( entry );
@@ -251,8 +326,14 @@
                        }
                }
 
+free:
                if ( head && retval == SUCCESS ) {
-                       add_assoc_zval( return_value, head, entry );
+                       if ( k_key_len > 0 ) {
+                               add_assoc_string_ex( entry, k_key, k_key_len + 
1, head, 1 );
+                               add_next_index_zval( return_value, entry );
+                       } else {
+                               add_assoc_zval( return_value, head, entry );
+                       }
                } else if ( entry ) {
                        zval_dtor( entry );
                }
@@ -263,6 +344,11 @@
                }
                if ( i > 0 )
                        efree( lines );
+
+               if ( k_array_len > 0 )
+                       efree( k_array );
+               if ( k_allowed_len > 0 )
+                       efree( k_allowed );
                
 
                if ( retval == FAILURE ) {
@@ -306,6 +392,17 @@
        }
 }
 
+int ltd_in_array( char** array, int array_len, char* string ) {
+       int i;
+
+       for ( i = 0; i < array_len; i++ ) {
+               if ( strcmp( array[i], string ) == 0 )
+                       return 1;
+       }
+
+       return 0;
+}
+
 int ltd_skip_subarrays( char *pos, int len, char **new_pos, char *filename, 
int line_number ) {
        char *brace_open, *brace_close, *old_brace, *init_pos;
        int brace_count = 0;

Modified: trunk/extensions/Configure/load_txt_def/php_load_txt_def.h
===================================================================
--- trunk/extensions/Configure/load_txt_def/php_load_txt_def.h  2009-03-19 
18:07:50 UTC (rev 48586)
+++ trunk/extensions/Configure/load_txt_def/php_load_txt_def.h  2009-03-19 
18:19:23 UTC (rev 48587)
@@ -19,7 +19,7 @@
 #ifndef PHP_LOAD_TXT_DEF_H
 #define PHP_LOAD_TXT_DEF_H
 
-#define PHP_LOAD_TXT_DEF_VERSION "0.1"
+#define PHP_LOAD_TXT_DEF_VERSION "0.2"
 
 extern zend_module_entry load_txt_def_module_entry;
 
@@ -27,6 +27,7 @@
 PHP_FUNCTION( load_txt_def );
 
 void ltd_trim( char*, int, char**, int* );
+int ltd_in_array( char**, int, char* );
 int ltd_skip_subarrays( char*, int, char**, char*, int );
 int ltd_process_array( zval*, char*, int, char*, int );
 int ltd_process_entry( zval*, char*, int, char*, int );

Modified: trunk/extensions/Configure/load_txt_def/tests/load_txt_def.phpt
===================================================================
--- trunk/extensions/Configure/load_txt_def/tests/load_txt_def.phpt     
2009-03-19 18:07:50 UTC (rev 48586)
+++ trunk/extensions/Configure/load_txt_def/tests/load_txt_def.phpt     
2009-03-19 18:19:23 UTC (rev 48587)
@@ -6,7 +6,7 @@
        print 'skip';
 --FILE--
 <?php
-var_dump( load_txt_def( dirname( __FILE__ ) . '/sample.txt' ) );
+var_dump( load_txt_def( dirname( __FILE__ ) . '/sample.txt', false, array( 
'array' => array( 'array3' ) ) ) );
 --EXPECT--
 array(2) {
   ["Scalar"]=>
@@ -15,7 +15,7 @@
     string(12) "scalar value"
   }
   ["Arrays"]=>
-  array(3) {
+  array(4) {
     ["array"]=>
     array(2) {
       ["key1"]=>
@@ -45,5 +45,12 @@
         string(4) "val4"
       }
     }
+    ["array3"]=>
+    array(2) {
+      ["key3"]=>
+      string(6) "value3"
+      ["key4"]=>
+      string(6) "value4"
+    }
   }
 }

Modified: trunk/extensions/Configure/load_txt_def/tests/sample.txt
===================================================================
--- trunk/extensions/Configure/load_txt_def/tests/sample.txt    2009-03-19 
18:07:50 UTC (rev 48586)
+++ trunk/extensions/Configure/load_txt_def/tests/sample.txt    2009-03-19 
18:19:23 UTC (rev 48587)
@@ -5,6 +5,8 @@
 
 # Arrays tests
 Arrays
-array[]    = key1: value1, key2: value2
+array[]    = key1: value1
+array[]    = key2: value2
 overload[] = key1: value1, key1: value2
-array2[]   = key1: { sub1: val1,  sub2: val2 }, key2: { sub3: val3, sub4: val4 
}
\ No newline at end of file
+array2[]   = key1: { sub1: val1,  sub2: val2 }, key2: { sub3: val3, sub4: val4 
}
+array3     = key3: value3, key4: value4 
\ No newline at end of file



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to