Joomla 3批量用户导入

时间:2013-03-07 01:53:14

标签: php mysql joomla

我使用Joomla 3,我希望能够通过sql或csv批量导入大约500个用户。我一直在寻找扩展来做到这一点,但我只能找到版本2.5的解决方案,它不会安装到版本3中。

基本上我正在寻找的是关于如何将用户添加到joomla 3数据库的描述或某种架构。

谢谢,

迈克尔

3 个答案:

答案 0 :(得分:0)

Joomla是一个很棒的节目。虽然,我建议2.5仍然因为它有更多的模块,并得到更好的支持。 3还有bug。但祝你好运,我希望你找到答案,赢家!

你太棒了!

答案 1 :(得分:0)

我发现了这个,但我还没有尝试过。 http://www.joomlarra.com/arra-user-export-import-for-joomla-3.0-and-2.5/vmchk.html

我知道joomlaxi为所有joomla和jomsocial用户的impex做了一个。我试图确定它是否可以应对joomla 3用户表的翻译。

答案 2 :(得分:0)

我也找不到它,所以我更新了为Joomla 1.5编写的PHP脚本,以便使用新的数据库模式。

<?php

// Handle form upload
if(isset($_POST['import'])) 
    {

    $mysql_host             = trim($_POST['mysql_host']);
    $mysql_user             = trim($_POST['mysql_username']);
    $mysql_password         = trim($_POST['mysql_password']);
    $mysql_schema           = trim($_POST['mysql_schema']);
    $table_prefix           = trim($_POST['table_prefix']);

    // Validate MySQL details    
    if(!mysql_connect($mysql_host, $mysql_user, $mysql_password) || !mysql_select_db($mysql_schema)) {
        echo 'Supplied MySQL details were incorrect - aborting';
        return;
        }

        // set tablenames
        $tb_users = $table_prefix . 'users';
        $tb_usergroups = $table_prefix . 'usergroups';
        $tb_user_usergroup_map = $table_prefix . 'user_usergroup_map';

        // Validate File Name
        if($_FILES["csv"]["error"] != 0) {
            printf('Invalid file name: "%s" - Error code "%s" <br />', $_FILES['csv']['tmp_name'], $_FILES["csv"]["error"]);
            die('<strong>Upload aborted...</strong><br />');
        } else {
            printf('Importing the following csv file<br />
                    File: "%s"<br />
                    Size: "%s" bytes<br />', $_FILES['csv']['name'], $_FILES['csv']['size']);
        }

        // open cvs file
        $fp = fopen($_FILES['csv']['tmp_name'], 'r');

        // validate data
        $valid_data = true;
        while($user = fgetcsv($fp)) {

            // Verify name
            if(!isset($user[0])) {
                display_user('<br /><strong>ERROR!!! Invalid Name </strong><br />', $user);
                $valid_data = false;
            } elseif($user[0] == '') {
                display_user('<br /><strong>ERROR!!! Invalid Name </strong><br />', $user);
                $valid_data = false;
                }

            // Verify username
            if(!isset($user[1])) {
                display_user('<br /><strong>ERROR!!! Invalid username </strong><br />', $user);
                $valid_data = false;
            } elseif($user[1] == '') {
                display_user('<br /><strong>ERROR!!! Invalid username </strong><br />', $user);
                $valid_data = false;
            }

            // Verify email
            if(!isset($user[2])) {
                display_user('<br /><strong>ERROR!!! Invalid email </strong><br />', $user);
                $valid_data = false;
            } elseif($user[2] == '') {
                display_user('<br /><strong>ERROR!!! Invalid email </strong><br />', $user);
                $valid_data = false;
            }

            // Verify Password
            if(!isset($user[3])) {
                display_user('<br /><strong>ERROR!!! Invalid password </strong><br />', $user);
                $valid_data = false;
            } elseif($user[3] == '') {
                display_user('<br /><strong>ERROR!!! Invalid password </strong><br />', $user);
                $valid_data = false;
            }

            // Verify usergroup
            if(!isset($user[4])) {
                display_user('<br /><strong>ERROR!!! Invalid usergroup </strong><br />', $user);
                $valid_data = false;
            } elseif($user[4] == '') {
                display_user('<br /><strong>ERROR!!! Invalid usergroup </strong><br />', $user);
                $valid_data = false;
            }

            // Check duplicate username
            $sql = '
                    SELECT COUNT(*) AS duplicate_username
                    FROM    '.$tb_users.'
                    WHERE   username = "'.$user[1].'"'; 

            $result = mysql_query($sql);
            if (!$result) {
                die('Invalid query: ' . mysql_error());
            }

            while ($row = mysql_fetch_assoc($result)) {
                $duplicate_users = $row['duplicate_username'];
            }

            if ($duplicate_users > 0) {
                display_user('<br /><strong>ERROR!!! Duplicate username </strong><br />', $user);
                    $valid_data = false;
            }

            // Check duplicate email
            $sql = '
                    SELECT COUNT(*) AS duplicate_email
                    FROM    '.$tb_users.'
                    WHERE   email = "'.$user[2].'"'; 

            $result = mysql_query($sql);
            if (!$result) {
                die('Invalid query: ' . mysql_error());
            }

            while ($row = mysql_fetch_assoc($result)) {
                $duplicate_users = $row['duplicate_email'];
            }

            if ($duplicate_users > 0) {
                display_user('<br /><strong>ERROR!!! Duplicate email </strong><br />', $user);
                    $valid_data = false;
            }

    }


    if(!$valid_data) {
        die('<strong>CSV file contains errors. Upload Aborted!</strong>');
    } else {
        printf('<strong>Data is valid. Uploading users...</strong><br />');
    }


    // Count initial users
    $sql = '
            SELECT COUNT(*) AS initial_users
            FROM    '.$tb_users.'
           '
        ; 

    $result = mysql_query($sql);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    while ($row = mysql_fetch_assoc($result)) {
        $initial_users = $row['initial_users'];
    }



    // Load users
    // open cvs file
    $fp = fopen($_FILES['csv']['tmp_name'], 'r');

    $counter = 0;
    while($user = fgetcsv($fp)) {


        // Check duplicate username
        $valid_username = true;
        $sql = '
                SELECT COUNT(*) AS duplicate_username
                FROM    '.$tb_users.'
                WHERE   username = "'.$user[1].'"'; 

        $result = mysql_query($sql);
        if (!$result) {
            die('Invalid query: ' . mysql_error());
        }

            while ($row = mysql_fetch_assoc($result)) {
            $duplicate_username = $row['duplicate_username'];
        }

        if ($duplicate_users > 0) {
            display_user('<br /><strong>ERROR!!! Duplicate username found in database</strong><br />', $user);
                $valid_username = false;
        }

        // Check duplicate email
        $valid_mail = true;
        $sql = '
                SELECT COUNT(*) AS duplicate_email
                FROM    '.$tb_users.'
                WHERE   email = "'.$user[2].'"'; 

        $result = mysql_query($sql);
        if (!$result) {
            die('Invalid query: ' . mysql_error());
        }

        while ($row = mysql_fetch_assoc($result)) {
            $duplicate_users = $row['duplicate_email'];
        }

        if ($duplicate_users > 0) {
            display_user('<br /><strong>ERROR!!! Duplicate email found in database</strong><br />', $user);
                $valid_mail = false;
        }

        if(!$valid_username or !$valid_mail) {
            printf('<strong>WARNING!!! User not uploaded</strong><br /><br />');
        } else {

            // Upload user
            display_user('<br />', $user);


            // Encypt password if required      
            $user_password = isset($_POST['md5_passwords']) ? md5($user[3]) : sql_prep($user[3]);

            // Insert record into users
            $sql = "
                INSERT INTO ".$tb_users." 
                SET
                    name            = '".sql_prep($user[0])."',
                    username        = '".sql_prep($user[1])."',
                    email           = '".sql_prep($user[2])."',
                    password        = '".$user_password."',
                    registerDate    = NOW(),
                    activation      = '',
                    params          = '{\"admin_style\":\"\",\"admin_language\":\"\",\"language\":\"\",\"editor\":\"\",\"helpsite\":\"\",\"timezone\":\"\"}',
                    otpKey          = '',
                    otep            = ''                
            ";

            $result = mysql_query($sql);
            if (!$result) {
                die('Invalid query: ' . mysql_error());
                }

            // Retrieve last uploaded user's ID
            list($user_id) = mysql_fetch_row(mysql_query('SELECT LAST_INSERT_ID()'));

            printf('Assigned user ID: '.$user_id.'<br />');


            // Insert record user_group_map
            $sql = '
                    INSERT INTO '.$tb_user_usergroup_map.'
                SET
                    user_id   = '.$user_id.',
                    group_id  = '.$user[4].'
               ';

            $result = mysql_query($sql);
            if (!$result) {
                die('Invalid query: ' . mysql_error());
                }

        echo 'done.<br />';
        flush();

        }

        // Update counter
        $counter++;

   }

    // Count final users
    $sql = '
            SELECT COUNT(*) AS final_users
            FROM    '.$tb_users.'
           '
        ; 

    $result = mysql_query($sql);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    while ($row = mysql_fetch_assoc($result)) {
        $final_users = $row['final_users'];
    }


    if(($final_users - $initial_users) == $counter) {
        echo '<br /><br /><strong>Done. <br />
              Uploaded '.$counter.' users </strong>';
    } else {
        echo '<br /><br /><strong>ERROR!!! <br />
              Uploaded only some users</strong><br />
              Users submitted: '.$counter.'<br />
              Users uploaded : '.($final_users - $initial_users);
    }


} else {


   // show upload form
   ?>
   <html><head><title>Bulk import users into Joomla 3</title></head><body>
   <h1>Import Users to Joomla</h1>
   <p>
       Use this script to do a bulk import of users into Joomla 1.5.<br />
    Upload a CSV file with the following format:<br />
    <code>
        name, username, email, password, group_number
    </code><br />
    Wrap details with commas in them in quotes.
    </p>
    <hr />
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"> 
        <input type="hidden" name="import" value="1" />
        <table cellpadding="4px">
            <tr>
                <td>CSV File: </td>
                <td><input type="file" name="csv" /></td>
            </tr>
            <tr>
                <td>MD5 Hash Passwords: </td>
                <td><input type="checkbox" name="md5_passwords" /><br /><small>*Check this option if the passwords in your CSV are in plain text</small></td>
            </tr>
            <tr>
                <td>Joomla Table Prefix: </td>
                <td><input type="text" name="table_prefix" value="jos_" /></td>
            </tr>
            <tr>
                <td>Joomla Database Name: </td>
                <td><input type="text" name="mysql_schema" value="joomla" /></td>
            </tr>
            <tr>
                <td>MySQL Host: </td>
                <td><input type="text" name="mysql_host" value="localhost" /></td>
            </tr>
            <tr>
                <td>MySQL Username: </td>
                <td><input type="text" name="mysql_username" value="" /></td>
            </tr>
            <tr>
                <td>MySQL Password: </td>
                <td><input type="password" name="mysql_password" value="" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value=" Import Users! " /></td>
            </tr>
        </table>
    </form>
    </body></html>

    <?php

}   

function sql_prep($var) {
    return mysql_real_escape_string($var);
}

function display_user($message, $user) {

    printf($message);
    printf('Name: "%s" ... ', sql_prep($user[0]));
    printf('  Username: "%s" ... ', sql_prep($user[1]));
    printf('  Password: "%s" ... ', sql_prep($user[3]));
    printf('  Mail: "%s" ... ', sql_prep($user[2]));
    printf('  Group: "%s" ', sql_prep($user[4]));
    printf('<br />');

    return;

}

?>