Subversion Repositories php_library

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
#! /usr/bin/perl -w
30 rodolico 2
 
3
# Converts a standard MySQL dump file to a PHP file ready to be used by library.php.
4
# NOTE: don't use comma's or parentheses in your comments
5
 
6
 
1 rodolico 7
#use Data::Dumper;
8
 
9
 
10
sub getFieldInfo {
11
   my $fieldInfo = shift;
12
   my $primary_key = shift;
13
   my @output;
30 rodolico 14
   my $comment = '';
1 rodolico 15
 
30 rodolico 16
   # For a lookup table, we will just build it
17
   if ( $fieldInfo =~ m/references\s+([a-z0-9-_]+)\s*\(\s*([a-z0-9-_]+)\s*\)/i ) { # create a lookup
18
      push @output, "'type' => 'lookup', 'table' => '$1', 'keyfield' => '$2', 'display_field' => 'name'";
19
   } else { # this is just a standard field
20
      push @output, "'keyfield' => true" if ( $primary_key);
21
      if ($fieldInfo =~ m/not null/i ) {
22
         push @output, "'required' => true";
23
         $fieldInfo =~ s/not null//i;  # remove the not null from the string
24
      }
25
 
26
      if ($fieldInfo =~ m/comment *\'([^']+)\'/i ) {
27
         $comment = $1;
28
         $fieldInfo =~ s/comment *\'([^']+)\'//i
29
      } else {
30
         $comment = '';
31
      }
32
 
33
      $fieldInfo =~ s/default +null//i; # we just ignore default null
34
      if ($fieldInfo =~ m/default +(\'.*\')/i ) {
35
         $default = $1;
36
         $fieldInfo =~ s/default +(\'.*\')//i;
37
         push @output, "'default' => $default";
38
      } elsif ($fieldInfo =~ /default +([^ ]+)/i ) {
39
         $default = $1;
40
         $fieldInfo =~ s/default +([^ ]+)//;
41
         push @output, "'default' => $default";
42
      }
43
      if ($fieldInfo =~ m/auto_increment/i ) {
44
         push @output, "'readonly' => true";
45
         $fieldInfo =~ s/auto_increment//i;
46
      }
47
      # at this point, we should only have the data type left
48
      if ( $fieldInfo =~ m/ *([a-z]+)( *\(\d+\))?/i ) {
49
         $type = $1;
50
         $size = $2;
51
         $type =~ s/(varchar)|(char)/string/i;
52
         push @output, "'type' => '$type'";
53
         if ($size) {
54
            $size =~ s/[^0-9]//gi;
55
            push @output, "'width' => $size";
56
         } # if
57
      }
1 rodolico 58
   }
59
   #return "\n//$original\n\n" . join(" , ", @output);
5 rodolico 60
   return ($comment, join(" , ", @output));
1 rodolico 61
}
62
 
63
 
64
 
65
sub getTableDef {
66
   my $table = shift;
30 rodolico 67
   # print "[$table]\n";
1 rodolico 68
   my $unique_key_regex = 'unique key [a-z0-9]+ \(([^)]+)\),?';
69
   my $primary_key_regex = 'primary key \(([^)]+)\),?';
5 rodolico 70
   my $comment_regex = "comment *'([^']+)'";
1 rodolico 71
   my %tableInformation;
72
   $table =~ s/ +/ /gi; # remove duplicate spaces
73
   $table =~ s/ ,/,/gi; # remove all spaces before comma's
30 rodolico 74
   #print "[$table]\n\n";
5 rodolico 75
   $table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; # 
1 rodolico 76
   $tableInformation{'table name'} = $1;
30 rodolico 77
   print STDERR "[$tableInformation{'table name'}]\n";
1 rodolico 78
   $table = $2;
5 rodolico 79
   $tableCommentStuff = $3;
80
   if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
81
      $tableInformation{'comment'} = $1;
82
   }
1 rodolico 83
   if ( $table =~ m/$primary_key_regex/i ) {
84
      $tableInformation{'primary keys'} = $1;
85
      $table =~ s/$primary_key_regex//i;
86
   }
87
   if ( $table =~ m/$unique_key_regex/i ) {
88
      $tableInformation{'unique keys'} = $1;
89
      $table =~ s/$unique_key_regex//i;
90
   }
91
   my @columnInformation = split(',',$table);
92
   my %fieldInfo;
93
   for ( $i = 0; $i < @columnInformation; $i++) {
94
      $columnInformation[$i] =~ s/^ +//;
95
      if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i )  {
96
         $fieldName = $1;
97
         $fieldDef = $2;
5 rodolico 98
         ($fieldInfo{$fieldName}{'comment'},$fieldInfo{$fieldName}{'data'}) = &getFieldInfo($fieldDef, $tableInformation{'primary keys'} ? ($tableInformation{'primary keys'} =~ m/$fieldName/) : 0 );
1 rodolico 99
      }
100
   } # for
101
   #$tableInformation{'fields'} = \%fieldInfo;
102
   #print Dumper(\%fieldInfo );
30 rodolico 103
 
104
   # create the output
105
   my $output = '';
5 rodolico 106
   if ( $tableInformation{'comment'} ) {
107
      $output .= "/* $tableInformation{'comment'} */\n";
108
   }
30 rodolico 109
   $output .= "'" .  $tableInformation{'table name'} . "' => array( \n";
1 rodolico 110
   $output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
111
   if ($tableInformation{'primary keys'}) {
112
      if ( $tableInformation{'primary keys'} =~ m/,/ ) {
113
         #following line takes all items in a comma delimited list and quotes them
114
         $output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
115
      } else {
116
         $output .= "\t'key field' => '" . $tableInformation{'primary keys'} . "',\n";
117
      }
118
   }
119
   # do the display columns, basically all columns
120
   @fields = keys %fieldInfo;
121
   #$fieldNames = join( ',',@fields );
122
   $output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
123
   $output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
124
   my @fieldInfo;
125
   foreach $thisField (keys %fieldInfo ) {
30 rodolico 126
      push @fieldInfo, ;
127
 
128
      push @fieldInfo,  ($fieldInfo{$thisField}{'comment'} ? "\t\t/* " . $fieldInfo{$thisField}{'comment'} . " */\n" : '') .
129
                        "\t\t'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
1 rodolico 130
   }
30 rodolico 131
   $output .= "\t'field info' => array(\n" . join( ",\n", @fieldInfo) . "\n\t)\n)";
5 rodolico 132
 
1 rodolico 133
   return $output;
134
}
135
 
30 rodolico 136
my $indentationChar = '   ';
137
 
1 rodolico 138
my $inputFileName = shift;
5 rodolico 139
if ($inputFileName) {
140
   open DATA, "$inputFileName" or die "could not open $inputFileName";
141
   @input = <DATA>;
142
   close DATA;
143
} else {
144
   @input = <STDIN>;
1 rodolico 145
}
5 rodolico 146
@input = grep( !/^(--)|(\/\*\!)/, @input );
1 rodolico 147
 
5 rodolico 148
 
1 rodolico 149
chomp @input;
30 rodolico 150
# next line read all input, joins it with null,then splits it based on a semi-colon
151
# this places all sql statements into one array entry
152
# It then filters for only those statements that start with create table
153
my @tables = grep(/^create table/i, split( ';', join( '', @input) ));
154
#foreach my $thisTable (@tables) {
155
#   print "$thisTable\n";
156
#}
157
#exit 1;
158
 
1 rodolico 159
my @results;
160
#push @results, &getTableDef( shift @tables );
161
foreach $thisTable( @tables ) {
5 rodolico 162
   $thisTable =~ s/\`//gi;
30 rodolico 163
   push @results, &getTableDef( $thisTable ) if $thisTable;
1 rodolico 164
}
165
 
30 rodolico 166
# apply proper indentation. The code inserts a tab char and does a decent job of indenting
167
# we now replace the tab chars with $indentationChar, and add one more to the beginning
168
for (my $line = 0; $line < @results; $line++ ) {
169
   $results[$line] = "\t" . $results[$line];
170
   $results[$line] =~ s/\n/\n\t/g;
171
   $results[$line] =~ s/\t/$indentationChar/g;
172
}
173
 
1 rodolico 174
print "<?php\n";
175
print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
176
define ( IMAGE_DIRECTORY, '/pictures/' );  // relative URL where pictures are stored
177
define ( EDIT_IMAGE_HEIGHT, 100 );         // height for thumbnail of pictuers
178
define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
179
define (DEFAULT_TEXTAREA_HEIGHT, 5 );
30 rodolico 180
define ( DEFAULT_TABLE, 'FILL ME IN');
1 rodolico 181
global \$DATABASE_DEFINITION;
182
 
183
";
184
print "\$DATABASE_DEFINITION = array(\n" . join( ",\n", @results) . "\n);";
185
print "\n?>\n";
186
1;
187
 
188
 
189
 
190