Skip to content
Snippets Groups Projects
command-db.php 36.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
        // 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;
    
      }
    
    }