Skip to content

Sanitize database at the end of install to prevent duplicate data#76

Merged
schlessera merged 4 commits intowp-cli:masterfrom
javorszky:issue/75
Jul 20, 2018
Merged

Sanitize database at the end of install to prevent duplicate data#76
schlessera merged 4 commits intowp-cli:masterfrom
javorszky:issue/75

Conversation

@javorszky
Copy link
Contributor

Closes #75

If the multisite constants are already in wp-config.php, there's no way to stop populate_network to add an empty site_admins option when creating a new install.

This PR adds code that, at the end, would remove all empty site_admins options. Later multisite-install calls add_site_admins which checks whether a user is a super admin, which would fail because the option exists, but is empty, so it would add another entry to the sitemeta table.

So normally the add_site_admins would just update the site_admins, and that should work. Not in this case.

update_network_option will check the existing value of the option, and if it's false, then it will use add_network_option because false means the option doesn't exist in the database.

get_network_option will return whatever is stored in the cache. If that happens to be nothing, then the first time a query is ran, it will check in the database, and if it's not in the database, it will store it in a $notoptions array, and store that in the cache.

The first time site_admins is queried, the database is practically empty, so the site_admins key ends up being part of the $notoptions variable and cache.

Subsequent query to get_network_option will hit the part where it's a notoption and return the default value - which is false.

Which means in order to get this working, we need to actually delete the empty site_admins option, because a new one will be added anyways.

WP_CLI::warning( "Multisite constants could not be written to 'wp-config.php'. You may need to add them manually:" . PHP_EOL . $ms_config );
}
} else {
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a valid docblock, as it is not attached to an entity that can receive docblocks. Please use a regular multiline comment instead.

}
} else {
/**
* Multisite constants are defined, therefore we already have an empty site_admins site meta
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS: Terminate first comment line with a period.

*
* Code based on parts of delete_network_option
*/
$rows = $wpdb->get_results( "SELECT meta_id, site_id FROM {$wpdb->sitemeta} WHERE meta_key = 'site_admins'" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this to only delete site admin options that are empty (by just adding that as an additional WHERE clause). Otherwise, someone might already have imported a pre-existing DB that contained the proper admin settings, and we are then deleting everything.

@schlessera
Copy link
Member

@javorszky Thanks for the PR, the code looks good to me.

Would you be up to writing a Behat regression test for that?

@javorszky
Copy link
Contributor Author

I can try. Not sure how I could test having object caching in place though.... bear with, I'll make an attempt.

@schlessera
Copy link
Member

You don't need to test object caching. Just test to make sure you don't leave empty site_admin options behind.

@schlessera schlessera added bug command:core-multisite-install Related to 'core multisite-install' command labels Jul 20, 2018
@schlessera schlessera added this to the 1.0.11 milestone Jul 20, 2018
@schlessera schlessera merged commit ecb6bcd into wp-cli:master Jul 20, 2018
@schlessera
Copy link
Member

Thanks for the pull-request, @javorszky !

@javorszky javorszky deleted the issue/75 branch July 23, 2018 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:core-multisite-install Related to 'core multisite-install' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants