do not display ban link for own profile
[phpbb.git] / phpBB / install / install_convert.php
blobeaec24ef4fdf9f670bfb25a8a216cd8f0ce2890f
1 <?php
2 /**
4 * @package install
5 * @version $Id$
6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
14 if (!defined('IN_INSTALL'))
16 // Someone has tried to access the file direct. This is not a good idea, so exit
17 exit;
20 if (!empty($setmodules))
22 $module[] = array(
23 'module_type' => 'install',
24 'module_title' => 'CONVERT',
25 'module_filename' => substr(basename(__FILE__), 0, -strlen(PHP_EXT)-1),
26 'module_order' => 20,
27 'module_subs' => '',
28 'module_stages' => array('INTRO', 'SETTINGS', 'IN_PROGRESS', 'FINAL'),
29 'module_reqs' => ''
33 /**
34 * Class holding all convertor-specific details.
35 * @package install
37 class convert
39 var $options = array();
41 var $convertor_tag = '';
42 var $src_dbms = '';
43 var $src_dbhost = '';
44 var $src_dbport = '';
45 var $src_dbuser = '';
46 var $src_dbpasswd = '';
47 var $src_dbname = '';
48 var $src_table_prefix = '';
50 var $convertor_data = array();
51 var $tables = array();
52 var $config_schema = array();
53 var $convertor = array();
54 var $src_truncate_statement = 'DELETE FROM ';
55 var $truncate_statement = 'DELETE FROM ';
57 var $fulltext_search;
59 // Batch size, can be adjusted by the conversion file
60 // For big boards a value of 6000 seems to be optimal
61 var $batch_size = 2000;
62 // Number of rows to be inserted at once (extended insert) if supported
63 // For installations having enough memory a value of 60 may be good.
64 var $num_wait_rows = 20;
66 // Mysqls internal recoding engine messing up with our (better) functions? We at least support more encodings than mysql so should use it in favor.
67 var $mysql_convert = false;
69 var $p_master;
71 function __construct(&$p_master)
73 $this->p_master = &$p_master;
77 /**
78 * Convert class for conversions
79 * @package install
81 class install_convert extends module
83 /**
84 * Variables used while converting, they are accessible from the global variable $convert
86 function install_convert(&$p_master)
88 $this->p_master = &$p_master;
91 function main($mode, $sub)
93 global $lang, $template, $cache, $config, $language, $table_prefix;
94 global $convert;
96 $this->tpl_name = 'install_convert';
97 $this->mode = $mode;
99 $convert = new convert($this->p_master);
101 switch ($sub)
103 case 'intro':
104 // Try opening config file
105 // @todo If phpBB is not installed, we need to do a cut-down installation here
106 // For now, we redirect to the installation script instead
107 if (@file_exists(PHPBB_ROOT_PATH . 'config.' . PHP_EXT))
109 include(PHPBB_ROOT_PATH . 'config.' . PHP_EXT);
112 if (!defined('PHPBB_INSTALLED'))
114 $template->assign_vars(array(
115 'S_NOT_INSTALLED' => true,
116 'TITLE' => $lang['BOARD_NOT_INSTALLED'],
117 'BODY' => sprintf($lang['BOARD_NOT_INSTALLED_EXPLAIN'], append_sid('install/index', 'mode=install&amp;language=' . $language)),
120 return;
123 require(PHPBB_ROOT_PATH . 'config.' . PHP_EXT);
124 require(PHPBB_ROOT_PATH . 'includes/constants.' . PHP_EXT);
125 require(PHPBB_ROOT_PATH . 'includes/db/' . $dbms . '.' . PHP_EXT);
126 require(PHPBB_ROOT_PATH . 'includes/functions_convert.' . PHP_EXT);
128 $db = new $sql_db();
129 $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
130 unset($dbpasswd);
132 // We need to fill the config to let internal functions correctly work
133 $sql = 'SELECT *
134 FROM ' . CONFIG_TABLE;
135 $result = $db->sql_query($sql);
137 $config = array();
138 while ($row = $db->sql_fetchrow($result))
140 $config[$row['config_name']] = $row['config_value'];
142 $db->sql_freeresult($result);
144 // Detect if there is already a conversion in progress at this point and offer to resume
145 // It's quite possible that the user will get disconnected during a large conversion so they need to be able to resume it
146 $new_conversion = request_var('new_conv', 0);
148 if ($new_conversion)
150 $config['convert_progress'] = '';
151 $config['convert_db_server'] = '';
152 $config['convert_db_user'] = '';
153 $db->sql_query('DELETE FROM ' . CONFIG_TABLE . "
154 WHERE config_name = 'convert_progress'
155 OR config_name = 'convert_db_server'
156 OR config_name = 'convert_db_user'"
160 // Let's see if there is a conversion in the works...
161 $options = array();
162 if (!empty($config['convert_progress']) && !empty($config['convert_db_server']) && !empty($config['convert_db_user']) && !empty($config['convert_options']))
164 $options = unserialize($config['convert_progress']);
165 $options = array_merge($options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options']));
168 // This information should have already been checked once, but do it again for safety
169 if (!empty($options) && !empty($options['tag']) &&
170 isset($options['dbms']) &&
171 isset($options['dbhost']) &&
172 isset($options['dbport']) &&
173 isset($options['dbuser']) &&
174 isset($options['dbpasswd']) &&
175 isset($options['dbname']) &&
176 isset($options['table_prefix']))
178 $this->page_title = $lang['CONTINUE_CONVERT'];
180 $template->assign_vars(array(
181 'TITLE' => $lang['CONTINUE_CONVERT'],
182 'BODY' => $lang['CONTINUE_CONVERT_BODY'],
183 'L_NEW' => $lang['CONVERT_NEW_CONVERSION'],
184 'L_CONTINUE' => $lang['CONTINUE_OLD_CONVERSION'],
185 'S_CONTINUE' => true,
187 'U_NEW_ACTION' => $this->p_master->module_url . "?mode={$this->mode}&amp;sub=intro&amp;new_conv=1&amp;language=$language",
188 'U_CONTINUE_ACTION' => $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag={$options['tag']}{$options['step']}&amp;language=$language",
191 return;
194 $this->list_convertors($sub);
196 break;
198 case 'settings':
199 $this->get_convert_settings($sub);
200 break;
202 case 'in_progress':
203 $this->convert_data($sub);
204 break;
206 case 'final':
207 $this->page_title = $lang['CONVERT_COMPLETE'];
209 $template->assign_vars(array(
210 'TITLE' => $lang['CONVERT_COMPLETE'],
211 'BODY' => $lang['CONVERT_COMPLETE_EXPLAIN'],
214 // If we reached this step (conversion completed) we want to purge the cache and log the user out.
215 // This is for making sure the session get not screwed due to the 3.0.x users table being completely new.
216 $cache->purge();
218 require(PHPBB_ROOT_PATH . 'config.' . PHP_EXT);
219 require(PHPBB_ROOT_PATH . 'includes/constants.' . PHP_EXT);
220 require(PHPBB_ROOT_PATH . 'includes/db/' . $dbms . '.' . PHP_EXT);
221 require(PHPBB_ROOT_PATH . 'includes/functions_convert.' . PHP_EXT);
223 $db = new $sql_db();
224 $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
225 unset($dbpasswd);
227 $sql = 'SELECT config_value
228 FROM ' . CONFIG_TABLE . '
229 WHERE config_name = \'search_type\'';
230 $result = $db->sql_query($sql);
232 if ($db->sql_fetchfield('config_value') != 'fulltext_mysql')
234 $template->assign_vars(array(
235 'S_ERROR_BOX' => true,
236 'ERROR_TITLE' => $lang['SEARCH_INDEX_UNCONVERTED'],
237 'ERROR_MSG' => $lang['SEARCH_INDEX_UNCONVERTED_EXPLAIN'],
241 if ($db->truncate)
243 $db->sql_query('TRUNCATE TABLE ' . SESSIONS_KEYS_TABLE);
244 $db->sql_query('TRUNCATE TABLE ' . SESSIONS_TABLE);
246 else
248 $db->sql_query('DELETE FROM ' . SESSIONS_KEYS_TABLE);
249 $db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
252 break;
257 * Generate a list of all available conversion modules
259 function list_convertors($sub)
261 global $lang, $language, $template;
263 $this->page_title = $lang['SUB_INTRO'];
265 $template->assign_vars(array(
266 'TITLE' => $lang['CONVERT_INTRO'],
267 'BODY' => $lang['CONVERT_INTRO_BODY'],
269 'L_AUTHOR' => $lang['AUTHOR'],
270 'L_AVAILABLE_CONVERTORS' => $lang['AVAILABLE_CONVERTORS'],
271 'L_CONVERT' => $lang['CONVERT'],
272 'L_NO_CONVERTORS' => $lang['NO_CONVERTORS'],
273 'L_OPTIONS' => $lang['CONVERT_OPTIONS'],
274 'L_SOFTWARE' => $lang['SOFTWARE'],
275 'L_VERSION' => $lang['VERSION'],
277 'S_LIST' => true,
280 $convertors = $sort = array();
281 $get_info = true;
283 $handle = @opendir('./convertors/');
285 if (!$handle)
287 $this->error('Unable to access the convertors directory', __LINE__, __FILE__);
290 while ($entry = readdir($handle))
292 if (preg_match('/^convert_([a-z0-9_]+).' . PHP_EXT . '$/i', $entry, $m))
294 include('./convertors/' . $entry);
295 if (isset($convertor_data))
297 $sort[strtolower($convertor_data['forum_name'])] = sizeof($convertors);
299 $convertors[] = array(
300 'tag' => $m[1],
301 'forum_name' => $convertor_data['forum_name'],
302 'version' => $convertor_data['version'],
303 'dbms' => $convertor_data['dbms'],
304 'dbhost' => $convertor_data['dbhost'],
305 'dbport' => $convertor_data['dbport'],
306 'dbuser' => $convertor_data['dbuser'],
307 'dbpasswd' => $convertor_data['dbpasswd'],
308 'dbname' => $convertor_data['dbname'],
309 'table_prefix' => $convertor_data['table_prefix'],
310 'author' => $convertor_data['author']
313 unset($convertor_data);
316 closedir($handle);
318 @ksort($sort);
320 foreach ($sort as $void => $index)
322 $template->assign_block_vars('convertors', array(
323 'AUTHOR' => $convertors[$index]['author'],
324 'SOFTWARE' => $convertors[$index]['forum_name'],
325 'VERSION' => $convertors[$index]['version'],
327 'U_CONVERT' => $this->p_master->module_url . "?mode={$this->mode}&amp;language=$language&amp;sub=settings&amp;tag=" . $convertors[$index]['tag'],
334 function get_convert_settings($sub)
336 global $lang, $language, $template, $db, $config, $cache;
338 require(PHPBB_ROOT_PATH . 'config.' . PHP_EXT);
339 require(PHPBB_ROOT_PATH . 'includes/constants.' . PHP_EXT);
340 require(PHPBB_ROOT_PATH . 'includes/db/' . $dbms . '.' . PHP_EXT);
341 require(PHPBB_ROOT_PATH . 'includes/functions_convert.' . PHP_EXT);
343 $db = new $sql_db();
344 $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
345 unset($dbpasswd);
347 $this->page_title = $lang['STAGE_SETTINGS'];
349 // We need to fill the config to let internal functions correctly work
350 $sql = 'SELECT *
351 FROM ' . CONFIG_TABLE;
352 $result = $db->sql_query($sql);
354 $config = array();
355 while ($row = $db->sql_fetchrow($result))
357 $config[$row['config_name']] = $row['config_value'];
359 $db->sql_freeresult($result);
361 $convertor_tag = request_var('tag', '');
363 if (empty($convertor_tag))
365 $this->p_master->error($lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__);
367 $get_info = true;
369 // check security implications of direct inclusion
370 $convertor_tag = basename($convertor_tag);
371 if (!file_exists('./convertors/convert_' . $convertor_tag . '.' . PHP_EXT))
373 $this->p_master->error($lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__);
376 include('./convertors/convert_' . $convertor_tag . '.' . PHP_EXT);
378 // The test_file is a file that should be present in the location of the old board.
379 if (!isset($test_file))
381 $this->p_master->error($lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__);
384 $submit = request::is_set_post('submit');
386 $src_dbms = request_var('src_dbms', $convertor_data['dbms']);
387 $src_dbhost = request_var('src_dbhost', $convertor_data['dbhost']);
388 $src_dbport = request_var('src_dbport', $convertor_data['dbport']);
389 $src_dbuser = request_var('src_dbuser', $convertor_data['dbuser']);
390 $src_dbpasswd = request_var('src_dbpasswd', $convertor_data['dbpasswd']);
391 $src_dbname = request_var('src_dbname', $convertor_data['dbname']);
392 $src_table_prefix = request_var('src_table_prefix', $convertor_data['table_prefix']);
393 $forum_path = request_var('forum_path', $convertor_data['forum_path']);
394 $refresh = request_var('refresh', 1);
396 // Default URL of the old board
397 // @todo Are we going to use this for attempting to convert URL references in posts, or should we remove it?
398 // -> We should convert old urls to the new relative urls format
399 // $src_url = request_var('src_url', 'Not in use at the moment');
401 // strip trailing slash from old forum path
402 $forum_path = (strlen($forum_path) && $forum_path[strlen($forum_path) - 1] == '/') ? substr($forum_path, 0, -1) : $forum_path;
404 $error = array();
405 if ($submit)
407 if (!@file_exists('./../' . $forum_path . '/' . $test_file))
409 $error[] = sprintf($lang['COULD_NOT_FIND_PATH'], $forum_path);
412 $connect_test = false;
413 $available_dbms = get_available_dbms(false, true, true);
415 if (!isset($available_dbms[$src_dbms]) || !$available_dbms[$src_dbms]['AVAILABLE'])
417 $error['db'][] = $lang['INST_ERR_NO_DB'];
418 $connect_test = false;
420 else
422 $connect_test = connect_check_db(true, $error, $available_dbms[$src_dbms], $src_table_prefix, $src_dbhost, $src_dbuser, htmlspecialchars_decode($src_dbpasswd), $src_dbname, $src_dbport, true, ($src_dbms == $dbms) ? false : true, false);
425 // The forum prefix of the old and the new forum can only be the same if two different databases are used.
426 if ($src_table_prefix == $table_prefix && $src_dbms == $dbms && $src_dbhost == $dbhost && $src_dbport == $dbport && $src_dbname == $dbname)
428 $error[] = sprintf($lang['TABLE_PREFIX_SAME'], $src_table_prefix);
431 // Check table prefix
432 if (!sizeof($error))
434 // initiate database connection to old db if old and new db differ
435 global $src_db, $same_db;
436 $src_db = $same_db = false;
438 if ($src_dbms != $dbms || $src_dbhost != $dbhost || $src_dbport != $dbport || $src_dbname != $dbname || $src_dbuser != $dbuser)
440 $sql_db = 'dbal_' . $src_dbms;
441 $src_db = new $sql_db();
442 $src_db->sql_connect($src_dbhost, $src_dbuser, htmlspecialchars_decode($src_dbpasswd), $src_dbname, $src_dbport, false, true);
443 $same_db = false;
445 else
447 $src_db = $db;
448 $same_db = true;
451 $src_db->sql_return_on_error(true);
452 $db->sql_return_on_error(true);
454 // Try to select one row from the first table to see if the prefix is OK
455 $result = $src_db->sql_query_limit('SELECT * FROM ' . $src_table_prefix . $tables[0], 1);
457 if (!$result)
459 $prefixes = array();
461 $tables_existing = get_tables($src_db);
462 $tables_existing = array_map('strtolower', $tables_existing);
463 foreach ($tables_existing as $table_name)
465 compare_table($tables, $table_name, $prefixes);
467 unset($tables_existing);
469 foreach ($prefixes as $prefix => $count)
471 if ($count >= sizeof($tables))
473 $possible_prefix = $prefix;
474 break;
478 $msg = '';
479 if (!empty($convertor_data['table_prefix']))
481 $msg .= sprintf($lang['DEFAULT_PREFIX_IS'], $convertor_data['forum_name'], $convertor_data['table_prefix']);
484 if (!empty($possible_prefix))
486 $msg .= '<br />';
487 $msg .= ($possible_prefix == '*') ? $lang['BLANK_PREFIX_FOUND'] : sprintf($lang['PREFIX_FOUND'], $possible_prefix);
488 $src_table_prefix = ($possible_prefix == '*') ? '' : $possible_prefix;
491 $error[] = $msg;
493 $src_db->sql_freeresult($result);
494 $src_db->sql_return_on_error(false);
497 if (!sizeof($error))
499 // Save convertor Status
500 set_config('convert_progress', serialize(array(
501 'step' => '',
502 'table_prefix' => $src_table_prefix,
503 'tag' => $convertor_tag,
504 )), true);
505 set_config('convert_db_server', serialize(array(
506 'dbms' => $src_dbms,
507 'dbhost' => $src_dbhost,
508 'dbport' => $src_dbport,
509 'dbname' => $src_dbname,
510 )), true);
511 set_config('convert_db_user', serialize(array(
512 'dbuser' => $src_dbuser,
513 'dbpasswd' => $src_dbpasswd,
514 )), true);
516 // Save options
517 set_config('convert_options', serialize(array('forum_path' => './../' . $forum_path, 'refresh' => $refresh)), true);
519 $template->assign_block_vars('checks', array(
520 'TITLE' => $lang['VERIFY_OPTIONS'],
521 'RESULT' => $lang['CONVERT_SETTINGS_VERIFIED'],
524 $template->assign_vars(array(
525 'L_SUBMIT' => $lang['BEGIN_CONVERT'],
526 // 'S_HIDDEN' => $s_hidden_fields,
527 'U_ACTION' => $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag=$convertor_tag&amp;language=$language",
530 return;
532 else
534 $template->assign_block_vars('checks', array(
535 'TITLE' => $lang['VERIFY_OPTIONS'],
536 'RESULT' => '<b style="color:red">' . implode('<br />', $error) . '</b>',
539 } // end submit
541 foreach ($this->convert_options as $config_key => $vars)
543 if (!is_array($vars) && strpos($config_key, 'legend') === false)
545 continue;
548 if (strpos($config_key, 'legend') !== false)
550 $template->assign_block_vars('options', array(
551 'S_LEGEND' => true,
552 'LEGEND' => $lang[$vars])
555 continue;
558 $options = isset($vars['options']) ? $vars['options'] : '';
560 $template->assign_block_vars('options', array(
561 'KEY' => $config_key,
562 'TITLE' => $lang[$vars['lang']],
563 'S_EXPLAIN' => $vars['explain'],
564 'S_LEGEND' => false,
565 'TITLE_EXPLAIN' => ($vars['explain']) ? $lang[$vars['lang'] . '_EXPLAIN'] : '',
566 'CONTENT' => $this->p_master->input_field($config_key, $vars['type'], $$config_key, $options),
571 $template->assign_vars(array(
572 'TITLE' => $lang['STAGE_SETTINGS'],
573 'BODY' => $lang['CONV_OPTIONS_BODY'],
574 'L_SUBMIT' => $lang['BEGIN_CONVERT'],
575 'U_ACTION' => $this->p_master->module_url . "?mode={$this->mode}&amp;sub=settings&amp;tag=$convertor_tag&amp;language=$language",
580 * The function which does the actual work (or dispatches it to the relevant places)
582 function convert_data($sub)
584 global $template, $user, $db, $lang, $config, $cache;
585 global $convert, $convert_row, $message_parser, $skip_rows, $language;
587 require(PHPBB_ROOT_PATH . 'config.' . PHP_EXT);
588 require(PHPBB_ROOT_PATH . 'includes/constants.' . PHP_EXT);
589 require(PHPBB_ROOT_PATH . 'includes/db/' . $dbms . '.' . PHP_EXT);
590 require(PHPBB_ROOT_PATH . 'includes/functions_convert.' . PHP_EXT);
592 $db = new $sql_db();
593 $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
594 unset($dbpasswd);
596 $sql = 'SELECT *
597 FROM ' . CONFIG_TABLE;
598 $result = $db->sql_query($sql);
600 $config = array();
601 while ($row = $db->sql_fetchrow($result))
603 $config[$row['config_name']] = $row['config_value'];
605 $db->sql_freeresult($result);
607 // Override a couple of config variables for the duration
608 $config['max_quote_depth'] = 0;
610 // @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
611 $config['max_post_chars'] = 0;
613 // Set up a user as well. We _should_ have enough of a database here at this point to do this
614 // and it helps for any core code we call
615 $user->session_begin();
616 $user->page = session::extract_current_page(PHPBB_ROOT_PATH);
618 // This is a little bit of a fudge, but it allows the language entries to be available to the
619 // core code without us loading them again
620 $user->lang = &$lang;
622 $this->page_title = $user->lang['STAGE_IN_PROGRESS'];
624 $convert->options = array();
625 if (isset($config['convert_progress']))
627 $convert->options = unserialize($config['convert_progress']);
628 $convert->options = array_merge($convert->options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options']));
631 // This information should have already been checked once, but do it again for safety
632 if (empty($convert->options) || empty($convert->options['tag']) ||
633 !isset($convert->options['dbms']) ||
634 !isset($convert->options['dbhost']) ||
635 !isset($convert->options['dbport']) ||
636 !isset($convert->options['dbuser']) ||
637 !isset($convert->options['dbpasswd']) ||
638 !isset($convert->options['dbname']) ||
639 !isset($convert->options['table_prefix']))
641 $this->p_master->error($user->lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__);
644 // Make some short variables accessible, for easier referencing
645 $convert->convertor_tag = basename($convert->options['tag']);
646 $convert->src_dbms = $convert->options['dbms'];
647 $convert->src_dbhost = $convert->options['dbhost'];
648 $convert->src_dbport = $convert->options['dbport'];
649 $convert->src_dbuser = $convert->options['dbuser'];
650 $convert->src_dbpasswd = $convert->options['dbpasswd'];
651 $convert->src_dbname = $convert->options['dbname'];
652 $convert->src_table_prefix = $convert->options['table_prefix'];
654 // initiate database connection to old db if old and new db differ
655 global $src_db, $same_db;
656 $src_db = $same_db = null;
657 if ($convert->src_dbms != $dbms || $convert->src_dbhost != $dbhost || $convert->src_dbport != $dbport || $convert->src_dbname != $dbname || $convert->src_dbuser != $dbuser)
659 if ($convert->src_dbms != $dbms)
661 require(PHPBB_ROOT_PATH . 'includes/db/' . $convert->src_dbms . '.' . PHP_EXT);
663 $sql_db = 'dbal_' . $convert->src_dbms;
664 $src_db = new $sql_db();
665 $src_db->sql_connect($convert->src_dbhost, $convert->src_dbuser, htmlspecialchars_decode($convert->src_dbpasswd), $convert->src_dbname, $convert->src_dbport, false, true);
666 $same_db = false;
668 else
670 $src_db = $db;
671 $same_db = true;
674 $convert->mysql_convert = false;
675 switch ($src_db->sql_layer)
677 case 'sqlite':
678 case 'firebird':
679 $convert->src_truncate_statement = 'DELETE FROM ';
680 break;
682 // Thanks MySQL, for silently converting...
683 case 'mysql':
684 if (version_compare($src_db->sql_server_info(true), '4.1.3', '>='))
686 $convert->mysql_convert = true;
688 $convert->src_truncate_statement = 'TRUNCATE TABLE ';
689 break;
691 case 'mysqli':
692 $convert->mysql_convert = true;
693 $convert->src_truncate_statement = 'TRUNCATE TABLE ';
694 break;
696 default:
697 $convert->src_truncate_statement = 'TRUNCATE TABLE ';
698 break;
701 if ($convert->mysql_convert && !$same_db)
703 $src_db->sql_query("SET NAMES 'binary'");
706 if ($db->truncate)
708 $convert->truncate_statement = 'TRUNCATE TABLE ';
710 else
712 $convert->truncate_statement = 'DELETE FROM ';
715 $get_info = false;
717 // check security implications of direct inclusion
718 if (!file_exists('./convertors/convert_' . $convert->convertor_tag . '.' . PHP_EXT))
720 $this->p_master->error($user->lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__);
723 if (file_exists('./convertors/functions_' . $convert->convertor_tag . '.' . PHP_EXT))
725 include('./convertors/functions_' . $convert->convertor_tag . '.' . PHP_EXT);
728 $get_info = true;
729 include('./convertors/convert_' . $convert->convertor_tag . '.' . PHP_EXT);
731 // Map some variables...
732 $convert->convertor_data = $convertor_data;
733 $convert->tables = $tables;
734 $convert->config_schema = $config_schema;
736 // Now include the real data
737 $get_info = false;
738 include('./convertors/convert_' . $convert->convertor_tag . '.' . PHP_EXT);
740 $convert->convertor_data = $convertor_data;
741 $convert->tables = $tables;
742 $convert->config_schema = $config_schema;
743 $convert->convertor = $convertor;
745 // The test_file is a file that should be present in the location of the old board.
746 if (!file_exists($convert->options['forum_path'] . '/' . $test_file))
748 $this->p_master->error(sprintf($user->lang['COULD_NOT_FIND_PATH'], $convert->options['forum_path']), __LINE__, __FILE__);
751 $search_type = basename(trim($config['search_type']));
753 // For conversions we are a bit less strict and set to a search backend we know exist...
754 if (!file_exists(PHPBB_ROOT_PATH . 'includes/search/' . $search_type . '.' . PHP_EXT))
756 $search_type = 'fulltext_native';
757 set_config('search_type', $search_type);
760 if (!file_exists(PHPBB_ROOT_PATH . 'includes/search/' . $search_type . '.' . PHP_EXT))
762 trigger_error('NO_SUCH_SEARCH_MODULE');
765 require(PHPBB_ROOT_PATH . 'includes/search/' . $search_type . '.' . PHP_EXT);
767 $error = false;
768 $convert->fulltext_search = new $search_type($error);
770 if ($error)
772 trigger_error($error);
775 include(PHPBB_ROOT_PATH . 'includes/message_parser.' . PHP_EXT);
776 $message_parser = new parse_message();
778 $jump = request_var('jump', 0);
779 $final_jump = request_var('final_jump', 0);
780 $sync_batch = request_var('sync_batch', -1);
781 $last_statement = request_var('last', 0);
783 // We are running sync...
784 if ($sync_batch >= 0)
786 $this->sync_forums($sync_batch);
787 return;
790 if ($jump)
792 $this->jump($jump, $last_statement);
793 return;
796 if ($final_jump)
798 $this->final_jump($final_jump);
799 return;
802 $current_table = request_var('current_table', 0);
803 $old_current_table = min(-1, $current_table - 1);
804 $skip_rows = request_var('skip_rows', 0);
806 if (!$current_table && !$skip_rows)
808 if (!request::variable('confirm', false))
810 // If avatars / ranks / smilies folders are specified make sure they are writable
811 $bad_folders = array();
813 $local_paths = array(
814 'avatar_path' => path($config['avatar_path']),
815 'avatar_gallery_path' => path($config['avatar_gallery_path']),
816 'icons_path' => path($config['icons_path']),
817 'ranks_path' => path($config['ranks_path']),
818 'smilies_path' => path($config['smilies_path'])
821 foreach ($local_paths as $folder => $local_path)
823 if (isset($convert->convertor[$folder]))
825 if (empty($convert->convertor['test_file']))
827 // test_file is mandantory at the moment so this should never be reached, but just in case...
828 $this->p_master->error($user->lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__);
831 if (!$local_path || !@is_writable(PHPBB_ROOT_PATH . $local_path))
833 if (!$local_path)
835 $bad_folders[] = sprintf($user->lang['CONFIG_PHPBB_EMPTY'], $folder);
837 else
839 $bad_folders[] = $local_path;
845 if (sizeof($bad_folders))
847 $msg = (sizeof($bad_folders) == 1) ? $user->lang['MAKE_FOLDER_WRITABLE'] : $user->lang['MAKE_FOLDERS_WRITABLE'];
848 sort($bad_folders);
849 $this->p_master->error(sprintf($msg, implode('<br />', $bad_folders)), __LINE__, __FILE__, true);
851 $template->assign_vars(array(
852 'L_SUBMIT' => $user->lang['INSTALL_TEST'],
853 'U_ACTION' => $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag={$convert->convertor_tag}&amp;language=$language",
855 return;
858 // Grab all the tables used in convertor
859 $missing_tables = $tables_list = $aliases = array();
861 foreach ($convert->convertor['schema'] as $schema)
863 // Skip those not used (because of addons/plugins not detected)
864 if (!$schema['target'])
866 continue;
869 foreach ($schema as $key => $val)
871 // we're dealing with an array like:
872 // array('forum_status', 'forums.forum_status', 'is_item_locked')
873 if (is_int($key) && !empty($val[1]))
875 $temp_data = $val[1];
876 if (!is_array($temp_data))
878 $temp_data = array($temp_data);
881 foreach ($temp_data as $val)
883 if (preg_match('/([a-z0-9_]+)\.([a-z0-9_]+)\)* ?A?S? ?([a-z0-9_]*?)\.?([a-z0-9_]*)$/i', $val, $m))
885 $table = $convert->src_table_prefix . $m[1];
886 $tables_list[$table] = $table;
888 if (!empty($m[3]))
890 $aliases[] = $convert->src_table_prefix . $m[3];
895 // 'left_join' => 'topics LEFT JOIN vote_desc ON topics.topic_id = vote_desc.topic_id AND topics.topic_vote = 1'
896 else if ($key == 'left_join')
898 // Convert the value if it wasn't an array already.
899 if (!is_array($val))
901 $val = array($val);
904 for ($j = 0; $j < sizeof($val); ++$j)
906 if (preg_match('/LEFT JOIN ([a-z0-9_]+) AS ([a-z0-9_]+)/i', $val[$j], $m))
908 $table = $convert->src_table_prefix . $m[1];
909 $tables_list[$table] = $table;
911 if (!empty($m[2]))
913 $aliases[] = $convert->src_table_prefix . $m[2];
921 // Remove aliased tables from $tables_list
922 foreach ($aliases as $alias)
924 unset($tables_list[$alias]);
927 // Check if the tables that we need exist
928 $src_db->sql_return_on_error(true);
929 foreach ($tables_list as $table => $null)
931 $sql = 'SELECT 1 FROM ' . $table;
932 $_result = $src_db->sql_query_limit($sql, 1);
934 if (!$_result)
936 $missing_tables[] = $table;
938 $src_db->sql_freeresult($_result);
940 $src_db->sql_return_on_error(false);
942 // Throw an error if some tables are missing
943 // We used to do some guessing here, but since we have a suggestion of possible values earlier, I don't see it adding anything here to do it again
945 if (sizeof($missing_tables) == sizeof($tables_list))
947 $this->p_master->error($user->lang['NO_TABLES_FOUND'] . ' ' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
949 else if (sizeof($missing_tables))
951 $this->p_master->error(sprintf($user->lang['TABLES_MISSING'], implode(', ', $missing_tables)) . '<br /><br />' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
954 $url = $this->save_convert_progress('&amp;confirm=1');
955 $msg = $user->lang['PRE_CONVERT_COMPLETE'];
957 if ($convert->convertor_data['author_notes'])
959 $msg .= '</p><p>' . sprintf($user->lang['AUTHOR_NOTES'], $convert->convertor_data['author_notes']);
962 $template->assign_vars(array(
963 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'],
964 'L_MESSAGE' => $msg,
965 'U_ACTION' => $url,
968 return;
969 } // if (!request::variable('confirm', false))
971 $template->assign_block_vars('checks', array(
972 'S_LEGEND' => true,
973 'LEGEND' => $user->lang['STARTING_CONVERT'],
976 // Convert the config table and load the settings of the old board
977 if (!empty($convert->config_schema))
979 restore_config($convert->config_schema);
981 // Override a couple of config variables for the duration
982 $config['max_quote_depth'] = 0;
984 // @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
985 $config['max_post_chars'] = 0;
988 $template->assign_block_vars('checks', array(
989 'TITLE' => $user->lang['CONFIG_CONVERT'],
990 'RESULT' => $user->lang['DONE'],
993 // Now process queries and execute functions that have to be executed prior to the conversion
994 if (!empty($convert->convertor['execute_first']))
996 eval($convert->convertor['execute_first']);
999 if (!empty($convert->convertor['query_first']))
1001 if (!is_array($convert->convertor['query_first']))
1003 $convert->convertor['query_first'] = array('target', array($convert->convertor['query_first']));
1005 else if (!is_array($convert->convertor['query_first'][0]))
1007 $convert->convertor['query_first'] = array(array($convert->convertor['query_first'][0], $convert->convertor['query_first'][1]));
1010 foreach ($convert->convertor['query_first'] as $query_first)
1012 if ($query_first[0] == 'src')
1014 if ($convert->mysql_convert && $same_db)
1016 $src_db->sql_query("SET NAMES 'binary'");
1019 $src_db->sql_query($query_first[1]);
1021 if ($convert->mysql_convert && $same_db)
1023 $src_db->sql_query("SET NAMES 'utf8'");
1026 else
1028 $db->sql_query($query_first[1]);
1033 $template->assign_block_vars('checks', array(
1034 'TITLE' => $user->lang['PREPROCESS_STEP'],
1035 'RESULT' => $user->lang['DONE'],
1037 } // if (!$current_table && !$skip_rows)
1039 $template->assign_block_vars('checks', array(
1040 'S_LEGEND' => true,
1041 'LEGEND' => $user->lang['FILLING_TABLES'],
1044 // This loop takes one target table and processes it
1045 while ($current_table < sizeof($convert->convertor['schema']))
1047 $schema = $convert->convertor['schema'][$current_table];
1049 // The target table isn't set, this can be because a module (for example the attachement mod) is taking care of this.
1050 if (empty($schema['target']))
1052 $current_table++;
1053 continue;
1056 $template->assign_block_vars('checks', array(
1057 'TITLE' => sprintf($user->lang['FILLING_TABLE'], $schema['target']),
1060 // This is only the case when we first start working on the tables.
1061 if (!$skip_rows)
1063 // process execute_first and query_first for this table...
1064 if (!empty($schema['execute_first']))
1066 eval($schema['execute_first']);
1069 if (!empty($schema['query_first']))
1071 if (!is_array($schema['query_first']))
1073 $schema['query_first'] = array('target', array($schema['query_first']));
1075 else if (!is_array($schema['query_first'][0]))
1077 $schema['query_first'] = array(array($schema['query_first'][0], $schema['query_first'][1]));
1080 foreach ($schema['query_first'] as $query_first)
1082 if ($query_first[0] == 'src')
1084 if ($convert->mysql_convert && $same_db)
1086 $src_db->sql_query("SET NAMES 'binary'");
1088 $src_db->sql_query($query_first[1]);
1089 if ($convert->mysql_convert && $same_db)
1091 $src_db->sql_query("SET NAMES 'utf8'");
1094 else
1096 $db->sql_query($query_first[1]);
1101 if (!empty($schema['autoincrement']))
1103 switch ($db->dbms_type)
1105 case 'postgres':
1106 $db->sql_query("SELECT SETVAL('" . $schema['target'] . "_seq',(select case when max(" . $schema['autoincrement'] . ")>0 then max(" . $schema['autoincrement'] . ")+1 else 1 end from " . $schema['target'] . '));');
1107 break;
1109 case 'oracle':
1110 $result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
1111 $row = $db->sql_fetchrow($result);
1112 $db->sql_freeresult($result);
1114 $largest_id = (int) $row['max_id'];
1116 if ($largest_id)
1118 $db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
1119 $db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
1121 break;
1126 // Process execute_always for this table
1127 // This is for code which needs to be executed on every pass of this table if
1128 // it gets split because of time restrictions
1129 if (!empty($schema['execute_always']))
1131 eval($schema['execute_always']);
1135 // Set up some variables
1137 // $waiting_rows holds rows for multirows insertion (MySQL only)
1138 // $src_tables holds unique tables with aliases to select from
1139 // $src_fields will quickly refer source fields (or aliases) corresponding to the current index
1140 // $select_fields holds the names of the fields to retrieve
1143 $sql_data = array(
1144 'source_fields' => array(),
1145 'target_fields' => array(),
1146 'source_tables' => array(),
1147 'select_fields' => array(),
1150 // This statement is building the keys for later insertion.
1151 $insert_query = $this->build_insert_query($schema, $sql_data, $current_table);
1153 // If no source table is affected, we skip the table
1154 if (empty($sql_data['source_tables']))
1156 $skip_rows = 0;
1157 $current_table++;
1158 continue;
1161 $distinct = (!empty($schema['distinct'])) ? 'DISTINCT ' : '';
1163 $sql = 'SELECT ' . $distinct . implode(', ', $sql_data['select_fields']) . " \nFROM " . implode(', ', $sql_data['source_tables']);
1165 // Where
1166 $sql .= (!empty($schema['where'])) ? "\nWHERE (" . $schema['where'] . ')' : '';
1168 // Group By
1169 if (!empty($schema['group_by']))
1171 $schema['group_by'] = array($schema['group_by']);
1172 foreach ($sql_data['select_fields'] as $select)
1174 $alias = strpos(strtolower($select), ' as ');
1175 $select = ($alias) ? substr($select, 0, $alias) : $select;
1176 if (!in_array($select, $schema['group_by']))
1178 $schema['group_by'][] = $select;
1182 $sql .= (!empty($schema['group_by'])) ? "\nGROUP BY " . implode(', ', $schema['group_by']) : '';
1184 // Having
1185 $sql .= (!empty($schema['having'])) ? "\nHAVING " . $schema['having'] : '';
1187 // Order By
1188 if (empty($schema['order_by']) && !empty($schema['primary']))
1190 $schema['order_by'] = $schema['primary'];
1192 $sql .= (!empty($schema['order_by'])) ? "\nORDER BY " . $schema['order_by'] : '';
1194 // Counting basically holds the amount of rows processed.
1195 $counting = -1;
1196 $batch_time = 0;
1198 while ($counting === -1 || ($counting >= $convert->batch_size && still_on_time()))
1200 $old_current_table = $current_table;
1202 $rows = '';
1203 $waiting_rows = array();
1205 if (!empty($batch_time))
1207 $mtime = explode(' ', microtime());
1208 $mtime = $mtime[0] + $mtime[1];
1209 $rows = ceil($counting/($mtime - $batch_time)) . " rows/s ($counting rows) | ";
1212 $template->assign_block_vars('checks', array(
1213 'TITLE' => "skip_rows = $skip_rows",
1214 'RESULT' => $rows . ((defined('DEBUG_EXTRA') && function_exists('memory_get_usage')) ? ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] : ''),
1217 $mtime = explode(' ', microtime());
1218 $batch_time = $mtime[0] + $mtime[1];
1220 if ($convert->mysql_convert && $same_db)
1222 $src_db->sql_query("SET NAMES 'binary'");
1225 // Take skip rows into account and only fetch batch_size amount of rows
1226 $___result = $src_db->sql_query_limit($sql, $convert->batch_size, $skip_rows);
1228 if ($convert->mysql_convert && $same_db)
1230 $src_db->sql_query("SET NAMES 'utf8'");
1233 // This loop processes each row
1234 $counting = 0;
1236 $convert->row = $convert_row = array();
1238 if (!empty($schema['autoincrement']))
1240 switch ($db->dbms_type)
1242 case 'mssql':
1243 $db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' ON');
1244 break;
1248 // Now handle the rows until time is over or no more rows to process...
1249 while ($counting === 0 || still_on_time())
1251 $convert_row = $src_db->sql_fetchrow($___result);
1253 if (!$convert_row)
1255 // move to the next batch or table
1256 break;
1259 // With this we are able to always save the last state
1260 $convert->row = $convert_row;
1262 // Increment the counting variable, it stores the number of rows we have processed
1263 $counting++;
1265 $insert_values = array();
1267 $sql_flag = $this->process_row($schema, $sql_data, $insert_values);
1269 if ($sql_flag === true)
1271 switch ($db->dbms_type)
1273 // If MySQL, we'll wait to have num_wait_rows rows to submit at once
1274 case 'mysql':
1275 $waiting_rows[] = '(' . implode(', ', $insert_values) . ')';
1277 if (sizeof($waiting_rows) >= $convert->num_wait_rows)
1279 $errored = false;
1281 $db->sql_return_on_error(true);
1283 if (!$db->sql_query($insert_query . implode(', ', $waiting_rows)))
1285 $errored = true;
1287 $db->sql_return_on_error(false);
1289 if ($errored)
1291 $db->sql_return_on_error(true);
1293 // Because it errored out we will try to insert the rows one by one... most of the time this
1294 // is caused by duplicate entries - but we also do not want to miss one...
1295 foreach ($waiting_rows as $waiting_sql)
1297 if (!$db->sql_query($insert_query . $waiting_sql))
1299 $this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
1303 $db->sql_return_on_error(false);
1306 $waiting_rows = array();
1309 break;
1311 default:
1312 $insert_sql = $insert_query . '(' . implode(', ', $insert_values) . ')';
1314 $db->sql_return_on_error(true);
1316 if (!$db->sql_query($insert_sql))
1318 $this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
1320 $db->sql_return_on_error(false);
1322 $waiting_rows = array();
1324 break;
1328 $skip_rows++;
1330 $src_db->sql_freeresult($___result);
1332 // We might still have some rows waiting
1333 if (sizeof($waiting_rows))
1335 $errored = false;
1336 $db->sql_return_on_error(true);
1338 if (!$db->sql_query($insert_query . implode(', ', $waiting_rows)))
1340 $errored = true;
1342 $db->sql_return_on_error(false);
1344 if ($errored)
1346 $db->sql_return_on_error(true);
1348 // Because it errored out we will try to insert the rows one by one... most of the time this
1349 // is caused by duplicate entries - but we also do not want to miss one...
1350 foreach ($waiting_rows as $waiting_sql)
1352 $db->sql_query($insert_query . $waiting_sql);
1353 $this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
1356 $db->sql_return_on_error(false);
1359 $waiting_rows = array();
1362 if (!empty($schema['autoincrement']))
1364 switch ($db->dbms_type)
1366 case 'mssql':
1367 $db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' OFF');
1368 break;
1370 case 'postgres':
1371 $db->sql_query("SELECT SETVAL('" . $schema['target'] . "_seq',(select case when max(" . $schema['autoincrement'] . ")>0 then max(" . $schema['autoincrement'] . ")+1 else 1 end from " . $schema['target'] . '));');
1372 break;
1374 case 'oracle':
1375 $result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
1376 $row = $db->sql_fetchrow($result);
1377 $db->sql_freeresult($result);
1379 $largest_id = (int) $row['max_id'];
1381 if ($largest_id)
1383 $db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
1384 $db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
1386 break;
1391 // When we reach this point, either the current table has been processed or we're running out of time.
1392 if (still_on_time() && $counting < $convert->batch_size/* && !defined('DEBUG_EXTRA')*/)
1394 $skip_rows = 0;
1395 $current_table++;
1397 else
1399 if (still_on_time() && $counting < $convert->batch_size)
1401 $skip_rows = 0;
1402 $current_table++;
1405 // Looks like we ran out of time.
1406 $url = $this->save_convert_progress('&amp;current_table=' . $current_table . '&amp;skip_rows=' . $skip_rows);
1408 $current_table++;
1409 // $percentage = ($skip_rows == 0) ? 0 : floor(100 / ($total_rows / $skip_rows));
1411 $msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $current_table, sizeof($convert->convertor['schema']));
1413 $template->assign_vars(array(
1414 'L_MESSAGE' => $msg,
1415 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'],
1416 'U_ACTION' => $url,
1419 $this->meta_refresh($url);
1420 return;
1424 // Process execute_last then we'll be done
1425 $url = $this->save_convert_progress('&amp;jump=1');
1427 $template->assign_vars(array(
1428 'L_SUBMIT' => $user->lang['FINAL_STEP'],
1429 'U_ACTION' => $url,
1432 $this->meta_refresh($url);
1433 return;
1437 * Sync function being executed at the middle, some functions need to be executed after a successful sync.
1439 function sync_forums($sync_batch)
1441 global $template, $user, $db, $config, $cache;
1442 global $convert;
1444 $template->assign_block_vars('checks', array(
1445 'S_LEGEND' => true,
1446 'LEGEND' => $user->lang['SYNC_TOPICS'],
1449 $batch_size = $convert->batch_size;
1451 $sql = 'SELECT MIN(topic_id) as min_value, MAX(topic_id) AS max_value
1452 FROM ' . TOPICS_TABLE;
1453 $result = $db->sql_query($sql);
1454 $row = $db->sql_fetchrow($result);
1455 $db->sql_freeresult($result);
1457 // Set values of minimum/maximum primary value for this table.
1458 $primary_min = $row['min_value'];
1459 $primary_max = $row['max_value'];
1461 if ($sync_batch == 0)
1463 $sync_batch = (int) $primary_min;
1466 if ($sync_batch == 0)
1468 $sync_batch = 1;
1471 // Fetch a batch of rows, process and insert them.
1472 while ($sync_batch <= $primary_max && still_on_time())
1474 $end = ($sync_batch + $batch_size - 1);
1476 // Sync all topics in batch mode...
1477 sync('topic_approved', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, false);
1478 sync('topic', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, true);
1480 $template->assign_block_vars('checks', array(
1481 'TITLE' => sprintf($user->lang['SYNC_TOPIC_ID'], $sync_batch, ($sync_batch + $batch_size)) . ((defined('DEBUG_EXTRA') && function_exists('memory_get_usage')) ? ' [' . ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] . ']' : ''),
1482 'RESULT' => $user->lang['DONE'],
1485 $sync_batch += $batch_size;
1488 if ($sync_batch >= $primary_max)
1490 $url = $this->save_convert_progress('&amp;final_jump=1');
1492 $template->assign_vars(array(
1493 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'],
1494 'U_ACTION' => $url,
1497 $this->meta_refresh($url);
1498 return;
1500 else
1502 $sync_batch--;
1505 $url = $this->save_convert_progress('&amp;sync_batch=' . $sync_batch);
1507 $template->assign_vars(array(
1508 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'],
1509 'U_ACTION' => $url,
1512 $this->meta_refresh($url);
1513 return;
1517 * Save the convertor status
1519 function save_convert_progress($step)
1521 global $convert, $language;
1523 // Save convertor Status
1524 set_config('convert_progress', serialize(array(
1525 'step' => $step,
1526 'table_prefix' => $convert->src_table_prefix,
1527 'tag' => $convert->convertor_tag,
1528 )), true);
1530 set_config('convert_db_server', serialize(array(
1531 'dbms' => $convert->src_dbms,
1532 'dbhost' => $convert->src_dbhost,
1533 'dbport' => $convert->src_dbport,
1534 'dbname' => $convert->src_dbname,
1535 )), true);
1537 set_config('convert_db_user', serialize(array(
1538 'dbuser' => $convert->src_dbuser,
1539 'dbpasswd' => $convert->src_dbpasswd,
1540 )), true);
1542 return $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag={$convert->convertor_tag}$step&amp;language=$language";
1546 * Finish conversion, the last function to be called.
1548 function finish_conversion()
1550 global $db, $convert, $config, $language, $user, $template;
1552 $db->sql_query('DELETE FROM ' . CONFIG_TABLE . "
1553 WHERE config_name = 'convert_progress'
1554 OR config_name = 'convert_options'
1555 OR config_name = 'convert_db_server'
1556 OR config_name = 'convert_db_user'");
1557 $db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
1559 @unlink(PHPBB_ROOT_PATH . 'cache/data_global.php');
1560 cache_moderators();
1562 // And finally, add a note to the log
1563 add_log('admin', 'LOG_INSTALL_CONVERTED', $convert->convertor_data['forum_name'], $config['version']);
1565 $url = $this->p_master->module_url . "?mode={$this->mode}&amp;sub=final&amp;language=$language";
1567 $template->assign_vars(array(
1568 'L_SUBMIT' => $user->lang['FINAL_STEP'],
1569 'U_ACTION' => $url,
1572 $this->meta_refresh($url);
1573 return;
1577 * This function marks the steps after syncing
1579 function final_jump($final_jump)
1581 global $template, $user, $src_db, $same_db, $db, $config, $cache;
1582 global $convert;
1584 $template->assign_block_vars('checks', array(
1585 'S_LEGEND' => true,
1586 'LEGEND' => $user->lang['PROCESS_LAST'],
1589 if ($final_jump == 1)
1591 $db->sql_return_on_error(true);
1593 update_topics_posted();
1595 $template->assign_block_vars('checks', array(
1596 'TITLE' => $user->lang['UPDATE_TOPICS_POSTED'],
1597 'RESULT' => $user->lang['DONE'],
1600 if ($db->sql_error_triggered)
1602 $template->assign_vars(array(
1603 'S_ERROR_BOX' => true,
1604 'ERROR_TITLE' => $user->lang['UPDATE_TOPICS_POSTED'],
1605 'ERROR_MSG' => $user->lang['UPDATE_TOPICS_POSTED_ERR'],
1608 $db->sql_return_on_error(false);
1610 $this->finish_conversion();
1611 return;
1616 * This function marks the steps before syncing (jump=1)
1618 function jump($jump, $last_statement)
1620 global $template, $user, $src_db, $same_db, $db, $config, $cache;
1621 global $convert;
1623 $template->assign_block_vars('checks', array(
1624 'S_LEGEND' => true,
1625 'LEGEND' => $user->lang['PROCESS_LAST'],
1628 if ($jump == 1)
1630 // Execute 'last' statements/queries
1631 if (!empty($convert->convertor['execute_last']))
1633 if (!is_array($convert->convertor['execute_last']))
1635 eval($convert->convertor['execute_last']);
1637 else
1639 while ($last_statement < sizeof($convert->convertor['execute_last']))
1641 eval($convert->convertor['execute_last'][$last_statement]);
1643 $template->assign_block_vars('checks', array(
1644 'TITLE' => $convert->convertor['execute_last'][$last_statement],
1645 'RESULT' => $user->lang['DONE'],
1648 $last_statement++;
1649 $url = $this->save_convert_progress('&amp;jump=1&amp;last=' . $last_statement);
1651 $percentage = ($last_statement == 0) ? 0 : floor(100 / (sizeof($convert->convertor['execute_last']) / $last_statement));
1652 $msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $last_statement, sizeof($convert->convertor['execute_last']), $percentage);
1654 $template->assign_vars(array(
1655 'L_SUBMIT' => $user->lang['CONTINUE_LAST'],
1656 'L_MESSAGE' => $msg,
1657 'U_ACTION' => $url,
1660 $this->meta_refresh($url);
1661 return;
1666 if (!empty($convert->convertor['query_last']))
1668 if (!is_array($convert->convertor['query_last']))
1670 $convert->convertor['query_last'] = array('target', array($convert->convertor['query_last']));
1672 else if (!is_array($convert->convertor['query_last'][0]))
1674 $convert->convertor['query_last'] = array(array($convert->convertor['query_last'][0], $convert->convertor['query_last'][1]));
1677 foreach ($convert->convertor['query_last'] as $query_last)
1679 if ($query_last[0] == 'src')
1681 if ($convert->mysql_convert && $same_db)
1683 $src_db->sql_query("SET NAMES 'binary'");
1686 $src_db->sql_query($query_last[1]);
1688 if ($convert->mysql_convert && $same_db)
1690 $src_db->sql_query("SET NAMES 'utf8'");
1693 else
1695 $db->sql_query($query_last[1]);
1700 // Sanity check
1701 $db->sql_return_on_error(false);
1702 $src_db->sql_return_on_error(false);
1704 fix_empty_primary_groups();
1706 if (!isset($config['board_startdate']))
1708 $sql = 'SELECT MIN(user_regdate) AS board_startdate
1709 FROM ' . USERS_TABLE;
1710 $result = $db->sql_query($sql);
1711 $row = $db->sql_fetchrow($result);
1712 $db->sql_freeresult($result);
1714 if (($row['board_startdate'] < $config['board_startdate'] && $row['board_startdate'] > 0) || !isset($config['board_startdate']))
1716 set_config('board_startdate', $row['board_startdate']);
1717 $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_regdate = ' . $row['board_startdate'] . ' WHERE user_id = ' . ANONYMOUS);
1721 update_dynamic_config();
1723 $template->assign_block_vars('checks', array(
1724 'TITLE' => $user->lang['CLEAN_VERIFY'],
1725 'RESULT' => $user->lang['DONE'],
1728 $url = $this->save_convert_progress('&amp;jump=2');
1730 $template->assign_vars(array(
1731 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'],
1732 'U_ACTION' => $url,
1735 $this->meta_refresh($url);
1736 return;
1739 if ($jump == 2)
1741 $db->sql_query('UPDATE ' . USERS_TABLE . " SET user_permissions = ''");
1743 // TODO: sync() is likely going to bomb out on forums with a considerable amount of topics.
1744 // TODO: the sync function is able to handle FROM-TO values, we should use them here (batch processing)
1745 sync('forum', '', '', false, true);
1746 $cache->destroy('sql', FORUMS_TABLE);
1748 $template->assign_block_vars('checks', array(
1749 'TITLE' => $user->lang['SYNC_FORUMS'],
1750 'RESULT' => $user->lang['DONE'],
1753 // Continue with synchronizing the forums...
1754 $url = $this->save_convert_progress('&amp;sync_batch=0');
1756 $template->assign_vars(array(
1757 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'],
1758 'U_ACTION' => $url,
1761 $this->meta_refresh($url);
1762 return;
1766 function build_insert_query(&$schema, &$sql_data, $current_table)
1768 global $db, $user;
1769 global $convert;
1771 // Can we use IGNORE with this DBMS?
1772 $sql_ignore = ($db->dbms_type === 'mysql' && !defined('DEBUG_EXTRA')) ? 'IGNORE ' : '';
1773 $insert_query = 'INSERT ' . $sql_ignore . 'INTO ' . $schema['target'] . ' (';
1775 $aliases = array();
1777 $sql_data = array(
1778 'source_fields' => array(),
1779 'target_fields' => array(),
1780 'source_tables' => array(),
1781 'select_fields' => array(),
1784 foreach ($schema as $key => $val)
1786 // Example: array('group_name', 'extension_groups.group_name', 'htmlspecialchars'),
1787 if (is_int($key))
1789 if (!empty($val[0]))
1791 // Target fields
1792 $sql_data['target_fields'][$val[0]] = $key;
1793 $insert_query .= $val[0] . ', ';
1796 if (!is_array($val[1]))
1798 $val[1] = array($val[1]);
1801 foreach ($val[1] as $valkey => $value_1)
1803 // This should cover about any case:
1805 // table.field => SELECT table.field FROM table
1806 // table.field AS alias => SELECT table.field AS alias FROM table
1807 // table.field AS table2.alias => SELECT table2.field AS alias FROM table table2
1808 // table.field AS table2.field => SELECT table2.field FROM table table2
1810 if (preg_match('/^([a-z0-9_]+)\.([a-z0-9_]+)( +AS +(([a-z0-9_]+?)\.)?([a-z0-9_]+))?$/i', $value_1, $m))
1812 // There is 'AS ...' in the field names
1813 if (!empty($m[3]))
1815 $value_1 = ($m[2] == $m[6]) ? $m[1] . '.' . $m[2] : $m[1] . '.' . $m[2] . ' AS ' . $m[6];
1817 // Table alias: store it then replace the source table with it
1818 if (!empty($m[5]) && $m[5] != $m[1])
1820 $aliases[$m[5]] = $m[1];
1821 $value_1 = str_replace($m[1] . '.' . $m[2], $m[5] . '.' . $m[2], $value_1);
1824 else
1826 // No table alias
1827 $sql_data['source_tables'][$m[1]] = (empty($convert->src_table_prefix)) ? $m[1] : $convert->src_table_prefix . $m[1] . ' ' . $m[1];
1830 $sql_data['select_fields'][$value_1] = $value_1;
1831 $sql_data['source_fields'][$key][$valkey] = (!empty($m[6])) ? $m[6] : $m[2];
1835 else if ($key == 'where' || $key == 'group_by' || $key == 'order_by' || $key == 'having')
1837 if (@preg_match_all('/([a-z0-9_]+)\.([a-z0-9_]+)/i', $val, $m))
1839 foreach ($m[1] as $value)
1841 $sql_data['source_tables'][$value] = (empty($convert->src_table_prefix)) ? $value : $convert->src_table_prefix . $value . ' ' . $value;
1847 // Add the aliases to the list of tables
1848 foreach ($aliases as $alias => $table)
1850 $sql_data['source_tables'][$alias] = $convert->src_table_prefix . $table . ' ' . $alias;
1853 // 'left_join' => 'forums LEFT JOIN forum_prune ON forums.forum_id = forum_prune.forum_id',
1854 if (!empty($schema['left_join']))
1856 if (!is_array($schema['left_join']))
1858 $schema['left_join'] = array($schema['left_join']);
1861 foreach ($schema['left_join'] as $left_join)
1863 // This won't handle concatened LEFT JOINs
1864 if (!preg_match('/([a-z0-9_]+) LEFT JOIN ([a-z0-9_]+) A?S? ?([a-z0-9_]*?) ?(ON|USING)(.*)/i', $left_join, $m))
1866 $this->p_master->error(sprintf($user->lang['NOT_UNDERSTAND'], 'LEFT JOIN', $left_join, $current_table, $schema['target']), __LINE__, __FILE__);
1869 if (!empty($aliases[$m[2]]))
1871 if (!empty($m[3]))
1873 $this->p_master->error(sprintf($user->lang['NAMING_CONFLICT'], $m[2], $m[3], $schema['left_join']), __LINE__, __FILE__);
1876 $m[2] = $aliases[$m[2]];
1877 $m[3] = $m[2];
1880 $right_table = $convert->src_table_prefix . $m[2];
1881 if (!empty($m[3]))
1883 unset($sql_data['source_tables'][$m[3]]);
1885 else if ($m[2] != $m[1])
1887 unset($sql_data['source_tables'][$m[2]]);
1890 if (strpos($sql_data['source_tables'][$m[1]], "\nLEFT JOIN") !== false)
1892 $sql_data['source_tables'][$m[1]] = '(' . $sql_data['source_tables'][$m[1]] . ")\nLEFT JOIN $right_table";
1894 else
1896 $sql_data['source_tables'][$m[1]] .= "\nLEFT JOIN $right_table";
1899 if (!empty($m[3]))
1901 unset($sql_data['source_tables'][$m[3]]);
1902 $sql_data['source_tables'][$m[1]] .= ' AS ' . $m[3];
1904 else if (!empty($convert->src_table_prefix))
1906 $sql_data['source_tables'][$m[1]] .= ' AS ' . $m[2];
1908 $sql_data['source_tables'][$m[1]] .= ' ' . $m[4] . $m[5];
1912 // Remove ", " from the end of the insert query
1913 $insert_query = substr($insert_query, 0, -2) . ') VALUES ';
1915 return $insert_query;
1919 * Function for processing the currently handled row
1921 function process_row(&$schema, &$sql_data, &$insert_values)
1923 global $template, $user, $db, $lang, $config, $cache;
1924 global $convert, $convert_row;
1926 $sql_flag = false;
1928 foreach ($schema as $key => $fields)
1930 // We are only interested in the lines with:
1931 // array('comment', 'attachments_desc.comment', 'htmlspecialchars'),
1932 if (is_int($key))
1934 if (!is_array($fields[1]))
1936 $fields[1] = array($fields[1]);
1939 $firstkey_set = false;
1940 $firstkey = 0;
1942 foreach ($fields[1] as $inner_key => $inner_value)
1944 if (!$firstkey_set)
1946 $firstkey = $inner_key;
1947 $firstkey_set = true;
1950 $src_field = isset($sql_data['source_fields'][$key][$inner_key]) ? $sql_data['source_fields'][$key][$inner_key] : '';
1952 if (!empty($src_field))
1954 $fields[1][$inner_key] = $convert->row[$src_field];
1958 if (!empty($fields[0]))
1960 // We have a target field, if we haven't set $sql_flag yet it will be set to TRUE.
1961 // If a function has already set it to FALSE it won't change it.
1962 if ($sql_flag === false)
1964 $sql_flag = true;
1967 // No function assigned?
1968 if (empty($fields[2]))
1970 $value = $fields[1][$firstkey];
1972 else if (is_array($fields[2]))
1974 // Execute complex function/eval/typecast
1975 $value = $fields[1];
1977 foreach ($fields[2] as $type => $execution)
1979 if (strpos($type, 'typecast') === 0)
1981 if (!is_array($value))
1983 $value = array($value);
1985 $value = $value[0];
1986 settype($value, $execution);
1988 else if (strpos($type, 'function') === 0)
1990 if (!is_array($value))
1992 $value = array($value);
1995 $value = call_user_func_array($execution, $value);
1997 else if (strpos($type, 'execute') === 0)
1999 if (!is_array($value))
2001 $value = array($value);
2004 $execution = str_replace('{RESULT}', '$value', $execution);
2005 $execution = str_replace('{VALUE}', '$value', $execution);
2006 eval($execution);
2010 else
2012 $value = call_user_func_array($fields[2], $fields[1]);
2015 if (is_null($value))
2017 $value = '';
2020 $insert_values[] = $db->_sql_validate_value($value);
2022 else if (!empty($fields[2]))
2024 if (is_array($fields[2]))
2026 // Execute complex function/eval/typecast
2027 $value = '';
2029 foreach ($fields[2] as $type => $execution)
2031 if (strpos($type, 'typecast') === 0)
2033 $value = settype($value, $execution);
2035 else if (strpos($type, 'function') === 0)
2037 if (!is_array($value))
2039 $value = array($value);
2042 $value = call_user_func_array($execution, $value);
2044 else if (strpos($type, 'execute') === 0)
2046 if (!is_array($value))
2048 $value = array($value);
2051 $execution = str_replace('{RESULT}', '$value', $execution);
2052 $execution = str_replace('{VALUE}', '$value', $execution);
2053 eval($execution);
2057 else
2059 call_user_func_array($fields[2], $fields[1]);
2065 return $sql_flag;
2069 * Own meta refresh function to be able to change the global time used
2071 function meta_refresh($url)
2073 global $convert, $template;
2075 if ($convert->options['refresh'])
2077 // Because we should not rely on correct settings, we simply use the relative path here directly.
2078 $template->assign_vars(array(
2079 'S_REFRESH' => true,
2080 'META' => '<meta http-equiv="refresh" content="5;url=' . $url . '" />')
2086 * The information below will be used to build the input fields presented to the user
2088 var $convert_options = array(
2089 'legend1' => 'SPECIFY_OPTIONS',
2090 'src_dbms' => array('lang' => 'DBMS', 'type' => 'select', 'options' => 'dbms_select(\'{VALUE}\', true)', 'explain' => false),
2091 'src_dbhost' => array('lang' => 'DB_HOST', 'type' => 'text:25:100', 'explain' => true),
2092 'src_dbport' => array('lang' => 'DB_PORT', 'type' => 'text:25:100', 'explain' => true),
2093 'src_dbname' => array('lang' => 'DB_NAME', 'type' => 'text:25:100', 'explain' => false),
2094 'src_dbuser' => array('lang' => 'DB_USERNAME', 'type' => 'text:25:100', 'explain' => false),
2095 'src_dbpasswd' => array('lang' => 'DB_PASSWORD', 'type' => 'password:25:100', 'explain' => false),
2096 'src_table_prefix' => array('lang' => 'TABLE_PREFIX', 'type' => 'text:25:100', 'explain' => false),
2097 //'src_url' => array('lang' => 'FORUM_ADDRESS', 'type' => 'text:50:100', 'explain' => true),
2098 'forum_path' => array('lang' => 'FORUM_PATH', 'type' => 'text:25:100', 'explain' => true),
2099 'refresh' => array('lang' => 'REFRESH_PAGE', 'type' => 'radio:yes_no', 'explain' => true),