E
E
Express7772015-02-25 07:06:29
1C-Bitrix
Express777, 2015-02-25 07:06:29

How to transfer user database from WordPress to 1C Bitrix?

Hello.
There is a task to transfer users from WordPress 3.9x to 1C Bitrix 15.0x standard.
In addition to users, nothing needs to be transferred.
So far, I noticed that information about users in WordPress is stored in two tables:
Wp_users - data about authorization, login, email, password are stored.
Wp_users_meta - additional data, first name, last name.
Is this correct or are there other tables that have information about users?
Bitrix users also have additional properties.
13ed4cf34d7244748ad56be04967aa2a
There is a b_users table in the Bitrix database, but standard values ​​​​are stored there, such as login, email, pass.
How to connect it with additional properties?
Update #1:
Googled this so far:
promst59.ru/page/bitrix-struktura-bazy-dannyh-modu...
Update #2:
Google found out that WordPress does not store avatars in the database.
https://buddypress.org/support/topic/were-is-avata...

Avatars don't have a database entry. They live here:
/wp-content/uploads/avatars/[member_id]/[avatar file]
But there needs to be a thumb and a full-size, and you'll need to change the file names.
And they may need to be jpg.
For example:
4f5664228829d-bpthumb.jpg
4f5664228829dauto-bpfull.jpg

Update #3
When joining tables, this resource can help you:
www.k-press.ru/cs/2010/2/sqlrecept%5Csqlrecept2.as...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Express777, 2015-03-10
@Express777

Finally I was able to import users from WordPress to Bitrix.
I've already written enough in updates to be able to write all the code myself.
But, just in case, I give my version with comments. It may come in handy:
1. We will import users through the standard Bitrix service "import users". Through it, you can import almost everything related to users:
Importing users via CSV
2. If you want to import a custom property, just specify the name of the column with an additional property, for example UF_USER_FIELD and the values ​​​​in it, (if this is not a multiple property, about multiple ones below) .
If it's a picture (avatar), just specify the relative path to the picture in the PERSONAL_PHOTO column. The folder from which the pictures will be taken, specify in the admin panel when importing users: "Path to pictures relative to the root of the site".
Now to the specifics.
information about users in WordPress is stored in tables:
wp_users - login, password, email
wp_bp_xprofile_data - analogy with Bitrix user properties, values ​​are stored
wp_bp_xprofile_fields - names of user properties
You need to link these tables in the request and create a CSV file.
Example:

//  Подключение к БД вордпресса
$mysqli = new mysqli("localhost", "root", "", "wp") or die("Not connected"); 
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
$query = "select 
  t_users.ID as `UF_OLD_USER_ID` /*Заранее создал поль 
        свойство с таким именем. Понадобится при импорте аватарок, 
        да и лучше сохранить старые ИД. На всякий случаи. */
  , '1.jpg' as `PERSONAL_PHOTO`
  , t_users.user_login as `LOGIN`
  , t_users.user_pass as `PASSWORD`
  , t_users.user_email as `EMAIL`
  , t_users.user_registered as `DATE_REGISTER`
  , SUBSTRING_INDEX(t_users.display_name, ' ', 1) as `NAME`
  , SUBSTRING_INDEX(t_users.display_name, ' ', -1) as `LAST_NAME`
  , max(CASE WHEN t2.name = 'Телефон'
    THEN t1.value ELSE null END) as `PERSONAL_PHONE` 
  , max(CASE WHEN t2.name = 'Город'
    THEN t1.value ELSE null END) as `PERSONAL_CITY` 
  , max(CASE WHEN t2.name = 'Адрес студии'
    THEN t1.value ELSE null END) as `UF_ADDRESS` 
  , max(CASE WHEN t2.name = 'Сайт'
    THEN t1.value ELSE null END) as `PERSONAL_WWW` 
  , max(CASE WHEN t2.name = 'Образ'
    THEN t1.value ELSE null END) as `MASTER_SERVICE_1` 
  , max(CASE WHEN t2.name = 'Визажист'
    THEN t1.value ELSE null END) as `MASTER_SERVICE_2` 
  

  
FROM wp_bp_xprofile_data as t1
INNER JOIN  wp_bp_xprofile_fields as t2
  on t1.field_id = t2.id
INNER JOIN wp_users t_users
  ON t_users.ID = t1.user_id 
#where user_id = 3
group by t1.user_id
";

if( !$mysqli->query( $query ) )
{
  printf("Error: %s\n<br />", $mysqli->error);	
}

$create_q = "";
$res = $mysqli->query( $query);
$fp = fopen('file.csv', 'w+'); // Создание csv файла

while ( $row = $res->fetch_assoc())
{
  // Некоторые значения хранятся в сериализированном виде. Преобразуем в массив.
  $tmp1 = unserialize($row["MASTER_SERVICE_1"]) ? unserialize($row["MASTER_SERVICE_1"])  : array(); 
  $tmp2 = unserialize($row["MASTER_SERVICE_2"]) ? unserialize($row["MASTER_SERVICE_2"])  : array();
  // в Битриксе в сер виде хранятся значения множественных свойств.
  $tmp = array_merge( $tmp1, $tmp2);

There are two ways to save the values ​​of multiple properties in Bitrix.
The first, according to Bitrikov: For each value, you need to create a new line in CSV.
That is, in each new line there should be the same data
for simple types and only the value of the multiple property should change.
And so on, until you sort through all the values.
The second, through a crutch.
Save all values ​​of multiple properties to an array and:
2.1 Write the values ​​directly to the Bitrix database
2.2 Update the values ​​of an already created user via the Cuser::update method. As a value, you need to pass just an array.
Out of ignorance, I chose method 2.1
. If necessary, we correct the data in the tmp array.
Importing old avatars.
As I already wrote, avatars are stored as /uploads/avatars/[member_id]/[avatar file]
$pathName = "./uploads-portfolio/avatars/".$row["UF_OLD_USER_ID"]."/";
  $entries = scandir( $pathName );
  $pattern = "#[\w\d]*-bpfull\.(png|jpg)$#";
  
  foreach($entries as $entry) 
  {
      if ( preg_match($pattern, $entry)) 
      {
          $row["PERSONAL_PHOTO"] = "/".$row["UF_OLD_USER_ID"]."/".$entry;
      }
  }

  $tmp = array_unique( $tmp );
// У меня UF_MASTER_SERVICE поль свойство множественного значения
  $row["UF_MASTER_SERVICE"] = serialize($tmp);
  fputcsv($fp, $row, ";", '"');
  
}

The import file is almost ready. Don't forget to add column titles. You can do it in Excel or Libre Calc.
Now we add events to init.php:
About saving passwords, read here:
dev.1c-bitrix.ru/community/webdev/user/42405/blog/... ( Petr Zhuchkov Petr Zhuchkov )
AddEventHandler("main", "OnAfterUserAdd", "__afterUserAdd");
function __afterUserAdd( $arParams )
{
     if(defined("USER_IMPORT_EXECUTION_TIME"))
     {
         // Adding multiply property for imported User
         // MY_MASTER_SERVICE - название колонки с множественным свойством
         // RESULT - ИД только, что созданного пользователя
         $mysqli = new mysqli("localhost", "root", "", "bitrix_db");
         $sql = "UPDATE b_uts_user SET UF_MASTER_SERVICE = '"
                .$arParams["MY_MASTER_SERVICE"]
                ."' WHERE VALUE_ID =".$arParams["RESULT"];
         $result = $mysqli->query( $sql);
         if( !$result )
         {
             echo $mysqli->error;
         }
     }
}

Be careful with this method. The entered data is visible in the user profile. Even taken by the Getlist method. But, for some reason, they do not work in the search. And I just needed a search for this property. The output in the loop is to update the values ​​of the set properties of all users via Cuser::update.
All.

P
Petr Zhuchkov, 2015-02-25
@HelsinG

There is a description of how to do it here dev.1c-bitrix.ru/community/webdev/user/42405/blog/...
Or marketplace.1c-bitrix.ru/solutions/yenisite.migrator

K
kompi, 2015-02-25
@kompi

A table with users from WP is added to the Bitrix database.
Further, either by hand directly into the database queries (joins / subqueries), or write a small script.

A
Alexey Emelyanov, 2015-02-25
@babarun

Express777 : what additional saints are we talking about? In my opinion, there are too many fields in b_user. Can you give an example?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question