Skip to content

Commit 23331b3

Browse files
committed
Reconstruct WordPress tables using "wp_get_db_schema" function
1 parent 7727cfa commit 23331b3

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

Diff for: tests/WP_SQLite_Information_Schema_Reconstructor_Tests.php

+68
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ public static function setUpBeforeClass(): void {
2929
$GLOBALS['wpdb']->suppress_errors = false;
3030
$GLOBALS['wpdb']->show_errors = true;
3131
}
32+
33+
// Mock symols that are used for WordPress table reconstruction.
34+
if ( ! defined( 'ABSPATH' ) ) {
35+
define( 'ABSPATH', __DIR__ );
36+
}
37+
if ( ! function_exists( 'wp_installing' ) ) {
38+
function wp_installing() {
39+
return false;
40+
}
41+
}
42+
if ( ! function_exists( 'wp_get_db_schema' ) ) {
43+
function wp_get_db_schema() {
44+
// Output from "wp_get_db_schema" as of WordPress 6.8.0.
45+
// See: https://github.com/WordPress/wordpress-develop/blob/6.8.0/src/wp-admin/includes/schema.php#L36
46+
return "CREATE TABLE wp_users ( ID bigint(20) unsigned NOT NULL auto_increment, user_login varchar(60) NOT NULL default '', user_pass varchar(255) NOT NULL default '', user_nicename varchar(50) NOT NULL default '', user_email varchar(100) NOT NULL default '', user_url varchar(100) NOT NULL default '', user_registered datetime NOT NULL default '0000-00-00 00:00:00', user_activation_key varchar(255) NOT NULL default '', user_status int(11) NOT NULL default '0', display_name varchar(250) NOT NULL default '', PRIMARY KEY (ID), KEY user_login_key (user_login), KEY user_nicename (user_nicename), KEY user_email (user_email) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_usermeta ( umeta_id bigint(20) unsigned NOT NULL auto_increment, user_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (umeta_id), KEY user_id (user_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_termmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY term_id (term_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_terms ( term_id bigint(20) unsigned NOT NULL auto_increment, name varchar(200) NOT NULL default '', slug varchar(200) NOT NULL default '', term_group bigint(10) NOT NULL default 0, PRIMARY KEY (term_id), KEY slug (slug(191)), KEY name (name(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_taxonomy ( term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default 0, taxonomy varchar(32) NOT NULL default '', description longtext NOT NULL, parent bigint(20) unsigned NOT NULL default 0, count bigint(20) NOT NULL default 0, PRIMARY KEY (term_taxonomy_id), UNIQUE KEY term_id_taxonomy (term_id,taxonomy), KEY taxonomy (taxonomy) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_relationships ( object_id bigint(20) unsigned NOT NULL default 0, term_taxonomy_id bigint(20) unsigned NOT NULL default 0, term_order int(11) NOT NULL default 0, PRIMARY KEY (object_id,term_taxonomy_id), KEY term_taxonomy_id (term_taxonomy_id) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_commentmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, comment_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY comment_id (comment_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_comments ( comment_ID bigint(20) unsigned NOT NULL auto_increment, comment_post_ID bigint(20) unsigned NOT NULL default '0', comment_author tinytext NOT NULL, comment_author_email varchar(100) NOT NULL default '', comment_author_url varchar(200) NOT NULL default '', comment_author_IP varchar(100) NOT NULL default '', comment_date datetime NOT NULL default '0000-00-00 00:00:00', comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', comment_content text NOT NULL, comment_karma int(11) NOT NULL default '0', comment_approved varchar(20) NOT NULL default '1', comment_agent varchar(255) NOT NULL default '', comment_type varchar(20) NOT NULL default 'comment', comment_parent bigint(20) unsigned NOT NULL default '0', user_id bigint(20) unsigned NOT NULL default '0', PRIMARY KEY (comment_ID), KEY comment_post_ID (comment_post_ID), KEY comment_approved_date_gmt (comment_approved,comment_date_gmt), KEY comment_date_gmt (comment_date_gmt), KEY comment_parent (comment_parent), KEY comment_author_email (comment_author_email(10)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_links ( link_id bigint(20) unsigned NOT NULL auto_increment, link_url varchar(255) NOT NULL default '', link_name varchar(255) NOT NULL default '', link_image varchar(255) NOT NULL default '', link_target varchar(25) NOT NULL default '', link_description varchar(255) NOT NULL default '', link_visible varchar(20) NOT NULL default 'Y', link_owner bigint(20) unsigned NOT NULL default '1', link_rating int(11) NOT NULL default '0', link_updated datetime NOT NULL default '0000-00-00 00:00:00', link_rel varchar(255) NOT NULL default '', link_notes mediumtext NOT NULL, link_rss varchar(255) NOT NULL default '', PRIMARY KEY (link_id), KEY link_visible (link_visible) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_options ( option_id bigint(20) unsigned NOT NULL auto_increment, option_name varchar(191) NOT NULL default '', option_value longtext NOT NULL, autoload varchar(20) NOT NULL default 'yes', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), KEY autoload (autoload) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_postmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, post_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY post_id (post_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_posts ( ID bigint(20) unsigned NOT NULL auto_increment, post_author bigint(20) unsigned NOT NULL default '0', post_date datetime NOT NULL default '0000-00-00 00:00:00', post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', post_content longtext NOT NULL, post_title text NOT NULL, post_excerpt text NOT NULL, post_status varchar(20) NOT NULL default 'publish', comment_status varchar(20) NOT NULL default 'open', ping_status varchar(20) NOT NULL default 'open', post_password varchar(255) NOT NULL default '', post_name varchar(200) NOT NULL default '', to_ping text NOT NULL, pinged text NOT NULL, post_modified datetime NOT NULL default '0000-00-00 00:00:00', post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00', post_content_filtered longtext NOT NULL, post_parent bigint(20) unsigned NOT NULL default '0', guid varchar(255) NOT NULL default '', menu_order int(11) NOT NULL default '0', post_type varchar(20) NOT NULL default 'post', post_mime_type varchar(100) NOT NULL default '', comment_count bigint(20) NOT NULL default '0', PRIMARY KEY (ID), KEY post_name (post_name(191)), KEY type_status_date (post_type,post_status,post_date,ID), KEY post_parent (post_parent), KEY post_author (post_author) ) DEFAULT CHARACTER SET utf8mb4;";
47+
}
48+
}
3249
}
3350

3451
// Before each test, we create a new database
@@ -102,6 +119,57 @@ public function testReconstructInformationSchemaTable(): void {
102119
);
103120
}
104121

122+
public function testReconstructInformationSchemaTableWithWpTables(): void {
123+
// Create a WP table with any columns.
124+
$this->engine->get_pdo()->exec( 'CREATE TABLE wp_posts ( id INTEGER )' );
125+
126+
// Reconstruct the information schema.
127+
$this->reconstructor->ensure_correct_information_schema();
128+
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "wp_posts"' );
129+
$this->assertEquals( 1, count( $result ) );
130+
131+
// The reconstructed schema should correspond to the original WP table definition.
132+
$result = $this->assertQuery( 'SHOW CREATE TABLE wp_posts' );
133+
$this->assertSame(
134+
implode(
135+
"\n",
136+
array(
137+
'CREATE TABLE `wp_posts` (',
138+
' `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,',
139+
" `post_author` bigint(20) unsigned NOT NULL DEFAULT '0',",
140+
" `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
141+
" `post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
142+
' `post_content` longtext NOT NULL,',
143+
' `post_title` text NOT NULL,',
144+
' `post_excerpt` text NOT NULL,',
145+
" `post_status` varchar(20) NOT NULL DEFAULT 'publish',",
146+
" `comment_status` varchar(20) NOT NULL DEFAULT 'open',",
147+
" `ping_status` varchar(20) NOT NULL DEFAULT 'open',",
148+
" `post_password` varchar(255) NOT NULL DEFAULT '',",
149+
" `post_name` varchar(200) NOT NULL DEFAULT '',",
150+
' `to_ping` text NOT NULL,',
151+
' `pinged` text NOT NULL,',
152+
" `post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
153+
" `post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
154+
' `post_content_filtered` longtext NOT NULL,',
155+
" `post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',",
156+
" `guid` varchar(255) NOT NULL DEFAULT '',",
157+
" `menu_order` int(11) NOT NULL DEFAULT '0',",
158+
" `post_type` varchar(20) NOT NULL DEFAULT 'post',",
159+
" `post_mime_type` varchar(100) NOT NULL DEFAULT '',",
160+
" `comment_count` bigint(20) NOT NULL DEFAULT '0',",
161+
' PRIMARY KEY (`ID`),',
162+
' KEY `post_name` (`post_name`(191)),',
163+
' KEY `type_status_date` (`post_type`, `post_status`, `post_date`, `ID`),',
164+
' KEY `post_parent` (`post_parent`),',
165+
' KEY `post_author` (`post_author`)',
166+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
167+
)
168+
),
169+
$result[0]->{'Create Table'}
170+
);
171+
}
172+
105173
private function assertQuery( $sql ) {
106174
$retval = $this->engine->query( $sql );
107175
$this->assertNotFalse( $retval );

Diff for: wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php

+47-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,38 @@ public function ensure_correct_information_schema(): void {
5454
$tables = $this->get_existing_table_names();
5555
$information_schema_tables = $this->get_information_schema_table_names();
5656

57+
// In WordPress, use "wp_get_db_schema()" to reconstruct WordPress tables.
58+
$wp_tables = array();
59+
if ( defined( 'ABSPATH' ) ) {
60+
if ( wp_installing() ) {
61+
// Avoid interfering with WordPress installation.
62+
return;
63+
}
64+
if ( file_exists( ABSPATH . 'wp-admin/includes/schema.php' ) ) {
65+
require_once ABSPATH . 'wp-admin/includes/schema.php';
66+
}
67+
if ( function_exists( 'wp_get_db_schema' ) ) {
68+
$schema = wp_get_db_schema();
69+
$parts = preg_split( '/(CREATE\s+TABLE)/', $schema, -1, PREG_SPLIT_NO_EMPTY );
70+
foreach ( $parts as $part ) {
71+
$name = $this->unquote_mysql_identifier(
72+
preg_split( '/\s+/', $part, 2, PREG_SPLIT_NO_EMPTY )[0]
73+
);
74+
$wp_tables[ $name ] = 'CREATE TABLE' . $part;
75+
}
76+
}
77+
}
78+
5779
// Reconstruct information schema records for tables that don't have them.
5880
foreach ( $tables as $table ) {
5981
if ( ! in_array( $table, $information_schema_tables, true ) ) {
60-
$sql = $this->generate_create_table_statement( $table );
82+
if ( isset( $wp_tables[ $table ] ) ) {
83+
// WordPress table.
84+
$sql = $wp_tables[ $table ];
85+
} else {
86+
// Non-WordPress table.
87+
$sql = $this->generate_create_table_statement( $table );
88+
}
6189
$ast = $this->driver->parse_query( $sql );
6290
$this->information_schema_builder->record_create_table( $ast );
6391
}
@@ -378,4 +406,22 @@ private function get_mysql_data_type( string $column_type ): string {
378406
private function quote_sqlite_identifier( string $unquoted_identifier ): string {
379407
return '`' . str_replace( '`', '``', $unquoted_identifier ) . '`';
380408
}
409+
410+
/**
411+
* Unquote a quoted MySQL identifier.
412+
*
413+
* Remove bounding quotes and replace escaped quotes with their values.
414+
*
415+
* @param string $quoted_identifier The quoted identifier value.
416+
* @return string The unquoted identifier value.
417+
*/
418+
private function unquote_mysql_identifier( string $quoted_identifier ): string {
419+
$first_byte = $quoted_identifier[0] ?? null;
420+
if ( '"' === $first_byte || '`' === $first_byte ) {
421+
$unquoted = substr( $quoted_identifier, 1, -1 );
422+
} else {
423+
$unquoted = $quoted_identifier;
424+
}
425+
return str_replace( $first_byte . $first_byte, $first_byte, $unquoted );
426+
}
381427
}

0 commit comments

Comments
 (0)