Extension talk:ImportUsers/Archive

Version 0.0.3

 * Blank line handling corrected (used to produce error message)
 * Added "UTF-8" to upload control label to suggest that import file should be UTF-8 encoded

Version 0.0.2

 * Initial public release

Compatible with Version 1.8.2
Hi, I wanted to know if this Extension is also working with MediaWiki 1.8.2.

Please contact me: claudia.oswald@gmail.com

THX

Added features to code - only tested with 1.9.3
I added some functionality to this version - and don't know whether I should post as new or just let the original authors decide. These features allow users to be imported, notified, and automatically assigned to groups as they are loaded. --Jpond 22:16, 27 March 2007 (UTC)

The additional features are:


 * 1) Gives option to send email notifying added users as they are imported (if notification is enabled on wiki)
 * 2) Add user to specified group when adding if box is checked

For imported user to be added to a group the following must be true:


 * 1) Logged on user importing must have 'userrights'
 * 2) The user must have checked the "Add users to groups" box
 * 3) The CSV line must have a group to add to(e.g. approved)
 * 4) The permission requested must be one of the available groups - does not create a new group
 * 5) The user has not already been assigned to that group

If you wanted to add the user(s) to more than one group, you could rerun the import after changing the group in the CSV file, then use the 'Update existing users' checkbox.

The CSV now has up to five columns (first 2 required):


 * 1) username
 * 2) password
 * 3) email
 * 4) real name
 * 5) add to this group [must be existing group]

Delimited with commas. For an example:

user1,pass1,user1@gmail.com,User One, Group 1 user2,pass2,user2@gmail.com,User Two, Group 2 user3,pass3,user3@gmail.com,User Three, Group 3 user4,pass4,user4@gmail.com,User Four, Group 4 user5,pass5,user5@gmail.com,User Five, Group 5 user6,pass6,user6@gmail.com,User Six, Group 6 . . . userN,passN,userN@gmail.com,User Nnn, Group nnn

New, Modified Code - Tested on MediaWiki V 1.9.3
--Jpond 23:11, 27 March 2007 (UTC)

 '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' => 'Update existing users ' , 'importusers_form_send_email' => 'Send Email to Users' , 'importusers_form_add_to_group' => 'Add users to groups (must have userrights)' , '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_user_invalid_email' => 'User data in the line #%s has invalid email 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', 'importusers_log_summary_email_sent' => 'Email Notifications Sent' , 'importusers_log_summary_email_failed' => 'Email Notifications Failed' ));
 * 1) Added by Jack D. pond
 * 1) End Add
 * 1) Jack D. Pond added next line for invalid email notification
 * 1) Jack D. Pond added next 2 lines for email notification counts

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']),IsSet($_POST['send_email']),IsSet($_POST['add_to_group'])) ); } else { $wgOut->addHTML( $this->MakeForm ); }   }

function MakeForm { $titleObj = Title::makeTitle( NS_SPECIAL, 'ImportUsers' ); $action = $titleObj->escapeLocalURL; $output =''; $output.='User file format (csv): ,, ,'; $output.=' Upload file '; $output.=' '; $output.=' '; $output.=' '; return $output; }

function AnalizeUsers($fileinfo,$replace_present,$importusers_send_email,$importusers_add_to_group) { global $IP, $wgOut, $wgEmailAuthentication ;
 * 1) Jack D. Pond added global $wgEmailAuthentication

require_once "$IP/includes/User.php"; $summary=array('all'=>0,'added'=>0,'updated'=>0,'email_sent'=>0,'email_failed'=>0);
 * 1) Jack D. Pond added email_sent and email_failed counters to array

$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; $this->AddToGroup($NextUser,$newuserarray,$importusers_add_to_group); if( $wgEmailAuthentication && $importusers_send_email && User::isValidEmailAddr( $NextUser->getEmail ) ) {				global $wgOut; $error = $NextUser->sendConfirmationMail; if( WikiError::isError( $error ) ) {					$output.=sprintf(wfMsg( 'importusers_user_invalid_email' ) ,$line+1 ).' ';					$summary['email_failed']++; } else {					$summary['email_sent']++; }			}         $output.=sprintf(wfMsg( 'importusers_user_added' ) ,$newuserarray[0] ).' ';         $summary['added']++; }       else { if ($replace_present) { $NextUser->setPassword( $newuserarray[1] ); $NextUser->saveSettings; $this->AddToGroup($NextUser,$newuserarray,$importusers_add_to_group); $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'].' ';     $output.=wfMsg( 'importusers_log_summary_email_sent' ).': '.$summary['email_sent'].' ';     $output.=wfMsg( 'importusers_log_summary_email_failed' ).': '.$summary['email_failed']; return $output; }
 * 1) Added line to import user group assignment too
 * 1) Extended by Jack D. Pond to send email notifications to users (if enabled)
 * 1) May want to delete here to end of extend if not using email addresses in import
 * 1) End Extend
 * 1) Added line to import user group assignment too
 * 1) Extended by Jack D. Pond to inform importer if any email addresses didn't check out
 * 1) Extension end

function AddToGroup($u,$user_array,$add_to_group_checked){ global $wgOut, $wgUser; if( $wgUser->isAllowed( 'userrights' ) && $add_to_group_checked && IsSet($user_array[4])) { if ( in_array($user_array[4],User::getAllGroups)){ if ( !in_array($user_array[4],$u->getGroups)){ $u->addGroup( $user_array[4] ); }			}		}	} }
 * 1) Extension by Jack D. Pond that adds imported user to the group in the last parameter in the line (4)
 * 2) under the following conditions:
 * 3) Logged on user importing must have 'userrights'
 * 4) The user must have checked the "Add users to groups" box
 * 5) The CSV line must have a group to add to(e.g. approved)
 * 6) The permission requested must be one of the available groups - does not create a new group
 * 7) The user has not already been assigned to that group
 * 1) The user has not already been assigned to that group

SpecialPage::addPage (new SpecialImportUsers); }

?>

Added Bonus - MS Excel Template for creating CSV with password generator
I had this and thought it might be of use to others.

The second tab is a template for creating csv file using MS Excel. You would use this template and use the file->save as->type=(.csv). Gives a couple of funky messages - but you'll be able to figure those out.

The first tab will actually generate random (or at least as random as MS Excel will allow) passwords. Code is signed.

Hope this helps someone else out. --Jpond 01:29, 29 March 2007 (UTC)