Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 721 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| convertor | |
0.00% |
0 / 721 |
|
0.00% |
0 / 12 |
57840 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| convert_data | |
0.00% |
0 / 399 |
|
0.00% |
0 / 1 |
19740 | |||
| sync_forums | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
72 | |||
| save_convert_progress | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| finish_conversion | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 | |||
| final_jump | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
| jump | |
0.00% |
0 / 79 |
|
0.00% |
0 / 1 |
380 | |||
| build_insert_query | |
0.00% |
0 / 60 |
|
0.00% |
0 / 1 |
1056 | |||
| process_row | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
992 | |||
| meta_refresh | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| error | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| db_error | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\convert; |
| 15 | |
| 16 | use phpbb\install\controller\helper; |
| 17 | use phpbb\template\template; |
| 18 | |
| 19 | /** |
| 20 | * Convertor backend class |
| 21 | * |
| 22 | * WARNING: This file did not meant to be present in a production environment, so moving this file to a location which |
| 23 | * is accessible after board installation might lead to security issues. |
| 24 | */ |
| 25 | class convertor |
| 26 | { |
| 27 | /** |
| 28 | * @var helper |
| 29 | */ |
| 30 | protected $controller_helper; |
| 31 | |
| 32 | /** |
| 33 | * @var \phpbb\filesystem\filesystem |
| 34 | */ |
| 35 | protected $filesystem; |
| 36 | |
| 37 | /** |
| 38 | * @var \phpbb\template\template |
| 39 | */ |
| 40 | protected $template; |
| 41 | |
| 42 | /** |
| 43 | * Constructor |
| 44 | * |
| 45 | * @param template $template |
| 46 | * @param helper $controller_helper |
| 47 | */ |
| 48 | public function __construct(template $template, helper $controller_helper) |
| 49 | { |
| 50 | global $convert, $phpbb_filesystem; |
| 51 | |
| 52 | $this->template = $template; |
| 53 | $this->filesystem = $phpbb_filesystem; |
| 54 | $this->controller_helper = $controller_helper; |
| 55 | |
| 56 | $convert = new convert($this); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * The function which does the actual work (or dispatches it to the relevant places) |
| 61 | */ |
| 62 | function convert_data($converter) |
| 63 | { |
| 64 | global $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache, $auth; |
| 65 | global $convert, $convert_row, $message_parser, $skip_rows, $language; |
| 66 | global $request, $phpbb_dispatcher, $phpbb_container; |
| 67 | |
| 68 | $phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx); |
| 69 | extract($phpbb_config_php_file->get_all()); |
| 70 | |
| 71 | require_once($phpbb_root_path . 'includes/constants.' . $phpEx); |
| 72 | require_once($phpbb_root_path . 'includes/functions_convert.' . $phpEx); |
| 73 | |
| 74 | $dbms = \phpbb\config_php_file::convert_30_dbms_to_31($dbms); |
| 75 | |
| 76 | /** @var \phpbb\db\driver\driver_interface $db */ |
| 77 | $db = new $dbms(); |
| 78 | $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true); |
| 79 | unset($dbpasswd); |
| 80 | |
| 81 | // We need to fill the config to let internal functions correctly work |
| 82 | $config = new \phpbb\config\db($db, new \phpbb\cache\driver\dummy, CONFIG_TABLE); |
| 83 | |
| 84 | // Override a couple of config variables for the duration |
| 85 | $config['max_quote_depth'] = 0; |
| 86 | |
| 87 | // @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues |
| 88 | $config['max_post_chars'] = $config['min_post_chars'] = 0; |
| 89 | |
| 90 | // Set up a user as well. We _should_ have enough of a database here at this point to do this |
| 91 | // and it helps for any core code we call |
| 92 | $user->session_begin(); |
| 93 | $user->page = $user->extract_current_page($phpbb_root_path); |
| 94 | |
| 95 | $convert->options = array(); |
| 96 | if (isset($config['convert_progress'])) |
| 97 | { |
| 98 | $convert->options = unserialize($config['convert_progress']); |
| 99 | $convert->options = array_merge($convert->options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options'])); |
| 100 | } |
| 101 | |
| 102 | // This information should have already been checked once, but do it again for safety |
| 103 | if (empty($convert->options) || empty($convert->options['tag']) || |
| 104 | !isset($convert->options['dbms']) || |
| 105 | !isset($convert->options['dbhost']) || |
| 106 | !isset($convert->options['dbport']) || |
| 107 | !isset($convert->options['dbuser']) || |
| 108 | !isset($convert->options['dbpasswd']) || |
| 109 | !isset($convert->options['dbname']) || |
| 110 | !isset($convert->options['table_prefix'])) |
| 111 | { |
| 112 | $this->error($user->lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__); |
| 113 | } |
| 114 | |
| 115 | $this->template->assign_var('S_CONV_IN_PROGRESS', true); |
| 116 | |
| 117 | // Make some short variables accessible, for easier referencing |
| 118 | $convert->convertor_tag = basename($convert->options['tag']); |
| 119 | $convert->src_dbms = $convert->options['dbms']; |
| 120 | $convert->src_dbhost = $convert->options['dbhost']; |
| 121 | $convert->src_dbport = $convert->options['dbport']; |
| 122 | $convert->src_dbuser = $convert->options['dbuser']; |
| 123 | $convert->src_dbpasswd = $convert->options['dbpasswd']; |
| 124 | $convert->src_dbname = $convert->options['dbname']; |
| 125 | $convert->src_table_prefix = $convert->options['table_prefix']; |
| 126 | |
| 127 | // initiate database connection to old db if old and new db differ |
| 128 | global $src_db, $same_db; |
| 129 | $src_db = $same_db = null; |
| 130 | if ($convert->src_dbms != $dbms || $convert->src_dbhost != $dbhost || $convert->src_dbport != $dbport || $convert->src_dbname != $dbname || $convert->src_dbuser != $dbuser) |
| 131 | { |
| 132 | $dbms = $convert->src_dbms; |
| 133 | /** @var \phpbb\db\driver\driver $src_db */ |
| 134 | $src_db = new $dbms(); |
| 135 | $src_db->sql_connect($convert->src_dbhost, $convert->src_dbuser, htmlspecialchars_decode($convert->src_dbpasswd, ENT_COMPAT), $convert->src_dbname, $convert->src_dbport, false, true); |
| 136 | $same_db = false; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | $src_db = $db; |
| 141 | $same_db = true; |
| 142 | } |
| 143 | |
| 144 | $convert->mysql_convert = false; |
| 145 | switch ($src_db->sql_layer) |
| 146 | { |
| 147 | case 'sqlite3': |
| 148 | $convert->src_truncate_statement = 'DELETE FROM '; |
| 149 | break; |
| 150 | |
| 151 | case 'mysqli': |
| 152 | $convert->mysql_convert = true; |
| 153 | $convert->src_truncate_statement = 'TRUNCATE TABLE '; |
| 154 | break; |
| 155 | |
| 156 | default: |
| 157 | $convert->src_truncate_statement = 'TRUNCATE TABLE '; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | if ($convert->mysql_convert && !$same_db) |
| 162 | { |
| 163 | $src_db->sql_query("SET NAMES 'binary'"); |
| 164 | } |
| 165 | |
| 166 | switch ($db->get_sql_layer()) |
| 167 | { |
| 168 | case 'sqlite3': |
| 169 | $convert->truncate_statement = 'DELETE FROM '; |
| 170 | break; |
| 171 | |
| 172 | default: |
| 173 | $convert->truncate_statement = 'TRUNCATE TABLE '; |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | $get_info = false; |
| 178 | |
| 179 | // check security implications of direct inclusion |
| 180 | if (!file_exists('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx)) |
| 181 | { |
| 182 | $this->error($user->lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__); |
| 183 | } |
| 184 | |
| 185 | if (file_exists('./convertors/functions_' . $convert->convertor_tag . '.' . $phpEx)) |
| 186 | { |
| 187 | include_once('./convertors/functions_' . $convert->convertor_tag . '.' . $phpEx); |
| 188 | } |
| 189 | |
| 190 | $get_info = true; |
| 191 | include('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx); |
| 192 | |
| 193 | // Map some variables... |
| 194 | $convert->convertor_data = $convertor_data; |
| 195 | $convert->tables = $tables; |
| 196 | $convert->config_schema = $config_schema; |
| 197 | |
| 198 | // Now include the real data |
| 199 | $get_info = false; |
| 200 | include('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx); |
| 201 | |
| 202 | $convert->convertor_data = $convertor_data; |
| 203 | $convert->tables = $tables; |
| 204 | $convert->config_schema = $config_schema; |
| 205 | $convert->convertor = $convertor; |
| 206 | |
| 207 | // The test_file is a file that should be present in the location of the old board. |
| 208 | if (!file_exists($convert->options['forum_path'] . '/' . $test_file)) |
| 209 | { |
| 210 | $this->error(sprintf($user->lang['COULD_NOT_FIND_PATH'], $convert->options['forum_path']), __LINE__, __FILE__); |
| 211 | } |
| 212 | |
| 213 | $search_type = $config['search_type']; |
| 214 | |
| 215 | // For conversions we are a bit less strict and set to a search backend we know exist... |
| 216 | if (!class_exists($search_type)) |
| 217 | { |
| 218 | $search_type = 'phpbb\search\backend\fulltext_native'; |
| 219 | $config->set('search_type', $search_type); |
| 220 | } |
| 221 | |
| 222 | if (!class_exists($search_type)) |
| 223 | { |
| 224 | trigger_error('NO_SUCH_SEARCH_MODULE'); |
| 225 | } |
| 226 | |
| 227 | include_once($phpbb_root_path . 'includes/message_parser.' . $phpEx); |
| 228 | $message_parser = new \parse_message(); |
| 229 | |
| 230 | $jump = $request->variable('jump', 0); |
| 231 | $final_jump = $request->variable('final_jump', 0); |
| 232 | $sync_batch = $request->variable('sync_batch', -1); |
| 233 | $last_statement = $request->variable('last', 0); |
| 234 | |
| 235 | // We are running sync... |
| 236 | if ($sync_batch >= 0) |
| 237 | { |
| 238 | $this->sync_forums($converter, $sync_batch); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if ($jump) |
| 243 | { |
| 244 | $this->jump($converter, $jump, $last_statement); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | if ($final_jump) |
| 249 | { |
| 250 | $this->final_jump($final_jump); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | $current_table = $request->variable('current_table', 0); |
| 255 | $old_current_table = min(-1, $current_table - 1); |
| 256 | $skip_rows = $request->variable('skip_rows', 0); |
| 257 | |
| 258 | if (!$current_table && !$skip_rows) |
| 259 | { |
| 260 | if (!$request->variable('confirm', false)) |
| 261 | { |
| 262 | // If avatars / ranks / smilies folders are specified make sure they are writable |
| 263 | $bad_folders = array(); |
| 264 | |
| 265 | $local_paths = array( |
| 266 | 'avatar_path' => path($config['storage\\avatar\\config\\path']), |
| 267 | 'avatar_gallery_path' => path($config['avatar_gallery_path']), |
| 268 | 'icons_path' => path($config['icons_path']), |
| 269 | 'ranks_path' => path($config['ranks_path']), |
| 270 | 'smilies_path' => path($config['smilies_path']) |
| 271 | ); |
| 272 | |
| 273 | foreach ($local_paths as $folder => $local_path) |
| 274 | { |
| 275 | if (isset($convert->convertor[$folder])) |
| 276 | { |
| 277 | if (empty($convert->convertor['test_file'])) |
| 278 | { |
| 279 | // test_file is mandantory at the moment so this should never be reached, but just in case... |
| 280 | $this->error($user->lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__); |
| 281 | } |
| 282 | |
| 283 | if (!$local_path || !$this->filesystem->is_writable($phpbb_root_path . $local_path)) |
| 284 | { |
| 285 | if (!$local_path) |
| 286 | { |
| 287 | $bad_folders[] = sprintf($user->lang['CONFIG_PHPBB_EMPTY'], $folder); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | $bad_folders[] = $local_path; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (count($bad_folders)) |
| 298 | { |
| 299 | $msg = (count($bad_folders) == 1) ? $user->lang['MAKE_FOLDER_WRITABLE'] : $user->lang['MAKE_FOLDERS_WRITABLE']; |
| 300 | sort($bad_folders); |
| 301 | $this->error(sprintf($msg, implode('<br />', $bad_folders)), __LINE__, __FILE__, true); |
| 302 | |
| 303 | $this->template->assign_vars(array( |
| 304 | 'L_SUBMIT' => $user->lang['INSTALL_TEST'], |
| 305 | 'U_ACTION' => $this->controller_helper->route('phpbb_convert_convert', array('converter' => $converter)), |
| 306 | )); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | // Grab all the tables used in convertor |
| 311 | $missing_tables = $tables_list = $aliases = array(); |
| 312 | |
| 313 | foreach ($convert->convertor['schema'] as $schema) |
| 314 | { |
| 315 | // Skip those not used (because of addons/plugins not detected) |
| 316 | if (!$schema['target']) |
| 317 | { |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | foreach ($schema as $key => $val) |
| 322 | { |
| 323 | // we're dealing with an array like: |
| 324 | // array('forum_status', 'forums.forum_status', 'is_item_locked') |
| 325 | if (is_int($key) && !empty($val[1])) |
| 326 | { |
| 327 | $temp_data = $val[1]; |
| 328 | if (!is_array($temp_data)) |
| 329 | { |
| 330 | $temp_data = array($temp_data); |
| 331 | } |
| 332 | |
| 333 | foreach ($temp_data as $value) |
| 334 | { |
| 335 | if (preg_match('/([a-z0-9_]+)\.([a-z0-9_]+)\)* ?A?S? ?([a-z0-9_]*?)\.?([a-z0-9_]*)$/i', $value, $m)) |
| 336 | { |
| 337 | $table = $convert->src_table_prefix . $m[1]; |
| 338 | $tables_list[$table] = $table; |
| 339 | |
| 340 | if (!empty($m[3])) |
| 341 | { |
| 342 | $aliases[] = $convert->src_table_prefix . $m[3]; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | // 'left_join' => 'topics LEFT JOIN vote_desc ON topics.topic_id = vote_desc.topic_id AND topics.topic_vote = 1' |
| 348 | else if ($key == 'left_join') |
| 349 | { |
| 350 | // Convert the value if it wasn't an array already. |
| 351 | if (!is_array($val)) |
| 352 | { |
| 353 | $val = array($val); |
| 354 | } |
| 355 | |
| 356 | for ($j = 0, $size = count($val); $j < $size; ++$j) |
| 357 | { |
| 358 | if (preg_match('/LEFT JOIN ([a-z0-9_]+) AS ([a-z0-9_]+)/i', $val[$j], $m)) |
| 359 | { |
| 360 | $table = $convert->src_table_prefix . $m[1]; |
| 361 | $tables_list[$table] = $table; |
| 362 | |
| 363 | if (!empty($m[2])) |
| 364 | { |
| 365 | $aliases[] = $convert->src_table_prefix . $m[2]; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // Remove aliased tables from $tables_list |
| 374 | foreach ($aliases as $alias) |
| 375 | { |
| 376 | unset($tables_list[$alias]); |
| 377 | } |
| 378 | |
| 379 | // Check if the tables that we need exist |
| 380 | $src_db->sql_return_on_error(true); |
| 381 | foreach ($tables_list as $table => $null) |
| 382 | { |
| 383 | $sql = 'SELECT 1 FROM ' . $table; |
| 384 | $_result = $src_db->sql_query_limit($sql, 1); |
| 385 | |
| 386 | if (!$_result) |
| 387 | { |
| 388 | $missing_tables[] = $table; |
| 389 | } |
| 390 | $src_db->sql_freeresult($_result); |
| 391 | } |
| 392 | $src_db->sql_return_on_error(false); |
| 393 | |
| 394 | // Throw an error if some tables are missing |
| 395 | // 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 |
| 396 | |
| 397 | if (count($missing_tables) == count($tables_list)) |
| 398 | { |
| 399 | $this->error($user->lang['NO_TABLES_FOUND'] . ' ' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__); |
| 400 | } |
| 401 | else if (count($missing_tables)) |
| 402 | { |
| 403 | $this->error(sprintf($user->lang['TABLES_MISSING'], implode($user->lang['COMMA_SEPARATOR'], $missing_tables)) . '<br /><br />' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__); |
| 404 | } |
| 405 | |
| 406 | $url = $this->save_convert_progress($converter, 'confirm=1'); |
| 407 | $msg = $user->lang['PRE_CONVERT_COMPLETE']; |
| 408 | |
| 409 | if ($convert->convertor_data['author_notes']) |
| 410 | { |
| 411 | $msg .= '</p><p>' . sprintf($user->lang['AUTHOR_NOTES'], $convert->convertor_data['author_notes']); |
| 412 | } |
| 413 | |
| 414 | $this->template->assign_vars(array( |
| 415 | 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], |
| 416 | 'BODY' => $msg, |
| 417 | 'U_ACTION' => $url, |
| 418 | )); |
| 419 | |
| 420 | return; |
| 421 | } // if (!$request->variable('confirm', false))) |
| 422 | |
| 423 | $this->template->assign_block_vars('checks', array( |
| 424 | 'S_LEGEND' => true, |
| 425 | 'LEGEND' => $user->lang['STARTING_CONVERT'], |
| 426 | )); |
| 427 | |
| 428 | // Convert the config table and load the settings of the old board |
| 429 | if (!empty($convert->config_schema)) |
| 430 | { |
| 431 | restore_config($convert->config_schema); |
| 432 | |
| 433 | // Override a couple of config variables for the duration |
| 434 | $config['max_quote_depth'] = 0; |
| 435 | |
| 436 | // @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues |
| 437 | $config['max_post_chars'] = $config['min_post_chars'] = 0; |
| 438 | } |
| 439 | |
| 440 | $this->template->assign_block_vars('checks', array( |
| 441 | 'TITLE' => $user->lang['CONFIG_CONVERT'], |
| 442 | 'RESULT' => $user->lang['DONE'], |
| 443 | )); |
| 444 | |
| 445 | // Now process queries and execute functions that have to be executed prior to the conversion |
| 446 | if (!empty($convert->convertor['execute_first'])) |
| 447 | { |
| 448 | // phpcs:disable Squiz.PHP.Eval |
| 449 | eval($convert->convertor['execute_first']); |
| 450 | // phpcs:enable Squiz.PHP.Eval |
| 451 | } |
| 452 | |
| 453 | if (!empty($convert->convertor['query_first'])) |
| 454 | { |
| 455 | if (!is_array($convert->convertor['query_first'])) |
| 456 | { |
| 457 | $convert->convertor['query_first'] = array('target', array($convert->convertor['query_first'])); |
| 458 | } |
| 459 | else if (!is_array($convert->convertor['query_first'][0])) |
| 460 | { |
| 461 | $convert->convertor['query_first'] = array(array($convert->convertor['query_first'][0], $convert->convertor['query_first'][1])); |
| 462 | } |
| 463 | |
| 464 | foreach ($convert->convertor['query_first'] as $query_first) |
| 465 | { |
| 466 | if ($query_first[0] == 'src') |
| 467 | { |
| 468 | if ($convert->mysql_convert && $same_db) |
| 469 | { |
| 470 | $src_db->sql_query("SET NAMES 'binary'"); |
| 471 | } |
| 472 | |
| 473 | $src_db->sql_query($query_first[1]); |
| 474 | |
| 475 | if ($convert->mysql_convert && $same_db) |
| 476 | { |
| 477 | $src_db->sql_query("SET NAMES 'utf8'"); |
| 478 | } |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | $db->sql_query($query_first[1]); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | $this->template->assign_block_vars('checks', array( |
| 488 | 'TITLE' => $user->lang['PREPROCESS_STEP'], |
| 489 | 'RESULT' => $user->lang['DONE'], |
| 490 | )); |
| 491 | } // if (!$current_table && !$skip_rows) |
| 492 | |
| 493 | $this->template->assign_block_vars('checks', array( |
| 494 | 'S_LEGEND' => true, |
| 495 | 'LEGEND' => $user->lang['FILLING_TABLES'], |
| 496 | )); |
| 497 | |
| 498 | // This loop takes one target table and processes it |
| 499 | while ($current_table < count($convert->convertor['schema'])) |
| 500 | { |
| 501 | $schema = $convert->convertor['schema'][$current_table]; |
| 502 | |
| 503 | // The target table isn't set, this can be because a module (for example the attachement mod) is taking care of this. |
| 504 | if (empty($schema['target'])) |
| 505 | { |
| 506 | $current_table++; |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | $this->template->assign_block_vars('checks', array( |
| 511 | 'TITLE' => sprintf($user->lang['FILLING_TABLE'], $schema['target']), |
| 512 | )); |
| 513 | |
| 514 | // This is only the case when we first start working on the tables. |
| 515 | if (!$skip_rows) |
| 516 | { |
| 517 | // process execute_first and query_first for this table... |
| 518 | if (!empty($schema['execute_first'])) |
| 519 | { |
| 520 | // phpcs:disable Squiz.PHP.Eval |
| 521 | eval($schema['execute_first']); |
| 522 | // phpcs:enable Squiz.PHP.Eval |
| 523 | } |
| 524 | |
| 525 | if (!empty($schema['query_first'])) |
| 526 | { |
| 527 | if (!is_array($schema['query_first'])) |
| 528 | { |
| 529 | $schema['query_first'] = array('target', array($schema['query_first'])); |
| 530 | } |
| 531 | else if (!is_array($schema['query_first'][0])) |
| 532 | { |
| 533 | $schema['query_first'] = array(array($schema['query_first'][0], $schema['query_first'][1])); |
| 534 | } |
| 535 | |
| 536 | foreach ($schema['query_first'] as $query_first) |
| 537 | { |
| 538 | if ($query_first[0] == 'src') |
| 539 | { |
| 540 | if ($convert->mysql_convert && $same_db) |
| 541 | { |
| 542 | $src_db->sql_query("SET NAMES 'binary'"); |
| 543 | } |
| 544 | $src_db->sql_query($query_first[1]); |
| 545 | if ($convert->mysql_convert && $same_db) |
| 546 | { |
| 547 | $src_db->sql_query("SET NAMES 'utf8'"); |
| 548 | } |
| 549 | } |
| 550 | else |
| 551 | { |
| 552 | $db->sql_query($query_first[1]); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (!empty($schema['autoincrement'])) |
| 558 | { |
| 559 | switch ($db->get_sql_layer()) |
| 560 | { |
| 561 | case 'postgres': |
| 562 | $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'] . '));'); |
| 563 | break; |
| 564 | |
| 565 | case 'oracle': |
| 566 | $result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']); |
| 567 | $row = $db->sql_fetchrow($result); |
| 568 | $db->sql_freeresult($result); |
| 569 | |
| 570 | $largest_id = (int) $row['max_id']; |
| 571 | |
| 572 | if ($largest_id) |
| 573 | { |
| 574 | $db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq'); |
| 575 | $db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1)); |
| 576 | } |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Process execute_always for this table |
| 583 | // This is for code which needs to be executed on every pass of this table if |
| 584 | // it gets split because of time restrictions |
| 585 | if (!empty($schema['execute_always'])) |
| 586 | { |
| 587 | // phpcs:disable Squiz.PHP.Eval |
| 588 | eval($schema['execute_always']); |
| 589 | // phpcs:enable Squiz.PHP.Eval |
| 590 | } |
| 591 | |
| 592 | // |
| 593 | // Set up some variables |
| 594 | // |
| 595 | // $waiting_rows holds rows for multirows insertion (MySQL only) |
| 596 | // $src_tables holds unique tables with aliases to select from |
| 597 | // $src_fields will quickly refer source fields (or aliases) corresponding to the current index |
| 598 | // $select_fields holds the names of the fields to retrieve |
| 599 | // |
| 600 | |
| 601 | $sql_data = array( |
| 602 | 'source_fields' => array(), |
| 603 | 'target_fields' => array(), |
| 604 | 'source_tables' => array(), |
| 605 | 'select_fields' => array(), |
| 606 | ); |
| 607 | |
| 608 | // This statement is building the keys for later insertion. |
| 609 | $insert_query = $this->build_insert_query($schema, $sql_data, $current_table); |
| 610 | |
| 611 | // If no source table is affected, we skip the table |
| 612 | if (empty($sql_data['source_tables'])) |
| 613 | { |
| 614 | $skip_rows = 0; |
| 615 | $current_table++; |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | $distinct = (!empty($schema['distinct'])) ? 'DISTINCT ' : ''; |
| 620 | |
| 621 | $sql = 'SELECT ' . $distinct . implode(', ', $sql_data['select_fields']) . " \nFROM " . implode(', ', $sql_data['source_tables']); |
| 622 | |
| 623 | // Where |
| 624 | $sql .= (!empty($schema['where'])) ? "\nWHERE (" . $schema['where'] . ')' : ''; |
| 625 | |
| 626 | // Group By |
| 627 | if (!empty($schema['group_by'])) |
| 628 | { |
| 629 | $schema['group_by'] = array($schema['group_by']); |
| 630 | foreach ($sql_data['select_fields'] as $select) |
| 631 | { |
| 632 | $alias = strpos(strtolower($select), ' as '); |
| 633 | $select = ($alias) ? substr($select, 0, $alias) : $select; |
| 634 | if (!in_array($select, $schema['group_by'])) |
| 635 | { |
| 636 | $schema['group_by'][] = $select; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | $sql .= (!empty($schema['group_by'])) ? "\nGROUP BY " . implode(', ', $schema['group_by']) : ''; |
| 641 | |
| 642 | // Having |
| 643 | $sql .= (!empty($schema['having'])) ? "\nHAVING " . $schema['having'] : ''; |
| 644 | |
| 645 | // Order By |
| 646 | if (empty($schema['order_by']) && !empty($schema['primary'])) |
| 647 | { |
| 648 | $schema['order_by'] = $schema['primary']; |
| 649 | } |
| 650 | $sql .= (!empty($schema['order_by'])) ? "\nORDER BY " . $schema['order_by'] : ''; |
| 651 | |
| 652 | // Counting basically holds the amount of rows processed. |
| 653 | $counting = -1; |
| 654 | $batch_time = 0; |
| 655 | |
| 656 | while ($counting === -1 || ($counting >= $convert->batch_size && still_on_time())) |
| 657 | { |
| 658 | $old_current_table = $current_table; |
| 659 | |
| 660 | $rows = ''; |
| 661 | $waiting_rows = array(); |
| 662 | |
| 663 | if (!empty($batch_time)) |
| 664 | { |
| 665 | $mtime = explode(' ', microtime()); |
| 666 | $mtime = $mtime[0] + $mtime[1]; |
| 667 | $rows = ceil($counting/($mtime - $batch_time)) . " rows/s ($counting rows) | "; |
| 668 | } |
| 669 | |
| 670 | $this->template->assign_block_vars('checks', array( |
| 671 | 'TITLE' => "skip_rows = $skip_rows", |
| 672 | 'RESULT' => $rows . (($phpbb_container->getParameter('debug.memory') && function_exists('memory_get_usage')) ? ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] : ''), |
| 673 | )); |
| 674 | |
| 675 | $mtime = explode(' ', microtime()); |
| 676 | $batch_time = $mtime[0] + $mtime[1]; |
| 677 | |
| 678 | if ($convert->mysql_convert && $same_db) |
| 679 | { |
| 680 | $src_db->sql_query("SET NAMES 'binary'"); |
| 681 | } |
| 682 | |
| 683 | // Take skip rows into account and only fetch batch_size amount of rows |
| 684 | $___result = $src_db->sql_query_limit($sql, $convert->batch_size, $skip_rows); |
| 685 | |
| 686 | if ($convert->mysql_convert && $same_db) |
| 687 | { |
| 688 | $src_db->sql_query("SET NAMES 'utf8'"); |
| 689 | } |
| 690 | |
| 691 | // This loop processes each row |
| 692 | $counting = 0; |
| 693 | |
| 694 | $convert->row = $convert_row = array(); |
| 695 | |
| 696 | if (!empty($schema['autoincrement'])) |
| 697 | { |
| 698 | switch ($db->get_sql_layer()) |
| 699 | { |
| 700 | case 'mssql_odbc': |
| 701 | case 'mssqlnative': |
| 702 | $db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' ON'); |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | // Now handle the rows until time is over or no more rows to process... |
| 708 | while ($counting === 0 || still_on_time()) |
| 709 | { |
| 710 | $convert_row = $src_db->sql_fetchrow($___result); |
| 711 | |
| 712 | if (!$convert_row) |
| 713 | { |
| 714 | // move to the next batch or table |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | // With this we are able to always save the last state |
| 719 | $convert->row = $convert_row; |
| 720 | |
| 721 | // Increment the counting variable, it stores the number of rows we have processed |
| 722 | $counting++; |
| 723 | |
| 724 | $insert_values = array(); |
| 725 | |
| 726 | $sql_flag = $this->process_row($schema, $sql_data, $insert_values); |
| 727 | |
| 728 | if ($sql_flag === true) |
| 729 | { |
| 730 | switch ($db->get_sql_layer()) |
| 731 | { |
| 732 | // If MySQL, we'll wait to have num_wait_rows rows to submit at once |
| 733 | case 'mysqli': |
| 734 | $waiting_rows[] = '(' . implode(', ', $insert_values) . ')'; |
| 735 | |
| 736 | if (count($waiting_rows) >= $convert->num_wait_rows) |
| 737 | { |
| 738 | $errored = false; |
| 739 | |
| 740 | $db->sql_return_on_error(true); |
| 741 | |
| 742 | if (!$db->sql_query($insert_query . implode(', ', $waiting_rows))) |
| 743 | { |
| 744 | $errored = true; |
| 745 | } |
| 746 | $db->sql_return_on_error(false); |
| 747 | |
| 748 | if ($errored) |
| 749 | { |
| 750 | $db->sql_return_on_error(true); |
| 751 | |
| 752 | // Because it errored out we will try to insert the rows one by one... most of the time this |
| 753 | // is caused by duplicate entries - but we also do not want to miss one... |
| 754 | foreach ($waiting_rows as $waiting_sql) |
| 755 | { |
| 756 | if (!$db->sql_query($insert_query . $waiting_sql)) |
| 757 | { |
| 758 | $this->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql, ENT_COMPAT) . '<br /><br />' . htmlspecialchars(print_r($db->sql_error(), true), ENT_COMPAT), __LINE__, __FILE__, true); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | $db->sql_return_on_error(false); |
| 763 | } |
| 764 | |
| 765 | $waiting_rows = array(); |
| 766 | } |
| 767 | |
| 768 | break; |
| 769 | |
| 770 | default: |
| 771 | $insert_sql = $insert_query . '(' . implode(', ', $insert_values) . ')'; |
| 772 | |
| 773 | $db->sql_return_on_error(true); |
| 774 | |
| 775 | if (!$db->sql_query($insert_sql)) |
| 776 | { |
| 777 | $this->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_sql, ENT_COMPAT) . '<br /><br />' . htmlspecialchars(print_r($db->sql_error(), true), ENT_COMPAT), __LINE__, __FILE__, true); |
| 778 | } |
| 779 | $db->sql_return_on_error(false); |
| 780 | |
| 781 | $waiting_rows = array(); |
| 782 | |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | $skip_rows++; |
| 788 | } |
| 789 | $src_db->sql_freeresult($___result); |
| 790 | |
| 791 | // We might still have some rows waiting |
| 792 | if (count($waiting_rows)) |
| 793 | { |
| 794 | $errored = false; |
| 795 | $db->sql_return_on_error(true); |
| 796 | |
| 797 | if (!$db->sql_query($insert_query . implode(', ', $waiting_rows))) |
| 798 | { |
| 799 | $errored = true; |
| 800 | } |
| 801 | $db->sql_return_on_error(false); |
| 802 | |
| 803 | if ($errored) |
| 804 | { |
| 805 | $db->sql_return_on_error(true); |
| 806 | |
| 807 | // Because it errored out we will try to insert the rows one by one... most of the time this |
| 808 | // is caused by duplicate entries - but we also do not want to miss one... |
| 809 | foreach ($waiting_rows as $waiting_sql) |
| 810 | { |
| 811 | $db->sql_query($insert_query . $waiting_sql); |
| 812 | $this->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql, ENT_COMPAT) . '<br /><br />' . htmlspecialchars(print_r($db->sql_error(), true), ENT_COMPAT), __LINE__, __FILE__, true); |
| 813 | } |
| 814 | |
| 815 | $db->sql_return_on_error(false); |
| 816 | } |
| 817 | |
| 818 | $waiting_rows = array(); |
| 819 | } |
| 820 | |
| 821 | if (!empty($schema['autoincrement'])) |
| 822 | { |
| 823 | switch ($db->get_sql_layer()) |
| 824 | { |
| 825 | case 'mssql_odbc': |
| 826 | case 'mssqlnative': |
| 827 | $db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' OFF'); |
| 828 | break; |
| 829 | |
| 830 | case 'postgres': |
| 831 | $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'] . '));'); |
| 832 | break; |
| 833 | |
| 834 | case 'oracle': |
| 835 | $result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']); |
| 836 | $row = $db->sql_fetchrow($result); |
| 837 | $db->sql_freeresult($result); |
| 838 | |
| 839 | $largest_id = (int) $row['max_id']; |
| 840 | |
| 841 | if ($largest_id) |
| 842 | { |
| 843 | $db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq'); |
| 844 | $db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1)); |
| 845 | } |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | // When we reach this point, either the current table has been processed or we're running out of time. |
| 852 | if (still_on_time() && $counting < $convert->batch_size/* && !defined('DEBUG')*/) |
| 853 | { |
| 854 | $skip_rows = 0; |
| 855 | $current_table++; |
| 856 | } |
| 857 | else |
| 858 | {/* |
| 859 | if (still_on_time() && $counting < $convert->batch_size) |
| 860 | { |
| 861 | $skip_rows = 0; |
| 862 | $current_table++; |
| 863 | }*/ |
| 864 | |
| 865 | // Looks like we ran out of time. |
| 866 | $url = $this->save_convert_progress($converter, 'current_table=' . $current_table . '&skip_rows=' . $skip_rows); |
| 867 | |
| 868 | $current_table++; |
| 869 | // $percentage = ($skip_rows == 0) ? 0 : floor(100 / ($total_rows / $skip_rows)); |
| 870 | |
| 871 | $msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $current_table, count($convert->convertor['schema'])); |
| 872 | |
| 873 | $this->template->assign_vars(array( |
| 874 | 'BODY' => $msg, |
| 875 | 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], |
| 876 | 'U_ACTION' => $url, |
| 877 | )); |
| 878 | |
| 879 | $this->meta_refresh($url); |
| 880 | return; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | // Process execute_last then we'll be done |
| 885 | $url = $this->save_convert_progress($converter, 'jump=1'); |
| 886 | |
| 887 | $this->template->assign_vars(array( |
| 888 | 'L_SUBMIT' => $user->lang['FINAL_STEP'], |
| 889 | 'U_ACTION' => $url, |
| 890 | )); |
| 891 | |
| 892 | $this->meta_refresh($url); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Sync function being executed at the middle, some functions need to be executed after a successful sync. |
| 898 | */ |
| 899 | function sync_forums($converter, $sync_batch) |
| 900 | { |
| 901 | global $user, $db, $phpbb_root_path, $phpEx, $config, $cache; |
| 902 | global $convert; |
| 903 | global $phpbb_container; |
| 904 | |
| 905 | include_once ($phpbb_root_path . 'includes/functions_admin.' . $phpEx); |
| 906 | |
| 907 | $this->template->assign_block_vars('checks', array( |
| 908 | 'S_LEGEND' => true, |
| 909 | 'LEGEND' => $user->lang['SYNC_TOPICS'], |
| 910 | )); |
| 911 | |
| 912 | $batch_size = $convert->batch_size; |
| 913 | |
| 914 | $sql = 'SELECT MIN(topic_id) as min_value, MAX(topic_id) AS max_value |
| 915 | FROM ' . TOPICS_TABLE; |
| 916 | $result = $db->sql_query($sql); |
| 917 | $row = $db->sql_fetchrow($result); |
| 918 | $db->sql_freeresult($result); |
| 919 | |
| 920 | // Set values of minimum/maximum primary value for this table. |
| 921 | $primary_min = $row['min_value']; |
| 922 | $primary_max = $row['max_value']; |
| 923 | |
| 924 | if ($sync_batch == 0) |
| 925 | { |
| 926 | $sync_batch = (int) $primary_min; |
| 927 | } |
| 928 | |
| 929 | if ($sync_batch == 0) |
| 930 | { |
| 931 | $sync_batch = 1; |
| 932 | } |
| 933 | |
| 934 | // Fetch a batch of rows, process and insert them. |
| 935 | while ($sync_batch <= $primary_max && still_on_time()) |
| 936 | { |
| 937 | $end = ($sync_batch + $batch_size - 1); |
| 938 | |
| 939 | // Sync all topics in batch mode... |
| 940 | sync('topic', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, true); |
| 941 | |
| 942 | $this->template->assign_block_vars('checks', array( |
| 943 | 'TITLE' => sprintf($user->lang['SYNC_TOPIC_ID'], $sync_batch, ($sync_batch + $batch_size)) . (($phpbb_container->getParameter('debug.memory') && function_exists('memory_get_usage')) ? ' [' . ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] . ']' : ''), |
| 944 | 'RESULT' => $user->lang['DONE'], |
| 945 | )); |
| 946 | |
| 947 | $sync_batch += $batch_size; |
| 948 | } |
| 949 | |
| 950 | if ($sync_batch >= $primary_max) |
| 951 | { |
| 952 | $url = $this->save_convert_progress($converter, 'final_jump=1'); |
| 953 | |
| 954 | $this->template->assign_vars(array( |
| 955 | 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], |
| 956 | 'U_ACTION' => $url, |
| 957 | )); |
| 958 | |
| 959 | $this->meta_refresh($url); |
| 960 | return; |
| 961 | } |
| 962 | else |
| 963 | { |
| 964 | $sync_batch--; |
| 965 | } |
| 966 | |
| 967 | $url = $this->save_convert_progress($converter, 'sync_batch=' . $sync_batch); |
| 968 | |
| 969 | $this->template->assign_vars(array( |
| 970 | 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], |
| 971 | 'U_ACTION' => $url, |
| 972 | )); |
| 973 | |
| 974 | $this->meta_refresh($url); |
| 975 | return; |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Save the convertor status |
| 980 | */ |
| 981 | function save_convert_progress($convertor_tag, $step) |
| 982 | { |
| 983 | global $config, $convert, $language; |
| 984 | |
| 985 | // Save convertor Status |
| 986 | $config->set('convert_progress', serialize(array( |
| 987 | 'step' => $step, |
| 988 | 'table_prefix' => $convert->src_table_prefix, |
| 989 | 'tag' => $convert->convertor_tag, |
| 990 | )), false); |
| 991 | |
| 992 | $config->set('convert_db_server', serialize(array( |
| 993 | 'dbms' => $convert->src_dbms, |
| 994 | 'dbhost' => $convert->src_dbhost, |
| 995 | 'dbport' => $convert->src_dbport, |
| 996 | 'dbname' => $convert->src_dbname, |
| 997 | )), false); |
| 998 | |
| 999 | $config->set('convert_db_user', serialize(array( |
| 1000 | 'dbuser' => $convert->src_dbuser, |
| 1001 | 'dbpasswd' => $convert->src_dbpasswd, |
| 1002 | )), false); |
| 1003 | |
| 1004 | return $this->controller_helper->route('phpbb_convert_convert', array('converter' => $convertor_tag)) . '?' . $step; |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Finish conversion, the last function to be called. |
| 1009 | */ |
| 1010 | function finish_conversion() |
| 1011 | { |
| 1012 | global $db, $phpbb_root_path, $phpEx, $convert, $config, $language, $user; |
| 1013 | global $cache, $auth, $phpbb_container, $phpbb_log; |
| 1014 | |
| 1015 | include_once ($phpbb_root_path . 'includes/functions_admin.' . $phpEx); |
| 1016 | |
| 1017 | $db->sql_query('DELETE FROM ' . CONFIG_TABLE . " |
| 1018 | WHERE config_name = 'convert_progress' |
| 1019 | OR config_name = 'convert_options' |
| 1020 | OR config_name = 'convert_db_server' |
| 1021 | OR config_name = 'convert_db_user'"); |
| 1022 | $db->sql_query('DELETE FROM ' . SESSIONS_TABLE); |
| 1023 | |
| 1024 | @unlink($phpbb_container->getParameter('core.cache_dir') . 'data_global.' . $phpEx); |
| 1025 | phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); |
| 1026 | |
| 1027 | // And finally, add a note to the log |
| 1028 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_INSTALL_CONVERTED', false, array($convert->convertor_data['forum_name'], $config['version'])); |
| 1029 | |
| 1030 | $url = $this->controller_helper->route('phpbb_convert_finish'); |
| 1031 | |
| 1032 | $this->template->assign_vars(array( |
| 1033 | 'L_SUBMIT' => $user->lang['FINAL_STEP'], |
| 1034 | 'U_ACTION' => $url, |
| 1035 | )); |
| 1036 | |
| 1037 | $this->meta_refresh($url); |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * This function marks the steps after syncing |
| 1043 | */ |
| 1044 | function final_jump($final_jump) |
| 1045 | { |
| 1046 | global $user, $src_db, $same_db, $db, $phpbb_root_path, $phpEx, $config, $cache; |
| 1047 | global $convert; |
| 1048 | |
| 1049 | $this->template->assign_block_vars('checks', array( |
| 1050 | 'S_LEGEND' => true, |
| 1051 | 'LEGEND' => $user->lang['PROCESS_LAST'], |
| 1052 | )); |
| 1053 | |
| 1054 | if ($final_jump == 1) |
| 1055 | { |
| 1056 | $db->sql_return_on_error(true); |
| 1057 | |
| 1058 | update_topics_posted(); |
| 1059 | |
| 1060 | $this->template->assign_block_vars('checks', array( |
| 1061 | 'TITLE' => $user->lang['UPDATE_TOPICS_POSTED'], |
| 1062 | 'RESULT' => $user->lang['DONE'], |
| 1063 | )); |
| 1064 | |
| 1065 | if ($db->get_sql_error_triggered()) |
| 1066 | { |
| 1067 | $this->template->assign_vars(array( |
| 1068 | 'S_ERROR_BOX' => true, |
| 1069 | 'ERROR_TITLE' => $user->lang['UPDATE_TOPICS_POSTED'], |
| 1070 | 'ERROR_MSG' => $user->lang['UPDATE_TOPICS_POSTED_ERR'], |
| 1071 | )); |
| 1072 | } |
| 1073 | $db->sql_return_on_error(false); |
| 1074 | |
| 1075 | $this->finish_conversion(); |
| 1076 | return; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * This function marks the steps before syncing (jump=1) |
| 1082 | */ |
| 1083 | function jump($converter, $jump, $last_statement) |
| 1084 | { |
| 1085 | /** @var \phpbb\db\driver\driver_interface $src_db */ |
| 1086 | /** @var \phpbb\cache\driver\driver_interface $cache */ |
| 1087 | global $user, $src_db, $same_db, $db, $phpbb_root_path, $phpEx, $config, $cache; |
| 1088 | global $convert; |
| 1089 | |
| 1090 | include_once ($phpbb_root_path . 'includes/functions_admin.' . $phpEx); |
| 1091 | |
| 1092 | $this->template->assign_block_vars('checks', array( |
| 1093 | 'S_LEGEND' => true, |
| 1094 | 'LEGEND' => $user->lang['PROCESS_LAST'], |
| 1095 | )); |
| 1096 | |
| 1097 | if ($jump == 1) |
| 1098 | { |
| 1099 | // Execute 'last' statements/queries |
| 1100 | if (!empty($convert->convertor['execute_last'])) |
| 1101 | { |
| 1102 | if (!is_array($convert->convertor['execute_last'])) |
| 1103 | { |
| 1104 | // phpcs:disable Squiz.PHP.Eval |
| 1105 | eval($convert->convertor['execute_last']); |
| 1106 | // phpcs:enable Squiz.PHP.Eval |
| 1107 | } |
| 1108 | else |
| 1109 | { |
| 1110 | while ($last_statement < count($convert->convertor['execute_last'])) |
| 1111 | { |
| 1112 | // phpcs:disable Squiz.PHP.Eval |
| 1113 | eval($convert->convertor['execute_last'][$last_statement]); |
| 1114 | // phpcs:enable Squiz.PHP.Eval |
| 1115 | |
| 1116 | $this->template->assign_block_vars('checks', array( |
| 1117 | 'TITLE' => $convert->convertor['execute_last'][$last_statement], |
| 1118 | 'RESULT' => $user->lang['DONE'], |
| 1119 | )); |
| 1120 | |
| 1121 | $last_statement++; |
| 1122 | $url = $this->save_convert_progress($converter, 'jump=1&last=' . $last_statement); |
| 1123 | |
| 1124 | $percentage = ($last_statement == 0) ? 0 : floor(100 / (count($convert->convertor['execute_last']) / $last_statement)); |
| 1125 | $msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $last_statement, count($convert->convertor['execute_last']), $percentage); |
| 1126 | |
| 1127 | $this->template->assign_vars(array( |
| 1128 | 'L_SUBMIT' => $user->lang['CONTINUE_LAST'], |
| 1129 | 'BODY' => $msg, |
| 1130 | 'U_ACTION' => $url, |
| 1131 | )); |
| 1132 | |
| 1133 | $this->meta_refresh($url); |
| 1134 | return; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | if (!empty($convert->convertor['query_last'])) |
| 1140 | { |
| 1141 | if (!is_array($convert->convertor['query_last'])) |
| 1142 | { |
| 1143 | $convert->convertor['query_last'] = array('target', array($convert->convertor['query_last'])); |
| 1144 | } |
| 1145 | else if (!is_array($convert->convertor['query_last'][0])) |
| 1146 | { |
| 1147 | $convert->convertor['query_last'] = array(array($convert->convertor['query_last'][0], $convert->convertor['query_last'][1])); |
| 1148 | } |
| 1149 | |
| 1150 | foreach ($convert->convertor['query_last'] as $query_last) |
| 1151 | { |
| 1152 | if ($query_last[0] == 'src') |
| 1153 | { |
| 1154 | if ($convert->mysql_convert && $same_db) |
| 1155 | { |
| 1156 | $src_db->sql_query("SET NAMES 'binary'"); |
| 1157 | } |
| 1158 | |
| 1159 | $src_db->sql_query($query_last[1]); |
| 1160 | |
| 1161 | if ($convert->mysql_convert && $same_db) |
| 1162 | { |
| 1163 | $src_db->sql_query("SET NAMES 'utf8'"); |
| 1164 | } |
| 1165 | } |
| 1166 | else |
| 1167 | { |
| 1168 | $db->sql_query($query_last[1]); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | // Sanity check |
| 1174 | $db->sql_return_on_error(false); |
| 1175 | $src_db->sql_return_on_error(false); |
| 1176 | |
| 1177 | fix_empty_primary_groups(); |
| 1178 | |
| 1179 | $sql = 'SELECT MIN(user_regdate) AS board_startdate |
| 1180 | FROM ' . USERS_TABLE; |
| 1181 | $result = $db->sql_query($sql); |
| 1182 | $row = $db->sql_fetchrow($result); |
| 1183 | $db->sql_freeresult($result); |
| 1184 | |
| 1185 | if (!isset($config['board_startdate']) || ($row['board_startdate'] < $config['board_startdate'] && $row['board_startdate'] > 0)) |
| 1186 | { |
| 1187 | $config->set('board_startdate', $row['board_startdate']); |
| 1188 | $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_regdate = ' . $row['board_startdate'] . ' WHERE user_id = ' . ANONYMOUS); |
| 1189 | } |
| 1190 | |
| 1191 | update_dynamic_config(); |
| 1192 | |
| 1193 | $this->template->assign_block_vars('checks', array( |
| 1194 | 'TITLE' => $user->lang['CLEAN_VERIFY'], |
| 1195 | 'RESULT' => $user->lang['DONE'], |
| 1196 | )); |
| 1197 | |
| 1198 | $url = $this->save_convert_progress($converter, 'jump=2'); |
| 1199 | |
| 1200 | $this->template->assign_vars(array( |
| 1201 | 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], |
| 1202 | 'U_ACTION' => $url, |
| 1203 | )); |
| 1204 | |
| 1205 | $this->meta_refresh($url); |
| 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | if ($jump == 2) |
| 1210 | { |
| 1211 | $db->sql_query('UPDATE ' . USERS_TABLE . " SET user_permissions = ''"); |
| 1212 | |
| 1213 | // TODO: sync() is likely going to bomb out on forums with a considerable amount of topics. |
| 1214 | // TODO: the sync function is able to handle FROM-TO values, we should use them here (batch processing) |
| 1215 | sync('forum', '', '', false, true); |
| 1216 | $cache->destroy('sql', FORUMS_TABLE); |
| 1217 | |
| 1218 | $this->template->assign_block_vars('checks', array( |
| 1219 | 'TITLE' => $user->lang['SYNC_FORUMS'], |
| 1220 | 'RESULT' => $user->lang['DONE'], |
| 1221 | )); |
| 1222 | |
| 1223 | // Continue with synchronizing the forums... |
| 1224 | $url = $this->save_convert_progress($converter, 'sync_batch=0'); |
| 1225 | |
| 1226 | $this->template->assign_vars(array( |
| 1227 | 'L_SUBMIT' => $user->lang['CONTINUE_CONVERT'], |
| 1228 | 'U_ACTION' => $url, |
| 1229 | )); |
| 1230 | |
| 1231 | $this->meta_refresh($url); |
| 1232 | return; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | function build_insert_query(&$schema, &$sql_data, $current_table) |
| 1237 | { |
| 1238 | global $db, $user; |
| 1239 | global $convert; |
| 1240 | |
| 1241 | $insert_query = 'INSERT INTO ' . $schema['target'] . ' ('; |
| 1242 | |
| 1243 | $aliases = array(); |
| 1244 | |
| 1245 | $sql_data = array( |
| 1246 | 'source_fields' => array(), |
| 1247 | 'target_fields' => array(), |
| 1248 | 'source_tables' => array(), |
| 1249 | 'select_fields' => array(), |
| 1250 | ); |
| 1251 | |
| 1252 | foreach ($schema as $key => $val) |
| 1253 | { |
| 1254 | // Example: array('group_name', 'extension_groups.group_name', 'htmlspecialchars'), |
| 1255 | if (is_int($key)) |
| 1256 | { |
| 1257 | if (!empty($val[0])) |
| 1258 | { |
| 1259 | // Target fields |
| 1260 | $sql_data['target_fields'][$val[0]] = $key; |
| 1261 | $insert_query .= $val[0] . ', '; |
| 1262 | } |
| 1263 | |
| 1264 | if (!is_array($val[1])) |
| 1265 | { |
| 1266 | $val[1] = array($val[1]); |
| 1267 | } |
| 1268 | |
| 1269 | foreach ($val[1] as $valkey => $value_1) |
| 1270 | { |
| 1271 | // This should cover about any case: |
| 1272 | // |
| 1273 | // table.field => SELECT table.field FROM table |
| 1274 | // table.field AS alias => SELECT table.field AS alias FROM table |
| 1275 | // table.field AS table2.alias => SELECT table2.field AS alias FROM table table2 |
| 1276 | // table.field AS table2.field => SELECT table2.field FROM table table2 |
| 1277 | // |
| 1278 | if (preg_match('/^([a-z0-9_]+)\.([a-z0-9_]+)( +AS +(([a-z0-9_]+?)\.)?([a-z0-9_]+))?$/i', $value_1, $m)) |
| 1279 | { |
| 1280 | // There is 'AS ...' in the field names |
| 1281 | if (!empty($m[3])) |
| 1282 | { |
| 1283 | $value_1 = ($m[2] == $m[6]) ? $m[1] . '.' . $m[2] : $m[1] . '.' . $m[2] . ' AS ' . $m[6]; |
| 1284 | |
| 1285 | // Table alias: store it then replace the source table with it |
| 1286 | if (!empty($m[5]) && $m[5] != $m[1]) |
| 1287 | { |
| 1288 | $aliases[$m[5]] = $m[1]; |
| 1289 | $value_1 = str_replace($m[1] . '.' . $m[2], $m[5] . '.' . $m[2], $value_1); |
| 1290 | } |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | // No table alias |
| 1295 | $sql_data['source_tables'][$m[1]] = (empty($convert->src_table_prefix)) ? $m[1] : $convert->src_table_prefix . $m[1] . ' ' . $db->sql_quote($m[1]); |
| 1296 | } |
| 1297 | |
| 1298 | $sql_data['select_fields'][$value_1] = $value_1; |
| 1299 | $sql_data['source_fields'][$key][$valkey] = (!empty($m[6])) ? $m[6] : $m[2]; |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | else if ($key == 'where' || $key == 'group_by' || $key == 'order_by' || $key == 'having') |
| 1304 | { |
| 1305 | if (@preg_match_all('/([a-z0-9_]+)\.([a-z0-9_]+)/i', $val, $m)) |
| 1306 | { |
| 1307 | foreach ($m[1] as $value) |
| 1308 | { |
| 1309 | $sql_data['source_tables'][$value] = (empty($convert->src_table_prefix)) ? $value : $convert->src_table_prefix . $value . ' ' . $db->sql_quote($value); |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | // Add the aliases to the list of tables |
| 1316 | foreach ($aliases as $alias => $table) |
| 1317 | { |
| 1318 | $sql_data['source_tables'][$alias] = $convert->src_table_prefix . $table . ' ' . $db->sql_quote($alias); |
| 1319 | } |
| 1320 | |
| 1321 | // 'left_join' => 'forums LEFT JOIN forum_prune ON forums.forum_id = forum_prune.forum_id', |
| 1322 | if (!empty($schema['left_join'])) |
| 1323 | { |
| 1324 | if (!is_array($schema['left_join'])) |
| 1325 | { |
| 1326 | $schema['left_join'] = array($schema['left_join']); |
| 1327 | } |
| 1328 | |
| 1329 | foreach ($schema['left_join'] as $left_join) |
| 1330 | { |
| 1331 | // This won't handle concatened LEFT JOINs |
| 1332 | if (!preg_match('/([a-z0-9_]+) LEFT JOIN ([a-z0-9_]+) A?S? ?([a-z0-9_]*?) ?(ON|USING)(.*)/i', $left_join, $m)) |
| 1333 | { |
| 1334 | $this->error(sprintf($user->lang['NOT_UNDERSTAND'], 'LEFT JOIN', $left_join, $current_table, $schema['target']), __LINE__, __FILE__); |
| 1335 | } |
| 1336 | |
| 1337 | if (!empty($aliases[$m[2]])) |
| 1338 | { |
| 1339 | if (!empty($m[3])) |
| 1340 | { |
| 1341 | $this->error(sprintf($user->lang['NAMING_CONFLICT'], $m[2], $m[3], $schema['left_join']), __LINE__, __FILE__); |
| 1342 | } |
| 1343 | |
| 1344 | $m[2] = $aliases[$m[2]]; |
| 1345 | $m[3] = $m[2]; |
| 1346 | } |
| 1347 | |
| 1348 | $right_table = $convert->src_table_prefix . $m[2]; |
| 1349 | if (!empty($m[3])) |
| 1350 | { |
| 1351 | unset($sql_data['source_tables'][$m[3]]); |
| 1352 | } |
| 1353 | else if ($m[2] != $m[1]) |
| 1354 | { |
| 1355 | unset($sql_data['source_tables'][$m[2]]); |
| 1356 | } |
| 1357 | |
| 1358 | if (strpos($sql_data['source_tables'][$m[1]], "\nLEFT JOIN") !== false) |
| 1359 | { |
| 1360 | $sql_data['source_tables'][$m[1]] = '(' . $sql_data['source_tables'][$m[1]] . ")\nLEFT JOIN $right_table"; |
| 1361 | } |
| 1362 | else |
| 1363 | { |
| 1364 | $sql_data['source_tables'][$m[1]] .= "\nLEFT JOIN $right_table"; |
| 1365 | } |
| 1366 | |
| 1367 | if (!empty($m[3])) |
| 1368 | { |
| 1369 | unset($sql_data['source_tables'][$m[3]]); |
| 1370 | $sql_data['source_tables'][$m[1]] .= ' AS ' . $m[3]; |
| 1371 | } |
| 1372 | else if (!empty($convert->src_table_prefix)) |
| 1373 | { |
| 1374 | $sql_data['source_tables'][$m[1]] .= ' AS ' . $m[2]; |
| 1375 | } |
| 1376 | $sql_data['source_tables'][$m[1]] .= ' ' . $m[4] . $m[5]; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | // Remove ", " from the end of the insert query |
| 1381 | $insert_query = substr($insert_query, 0, -2) . ') VALUES '; |
| 1382 | |
| 1383 | return $insert_query; |
| 1384 | } |
| 1385 | |
| 1386 | /** |
| 1387 | * Function for processing the currently handled row |
| 1388 | */ |
| 1389 | function process_row(&$schema, &$sql_data, &$insert_values) |
| 1390 | { |
| 1391 | global $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache; |
| 1392 | global $convert, $convert_row; |
| 1393 | |
| 1394 | $sql_flag = false; |
| 1395 | |
| 1396 | foreach ($schema as $key => $fields) |
| 1397 | { |
| 1398 | // We are only interested in the lines with: |
| 1399 | // array('comment', 'attachments_desc.comment', 'htmlspecialchars'), |
| 1400 | if (is_int($key)) |
| 1401 | { |
| 1402 | if (!is_array($fields[1])) |
| 1403 | { |
| 1404 | $fields[1] = array($fields[1]); |
| 1405 | } |
| 1406 | |
| 1407 | $firstkey_set = false; |
| 1408 | $firstkey = 0; |
| 1409 | |
| 1410 | foreach ($fields[1] as $inner_key => $inner_value) |
| 1411 | { |
| 1412 | if (!$firstkey_set) |
| 1413 | { |
| 1414 | $firstkey = $inner_key; |
| 1415 | $firstkey_set = true; |
| 1416 | } |
| 1417 | |
| 1418 | $src_field = isset($sql_data['source_fields'][$key][$inner_key]) ? $sql_data['source_fields'][$key][$inner_key] : ''; |
| 1419 | |
| 1420 | if (!empty($src_field)) |
| 1421 | { |
| 1422 | $fields[1][$inner_key] = $convert->row[$src_field]; |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | if (!empty($fields[0])) |
| 1427 | { |
| 1428 | // We have a target field, if we haven't set $sql_flag yet it will be set to TRUE. |
| 1429 | // If a function has already set it to FALSE it won't change it. |
| 1430 | if ($sql_flag === false) |
| 1431 | { |
| 1432 | $sql_flag = true; |
| 1433 | } |
| 1434 | |
| 1435 | // No function assigned? |
| 1436 | if (empty($fields[2])) |
| 1437 | { |
| 1438 | $value = $fields[1][$firstkey]; |
| 1439 | } |
| 1440 | else if (is_array($fields[2]) && !is_callable($fields[2])) |
| 1441 | { |
| 1442 | // Execute complex function/eval/typecast |
| 1443 | $value = $fields[1]; |
| 1444 | |
| 1445 | foreach ($fields[2] as $type => $execution) |
| 1446 | { |
| 1447 | if (strpos($type, 'typecast') === 0) |
| 1448 | { |
| 1449 | if (!is_array($value)) |
| 1450 | { |
| 1451 | $value = array($value); |
| 1452 | } |
| 1453 | $value = $value[0]; |
| 1454 | settype($value, $execution); |
| 1455 | } |
| 1456 | else if (strpos($type, 'function') === 0) |
| 1457 | { |
| 1458 | if (!is_array($value)) |
| 1459 | { |
| 1460 | $value = array($value); |
| 1461 | } |
| 1462 | |
| 1463 | // Add ENT_COMPAT default flag to html specialchars/entities functions, see PHPBB3-16690 |
| 1464 | if (in_array($execution, ['htmlspecialchars', 'htmlentities', 'htmlspecialchars_decode', 'html_entitity_decode'])) |
| 1465 | { |
| 1466 | $value[] = ENT_COMPAT; |
| 1467 | } |
| 1468 | |
| 1469 | $value = call_user_func_array($execution, $value); |
| 1470 | } |
| 1471 | else if (strpos($type, 'execute') === 0) |
| 1472 | { |
| 1473 | if (!is_array($value)) |
| 1474 | { |
| 1475 | $value = array($value); |
| 1476 | } |
| 1477 | |
| 1478 | $execution = str_replace('{RESULT}', '$value', $execution); |
| 1479 | $execution = str_replace('{VALUE}', '$value', $execution); |
| 1480 | // phpcs:disable Squiz.PHP.Eval |
| 1481 | eval($execution); |
| 1482 | // phpcs:enable Squiz.PHP.Eval |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | else |
| 1487 | { |
| 1488 | $value = call_user_func_array($fields[2], $fields[1]); |
| 1489 | } |
| 1490 | |
| 1491 | if (is_null($value)) |
| 1492 | { |
| 1493 | $value = ''; |
| 1494 | } |
| 1495 | |
| 1496 | $insert_values[] = $db->_sql_validate_value($value); |
| 1497 | } |
| 1498 | else if (!empty($fields[2])) |
| 1499 | { |
| 1500 | if (is_array($fields[2])) |
| 1501 | { |
| 1502 | // Execute complex function/eval/typecast |
| 1503 | $value = ''; |
| 1504 | |
| 1505 | foreach ($fields[2] as $type => $execution) |
| 1506 | { |
| 1507 | if (strpos($type, 'typecast') === 0) |
| 1508 | { |
| 1509 | $value = settype($value, $execution); |
| 1510 | } |
| 1511 | else if (strpos($type, 'function') === 0) |
| 1512 | { |
| 1513 | if (!is_array($value)) |
| 1514 | { |
| 1515 | $value = array($value); |
| 1516 | } |
| 1517 | |
| 1518 | // Add ENT_COMPAT default flag to html specialchars/entities functions, see PHPBB3-16690 |
| 1519 | if (in_array($execution, ['htmlspecialchars', 'htmlentities', 'htmlspecialchars_decode', 'html_entitity_decode'])) |
| 1520 | { |
| 1521 | $value[] = ENT_COMPAT; |
| 1522 | } |
| 1523 | |
| 1524 | $value = call_user_func_array($execution, $value); |
| 1525 | } |
| 1526 | else if (strpos($type, 'execute') === 0) |
| 1527 | { |
| 1528 | if (!is_array($value)) |
| 1529 | { |
| 1530 | $value = array($value); |
| 1531 | } |
| 1532 | |
| 1533 | $execution = str_replace('{RESULT}', '$value', $execution); |
| 1534 | $execution = str_replace('{VALUE}', '$value', $execution); |
| 1535 | // phpcs:disable Squiz.PHP.Eval |
| 1536 | eval($execution); |
| 1537 | // phpcs:enable Squiz.PHP.Eval |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | else |
| 1542 | { |
| 1543 | call_user_func_array($fields[2], $fields[1]); |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | return $sql_flag; |
| 1550 | } |
| 1551 | |
| 1552 | /** |
| 1553 | * Own meta refresh function to be able to change the global time used |
| 1554 | */ |
| 1555 | function meta_refresh($url) |
| 1556 | { |
| 1557 | global $convert; |
| 1558 | |
| 1559 | if ($convert->options['refresh']) |
| 1560 | { |
| 1561 | // Because we should not rely on correct settings, we simply use the relative path here directly. |
| 1562 | $this->template->assign_vars(array( |
| 1563 | 'S_REFRESH' => true, |
| 1564 | 'META' => '<meta http-equiv="refresh" content="5; url=' . $url . '" />') |
| 1565 | ); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | /** |
| 1570 | * Error handler function |
| 1571 | * |
| 1572 | * This function needs to be kept for BC |
| 1573 | * |
| 1574 | * @param $error |
| 1575 | * @param $line |
| 1576 | * @param $file |
| 1577 | * @param bool|false $skip |
| 1578 | */ |
| 1579 | public function error($error, $line, $file, $skip = false) |
| 1580 | { |
| 1581 | $this->template->assign_block_vars('errors', array( |
| 1582 | 'TITLE' => $error, |
| 1583 | 'DESCRIPTION' => 'In ' . $file . ' on line ' . $line, |
| 1584 | )); |
| 1585 | } |
| 1586 | |
| 1587 | /** |
| 1588 | * Database error handler function |
| 1589 | * |
| 1590 | * This function needs to be kept for BC |
| 1591 | * |
| 1592 | * @param $error |
| 1593 | * @param $sql |
| 1594 | * @param $line |
| 1595 | * @param $file |
| 1596 | * @param bool|false $skip |
| 1597 | */ |
| 1598 | public function db_error($error, $sql, $line, $file, $skip = false) |
| 1599 | { |
| 1600 | $this->template->assign_block_vars('errors', array( |
| 1601 | 'TITLE' => $error, |
| 1602 | 'DESCRIPTION' => 'In ' . $file . ' on line ' . $line . '<br /><br /><strong>SQL:</strong> ' . $sql, |
| 1603 | )); |
| 1604 | } |
| 1605 | } |