File: migrate-phpwiki-config

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (208 lines) | stat: -rw-r--r-- 6,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/perl -w
#
# phpwiki configuration file migration script
#
# Author:   Matt Brown <matt@mattb.net.nz>
# Version:  $Id: migrate-phpwiki-config 156 2006-05-14 06:09:36Z matt $
#
# Run this script without any arguments for usage information.
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PhpWiki is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PhpWiki; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Store the values
# In the form $values{$varname} = (value, comment_state)
# Where value is the value in index.php and comment_state indicates
# whether the line was commented or not
%values = ();

# Check we got a template specified on the command line
if ($#ARGV < 0) {
    print "\n";
    print "Usage: cat index.php | ./migrate-phpwiki-config <config-template.ini>\n\n";
    print "Takes an old style (<= 1.3.9) index.php configuration file\n";
    print "for phpwiki and outputs a new style (>= 1.3.10) config.ini\n";
    print "configuration file for phpwiki based on a supplied template.\n";
    print "\n";
    print "config-template.ini should be a valid phpwiki config.ini\n";
    print "file with each valid directive occuring only once and\n";
    print "uncommented. The value of the directive in the template\n";
    print "should be set to a default value.\n\n";
    print "The values extracted from the old config file will be\n";
    print "substituted into this file as necessary. Commented out\n";
    print "directives in index.php will remain commented in config.ini\n";
    
    exit 1;
}

# Read in values from index.php which should be piped to stdin
open INDEXPHP, "-";

$epmaj = 0;
$epmin = 0;

while (<INDEXPHP>)
{
    $name = "";
    # Check if the line starts with a comment (# or / char)
    $is_comment = /^[#\/]/;
    if (!$is_comment) {
        $is_comment = 0;
    }
    
    # Process expire parameters
    if (/'keep'/) {
        ($value) = (/'keep'\s*=>\s*(.*)\)/);
        if ($epmaj) {
            $name = "MAJOR_KEEP";
        } elsif ($epmin) {
            $name = "MINOR_KEEP";
        }
    }
    $epmaj = 0;
    $epmin = 0;

    # Skip some known bad options
    if (/ALLOW_LDAP_LOGIN/) {
        next;
    } elsif (/ALLOW_IMAP_LOGIN/) {
        next;
        
    # Process defined values
    } elsif (/define\s*\(/) {
        # Extract the name of the define and the value of it
        ($name, $value) = (/define\s*\(\s*['"](.*)['"]\s*,\s*['"]*([^'"]*)['"]*\s*\)/);

    # Process PHP variable values
    } elsif (/HTML_DUMP_SUFFIX\s*=/) {
        ($name, $value) = (/\$(\w*)\s*=\s*['"](.*)['"];\s*/);
    } elsif (/AllowedProtocols\s*=/) {
        ($value) = (/\$\w*\s*=\s*(.*);\s*/);
        $name = "ALLOWED_PROTOCOLS";
    } elsif (/InlineImages\s*=/) {
        ($value) = (/\$\w*\s*=\s*(.*);\s*/);
        $name = "INLINE_IMAGES";
    } elsif (/WikiNameRegexp\s*=/) {
        ($value) = (/\$\w*\s*=\s*(.*);\s*/);
        $name = "WIKI_NAME_REGEXP";

    # Process arrays that need to be converted
    } elsif (/GenericPages\s*=/) {
        ($value) = (/\$\w*\s*=\s*array\((.*)\)/);
        $name = "DEFAULT_WIKI_PAGES";
        $value =~ s/[" ]//g;
        $value =~ s/,/:/g;
        $value = "\"$value\""
    } elsif (/keywords\s*=/) {
        ($value) = (/\$\w*\s*=\s*array\((.*)\)/);
        $name = "KEYWORDS";
        $value =~ s/[" ]//g;
        $value =~ s/,/:/g;
        $value = "\"$value\""
        
    # Process database paramters
    } elsif (/'dbtype'\s*=>/) {
        ($value) = (/'dbtype'\s*=>\s*['"](.*)['"]/);
        $name = "DATABASE_TYPE";
    } elsif (/'dsn'\s*=>/) {
        ($value) = (/'dsn'\s*=>\s*['"](.*)['"]/);
        $name = "DATABASE_DSN";
    } elsif (/'timeout'\s*=>/) {
        ($value) = (/'timeout'\s*=>\s*(.*)\s*,/);
        $name = "DATABASE_TIMEOUT";
    } elsif (/'db_session_table'\s*=>/) {
        ($value) = (/'db_session_table'\s*=>\s*['"](.*)['"]/);
        $name = "DATABASE_SESSION_TABLE";
    } elsif (/'prefix'\s*=>/) {
        ($value) = (/'prefix'\s*=>\s*['"](.*)['"]/);
        $name = "DATABASE_PREFIX";
    } elsif (/'directory'\s*=>/) {
        ($value) = (/'directory'\s*=>\s*['"](.*)['"]/);
        $name = "DATABASE_DIRECTORY";
    } elsif (/'dba_handler'\s*=>/) {
        ($value) = (/'dba_handler'\s*=>\s*['"](.*)['"]/);
        $name = "DATABASE_DBA_HANDLER";
    
    # Process Expire Parameters
    } elsif (/\$ExpireParams\['major'\]\s*=/) {
        ($value) = (/'max_age'\s*=>\s*(.*),/);
        $name = "MAJOR_MAX_AGE";
        $epmaj = 1;
    } elsif (/\$ExpireParams\['minor'\]\s*=/) {
        ($value) = (/'max_age'\s*=>\s*(.*),/);
        $name = "MINOR_MAX_AGE";
        $epmin = 1;
        
    # Process include path
    } elsif (/ini_set.*include_path'/) {
        ($value) = (/ini_set.*include_path'\s*,\s*(.*)\s*\)/);
        $name = "INCLUDE_PATH";
        
    }
    if ($name =~ /^$/) {
        next;
    }
    
    # Put it into the array
    if (exists $values{$name} && $is_comment) {
        # If we already have a value and this one is commented, skip it
        next;
    }
    #print "$name => $value ($is_comment)\n";
    $values{$name} = [$value, $is_comment];
        
}

close INDEXPHP;

# Print values we got
#for my $name ( keys %values ) {
#    ($value, $is_comment) = @{$values{$name}};
#    print "$name => $value";
#    if ($is_comment) { print " (commented)"; }
#    print "\n";
#}

# Write out migration header
print "; phpwiki 1.3.10 configuration automatically generated by\n";
print "; migrate-phpwiki-config script\n\n";

# Write out config.ini, substituting values as needed
open CONFTEMP, "< $ARGV[0]"
    or die "Failed to open $ARGV[0]: $!\n";

while (<CONFTEMP>)
{
    # Look for config var lines
    if (!/\w* = /) {
        print $_;
        next;
    }

    ($name) = (/(\w*) = /);
    if (!exists $values{$name}) {
        print $_;
        next;
    }
    ($value, $is_comment) = @{$values{$name}};
    if ($is_comment) {
        print "; $name = $value\n";
    } else {
        print "$name = $value\n";
    }
}

close CONFTEMP;

exit 0;