diff --git a/CRM/Utils/Migrate/Export.php b/CRM/Utils/Migrate/Export.php
index 1e13ba6fd6c78563319b70e88a2877aabb2a1548..e1f5105f56bafbdb106beed6539f6af71e589d7a 100644
--- a/CRM/Utils/Migrate/Export.php
+++ b/CRM/Utils/Migrate/Export.php
@@ -34,267 +34,279 @@
  */
 class CRM_Utils_Migrate_Export {
 
-  protected $_xml; function __construct() {
+  const XML_VALUE_SEPARATOR = ":;:;:;";
+
+  /**
+   * @var array description of export field mapping
+   *
+   * @code
+   * 'exampleEntityMappingName' => array(
+   *   'data' => array(),                     // placeholder; this will get filled-in during execution
+   *   'name' => 'CustomGroup',               // per-item XML tag name
+   *   'scope' => 'CustomGroups',             // container XML tag name
+   *   'required' => FALSE,                   // whether we *must* find records of this type
+   *   'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
+   *   'idNameMap' => array(),                // placeholder; this will get filled-in during execution
+   * ),
+   * @endcode
+   */
+  protected $_xml;
+
+  function __construct() {
     $this->_xml = array(
-      'customGroup' => array('data' => NULL,
+      'customGroup' => array(
+        'data' => array(),
         'name' => 'CustomGroup',
         'scope' => 'CustomGroups',
         'required' => FALSE,
-        'map' => array(),
+        'idNameFields' => array('id', 'name'),
+        'idNameMap' => array(),
       ),
       'customField' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'CustomField',
         'scope' => 'CustomFields',
         'required' => FALSE,
-        'map' => array(),
+        'idNameFields' => array('id', 'column_name'),
+        'idNameMap' => array(),
+        'mappedFields' => array(
+          array('optionGroup', 'option_group_id', 'option_group_name'),
+          array('customGroup', 'custom_group_id', 'custom_group_name'),
+        )
       ),
       'optionGroup' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'OptionGroup',
         'scope' => 'OptionGroups',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
+        'idNameFields' => array('id', 'name'),
       ),
       'relationshipType' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'RelationshipType',
         'scope' => 'RelationshipTypes',
         'required' => FALSE,
-        'map' => array(),
+        'idNameFields' => array('id', 'name_a_b'),
+        'idNameMap' => array(),
       ),
       'locationType' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'LocationType',
         'scope' => 'LocationTypes',
         'required' => FALSE,
-        'map' => array(),
+        'idNameFields' => array('id', 'name'),
+        'idNameMap' => array(),
       ),
       'optionValue' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'OptionValue',
         'scope' => 'OptionValues',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
+        'idNameFields' => array('value', 'name', 'prefix'),
+        'mappedFields' => array(
+          array('optionGroup', 'option_group_id', 'option_group_name'),
+        ),
       ),
       'profileGroup' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'ProfileGroup',
         'scope' => 'ProfileGroups',
         'required' => FALSE,
-        'map' => array(),
+        'idNameFields' => array('id', 'title'),
+        'idNameMap' => array(),
       ),
       'profileField' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'ProfileField',
         'scope' => 'ProfileFields',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
+        'mappedFields' => array(
+          array('profileGroup', 'uf_group_id', 'profile_group_name')
+        ),
       ),
       'profileJoin' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'ProfileJoin',
         'scope' => 'ProfileJoins',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
+        'mappedFields' => array(
+          array('profileGroup', 'uf_group_id', 'profile_group_name')
+        ),
       ),
       'mappingGroup' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'MappingGroup',
         'scope' => 'MappingGroups',
         'required' => FALSE,
-        'map' => array(),
+        'idNameFields' => array('id', 'name'),
+        'idNameMap' => array(),
+        'mappedFields' => array(
+          array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
+        )
       ),
       'mappingField' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'MappingField',
         'scope' => 'MappingFields',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
+        'mappedFields' => array(
+          array('mappingGroup', 'mapping_id', 'mapping_group_name'),
+          array('locationType', 'location_type_id', 'location_type_name'),
+          array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
+        ),
       ),
     );
   }
 
-  function run() {
+  /**
+   * Scan local customizations and build an in-memory representation
+   *
+   * @return void
+   */
+  function build() {
     // fetch the option group / values for
     // activity type and event_type
 
     $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
 
     $sql = "
-SELECT distinct(g.id), g.*
-FROM   civicrm_option_group g
-WHERE  g.name IN $optionGroups
-";
-    $this->fetch('optionGroup',
-      'CRM_Core_DAO_OptionGroup',
-      $sql,
-      array('id', 'name')
-    );
+      SELECT distinct(g.id), g.*
+      FROM   civicrm_option_group g
+      WHERE  g.name IN $optionGroups
+    ";
+    $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
 
     $sql = "
-SELECT distinct(g.id), g.*
-FROM   civicrm_option_group g,
-       civicrm_custom_field f,
-       civicrm_custom_group cg
-WHERE  f.option_group_id = g.id
-AND    f.custom_group_id = cg.id
-AND    cg.is_active = 1
-";
-    $this->fetch('optionGroup',
-      'CRM_Core_DAO_OptionGroup',
-      $sql,
-      array('id', 'name')
-    );
+      SELECT distinct(g.id), g.*
+      FROM   civicrm_option_group g,
+             civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  f.option_group_id = g.id
+      AND    f.custom_group_id = cg.id
+      AND    cg.is_active = 1
+    ";
+    $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
 
     $sql = "
-SELECT v.*, g.name as prefix
-FROM   civicrm_option_value v,
-       civicrm_option_group g
-WHERE  v.option_group_id = g.id
-AND    g.name IN $optionGroups
-";
-
-    $this->fetch('optionValue',
-      'CRM_Core_DAO_OptionValue',
-      $sql,
-      array('value', 'name', 'prefix'),
-      array(array('optionGroup', 'option_group_id', 'option_group_name'))
-    );
+      SELECT v.*, g.name as prefix
+      FROM   civicrm_option_value v,
+             civicrm_option_group g
+      WHERE  v.option_group_id = g.id
+      AND    g.name IN $optionGroups
+    ";
 
-    $sql = "
-SELECT distinct(v.id), v.*, g.name as prefix
-FROM   civicrm_option_value v,
-       civicrm_option_group g,
-       civicrm_custom_field f,
-       civicrm_custom_group cg
-WHERE  v.option_group_id = g.id
-AND    f.option_group_id = g.id
-AND    f.custom_group_id = cg.id
-AND    cg.is_active = 1
-";
-
-    $this->fetch('optionValue',
-      'CRM_Core_DAO_OptionValue',
-      $sql,
-      array('id', 'name', 'prefix'),
-      array(array('optionGroup', 'option_group_id', 'option_group_name'))
-    );
+    $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
 
     $sql = "
-SELECT rt.*
-FROM   civicrm_relationship_type rt
-WHERE  rt.is_active = 1
-";
-    $this->fetch('relationshipType',
-      'CRM_Contact_DAO_RelationshipType',
-      $sql,
-      array('id', 'name_a_b')
-    );
+      SELECT distinct(v.id), v.*, g.name as prefix
+      FROM   civicrm_option_value v,
+             civicrm_option_group g,
+             civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  v.option_group_id = g.id
+      AND    f.option_group_id = g.id
+      AND    f.custom_group_id = cg.id
+      AND    cg.is_active = 1
+    ";
 
+    $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
 
     $sql = "
-SELECT lt.*
-FROM   civicrm_location_type lt
-WHERE  lt.is_active = 1
-";
-    $this->fetch('locationType',
-      'CRM_Core_DAO_LocationType',
-      $sql,
-      array('id', 'name')
-    );
+      SELECT rt.*
+      FROM   civicrm_relationship_type rt
+      WHERE  rt.is_active = 1
+    ";
+    $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
 
+    $sql = "
+      SELECT lt.*
+      FROM   civicrm_location_type lt
+      WHERE  lt.is_active = 1
+    ";
+    $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
 
     $sql = "
-SELECT cg.*
-FROM   civicrm_custom_group cg
-WHERE  cg.is_active = 1
-";
-    $this->fetch('customGroup',
-      'CRM_Core_DAO_CustomGroup',
-      $sql,
-      array('id', 'name')
-    );
+      SELECT cg.*
+      FROM   civicrm_custom_group cg
+      WHERE  cg.is_active = 1
+    ";
+    $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
 
     $sql = "
-SELECT f.*
-FROM   civicrm_custom_field f,
-       civicrm_custom_group cg
-WHERE  f.custom_group_id = cg.id
-AND    cg.is_active = 1
-";
-    $this->fetch('customField',
-      'CRM_Core_DAO_CustomField',
-      $sql,
-      array('id', 'column_name'),
-      array(array('optionGroup', 'option_group_id', 'option_group_name'),
-        array('customGroup', 'custom_group_id', 'custom_group_name'),
-      )
-    );
+      SELECT f.*
+      FROM   civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  f.custom_group_id = cg.id
+      AND    cg.is_active = 1
+    ";
+    $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
 
-    $this->fetch('profileGroup',
-      'CRM_Core_DAO_UFGroup',
-      NULL,
-      array('id', 'title'),
-      NULL
-    );
+    $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
 
-    $this->fetch('profileField',
-      'CRM_Core_DAO_UFField',
-      NULL,
-      NULL,
-      array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
-    );
+    $this->fetch('profileField', 'CRM_Core_DAO_UFField');
 
     $sql = "
-SELECT *
-FROM   civicrm_uf_join
-WHERE  entity_table IS NULL
-AND    entity_id    IS NULL
-";
-    $this->fetch('profileJoin',
-      'CRM_Core_DAO_UFJoin',
-      $sql,
-      NULL,
-      array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
-    );
+      SELECT *
+      FROM   civicrm_uf_join
+      WHERE  entity_table IS NULL
+      AND    entity_id    IS NULL
+    ";
+    $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
 
-    $this->fetch('mappingGroup',
-      'CRM_Core_DAO_Mapping',
-      NULL,
-      array('id', 'name'),
-      array(array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'))
-    );
+    $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
 
-    $this->fetch('mappingField',
-      'CRM_Core_DAO_MappingField',
-      NULL,
-      NULL,
-      array(array('mappingGroup', 'mapping_id', 'mapping_group_name'),
-        array('locationType', 'location_type_id', 'location_type_name'),
-        array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
-      )
-    );
+    $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
+  }
 
+  /**
+   * Render the in-memory representation as XML
+   *
+   * @return string XML
+   */
+  function toXML() {
     $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
     $buffer .= "\n\n<CustomData>\n";
     foreach (array_keys($this->_xml) as $key) {
       if (!empty($this->_xml[$key]['data'])) {
-        $buffer .= "  <{$this->_xml[$key]['scope']}>\n{$this->_xml[$key]['data']}  </{$this->_xml[$key]['scope']}>\n";
+        $buffer .= "  <{$this->_xml[$key]['scope']}>\n";
+        foreach ($this->_xml[$key]['data'] as $item) {
+          $buffer .= $this->renderKeyValueXML($this->_xml[$key]['name'], $item);
+        }
+        $buffer .= "  </{$this->_xml[$key]['scope']}>\n";
       }
       elseif ($this->_xml[$key]['required']) {
         CRM_Core_Error::fatal("No records in DB for $key");
       }
     }
     $buffer .= "</CustomData>\n";
+    return $buffer;
+  }
 
-    CRM_Utils_System::download('CustomGroupData.xml', 'text/plain', $buffer);
+  /**
+   * Generate an array-tree representation of the exported elements.
+   *
+   * @return array
+   */
+  function toArray() {
+    $result = array();
+    foreach (array_keys($this->_xml) as $key) {
+      if (!empty($this->_xml[$key]['data'])) {
+        $result[ $this->_xml[$key]['name'] ] = $this->_xml[$key]['data'];
+      }
+    }
+    return $result;
   }
 
-  function fetch($groupName, $daoName, $sql = NULL, $map = NULL, $add = NULL) {
-    require_once (str_replace('_', DIRECTORY_SEPARATOR, $daoName) . '.php');
+  function fetch($groupName, $daoName, $sql = NULL) {
+    $idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
+    $mappedFields = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL;
 
-    eval("\$dao = new $daoName( );");
+    $dao = new $daoName();
     if ($sql) {
       $dao->query($sql);
     }
@@ -303,49 +315,60 @@ AND    entity_id    IS NULL
     }
 
     while ($dao->fetch()) {
-      $additional = NULL;
-      if ($add) {
-        foreach ($add as $filter) {
-          if (isset($dao->{$filter[1]})) {
-            if (isset($filter[3])) {
-              $label = $this->_xml[$filter[0]]['map']["{$filter[3]}." . $dao->{$filter[1]}];
-            }
-            else {
-              $label = $this->_xml[$filter[0]]['map'][$dao->{$filter[1]}];
-            }
-            $additional .= "\n      <{$filter[2]}>{$label}</{$filter[2]}>";
-          }
-        }
-      }
-      $this->_xml[$groupName]['data'] .= $this->exportDAO($dao,
-        $this->_xml[$groupName]['name'],
-        $additional
-      );
-      if ($map) {
-        if (isset($map[2])) {
-          $this->_xml[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
+      $this->_xml[$groupName]['data'][] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields);
+      if ($idNameFields) {
+        // index the id/name fields so that we can translate from FK ids to FK names
+        if (isset($idNameFields[2])) {
+          $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
         }
         else {
-          $this->_xml[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
+          $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
         }
       }
     }
   }
 
-  function exportDAO($object, $objectName, $additional = NULL) {
-    $dbFields = &$object->fields();
+  /**
+   * Compute any fields of the entity defined by the $mappedFields specification
+   *
+   * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
+   * @param CRM_Core_DAO $dao the entity for which we want to prepare mapped fields
+   * @return array new fields
+   */
+  public function computeMappedFields($mappedFields, $dao) {
+    $keyValues = array();
+    if ($mappedFields) {
+      foreach ($mappedFields as $mappedField) {
+        if (isset($dao->{$mappedField[1]})) {
+          if (isset($mappedField[3])) {
+            $label = $this->_xml[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
+          }
+          else {
+            $label = $this->_xml[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
+          }
+          $keyValues[$mappedField[2]] = $label;
+        }
+      }
+    }
+    return $keyValues;
+  }
 
-    $xml = "    <$objectName>";
+  /**
+   * @param CRM_Core_DAO $object
+   * @param string $objectName business-entity/xml-tag name
+   * @return array
+   */
+  function exportDAO($objectName, $object, $mappedFields) {
+    $dbFields = & $object->fields();
+
+    // Filter the list of keys and values so that we only export interesting stuff
+    $keyValues = array();
     foreach ($dbFields as $name => $dontCare) {
       // ignore all ids
-      if ($name == 'id' ||
-        substr($name, -3, 3) == '_id'
-      ) {
+      if ($name == 'id' || substr($name, -3, 3) == '_id') {
         continue;
       }
-      if (isset($object->$name) &&
-        $object->$name !== NULL
-      ) {
+      if (isset($object->$name) && $object->$name !== NULL) {
         // hack for extends_entity_column_value
         if ($name == 'extends_entity_column_value') {
           if ($object->extends == 'Event' ||
@@ -361,46 +384,65 @@ AND    entity_id    IS NULL
             elseif ($object->extends == 'Relationship') {
               $key = 'relationship_type';
             }
-            $xml .= "\n      <extends_entity_column_value_option_group>$key</extends_entity_column_value_option_group>";
-            $types = explode(CRM_Core_DAO::VALUE_SEPARATOR,
-              substr($object->$name, 1, -1)
-            );
-            $value = array();
+            $keyValues['extends_entity_column_value_option_group'] = $key;
+            $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1));
+            $values = array();
             foreach ($types as $type) {
-              $values[] = $this->_xml['optionValue']['map']["$key.{$type}"];
+              $values[] = $this->_xml['optionValue']['idNameMap']["$key.{$type}"];
             }
             $value = implode(',', $values);
-            $xml .= "\n      <extends_entity_column_value_option_value>$value</extends_entity_column_value_option_value>";
+            $keyValues['extends_entity_column_value_option_value'] = $value;
           }
           else {
             echo "This extension: {$object->extends} is not yet handled";
             exit();
           }
         }
+
+        $value = $object->$name;
         if ($name == 'field_name') {
-          $value = $object->$name;
           // hack for profile field_name
           if (substr($value, 0, 7) == 'custom_') {
             $cfID = substr($value, 7);
             list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($cfID);
             $value = "custom.{$tableName}.{$columnName}";
           }
-          $xml .= "\n      <$name>$value</$name>";
-        }
-        else {
-          $value = str_replace(CRM_Core_DAO::VALUE_SEPARATOR,
-            ":;:;:;",
-            $object->$name
-          );
-          $xml .= "\n      <$name>$value</$name>";
         }
+        $keyValues[$name] = $value;
       }
     }
-    if ($additional) {
-      $xml .= $additional;
+
+    $keyValues += $this->computeMappedFields($mappedFields, $object);
+
+    return $keyValues;
+  }
+
+  /**
+   * @param string $tagName
+   * @param array $keyValues
+   * @param string $additional XML
+   * @return string XML
+   */
+  public function renderKeyValueXML($tagName, $keyValues) {
+    $xml = "    <$tagName>";
+    foreach ($keyValues as $k => $v) {
+      $xml .= "\n      " . $this->renderTextTag($k, str_replace(CRM_Core_DAO::VALUE_SEPARATOR, self::XML_VALUE_SEPARATOR, $v));
     }
-    $xml .= "\n    </$objectName>\n";
+    $xml .= "\n    </$tagName>\n";
     return $xml;
   }
+
+  /**
+   * @param string $name tag name
+   * @param string $value text
+   * @param string $prefix
+   * @return string XML
+   */
+  function renderTextTag($name, $value, $prefix = '') {
+    if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
+      throw new Exception("Malformed tag name: $name");
+    }
+    return $prefix . "<$name>" . htmlentities($value) . "</$name>";
+  }
 }
 
diff --git a/CRM/Utils/Migrate/ExportJSON.php b/CRM/Utils/Migrate/ExportJSON.php
index f7e5ca30ff11634f88d750ef7cf11dbb5c1856cc..d0116e591edaac8a9fd92aae1659403fcf1a3366 100644
--- a/CRM/Utils/Migrate/ExportJSON.php
+++ b/CRM/Utils/Migrate/ExportJSON.php
@@ -47,7 +47,9 @@ class CRM_Utils_Migrate_ExportJSON {
 
   protected $_renameTags = 1;
 
-  protected $_sitePrefix = 'Site 1'; function __construct(&$params) {
+  protected $_sitePrefix = 'Site 1';
+
+  function __construct(&$params) {
     foreach ($params as $name => $value) {
       $varName = '_' . $name;
       $this->$varName = $value;
@@ -60,10 +62,10 @@ class CRM_Utils_Migrate_ExportJSON {
   function &splitContactIDs(&$contactIDs) {
     // contactIDs could be a real large array, so we split it up into
     // smaller chunks and then general xml for each chunk
-    $chunks           = array();
-    $current          = 0;
+    $chunks = array();
+    $current = 0;
     $chunks[$current] = array();
-    $count            = 0;
+    $count = 0;
 
     foreach ($contactIDs as $k => $v) {
       $chunks[$current][$k] = $v;
@@ -133,7 +135,7 @@ class CRM_Utils_Migrate_ExportJSON {
 
   function auxTable($tables) {
     foreach ($tables as $tableName => $daoName) {
-      $fields = &$this->dbFields($daoName, TRUE);
+      $fields = & $this->dbFields($daoName, TRUE);
 
       $sql = "SELECT * from $tableName";
       $this->sql($sql, $tableName, $fields);
@@ -153,7 +155,7 @@ SELECT *
 FROM   civicrm_option_group
 WHERE  name IN ( $nameString )
 ";
-    $fields = &$this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
     $this->sql($sql, 'civicrm_option_group', $fields);
 
     $sql = "
@@ -162,15 +164,15 @@ FROM       civicrm_option_value v
 INNER JOIN civicrm_option_group g ON v.option_group_id = g.id
 WHERE      g.name IN ( $nameString )
 ";
-    $fields = &$this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
     $this->sql($sql, 'civicrm_option_value', $fields);
   }
 
   function table(&$ids,
-    $tableName,
-    &$fields,
-    $whereField,
-    $additionalWhereCond = NULL
+                 $tableName,
+                 &$fields,
+                 $whereField,
+                 $additionalWhereCond = NULL
   ) {
     if (empty($ids)) {
       return;
@@ -192,7 +194,7 @@ SELECT *
   }
 
   function sql($sql, $tableName, &$fields) {
-    $dao = &CRM_Core_DAO::executeQuery($sql);
+    $dao = & CRM_Core_DAO::executeQuery($sql);
 
     while ($dao->fetch()) {
       $value = array();
@@ -210,42 +212,42 @@ SELECT *
   }
 
   function contact(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Contact_DAO_Contact', TRUE);
+    $fields = & $this->dbFields('CRM_Contact_DAO_Contact', TRUE);
     $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
   }
 
   function note(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Core_DAO_Note', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_Note', TRUE);
     $this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
   }
 
   function phone(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Core_DAO_Phone', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_Phone', TRUE);
     $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
   }
 
   function email(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
     $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
   }
 
   function im(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Core_DAO_IM', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_IM', TRUE);
     $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
   }
 
   function website(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Core_DAO_Website', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_Website', TRUE);
     $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
   }
 
   function address(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
     $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
   }
 
   function groupContact(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
+    $fields = & $this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
     $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
   }
 
@@ -271,7 +273,7 @@ WHERE  contact_id IN ( $ids )
       }
     }
 
-    $fields = &$this->dbFields('CRM_Contact_DAO_Group', TRUE);
+    $fields = & $this->dbFields('CRM_Contact_DAO_Group', TRUE);
     $this->table($groupIDs, 'civicrm_group', $fields, 'id');
 
     $this->savedSearch($groupIDs);
@@ -291,12 +293,12 @@ INNER JOIN civicrm_group g on g.saved_search_id = s.id
 WHERE      g.id IN ( $idString )
 ";
 
-    $fields = &$this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
+    $fields = & $this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
     $this->sql($sql, 'civicrm_saved_search', $fields);
   }
 
   function entityTag(&$contactIDs) {
-    $fields = &$this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
     $this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
   }
 
@@ -321,7 +323,7 @@ AND    entity_table = 'civicrm_contact'
       }
     }
 
-    $fields = &$this->dbFields('CRM_Core_DAO_Tag', TRUE);
+    $fields = & $this->dbFields('CRM_Core_DAO_Tag', TRUE);
     $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
   }
 
@@ -343,7 +345,7 @@ AND    entity_table = 'civicrm_contact'
 ";
 
     $fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
-    $dao = &CRM_Core_DAO::executeQuery($sql);
+    $dao = & CRM_Core_DAO::executeQuery($sql);
     while ($dao->fetch()) {
       if (isset($_relationshipsHandled[$dao->id])) {
         continue;
@@ -362,7 +364,7 @@ AND    entity_table = 'civicrm_contact'
       $this->appendValue($dao->id, 'civicrm_relationship', $relationship);
 
       $this->addAdditionalContacts(array(
-        $dao->contact_id_a,
+          $dao->contact_id_a,
           $dao->contact_id_b,
         ),
         $additionalContacts
@@ -391,10 +393,10 @@ AND    entity_table = 'civicrm_contact'
 )
 ";
 
-    $fields = &$this->dbFields('CRM_Activity_DAO_Activity', TRUE);
+    $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE);
 
     $activityIDs = array();
-    $dao = &CRM_Core_DAO::executeQuery($sql);
+    $dao = & CRM_Core_DAO::executeQuery($sql);
     while ($dao->fetch()) {
       if (isset($_activitiesHandled[$dao->id])) {
         continue;
@@ -426,8 +428,8 @@ AND    entity_table = 'civicrm_contact'
     $activityIDString = implode(",", $activityIDs);
 
     // now get all assignee contact ids and target contact ids for this activity
-    $sql              = "SELECT * FROM civicrm_activity_assignment WHERE activity_id IN ($activityIDString)";
-    $aaDAO            = &CRM_Core_DAO::executeQuery($sql);
+    $sql = "SELECT * FROM civicrm_activity_assignment WHERE activity_id IN ($activityIDString)";
+    $aaDAO = & CRM_Core_DAO::executeQuery($sql);
     $activityContacts = array();
     while ($aaDAO->fetch()) {
       $activityAssignee = array(
@@ -441,7 +443,7 @@ AND    entity_table = 'civicrm_contact'
     $aaDAO->free();
 
     $sql = "SELECT * FROM civicrm_activity_target WHERE activity_id IN ($activityIDString)";
-    $atDAO = &CRM_Core_DAO::executeQuery($sql);
+    $atDAO = & CRM_Core_DAO::executeQuery($sql);
     while ($atDAO->fetch()) {
       $activityTarget = array(
         'id' => $atDAO->id,
@@ -479,7 +481,7 @@ AND    entity_table = 'civicrm_contact'
       ) . '.php';
       include_once ($daoFile);
 
-      $daoFields = &$daoName::fields();
+      $daoFields = & $daoName::fields();
 
       foreach ($daoFields as $key => & $value) {
         $_fieldsRetrieved[$daoName][$value['name']] = array(
@@ -514,7 +516,7 @@ AND    entity_table = 'civicrm_contact'
   }
 
   function export(&$contactIDs) {
-    $chunks = &$this->splitContactIDs($contactIDs);
+    $chunks = & $this->splitContactIDs($contactIDs);
 
     $additionalContactIDs = array();
 
@@ -529,8 +531,8 @@ AND    entity_table = 'civicrm_contact'
   }
 
   function run($fileName,
-    $lastExportTime = NULL,
-    $discoverContacts = FALSE
+               $lastExportTime = NULL,
+               $discoverContacts = FALSE
   ) {
     $this->_discoverContacts = $discoverContacts;
 
@@ -555,7 +557,7 @@ WHERE  date >= $lastExportTime
     }
 
 
-    $dao = &CRM_Core_DAO::executeQuery($sql);
+    $dao = & CRM_Core_DAO::executeQuery($sql);
 
     $contactIDs = array();
     while ($dao->fetch()) {
diff --git a/CRM/Utils/Migrate/Import.php b/CRM/Utils/Migrate/Import.php
index 968fe3d8b42e5c222f737654c1ee6dc9a5c3ca96..d2c312d65aac10310b0acbcd676b747a7013128c 100644
--- a/CRM/Utils/Migrate/Import.php
+++ b/CRM/Utils/Migrate/Import.php
@@ -33,7 +33,8 @@
  *
  */
 class CRM_Utils_Migrate_Import {
-  function __construct() {}
+  function __construct() {
+  }
 
   function run($file) {
 
@@ -42,7 +43,8 @@ class CRM_Utils_Migrate_Import {
     $dom->xinclude();
     $xml = simplexml_import_dom($dom);
 
-    $idMap = array('custom_group' => array(),
+    $idMap = array(
+      'custom_group' => array(),
       'option_group' => array(),
     );
 
@@ -75,22 +77,22 @@ class CRM_Utils_Migrate_Import {
         $dao->$keyName = (string ) $xml->$keyName;
         if ($dao->find(TRUE)) {
           CRM_Core_Session::setStatus(ts("Found %1, %2, %3",
-              array(
-                1 => $keyName,
-                2 => $dao->$keyName,
-                3 => $dao->__table
-              )
-            ), '', 'info');
+            array(
+              1 => $keyName,
+              2 => $dao->$keyName,
+              3 => $dao->__table
+            )
+          ), '', 'info');
           return FALSE;
         }
       }
     }
 
-    $fields = &$dao->fields();
+    $fields = & $dao->fields();
     foreach ($fields as $name => $dontCare) {
       if (isset($xml->$name)) {
         $value = (string ) $xml->$name;
-        $value = str_replace(":;:;:;",
+        $value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR,
           CRM_Core_DAO::VALUE_SEPARATOR,
           $value
         );
@@ -147,7 +149,7 @@ WHERE      v.option_group_id = %1
 
     foreach ($xml->ContributionTypes as $contributionTypesXML) {
       foreach ($contributionTypesXML->ContributionType as $contributionTypeXML) {
-                $contributionType = new CRM_Financial_DAO_FinancialType( );
+        $contributionType = new CRM_Financial_DAO_FinancialType();
         $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
       }
     }
@@ -187,12 +189,13 @@ WHERE      g.name = %1
 AND        v.name IN (%2)
 ";
             $params = array(
-              1 => array((string ) $customGroupXML->extends_entity_column_value_option_group,
+              1 => array(
+                (string ) $customGroupXML->extends_entity_column_value_option_group,
                 'String',
               ),
               2 => array((string ) $optValues, 'String'),
             );
-            $dao = &CRM_Core_DAO::executeQuery($sql, $params);
+            $dao = & CRM_Core_DAO::executeQuery($sql, $params);
 
             $valueIDs = array();
             while ($dao->fetch()) {
@@ -218,9 +221,11 @@ WHERE      g.name = 'custom_data_type'
 AND        v.name = %1
 ";
             $params = array(
-              1 => array((string ) $customGroupXML->extends_entity_column_value_option_group,
+              1 => array(
+                (string ) $customGroupXML->extends_entity_column_value_option_group,
                 'String',
-              ));
+              )
+            );
             $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params);
             if ($valueID) {
               $customGroup->extends_entity_column_id = $valueID;
@@ -280,10 +285,10 @@ AND        v.name = %1
         $fields_indexed_by_group_id[$id][] = $customFieldXML;
       }
     }
-    while(list($group_id, $fields) = each($fields_indexed_by_group_id)) {
+    while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
       $total = count($fields);
       $count = 0;
-      while(list(,$customFieldXML) = each($fields)) {
+      while (list(, $customFieldXML) = each($fields)) {
         $count++;
         $customField = new CRM_Core_DAO_CustomField();
         $customField->custom_group_id = $group_id;
@@ -305,9 +310,9 @@ AND        v.name = %1
         // Only rebuild the table's trigger on the last field added to avoid un-necessary
         // and slow rebuilds when adding many fields at the same time.
         $triggerRebuild = FALSE;
-        if($count == $total) {
+        if ($count == $total) {
           $triggerRebuild = TRUE;
-        } 
+        }
         $indexExist = FALSE;
         CRM_Core_BAO_CustomField::createField($customField, 'add', $indexExist, $triggerRebuild);
       }
@@ -355,18 +360,19 @@ INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
 WHERE      g.table_name  = %1
 AND        f.column_name = %2
 ";
-          $params = array(1 => array($tableName, 'String'),
+          $params = array(
+            1 => array($tableName, 'String'),
             2 => array($columnName, 'String'),
           );
           $cfID = CRM_Core_DAO::singleValueQuery($sql, $params);
           if (!$cfID) {
             CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3",
-                array(
-                  1 => $profileField->field_name,
-                  2 => $tableName,
-                  3 => $columnName
-                )
-              ) . "<br />");
+              array(
+                1 => $profileField->field_name,
+                2 => $tableName,
+                3 => $columnName
+              )
+            ) . "<br />");
           }
           $profileField->field_name = "custom_{$cfID}";
         }
diff --git a/CRM/Utils/Migrate/ImportJSON.php b/CRM/Utils/Migrate/ImportJSON.php
index ca2d528d54169f0a9078e8f93569794289b8ad95..694efe808d726a9646996797bdead0b7e6e2d1fe 100644
--- a/CRM/Utils/Migrate/ImportJSON.php
+++ b/CRM/Utils/Migrate/ImportJSON.php
@@ -37,6 +37,7 @@ class CRM_Utils_Migrate_ImportJSON {
   protected $_lookupCache;
 
   protected $_saveMapping;
+
   function __construct() {
     $this->_lookupCache = array();
     $this->_saveMapping = array();
diff --git a/bin/migrate/export.php b/bin/migrate/export.php
index 4511499c5fdb62fd02ccec0ff01a48b9ba3f3c68..fc4076120e001db5d2641fff7a57af8348c8fa81 100644
--- a/bin/migrate/export.php
+++ b/bin/migrate/export.php
@@ -44,8 +44,8 @@ function run() {
 
   require_once 'CRM/Utils/Migrate/Export.php';
   $export = new CRM_Utils_Migrate_Export();
-
-  $export->run();
+  $export->build();
+  CRM_Utils_System::download('CustomGroupData.xml', 'text/plain', $export->toXML());
 }
 
 run();