WordPress.org

Make WordPress Core

Ticket #31018: 31018-2.diff

File 31018-2.diff, 2.5 KB (added by jtsternberg, 5 years ago)

While I'm still a +1 on the original patch (I think persistent connections are useful enough to warrant config override), I still think some abstraction is needed in these methods. This patch abstracts mysqli_real_connect and mysql_pconnect, for easy replacement by drop-in replacements.

  • wp-includes/wp-db.php

     
    14171417                                }
    14181418                        }
    14191419
    1420                         if ( WP_DEBUG ) {
    1421                                 mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    1422                         } else {
    1423                                 @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    1424                         }
     1420                        $this->mysqli_real_connect( $host, $port, $socket, $client_flags );
    14251421
    14261422                        if ( $this->dbh->connect_errno ) {
    14271423                                $this->dbh = null;
     
    14471443                                }
    14481444                        }
    14491445                } else {
    1450                         if ( WP_DEBUG ) {
    1451                                 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    1452                         } else {
    1453                                 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    1454                         }
     1446                        $this->mysql_connect( $new_link, $client_flags );
    14551447                }
    14561448
    14571449                if ( ! $this->dbh && $allow_bail ) {
     
    14891481        }
    14901482
    14911483        /**
     1484         * A mysqli_real_connect abstraction method
     1485         *
     1486         * @since 4.2.0
     1487         *
     1488         * @param string $host         host name or an IP address
     1489         * @param int    $port         Specifies the port number to attempt to connect to the MySQL server.
     1490         * @param string $socket       Specifies the socket or named pipe that should be used.
     1491         * @param int    $client_flags Sets different connection options
     1492         */
     1493        public function mysqli_real_connect( $host, $port, $socket, $client_flags ) {
     1494                if ( WP_DEBUG ) {
     1495                        mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
     1496                } else {
     1497                        @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
     1498                }
     1499        }
     1500
     1501        /**
     1502         * A mysql_connect abstraction method
     1503         *
     1504         * @since 4.2.0
     1505         *
     1506         * @param bool  $new_link     Is this a new link?
     1507         * @param int    $client_flags Sets different connection options
     1508         */
     1509        public function mysql_connect( $new_link, $client_flags ) {
     1510                if ( WP_DEBUG ) {
     1511                        $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1512                } else {
     1513                        $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1514                }
     1515        }
     1516
     1517        /**
    14921518         * Check that the connection to the database is still up. If not, try to reconnect.
    14931519         *
    14941520         * If this function is unable to reconnect, it will forcibly die, or if after the