Extension:ImportUsers

The ImportUser extension imports users from a CSV file.

Import File Format
The CSV file consists of four columns:


 * 1) username
 * 2) password
 * 3) email
 * 4) real name

Delimited with commas. For an example:

user1,pass1,user1@gmail.com,User One user2,pass2,user2@gmail.com,User Two user3,pass3,user3@gmail.com,User Three user4,pass4,user4@gmail.com,User Four user5,pass5,user5@gmail.com,User Five user6,pass6,user6@gmail.com,User Six . . . userN,passN,userN@gmail.com,User Nnn

Dupliate/Collision handling
Extension:ImportUsers extension will never create duplicate user accounts.

Two accounts are in collision (or are duplicates of each other) if their login names (user names) are equal.
 * What is account collision?

Collision handling behavior is determined by the "Replace existing users" check box.

When "Replace existing users" check box is clear (unchecked), Extension:ImportUsers extension will ignore records in the input file which are duplicates of existing accounts.

When "Replace existing users" check box is checked, Extension:ImportUsers extension will update corresponding accounts with data from input file.

Note, that MediaWiki tolerates duplicate email addresses. In other words, there may be multiple accounts connected to the same email address. Extension:ImportUsers extension will not validate for duplicate email addresses.

Download and Installation
Step 1: Copy-paste this code into a file SpecialImportUsers.php and place it in extensions directory.

 'Import Users',  'author' =>'Yuriy Ilkiv, Rouslan Zenetl',  'description' => 'Imports users in bulk from CSV-file; encoding: UTF-8', );

$wgAvailableRights[] = 'import_users'; $wgGroupPermissions['bureaucrat']['import_users'] = true;

function wfSpecialImportUsers { global $IP, $wgMessageCache;

$wgMessageCache->addMessages(   array( 'importusers' => 'Import Users' , 'importusers_form_caption' => 'Input CSV-file (UTF-8)' , 'importusers_form_replace_present' => 'Replace existing users' , 'importusers_form_button' => 'Import' , 'importusers_user_added' => 'User %s has been added.' , 'importusers_user_present_update' => 'User %s already exists. Updated.' , 'importusers_user_present_not_update' => 'User %s already exists. Did not update.' , 'importusers_user_invalid_format' => 'User data in the line #%s has invalid format or is blank. Skipped.' , 'importusers_log' => 'Import log' , 'importusers_log_summary' => 'Summary' , 'importusers_log_summary_all' => 'All' , 'importusers_log_summary_added' => 'Added' , 'importusers_log_summary_updated' => 'Updated' ));

class SpecialImportUsers extends SpecialPage {

function SpecialImportUsers { SpecialPage::SpecialPage('ImportUsers', 'import_users' ); }   function execute( $par ) { global $wgOut, $wgUser; $wgOut->setArticleRelated( false ); if( !$wgUser->isAllowed( 'import_users' ) ) { $wgOut->permissionRequired( 'import_users' ); return; }     $wgOut->setPagetitle( wfMsg( 'importusers' ) ); if (IsSet($_FILES['users_file'])) { $wgOut->addHTML( $this->AnalizeUsers($_FILES['users_file'],IsSet($_POST['replace_present'])) ); } else { $wgOut->addHTML( $this->MakeForm ); }   }

function MakeForm { $titleObj = Title::makeTitle( NS_SPECIAL, 'ImportUsers' ); $action = $titleObj->escapeLocalURL; $output =''; $output.='User file format (csv): &lt;login-name&gt;,&lt;password&gt;,&lt;email&gt;,&lt;real-name&gt;'; $output.=' Upload file '; $output.=' '; $output.=' '; $output.=' '; return $output; }

function AnalizeUsers($fileinfo,$replace_present) { global $IP, $wgOut; require_once "$IP/includes/User.php"; $summary=array('all'=>0,'added'=>0,'updated'=>0); $filedata=explode("\n",rtrim(file_get_contents($fileinfo['tmp_name']))); $output=' '.wfMsg( 'importusers_log' ).' '; foreach ($filedata as $line=>$newuserstr) { $newuserarray=explode(',', trim( $newuserstr ) ); if (count($newuserarray)<2) { $output.=sprintf(wfMsg( 'importusers_user_invalid_format' ) ,$line+1 ).' ';         continue; }       if (!IsSet($newuserarray[2])) $newuserarray[2]=''; if (!IsSet($newuserarray[3])) $newuserarray[3]=''; $NextUser=User::newFromName( $newuserarray[0] ); $NextUser->setEmail( $newuserarray[2] ); $NextUser->setRealName( $newuserarray[3] ); $uid=$NextUser->idForName; if ($uid===0) { $NextUser->addToDatabase; $NextUser->setPassword( $newuserarray[1] ); $NextUser->saveSettings; $output.=sprintf(wfMsg( 'importusers_user_added' ) ,$newuserarray[0] ).' ';         $summary['added']++; }       else { if ($replace_present) { $NextUser->setPassword( $newuserarray[1] ); $NextUser->saveSettings; $output.=sprintf( wfMsg( 'importusers_user_present_update' ) ,$newuserarray[0] ).' ';           $summary['updated']++; }         else $output.=sprintf(wfMsg( 'importusers_user_present_not_update' ) ,$newuserarray[0] ).' ';       }        $summary['all']++; }     $output.=''.wfMsg( 'importusers_log_summary' ).' ';     $output.=wfMsg( 'importusers_log_summary_all' ).': '.$summary['all'].' ';     $output.=wfMsg( 'importusers_log_summary_added' ).': '.$summary['added'].' ';     $output.=wfMsg( 'importusers_log_summary_updated' ).': '.$summary['updated']; return $output; } }

SpecialPage::addPage (new SpecialImportUsers); }

?>

Step 2: Add the following line to LocalSettings.php:

require_once("$IP/extensions/SpecialImportUsers.php");

Thats it!

License
You are free to use this extension for any reason and mutilate it to your heart's liking. If you feel your work might benefit others, post your changes here.