Skip to content

Commit b18dbb2

Browse files
authored
[Blueprints] Import WXRs via the DataLiberation importer (#127)
1 parent d395f13 commit b18dbb2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1491
-588
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ new-wp
1919
test.txt
2020
secrets.php
2121
testing-grounds.php
22+
test-script.php
2223
plugins/git-repo/git-test-server-data
2324
*.secret.json
2425
plugins/static-files-editor/build
@@ -30,4 +31,4 @@ components/DataLiberation/Tests/test-output-html/
3031
components/DataLiberation/Tests/test-output-md
3132
blueprint-dev.json
3233
examples/create-wp-site/data-liberation.zip
33-
untracked
34+
untracked

bin/build-libraries-phar.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cd $PROJECT_DIR
2020
mkdir -p $BUILD_DIR
2121
rm $DIST_DIR/wordpress-libraries.* > /dev/null 2>&1 || true
2222
export BOX_BASE_PATH=$(type -a box | grep -v 'alias' | awk '{print $3}')
23-
php $BUILD_DIR/box.php compile -d $PROJECT_DIR -c $PROJECT_DIR/phar-box.json
23+
php $BUILD_DIR/box.php compile -d $PROJECT_DIR -c $PROJECT_DIR/phar-libraries.json
2424
php -d 'phar.readonly=0' $BUILD_DIR/truncate-composer-checks.php $DIST_DIR/wordpress-libraries.phar
2525
cd $DIST_DIR
2626
php $BUILD_DIR/smoke-test.php

bin/build-phar/smoke-test.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require_once __DIR__ . '/../../dist/wordpress-libraries.phar';
3+
require_once __DIR__ . '/../../dist/php-toolkit.phar';
44

55
/**
66
* None of this will actually try to parse a file or import
@@ -9,7 +9,9 @@
99
*/
1010
$c = WordPress\DataLiberation\Importer\StreamImporter::create_for_wxr_file(__DIR__ . '/nosuchfile.xml', [
1111
'uploads_path' => __DIR__ . '/uploads',
12-
'new_site_url' => 'https://smoke-test.org'
12+
'new_site_url' => 'https://smoke-test.org',
13+
'new_site_content_root_url' => 'https://smoke-test.org',
14+
'new_media_root_url' => 'https://smoke-test.org',
1315
]);
1416

1517
WordPress\DataLiberation\URL\WPURL::parse('https://example.com');

components/Blueprints/DataReference/DataReference.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ class DataReference {
1111
* @var int
1212
*/
1313
public $id;
14+
15+
/**
16+
* @var array
17+
*/
18+
public $original_definition;
19+
1420
/**
1521
* @var int
1622
*/
1723
private static $instanceCounter = 0;
1824

19-
public function __construct() {
25+
public function __construct($original_definition = null) {
2026
$this->id = self::$instanceCounter ++;
27+
$this->original_definition = $original_definition;
2128
}
2229

2330
static public function create( $reference, array $additional_reference_classes = [] ) {
@@ -29,11 +36,7 @@ static public function create( $reference, array $additional_reference_classes =
2936
], $additional_reference_classes );
3037
foreach ( $classes as $class ) {
3138
if ( $class::is_valid( $reference ) ) {
32-
if ( method_exists( $class, 'from_blueprint_data' ) ) {
33-
return $class::from_blueprint_data( $reference );
34-
} else {
35-
return new $class( $reference );
36-
}
39+
return new $class( $reference );
3740
}
3841
}
3942
throw new InvalidArgumentException(

components/Blueprints/DataReference/DataReferenceResolver.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ public function startEagerResolution( array $dataReferences, Tracker $dataResolu
7474
*/
7575
public function resolve( DataReference $reference ) {
7676
// TODO: Comment this. Make semantics clearer.
77-
if ( isset( $this->resolvedDataReferences[ $reference->id ] ) ) {
78-
return $this->resolvedDataReferences[ $reference->id ];
77+
if ( ! isset( $this->resolvedDataReferences[ $reference->id ] ) ) {
78+
$this->resolvedDataReferences[ $reference->id ] = $this->resolve_uncached( $reference );
7979
}
80+
return $this->resolvedDataReferences[ $reference->id ];
81+
}
8082

83+
// @TODO: Clean up the semantics of this class. Resolve() and separate resolve_uncached() seem confusing. There's
84+
// a bunch of implicit behaviors related to caching. Ideally we would either have a self-contained resolution
85+
// method, or co-locate the resolution logic with the data reference classes and only use this class for
86+
// caching.
87+
public function resolve_uncached( DataReference $reference ) {
8188
$progress_tracker = $this->subTrackers[ $reference->id ] ?? new Tracker();
8289

8390
if ( $reference instanceof WordPressOrgPlugin ) {

components/Blueprints/DataReference/ExecutionContextPath.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ExecutionContextPath extends DataReference {
2020
*/
2121
public function __construct( string $path ) {
2222
$this->path = $path;
23-
parent::__construct();
23+
parent::__construct($path);
2424
}
2525

2626
/**

components/Blueprints/DataReference/GitPath.php

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@ class GitPath extends DataReference {
2525

2626
/**
2727
* Constructor.
28-
*
29-
* @param string $git_repository The git repository URL.
30-
* @param string|null $ref The git reference.
31-
* @param string|null $path The path within the repository.
3228
*/
3329
public function __construct(
34-
string $git_repository,
35-
?string $ref = null,
36-
?string $path = null
30+
$definition
3731
) {
38-
$this->git_repository = $git_repository;
39-
$this->ref = $ref;
40-
$this->path = $path;
41-
parent::__construct();
32+
if ( ! isset( $definition['gitRepository'] ) ) {
33+
throw new InvalidArgumentException( 'Invalid git path data' );
34+
}
35+
$this->git_repository = $definition['gitRepository'];
36+
$this->ref = $definition['ref'] ?? null;
37+
$this->path = $definition['path'] ?? null;
38+
parent::__construct($definition);
4239
}
4340

4441
/**
@@ -72,25 +69,6 @@ public function get_filename(): string {
7269
return basename( $this->path );
7370
}
7471

75-
/**
76-
* Create an instance from an array.
77-
*
78-
* @param array $data The array data.
79-
*
80-
* @return self The created instance.
81-
*/
82-
public static function from_blueprint_data( array $data ): self {
83-
if ( ! isset( $data['gitRepository'] ) ) {
84-
throw new InvalidArgumentException( 'Invalid git path data' );
85-
}
86-
87-
return new self(
88-
$data['gitRepository'],
89-
$data['ref'] ?? null,
90-
$data['path'] ?? null
91-
);
92-
}
93-
9472
/**
9573
* Check if an array represents a valid git path.
9674
*

components/Blueprints/DataReference/InlineDirectory.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,31 @@ class InlineDirectory extends DataReference {
2525
/**
2626
* Constructor.
2727
*
28-
* @param string $name The directory name.
29-
* @param array $children The directory children.
28+
* @param array $data The blueprint data array.
3029
*/
31-
public function __construct( string $name, array $children ) {
32-
$this->name = $name;
30+
public function __construct( array $data ) {
31+
if ( ! isset( $data['directoryName'] ) || ! isset( $data['files'] ) || ! is_array( $data['files'] ) ) {
32+
throw new InvalidArgumentException( 'Invalid inline directory data' );
33+
}
34+
35+
$this->name = $data['directoryName'];
36+
37+
$children = [];
38+
foreach ( $data['files'] as $fileName => $child ) {
39+
if ( is_string( $child ) ) {
40+
$children[$fileName] = new InlineFile( [
41+
'filename' => $fileName,
42+
'content' => $child
43+
] );
44+
} elseif ( self::is_valid( $child ) ) {
45+
$children[$fileName] = new self( $child );
46+
} else {
47+
throw new InvalidArgumentException( 'Invalid inline directory child' );
48+
}
49+
}
50+
3351
$this->children = $children;
34-
parent::__construct();
52+
parent::__construct( $data );
3553
}
3654

3755
/**
@@ -81,31 +99,6 @@ public function as_directory(): Directory {
8199
return new Directory( $this->as_filesystem(), $this->get_name() );
82100
}
83101

84-
/**
85-
* Create an instance from an array.
86-
*
87-
* @param array $data The array data.
88-
*
89-
* @return self The created instance.
90-
*/
91-
public static function from_blueprint_data( array $data ): self {
92-
if ( ! isset( $data['directoryName'] ) || ! isset( $data['files'] ) || ! is_array( $data['files'] ) ) {
93-
throw new InvalidArgumentException( 'Invalid inline directory data' );
94-
}
95-
96-
$children = [];
97-
foreach ( $data['files'] as $fileName => $child ) {
98-
if ( is_string( $child ) ) {
99-
$children[$fileName] = new InlineFile( $fileName, $child );
100-
} elseif ( self::is_valid( $child ) ) {
101-
$children[$fileName] = self::from_blueprint_data( $child );
102-
} else {
103-
throw new InvalidArgumentException( 'Invalid inline directory child' );
104-
}
105-
}
106-
107-
return new self( $data['directoryName'], $children );
108-
}
109102

110103
/**
111104
* Check if an array represents a valid inline directory.

components/Blueprints/DataReference/InlineFile.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ class InlineFile extends DataReference {
2424
* @param string $filename The filename.
2525
* @param string $content The content.
2626
*/
27-
public function __construct( string $filename, string $content ) {
28-
$this->filename = $filename;
29-
$this->content = $content;
30-
parent::__construct();
27+
public function __construct( array $definition ) {
28+
if ( ! isset( $definition['filename'] ) || ! isset( $definition['content'] ) ) {
29+
throw new InvalidArgumentException( 'Invalid inline file definition' );
30+
}
31+
32+
$this->filename = $definition['filename'];
33+
$this->content = $definition['content'];
34+
parent::__construct($definition);
3135
}
3236

3337
/**
@@ -48,21 +52,6 @@ public function get_content(): string {
4852
return $this->content;
4953
}
5054

51-
/**
52-
* Create an instance from an array.
53-
*
54-
* @param array $data The array data.
55-
*
56-
* @return self The created instance.
57-
*/
58-
public static function from_blueprint_data( array $data ): self {
59-
if ( ! isset( $data['filename'] ) || ! isset( $data['content'] ) ) {
60-
throw new InvalidArgumentException( 'Invalid inline file data' );
61-
}
62-
63-
return new self( $data['filename'], $data['content'] );
64-
}
65-
6655
/**
6756
* Check if an array represents a valid inline file.
6857
*

components/Blueprints/DataReference/URLReference.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class URLReference extends DataReference {
2020
*/
2121
public function __construct( string $url ) {
2222
$this->url = $url;
23-
parent::__construct();
23+
parent::__construct($url);
2424
}
2525

2626
/**

0 commit comments

Comments
 (0)