[ 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: ''.
   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      $msg = 'Connected to ' . $dsn;
  64      if ($context !== '') {
  65          $msg .= ', context: ' . $context;
  66      }
  67      yourls_debug_log( $msg );
  68  
  69      yourls_debug_mode( YOURLS_DEBUG );
  70  
  71      return $ydb;
  72  }
  73  
  74  /**
  75   * Helper function: return instance of the DB
  76   *
  77   * Instead of:
  78   *     global $ydb;
  79   *     $ydb->do_stuff()
  80   * Prefer :
  81   *     yourls_get_db()->do_stuff()
  82   *
  83   * @since  1.7.10
  84   * @param string $context Optional context. Default: ''.
  85   *   If not provided, the function will trigger a notice to encourage developers to provide a context while not
  86   *   breaking existing code. A context is a string describing the operation for which the DB is requested.
  87   *   Use a naming schema starting with a prefix describing the operation, followed by a short description:
  88   *   - Prefix should be either "read-" or "write-", as follows:
  89   *        * "read-" for operations that only read from the DB (eg get_keyword_infos)
  90   *        * "write-" for operations that write to the DB (eg insert_link_in_db)
  91   *   - The description should be lowercase, words separated with underscores, eg "insert_link_in_db".
  92   *   Examples:
  93   *   - read-fetch_keyword
  94   *   - write-insert_link_in_db
  95   * @return \YOURLS\Database\YDB
  96   */
  97  function yourls_get_db($context = '') {
  98      // Allow plugins to short-circuit the whole function
  99      $pre = yourls_apply_filter( 'shunt_get_db', false, $context );
 100      if ( false !== $pre ) {
 101          return $pre;
 102      }
 103  
 104      // Validate context and raise notice if missing or malformed
 105      if ($context == '' || !preg_match('/^(read|write)-[a-z0-9_]+$/', $context)) {
 106          $db = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
 107          $file = $db[0]['file'];
 108          $line = $db[0]['line'];
 109  
 110          if ($context == '') {
 111              $msg = 'Undefined yourls_get_db() context';
 112          } else {
 113              $msg = 'Improperly formatted yourls_get_db() context ("' . $context . '")';
 114          }
 115  
 116          trigger_error( $msg . ' at <b>' . $file . ':' . $line .'</b>', E_USER_NOTICE );
 117      }
 118  
 119      yourls_do_action( 'get_db_action', $context );
 120  
 121      global $ydb;
 122      $ydb = ( isset( $ydb ) ) ? $ydb : yourls_db_connect($context);
 123      return yourls_apply_filter('get_db', $ydb, $context);
 124  }
 125  
 126  /**
 127   * Helper function : set instance of DB, or unset it
 128   *
 129   * Instead of:
 130   *     global $ydb;
 131   *     $ydb = stuff
 132   * Prefer :
 133   *     yourls_set_db( stuff )
 134   * (This is mostly used in the test suite)
 135   *
 136   * @since 1.7.10
 137   * @param  mixed $db    Either a \YOURLS\Database\YDB instance, or anything. If null, the function will unset $ydb
 138   * @return void
 139   */
 140  function yourls_set_db($db) {
 141      global $ydb;
 142  
 143      if (is_null($db)) {
 144          unset($ydb);
 145      } else {
 146          $ydb = $db;
 147      }
 148  }


Generated: Tue Jan 27 05:10:15 2026 Cross-referenced by PHPXref 0.7.1