4 * Pick the right DB class and return an instance
7 * @param string $extension Optional: user defined choice
8 * @return class $ydb DB class instance
10 function yourls_set_DB_driver( ) {
12 // Auto-pick the driver. Priority: user defined, then PDO, then mysqli, then mysql
13 if ( defined( 'YOURLS_DB_DRIVER' ) ) {
14 $driver = strtolower( YOURLS_DB_DRIVER ); // accept 'MySQL', 'mySQL', etc
15 } elseif ( extension_loaded( 'pdo_mysql' ) ) {
17 } elseif ( extension_loaded( 'mysqli' ) ) {
19 } elseif ( extension_loaded( 'mysql' ) ) {
26 if ( in_array( $driver, array( 'mysql', 'mysqli', 'pdo' ) ) ) {
27 $class = yourls_require_db_files( $driver );
32 if ( !class_exists( $class, false ) ) {
33 $ydb = new stdClass();
35 yourls__( 'YOURLS requires the mysql, mysqli or pdo_mysql PHP extension. No extension found. Check your server config, or contact your host.' ),
36 yourls__( 'Fatal error' ),
41 yourls_do_action( 'set_DB_driver', $driver );
43 $ydb = new $class( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST );
44 $ydb->DB_driver = $driver;
46 yourls_debug_log( "DB driver: $driver" );
50 * Load required DB class files
52 * This goes in its own function to allow easier unit tests
55 * @param string $driver DB driver
56 * @return string name of the DB class to instantiate
58 function yourls_require_db_files( $driver ) {
59 require_once( YOURLS_INC . '/ezSQL/ez_sql_core.php' );
60 require_once( YOURLS_INC . '/ezSQL/ez_sql_core_yourls.php' );
61 require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '.php' );
62 require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '_yourls.php' );
63 return 'ezSQL_' . $driver . '_yourls';
71 function yourls_db_connect() {
74 if ( !defined( 'YOURLS_DB_USER' )
75 or !defined( 'YOURLS_DB_PASS' )
76 or !defined( 'YOURLS_DB_NAME' )
77 or !defined( 'YOURLS_DB_HOST' )
78 ) yourls_die ( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );
80 // Are we standalone or in the WordPress environment?
81 if ( class_exists( 'wpdb', false ) ) {
82 /* TODO: should we deprecate this? Follow WP dev in that area */
83 $ydb = new wpdb( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST );
85 yourls_set_DB_driver();
92 * Return true if DB server is responding
94 * This function is supposed to be called right after yourls_get_all_options() has fired. It is not designed (yet) to
95 * check for a responding server after several successful operation to check if the server has gone MIA
99 function yourls_is_db_alive() {
103 switch( $ydb->DB_driver ) {
105 $alive = isset( $ydb->dbh );
109 $alive = ( isset( $ydb->dbh ) && false !== $ydb->dbh );
113 $alive = ( null == mysqli_connect_error() );
116 // Custom DB driver & class : delegate check
118 $alive = yourls_apply_filter( 'is_db_alive_custom', false );
125 * Die with a DB error message
127 * @TODO in version 1.8 : use a new localized string, specific to the problem (ie: "DB is dead")
131 function yourls_db_dead() {
132 // Use any /user/db_error.php file
133 if( file_exists( YOURLS_USERDIR . '/db_error.php' ) ) {
134 include_once( YOURLS_USERDIR . '/db_error.php' );
138 yourls_die( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );