]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - config/migrate-phpwiki-config
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / config / migrate-phpwiki-config
1 #!/usr/bin/perl -w
2 #
3 # phpwiki configuration file migration script
4 # index.php => config.ini
5 #
6 # Author:   Matt Brown <matt@mattb.net.nz>,
7 #           Reini Urban <rurban@x-ray.at>
8 #
9 # Run this script without any arguments for usage information.
10 #
11 # This script is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # PhpWiki is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with PhpWiki; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 # Store the values
26 # In the form $values{$varname} = (value, comment_state)
27 # Where value is the value in index.php and comment_state indicates
28 # whether the line was commented or not
29 use strict;
30 use warnings;
31
32 my %values = ();
33
34 # Check we got a index.php to read from
35 if ($#ARGV < 0) {
36   print "Usage: ./phpwiki-migrate-config <../path/to/index.php>\n\n";
37   print "Takes an old style (<= 1.3.9) index.php configuration file\n";
38   print "for phpwiki and outputs a new style (>= 1.3.10) ./config.ini\n";
39   print "configuration file for phpwiki, based on a set of\n";
40   print "./config-{dist,default}.ini files.\n";
41   print "\n";
42   print "The values extracted from the old config file will be\n";
43   print "substituted into a new config.ini file as necessary. Commented\n";
44   print "directives in index.php will remain commented in config.ini\n";
45
46   exit 1;
47 }
48
49 # Read in values from index.php
50 my $infile = shift;
51 open INDEXPHP, "< $infile" or die "Cannot read $infile: $!\n";
52 my $inifile     = "config.ini";
53 my $distfile    = "config-dist.ini";
54 my $defaultfile = "config-default.ini";
55 die "$inifile already exists.\n" if -e $inifile;
56
57 # Write out config.ini, substituting values as needed
58 open OUT, "> $inifile"
59   or die "Failed to write $inifile. $!\n";
60 open CONFDIST, "< $distfile"
61   or die "Failed to open $distfile: $!\n";
62 open CONFDEFAULT, "< $defaultfile"
63   or die "Failed to open $defaultfile: $!\n";
64
65 my $epmaj = 0;
66 my $epmin = 0;
67
68 while (<INDEXPHP>) {
69   my ($name, $value) = ('','');
70   # Check if the line starts with a comment (# or / char)
71   my $is_comment = /^[#\/]/;
72   if (!$is_comment) {
73     $is_comment = 0;
74   }
75
76   # Process expire parameters
77   if (/'keep'/) {
78     ($value) = (/'keep'\s*=>\s*(.*)\)/);
79     if ($epmaj) {
80       $name = "MAJOR_KEEP";
81     } elsif ($epmin) {
82       $name = "MINOR_KEEP";
83     }
84   }
85   $epmaj = 0;
86   $epmin = 0;
87
88   # Skip some known bad options
89   if (/ALLOW_LDAP_LOGIN/) {
90     next;
91   } elsif (/ALLOW_IMAP_LOGIN/) {
92     next;
93
94     # Process defined values
95   } elsif (/define\s*\(/) {
96     # Extract the name of the define and the value of it
97     ($name, $value) = (/define\s*\(\s*['"](.*)['"]\s*,\s*['"]*([^'"]*)['"]*\s*\)/);
98
99     # Process PHP variable values
100   } elsif (/HTML_DUMP_SUFFIX\s*=/) {
101     ($name, $value) = (/\$(\w*)\s*=\s*['"](.*)['"];\s*/);
102   } elsif (/AllowedProtocols\s*=/) {
103     ($value) = (/\$\w*\s*=\s*(.*);\s*/);
104     $name = "ALLOWED_PROTOCOLS";
105   } elsif (/InlineImages\s*=/) {
106     ($value) = (/\$\w*\s*=\s*(.*);\s*/);
107     $name = "INLINE_IMAGES";
108   } elsif (/WikiNameRegexp\s*=/) {
109     ($value) = (/\$\w*\s*=\s*(.*);\s*/);
110     $name = "WIKI_NAME_REGEXP";
111
112     # Process arrays that need to be converted
113   } elsif (/GenericPages\s*=/) {
114     ($value) = (/\$\w*\s*=\s*array\((.*)\)/);
115     $name = "DEFAULT_WIKI_PAGES";
116     $value =~ s/[" ]//g;
117     $value =~ s/,/:/g;
118     $value = "\"$value\""
119   } elsif (/keywords\s*=/) {
120     ($value) = (/\$\w*\s*=\s*array\((.*)\)/);
121     $name = "KEYWORDS";
122     $value =~ s/[" ]//g;
123     $value =~ s/,/:/g;
124     $value = "\"$value\""
125
126       # Process database paramters
127   } elsif (/'dbtype'\s*=>/) {
128     ($value) = (/'dbtype'\s*=>\s*['"](.*)['"]/);
129     $name = "DATABASE_TYPE";
130   } elsif (/'dsn'\s*=>/) {
131     ($value) = (/'dsn'\s*=>\s*['"](.*)['"]/);
132     $name = "DATABASE_DSN";
133   } elsif (/'timeout'\s*=>/) {
134     ($value) = (/'timeout'\s*=>\s*(.*)\s*,/);
135     $name = "DATABASE_TIMEOUT";
136   } elsif (/'db_session_table'\s*=>/) {
137     ($value) = (/'db_session_table'\s*=>\s*['"](.*)['"]/);
138     $name = "DATABASE_SESSION_TABLE";
139   } elsif (/'prefix'\s*=>/) {
140     ($value) = (/'prefix'\s*=>\s*['"](.*)['"]/);
141     $name = "DATABASE_PREFIX";
142   } elsif (/'directory'\s*=>/) {
143     ($value) = (/'directory'\s*=>\s*['"](.*)['"]/);
144     $name = "DATABASE_DIRECTORY";
145   } elsif (/'dba_handler'\s*=>/) {
146     ($value) = (/'dba_handler'\s*=>\s*['"](.*)['"]/);
147     $name = "DATABASE_DBA_HANDLER";
148
149     # Process Expire Parameters
150   } elsif (/\$ExpireParams\['major'\]\s*=/) {
151     ($value) = (/'max_age'\s*=>\s*(.*),/);
152     $name = "MAJOR_MAX_AGE";
153     $epmaj = 1;
154   } elsif (/\$ExpireParams\['minor'\]\s*=/) {
155     ($value) = (/'max_age'\s*=>\s*(.*),/);
156     $name = "MINOR_MAX_AGE";
157     $epmin = 1;
158
159     # Process include path
160   } elsif (/ini_set.*include_path'/) {
161     ($value) = (/ini_set.*include_path'\s*,\s*(.*)\s*\)/);
162     $name = "INCLUDE_PATH";
163
164   }
165   if ($name =~ /^$/) {
166     next;
167   }
168
169   # Put it into the array
170   if (exists $values{$name} && $is_comment) {
171     # If we already have a value and this one is commented, skip it
172     next;
173   }
174   #print "$name => $value ($is_comment)\n";
175   $values{$name} = [$value, $is_comment];
176 }
177
178 close INDEXPHP;
179
180 # Print values we got
181 #for my $name ( keys %values ) {
182 #    ($value, $is_comment) = @{$values{$name}};
183 #    print "$name => $value";
184 #    if ($is_comment) { print " (commented)"; }
185 #    print "\n";
186 #}
187
188 select OUT;
189 # Write out migration header
190 print "; phpwiki 1.3.x configuration automatically generated from $infile\n";
191 print "; by migrate-phpwiki-config script\n\n";
192
193 while (<CONFDIST>) {
194   # Look for config var lines
195   if (!/\w* = /) {
196     print $_;
197     next;
198   }
199   my ($name, $value, $is_comment);
200   ($name) = (/(\w*) = /);
201   if (!exists $values{$name}) {
202     print $_;
203     next;
204   }
205   ($value, $is_comment) = @{$values{$name}};
206   if ($is_comment) {
207     print "; $name = $value\n";
208   } else {
209     print "$name = $value\n";
210   }
211 }
212
213 close OUT;
214 close CONFDIST;
215 close CONFDEFAULT;