<commit_before><?php
	require_once "../includes/common.php";

	if (ini_get("memory_limit") < 20) # Some imports are relatively large.
		ini_set("memory_limit", "20M");

	if (!in_array("text", $config->enabled_feathers))
		error(__("Missing Feather"), __("Importing from WordPress requires the Text feather to be installed and enabled."));

	if (!$visitor->group()->can("add_post"))
		error(__("Access Denied"), __("You do not have sufficient privileges to create posts."));

	$errors = array();
	if (!empty($_POST)) {
		switch($_POST['step']) {
			case "1":
				$stupid_xml = utf8_encode(file_get_contents($_FILES['xml_file']['tmp_name']));
				$sane_xml = preg_replace(array("/<wp:comment_content>(?!<!\[CDATA\[)/", "/(?!\]\]>)<\/wp:comment_content>/"),
				                         array("<wp:comment_content><![CDATA[", "]]></wp:comment_content>"),
				                         $stupid_xml);

				$fix_amps_count = 1;
				while ($fix_amps_count)
					$sane_xml = preg_replace("/<wp:meta_value>(.+)&(?!amp;)(.+)<\/wp:meta_value>/m",
					                         "<wp:meta_value>\\1&amp;\\2</wp:meta_value>",
					                         $sane_xml, -1, $fix_amps_count);

				$xml = simplexml_load_string($sane_xml, "SimpleXMLElement", LIBXML_NOCDATA);

				if ($xml and strpos($xml->channel->generator, "wordpress.org")) {
					foreach ($xml->channel->item as $item) {
						$wordpress = $item->children("http://wordpress.org/export/1.0/");
						$content   = $item->children("http://purl.org/rss/1.0/modules/content/");
						if ($wordpress->status == "attachment" or $item->title == "zz_placeholder")
							continue;

						if (!empty($_POST['media_url']) and preg_match_all("/".preg_quote($_POST['media_url'], "/")."([^ \t\"]+)/", $content->encoded, $media))
							foreach ($media[0] as $matched_url) {
								$file = tempnam(sys_get_temp_dir(), "chyrp");
								file_put_contents($file, get_remote($matched_url));
								$fake_file = array("name" => basename(parse_url($matched_url, PHP_URL_PATH)),
								                   "tmp_name" => $file);
								$filename = upload($fake_file, null, "", true);
								$content->encoded = str_replace($matched_url, $config->url.$config->uploads_path.$filename, $content->encoded);
							}

						$clean = (isset($wordpress->post_name)) ? $wordpress->post_name : sanitize($item->title) ;

						if (empty($wordpress->post_type) or $wordpress->post_type == "post") {
							$status_translate = array("publish"    => "public",
							                          "draft"      => "draft",
							                          "private"    => "private",
							                          "static"     => "public",
							                          "object"     => "public",
							                          "inherit"    => "public",
							                          "future"     => "draft",
							                          "pending"    => "draft");

							$_POST['status']  = str_replace(array_keys($status_translate), array_values($status_translate), $wordpress->status);
							$_POST['pinned']  = false;
							$_POST['created_at'] = ($wordpress->post_date == "0000-00-00 00:00:00") ? datetime() : $wordpress->post_date ;
							$_POST['feather'] = "text";

							$data = array("title" => (string) $item->title, "body" => (string) $content->encoded);
							$post = Post::add($data, $clean, Post::check_url($clean));

							$trigger->call("import_wordpress_post", array($item, $post));
						} elseif ($wordpress->post_type == "page") {
							$page = Page::add((string) $item->title, (string) $content->encoded, 0, true, 0, $clean, Page::check_url($clean));
							$trigger->call("import_wordpress_page", array($item, $post));
						}
					}
					$step = "2";
				} else
					$errors[] = __("File does not seem to be a valid WordPress export file.");

				break;<commit_msg>Fixed some imported content being parsed as arrays.<commit_after><?php
	require_once "../includes/common.php";

	if (ini_get("memory_limit") < 20) # Some imports are relatively large.
		ini_set("memory_limit", "20M");

	if (!in_array("text", $config->enabled_feathers))
		error(__("Missing Feather"), __("Importing from WordPress requires the Text feather to be installed and enabled."));

	if (!$visitor->group()->can("add_post"))
		error(__("Access Denied"), __("You do not have sufficient privileges to create posts."));

	$errors = array();
	if (!empty($_POST)) {
		switch($_POST['step']) {
			case "1":
				$stupid_xml = utf8_encode(file_get_contents($_FILES['xml_file']['tmp_name']));
				$sane_xml = preg_replace(array("/<wp:comment_content>(?!<!\[CDATA\[)/", "/(?!\]\]>)<\/wp:comment_content>/"),
				                         array("<wp:comment_content><![CDATA[", "]]></wp:comment_content>"),
				                         $stupid_xml);

				$fix_amps_count = 1;
				while ($fix_amps_count)
					$sane_xml = preg_replace("/<wp:meta_value>(.+)&(?!amp;)(.+)<\/wp:meta_value>/m",
					                         "<wp:meta_value>\\1&amp;\\2</wp:meta_value>",
					                         $sane_xml, -1, $fix_amps_count);

				$xml = simplexml_load_string($sane_xml, "SimpleXMLElement", LIBXML_NOCDATA);

				if ($xml and strpos($xml->channel->generator, "wordpress.org")) {
					foreach ($xml->channel->item as $item) {
						$wordpress = $item->children("http://wordpress.org/export/1.0/");
						$content   = $item->children("http://purl.org/rss/1.0/modules/content/");
						if ($wordpress->status == "attachment" or $item->title == "zz_placeholder")
							continue;

						if (!empty($_POST['media_url']) and preg_match_all("/".preg_quote($_POST['media_url'], "/")."([^ \t\"]+)/", $content->encoded, $media))
							foreach ($media[0] as $matched_url) {
								$file = tempnam(sys_get_temp_dir(), "chyrp");
								file_put_contents($file, get_remote($matched_url));
								$fake_file = array("name" => basename(parse_url($matched_url, PHP_URL_PATH)),
								                   "tmp_name" => $file);
								$filename = upload($fake_file, null, "", true);
								$content->encoded = str_replace($matched_url, $config->url.$config->uploads_path.$filename, $content->encoded);
							}

						$clean = (isset($wordpress->post_name)) ? $wordpress->post_name : sanitize($item->title) ;

						if (empty($wordpress->post_type) or $wordpress->post_type == "post") {
							$status_translate = array("publish"    => "public",
							                          "draft"      => "draft",
							                          "private"    => "private",
							                          "static"     => "public",
							                          "object"     => "public",
							                          "inherit"    => "public",
							                          "future"     => "draft",
							                          "pending"    => "draft");

							$_POST['status']  = str_replace(array_keys($status_translate), array_values($status_translate), $wordpress->status);
							$_POST['pinned']  = false;
							$_POST['created_at'] = ($wordpress->post_date == "0000-00-00 00:00:00") ? datetime() : $wordpress->post_date ;
							$_POST['feather'] = "text";

							$data = array("title" => trim($item->title), "body" => trim($content->encoded));

							$post = Post::add($data, $clean, Post::check_url($clean));

							$trigger->call("import_wordpress_post", array($item, $post));
						} elseif ($wordpress->post_type == "page") {
							$page = Page::add(trim($item->title), trim($content->encoded), 0, true, 0, $clean, Page::check_url($clean));
							$trigger->call("import_wordpress_page", array($item, $post));
						}
					}
					$step = "2";
				} else
					$errors[] = __("File does not seem to be a valid WordPress export file.");

				break;
{'fraction_non_alphanumeric': 0.19447700924840433, 'fraction_numerical': 0.008336589813729322, 'mean_word_length': 2.6948989412897015, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, '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': '4517785', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2320, 'n_words': 418}