-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze_rows.php
53 lines (39 loc) · 1.56 KB
/
analyze_rows.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
if ( count( $args ) < 2 ) {
echo "Usage: wp eval-file analyze_rows.php <site_id> <output_file>\n";
exit( 1 );
}
$site_id = intval( $args[0] );
$output_file = $args[1];
global $wpdb;
// Switch to the specified site
switch_to_blog( $site_id );
echo "Analyzing posts for site $site_id...\n";
// Fetch all post IDs
$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts}" );
// Prepare output data
$output_data = [];
foreach ( $post_ids as $post_id ) {
// Fetch post content in utf8
$wpdb->query( "SET NAMES utf8" );
$utf8_content = $wpdb->get_var( $wpdb->prepare( "SELECT post_content FROM {$wpdb->posts} WHERE ID = %d", $post_id ) );
// Fetch post content in utf8mb4
$wpdb->query( "SET NAMES utf8mb4" );
$utf8mb4_content = $wpdb->get_var( $wpdb->prepare( "SELECT post_content FROM {$wpdb->posts} WHERE ID = %d", $post_id ) );
// Compare content
if ( $utf8_content !== $utf8mb4_content ) {
$date_modified = $wpdb->get_var( $wpdb->prepare( "SELECT post_modified FROM {$wpdb->posts} WHERE ID = %d", $post_id ) );
// Add to output data
$output_data[] = [
'ID' => $post_id,
'post_content_bad' => $utf8_content,
'post_content' => $utf8mb4_content,
'date_modified' => $date_modified,
];
}
}
// Write output to JSON file
file_put_contents( $output_file, json_encode( $output_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) );
echo "Exported mismatched posts to $output_file\n";
// Restore original site
restore_current_blog();