{"id":20028740,"url":"https://github.com/phase2/p2-importer","last_synced_at":"2025-05-05T03:32:17.712Z","repository":{"id":4811364,"uuid":"5964991","full_name":"phase2/p2-importer","owner":"phase2","description":"A Data Import Library","archived":false,"fork":false,"pushed_at":"2013-05-14T21:06:57.000Z","size":150,"stargazers_count":3,"open_issues_count":0,"forks_count":4,"subscribers_count":73,"default_branch":"master","last_synced_at":"2025-04-08T16:05:31.506Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"sasstools/sass-lint","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phase2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-09-26T11:48:09.000Z","updated_at":"2013-12-05T13:32:49.000Z","dependencies_parsed_at":"2022-08-20T16:10:24.534Z","dependency_job_id":null,"html_url":"https://github.com/phase2/p2-importer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2-importer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2-importer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2-importer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phase2%2Fp2-importer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phase2","download_url":"https://codeload.github.com/phase2/p2-importer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252435176,"owners_count":21747358,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T09:16:19.428Z","updated_at":"2025-05-05T03:32:17.451Z","avatar_url":"https://github.com/phase2.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Creates a lite weight importer for Drupal.  There is no front end.\n\nFetcher:  Get data from a source and return an Iterator\n\n* CSV:  A csv file\n  * file:  The file location.  can be any uri where you have a compatible schema\n* DB:  A database table\n  * db:  The db connection from the $databases array in settings.php\n  * table:  the name of the table\n  * count:  the number of rows\n* JSON:  A json file\n  * url:  The url of the json\n\nImporter:  Controls the import process\n\nField Types:  Process each field from the source to the destination\n\nParser:  Pares the Iterator from the Fetcher using the Field Types\n\nProcessor:  Takes the output from the Parser and Persists the information\n\nData Container:  The P2Importer\\DataContainer\n\n\n            ----------------               -------------\n           |                |             |             |\n           |     Fetcher    | \u003c---------- | Data Source |\n           |                |             |             |\n            ----------------               -------------\n                   |\n                   |\n                   v\n             ---------------               -------------\n            |               |             |             |\n            |    Parser     | \u003c---------\u003e | Field Types |\n            |               |             |             |\n             ---------------               -------------\n                   |\n                   |\n                   v\n             ---------------\n            |               |\n            |   Processor   |\n            |               |\n             ---------------\n                   |\n                   |\n                   v\n             ---------------\n            |               |\n            |    Drupal     |\n            |               |\n             ---------------\n\n\n## Code Registry\n\nPimple is used as a DI container.  See http://pimple.sensiolabs.org/\n\n * parser: The Parser Class\n * row-parser: The Parser for Each Row\n * processor: The Processor\n * row-processor: The Processor for Each Row\n * fetcher: The Fetch Class\n * ctype:  string value for the content type so save a node as if using the NodeRowProcessor\n * Language:  The language to use in the node\n * data_container: The DataContainer Object.  Should use or extend P2Importer\\DataContainer\n\n### Field Map\n\n\nThe field map is an extension of Pimple P2Importer\\FieldMap()\n\n* the addField method takes a closure that returns a FieldType object\n* the addUnique method takes a closure that returns an instance of P2Importer\\UniqueField\n* the \"as_is\" setting just copies the imported value to the local and avoids any field logic\n\n### Row Processor - This is what persists the data to drupal\n\n* P2Importer\\Processors\\NodeRowProcessor: Updates or creates a node.  Uses the unique fields in the field map\n* P2Importer\\Processors\\DirectFieldRowProcessor: Does direct table updates on fields only.\n  * This required State Machine Module to determine which revisions to update.\n  * Does not work with file field\n  * Only works with SQL field storage.\n  * Only works on existing entities and not new ones\n  \n## Example\n\n```\nfunction run_import($id) {\n  require_once libraries_get_path('p2_importer') . '/lib/Pimple.php';\n\n  $di = new Pimple();\n\n  $di['ctype'] = 'sometype';\n  $di['language'] = LANGUAGE_NONE;\n\n  // using \"share\" makes it resues the same instance.  \n  $di['field_map'] = $di-\u003eshare(function($c) {\n    return create_get_field_map();\n  });\n\n  $di['fetcher'] = function($c) use ($program_id) {\n    $url = get_url($id);\n    return new \\P2Importer\\Fetchers\\Json(array('url' =\u003e $url));\n  };\n\n  $di['parser'] = function($c) {\n    return new \\P2Importer\\Parser\\SingleParser();\n  };\n\n  $di['row_parser'] = function($c) {\n    return new \\P2Importer\\RowParser();\n  };\n\n  $di['processor'] = function($c) {\n    return new \\P2Importer\\Processors\\SingleProcessor();\n  };\n\n  $di['row_processor'] = function($c) {\n    return new \\P2Importer\\Processors\\DirectFieldRowProcessor();\n  };\n\n  $di['data_container'] = function($C) {\n    return new \\P2Importer\\DataContainer();\n  };\n\n  $importer = new \\P2Importer\\Importer($di);\n  $importer-\u003eprocess();\n}\n```\n\nGet the Field Map\n\n```\nfunction create_get_field_map() {\n  $field_map = new FieldMap();\n\n  $map = array(\n    'local_field_1' =\u003e 'source_field_1',\n    'local_field_2' =\u003e 'source_field_2',\n    'local_field_3' =\u003e 'source_field_3',\n    'local_field_4' =\u003e 'source_field_4',\n  );\n\n  // We use property because we just want to exact map the dest to the source\n  foreach ($map as $local =\u003e $source) {\n    $field_map-\u003eaddField(function() use ($local, $source) {\n        $settings = array('as_is' =\u003e TRUE);\n        return new Property(\n          $local,\n          $source,\n          $settings\n        );\n      });\n  }\n\n  foreach ($tax_fields as $local =\u003e $source) {\n    $field_map-\u003eaddField(function() use ($local, $source) {\n        return new CustomFieldType(\n          $local,\n          $source\n        );\n      });\n  }\n\n  // Add unique fields to load the entity by\n  $field_map-\u003eaddUniqueField(function() {\n      return new \\P2Importer\\UniqueField(\n        'local_field',\n        'remote_field',\n        'field',\n        'value'\n      );\n    });\n\n  return $field_map;\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphase2%2Fp2-importer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphase2%2Fp2-importer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphase2%2Fp2-importer/lists"}