-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMigrationCommand.php
145 lines (114 loc) · 3.48 KB
/
MigrationCommand.php
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
<?php
/**
* MigrationCommand
*
* @package convert-to-blocks
*/
namespace ConvertToBlocks;
/**
* Bulk migrates classic editor posts to Gutenberg blocks.
*/
class MigrationCommand extends \WP_CLI_Command {
/**
* Starts a new Migration. The command prints the URL that must be opened in a browser to connect it to the WP CLI.
*
* ## OPTIONS
*
* [--post_type=<post_type>]
* : Optional comma delimited list of post types to migrate. Defaults to post,page
*
* [--only=<only>]
* : Optional comma delimited list of post ids to migrate.
*
* @param array $args The command args
* @param array $opts The command opts
*/
public function start( $args = [], $opts = [] ) {
$agent = new MigrationAgent();
$delay = 5; // 5 second delay between each tick
if ( $agent->is_running() ) {
\WP_CLI::error( 'Please stop the currently running migration first.' );
}
$result = $agent->start( $opts );
if ( empty( $result ) ) {
\WP_CLI::error( 'No posts to migrate.' );
}
$status = $agent->get_status( $opts );
if ( ! $status['running'] ) {
\WP_CLI::error( 'Failed to start migration.' );
}
\WP_CLI::log( 'Migration started...' );
\WP_CLI::log( 'Please open the following URL in a browser to start the migration agent.' );
\WP_CLI::line( '' );
\WP_CLI::log( $result );
\WP_CLI::line( '' );
$total = $status['total'];
$message = "Converting $total Posts ...";
$progress_bar = \WP_CLI\Utils\make_progress_bar( $message, $total );
$progress_bar->tick();
$prev_progress = 0;
$ticks = 0;
while ( true ) {
$status = $agent->get_status();
if ( ! $status['running'] ) {
break;
}
$progress = $status['progress'];
// since the WP CLI progress bar can't tick upto a progress % we need to
// tick in steps upto the progress % of total
if ( $progress !== $prev_progress ) {
$required_ticks = floor( $progress / 100 * $total );
while ( $ticks < $required_ticks ) {
$progress_bar->tick();
$ticks++;
}
$prev_progress = $progress;
}
if ( $ticks < $total ) {
// sleeping helps reduce load on server
sleep( $delay );
} else {
// don't need the full sleep delay on last tick
sleep( 1 );
}
// required as we need to reload options that the browser client is updating
wp_cache_delete( 'alloptions', 'options' );
}
$progress_bar->finish();
\WP_CLI::success( 'Migration finished successfully.' );
// cleanup the options used during migration
$agent->stop();
}
/**
* Stops the currently running migration if active.
*
* @param array $args The command args
* @param array $opts The command opts
*/
public function stop( $args = [], $opts = [] ) {
$agent = new MigrationAgent();
if ( ! $agent->is_running() ) {
\WP_CLI::warning( 'No migrations are currently running' );
return;
}
$agent->stop( $opts );
\WP_CLI::success( 'Migration stopped successfully' );
}
/**
* Prints the status of the currently running migration.
*
* @param array $args The command args
* @param array $opts The command opts
*/
public function status( $args = [], $opts = [] ) {
$agent = new MigrationAgent();
$status = $agent->get_status( $opts );
if ( ! $status['running'] ) {
\WP_CLI::log( 'No migrations are currently running.' );
return;
}
\WP_CLI::log( 'Migration is currently running ...' );
\WP_CLI::log( $status['progress'] . ' [' . ( $status['cursor'] + 1 ) . '/' . $status['total'] . ']' );
\WP_CLI::log( 'Active: ' . $status['active'] );
}
}