function buildQueryCreateWPPosts($wp_settings_array, $d_settings_array)
{
    // Get all available Drupal node types
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_NODE_TYPES, $errors);
    $node_types_count = count($result);
    $node_types_params_list = buildNodeTypesParamsList($result);
    // Insert associated nodes as WordPress posts
    $query = "INSERT INTO " . $wp_settings_array['data'] . ".wp_posts " . "(id, post_author, post_date, post_content, post_title, post_excerpt, " . "post_name, post_modified, post_type, post_status, to_ping, pinged, post_content_filtered) " . "SELECT DISTINCT " . "n.nid 'id', " . "n.uid 'post_author', " . "DATE_ADD(FROM_UNIXTIME(0), interval n.created second) 'post_date', " . "r.body 'post_content', " . "n.title 'post_title', " . "r.teaser 'post_excerpt', " . "IF(a.dst IS NULL, n.nid, SUBSTRING_INDEX(a.dst, '/', -1)) 'post_name', " . "DATE_ADD(FROM_UNIXTIME(0), interval n.changed second) 'post_modified', " . "n.type 'post_type', " . "IF(n.status = 1, 'publish', 'private') 'post_status', " . "' ', " . "' ', " . "' ' " . "FROM " . $d_settings_array['data'] . ".node n " . "INNER JOIN " . $d_settings_array['data'] . ".node_revisions r " . "USING(vid) " . "LEFT OUTER JOIN " . $d_settings_array['data'] . ".url_alias a " . "ON a.src = CONCAT('node/', n.nid) " . "WHERE n.type IN (" . $node_types_params_list . ");";
    return $query;
}
function displayAnalysisResultsPage($wp_settings_array, $d_settings_array)
{
    $errors = "";
    $abort = false;
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_TERMS, $errors);
    $terms_count = count($result);
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_NODE_TYPES, $errors);
    $node_types_result = $result;
    $node_types_count = count($result);
    $node_types_params_list = buildNodeTypesParamsList($result);
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_POSTS, $errors);
    $posts_count = count($result);
    // Check for problems
    $result_dup_terms = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_DUPLICATE_TERMS, $errors);
    $duplicate_terms_count = count($result_dup_terms);
    $result_terms_charlength_exceeded = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_TERMS_CHARLENGTH, $errors);
    $terms_charlength_exceeded_count = count($result_terms_charlength_exceeded);
    $result_dup_aliases = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_DUPLICATE_ALIAS, $errors);
    $duplicate_aliases_count = count($result_dup_aliases);
    if ($duplicate_terms_count > 0 || $terms_charlength_exceeded_count > 0 || $duplicate_aliases_count > 0) {
        $abort = true;
    }
    // Show the database analysis results
    showHTMLHeader("Drupal database analysis", $errors);
    echo "<table class=\"analysis_table\"><caption>Drupal properties</caption><thead>";
    echo "<th class=\"property\">Property</th><th class=\"found\">Found in Drupal</th></thead><tbody>";
    echo "<tr><td>Terms</td><td>{$terms_count} terms</td></tr>";
    echo "<tr><td>Node types</td><td>{$node_types_count} node types (" . $node_types_params_list . ")</td></tr>";
    echo "<tr><td>Entries</td><td>{$posts_count} entries</td></tr>";
    echo "</tbody></table>";
    // Any potential problems? Don't show migration options
    if ($abort) {
        echo "<table class=\"problems_table\"><caption id=\"problems\">Possible problems</caption><thead>";
        echo "<th class=\"problem_property\">Problem</th><th class=\"problem_found\">Description</th></thead><tbody>";
        // Duplicate terms?
        if ($duplicate_terms_count > 0) {
            showDuplicateTermsRow($result_dup_terms, $duplicate_terms_count, $d_settings_array);
        }
        // Exceeded terms character length?
        if ($terms_charlength_exceeded_count > 0) {
            showCharLengthExceededRow($result_terms_charlength_exceeded, $terms_charlength_exceeded_count, $d_settings_array);
        }
        // Duplicate aliases?
        if ($duplicate_aliases_count > 0) {
            showDuplicateAliasesRow($result_dup_aliases, $duplicate_aliases_count, $d_settings_array);
        }
        echo "</tbody></table>";
    }
    ?>
	<form action="<?php 
    step_form_action();
    ?>
" method="post">
		<input type="hidden" name="wp_host" id="wp_host" value="<?php 
    esc_html_attr($wp_settings_array['host'], true);
    ?>
">
		<input type="hidden" name="d_host" id="d_host" value="<?php 
    esc_html_attr($d_settings_array['host'], true);
    ?>
">
		<input type="hidden" name="wp_data" id="wp_data" value="<?php 
    esc_html_attr($wp_settings_array['data'], true);
    ?>
">
		<input type="hidden" name="d_data" id="d_data" value="<?php 
    esc_html_attr($d_settings_array['data'], true);
    ?>
">
		<input type="hidden" name="wp_user" id="wp_user" value="<?php 
    esc_html_attr($wp_settings_array['user'], true);
    ?>
">
		<input type="hidden" name="d_user" id="d_user" value="<?php 
    esc_html_attr($d_settings_array['user'], true);
    ?>
">
		<input type="hidden" name="wp_pass" id="wp_pass" value="<?php 
    esc_html_attr($wp_settings_array['pass'], true);
    ?>
">
		<input type="hidden" name="d_pass" id="d_pass" value="<?php 
    esc_html_attr($d_settings_array['pass'], true);
    ?>
">
		<input type="hidden" name="wp_char" id="wp_char" value="<?php 
    esc_html_attr($wp_settings_array['char'], true);
    ?>
">
		<input type="hidden" name="d_char" id="d_char" value="<?php 
    esc_html_attr($d_settings_array['char'], true);
    ?>
">		
	<?php 
    step_submit("Set options");
    ?>
	</form>
	
	<div style="visibility:hidden">
		<p id="status"></p>
	</div>
	
	<?php 
    showHTMLFooter();
}
function displaySetOptionsPage($wp_settings_array, $d_settings_array)
{
    $errors = "";
    $abort = false;
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_TERMS, $errors);
    $terms_count = count($result);
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_NODE_TYPES, $errors);
    $node_types_result = $result;
    $node_types_count = count($result);
    $node_types_params_list = buildNodeTypesParamsList($result);
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_POSTS, $errors);
    $posts_count = count($result);
    showHTMLHeader("Drupal migration", $errors);
    ?>
	<form action="<?php 
    step_form_action();
    ?>
" method="post">

	<form action="../" onsubmit="return checkCheckBoxes(this);" >
	<input type="hidden" name="wp_host" id="wp_host" value="<?php 
    esc_html_attr($wp_settings_array['host'], true);
    ?>
">
	<input type="hidden" name="d_host" id="d_host" value="<?php 
    esc_html_attr($d_settings_array['host'], true);
    ?>
">
	<input type="hidden" name="wp_data" id="wp_data" value="<?php 
    esc_html_attr($wp_settings_array['data'], true);
    ?>
">
	<input type="hidden" name="d_data" id="d_data" value="<?php 
    esc_html_attr($d_settings_array['data'], true);
    ?>
">
	<input type="hidden" name="wp_user" id="wp_user" value="<?php 
    esc_html_attr($wp_settings_array['user'], true);
    ?>
">
	<input type="hidden" name="d_user" id="d_user" value="<?php 
    esc_html_attr($d_settings_array['user'], true);
    ?>
">
	<input type="hidden" name="wp_pass" id="wp_pass" value="<?php 
    esc_html_attr($wp_settings_array['pass'], true);
    ?>
">
	<input type="hidden" name="d_pass" id="d_pass" value="<?php 
    esc_html_attr($d_settings_array['pass'], true);
    ?>
">
	<input type="hidden" name="wp_char" id="wp_char" value="<?php 
    esc_html_attr($wp_settings_array['char'], true);
    ?>
">
	<input type="hidden" name="d_char" id="d_char" value="<?php 
    esc_html_attr($d_settings_array['char'], true);
    ?>
">
	
	<table class="settings_table">
		<caption>Migration options</caption>
		<thead>
			<tr>
				<th class="description">Description</th>				
				<th class="setting">Setting</th>		
			</tr>
		</thead>
		<tbody>
			<tr>
				<td class="description">Delete additional authors in WordPress? (Default admin user created during installation will not be deleted.)</td>				
				<td class="setting"><input type="checkbox" name="formDeleteAuthors" value="Yes" /></td>
			</tr>
			<tr>
					<?php 
    showContentTypeConversion($node_types_result, $node_types_count);
    ?>
			</tr>
			<tr>
				<td class="description">Drupal file directory</td>
				<td class="setting"><input type="text" size="20" maxlength="255" name="formFilePath" value="/files/"></td>				
			</tr>
			<tr>
				<td class="description">Permalink structure</td>
				<td class="setting"><input type="text" size="20" maxlength="255" name="formPermalinkStructure" value="/content/%postname%/"></td>				
			</tr>
			<tr>
				<td>Please select which Drupal terms will be used as WordPress categories. The remaining terms will be converted into WordPress post tags. It's best not to create too many categories.</td>
				<td><select size="5" name="formTerms[]" multiple="yes">
				<?php 
    $result = runFetchFromDatabase($d_settings_array, QUERY_DRUPAL_GET_TERMS, $errors);
    $terms_count = count($result);
    if ($terms_count) {
        foreach ($result as $row_key => $row_val) {
            echo "<option value='" . $row_val['tid'] . "'>" . $row_val['name'] . " (ID " . $row_val['tid'] . ")</option>";
        }
    }
    ?>
				</select></td>
			</tr>
			<tr>
				<td>Please select your WordPress default category.</td>
				<td><select size="5" name="formDefaultCategory">
				<?php 
    if ($terms_count) {
        foreach ($result as $row_key => $row_val) {
            echo "<option value='" . $row_val['tid'] . "'>" . $row_val['name'] . " (ID " . $row_val['tid'] . ")</option>";
        }
    }
    ?>
				</select></td>
			</tr>			
			
			
		</tbody>
	</table>
		<?php 
    step_submit("Migrate now", "This will alter your WordPress tables. Are you sure?");
    ?>
	</form>
	
	
	<script>
	function checkCheckBoxes(theForm) {
		if (
			
		<?php 
    $checkCount = 0;
    if ($node_types_count) {
        foreach ($node_types_result as $row_key => $row_val) {
            echo "theForm.formContentTypes[{$checkCount}].checked == false";
            $checkCount = $checkCount + 1;
            if ($checkCount < $node_types_count) {
                echo " && ";
            } else {
                echo " ) ";
            }
        }
    }
    ?>
		{
			alert ('You didn\'t choose any of the checkboxes!');
			return false;
		} else { 	
			return true;
		}
	}
	</script>
	
<?php 
    showHTMLFooter();
}