/** * Get data from wordpress db * * @return array Result array */ function get_wordpress_data() { $host_name = post_param('wp_host'); $db_name = post_param('wp_db'); $db_user = post_param('wp_db_user'); $db_passwrod = post_param('wp_db_password'); $db_table_prefix = post_param('wp_table_prefix'); // Create DB connection $db = new database_driver($db_name, $host_name, $db_user, $db_passwrod, $db_table_prefix); $row = $db->query('SELECT * FROM ' . db_escape_string($db_name) . '.' . db_escape_string($db_table_prefix) . '_users'); $data = array(); foreach ($row as $users) { $user_id = $users['ID']; $data[$user_id] = $users; // Fetch user posts $row1 = $db->query('SELECT * FROM ' . $db_table_prefix . '_posts WHERE post_author=' . strval($user_id) . ' AND (post_type=\'post\' OR post_type=\'page\')'); foreach ($row1 as $posts) { $post_id = $posts['ID']; $data[$user_id]['POSTS'][$post_id] = $posts; // Get categories $row3 = $db->query('SELECT t1.slug,t1.name FROM ' . $db_table_prefix . '_terms t1,' . db_escape_string($db_name) . '.' . db_escape_string($db_table_prefix) . '_term_relationships t2 WHERE t1.term_id=t2.term_taxonomy_id AND t2.object_id=' . strval($post_id)); foreach ($row3 as $categories) { $data[$user_id]['POSTS'][$post_id]['category'][$categories['slug']] = $categories['name']; } // Comments $row2 = $db->query('SELECT * FROM ' . $db_table_prefix . '_comments WHERE comment_post_ID=' . strval($post_id) . ' AND comment_approved=1'); foreach ($row2 as $comments) { $comment_id = $comments['comment_ID']; $data[$user_id]['POSTS'][$post_id]['COMMENTS'][$comment_id] = $comments; } } } return $data; }
/** * Script to handle XML DB/MySQL chain synching. */ function xml_dump_script() { // Run checks and set up chain DB if (get_db_type() != 'xml') { warn_exit('It makes no sense to run this script if you are not running the XML database driver.'); } global $SITE_INFO; if (array_key_exists('db_chain_type', $SITE_INFO)) { require_code('database/' . $SITE_INFO['db_chain_type']); $chain_db = new database_driver($SITE_INFO['db_chain'], $SITE_INFO['db_chain_host'], $SITE_INFO['db_chain_user'], $SITE_INFO['db_chain_password'], get_table_prefix(), false, object_factory('Database_Static_' . $SITE_INFO['db_chain_type'])); } else { warn_exit('It makes no sense to run this script if you have not set up the following config options in info.php: db_chain_type, db_chain_host, db_chain_user, db_chain_password, db_chain'); } $chain_connection =& $chain_db->connection_write; if (count($chain_connection) > 4) { $chain_connection = call_user_func_array(array($chain_db->static_ob, 'db_get_connection'), $chain_connection); _general_db_init(); } if (function_exists('set_time_limit')) { @set_time_limit(0); } $GLOBALS['DEBUG_MODE'] = false; $GLOBALS['SEMI_DEBUG_MODE'] = false; @ini_set('ocproducts.xss_detect', '0'); if (strtolower(ocp_srv('REQUEST_METHOD')) == 'get') { $from = get_param('from', NULL); $skip = get_param('skip', NULL); $only = get_param('only', NULL); echo ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>XML/MySQL DB syncher</title> </head> <body> '; echo '<p>Select the tables to sync below. Tables have been auto-ticked based on what seems to need re-synching.</p>'; $keep = symbol_tempcode('KEEP', array('1')); echo '<form title="Choose tables" method="post" action="' . escape_html(find_script('xml_db_import') . $keep->evaluate()) . '">'; $tables = array_keys(find_all_tables($GLOBALS['SITE_DB'])); $mysql_status = list_to_map('Name', $chain_db->query('SHOW TABLE STATUS')); $mysql_tables = array_keys($mysql_status); foreach ($tables as $table_name) { $default_selected = (!is_null($from) && $table_name >= $from || !is_null($only) && in_array($table_name, explode(',', $only))) && (!is_null($skip) || !in_array($table_name, explode(',', $skip))); $missing = !in_array(get_table_prefix() . $table_name, $mysql_tables); $count_mismatch = !$missing && $chain_db->query_value($table_name, 'COUNT(*)') != $GLOBALS['SITE_DB']->query_value($table_name, 'COUNT(*)'); $date_mismatch = false; if (!$missing && !$count_mismatch) { $last_m_time = NULL; $path = get_custom_file_base() . '/uploads/website_specific/' . get_db_site() . '/' . get_table_prefix() . $table_name; $dh = @opendir($path); if ($dh !== false) { while (($f = readdir($dh)) !== false) { if (substr($f, -4) == '.dat' || substr($f, -4) == '.xml') { $last_m_time = @max($last_m_time, filemtime($path . '/' . $f)); } // @ because of the 255 read filepath limit on Windows } closedir($dh); } if (!is_null($last_m_time)) { $mysql_time = strtotime($mysql_status[get_table_prefix() . $table_name]['Update_time']); $date_mismatch = $mysql_time < $last_m_time; // We can't do "!=" as last m-time for MySQL could well by the last sync time } } $needs_doing = $count_mismatch || $date_mismatch || $missing || $default_selected; echo ' <div style="width: 500px"> <span style="float: right; font-style: italic"> ' . ($missing ? '[table is missing]' : '') . ' ' . ($count_mismatch ? '[different record-counts]' : '') . ' ' . ($date_mismatch ? '[different last-modified-time]' : '') . ' </span> <input ' . ($needs_doing ? 'checked="checked" ' : '') . 'type="checkbox" name="table_' . htmlentities($table_name) . '" id="table_' . htmlentities($table_name) . '" value="1" /> <label for="table_' . htmlentities($table_name) . '">' . htmlentities($table_name) . '</label> </div> '; } echo '<p><input type="submit" value="Sync" /> [<a href="#" onclick="var form=document.getElementsByTagName(\'form\')[0]; for (var i=0;i<form.elements.length;i++) if (form.elements[i].checked) form.elements[i].checked=false; return false;">un-tick all</a>]</p>'; echo '</form>'; echo ' </body> </html> '; exit; } // Actualiser $from = NULL; $skip = NULL; $only = ''; foreach (array_keys($_POST) as $key) { if (substr($key, 0, 6) == 'table_') { if ($only != '') { $only .= ','; } $only .= substr($key, 6); } } if ($only == '') { $only = NULL; } @header('Content-type: text/plain'); @ob_end_clean(); $sql = get_sql_dump(true, true, $from, is_null($skip) ? array() : explode(',', $skip), is_null($only) ? NULL : explode(',', $only)); $cnt = count($sql); foreach ($sql as $i => $s) { print 'Executing query ' . strval($i + 1) . '/' . strval($cnt) . ' ... ' . $s . "\n\n"; flush(); $fail_ok = substr($s, 0, 5) == 'ALTER'; $chain_db->static_ob->db_query($s, $chain_connection, NULL, NULL, $fail_ok, false); } print '!!Done!!'; }