Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
Merge branch '2.0.0' into implement_41
Browse files Browse the repository at this point in the history
  • Loading branch information
derpixler committed Jan 21, 2016
2 parents c2e2249 + aba3959 commit dc325c7
Show file tree
Hide file tree
Showing 28 changed files with 1,100 additions and 200 deletions.
101 changes: 101 additions & 0 deletions bin/validate-xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php # -*- coding: utf-8 -*-

/**
* Quick »5-minutes-script« to validate all *.xml files in a given directory
*
* Usage: php validate-xml.php path/to/directory
*/

function print_usage() {

$file = basename( __FILE__ );
$str = <<<STR
Validate-xml
checks each xml file in a directory for validity.
Usage
php {$file} <DIRECTORY>
STR;

echo $str;
}

function start_timer() {

$GLOBALS[ 'start_time' ] = microtime( TRUE );
}
function stop_timer() {

return microtime( TRUE ) - $GLOBALS[ 'start_time' ];
}

function format_xml_error( libXMLError $error ) {

switch ( $error->level ) {
case LIBXML_ERR_WARNING :
$level = 'WARNING';
break;

case LIBXML_ERR_ERROR :
$level = 'ERROR';
break;

case LIBXML_ERR_FATAL :
$level = 'FATAL';
break;

default:
$level = 'NOTICE';
break;
}

$msg = "{$level}({$error->code}) at line {$error->line}:{$error->column}: {$error->message}";

return $msg;
}

/**
* @link https://secure.php.net/manual/de/function.memory-get-usage.php#96280
*
* @param $size
*
* @return string
*/
function convert_memory_size( $size )
{
$unit = [ 'b', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' ];
return round( $size / pow( 1024, ( $i = floor ( log( $size, 1024 ) ) ) ), 2 ).' '.$unit[ $i ];
}

if ( empty( $argv[ 1 ] ) ) {
print_usage();
exit;
}
start_timer();
$dir = realpath( $argv[ 1 ] );
$iterator = new DirectoryIterator( $dir );
libxml_use_internal_errors( TRUE );

foreach ( $iterator as $file ) {
if ( ! $file->isFile() )
continue;

if ( '.xml' !== substr( $file->getBasename(), -4 ) )
continue;

$document = simplexml_load_file( $file->getPathname() );
if ( is_a( $document, 'SimpleXMLElement' ) ) {
echo "File {$file->getFilename()} is valid XML.\n";
} else {
echo "File {$file->getFilename()} is not valid:\n";
foreach ( libxml_get_errors() as $error ) {
echo "\t" . format_xml_error( $error );
}
}
}

$runtime = number_format( stop_timer(), 3 );
$memory_usage = convert_memory_size( memory_get_peak_usage( TRUE ) );
echo "Finished check in {$runtime} s. Memory Usage {$memory_usage}\n";
return 0;
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"autoload": {
"psr-4": {
"W2M\\Import\\": "inc/Import/"
"W2M\\": "inc/"
}
},
"autoload-dev": {
Expand Down
119 changes: 102 additions & 17 deletions inc/Cli/WpCliW2MCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,156 @@
namespace W2M\Cli;

use
W2M\System,
W2M\Controller,
W2M\Import,
WP_CLI,
WP_CLI_Command,
WP_Error;
WP_Error,
Monolog;

/**
* Manages migration from WPML to MultilingualPress
*
* @package W2M\Cli
*/
class WpCliW2MCommand extends \WP_CLI_Command {

/**
* Imports a single extended WXR file to a blog.
* Imports a single extended WXR file to a blog. Use the --url parameter to specify the home URL
* of the site you want to import the language to.
*
* Example: If you exported all spanish content (es_ES) to ~/my-site-es_ES.xml and want to import it
* to my-site.es use `wp w2m import ~/my-site-es_ES.xml --url=my-site.es
*
* ## Options
*
* <FILE>
* : Path to the WXR file
*
* <BLOG_ID>
* : Id of the blog to import
*
* @synopsis <FILE> <BLOG_ID>
* @synopsis <FILE> --url=<url> [--no_confirm]
*
* @param array $args
* @param array $assoc_args
*/
public function import( Array $args, Array $assoc_args ) {

exit(
$this->handle_warning( new WP_Error( 'msg', 'Not implemented yet' ) )
if ( ! isset( $args[ 0 ] ) ) {
$this->handle_error( new WP_Error( 'parameter', 'Missing parameter <FILE>' ) );
exit;
}

$import_file = realpath( $args[ 0 ] );
if ( ! is_file( $import_file ) || ! is_readable( $import_file ) ) {
$this->handle_error( new WP_Error( 'parameter', 'Import file does not exist or is not readable.' ) );
exit;
}

$env = new System\ImportEnvironment;
if ( ! is_multisite() ) {
$this->handle_error( new WP_Error( 'environment', 'This is not a multisite setup' ) );
exit;
}
if ( ! $env->mlp_is_active() ) {
$this->handle_error( new WP_Error( 'environment', 'MultilingualPress is not active' ) );
exit;
}

$blog_id = get_current_blog_id();
$locale = $env->mlp_blog_language( $blog_id );

if ( ! isset( $assoc_args[ 'no_confirm' ] ) ) {
$ays = readline( "Start import to blog {$blog_id}[{$locale}]? [yes]" );
if ( 'yes' !== strtolower( $ays ) ) {
WP_CLI::line( 'Aborting' );
exit;
}
}

$log_dir = WP_CONTENT_DIR.'/log';
if ( !is_dir( $log_dir ) ) {
wp_mkdir_p( $log_dir );
}

$logger = new Monolog\Logger( 'w2m-import' );
$log_setup = new System\LoggerSetup( $logger, $log_dir, 'w2m-import.log' );
$log_controller = new Controller\TmpLogController( $logger );

$log_setup->setup_handler();
$log_controller->register_log_recorder();

WP_CLI::line( 'Start import ...' );

//Todo: use DI-Container ASAP

$import_id_mapper = new Import\Data\ImportListeningTypeIdMapper;
$ancestor_mapper = new Import\Data\ImportListeningMTAncestorList;
$mapper_controller = new Controller\DataIdObserverProvider(
$import_id_mapper,
$ancestor_mapper
);
}
$mapper_controller->register_id_observer();

$user_iterator = new Import\Iterator\UserIterator(
new Import\Iterator\SimpleXmlItemWrapper(
new Import\Iterator\XmlNodeIterator(
$import_file,
'wp:author'
)
),
new Import\Service\WpUserParser
);
$user_processor = new Import\Service\UserProcessor(
$user_iterator,
new Import\Service\WpUserImporter( $import_id_mapper )
);
$user_processor->process_elements();
}

private function die_on_missing_dependency() {
$msg = 'A $GLOBAL variable is not in a state it supposed to be.'; //surprise

$msg = 'A $GLOBAL variable is not in a state it supposed to be.'; //surprise
exit(
$this->handle_error(
new WP_Error(
1,
$msg
)
$this->handle_error(
new WP_Error(
1,
$msg
)
)
);
}

/**
* @param WP_Error $error
*
* @return int
*/
private function handle_error( WP_Error $error ) {

foreach ( $error->get_error_messages() as $msg )
foreach ( $error->get_error_messages() as $msg ) {
WP_CLI::error( $msg, FALSE );
}

return 1;
}

/**
* @param WP_Error $error
*
* @return int
*/
private function handle_warning( WP_Error $error ) {

foreach ( $error->get_error_messages() as $msg )
foreach ( $error->get_error_messages() as $msg ) {
WP_CLI::warning( $msg );
}

return 1;
}

/**
* @param $msg
*
* @return int
*/
private function handle_success( $msg ) {
Expand Down
2 changes: 1 addition & 1 deletion inc/Controller/DataIdObserverProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
/**
* Provide action handler
*/
public function set_up() {
public function register_id_observer() {

add_action( 'w2m_import_set_comment_id_id', [ $this->id_mapper, 'record_comment' ] );
add_action( 'w2m_import_set_post_id', [ $this->id_mapper, 'record_post' ] );
Expand Down
2 changes: 1 addition & 1 deletion inc/Controller/TmpLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct( Monolog\Logger $logger ) {
/**
* setup and assign all log recorder
*/
public function setup_logger() {
public function register_log_recorder() {

$parser_error_recorder = new Recorder\DefaultElementParserError( $this->logger );
add_action( 'w2m_import_parse_user_error', [ $parser_error_recorder, 'record' ] );
Expand Down
4 changes: 2 additions & 2 deletions inc/Import/Iterator/SimpleXmlItemWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public function current() {
if ( ! is_a( $document, $this->simple_xml_class ) ) {
$error = $this->wp_factory->wp_error( 'xml', "Invalid XML" );
$error->add_data(
'xml',
[
'data' => [
'xml_string' => $xml,
'xml_errors' => libxml_get_errors()
]
]
],
'xml'
);
libxml_clear_errors();
$this->propagate_invalid_xml_error( $error );
Expand Down
8 changes: 4 additions & 4 deletions inc/Import/Module/MlpTranslationConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ private function trigger_missing_blog_error(
"Cannot find blog for locale {$locale_relation->locale()}"
);
$error->add_data(
'locale',
array (
'data' => array(
'locale_relation' => $locale_relation,
'import_post' => $import_post
)
)
),
'locale'
);
$this->propagate_error( $error );
}
Expand All @@ -205,13 +205,13 @@ private function trigger_missing_remote_post(
"Cannot find remote post for locale {$locale_relation->locale()}"
);
$error->add_data(
'post',
array (
'data' => array(
'locale_relation' => $locale_relation,
'import_post' => $import_post
)
)
),
'post'
);
$this->propagate_error( $error );
}
Expand Down
Loading

0 comments on commit dc325c7

Please sign in to comment.