interconnectit / Search-Replace-DB

This script was made to aid the process of migrating PHP and MySQL based websites. Works with most common CMSes.
https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
GNU General Public License v3.0
3.99k stars 850 forks source link

Work with Socket instead of Host+Port #343

Closed cliffordp closed 3 years ago

cliffordp commented 4 years ago

https://localwp.com/ uses a Socket on Mac instead of an IP + Port. Would be nice to use it with that tool.

will-ashworth commented 4 years ago

I'm trying to figure this out as well. Any ideas?

will-ashworth commented 4 years ago

@cliffordp

I did some digging into the CLI code last night. The GUI appears to work in Local; I think because it exists/runs within the overall container of what Local has setup for the site you're running locally. However, the CLI didn't work. Perhaps because I'm calling a different, system-level PHP binary for running the CLI script.

Updated this function around line #490 in srdb.class.php

    /**
     * Sets up database connection using PDO
     *
     * @return PDO|bool
     */
    public function connect_pdo() {
        if(preg_match('/^\/.*/', $this->host)) {
            $unix_socket = $this->host;
            $pdo_string = "mysql:dbname={$this->name};unix_socket={$unix_socket}";
        } else {
            $pdo_string = "mysql:host={$this->host};port={$this->port};dbname={$this->name}";
        }
        try {
            $connection = new PDO( $pdo_string, $this->user, $this->pass );
        } catch( PDOException $e ) {
            $this->add_error( $e->getMessage(), 'db' );
            $connection = false;
        }
        // check if there's a problem with our database at this stage
        if ( $connection && ! $connection->query( 'SHOW TABLES' ) ) {
            $error_info = $connection->errorInfo();
            if ( !empty( $error_info ) && is_array( $error_info ) )
                $this->add_error( array_pop( $error_info ), 'db' ); // Array pop will only accept a $var..
            $connection = false;
        }
        return $connection;
    }

I hope this helps! I'm going to submit a PR on Github and see where that gets us.

cliffordp commented 4 years ago

that's great! tbh, I think the WP-CLI command is sufficient: https://developer.wordpress.org/cli/commands/search-replace/

gianluigi-icit commented 3 years ago

nice you found a way to use sockets, let's close this.