[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/ -> class-mysql.php (source)

   1  <?php
   2  
   3  /**
   4   * Connect to DB
   5   *
   6   * @since 1.0
   7   * @param string $context Optional context. Default: ''. See yourls_get_db()
   8   * @return \YOURLS\Database\YDB
   9   */
  10  function yourls_db_connect($context = '') {
  11      global $ydb;
  12  
  13      if ( !defined( 'YOURLS_DB_USER' )
  14           or !defined( 'YOURLS_DB_PASS' )
  15           or !defined( 'YOURLS_DB_NAME' )
  16           or !defined( 'YOURLS_DB_HOST' )
  17      ) {
  18          yourls_die( yourls__( 'Incorrect DB config, please refer to documentation' ), yourls__( 'Fatal error' ), 503 );
  19      }
  20  
  21      $dbhost = YOURLS_DB_HOST;
  22      $user = YOURLS_DB_USER;
  23      $pass = YOURLS_DB_PASS;
  24      $dbname = YOURLS_DB_NAME;
  25  
  26      // This action is deprecated
  27      yourls_do_action( 'set_DB_driver', 'deprecated' );
  28  
  29      // Get custom port if any
  30      if (str_contains($dbhost, ':')) {
  31          list( $dbhost, $dbport ) = explode( ':', $dbhost );
  32          $dbhost = sprintf( '%1$s;port=%2$d', $dbhost, $dbport );
  33      }
  34  
  35      $charset = yourls_apply_filter( 'db_connect_charset', 'utf8mb4', $context );
  36  
  37      /**
  38       * Data Source Name (dsn) used to connect the DB
  39       *
  40       * DSN with PDO is something like:
  41       * 'mysql:host=123.4.5.6;dbname=test_db;port=3306'
  42       * 'sqlite:/opt/databases/mydb.sq3'
  43       * 'pgsql:host=192.168.13.37;port=5432;dbname=omgwtf'
  44       */
  45      $dsn = sprintf( 'mysql:host=%s;dbname=%s;charset=%s', $dbhost, $dbname, $charset );
  46      $dsn = yourls_apply_filter( 'db_connect_custom_dsn', $dsn, $context );
  47  
  48      /**
  49       * PDO driver options and attributes
  50       *
  51       * The PDO constructor is something like:
  52       *   new PDO( string $dsn, string $username, string $password [, array $options ] )
  53       * The driver options are passed to the PDO constructor, eg array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  54       * The attribute options are then set in a foreach($attr as $k=>$v){$db->setAttribute($k, $v)} loop
  55       */
  56      $driver_options = yourls_apply_filter( 'db_connect_driver_option', [], $context ); // driver options as key-value pairs
  57      $attributes = yourls_apply_filter( 'db_connect_attributes', [], $context ); // attributes as key-value pairs
  58  
  59      $ydb = new \YOURLS\Database\YDB( $dsn, $user, $pass, $driver_options, $attributes );
  60      $ydb->init();
  61  
  62      // Past this point, we're connected
  63      yourls_debug_log( 'Connected to ' . $dsn );
  64  
  65      yourls_debug_mode( YOURLS_DEBUG );
  66  
  67      return $ydb;
  68  }
  69  
  70  /**
  71   * Helper function: return instance of the DB
  72   *
  73   * Instead of:
  74   *     global $ydb;
  75   *     $ydb->do_stuff()
  76   * Prefer :
  77   *     yourls_get_db()->do_stuff()
  78   *
  79   * @since  1.7.10
  80   * @param string $context Optional context. Default: ''.
  81   *   If not provided, the function will trigger a notice to encourage developers to provide a context while not
  82   *   breaking existing code. A context is a string describing the operation for which the DB is requested.
  83   *   Use a naming schema starting with a prefix describing the operation, followed by a short description:
  84   *   - Prefix should be either "read-" or "write-", as follows:
  85   *        * "read-" for operations that only read from the DB (eg get_keyword_infos)
  86   *        * "write-" for operations that write to the DB (eg insert_link_in_db)
  87   *   - The description should be lowercase, words separated with underscores, eg "insert_link_in_db".
  88   *   Examples:
  89   *   - read-fetch_keyword
  90   *   - write-insert_link_in_db
  91   * @return \YOURLS\Database\YDB
  92   */
  93  function yourls_get_db($context = '') {
  94      // Allow plugins to short-circuit the whole function
  95      $pre = yourls_apply_filter( 'shunt_get_db', yourls_shunt_default(), $context );
  96      if ( yourls_shunt_default() !== $pre ) {
  97          return $pre;
  98      }
  99  
 100      // Validate context and raise notice if missing or malformed
 101      if ($context == '' || !preg_match('/^(read|write)-[a-z0-9_]+$/', $context)) {
 102          $db = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
 103          $file = $db[0]['file'];
 104          $line = $db[0]['line'];
 105  
 106          if ($context == '') {
 107              $msg = 'Undefined yourls_get_db() context';
 108          } else {
 109              $msg = 'Improperly formatted yourls_get_db() context ("' . $context . '")';
 110          }
 111  
 112          trigger_error( $msg . ' at <b>' . $file . ':' . $line .'</b>', E_USER_NOTICE );
 113      }
 114  
 115      yourls_do_action( 'get_db_action', $context );
 116  
 117      global $ydb;
 118      $ydb = ( isset( $ydb ) ) ? $ydb : yourls_db_connect($context);
 119      return yourls_apply_filter('get_db', $ydb, $context);
 120  }
 121  
 122  /**
 123   * Helper function : set instance of DB, or unset it
 124   *
 125   * Instead of:
 126   *     global $ydb;
 127   *     $ydb = stuff
 128   * Prefer :
 129   *     yourls_set_db( stuff )
 130   * (This is mostly used in the test suite)
 131   *
 132   * @since 1.7.10
 133   * @param  mixed $db    Either a \YOURLS\Database\YDB instance, or anything. If null, the function will unset $ydb
 134   * @return void
 135   */
 136  function yourls_set_db($db) {
 137      global $ydb;
 138  
 139      if (is_null($db)) {
 140          unset($ydb);
 141      } else {
 142          $ydb = $db;
 143      }
 144  }


Generated: Wed Apr 8 05:10:41 2026 Cross-referenced by PHPXref 0.7.1