[ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Connect to DB 5 * 6 * @since 1.0 7 * @return \YOURLS\Database\YDB 8 */ 9 function yourls_db_connect() { 10 global $ydb; 11 12 if ( !defined( 'YOURLS_DB_USER' ) 13 or !defined( 'YOURLS_DB_PASS' ) 14 or !defined( 'YOURLS_DB_NAME' ) 15 or !defined( 'YOURLS_DB_HOST' ) 16 ) { 17 yourls_die( yourls__( 'Incorrect DB config, please refer to documentation' ), yourls__( 'Fatal error' ), 503 ); 18 } 19 20 $dbhost = YOURLS_DB_HOST; 21 $user = YOURLS_DB_USER; 22 $pass = YOURLS_DB_PASS; 23 $dbname = YOURLS_DB_NAME; 24 25 // This action is deprecated 26 yourls_do_action( 'set_DB_driver', 'deprecated' ); 27 28 // Get custom port if any 29 if ( false !== strpos( $dbhost, ':' ) ) { 30 list( $dbhost, $dbport ) = explode( ':', $dbhost ); 31 $dbhost = sprintf( '%1$s;port=%2$d', $dbhost, $dbport ); 32 } 33 34 $charset = yourls_apply_filter( 'db_connect_charset', 'utf8mb4' ); 35 36 /** 37 * Data Source Name (dsn) used to connect the DB 38 * 39 * DSN with PDO is something like: 40 * 'mysql:host=123.4.5.6;dbname=test_db;port=3306' 41 * 'sqlite:/opt/databases/mydb.sq3' 42 * 'pgsql:host=192.168.13.37;port=5432;dbname=omgwtf' 43 */ 44 $dsn = sprintf( 'mysql:host=%s;dbname=%s;charset=%s', $dbhost, $dbname, $charset ); 45 $dsn = yourls_apply_filter( 'db_connect_custom_dsn', $dsn ); 46 47 /** 48 * PDO driver options and attributes 49 * 50 * The PDO constructor is something like: 51 * new PDO( string $dsn, string $username, string $password [, array $options ] ) 52 * The driver options are passed to the PDO constructor, eg array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) 53 * The attribute options are then set in a foreach($attr as $k=>$v){$db->setAttribute($k, $v)} loop 54 */ 55 $driver_options = yourls_apply_filter( 'db_connect_driver_option', [] ); // driver options as key-value pairs 56 $attributes = yourls_apply_filter( 'db_connect_attributes', [] ); // attributes as key-value pairs 57 58 $ydb = new \YOURLS\Database\YDB( $dsn, $user, $pass, $driver_options, $attributes ); 59 $ydb->init(); 60 61 // Past this point, we're connected 62 yourls_debug_log( sprintf( 'Connected to database %s on %s ', $dbname, $dbhost ) ); 63 64 yourls_debug_mode( YOURLS_DEBUG ); 65 66 return $ydb; 67 } 68 69 /** 70 * Helper function : return instance of the DB 71 * 72 * Instead of: 73 * global $ydb; 74 * $ydb->do_stuff() 75 * Prefer : 76 * yourls_get_db()->do_stuff() 77 * 78 * @since 1.7.10 79 * @return \YOURLS\Database\YDB 80 */ 81 function yourls_get_db() { 82 // Allow plugins to short-circuit the whole function 83 $pre = yourls_apply_filter( 'shunt_get_db', false ); 84 if ( false !== $pre ) { 85 return $pre; 86 } 87 88 global $ydb; 89 $ydb = ( isset( $ydb ) ) ? $ydb : yourls_db_connect(); 90 return yourls_apply_filter('get_db', $ydb); 91 } 92 93 /** 94 * Helper function : set instance of DB, or unset it 95 * 96 * Instead of: 97 * global $ydb; 98 * $ydb = stuff 99 * Prefer : 100 * yourls_set_db( stuff ) 101 * (This is mostly used in the test suite) 102 * 103 * @since 1.7.10 104 * @param mixed $db Either a \YOURLS\Database\YDB instance, or anything. If null, the function will unset $ydb 105 * @return void 106 */ 107 function yourls_set_db($db) { 108 global $ydb; 109 110 if (is_null($db)) { 111 unset($ydb); 112 } else { 113 $ydb = $db; 114 } 115 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Jan 21 05:10:11 2025 | Cross-referenced by PHPXref 0.7.1 |