-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Data Liberation] Add WXR import CLI script #2012
Changes from all commits
7e97d53
92c830e
fab4f2f
a919bed
d6b24a4
d6c9019
83d8626
6e3e05e
388965b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"$schema": "../../../blueprints/public/blueprint-schema.json", | ||
"constants": { | ||
"WP_DEBUG": true, | ||
"WP_DEBUG_LOG": true | ||
}, | ||
"login": true, | ||
"steps": [ | ||
{ | ||
"step": "activatePlugin", | ||
"pluginPath": "data-liberation/plugin.php" | ||
}, | ||
{ | ||
"step": "runPHP", | ||
"code": "<?php require_once 'wordpress/wp-load.php';\n$upload_dir = wp_upload_dir();\nforeach ( wp_visit_file_tree( $upload_dir['basedir'] . '/import-wxr' ) as $event ) {\nforeach ( $event->files as $file ) {\nif ( $file->isFile() && pathinfo( $file->getPathname(), PATHINFO_EXTENSION ) === 'xml' ) {\ndata_liberation_import( $file->getPathname() );\n}\n}\n};" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
# | ||
# A script that accepts a folder and imports all WXR files into a WordPress site | ||
# | ||
# Usage: | ||
# ./import-wxr.sh <folder-name> | ||
# | ||
|
||
# Display help message | ||
show_help() { | ||
echo "Usage: $0 [-h|--help] <folder-name>" | ||
echo "Options:" | ||
echo " -h, --help Show this help message" | ||
} | ||
|
||
# Check if no arguments were provided. If so, display help message | ||
if [ $# -eq 0 ]; then | ||
show_help | ||
exit 1 | ||
fi | ||
|
||
# Parse command line arguments. If an invalid argument is provided, display help message | ||
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in | ||
-h | --help ) | ||
show_help | ||
exit 0 | ||
;; | ||
esac; shift; done | ||
if [[ "$1" == '--' ]]; then shift; fi | ||
|
||
# Check if filename is provided. If not, display error message. | ||
if [ -z "$1" ]; then | ||
echo "Error: No folder provided" | ||
show_help | ||
exit 1 | ||
fi | ||
|
||
# Check if the file exists | ||
if [ -d "$1" ]; then | ||
bun ../../../cli/src/cli.ts \ | ||
server \ | ||
--mount=../../:/wordpress/wp-content/plugins/data-liberation \ | ||
--mount=$1:/wordpress/wp-content/uploads/import-wxr \ | ||
--blueprint=./blueprint-import-wxr.json | ||
else | ||
echo "Error: Folder '$1' does not exist" | ||
exit 1 | ||
fi |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Tests for the WPStreamImporter class. | ||
*/ | ||
class WPStreamImporterTests extends TestCase { | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
if ( ! isset( $_SERVER['SERVER_SOFTWARE'] ) || $_SERVER['SERVER_SOFTWARE'] !== 'PHP.wasm' ) { | ||
$this->markTestSkipped( 'Test only runs in Playground' ); | ||
} | ||
} | ||
|
||
public function test_import_simple_wxr() { | ||
$import = data_liberation_import( __DIR__ . '/wxr/small-export.xml' ); | ||
|
||
$this->assertTrue( $import ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
{ | ||
"$schema": "../../../blueprints/public/blueprint-schema.json", | ||
"constants": { | ||
"WP_DEBUG": true, | ||
"WP_DEBUG_DISPLAY": true, | ||
"WP_DEBUG_LOG": true | ||
}, | ||
"login": true, | ||
"steps": [ | ||
{ | ||
"step": "activatePlugin", | ||
"pluginPath": "data-liberation/plugin.php" | ||
}, | ||
{ | ||
"step": "runPHP", | ||
"code": "<?php require_once 'wordpress/wp-load.php'; $base = '/wordpress/wp-content/plugins/data-liberation/';\nrequire $base . 'vendor/autoload.php';\ntry {\n$arguments = [\n'--stderr',\n'--configuration', $base . 'phpunit.xml'\n];\n$res = (new PHPUnit\\TextUI\\Application())->run($arguments);\nif ( $res !== 0 ) {\ntrigger_error('PHPUnit failed', E_USER_ERROR);\n}\n} catch (Throwable $e) {\ntrigger_error('PHPUnit failed: ' . $e->getMessage(), E_USER_ERROR);\n};" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool idea! This will suffice for starters, but here's something if you'd like to the next level. What would it take to go from this to something more like a typical CLI command, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea. Didn't touched the CLI in this first phase. |
||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point, a dedicated WP_CLI command might make sense. It would only be a thin wrapper. The website and the unit tests would use the same underlying import library with their own dedicated logging facilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I made this way so that a user that want to use only the plugin do not need to have WP-CLI all the times.