<commit_before><?php
namespace Craft;

class ArtVandelay_FieldsService extends BaseApplicationComponent
{
	public function export(array $groups)
	{
		$groupDefs = array();

		foreach ($groups as $group)
		{
			$fieldDefs = array();

			foreach ($group->getFields() as $field)
			{
				$fieldDefs[$field->handle] = array(
					'name'         => $field->name,
					'context'      => $field->context,
					'instructions' => $field->instructions,
					'translatable' => $field->translatable,
					'type'         => $field->type,
					'settings'     => $field->settings
				);
			}

			$groupDefs[$group->name] = $fieldDefs;
		}

		return $groupDefs;
	}

	public function import($groupDefs)
	{
		$groups = craft()->fields->getAllGroups('name');
		$fields = craft()->fields->getAllFields('handle');

		if (!is_array($groupDefs))
		{
			return false;
		}

		foreach ($groupDefs as $groupName => $fieldDefs)
		{
			$group = array_key_exists($groupName, $groups)
				? $groups[$groupName]
				: new FieldGroupModel();

			$group->name = $groupName;

			if (!craft()->fields->saveGroup($group))
				return false;

			if (!is_array($fieldDefs))
			{
				return false;
			}

			foreach ($fieldDefs as $fieldHandle => $fieldDef)
			{
				$field = array_key_exists($fieldHandle, $fields)
					? $fields[$fieldHandle]
					: new FieldModel();

				$field->handle  = $fieldHandle;
				$field->groupId = $group->id;

				$field->name         = $fieldDef->name;
				$field->context      = $fieldDef->context;
				$field->instructions = $fieldDef->instructions;
				$field->translatable = $fieldDef->translatable;
				$field->type         = $fieldDef->type;
				$field->settings     = (array) $fieldDef->settings;

				if (!craft()->fields->saveField($field))
					return false;
			}
		}
<commit_msg>Import and export for fields works.<commit_after><?php
namespace Craft;

class ArtVandelay_FieldsService extends BaseApplicationComponent
{
	public function export(array $groups)
	{
		$groupDefs = array();

		foreach ($groups as $group)
		{
			$fieldDefs = array();

			foreach ($group->getFields() as $field)
			{
				$fieldDefs[$field->handle] = array(
					'name'         => $field->name,
					'context'      => $field->context,
					'instructions' => $field->instructions,
					'translatable' => $field->translatable,
					'type'         => $field->type,
					'settings'     => $field->settings
				);
			}

			$groupDefs[$group->name] = $fieldDefs;
		}

		return $groupDefs;
	}

	public function import($groupDefs)
	{
		$groups     = craft()->fields->getAllGroups('name');
		$fields     = craft()->fields->getAllFields('handle');
		$fieldTypes = craft()->fields->getAllFieldTypes();

		if (!is_array($groupDefs) && !is_object($groupDefs))
		{
			return false;
		}

		foreach ($groupDefs as $groupName => $fieldDefs)
		{
			$group = array_key_exists($groupName, $groups)
				? $groups[$groupName]
				: new FieldGroupModel();

			$group->name = $groupName;

			if (!craft()->fields->saveGroup($group))
				return false;

			if (!is_array($fieldDefs) && !is_object($fieldDefs))
			{
				return false;
			}

			foreach ($fieldDefs as $fieldHandle => $fieldDef)
			{
				if (in_array($fieldDef->type, $fieldTypes))
				{
					$field = array_key_exists($fieldHandle, $fields)
						? $fields[$fieldHandle]
						: new FieldModel();

					$field->handle  = $fieldHandle;
					$field->groupId = $group->id;

					$field->name         = $fieldDef->name;
					$field->context      = $fieldDef->context;
					$field->instructions = $fieldDef->instructions;
					$field->translatable = $fieldDef->translatable;
					$field->type         = $fieldDef->type;
					$field->settings     = (array) $fieldDef->settings;

					if (!craft()->fields->saveField($field))
					{
						return false;
					}
				}
			}
		}

{'fraction_non_alphanumeric': 0.16834038054968287, 'fraction_numerical': 0.0, 'mean_word_length': 3.0308839190628327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 88, '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': '820062', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1229, 'n_words': 222}