<commit_before><?php

function drupal() {
	
	static $drupalLink;
	if( ! $drupalLink )
		$drupalLink = new AutoDB( DR_HOST, DR_DB , DR_USER, DR_PASS );
	return $drupalLink;
	
}

function wordpress() {
	
	static $wpLink;
	if( ! $wpLink )
		$wpLink = new AutoDB( WP_HOST, WP_DB , WP_USER, WP_PASS );
	return $wpLink;
	
}

class Drupal_to_WP {
	
	static $node_to_post_map = array();
	static $term_to_term_map = array();
	static $user_to_user_map = array();
	static $file_to_file_map = array();
	
	/**
	 * Import Drupal nodes into WP posts, mapping types as specified
	 * Optionally tag/categorize imported content acording to user instructions
	 */
	static function importNodes( $type_map, $node_extras ) {
		
		extract( $node_extras );
		
		echo 'Importing posts...' . "<br>\n";
		
		# Build array of types that aren't being skipped
		$import_types = array();
		$import_types =  array_map(
			function( $key, $value ) {
				if( $value != 'skip' ) 
					return $key;
			},
			array_keys( $type_map ),
			$type_map
		);
		$import_types = array_filter( $import_types, 'strlen' );
		
		$nodes = drupal()->node->getRecords(
			array(
				'type' => $import_types
			)
		);
		
		foreach( $nodes as $node ) {
			
			if( apply_filters( 'import_node_skip_node', false, $node ) )
				continue;
			
			# Get content from latest revision
			$node_content = drupal()->node_revisions->getRecord(
				array(
					'nid'  => $node['nid'],
					'DESC' => 'timestamp'
				)
			);

			# Get timestamp from earliest revision and overwrite latest record
			$node_creation = drupal()->node_revisions->getRecord(
				array(
					'nid'  => $node['nid'],
					'ASC' => 'timestamp'
				)
			);
			$node_content['mod_timestamp'] = $node_content['timestamp'];
			$node_content['timestamp'] = $node_creation['timestamp'];
			
			
			# Get slug from url_alias table
			$node_url = drupal()->url_alias->dst->getValue(
				array(
					'src' => 'node/' . $node['nid']
				)
			);
			
			if( false === $node_url || empty( $node_url ) ) {
				echo 'WARNING - No URL available for node: ' . $node['nid'];
				echo ' -- generating a slug from the title' . "<br>\n";
				$node_url = strtolower( str_replace( ' ','-', $node['title'] ) );
			}
			
			$post_author = array_key_exists( $node_content['uid'], self::$user_to_user_map ) ? self::$user_to_user_map[ $node_content['uid'] ] : 0;
			
			// If user requested a new parent page, create or locate it
			if( ! empty( $parents ) && array_key_exists( $node['type'], $parents ) && ! empty( $parents[ $node['type'] ] ) ) {
				
				$parent_page = $parents[ $node['type'] ];
				
				if( is_numeric( $parent_page ) ) {
					
					if( ! get_post( (int)$parent_page ) ) {
						$parent_page = 0;
					}
					
				} else {
					
					$page = get_page_by_path(
						untrailingslashit( strtolower( str_replace( ' ','-', $parent_page ) ) ), 
						OBJECT, 
						$_POST['content_map'][ $node['type'] ]
					);
					
					if( $page ) {
						
						$parent_page = (int)$page->ID;
						
					} else {
						
						$post_data = array(
							'post_type'    => $_POST['content_map'][ $node['type'] ],
							'post_title'   => $parent_page,
							'post_name'    => strtolower( str_replace( ' ','-', $parent_page ) ),
							'post_content' => 'Created by Drupal to WP importer' 
						);
						
						$parent_page = wp_insert_post( $post_data );
						
					}
					
				}
				
			} else {
				$parent_page = 0;
			}
			
			$post_data = array(
				'post_content'   => $node_content['body'],
				'post_date'      => date( 'Y-m-d H:i:s', $node_content['timestamp'] ),
//				'post_date_gmt'  => date( 'Y-m-d H:i:s', $node_content['timestamp'] ),
				'most_modified'  => date( 'Y-m-d H:i:s', $node_content['mod_timestamp'] ),
				'post_excerpt'   => $node_content['teaser'],
				'post_name'      => basename( $node_url ),
				'post_author'    => $post_author,
				'post_parent'    => (int)$parent_page,
				'post_password'  => '',
				'post_status'    => ( $node['status'] == 1 ? 'publish' : 'draft' ),
				'post_title'     => $node['title'],
				'post_type'      => $_POST['content_map'][ $node['type'] ]
			);
			
			$post_data = apply_filters(
				sprintf( 'import_node_pre_%s', $_POST['content_map'][ $node['type'] ] ),
				$post_data,
				$node 
			);
			
			$new_post_id = wp_insert_post(
				$post_data
			);
			update_post_meta( $new_post_id, '_drupal_nid', $node['nid'] );
			self::$node_to_post_map[ $node['nid'] ] = $new_post_id;
			
			// Store all other URL aliases
			$aliases = drupal()->url_alias->dst->getValues(
				array(
					'src' => 'node/' . $node['nid']
				)
			);
			update_post_meta( $new_post_id, '_drupal_aliases', $aliases );
			
			// Add tags / categories if requested
			
			if( array_key_exists( $node['type'], $add_tag_map ) ) {
				wp_set_post_tags(
					$new_post_id,
					$add_tag_map[ $node['type'] ],
					true
				);
			}
			
			if( ! function_exists( 'wp_create_category' ) )
				require WP_PATH . '/wp-admin/includes/taxonomy.php';
			
			if( array_key_exists( $node['type'], $add_cat_map ) ) {
				
				$term = term_exists( $add_cat_map[ $node['type'] ], 'category' );
				if( ! $term ) {
					$term = wp_create_category( $add_cat_map[ $node['type'] ] );
				}
				<commit_msg>Tag imported nodes with source Drupal database name<commit_after><?php

function drupal() {
	
	static $drupalLink;
	if( ! $drupalLink )
		$drupalLink = new AutoDB( DR_HOST, DR_DB , DR_USER, DR_PASS );
	return $drupalLink;
	
}

function wordpress() {
	
	static $wpLink;
	if( ! $wpLink )
		$wpLink = new AutoDB( WP_HOST, WP_DB , WP_USER, WP_PASS );
	return $wpLink;
	
}

class Drupal_to_WP {
	
	static $node_to_post_map = array();
	static $term_to_term_map = array();
	static $user_to_user_map = array();
	static $file_to_file_map = array();
	
	/**
	 * Import Drupal nodes into WP posts, mapping types as specified
	 * Optionally tag/categorize imported content acording to user instructions
	 */
	static function importNodes( $type_map, $node_extras ) {
		
		extract( $node_extras );
		
		echo 'Importing posts...' . "<br>\n";
		
		# Build array of types that aren't being skipped
		$import_types = array();
		$import_types =  array_map(
			function( $key, $value ) {
				if( $value != 'skip' ) 
					return $key;
			},
			array_keys( $type_map ),
			$type_map
		);
		$import_types = array_filter( $import_types, 'strlen' );
		
		$nodes = drupal()->node->getRecords(
			array(
				'type' => $import_types
			)
		);
		
		foreach( $nodes as $node ) {
			
			if( apply_filters( 'import_node_skip_node', false, $node ) )
				continue;
			
			# Get content from latest revision
			$node_content = drupal()->node_revisions->getRecord(
				array(
					'nid'  => $node['nid'],
					'DESC' => 'timestamp'
				)
			);

			# Get timestamp from earliest revision and overwrite latest record
			$node_creation = drupal()->node_revisions->getRecord(
				array(
					'nid'  => $node['nid'],
					'ASC' => 'timestamp'
				)
			);
			$node_content['mod_timestamp'] = $node_content['timestamp'];
			$node_content['timestamp'] = $node_creation['timestamp'];
			
			
			# Get slug from url_alias table
			$node_url = drupal()->url_alias->dst->getValue(
				array(
					'src' => 'node/' . $node['nid']
				)
			);
			
			if( false === $node_url || empty( $node_url ) ) {
				echo 'WARNING - No URL available for node: ' . $node['nid'];
				echo ' -- generating a slug from the title' . "<br>\n";
				$node_url = strtolower( str_replace( ' ','-', $node['title'] ) );
			}
			
			$post_author = array_key_exists( $node_content['uid'], self::$user_to_user_map ) ? self::$user_to_user_map[ $node_content['uid'] ] : 0;
			
			// If user requested a new parent page, create or locate it
			if( ! empty( $parents ) && array_key_exists( $node['type'], $parents ) && ! empty( $parents[ $node['type'] ] ) ) {
				
				$parent_page = $parents[ $node['type'] ];
				
				if( is_numeric( $parent_page ) ) {
					
					if( ! get_post( (int)$parent_page ) ) {
						$parent_page = 0;
					}
					
				} else {
					
					$page = get_page_by_path(
						untrailingslashit( strtolower( str_replace( ' ','-', $parent_page ) ) ), 
						OBJECT, 
						$_POST['content_map'][ $node['type'] ]
					);
					
					if( $page ) {
						
						$parent_page = (int)$page->ID;
						
					} else {
						
						$post_data = array(
							'post_type'    => $_POST['content_map'][ $node['type'] ],
							'post_title'   => $parent_page,
							'post_name'    => strtolower( str_replace( ' ','-', $parent_page ) ),
							'post_content' => 'Created by Drupal to WP importer' 
						);
						
						$parent_page = wp_insert_post( $post_data );
						
					}
					
				}
				
			} else {
				$parent_page = 0;
			}
			
			$post_data = array(
				'post_content'   => $node_content['body'],
				'post_date'      => date( 'Y-m-d H:i:s', $node_content['timestamp'] ),
//				'post_date_gmt'  => date( 'Y-m-d H:i:s', $node_content['timestamp'] ),
				'most_modified'  => date( 'Y-m-d H:i:s', $node_content['mod_timestamp'] ),
				'post_excerpt'   => $node_content['teaser'],
				'post_name'      => basename( $node_url ),
				'post_author'    => $post_author,
				'post_parent'    => (int)$parent_page,
				'post_password'  => '',
				'post_status'    => ( $node['status'] == 1 ? 'publish' : 'draft' ),
				'post_title'     => $node['title'],
				'post_type'      => $_POST['content_map'][ $node['type'] ]
			);
			
			$post_data = apply_filters(
				sprintf( 'import_node_pre_%s', $_POST['content_map'][ $node['type'] ] ),
				$post_data,
				$node 
			);
			
			$new_post_id = wp_insert_post(
				$post_data
			);
			update_post_meta( $new_post_id, '_drupal_nid', $node['nid'] );
			update_post_meta( $new_post_id, '_drupal_database', drupal()->dbName );
			self::$node_to_post_map[ $node['nid'] ] = $new_post_id;
			
			// Store all other URL aliases
			$aliases = drupal()->url_alias->dst->getValues(
				array(
					'src' => 'node/' . $node['nid']
				)
			);
			update_post_meta( $new_post_id, '_drupal_aliases', $aliases );
			
			// Add tags / categories if requested
			
			if( array_key_exists( $node['type'], $add_tag_map ) ) {
				wp_set_post_tags(
					$new_post_id,
					$add_tag_map[ $node['type'] ],
					true
				);
			}
			
			if( ! function_exists( 'wp_create_category' ) )
				require WP_PATH . '/wp-admin/includes/taxonomy.php';
			
			if( array_key_exists( $node['type'], $add_cat_map ) ) {
				
				$term = term_exists( $add_cat_map[ $node['type'] ], 'category' );
				if( ! $term ) {
					$term = wp_create_category( $add_cat_map[ $node['type'] ] );
				}
				
{'fraction_non_alphanumeric': 0.1708914839077186, 'fraction_numerical': 0.0007595177062565271, 'mean_word_length': 2.8403208166241343, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3292969', 'n_tokens_mistral': 4791, 'n_tokens_neox': 3780, 'n_words': 747}