|
| 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 | +} |
0 commit comments