-
Notifications
You must be signed in to change notification settings - Fork 4
/
converter.php
175 lines (140 loc) · 6.9 KB
/
converter.php
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
<?php
/**
* Command line based converter script
*
* @copyright (C) 2012-2014 FluxBB (http://fluxbb.org)
* @license GPL - GNU General Public License (http://www.gnu.org/licenses/gpl.html)
* @package FluxBB
*/
define('CONV_ROOT', dirname(__FILE__).'/');
require CONV_ROOT.'include/functions_cli.php';
require CONV_ROOT.'include/common.php';
// Output log messages to file
define('CONV_LOG', PUN_ROOT.'cache/converter.log');
// The number of items to process per page view (very hackish :P)
define('PER_PAGE', pow(2, 32));
// If PUN isn't defined, config.php is missing or corrupt
if (!defined('PUN'))
conv_error('Not installed');
echo '=========================================='."\n";
echo ' '.sprintf($lang_convert['FluxBB converter'], CONV_VERSION).' '."\n";
echo '=========================================='."\n\n";
$params = array('help', 'forum:', 'path:', 'type:', 'host::', 'name:', 'user:', 'pass:', 'prefix:', 'charset:');
$options = getopt('hf:d:t:s:n:u:p:r:c:', $params);
if (empty($options) || isset($options['h']) || isset($options['help']))
{
echo $lang_convert['Usage'].': '."\n";
echo "\t".'-f --forum'."\t".$lang_convert['Forum software']."\n";
echo "\t\t\t".sprintf($lang_convert['Possible values'], "\n\t\t\t\t".implode("\n\t\t\t\t", array_keys($forums)))."\n";
echo "\t".'-d --path'."\t".$lang_convert['Old forum path']."\n";
echo "\t".'-t --type'."\t".$lang_convert['Database type'].' '.sprintf($lang_convert['Default value'], $db_config_default['type'])."\n";
echo "\t\t\t".sprintf($lang_convert['Possible values'], "\n\t\t\t\t".implode("\n\t\t\t\t", $engines))."\n";
echo "\t".'-s --host'."\t".$lang_convert['Database server hostname'].' '.sprintf($lang_convert['Default value'], $db_config_default['host'])."\n";
echo "\t".'-n --name'."\t".$lang_convert['Database name']."\n";
echo "\t".'-u --user'."\t".$lang_convert['Database username'].' '.sprintf($lang_convert['Default value'], $db_config_default['username'])."\n";
echo "\t".'-p --pass'."\t".$lang_convert['Database password']."\n";
echo "\t".'-r --prefix'."\t".$lang_convert['Table prefix']."\n";
echo "\t".'-c --charset'."\t".$lang_convert['Database charset'].' '.sprintf($lang_convert['Default value'], $db_config_default['charset'])."\n";
echo "\n".$lang_convert['Note']."\n";
echo $lang_convert['Note info']."\n";
exit(1);
}
$forum_config = array(
'type' => isset($options['f']) ? $options['f'] : (isset($options['forum']) ? $options['forum'] : null),
'path' => isset($options['d']) ? $options['d'] : (isset($options['path']) ? $options['path'] : null),
);
$old_db_config = array(
'type' => isset($options['t']) ? $options['t'] : (isset($options['type']) ? $options['type'] : $db_config_default['type']),
'host' => isset($options['s']) ? $options['s'] : (isset($options['host']) ? $options['host'] : $db_config_default['host']),
'name' => isset($options['n']) ? $options['n'] : (isset($options['name']) ? $options['name'] : $db_config_default['name']),
'username' => isset($options['u']) ? $options['u'] : (isset($options['user']) ? $options['user'] : $db_config_default['username']),
'password' => isset($options['p']) ? $options['p'] : (isset($options['pass']) ? $options['pass'] : $db_config_default['password']),
'prefix' => isset($options['r']) ? $options['r'] : (isset($options['prefix']) ? $options['prefix'] : $db_config_default['prefix']),
'charset' => isset($options['c']) ? $options['c'] : (isset($options['charset']) ? $options['charset'] : $db_config_default['charset']),
);
$forum_config = array_map('trim', $forum_config);
$old_db_config = array_map('trim', $old_db_config);
// Check whether we have all needed data valid
validate_params($forum_config, $old_db_config);
if (!array_key_exists($forum_config['type'], $forums))
{
// Try to correct forum name (ignore case)
$keys = array_keys($forums);
$values = array();
foreach ($keys as $cur_key)
if (strpos(strtolower($cur_key), strtolower($forum_config['type'])) === 0)
$values[] = $cur_key;
if (count($values) == 1)
$forum_config['type'] = $values[0];
else if (($key = array_search(strtolower($forum_config['type']), array_map('strtolower', $keys))) !== false)
$forum_config['type'] = $keys[$key];
else
conv_error($lang_convert['Invalid forum software'].' '.sprintf($lang_convert['Possible values'], "\n".implode("\n", array_keys($forums))));
}
if (!in_array($old_db_config['type'], $engines))
conv_error($lang_convert['Invalid database type'].' '.sprintf($lang_convert['Possible values'], "\n".implode("\n", $engines)));
// Get database configuration from config.php
$db_config = array(
'type' => $db_type,
'host' => $db_host,
'name' => $db_name,
'username' => $db_username,
'password' => $db_password,
'prefix' => $db_prefix,
);
// Check we aren't trying to convert to the same database
if ($old_db_config == $db_config)
conv_error('Same database tables');
if (defined('CONV_LOG') && file_exists(CONV_LOG))
@unlink(CONV_LOG);
conv_log('Running command line based converter for: '.$forum_config['type'].' ('.gmdate('Y-m-d H:i:s').')');
conv_log('PHP version: '.PHP_VERSION.', OS: '.PHP_OS);
$session = array();
// Create a wrapper for fluxbb (has easy functions for adding users etc.)
require CONV_ROOT.'include/fluxbb.class.php';
$fluxbb = new FluxBB($pun_config);
$db = $fluxbb->connect_database($db_config);
// Load the migration script
require CONV_ROOT.'include/forum.class.php';
$forum = load_forum($forum_config, $fluxbb);
$forum->connect_database($old_db_config);
// Load converter script
require CONV_ROOT.'include/converter.class.php';
$converter = new Converter($fluxbb, $forum);
// Start the converter
$next_step = array(null);
while ($next_step !== false)
{
conv_message();
conv_log('-----------------'."\n");
$next_step = $converter->convert($next_step[0], isset($next_step[1]) ? $next_step[1] : 0);
}
// We're done
$alerts = array(sprintf($lang_convert['Rebuild search index note'], $lang_convert['rebuild search index']));
if (!$forum->converts_password)
$alerts[] = $lang_convert['Password converter mod'];
$fluxbb->close_database();
if (!empty($session['dupe_users']))
{
conv_message("\n".'---------------------------'."\n");
conv_message($lang_convert['Username dupes head']);
conv_message($lang_convert['Error info 1']);
conv_message($lang_convert['Error info 2']);
foreach ($_SESSION['converter']['dupe_users'] as $id => $cur_user)
conv_message("\t".$lang_convert['was renamed to'], $cur_user['old_username'], $cur_user['username']);
conv_message();
conv_message($lang_convert['Convert username dupes question']);
$handle = fopen('php://stdin', 'r');
$line = trim(fgets($handle));
if ($line == 'yes')
alert_dupe_users();
}
if (!empty($alerts))
{
conv_message("\n".'---------------------------'."\n");
conv_message($lang_convert['Notes'].':'."\n".implode("\n", $alerts));
}
conv_message();
conv_message($lang_convert['Conversion completed in'], round($session['time'], 4));
conv_log('Conversion completed in '.$session['time'], false, true);
exit(0);