<commit_before><?php
namespace Podlove\Modules\ImportExport;

class Importer {

	// path to import file
	private $file;

	// SimpleXML document of import file
	private $xml;

	public function __construct($file) {
		$this->file = $file;
	}

	public function import() {

		$this->xml = simplexml_load_file($this->file);
		$this->xml->registerXPathNamespace('wpe', \Podlove\Exporter::XML_NAMESPACE);

		// TODO: clean podlove tables beforehand?

		$this->importEpisodes();
	}

	private function importEpisodes()
	{
		global $wpdb;
		
		$episodes = $this->xml->xpath('//wpe:episode');
		foreach ($episodes as $episode) {
			$new_episode = new \Podlove\Model\Episode;

			foreach ($episode->children('wpe', true) as $attribute) {
				$value = (string) $attribute;
				$wpdb->escape_by_ref($value);
				$new_episode->{$attribute->getName()} = $value;
			}

			if ($new_post_id = $this->getNewPostId($new_episode->post_id)) {
				$new_episode->post_id = $new_post_id;
				$new_episode->save();
			} else {
				// no matching post found
			}
		}
	}

	/**
	 * Get mapping for post id after post import.
	 *
	 * When importing posts, their IDs might change.
	 * This function maps an existing post id to the new one.
	 * 
	 * @param  int      $old_post_id
	 * @return int|null post_id on success, otherwise null.
	 */
	private function getNewPostId($old_post_id)
	{
		$query_for_post_id = new \WP_Query(array(
			'post_type' => 'podcast',
			'meta_query' => array(
				array(
					'key' => 'import_id',
					'value' => $old_post_id,
					'compare' => '='
				)
			)<commit_msg>add template import<commit_after><?php
namespace Podlove\Modules\ImportExport;

use Podlove\Model;

class Importer {

	// path to import file
	private $file;

	// SimpleXML document of import file
	private $xml;

	public function __construct($file) {
		$this->file = $file;
	}

	public function import() {

		$this->xml = simplexml_load_file($this->file);
		$this->xml->registerXPathNamespace('wpe', \Podlove\Exporter::XML_NAMESPACE);

		// TODO: clean podlove tables beforehand?

		$this->importEpisodes();
		$this->importTemplates();
	}

	private function importEpisodes()
	{
		global $wpdb;

		$episodes = $this->xml->xpath('//wpe:episode');
		foreach ($episodes as $episode) {
			$new_episode = new Model\Episode;

			foreach ($episode->children('wpe', true) as $attribute) {
				$value = (string) $attribute;
				$wpdb->escape_by_ref($value);
				$new_episode->{$attribute->getName()} = $value;
			}

			if ($new_post_id = $this->getNewPostId($new_episode->post_id)) {
				$new_episode->post_id = $new_post_id;
				$new_episode->save();
			} else {
				// no matching post found
			}
		}
	}

	private function importTemplates()
	{
		global $wpdb;

		$templates = $this->xml->xpath('//wpe:template');
		foreach ($templates as $template) {
			$new_template = new Model\Template;

			foreach ($template->children('wpe', true) as $attribute) {
				$value = (string) $attribute;
				$wpdb->escape_by_ref($value);
				$new_template->{$attribute->getName()} = $value;
			}

			$new_template->save();
		}
	}

	/**
	 * Get mapping for post id after post import.
	 *
	 * When importing posts, their IDs might change.
	 * This function maps an existing post id to the new one.
	 * 
	 * @param  int      $old_post_id
	 * @return int|null post_id on success, otherwise null.
	 */
	private function getNewPostId($old_post_id)
	{
		$query_for_post_id = new \WP_Query(array(
			'post_type' => 'podcast',
			'meta_query' => array(
				array(
					'key' => 'import_id',
					'value' => $old_post_id,
					'compare' => '='
				)
			)
{'fraction_non_alphanumeric': 0.1539319576129392, 'fraction_numerical': 0.0, 'mean_word_length': 3.472568578553616, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 11}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2750154', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1241, 'n_words': 279}