diff --git a/packages/playground/data-liberation/bin/import/blueprint-import-wxr.json b/packages/playground/data-liberation/bin/import/blueprint-import-wxr.json index b8ad517fae..55ab107921 100644 --- a/packages/playground/data-liberation/bin/import/blueprint-import-wxr.json +++ b/packages/playground/data-liberation/bin/import/blueprint-import-wxr.json @@ -11,8 +11,8 @@ "pluginPath": "data-liberation/plugin.php" }, { - "step": "wp-cli", - "command": "wp data-liberation import /wordpress/wp-content/uploads/import-wxr" + "step": "runPHP", + "code": "files as $file ) {\nif ( $file->isFile() && pathinfo( $file->getPathname(), PATHINFO_EXTENSION ) === 'xml' ) {\ndata_liberation_import( $file->getPathname() );\n}\n}\n};" } ] } diff --git a/packages/playground/data-liberation/bootstrap.php b/packages/playground/data-liberation/bootstrap.php index 917dcf1197..decd7bfe82 100644 --- a/packages/playground/data-liberation/bootstrap.php +++ b/packages/playground/data-liberation/bootstrap.php @@ -65,7 +65,6 @@ require_once __DIR__ . '/src/import/WP_Stream_Importer.php'; require_once __DIR__ . '/src/import/WP_Entity_Iterator_Chain.php'; require_once __DIR__ . '/src/import/WP_Retry_Frontloading_Iterator.php'; -require_once __DIR__ . '/src/import/WP_Logger.php'; require_once __DIR__ . '/src/utf8_decoder.php'; diff --git a/packages/playground/data-liberation/plugin.php b/packages/playground/data-liberation/plugin.php index f91ea4a0ca..18520d3e20 100644 --- a/packages/playground/data-liberation/plugin.php +++ b/packages/playground/data-liberation/plugin.php @@ -39,29 +39,40 @@ function () { } ); -function data_liberation_init() { - if ( defined( 'WP_CLI' ) && WP_CLI ) { - require_once __DIR__ . '/src/cli/WP_Import_Command.php'; +add_action( + 'init', + function () { + if ( defined( 'WP_CLI' ) && WP_CLI ) { + /** + * Import a WXR file. + * + * + * : The WXR file to import. + */ + $command = function ( $args, $assoc_args ) { + $file = $args[0]; + data_liberation_import( $file ); + }; + + // Register the WP-CLI import command. + // Example usage: wp data-liberation /path/to/file.xml + WP_CLI::add_command( 'data-liberation', $command ); + } - // Register the WP-CLI import command. - WP_CLI::add_command( 'data-liberation', WP_Import_Command::class ); + register_post_status( + 'error', + array( + 'label' => _x( 'Error', 'post' ), // Label name + 'public' => false, + 'exclude_from_search' => false, + 'show_in_admin_all_list' => false, + 'show_in_admin_status_list' => false, + // translators: %s is the number of errors + 'label_count' => _n_noop( 'Error (%s)', 'Error (%s)' ), + ) + ); } - - register_post_status( - 'error', - array( - 'label' => _x( 'Error', 'post' ), // Label name - 'public' => false, - 'exclude_from_search' => false, - 'show_in_admin_all_list' => false, - 'show_in_admin_status_list' => false, - // translators: %s is the number of errors - 'label_count' => _n_noop( 'Error (%s)', 'Error (%s)' ), - ) - ); -} - -add_action( 'init', 'data_liberation_init' ); +); function data_liberation_activate() { // Create tables and option. diff --git a/packages/playground/data-liberation/src/functions.php b/packages/playground/data-liberation/src/functions.php index 90e41e5dd6..b26ff145cd 100644 --- a/packages/playground/data-liberation/src/functions.php +++ b/packages/playground/data-liberation/src/functions.php @@ -255,3 +255,37 @@ function mb_str_split( $input, $split_length = 1, $encoding = null ) { return $result; } } + +/** + * Import a WXR file. Used by the CLI. + * + * @param string $path The path to the WXR file. + * @return void + */ +function data_liberation_import( $path ): bool { + $importer = WP_Stream_Importer::create_for_wxr_file( $path ); + + if ( ! $importer ) { + return false; + } + + $is_wp_cli = defined( 'WP_CLI' ) && WP_CLI; + + if ( $is_wp_cli ) { + WP_CLI::line( "Importing from {$path}" ); + } + + while ( $importer->next_step() ) { + // Output the current stage if running in WP-CLI. + if ( $is_wp_cli ) { + $current_stage = $importer->get_current_stage(); + WP_CLI::line( "Import: stage {$current_stage}" ); + } + } + + if ( $is_wp_cli ) { + WP_CLI::success( 'Import ended' ); + } + + return true; +} diff --git a/packages/playground/data-liberation/src/import/WP_Entity_Importer.php b/packages/playground/data-liberation/src/import/WP_Entity_Importer.php index a437823777..03ec4cbc21 100644 --- a/packages/playground/data-liberation/src/import/WP_Entity_Importer.php +++ b/packages/playground/data-liberation/src/import/WP_Entity_Importer.php @@ -95,7 +95,7 @@ public function __construct( $options = array() ) { $this->mapping['term_id'] = array(); $this->requires_remapping = $empty_types; $this->exists = $empty_types; - $this->logger = isset( $options['logger'] ) ? $options['logger'] : new WP_Logger(); + $this->logger = new Logger(); $this->options = wp_parse_args( $options, @@ -126,8 +126,6 @@ public function import_entity( WP_Imported_Entity $entity ) { case WP_Imported_Entity::TYPE_TAG: case WP_Imported_Entity::TYPE_CATEGORY: return $this->import_term( $data ); - case WP_Imported_Entity::TYPE_TERM_META: - return $this->import_term_meta( $data, $data['term_id'] ); case WP_Imported_Entity::TYPE_USER: return $this->import_user( $data ); case WP_Imported_Entity::TYPE_SITE_OPTION: @@ -390,40 +388,6 @@ public function import_term( $data ) { return $term_id; } - public function import_term_meta( $meta_item, $term_id ) { - if ( empty( $meta_item ) ) { - return true; - } - - /** - * Pre-process term meta data. - * - * @param array $meta_item Meta data. (Return empty to skip.) - * @param int $term_id Term the meta is attached to. - */ - $meta_item = apply_filters( 'wxr_importer_pre_process_term_meta', $meta_item, $term_id ); - if ( empty( $meta_item ) ) { - return false; - } - - // Have we already processed this? - if ( isset( $element['_already_mapped'] ) ) { - $this->logger->debug( 'Skipping term meta, already processed' ); - return; - } - - if ( ! isset( $meta_item['term_id'] ) ) { - $meta_item['term_id'] = $term_id; - } - - $value = maybe_unserialize( $meta_item['meta_value'] ); - $term_meta_id = add_term_meta( $meta_item['term_id'], wp_slash( $meta_item['meta_key'] ), wp_slash_strings_only( $value ) ); - - do_action( 'wxr_importer_processed_term_meta', $term_meta_id, $meta_item, $meta_item['term_id'] ); - - return $term_meta_id; - } - /** * Prefill existing post data. * @@ -480,8 +444,6 @@ protected function post_exists( $data ) { * Note that new/updated terms, comments and meta are imported for the last of the above. */ public function import_post( $data ) { - $parent_id = isset( $data['post_parent'] ) ? (int) $data['post_parent'] : 0; - /** * Pre-process post data. * @@ -490,7 +452,7 @@ public function import_post( $data ) { * @param array $comments Comments on the post. * @param array $terms Terms on the post. */ - $data = apply_filters( 'wxr_importer_pre_process_post', $data, $parent_id ); + $data = apply_filters( 'wxr_importer_pre_process_post', $data ); if ( empty( $data ) ) { $this->logger->debug( 'Skipping post, empty data' ); return false; @@ -659,37 +621,6 @@ public function import_post( $data ) { } $this->mark_post_exists( $data, $post_id ); - // Add terms to the post - /*if ( ! empty( $data['terms'] ) ) { - $terms_to_set = array(); - - foreach ( $data['terms'] as $term ) { - // Back compat with WXR 1.0 map 'tag' to 'post_tag' - $taxonomy = ( 'tag' === $term['taxonomy'] ) ? 'post_tag' : $term['taxonomy']; - $term_exists = term_exists( $term['slug'], $taxonomy ); - $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists; - - if ( ! $term_id ) { - // @TODO: Add a unit test with a WXR with one post and X tags without root declated tags. - $new_term = wp_insert_term( $term['slug'], $taxonomy, $term ); - - if ( ! is_wp_error( $new_term ) ) { - $term_id = $new_term['term_id']; - - $this->topological_sorter->update_mapped_id( $new_term, $term_id ); - } else { - continue; - } - } - $terms_to_set[ $taxonomy ][] = intval( $term_id ); - } - - foreach ( $terms_to_set as $tax => $ids ) { - // Add the post terms to the post - wp_set_post_terms( $post_id, $ids, $tax ); - } - }*/ - $this->logger->info( sprintf( /* translators: 1: post title, 2: post type name */ @@ -717,7 +648,6 @@ public function import_post( $data ) { * @param array $terms Raw term data, already processed. */ do_action( 'wxr_importer_processed_post', $post_id, $data ); - return $post_id; } @@ -1289,3 +1219,57 @@ public static function sort_comments_by_id( $a, $b ) { return $a['comment_id'] - $b['comment_id']; } } + +/** + * @TODO how to treat this? Should this class even exist? + * how does WordPress handle different levels? It + * seems useful for usage in wp-cli, Blueprints, + * and other non-web environments. + */ +// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound +class Logger { + /** + * Log a debug message. + * + * @param string $message Message to log + */ + public function debug( $message ) { + // echo( '[DEBUG] ' . $message ); + } + + /** + * Log an info message. + * + * @param string $message Message to log + */ + public function info( $message ) { + // echo( '[INFO] ' . $message ); + } + + /** + * Log a warning message. + * + * @param string $message Message to log + */ + public function warning( $message ) { + echo( '[WARNING] ' . $message ); + } + + /** + * Log an error message. + * + * @param string $message Message to log + */ + public function error( $message ) { + echo( '[ERROR] ' . $message ); + } + + /** + * Log a notice message. + * + * @param string $message Message to log + */ + public function notice( $message ) { + // echo( '[NOTICE] ' . $message ); + } +} diff --git a/packages/playground/data-liberation/src/import/WP_Imported_Entity.php b/packages/playground/data-liberation/src/import/WP_Imported_Entity.php index 8e0dcb230e..96c3dd3dd2 100644 --- a/packages/playground/data-liberation/src/import/WP_Imported_Entity.php +++ b/packages/playground/data-liberation/src/import/WP_Imported_Entity.php @@ -7,7 +7,6 @@ class WP_Imported_Entity { const TYPE_COMMENT = 'comment'; const TYPE_COMMENT_META = 'comment_meta'; const TYPE_TERM = 'term'; - const TYPE_TERM_META = 'term_meta'; const TYPE_TAG = 'tag'; const TYPE_CATEGORY = 'category'; const TYPE_USER = 'user'; diff --git a/packages/playground/data-liberation/src/import/WP_Logger.php b/packages/playground/data-liberation/src/import/WP_Logger.php deleted file mode 100644 index 87605336fe..0000000000 --- a/packages/playground/data-liberation/src/import/WP_Logger.php +++ /dev/null @@ -1,51 +0,0 @@ -