Skip to content

Commit

Permalink
Merge pull request #172 from PRX/redirects_tables
Browse files Browse the repository at this point in the history
Redirects tables and tool to update single elements
  • Loading branch information
gtenaschuk authored Mar 19, 2024
2 parents 278cd7c + b203137 commit 406acba
Show file tree
Hide file tree
Showing 6 changed files with 399 additions and 7 deletions.
9 changes: 9 additions & 0 deletions wp-content/plugins/pri-migration-helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ Used to test the code used to replace img tags by image blocks in a single post.
`wp tw-fix post_tags_fix_duplicate [start_page_number post_per_page]`
Command to loop through all post_tags, check each term if it is duplicated in other custom taxonomies.
If it is duplicated, add posts related to the post_tag to the custom taxonomy term.
### Redirect table
`wp tw-fix repair_wp_fg_redirect_tables [page_number]`
Command to create redirects for all taxonomies and post types migrated.
`wp tw-fix repair_wp_fg_redirect_table [obect_type, object_name, page_number]`
Command to create redirects for specific taxonomies or post types migrated.
* Example: `wp tw-fix repair_wp_fg_redirect_table taxonomy category`
### Redirect table
`wp tw-fix update_url_alias_table [last_id, limit]`
Command to add all the aliases stored in url_alias Drupal table. Last ID imported will be stored to continue from there. If migration from scratch is needed the last_id parameter must be set as 1.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ function test_json_diff_option_menu() {
30
);

add_submenu_page(
'pmh-admin-test',
'Selective Fix',
'Selective Fix',
'manage_options',
'pmh-selective-fix',
'pmh_selective_fix',
30
);

add_submenu_page(
'pmh-admin-test',
'Cron',
Expand Down Expand Up @@ -186,6 +196,14 @@ function pmh_acf_fix() {
require_once PMH_ADMIN_DIR . '/parts/acf-fix.php';
}

function pmh_selective_fix() {

echo wp_sprintf( '<h2>%s</h2>', __( 'Selective Fix' ) );
echo wp_sprintf( '<p>%s</p>', __( 'Import node selectively by ID' ) );

require_once PMH_ADMIN_DIR . '/parts/selective-fix.php';
}

/**
* Add admin option page.
*
Expand Down Expand Up @@ -478,15 +496,27 @@ function pmh_post_worker_get_sample() {
/**
* Get object names.
*
* @return void
* @param array $args taxonomy, category, pagedProcess
*
* @return mixed
*/
function pmh_post_worker_run_process() {
function pmh_post_worker_run_process( $args ) {

global $wpdb;

$object_type = sanitize_text_field( $_POST['objectType'] );
$object_name = sanitize_text_field( $_POST['objectName'] );
$paged_process = (int) sanitize_text_field( $_POST['pagedProcess'] );
// If doing ajax.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {

$object_type = sanitize_text_field( $_POST['objectType'] );
$object_name = sanitize_text_field( $_POST['objectName'] );
$paged_process = (int) sanitize_text_field( $_POST['pagedProcess'] );

} else {

$object_type = $args['object_type'];
$object_name = $args['object_name'];
$paged_process = $args['paged_process'];
}

$next_paged_process = $paged_process + 1;

Expand Down Expand Up @@ -634,7 +664,15 @@ function pmh_post_worker_run_process() {
'next_paged_process' => $next_paged_process,
);

wp_send_json( $response );
// Is doing ajax.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {

wp_send_json( $response );

} else {

return $response;
}
}
add_action( 'wp_ajax_pmh_post_worker_run_process', 'pmh_post_worker_run_process' );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
if ( isset( $_POST['pmh-selective-fix'] ) && '1' === $_POST['pmh-selective-fix'] ) {

// Save content types default.
$set_selective_fix_content_types = array();

// Save post node ids.
if ( isset( $_POST['pmh-post-fix-ids'] ) ) {

update_option( 'tw_get_nodes_story_target_ids', sanitize_textarea_field( $_POST['pmh-post-fix-ids'] ) );

if ( ! empty( $_POST['pmh-post-fix-ids'] ) ) {

$set_selective_fix_content_types[] = 'story';
}
}

// Save episode node ids.
if ( isset( $_POST['pmh-episode-fix-ids'] ) ) {

update_option( 'tw_get_nodes_episode_target_ids', sanitize_textarea_field( $_POST['pmh-episode-fix-ids'] ) );

if ( ! empty( $_POST['pmh-episode-fix-ids'] ) ) {

$set_selective_fix_content_types[] = 'episode';
}
}

// Save content types.
update_option( 'tw_selective_fix_content_types', $set_selective_fix_content_types );
}

// Get node ids.
$post_node_ids = get_option( 'tw_get_nodes_story_target_ids' );
$episode_node_ids = get_option( 'tw_get_nodes_episode_target_ids' );

// Current selected content types.
$selective_fix_content_types = get_option( 'tw_selective_fix_content_types', array() );
$selective_fix_content_types_string = $selective_fix_content_types ? implode( ', ', $selective_fix_content_types ) : 'None';
?>
<p>This tool will scope the import process to selected IDs only.</p>
<p>Current selected content types: <?php echo $selective_fix_content_types_string; ?></p>

<form method="POST">
<table id="pmh-episode-fix" class="form-table">
<tbody>
<tr>
<th scope="row"><label for="pmh-post-fix-ids"><?php esc_html_e( 'Story nid', 'pmh' ); ?></label></th>
<td>
<textarea name="pmh-post-fix-ids" id="pmh-post-fix-ids" cols="80" rows="5"><?php echo $post_node_ids; ?></textarea>
<p><i>Separated by comma (,). Leave empty to process all.</i></p>
</td>
</tr>
<tr>
<th scope="row"><label for="pmh-episode-fix-ids"><?php esc_html_e( 'Episode nid', 'pmh' ); ?></label></th>
<td>
<textarea name="pmh-episode-fix-ids" id="pmh-episode-fix-ids" cols="80" rows="5"><?php echo $episode_node_ids; ?></textarea>
<p><i>Separated by comma (,). Leave empty to process all.</i></p>
</td>
</tr>
</tbody>
</table>
<p>
<input type="hidden" name="pmh-selective-fix" value="1">
<input type="submit" class="button button-primary" value="Save Settings">
</p>
</form>
4 changes: 4 additions & 0 deletions wp-content/plugins/pri-migration-helper/cli/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
* Both are optional. If not provided, it will start from the beginning by 100 per process.
* 8. _image_captions [wp_image_id]
* If targetting a single image, use this command instead.
* 9. repair_wp_fg_redirect_tables [page_number]
* 10. repair_wp_fg_redirect_table [obect_type, object_name, page_number]
* Example: repair_wp_fg_redirect_table taxonomy category
* 11. update_url_alias_table [last_id, limit]
*/

// Add 'tw-media-fix-all' command to wp-cli.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,5 +1049,221 @@ public function _image_captions( $args ) {
}
}
}

/**
* Repair the redirect table for selected object and type.
*
* @param array $args
* $args[1]: page number to start from
*
* @return void
*/
public function repair_wp_fg_redirect_tables( $args ) {
$paged_process = isset( $args[1] ) ? (int) $args[1] : 0;
$object_types = array( 'taxonomy', 'post-type' );
$object_names = array( 'taxonomy' => array( 'category', 'post_format', 'contributor', 'city', 'continent', 'country', 'province_or_state', 'region', 'license', 'resource_development', 'story_format', 'program', 'media_tag', 'post_tag', 'person', 'social_tags' ), 'post-type' => array( 'page', 'episode', 'post' ) );
WP_CLI::log( '-------------------------------' );
WP_CLI::log( 'repair_wp_fg_redirect_tables started.' );
foreach ( $object_types as $object_type ) {
foreach ( $object_names[ $object_type ] as $object_name ) {
WP_CLI::log( "Start Adding '{$object_type} - {$object_name}' redirects." );
$this->repair_wp_fg_redirect_table( array(
$object_type,
$object_name,
$paged_process,
) );
WP_CLI::log( "Finish Adding '{$object_type} - {$object_name}' redirects." );
}
}
WP_CLI::log( 'repair_wp_fg_redirect_tables finished.' );
WP_CLI::log( '-------------------------------' );
}

/**
* Repair the redirect table for selected object and type.
*
* @param array $args
* $args[1]: taxonomy | post-type
* $args[2]: slug
* $args[3]: paging number to start from
*
* @return void
*/
public function repair_wp_fg_redirect_table( $args ) {

$object_type = $args[0];
$object_name = $args[1];
$paged_process = isset( $args[2] ) ? (int) $args[2] : 0;

// If object type or name is not set.
if ( ! $object_type || ! $object_name ) {
WP_CLI::error( 'Object type or name is not set.' );
}

// Set variable.
$worker_args = array(
'object_type' => $object_type,
'object_name' => $object_name,
'paged_process' => $paged_process,
);

// Run worker.
$response = pmh_post_worker_run_process( $worker_args );

// Get log.
$log = $response['log'];

// Print log.
WP_CLI::log( $log );

// Next?
$next = $response['next_paged_process'];

// Loop if next is not false.
if ( $next ) {

// Set above variable to $_POST.
$_POST['objectType'] = $object_type;
$_POST['objectName'] = $object_name;
$_POST['pagedProcess'] = $paged_process + 1;

$this->repair_wp_fg_redirect_table( array(
$object_type,
$object_name,
$paged_process + 1,
) );
} else {

// Print success. 'Redirect table for objectName updated.'
WP_CLI::success( "Redirect table for {$object_name} updated." );
}
}

/**
* Get from Drupal url_alias table and update migrated WordPress redirect table.
*
* The option name for the last id is 'tw_fix_url_alias_last_id'.
*
* Be sure to already have table wp_migrated_legacy_url_alias created.
*
* @param array $args
* $args[1] : Start from id. Use 0 or empty to start from the last id in the option. Use 1 to start from the first id.
* $args[2] : Limit.
*
* @return void
*/
public function update_url_alias_table( $args ) {

// Get the last id.
$last_id = isset( $args[0] ) ? (int) $args[0] : false;

// Get the limit.
$limit = isset( $args[1] ) ? (int) $args[1] : 100;

// If last id is not set. Get the last id from the option.
if ( ! $last_id ) {
$last_id = (int) get_option( 'tw_fix_url_alias_last_id', 0 );
}

// Simulate running importer.
global $fgd2wpp;

ob_start();
$fgd2wpp->importer();
ob_get_clean();

// Connect.
$fgd2wpp->drupal_connect();

// DB Information.
$drupal_table_name = 'url_alias';
$wp_table_name = 'wp_migrated_legacy_url_alias';

// Get total row in url_alias table in Drupal starting from the last id.
$query_str = "SELECT COUNT(*) as qty FROM {$drupal_table_name} WHERE pid > {$last_id}";

$total_rows = $fgd2wpp->drupal_query( $query_str, 'total' );

// If no rows found, Exit.
if (
isset( $total_rows[0]['qty'] )
&&
0 === $total_rows[0]['qty']
) {

WP_CLI::log( "No rows found." );

return;
}

$total_row_count = (int) $total_rows[0]['qty'];

// Make WP_CLI progress bar.
$progress = \WP_CLI\Utils\make_progress_bar( 'Updating url_alias table', $total_row_count );

// Get row from url_alias table by limit and last id.
$query_str = "SELECT * FROM {$drupal_table_name} WHERE pid > {$last_id} LIMIT {$limit}";

$rows = $fgd2wpp->drupal_query( $query_str );

// Check if each row exists in the wp_migrated_legacy_url_alias table in WordPress.
// Use the pid as the unique key. Use global $wpdb.
global $wpdb;

// Failed row ids.
$failed_row_ids = array();

// Loop through each row.
do {
foreach ( $rows as $row ) {

// Get the pid.
$pid = $row['pid'];

// Check if the pid exists in the wp_migrated_legacy_url_alias table.
$exists = (int) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM {$wp_table_name} WHERE pid = %d LIMIT 1", $pid ) );

// If the pid does not exist in the wp_migrated_legacy_url_alias table.
if ( ! $exists ) {

// Insert the row to the wp_migrated_legacy_url_alias table.
$insert = $wpdb->insert( $wp_table_name, $row );

// Add failed row id.
if ( ! $insert ) {
$failed_row_ids[] = $pid;
}
}

// Update the last id.
update_option( 'tw_fix_url_alias_last_id', $pid );

// Tick the progress bar.
$progress->tick();
}

// Get the next batch of rows.
$query_str = "SELECT * FROM {$drupal_table_name} WHERE pid > {$pid} LIMIT {$limit}";
$rows = $fgd2wpp->drupal_query( $query_str );

} while ( $rows );

$progress->finish();

// If failed row ids is not empty.
if ( $failed_row_ids ) {

// Log failed row ids.
WP_CLI::log( '' );
WP_CLI::log( 'Failed row ids:' );
WP_CLI::log( implode( ', ', $failed_row_ids ) );
WP_CLI::log( '' );

} else {

// Log success.
WP_CLI::success( 'All rows are updated.' );
}
}
}

Loading

0 comments on commit 406acba

Please sign in to comment.