Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions features/core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Feature: Manage WordPress installation

When I try `wp core is-installed`
Then the return code should be 1
And STDERR should match /WordPress is not installed[\s\S]*Missing tables:.*\b[^\s_]+_/

When I try `wp core is-installed --network`
Then the return code should be 1
Expand Down
86 changes: 82 additions & 4 deletions src/Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Composer\Semver\Comparator;
use WP_CLI\Extractor;
use WP_CLI\Iterators\Table as TableIterator;
use WP_CLI\Iterators\Table;
use WP_CLI\Utils;
use WP_CLI\Formatter;
use WP_CLI\WpOrgApi;
Expand Down Expand Up @@ -393,11 +393,89 @@
* @param array{network?: bool} $assoc_args Associative arguments.
*/
public function is_installed( $args, $assoc_args ) {
if ( is_blog_installed() && ( ! Utils\get_flag_value( $assoc_args, 'network' ) || is_multisite() ) ) {
try {
if ( ! is_blog_installed() ) {
WP_CLI::halt( 1 );
}

if ( Utils\get_flag_value( $assoc_args, 'network' ) && ! is_multisite() ) {
WP_CLI::halt( 1 );
}

global $wpdb;

// Get all tables including multisite if needed
$tables = $wpdb->tables( 'all', true );

Check warning on line 408 in src/Core_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space
$missing_tables = [];

// Check if we're using SQLite - check multiple possible indicators
$is_sqlite = (
( isset( $wpdb->dbdriver ) && 'sqlite' === $wpdb->dbdriver ) ||
( defined( 'DB_DRIVER' ) && 'sqlite' === DB_DRIVER ) ||
( defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) ||
( is_string( DB_HOST ) && ( strpos( DB_HOST, 'sqlite' ) !== false ) )

Check failure on line 416 in src/Core_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Constant DB_HOST not found.
);

if ( defined( 'WP_CLI_TEST' ) && WP_CLI_TEST ) {
WP_CLI::debug( 'Database type: ' . ( $is_sqlite ? 'SQLite' : 'MySQL/MariaDB' ) );
}

foreach ( $tables as $table ) {
$table_name = $wpdb->prefix . $table;

Check failure on line 425 in src/Core_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Whitespace found at end of line
try {
try {
if ( $is_sqlite ) {
// SQLite uses a simpler table existence check
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT name FROM sqlite_master WHERE type='table' AND name=%s",
$table_name
)
);
} else {
// Standard MySQL/MariaDB check
$result = $wpdb->get_var(
$wpdb->prepare(
'SHOW TABLES LIKE %s',
$table_name
)
);
}
} catch ( Exception $e ) {
// If there's an error, assume the table doesn't exist
$result = null;
}

if ( defined( 'WP_CLI_TEST' ) && WP_CLI_TEST ) {
WP_CLI::debug( sprintf( 'Table %s: %s', $table_name, $result ? 'exists' : 'missing' ) );
}

if ( $result !== $table_name ) {
$missing_tables[] = $table_name;
}
} catch ( Exception $e ) {
WP_CLI::debug( sprintf( 'Error checking table %s: %s', $table_name, $e->getMessage() ) );
$missing_tables[] = $table_name;
}
}

if ( ! empty( $missing_tables ) ) {
WP_CLI::error(
sprintf(
'WordPress is not installed. Missing tables: %s',
implode( ', ', $missing_tables )
),
1
);
}

WP_CLI::halt( 0 );
}

WP_CLI::halt( 1 );
} catch ( Exception $e ) {
WP_CLI::debug( 'Error in is_installed: ' . $e->getMessage() );
WP_CLI::halt( 1 );
}
}

/**
Expand Down Expand Up @@ -1293,7 +1371,7 @@
'archived' => 0,
],
];
$it = new TableIterator( $iterator_args );

Check failure on line 1374 in src/Core_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Instantiated class TableIterator not found.
$success = 0;
$total = 0;
$site_ids = [];
Expand All @@ -1301,7 +1379,7 @@
/**
* @var object{site_id: int, domain: string, path: string} $blog
*/
foreach ( $it as $blog ) {

Check failure on line 1382 in src/Core_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Iterating over an object of an unknown class TableIterator.
++$total;
$site_ids[] = $blog->site_id;
$url = $blog->domain . $blog->path;
Expand Down
Binary file added wp-cli.phar
Binary file not shown.
Loading