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3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / ProductAttributesLookup / CLIRunner.php
woocommerce / src / Internal / ProductAttributesLookup Last commit date
CLIRunner.php 1 year ago DataRegenerator.php 3 months ago Filterer.php 1 year ago LookupDataStore.php 3 months ago
CLIRunner.php
517 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Internal\ProductAttributesLookup;
4
5 use WP_CLI;
6
7 /**
8 * Command line tools to handle the regeneration of the product attributes lookup table.
9 */
10 class CLIRunner {
11
12 /**
13 * The instance of DataRegenerator to use.
14 *
15 * @var DataRegenerator
16 */
17 private DataRegenerator $data_regenerator;
18
19 /**
20 * The instance of DataRegenerator to use.
21 *
22 * @var LookupDataStore
23 */
24 private LookupDataStore $lookup_data_store;
25
26 /**
27 * Creates a new instance of the class.
28 *
29 * Normally we define a public 'init' method with the class dependencies passed as arguments
30 * and then the DI container executes it, but if we do that a dummy command will be created
31 * for that method. Therefore, in this case we retrieve the dependencies manually instead.
32 */
33 public function __construct() {
34 $container = wc_get_container();
35 $this->data_regenerator = $container->get( DataRegenerator::class );
36 $this->lookup_data_store = $container->get( LookupDataStore::class );
37 }
38
39 // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
40
41 /**
42 * Enable the usage of the product attributes lookup table.
43 *
44 * @param array $args Positional arguments passed to the command.
45 * @param array $assoc_args Associative arguments (options) passed to the command.
46 */
47 public function enable( array $args = array(), array $assoc_args = array() ) {
48 return $this->invoke( 'enable_core', $args, $assoc_args );
49 }
50
51 /**
52 * Core method for the "enable" command.
53 *
54 * @param array $args Positional arguments passed to the command.
55 * @param array $assoc_args Associative arguments (options) passed to the command.
56 */
57 private function enable_core( array $args, array $assoc_args ) {
58 $table_name = $this->lookup_data_store->get_lookup_table_name();
59 if ( 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' ) ) {
60 $this->warning( "The usage of the of the %W{$table_name}%n table is already enabled." );
61 return;
62 }
63
64 if ( ! array_key_exists( 'force', $assoc_args ) ) {
65 $must_confirm = true;
66 if ( $this->lookup_data_store->regeneration_is_in_progress() ) {
67 $this->warning( "The regeneration of the %W{$table_name}%n table is currently in process." );
68 } elseif ( $this->lookup_data_store->regeneration_was_aborted() ) {
69 $this->warning( "The regeneration of the %W{$table_name}%n table was aborted." );
70 } elseif ( 0 === $this->get_lookup_table_info()['total_rows'] ) {
71 $this->warning( "The %W{$table_name}%n table is empty." );
72 } else {
73 $must_confirm = false;
74 }
75
76 if ( $must_confirm ) {
77 WP_CLI::confirm( 'Are you sure that you want to enable the table usage?' );
78 }
79 }
80
81 update_option( 'woocommerce_attribute_lookup_enabled', 'yes' );
82 $table_name = $this->lookup_data_store->get_lookup_table_name();
83 $this->success( "The usage of the %W{$table_name}%n table for product attribute lookup has been enabled." );
84 }
85
86 /**
87 * Disable the usage of the product attributes lookup table.
88 *
89 * @param array $args Positional arguments passed to the command.
90 * @param array $assoc_args Associative arguments (options) passed to the command.
91 */
92 public function disable( array $args = array(), array $assoc_args = array() ) {
93 return $this->invoke( 'disable_core', $args, $assoc_args );
94 }
95
96 /**
97 * Core method for the "disable" command.
98 *
99 * @param array $args Positional arguments passed to the command.
100 * @param array $assoc_args Associative arguments (options) passed to the command.
101 */
102 private function disable_core( array $args, array $assoc_args ) {
103 if ( 'yes' !== get_option( 'woocommerce_attribute_lookup_enabled' ) ) {
104 $table_name = $this->lookup_data_store->get_lookup_table_name();
105 $this->warning( "The usage of the of the %W{$table_name}%n table is already disabled." );
106 return;
107 }
108 update_option( 'woocommerce_attribute_lookup_enabled', 'no' );
109 $table_name = $this->lookup_data_store->get_lookup_table_name();
110 $this->success( "The usage of the %W{$table_name}%n table for product attribute lookup has been disabled." );
111 }
112
113 /**
114 * Regenerate the product attributes lookup table data for one single product.
115 *
116 * ## OPTIONS
117 *
118 * <product-id>
119 * : The id of the product for which the data will be regenerated.
120 *
121 * [--disable-db-optimization]
122 * : Don't use optimized database access even if products are stored as custom post types.
123 *
124 * ## EXAMPLES
125 *
126 * wp wc palt regenerate_for_product 34 --disable-db-optimization
127 *
128 * @param array $args Positional arguments passed to the command.
129 * @param array $assoc_args Associative arguments (options) passed to the command.
130 */
131 public function regenerate_for_product( array $args = array(), array $assoc_args = array() ) {
132 return $this->invoke( 'regenerate_for_product_core', $args, $assoc_args );
133 }
134
135 /**
136 * Core method for the "regenerate_for_product" command.
137 *
138 * @param array $args Positional arguments passed to the command.
139 * @param array $assoc_args Associative arguments (options) passed to the command.
140 */
141 private function regenerate_for_product_core( array $args = array(), array $assoc_args = array() ) {
142 $product_id = current( $args );
143 $this->data_regenerator->check_can_do_lookup_table_regeneration( $product_id );
144 $use_db_optimization = ! array_key_exists( 'disable-db-optimization', $assoc_args );
145 $this->check_can_use_db_optimization( $use_db_optimization );
146 $start_time = microtime( true );
147 $this->lookup_data_store->create_data_for_product( $product_id, $use_db_optimization );
148
149 if ( $this->lookup_data_store->get_last_create_operation_failed() ) {
150 $this->error( "Lookup data regeneration failed.\nSee the WooCommerce logs (source is %9palt-updates%n) for details." );
151 } else {
152 $total_time = microtime( true ) - $start_time;
153 WP_CLI::success( sprintf( 'Attributes lookup data for product %d regenerated in %f seconds.', $product_id, $total_time ) );
154 }
155 }
156
157 /**
158 * If database access optimization is requested but can't be used, show a warning.
159 *
160 * @param bool $use_db_optimization True if database access optimization is requested.
161 */
162 private function check_can_use_db_optimization( bool $use_db_optimization ) {
163 if ( $use_db_optimization && ! $this->lookup_data_store->can_use_optimized_db_access() ) {
164 $this->warning( "Optimized database access can't be used (products aren't stored as custom post types)." );
165 }
166 }
167
168 /**
169 * Obtain information about the product attributes lookup table.
170 *
171 * @param array $args Positional arguments passed to the command.
172 * @param array $assoc_args Associative arguments (options) passed to the command.
173 */
174 public function info( array $args = array(), array $assoc_args = array() ) {
175 return $this->invoke( 'info_core', $args, $assoc_args );
176 }
177
178 /**
179 * Core method for the "info" command.
180 *
181 * @param array $args Positional arguments passed to the command.
182 * @param array $assoc_args Associative arguments (options) passed to the command.
183 */
184 private function info_core( array $args, array $assoc_args ) {
185 global $wpdb;
186
187 $enabled = 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' );
188
189 $table_name = $this->lookup_data_store->get_lookup_table_name();
190 $info = $this->get_lookup_table_info();
191
192 $this->log( "Table name: %W{$table_name}%n" );
193 $this->log( 'Table usage is ' . ( $enabled ? '%Genabled%n' : '%Ydisabled%n' ) );
194 $this->log( "The table contains %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
195
196 if ( $info['total_rows'] > 0 ) {
197 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
198 $highest_product_id_in_table = $wpdb->get_var( 'select max(product_or_parent_id) from ' . $table_name );
199 $this->log( "The highest product id in the table is %B{$highest_product_id_in_table}%n." );
200 }
201
202 if ( $this->lookup_data_store->regeneration_is_in_progress() ) {
203 $max_product_id_to_process = get_option( 'woocommerce_attribute_lookup_last_product_id_to_process', '???' );
204 WP_CLI::log( '' );
205 $this->warning( 'Full regeneration of the table is currently %Gin progress.%n' );
206 if ( ! $this->data_regenerator->has_scheduled_action_for_regeneration_step() ) {
207 $this->log( 'However, there are %9NO%n actions scheduled to run the regeneration steps (a %9wp cli palt regenerate%n command was aborted?).' );
208 }
209 $this->log( "The last product id that will be processed is %Y{$max_product_id_to_process}%n." );
210 $this->log( "\nRun %9wp cli palt abort_regeneration%n to abort the regeneration process," );
211 $this->log( "then you'll be able to run %9wp cli palt resume_regeneration%n to resume the regeneration process," );
212 } elseif ( $this->lookup_data_store->regeneration_was_aborted() ) {
213 $max_product_id_to_process = get_option( 'woocommerce_attribute_lookup_last_product_id_to_process', '???' );
214 WP_CLI::log( '' );
215 $this->warning( "Full regeneration of the table has been %Raborted.%n\nThe last product id that will be processed is %Y{$max_product_id_to_process}%n." );
216 $this->log( "\nRun %9wp cli palt resume_regeneration%n to resume the regeneration process." );
217 }
218 }
219
220 /**
221 * Abort the background regeneration of the product attributes lookup table that is happening in the background.
222 *
223 * [--cleanup]
224 * : Also cleanup temporary data (so regeneration can't be resumed, but it can be restarted).
225 *
226 * ## EXAMPLES
227 *
228 * wp wc palt abort_regeneration --cleanup
229 *
230 * @param array $args Positional arguments passed to the command.
231 * @param array $assoc_args Associative arguments (options) passed to the command.
232 */
233 public function abort_regeneration( array $args = array(), array $assoc_args = array() ) {
234 return $this->invoke( 'abort_regeneration_core', $args, $assoc_args );
235 }
236
237 /**
238 * Core method for the "abort_regeneration" command.
239 *
240 * @param array $args Positional arguments passed to the command.
241 * @param array $assoc_args Associative arguments (options) passed to the command.
242 */
243 private function abort_regeneration_core( array $args, array $assoc_args ) {
244 $this->data_regenerator->abort_regeneration( false );
245 $table_name = $this->lookup_data_store->get_lookup_table_name();
246 $this->success( "The regeneration of the data in the %W{$table_name}%n table has been aborted." );
247 if ( array_key_exists( 'cleanup', $assoc_args ) ) {
248 $this->cleanup_regeneration_progress( array(), array() );
249 }
250 }
251
252 /**
253 * Resume the background regeneration of the product attributes lookup table after it has been aborted.
254 *
255 * @param array $args Positional arguments passed to the command.
256 * @param array $assoc_args Associative arguments (options) passed to the command.
257 */
258 public function resume_regeneration( array $args = array(), array $assoc_args = array() ) {
259 return $this->invoke( 'resume_regeneration_core', $args, $assoc_args );
260 }
261
262 /**
263 * Core method for the "resume_regeneration" command.
264 *
265 * @param array $args Positional arguments passed to the command.
266 * @param array $assoc_args Associative arguments (options) passed to the command.
267 */
268 private function resume_regeneration_core( array $args, array $assoc_args ) {
269 $this->data_regenerator->resume_regeneration( false );
270 $table_name = $this->lookup_data_store->get_lookup_table_name();
271 $this->success( "The regeneration of the data in the %W{$table_name}%n table has been resumed." );
272 }
273
274 /**
275 * Delete the temporary data used during the regeneration of the product attributes lookup table. This data is normally deleted automatically after the regeneration process finishes.
276 *
277 * @param array $args Positional arguments passed to the command.
278 * @param array $assoc_args Associative arguments (options) passed to the command.
279 */
280 public function cleanup_regeneration_progress( array $args = array(), array $assoc_args = array() ) {
281 return $this->invoke( 'cleanup_regeneration_progress_core', $args, $assoc_args );
282 }
283
284 /**
285 * Core method for the "cleanup_regeneration_progress" command.
286 *
287 * @param array $args Positional arguments passed to the command.
288 * @param array $assoc_args Associative arguments (options) passed to the command.
289 */
290 private function cleanup_regeneration_progress_core( array $args, array $assoc_args ) {
291 $this->data_regenerator->finalize_regeneration( false );
292 $table_name = $this->lookup_data_store->get_lookup_table_name();
293 $this->success( "The temporary data used for regeneration of the data in the %W{$table_name}%n table has been deleted." );
294 }
295
296 /**
297 * Initiate the background regeneration of the product attributes lookup table. The regeneration will happen in the background, using scheduled actions.
298 *
299 * ## OPTIONS
300 *
301 * [--force]
302 * : Don't prompt for confirmation if the product attributes lookup table isn't empty.
303 *
304 * ## EXAMPLES
305 *
306 * wp wc palt initiate_regeneration --force
307 *
308 * @param array $args Positional arguments passed to the command.
309 * @param array $assoc_args Associative arguments (options) passed to the command.
310 */
311 public function initiate_regeneration( array $args = array(), array $assoc_args = array() ) {
312 return $this->invoke( 'initiate_regeneration_core', $args, $assoc_args );
313 }
314
315 /**
316 * Core method for the "initiate_regeneration" command.
317 *
318 * @param array $args Positional arguments passed to the command.
319 * @param array $assoc_args Associative arguments (options) passed to the command.
320 */
321 private function initiate_regeneration_core( array $args, array $assoc_args ) {
322 $this->data_regenerator->check_can_do_lookup_table_regeneration();
323 $info = $this->get_lookup_table_info();
324 if ( $info['total_rows'] > 0 && ! array_key_exists( 'force', $assoc_args ) ) {
325 $table_name = $this->lookup_data_store->get_lookup_table_name();
326 $this->warning( "The %W{$table_name}%n table contains %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
327 WP_CLI::confirm( 'Initiating the regeneration will first delete the data. Are you sure?' );
328 }
329
330 $this->data_regenerator->initiate_regeneration();
331 $table_name = $this->lookup_data_store->get_lookup_table_name();
332 $this->log( "%GSuccess:%n The regeneration of the data in the %W{$table_name}%n table has been initiated." );
333 }
334
335 /**
336 * Regenerate the product attributes lookup table immediately, without using scheduled tasks.
337 *
338 * ## OPTIONS
339 *
340 * [--force]
341 * : Don't prompt for confirmation if the product attributes lookup table isn't empty.
342 *
343 * [--from-scratch]
344 * : Start table regeneration from scratch even if a regeneration is already in progress.
345 *
346 * [--disable-db-optimization]
347 * : Don't use optimized database access even if products are stored as custom post types.
348 *
349 * [--batch-size=<size>]
350 * : How many products to process in each iteration of the loop.
351 * ---
352 * default: 10
353 * ---
354 *
355 * ## EXAMPLES
356 *
357 * wp wc palt regenerate --force --from-scratch --batch-size=20
358 *
359 * @param array $args Positional arguments passed to the command.
360 * @param array $assoc_args Associative arguments (options) passed to the command.
361 */
362 public function regenerate( array $args = array(), array $assoc_args = array() ) {
363 return $this->invoke( 'regenerate_core', $args, $assoc_args );
364 }
365
366 /**
367 * Core method for the "regenerate" command.
368 *
369 * @param array $args Positional arguments passed to the command.
370 * @param array $assoc_args Associative arguments (options) passed to the command.
371 * @throws \Exception Invalid batch size argument.
372 */
373 private function regenerate_core( array $args = array(), array $assoc_args = array() ) {
374 global $wpdb;
375
376 $table_name = $this->lookup_data_store->get_lookup_table_name();
377
378 $batch_size = $assoc_args['batch-size'] ?? DataRegenerator::PRODUCTS_PER_GENERATION_STEP;
379 if ( ! is_numeric( $batch_size ) || $batch_size < 1 ) {
380 throw new \Exception( 'batch_size must be a number bigger than 0' );
381 }
382
383 $was_enabled = 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' );
384
385 // phpcs:ignore Generic.Commenting.Todo.TaskFound
386 // TODO: adjust for non-CPT datastores (this is only used for the progress bar, though).
387 $products_count = wp_count_posts( 'product' );
388 $products_count = intval( $products_count->publish ) + intval( $products_count->pending ) + intval( $products_count->draft );
389
390 if ( ! $this->lookup_data_store->regeneration_is_in_progress() || array_key_exists( 'from-scratch', $assoc_args ) ) {
391 $info = $this->get_lookup_table_info();
392 if ( $info['total_rows'] > 0 && ! array_key_exists( 'force', $assoc_args ) ) {
393 $this->warning( "The %W{$table_name}%n table contains %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
394 WP_CLI::confirm( 'Triggering the regeneration will first delete the data. Are you sure?' );
395 }
396
397 $this->data_regenerator->finalize_regeneration( false );
398 $last_product_id = $this->data_regenerator->initiate_regeneration( false );
399 if ( 0 === $last_product_id ) {
400 $this->data_regenerator->finalize_regeneration( $was_enabled );
401 WP_CLI::log( 'No products exist in the database, the table is left empty.' );
402 return;
403 }
404 $processed_count = 0;
405 } else {
406 $last_product_id = get_option( 'woocommerce_attribute_lookup_last_product_id_to_process' );
407 if ( false === $last_product_id ) {
408 WP_CLI::error( 'Regeneration seems to be already in progress, but the woocommerce_attribute_lookup_last_product_id_to_process option isn\'t there. Try %9wp cli palt cleanup_regeneration_progress%n first." );' );
409 return 1;
410 }
411 $processed_count = get_option( 'woocommerce_attribute_lookup_processed_count', 0 );
412 $this->log( "Resuming regeneration, %C{$processed_count}%n products have been processed already" );
413 $this->lookup_data_store->set_regeneration_in_progress_flag();
414 }
415
416 $this->data_regenerator->cancel_regeneration_scheduled_action();
417
418 $use_db_optimization = ! array_key_exists( 'disable-db-optimization', $assoc_args );
419 $this->check_can_use_db_optimization( $use_db_optimization );
420 $progress = WP_CLI\Utils\make_progress_bar( '', $products_count );
421 $this->log( "Regenerating %W{$table_name}%n..." );
422 $progress->tick( $processed_count );
423
424 $regeneration_step_failed = false;
425 while ( $this->data_regenerator->do_regeneration_step( $batch_size, $use_db_optimization ) ) {
426 $progress->tick( $batch_size );
427 $regeneration_step_failed = $regeneration_step_failed || $this->data_regenerator->get_last_regeneration_step_failed();
428 }
429
430 $this->data_regenerator->finalize_regeneration( $was_enabled );
431 $time = $progress->formatTime( $progress->elapsed() );
432 $progress->finish();
433
434 if ( $regeneration_step_failed ) {
435 $this->warning( "Lookup data regeneration failed for at least one product.\nSee the WooCommerce logs (source is %9palt-updates%n) for details.\n" );
436 $this->log( "Table %W{$table_name}%n regenerated in {$time}." );
437 } else {
438 $this->log( "%GSuccess:%n Table %W{$table_name}%n regenerated in {$time}." );
439 }
440
441 $info = $this->get_lookup_table_info();
442 $this->log( "The table contains now %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
443 }
444
445 // phpcs:enable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
446
447 /**
448 * Get information about the product attributes lookup table.
449 *
450 * @return array Array containing the 'total_rows' and 'products_count' keys.
451 */
452 private function get_lookup_table_info(): array {
453 global $wpdb;
454
455 $table_name = $this->lookup_data_store->get_lookup_table_name();
456 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
457 $info = $wpdb->get_row( 'select count(1), count(distinct(product_or_parent_id)) from ' . $table_name, ARRAY_N );
458 return array(
459 'total_rows' => absint( $info[0] ),
460 'products_count' => absint( $info[1] ),
461 );
462 }
463
464 /**
465 * Invoke a method from the class, and if an exception is thrown, show it using WP_CLI::error.
466 *
467 * @param string $method_name Name of the method to invoke.
468 * @param array $args Positional arguments to pass to the method.
469 * @param array $assoc_args Associative arguments to pass to the method.
470 * @return mixed Result from the method, or 1 if an exception is thrown.
471 */
472 private function invoke( string $method_name, array $args, array $assoc_args ) {
473 try {
474 return call_user_func( array( $this, $method_name ), $args, $assoc_args );
475 } catch ( \Exception $e ) {
476 WP_CLI::error( $e->getMessage() );
477 return 1;
478 }
479 }
480
481 /**
482 * Show a log message using the WP_CLI text colorization feature.
483 *
484 * @param string $text Text to show.
485 */
486 private function log( string $text ) {
487 WP_CLI::log( WP_CLI::colorize( $text ) );
488 }
489
490 /**
491 * Show a warning message using the WP_CLI text colorization feature.
492 *
493 * @param string $text Text to show.
494 */
495 private function warning( string $text ) {
496 WP_CLI::warning( WP_CLI::colorize( $text ) );
497 }
498
499 /**
500 * Show a success message using the WP_CLI text colorization feature.
501 *
502 * @param string $text Text to show.
503 */
504 private function success( string $text ) {
505 WP_CLI::success( WP_CLI::colorize( $text ) );
506 }
507
508 /**
509 * Show an error message using the WP_CLI text colorization feature.
510 *
511 * @param string $text Text to show.
512 */
513 private function error( string $text ) {
514 WP_CLI::error( WP_CLI::colorize( $text ) );
515 }
516 }
517