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