/** * Connects to the database server. If no arguments are * supplied, then the connection is attempted for the * database authentication variables in config.php. * * @param (optional) connection_string a PDO connection string * @param (optional) db_user the database user name * @param (optional) db_pass the database user password * @return bool */ public static function connect() { /* if connection has been instantiated (ie: not null), check if is already connected */ if (null != DB::instance()->connection) { if (func_num_args() == 0 && false != DB::instance()->connection->is_connected()) { return true; } } if (func_num_args() > 0) { $connect_string = func_get_arg(0); $db_user = func_get_arg(1); $db_pass = func_get_arg(2); } else { /* We use the config.php variables */ $connect_string = Config::get('db_connection')->connection_string; $db_user = Config::get('db_connection')->username; $db_pass = Config::get('db_connection')->password; } DB::instance()->connection = DatabaseConnection::ConnectionFactory($connect_string); if (null != DB::instance()->connection) { return DB::instance()->connection->connect($connect_string, $db_user, $db_pass); } else { // do some error handling here. The connect string does not have a corresponding DB connection object print _t('Panic! No database connection class appears to be found for the connection string specified. Please check config.php'); } }
public function action_handler_dev_feed($handler_vars) { $rss = Plugins::get_by_interface('RSS'); if (count($rss) !== 1) { exit; } $xml = reset($rss)->create_rss_wrapper(); $connection_string = Options::get('tracfeed__connection_string'); $username = Options::get('tracfeed__username'); $password = Options::get('tracfeed__password'); $db = DatabaseConnection::ConnectionFactory($connection_string); $db->connect($connection_string, $username, $password); $times = $db->get_column('SELECT time from ticket_change group by time order by time desc limit 15;'); $mintime = array_reduce($times, 'min', reset($times)); $comments = $db->get_results("\n\t\t\tSELECT *, ticket_change.time as changetime from ticket_change \n\t\t\tINNER JOIN ticket on ticket.id = ticket_change.ticket\n\t\t\twhere \n\t\t\tchangetime >= ? and\n\t\t\tnot (field ='comment' and newvalue = '') \n\t\t\torder by ticket_change.time DESC;\n\t\t", array($mintime)); $posts = array(); foreach ($comments as $comment) { $post_id = md5($comment->ticket . ':' . $comment->changetime . ':' . $comment->author); if (!array_key_exists($post_id, $posts)) { $post = new Post(); $post->title = "Ticket #{$comment->ticket}: {$comment->summary}"; $post->content = "Changes by {$comment->author} on ticket <a href=\"http://trac.habariproject.org/habari/ticket/{$comment->ticket}\">#{$comment->ticket}</a>.\n<br>\n<br>"; $post->guid = 'tag:' . Site::get_url('hostname') . ',trac_comment,' . $post_id; $post->content_type = 'dev_feed'; $post->slug = "http://trac.habariproject.org/habari/ticket/{$comment->ticket}"; $post->pubdate = date('r', $comment->changetime); $posts[$post_id] = $post; } else { $post = $posts[$post_id]; } switch ($comment->field) { case 'comment': $content = $comment->newvalue; $content = preg_replace('/\\b(https?|ftp|file):\\/\\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\\/%=~_|]/i', '<a href="$0">[link]</a>', $content); $content = preg_replace('%\\br([0-9]+)\\b%', '<a href="http://trac.habariproject.org/habari/changeset/$1">r$1</a>', $content); $content = preg_replace('%\\b#([0-9]+)\\b%', '<a href="http://trac.habariproject.org/habari/ticket/$1">$1</a>', $content); $post->content .= "Comment #{$comment->oldvalue} by {$comment->author}:\n<blockquote><pre>{$content}</pre></blockquote>\n<br>"; break; default: if (trim($comment->oldvalue) == '') { $post->content .= "Set <b>{$comment->field}</b> to: {$comment->newvalue}\n<br>"; } else { $post->content .= "Changed <b>{$comment->field}</b> from: {$comment->oldvalue}\n<br> To: {$comment->newvalue}\n<br>"; } break; } } $xml = RSS::add_posts($xml, $posts); ob_clean(); header('Content-Type: application/xml'); echo $xml->asXML(); exit; }
/** * Attempt to connect to the WordPress database * * @param string $db_host The hostname of the WP database * @param string $db_name The name of the WP database * @param string $db_user The user of the WP database * @param string $db_pass The user's password for the WP database * @param string $db_prefix The table prefix for the WP instance in the database * @return mixed false on failure, DatabseConnection on success */ private function wp_connect($db_host, $db_name, $db_user, $db_pass, $db_prefix) { // Connect to the database or return false try { $wpdb = DatabaseConnection::ConnectionFactory("mysql:host={$db_host};dbname={$db_name}"); $wpdb->connect("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass); return $wpdb; } catch (Exception $e) { return false; } }
private function wp_connect($db_host, $db_name, $db_user, $db_pass) { // build the connection string, since we stupidly have to use it twice $connection_string = 'mysql:host=' . $db_host . ';dbname=' . $db_name; try { $wpdb = DatabaseConnection::ConnectionFactory($connection_string); $wpdb->connect($connection_string, $db_user, $db_pass); // @todo make sure preifx_* tables exist? return $wpdb; } catch (Exception $e) { // just hide connection errors, it's enough that we errored out return false; } }
/** * Attempt to connect to the Chyrp database * * @param string $connect_string The connection string of the Habari database * @param string $db_user The user of the database * @param string $db_pass The user's password for the database * @return mixed false on failure, DatabseConnection on success */ private function chryp_connect($connect_string, $db_user, $db_pass) { // Connect to the database or return false try { $db = DatabaseConnection::ConnectionFactory($connect_string); $db->connect($connect_string, $db_user, $db_pass); return $db; } catch (Exception $e) { return false; } }