DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:wordpress_user_export

Wordpress User Export

If you want to export all users from a Wordpress instance and then easily import them into DokuWiki the following might be helpful.

Note: in the queries below a database table prefix of wp_ is assumed. Change it accordingly to your setup.

For CSV based import

The following query would be suitable to be run in phpMyAdmin. The result could be exported as CSV and then imported via the user manager.

It does not export the passwords. Instead the user manager will create a new password for each user and mail it to them.

SELECT
   REGEXP_REPLACE(LOWER(user_login), '[^\\w]', '_') AS 'User',
   display_name AS 'Real Name',
   user_email AS 'Email',
   TRIM(BOTH ',' FROM REGEXP_REPLACE(CONCAT('"',meta_value,'"'), '"(.*?)"', ',')) AS 'Groups'
FROM wp_users u
JOIN wp_usermeta m ON u.id = m.user_id AND meta_key = 'wp_capabilities';

For creating a users.auth.php

If you want the passwords as well, you need to create the `conf/users.auth.php` file yourself. You can create the appropriate lines with the following query:

SELECT CONCAT_WS(':',
   REGEXP_REPLACE(LOWER(user_login), '[^\\w]', '_'),
   user_pass,
   REGEXP_REPLACE(display_name, '[^\\w ]', ''),
   user_email,
   CONCAT(TRIM(LEADING ',' FROM REGEXP_REPLACE(CONCAT('"',meta_value,'"'), '"(.*?)"', ',')), 'user')
) AS line
FROM wp_users u
JOIN wp_usermeta m ON u.id = m.user_id AND meta_key = 'wp_capabilities';

The resulting lines can be added to your users.auth file. DokuWiki should be able to work with the hasshed passwords Wordpress uses.

How does it work?

Wordpress stores group info as PHP serialized array. Normally you would need PHP to deserialize the array. The above code uses some regex magic to do it in the database query itself. This works here but is not a general solution.

Here is a step by step break-down on how it works:

When we look at the meta_value column in the wp_usermeta table, we see the PHP serialized array of groups:

SELECT meta_value FROM wp_usermeta WHERE user_id = 1 AND meta_key = 'wp_capabilities';
a:3:{s:13:"administrator";b:1;s:8:"explorer";b:1;s:10:"subscriber";b:1;}

There's some meta information in there, but what we immediately see is that all our groups are surrounded by double quotes. We can use the REGEXP_REPLACE function to match them and replace them with a comma:

SELECT REGEXP_REPLACE(meta_value, '"(.*?)"', ',') ...
a:3:{s:13:,;b:1;s:8:,;b:1;s:10:,;b:1;}

But now we got rid of what we actually want and have only the meta data stuff left. What we would need is the field to start and end with an additional quote. This way the meta cruft is surrounded by quotes:

SELECT CONCAT('"',meta_value,'"') ...
"a:3:{s:13:"administrator";b:1;s:8:"explorer";b:1;s:10:"subscriber";b:1;}"

Now we can do the regular expression replacement again. This time it removes the cruft and keeps the groups:

SELECT REGEXP_REPLACE(CONCAT('"',meta_value,'"'), '"(.*?)"', ',') ...
,administrator,explorer,subscriber,

Next we need to get rid of the commas at the beginning and end. We can specify BOTH or LEADING in the TRIM function. Here we only trim the leading comma:

SELECT TRIM(LEADING ',' FROM REGEXP_REPLACE(CONCAT('"',meta_value,'"'), '"(.*?)"', ',')) ...
administrator,explorer,subscriber,

We left the trailing comman intact, so we can add the DokuWiki default group user to all results:

SELECT CONCAT(TRIM(BOTH ',' FROM REGEXP_REPLACE(CONCAT('"',meta_value,'"'), '"(.*?)"', ',')), 'user') ...
administrator,explorer,subscriber,user

That's it.

Some additional tweaks are done above to fix the username (Replace non-word chars with underscores, lowercase) and full name (Remove non-word chars, but keep spaces).

tips/wordpress_user_export.txt · Last modified: 2023-10-18 18:32 by andi

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki