Skip to content

Commit c51d9c4

Browse files
committed
First topological sorter draft
1 parent 4438d72 commit c51d9c4

9 files changed

+413
-75
lines changed

packages/playground/data-liberation/bootstrap.php

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
require_once __DIR__ . '/src/import/WP_Attachment_Downloader_Event.php';
5656
require_once __DIR__ . '/src/import/WP_Stream_Importer.php';
5757
require_once __DIR__ . '/src/import/WP_Markdown_Importer.php';
58+
require_once __DIR__ . '/src/import/WP_Logger.php';
59+
require_once __DIR__ . '/src/import/WP_Topological_Sorter.php';
5860

5961
require_once __DIR__ . '/src/utf8_decoder.php';
6062

packages/playground/data-liberation/plugin.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,10 @@
2727

2828
add_action('init', function() {
2929
if ( defined( 'WP_CLI' ) && WP_CLI ) {
30-
/**
31-
* Import a WXR file.
32-
*
33-
* <file>
34-
* : The WXR file to import.
35-
*/
36-
$command = function ( $args, $assoc_args ) {
37-
$file = $args[0];
38-
data_liberation_import( $file );
39-
};
30+
require_once __DIR__ . '/src/cli/WP_Import_Command.php';
4031

4132
// Register the WP-CLI import command.
42-
// Example usage: wp data-liberation /path/to/file.xml
43-
WP_CLI::add_command( 'data-liberation', $command );
33+
WP_CLI::add_command( 'data-liberation', WP_Import_Command::class );
4434
}
4535
});
4636

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
require_once __DIR__ . '/WP_Import_Logger.php';
4+
5+
/**
6+
* Implements the `wp data-liberation` command.
7+
*
8+
* ## EXAMPLES
9+
*
10+
* # Import a WXR file.
11+
* wp data-liberation import /path/to/file.xml
12+
*
13+
* # Import all files inside a folder.
14+
* wp data-liberation import /path/to/folder
15+
*
16+
* # Import a WXR file from a URL.
17+
* wp data-liberation import http://example.com/file.xml
18+
*
19+
* Success: Imported data.
20+
*/
21+
class WP_Import_Command {
22+
/**
23+
* @var bool $dry_run Whether to perform a dry run.
24+
*/
25+
private $dry_run = false;
26+
27+
/**
28+
* @var WP_Stream_Importer $importer The importer instance.
29+
*/
30+
private $importer = null;
31+
32+
private $wxr_path = '';
33+
34+
/**
35+
* Import a WXR file.
36+
*
37+
* ## OPTIONS
38+
*
39+
* <path>
40+
* : The path to the WXR file. Either a file, a directory or a URL.
41+
*
42+
* [--dry-run]
43+
* : Perform a dry run if set.
44+
*
45+
* ## EXAMPLES
46+
*
47+
* wp data-liberation import /path/to/file.xml
48+
*
49+
* @param array $args
50+
* @param array $assoc_args
51+
* @return void
52+
*/
53+
public function import( $args, $assoc_args ) {
54+
$path = $args[0];
55+
$this->dry_run = WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run', false );
56+
$options = array(
57+
'logger' => new WP_Import_logger(),
58+
);
59+
60+
if ( extension_loaded( 'pcntl' ) ) {
61+
// Set the signal handler.
62+
$this->register_handlers();
63+
}
64+
65+
if ( filter_var( $path, FILTER_VALIDATE_URL ) ) {
66+
// Import URL.
67+
$this->import_wxr_url( $path, $options );
68+
} elseif ( is_dir( $path ) ) {
69+
$count = 0;
70+
// Get all the WXR files in the directory.
71+
foreach ( wp_visit_file_tree( $path ) as $event ) {
72+
foreach ( $event->files as $file ) {
73+
if ( $file->isFile() && 'xml' === pathinfo( $file->getPathname(), PATHINFO_EXTENSION ) ) {
74+
++$count;
75+
76+
// Import the WXR file.
77+
$this->import_wxr_file( $file->getPathname(), $options );
78+
}
79+
}
80+
}
81+
82+
if ( ! $count ) {
83+
WP_CLI::error( WP_CLI::colorize( "No WXR files found in the {$path} directory" ) );
84+
}
85+
} else {
86+
if ( ! is_file( $path ) ) {
87+
WP_CLI::error( WP_CLI::colorize( "File not found: %R{$path}%n" ) );
88+
}
89+
90+
// Import the WXR file.
91+
$this->import_wxr_file( $path, $options );
92+
}
93+
}
94+
95+
/**
96+
* Import a WXR file.
97+
*
98+
* @param string $file_path The path to the WXR file.
99+
* @return void
100+
*/
101+
private function import_wxr_file( $file_path, $options = array() ) {
102+
$this->wxr_path = $file_path;
103+
$this->importer = WP_Stream_Importer::create_for_wxr_file( $file_path, $options );
104+
105+
$this->import_wxr();
106+
}
107+
108+
/**
109+
* Import a WXR file from a URL.
110+
*
111+
* @param string $url The URL to the WXR file.
112+
* @return void
113+
*/
114+
private function import_wxr_url( $url, $options = array() ) {
115+
$this->wxr_path = $url;
116+
$this->importer = WP_Stream_Importer::create_for_wxr_url( $url, $options );
117+
118+
$this->import_wxr();
119+
}
120+
121+
/**
122+
* Import the WXR file.
123+
*/
124+
private function import_wxr() {
125+
if ( ! $this->importer ) {
126+
WP_CLI::error( 'Could not create importer' );
127+
}
128+
129+
WP_CLI::line( "Importing {$this->wxr_path}" );
130+
131+
if ( $this->dry_run ) {
132+
WP_CLI::line( 'Dry run enabled.' );
133+
} else {
134+
while ( $this->importer->next_step() ) {
135+
$current_stage = $this->importer->get_current_stage();
136+
// WP_CLI::line( "Stage {$current_stage}" );
137+
}
138+
}
139+
140+
WP_CLI::success( 'Import finished' );
141+
}
142+
143+
/**
144+
* Callback function registered to `pcntl_signal` to handle signals.
145+
*
146+
* @param int $signal The signal number.
147+
* @return void
148+
*/
149+
protected function signal_handler( $signal ) {
150+
switch ( $signal ) {
151+
case SIGINT:
152+
WP_CLI::line( 'Received SIGINT signal' );
153+
exit( 0 );
154+
155+
case SIGTERM:
156+
WP_CLI::line( 'Received SIGTERM signal' );
157+
exit( 0 );
158+
}
159+
}
160+
161+
/**
162+
* Register signal handlers for the command.
163+
*
164+
* @return void
165+
*/
166+
private function register_handlers() {
167+
// Handle the Ctrl + C signal to terminate the program.
168+
pcntl_signal( SIGINT, array( $this, 'signal_handler' ) );
169+
170+
// Handle the `kill` command to terminate the program.
171+
pcntl_signal( SIGTERM, array( $this, 'signal_handler' ) );
172+
}
173+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Entity importer logger class.
5+
*/
6+
class WP_Import_Logger {
7+
/**
8+
* Log a debug message.
9+
*
10+
* @param string $message Message to log
11+
*/
12+
public function debug( $message ) {
13+
WP_CLI::debug( $message );
14+
}
15+
16+
/**
17+
* Log an info message.
18+
*
19+
* @param string $message Message to log
20+
*/
21+
public function info( $message ) {
22+
WP_CLI::log( $message );
23+
}
24+
25+
/**
26+
* Log a warning message.
27+
*
28+
* @param string $message Message to log
29+
*/
30+
public function warning( $message ) {
31+
WP_CLI::warning( $message );
32+
}
33+
34+
/**
35+
* Log an error message.
36+
*
37+
* @param string $message Message to log
38+
*/
39+
public function error( $message ) {
40+
WP_CLI::error( $message, false );
41+
}
42+
43+
/**
44+
* Log a notice message.
45+
*
46+
* @param string $message Message to log
47+
*/
48+
public function notice( $message ) {
49+
WP_CLI::log( $message );
50+
}
51+
}

packages/playground/data-liberation/src/import/WP_Entity_Importer.php

+1-55
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function __construct( $options = array() ) {
9393
$this->mapping['term_id'] = array();
9494
$this->requires_remapping = $empty_types;
9595
$this->exists = $empty_types;
96-
$this->logger = new Logger();
96+
$this->logger = isset( $options['logger'] ) ? $options['logger'] : new WP_Logger();
9797

9898
$this->options = wp_parse_args(
9999
$options,
@@ -1191,57 +1191,3 @@ public static function sort_comments_by_id( $a, $b ) {
11911191
return $a['comment_id'] - $b['comment_id'];
11921192
}
11931193
}
1194-
1195-
/**
1196-
* @TODO how to treat this? Should this class even exist?
1197-
* how does WordPress handle different levels? It
1198-
* seems useful for usage in wp-cli, Blueprints,
1199-
* and other non-web environments.
1200-
*/
1201-
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound
1202-
class Logger {
1203-
/**
1204-
* Log a debug message.
1205-
*
1206-
* @param string $message Message to log
1207-
*/
1208-
public function debug( $message ) {
1209-
// echo( '[DEBUG] ' . $message );
1210-
}
1211-
1212-
/**
1213-
* Log an info message.
1214-
*
1215-
* @param string $message Message to log
1216-
*/
1217-
public function info( $message ) {
1218-
// echo( '[INFO] ' . $message );
1219-
}
1220-
1221-
/**
1222-
* Log a warning message.
1223-
*
1224-
* @param string $message Message to log
1225-
*/
1226-
public function warning( $message ) {
1227-
echo( '[WARNING] ' . $message );
1228-
}
1229-
1230-
/**
1231-
* Log an error message.
1232-
*
1233-
* @param string $message Message to log
1234-
*/
1235-
public function error( $message ) {
1236-
echo( '[ERROR] ' . $message );
1237-
}
1238-
1239-
/**
1240-
* Log a notice message.
1241-
*
1242-
* @param string $message Message to log
1243-
*/
1244-
public function notice( $message ) {
1245-
// echo( '[NOTICE] ' . $message );
1246-
}
1247-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Entity importer logger class.
5+
*/
6+
class WP_Logger {
7+
/**
8+
* Log a debug message.
9+
*
10+
* @param string $message Message to log
11+
*/
12+
public function debug( $message ) {
13+
// echo( '[DEBUG] ' . $message );
14+
}
15+
16+
/**
17+
* Log an info message.
18+
*
19+
* @param string $message Message to log
20+
*/
21+
public function info( $message ) {
22+
// echo( '[INFO] ' . $message );
23+
}
24+
25+
/**
26+
* Log a warning message.
27+
*
28+
* @param string $message Message to log
29+
*/
30+
public function warning( $message ) {
31+
echo( "[WARNING] {$message}\n" );
32+
}
33+
34+
/**
35+
* Log an error message.
36+
*
37+
* @param string $message Message to log
38+
*/
39+
public function error( $message ) {
40+
echo( "[ERROR] $message\n" );
41+
}
42+
43+
/**
44+
* Log a notice message.
45+
*
46+
* @param string $message Message to log
47+
*/
48+
public function notice( $message ) {
49+
// echo( '[NOTICE] ' . $message );
50+
}
51+
}

0 commit comments

Comments
 (0)