Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
// Pre-filter now.
$tables = $this->names_filter($pre_filter, $tables);
// When tables are part of the query, add tables that are present in civicrm tables.
if (empty($views_only)) {
$civicrm_tables = array_keys(CRM_Core_DAO_AllCoreTables::tables());
foreach ($civicrm_tables as $table) {
if (!in_array($table, $tables)) {
$tables[] = $table;
}
}
}
// Filter by `$args` wildcards.
if ($args) {
$tables = $this->names_filter($args, $tables);
}
// Render output.
if ('csv' === $format) {
WP_CLI::log(implode(',', $tables));
}
elseif ('json' === $format) {
$json = json_encode($tables);
if (JSON_ERROR_NONE !== json_last_error()) {
WP_CLI::error(sprintf(WP_CLI::colorize('Failed to encode JSON: %Y%s.%n'), json_last_error_msg()));
}
echo $json . "\n";
}
else {
foreach ($tables as $table) {
WP_CLI::log($table);
}
}
}
// ----------------------------------------------------------------------------
// Private methods.
// ----------------------------------------------------------------------------
/**
* Gets the instance of wpdb with CiviCRM credentials.
*
* @since 5.69
*
* @return object $cividb The instance of wpdb with CiviCRM credentials.
*/
private function cividb_get() {
// Return instance if we have it.
static $cividb;
if (isset($cividb)) {
return $cividb;
}
// Let's use an instance of wpdb with CiviCRM credentials.
$dsn = $this->cividb_dsn_get();
$cividb = new wpdb($dsn['username'], $dsn['password'], $dsn['database'], $dsn['hostspec']);
return $cividb;
}
/**
* Gets the CiviCRM database credentials.
*
* @since 5.69
*
* @return array $dsn The array of CiviCRM database credentials.
*/
private function cividb_dsn_get() {
// Bootstrap CiviCRM.
$this->bootstrap_civicrm();
// Bail if we can't fetch database credentials.
if (!defined('CIVICRM_DSN')) {
WP_CLI::error('CIVICRM_DSN is not defined.');
}
// Parse the CiviCRM credentials.
$dsn = DB::parseDSN(CIVICRM_DSN);
return $dsn;
}
/**
* Gets the CiviCRM database functions.
*
* @since 5.69
*
* @return array $functions The array of CiviCRM database functions.
*/
private function cividb_functions_get() {
// Use "wp civicrm db functions" to find the CiviCRM database functions.
$command = "civicrm db functions 'civicrm_*' --format=json";
$options = ['launch' => FALSE, 'return' => TRUE];
$core_functions = WP_CLI::runcommand($command, $options);
// Convert to array.
$functions = json_decode($core_functions, TRUE);
if (JSON_ERROR_NONE !== json_last_error()) {
WP_CLI::error(sprintf(WP_CLI::colorize('Failed to decode JSON: %y%s.%n'), json_last_error_msg()));
}
return $functions;
}
/**
* Gets the CiviCRM database procedures.
*
* @since 5.69
*
* @return array $procedures The array of CiviCRM database procedures.
*/
private function cividb_procedures_get() {
// Use "wp civicrm db procedures" to find the CiviCRM database procedures.
$command = 'civicrm db procedures --format=json';
$options = ['launch' => FALSE, 'return' => TRUE];
$core_procedures = WP_CLI::runcommand($command, $options);
// Convert to array.
$procedures = json_decode($core_procedures, TRUE);
if (JSON_ERROR_NONE !== json_last_error()) {
WP_CLI::error(sprintf(WP_CLI::colorize('Failed to decode JSON: %y%s.%n'), json_last_error_msg()));
}
return $procedures;
}
/**
* Gets the CiviCRM database tables.
*
* @since 5.69
*
* @param array $assoc_args The WP-CLI associative arguments.
* @return array $tables The array of CiviCRM database tables.
*/
private function cividb_tables_get($assoc_args) {
// Maybe add extra filters.
$also_include = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'also-include', '');
$also_include_args = '';
if (!empty($also_include)) {
$also_include_args = " --also-include={$also_include}";
}
// Use "wp civicrm db tables" to find the CiviCRM database tables.
$command = "civicrm db tables{$also_include_args} --tables-only --format=json";
$options = ['launch' => FALSE, 'return' => TRUE];
$core_tables = WP_CLI::runcommand($command, $options);
// Convert to array.
$tables = json_decode($core_tables, TRUE);
if (JSON_ERROR_NONE !== json_last_error()) {
WP_CLI::error(sprintf(WP_CLI::colorize('Failed to decode JSON: %y%s.%n'), json_last_error_msg()));
}
return $tables;
}
/**
* Gets the CiviCRM database views.
*
* @since 5.69
*
* @param array $assoc_args The WP-CLI associative arguments.
* @return array $views The array of CiviCRM database views.
*/
private function cividb_views_get($assoc_args) {
// Maybe add extra filters.
$also_include = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'also-include', '');
$also_include_args = '';
if (!empty($also_include)) {
$also_include_args = " --also-include={$also_include}";
}
// Use "wp civicrm db tables" to find the CiviCRM database views.
$command = "civicrm db tables 'civicrm_*'{$also_include_args} --views-only --format=json";
$options = ['launch' => FALSE, 'return' => TRUE];
$core_views = WP_CLI::runcommand($command, $options);
// Convert to array.
$views = json_decode($core_views, TRUE);
if (JSON_ERROR_NONE !== json_last_error()) {
WP_CLI::error(sprintf(WP_CLI::colorize('Failed to decode JSON: %y%s.%n'), json_last_error_msg()));
}
return $views;
}
/**
* Filters an array of CiviCRM database entity names.
*
* @since 5.69
*
* @param array $wildcards The array of wildcards.
* @param array $tables The array of CiviCRM table names.
* @return array $filtered The filtered array of CiviCRM table names.
*/
private function names_filter($wildcards, $tables) {
// Build filtered array.
$args_tables = [];
foreach ($wildcards as $wildcard) {
if (FALSE !== strpos($wildcard, '*') || FALSE !== strpos($wildcard, '?')) {
$args_tables = array_merge(
$args_tables,
array_filter(
$tables,
function ($v) use ($wildcard) {
// WP-CLI itself uses fnmatch() so ignore the civilint warning.
// phpcs:disable
return fnmatch($wildcard, $v);
// phpcs:enable
}
)
);
}
else {
$args_tables[] = $wildcard;
}
}
// Clean up.
$args_tables = array_values(array_unique($args_tables));
$filtered = array_values(array_intersect($tables, $args_tables));
return $filtered;
}
}