-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReplicatorCommand.php
More file actions
244 lines (200 loc) · 4.93 KB
/
ReplicatorCommand.php
File metadata and controls
244 lines (200 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace WPSH_Replicator;
use WP_CLI_Command;
use WP_CLI\Utils;
/**
* Defines the Replicator WP CLI command.
*/
class ReplicatorCommand extends WP_CLI_Command {
/**
* @var JsonImporter Our importer tool.
*/
protected $importer;
/**
* @var \CliTool Instance of generic CLI tools.
*/
protected $cli;
/**
* Init the command.
*/
public function __construct() {
/**
* @var \wpdb $wpdb
*/
global $wpdb;
$this->importer = new JsonImporter( $wpdb );
$this->cli = new CliTool();
}
/**
* Convert WXR export XML files into JSON files.
*
* ## OPTIONS
*
* <path>
* : Path to the directory containing XML files.
*
*
* [--fresh]
* : Override any existing files.
*
* @subcommand parse-wxr
*/
public function parse_wxr( $args, $assoc_args ) {
list( $xml_dir ) = $args;
$fresh = (bool) Utils\get_flag_value( $assoc_args, 'fresh' );
$xml_dir = rtrim( $xml_dir, '/' );
$files = glob( $xml_dir . '/*.xml' );
if ( empty( $files ) ) {
return $this->cli->error( sprintf(
'No XML files found in %s.',
$xml_dir
) );
}
$json_dir = dirname( $xml_dir ) . '/json';
if ( ! file_exists( $json_dir ) ) {
mkdir( $json_dir );
}
$parser = new XmlToJson();
$users_filename = sprintf( '%s/users.json', $json_dir );
$terms_filename = sprintf( '%s/terms.json', $json_dir );
// Parse all terms out of one file.
if ( ! file_exists( $users_filename ) || $fresh ) {
$users = $parser->parse_users( $files[0] );
$this->to_json_file( $users_filename, $users );
$this->cli->success( sprintf(
'Parsed %d users to %s.',
count( $users ),
$users_filename
) );
} else {
$this->cli->warn( sprintf(
'Skip parsing users because %s exists.',
$users_filename
) );
}
// Parse all users out of one file.
if ( ! file_exists( $terms_filename ) || $fresh ) {
$terms = $parser->parse_terms( $files[0] );
$this->to_json_file( $terms_filename, $terms );
$this->cli->success( sprintf(
'Parsed terms to %s.',
$terms_filename
) );
} else {
$this->cli->warn( sprintf(
'Skip parsing terms because %s exists.',
$terms_filename
) );
}
foreach ( $files as $file ) {
$posts_filename = sprintf( '%s/posts-%s.json', $json_dir, basename( $file, '.xml' ) );
if ( ! file_exists( $posts_filename ) || $fresh ) {
$posts = $parser->parse( $file );
$this->to_json_file( $posts_filename, $posts );
$this->cli->success( sprintf(
'Parsed posts from %s to %s.',
basename( $file ),
$posts_filename
) );
} else {
$this->cli->warn( sprintf(
'Skip parsing posts because %s exists.',
$posts_filename
) );
}
}
}
/**
* Import options.
*
* ## OPTIONS
*
* <path>
* : Path to the options.json file.
*
* @subcommand import-options
*/
public function import_options( $args, $assoc_args ) {
list( $options_file ) = $args;
$this->importer->import_options( $this->from_json_file( $options_file ) );
// Options also contain the rewrite rules.
flush_rewrite_rules();
$this->cli->success( 'Options imported.' );
}
/**
* Import users.
*
* ## OPTIONS
*
* <path>
* : Path to the users.json file.
*
* @subcommand import-users
*/
public function import_users( $args, $assoc_args ) {
list( $users_file ) = $args;
$this->importer->import_users( $this->from_json_file( $users_file ) );
$this->cli->success( 'Users imported.' );
}
/**
* Import terms.
*
* ## OPTIONS
*
* <path>
* : Path to terms.json file.
*
* @subcommand import-terms
*/
public function import_terms( $args, $assoc_args ) {
list( $terms_file ) = $args;
$this->importer->import_terms( $this->from_json_file( $terms_file ) );
$this->cli->success( 'Taxonomies and terms imported.' );
}
/**
* Import posts.
*
* ## OPTIONS
*
* <path>
* : Path to the directory with the posts-*.json files.
*
* @subcommand import-posts
*/
public function import_posts( $args, $assoc_args ) {
list( $posts_dir ) = $args;
$files = glob( rtrim( $posts_dir, '/' ) . '/posts-*.json' );
if ( empty( $files ) ) {
return $this->cli->error( sprintf(
'Failed to find post json files at %s.',
$posts_dir
) );
}
foreach ( $files as $file ) {
$this->cli->log( sprintf(
'Start importing posts from %s',
$file
) );
$this->importer->import_post( $this->from_json_file( $file ) );
$this->cli->success( sprintf(
'Finished importing posts from %s using %s of memory.',
$file,
size_format( memory_get_peak_usage() )
) );
Utils\wp_clear_object_cache();
}
$this->cli->success( 'All posts imported.' );
}
public function from_json_file( $filename ) {
if ( ! file_exists( $filename ) ) {
return $this->cli->error( sprintf(
'File %s not found.',
$filename
) );
}
return json_decode( file_get_contents( $filename ) );
}
protected function to_json_file( $filename, $data ) {
return file_put_contents( $filename, json_encode( $data ) );
}
}