Last active
April 22, 2016 07:42
-
-
Save druman/9ce5d80d776a696d022f117c72a8f207 to your computer and use it in GitHub Desktop.
Migrate users: drupal 6 to 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Standalone PHP script to migrate users from Drupal 6 to Drupal 7 programatically. | |
*/ | |
// set HTTP_HOST or drupal will refuse to bootstrap | |
$_SERVER['HTTP_HOST'] = 'SITENAME'; | |
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; | |
//root of Drupal 7 site | |
$DRUPAL7_ROOT="/var/www/site.com"; | |
define('DRUPAL_ROOT',$DRUPAL7_ROOT); | |
chdir($DRUPAL7_ROOT); | |
require_once "./includes/bootstrap.inc"; | |
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); | |
require_once "./includes/password.inc"; | |
//connect to Drupal 6 database | |
//syntax:mysqli(hostname,username,password,databasename); | |
$db= new mysqli('mysql5.activeby.net','dominoandrew','123456789','vithimtorg_pizza'); | |
if(mysqli_connect_errno()) { | |
echo "Conection error. Could not connect to Drupal 6 site!"; | |
exit; | |
} | |
//get users from Drupal 6 database | |
$query="select * from users WHERE uid BETWEEN 2 AND 10000"; | |
$result=$db->query($query); | |
//count number of users | |
$num_results=$result->num_rows; | |
for($i=0;$i<$num_results;$i++){ | |
//fetch each row/user | |
$row=$result->fetch_assoc(); | |
//migrate only active users | |
//print_r($row); | |
if($row['status']==1){ | |
//convert password from Drupal 6 style to Drupal 7 style | |
$hashed_pass='U'.user_hash_password($row['pass'],11); | |
//check if user with same email address already exists in Drupal 7 database, if it does, do not migrate | |
if (!user_load_by_mail($row['mail'])) { | |
// !!! For cyrrillic !!! | |
//$myname = iconv('CP1251','UTF-8',$row['name']); | |
$myname = $row['name']; | |
$account = new stdClass; | |
$account->is_new = TRUE; | |
$account->name = $myname; | |
$account->pass = $hashed_pass; | |
$account->mail = $row['mail']; | |
$account->init = $row['mail']; | |
$account->status = TRUE; | |
$account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE); | |
$account->timezone = variable_get('date_default_timezone', ''); | |
//create user in Drupal 7 site | |
user_save($account); | |
//print message | |
echo "User acount ".$row['name']." has been created\n"; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment