Skip to content

Instantly share code, notes, and snippets.

@josephdpurcell
Last active October 17, 2019 00:04
Show Gist options
  • Save josephdpurcell/728dd8744fabd4fcf80798664e270df5 to your computer and use it in GitHub Desktop.
Save josephdpurcell/728dd8744fabd4fcf80798664e270df5 to your computer and use it in GitHub Desktop.
A script to import sample data into Jackrabbit JCR
id: blog
label: Blog
source:
plugin: jcr
host: "http://localhost:8080/server"
query: 'SELECT * FROM [nt:unstructured] AS node WHERE ISDESCENDANTNODE(node, "/migrate_source_jcr_example/blog") AND [sling:resourceType] = "components/structure/page"'
type: "JCR-SQL2"
user: "admin"
pass: "admin"
workspace: "default"
fields:
-
name: title
subpath: ''
property: 'jcr:title'
-
name: body
subpath: body
property: text
process:
title:
-
plugin: skip_on_empty
method: row
source: title
-
plugin: get
source: title
body/value:
-
plugin: get
source: body
body/format:
-
plugin: default_value
default_value: rich_text
destination:
plugin: 'entity:node'
default_bundle: blog
<?xml version="1.0"?>
<blog xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:sling="http://sling.apache.org/jcr/sling/1.0" jcr:primaryType="nt:unstructured" jcr:createdBy="admin" jcr:created="2019-10-16T12:34:56.123+00:00">
<jcr:content jcr:uuid="f8db54ed-593a-420c-a291-d7cc650577eb">
<blog>
<node1>
<jcr:content jcr:title="Node with no body" sling:resourceType="components/structure/page" jcr:uuid="c2ce1e97-51bf-48b2-ab8f-daa963b73aa8"/>
</node1>
<node2>
<jcr:content jcr:title="Node with a body" sling:resourceType="components/structure/page" jcr:uuid="24146ada-9567-4455-a2b8-8b6582e78c36">
<body jcr:primaryType="nt:unstructured" text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/>
</jcr:content>
</node2>
</blog>
</jcr:content>
</blog>
<?php
/**
* This script is for helping setup and test Migrate Source JCR module.
*
* @see https://www.drupal.org/docs/8/api/migrate-api/migrate-source-plugins/migrating-data-from-a-jcr-source
*/
// IMPORTANT! This must point to a vendor directory that has the
// jackalope/jackalope-jackrabbit library installed. The easiest way to ensure
// this is to put this file in the root of your Drupal repository (i.e. sibling
// to "docroot" directory) and make sure you've run: composer require
// drupal/migrate_source_jcr.
include 'vendor/autoload.php';
$jackrabbit_url = 'http://localhost:8080/server';
$user = 'admin';
$pass = 'admin';
$workspace = 'default';
$factory = new \Jackalope\RepositoryFactoryJackrabbit();
$repository = $factory->getRepository(["jackalope.jackrabbit_uri" => $jackrabbit_url]);
$credentials = new \PHPCR\SimpleCredentials($user, $pass);
try {
$session = $repository->login($credentials, $workspace);
}
catch (\PHPCR\LoginException $e) {
die('Invalid credentials: ' . $e->getMessage());
}
catch (\PHPCR\NoSuchWorkspaceException $e) {
die("No workspace $workspace: " . $e->getMessage());
}
// Re-create the sample data node.
$session->nodeExists('/migrate_source_jcr_example') && $session->removeItem('/migrate_source_jcr_example');
$session->getRootNode()->addNode('migrate_source_jcr_example', 'nt:unstructured');
$session->save();
// Import data.
$session->importXML('/migrate_source_jcr_example', 'migrate_source_jcr_example.xml', \PHPCR\ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING);
$session->save();
// Print the nodes to make sure they were stored.
$workspace = $session->getWorkspace();
$queryManager = $workspace->getQueryManager();
$sql = 'SELECT * FROM [nt:unstructured] AS node WHERE ISDESCENDANTNODE(node, "/migrate_source_jcr_example/blog") AND [sling:resourceType] = "components/structure/page"';
$query = $queryManager->createQuery($sql, 'JCR-SQL2');
$queryResult = $query->execute();
$nodes = $queryResult->getNodes();
if (count($nodes) < 1) {
throw new \Exception('The data did not import!');
}
// Verify the data was loaded correctly.
/** @var Jackalope\Node $node */
foreach ($nodes as $path => $node) {
switch ($path) {
case '/migrate_source_jcr_example/blog/jcr:content/blog/node1/jcr:content':
if ($node->getPropertyValue('jcr:title') !== 'Node with no body') {
throw new \Exception('Unexpected title on node: ' . $path);
}
if ($node->hasNode('body')) {
throw new \Exception('Unexpected body on node: ' . $path);
}
break;
case '/migrate_source_jcr_example/blog/jcr:content/blog/node2/jcr:content':
if ($node->getPropertyValue('jcr:title') !== 'Node with a body') {
throw new \Exception('Unexpected title on node: ' . $path);
}
if (!$node->hasNode('body') || !$node->getnode('body')->hasProperty('text')) {
throw new \Exception('Unexpected missing body on node: ' . $path);
}
break;
default:
throw new \Exception('We imported data we didn\'t expect!');
}
}
// Print data to screen so the user sees it too.
foreach ($nodes as $path => $node) {
echo $path . "\n";
echo " Title: " . $node->getPropertyValue('jcr:title') . "\n";
if ($node->hasNode('body')) {
$body = $node->getNode('body');
echo " Body: " . $body->getPropertyValue('text') . "\n";
}
else {
echo " Body: NULL\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment