| 1 |
<?php
|
| 2 |
// $Id: drupalvb.module,v 1.11 2008/04/19 02:58:47 sun Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Drupal vB module database functions.
|
| 7 |
*
|
| 8 |
* Forked from Migrator module, http://drupal.org/project/migrator
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Add vB database connection and connect to remote system database.
|
| 13 |
*
|
| 14 |
* If remote system database connection is identical to Drupal's database
|
| 15 |
* connection, we skip switching the connection and alter the prefix only.
|
| 16 |
*
|
| 17 |
* @see drupalvb_db_disconnect(), drupalvb_get()
|
| 18 |
*/
|
| 19 |
function drupalvb_db_connect() {
|
| 20 |
global $db_url, $db_prefix;
|
| 21 |
if (drupalvb_db_is_valid()) {
|
| 22 |
if (!variable_get('drupalvb_db_is_default', TRUE)) {
|
| 23 |
$drupalvb_db_url = variable_get('drupalvb_db', '');
|
| 24 |
if (!is_array($db_url)) {
|
| 25 |
$db_url = array('default' => $db_url);
|
| 26 |
}
|
| 27 |
if (!empty($drupalvb_db_url)) {
|
| 28 |
$db_url['drupalvb'] = $drupalvb_db_url;
|
| 29 |
db_set_active('drupalvb');
|
| 30 |
}
|
| 31 |
}
|
| 32 |
drupalvb_get_default_db_prefix();
|
| 33 |
$db_prefix = variable_get('drupalvb_db_prefix', 'vb_');
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Disconnect from remote system database.
|
| 39 |
*
|
| 40 |
* @see drupalvb_db_connect(), drupalvb_get()
|
| 41 |
*/
|
| 42 |
function drupalvb_db_disconnect() {
|
| 43 |
if (drupalvb_db_is_valid()) {
|
| 44 |
if (!variable_get('drupalvb_db_is_default', TRUE)) {
|
| 45 |
db_set_active();
|
| 46 |
}
|
| 47 |
drupalvb_set_default_db_prefix();
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Helper function to recall Drupal's default database table prefix.
|
| 53 |
*
|
| 54 |
* @see drupalvb_set_default_prefix()
|
| 55 |
*/
|
| 56 |
function drupalvb_get_default_db_prefix() {
|
| 57 |
global $db_prefix;
|
| 58 |
static $drupal_db_prefix;
|
| 59 |
if (!isset($drupal_db_prefix)) {
|
| 60 |
$drupal_db_prefix = $db_prefix;
|
| 61 |
}
|
| 62 |
return $drupal_db_prefix;
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Helper function to reset Drupal's default database table prefix.
|
| 67 |
*
|
| 68 |
* @see drupalvb_get_default_prefix()
|
| 69 |
*/
|
| 70 |
function drupalvb_set_default_db_prefix() {
|
| 71 |
global $db_prefix;
|
| 72 |
$db_prefix = drupalvb_get_default_db_prefix();
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Check if a configured remote system database connection is valid.
|
| 77 |
*
|
| 78 |
* @see drupalvb_settings_system()
|
| 79 |
*/
|
| 80 |
function drupalvb_db_is_valid() {
|
| 81 |
static $valid;
|
| 82 |
|
| 83 |
if (isset($valid)) {
|
| 84 |
return $valid;
|
| 85 |
}
|
| 86 |
|
| 87 |
$valid = FALSE;
|
| 88 |
$connection_string = variable_get('drupalvb_db', '');
|
| 89 |
$db = parse_url($connection_string);
|
| 90 |
if (!empty($db['scheme']) && !empty($db['host']) && !empty($db['user']) && !empty($db['pass']) && !empty($db['path'])) {
|
| 91 |
foreach (array('user', 'pass', 'host', 'path') as $value) {
|
| 92 |
$db[$value] = urldecode($db[$value]);
|
| 93 |
}
|
| 94 |
// Drupal can't switch database layers, so we fix it if it differs from the
|
| 95 |
// globally used type.
|
| 96 |
if ($db['scheme'] != $GLOBALS['db_type']) {
|
| 97 |
variable_set('drupalvb_db', preg_replace('/^'. $db['scheme'] .'/', $GLOBALS['db_type'], $connection_string));
|
| 98 |
}
|
| 99 |
switch ($GLOBALS['db_type']) {
|
| 100 |
case 'mysql':
|
| 101 |
$connection = @mysql_connect($db['host'], $db['user'], $db['pass'], TRUE, 2);
|
| 102 |
if ($connection && mysql_select_db(substr($db['path'], 1))) {
|
| 103 |
$valid = TRUE;
|
| 104 |
}
|
| 105 |
@mysql_close($connection);
|
| 106 |
break;
|
| 107 |
|
| 108 |
case 'mysqli':
|
| 109 |
$connection = mysqli_init();
|
| 110 |
@mysqli_real_connect($connection, $db['host'], $db['user'], $db['pass'], substr($db['path'], 1), $db['port'], NULL, MYSQLI_CLIENT_FOUND_ROWS);
|
| 111 |
if ($connection) {
|
| 112 |
$valid = TRUE;
|
| 113 |
}
|
| 114 |
@mysqli_close($connection);
|
| 115 |
break;
|
| 116 |
}
|
| 117 |
}
|
| 118 |
if (!$valid && user_access('administer drupalvb')) {
|
| 119 |
drupal_set_message(t('Invalid database connection for vBulletin. Please configure the connection in <a href="!settings">Drupal vB\'s settings</a>', array('!settings' => url('admin/settings/drupalvb/database'))), 'error');
|
| 120 |
}
|
| 121 |
return $valid;
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Generic database callback for querying pre-defined data from remote system.
|
| 126 |
*
|
| 127 |
* @param string $op
|
| 128 |
* The get-operation to perform in the remote system.
|
| 129 |
*/
|
| 130 |
function drupalvb_get($op) {
|
| 131 |
$function = 'drupalvb_get_'. $op;
|
| 132 |
if (!function_exists($function)) {
|
| 133 |
return FALSE;
|
| 134 |
}
|
| 135 |
drupalvb_db_connect();
|
| 136 |
$args = func_get_args();
|
| 137 |
array_shift($args);
|
| 138 |
$result = call_user_func_array($function, $args);
|
| 139 |
drupalvb_db_disconnect();
|
| 140 |
return $result;
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Query remote system database.
|
| 145 |
*
|
| 146 |
* @see db_query()
|
| 147 |
*/
|
| 148 |
function drupalvb_db_query($query) {
|
| 149 |
drupalvb_db_connect();
|
| 150 |
|
| 151 |
$args = func_get_args();
|
| 152 |
array_shift($args);
|
| 153 |
$query = db_prefix_tables($query);
|
| 154 |
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
|
| 155 |
$args = $args[0];
|
| 156 |
}
|
| 157 |
_db_query_callback($args, TRUE);
|
| 158 |
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
|
| 159 |
$result = _db_query($query);
|
| 160 |
|
| 161 |
drupalvb_db_disconnect();
|
| 162 |
return $result;
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Query remote system database with range.
|
| 167 |
*
|
| 168 |
* @see db_query_range
|
| 169 |
*/
|
| 170 |
function drupalvb_db_query_range($query) {
|
| 171 |
drupalvb_db_connect();
|
| 172 |
|
| 173 |
$args = func_get_args();
|
| 174 |
$count = array_pop($args);
|
| 175 |
$from = array_pop($args);
|
| 176 |
array_shift($args);
|
| 177 |
|
| 178 |
$query = db_prefix_tables($query);
|
| 179 |
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
|
| 180 |
$args = $args[0];
|
| 181 |
}
|
| 182 |
_db_query_callback($args, TRUE);
|
| 183 |
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
|
| 184 |
$query .= ' LIMIT '. (int)$count .' OFFSET '. (int)$from;
|
| 185 |
$result = _db_query($query);
|
| 186 |
|
| 187 |
drupalvb_db_disconnect();
|
| 188 |
return $result;
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Return the last insert id.
|
| 193 |
*
|
| 194 |
* Borrowed from Drupal 6.
|
| 195 |
*/
|
| 196 |
function drupalvb_db_last_insert_id($table, $field) {
|
| 197 |
return db_result(drupalvb_db_query('SELECT LAST_INSERT_ID()'));
|
| 198 |
}
|
| 199 |
|
| 200 |
/**
|
| 201 |
* Initialize DrupalvB's user mapping table upon installation.
|
| 202 |
*
|
| 203 |
* Note: We can't do this in a single query, because Drupal's and vB's tables
|
| 204 |
* need not to be in the same database.
|
| 205 |
*
|
| 206 |
* @see drupalvb.admin-pages.inc
|
| 207 |
*/
|
| 208 |
function _drupalvb_init_user_map() {
|
| 209 |
$users = $vbusers = array();
|
| 210 |
// Fetch all users in Drupal.
|
| 211 |
$result = db_query("SELECT uid, name FROM {users}");
|
| 212 |
while ($user = db_fetch_array($result)) {
|
| 213 |
$users[$user['name']] = $user['uid'];
|
| 214 |
}
|
| 215 |
// Fetch all users in vBulletin.
|
| 216 |
$result = drupalvb_db_query("SELECT userid, username FROM {user}");
|
| 217 |
// Insert all vB users who already exist in Drupal with corresponding username
|
| 218 |
// into our mapping table.
|
| 219 |
while ($vbuser = db_fetch_array($result)) {
|
| 220 |
if (isset($users[$vbuser['username']])) {
|
| 221 |
db_query("INSERT INTO {drupalvb_users} (uid, userid) VALUES (%d, %d)", $users[$vbuser['username']], $vbuser['userid']);
|
| 222 |
}
|
| 223 |
}
|
| 224 |
}
|
| 225 |
|