Extension talk:ImportUsers/Archive 1
History
editVersion 0.0.3
edit- 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
edit- Initial public release
Compatible with Version 1.8.2
editHi, I wanted to know if this Extension is also working with MediaWiki 1.8.2.
Please contact me: claudia.oswald@gmail.com
THX
Tested with 1.10 - OK!
edit- a single test on a siteground hosted site. - works fine -
- needed to make the minor fix to the message that displays the data format (see below).
Added features to code - only tested with 1.9.3
editI 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:
- Gives option to send email notifying added users as they are imported (if notification is enabled on wiki)
- 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:
- Logged on user importing must have 'userrights'
- The user must have checked the "Add users to groups" box
- The CSV line must have a group to add to(e.g. approved)
- The permission requested must be one of the available groups - does not create a new group
- 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):
- username
- password
- real name
- 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
edit--Jpond 23:11, 27 March 2007 (UTC)
Code |
---|
<?php
if (!defined('MEDIAWIKI')) die();
require_once "$IP/includes/SpecialPage.php";
$wgExtensionFunctions[] = 'wfSpecialImportUsers';
$wgExtensionCredits['specialpage'][] = array(
'name' => '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 ' ,
# Added by Jack D. pond
'importusers_form_send_email' => 'Send Email to Users' ,
'importusers_form_add_to_group' => 'Add users to groups (must have userrights)' ,
# End Add
'importusers_form_button' => 'Import' ,
'importusers_user_added' => 'User <b>%s</b> has been added.' ,
'importusers_user_present_update' => 'User <b>%s</b> already exists. Updated.' ,
'importusers_user_present_not_update' => 'User <b>%s</b> already exists. Did not update.' ,
'importusers_user_invalid_format' => 'User data in the line #%s has invalid format or is blank. Skipped.' ,
# Jack D. Pond added next line for invalid email notification
'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',
# Jack D. Pond added next 2 lines for email notification counts
'importusers_log_summary_email_sent' => 'Email Notifications Sent' ,
'importusers_log_summary_email_failed' => 'Email Notifications Failed' ));
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 ='<form enctype="multipart/form-data" method="post" action="'.$action.'">';
$output.='<dl><dt>User file format (csv): </dt><dd><login-name>,<password>,<email>,<real-name></dd></dl>';
$output.='<fieldset><legend>Upload file</legend>';
$output.='<table border=0 a-valign=center width=100%>';
$output.='<tr><td align=right width=160>'.wfMsg( 'importusers_form_caption' ).': </td><td><input name="users_file" type="file" size=40 /></td></tr>';
# Jack D. Pond changed line to add send email and ask if add user to groups (if in file)
$output.='<tr><td align=right></td><td><input name="replace_present" type="checkbox" />'.wfMsg( 'importusers_form_replace_present' ).'<input name="send_email" type="checkbox" />'.wfMsg( 'importusers_form_send_email' ).'</td></tr>';
$output.='<tr><td align=right></td><td><input name="add_to_group" type="checkbox" />'.wfMsg( 'importusers_form_add_to_group' ).'</td></tr>';
# End change
$output.='<tr><td align=right></td><td><input type="submit" value="'.wfMsg( 'importusers_form_button' ).'" /></td></tr>';
$output.='</table>';
$output.='</fieldset>';
$output.='</form>';
return $output;
}
function AnalizeUsers($fileinfo,$replace_present,$importusers_send_email,$importusers_add_to_group) {
global $IP, $wgOut, $wgEmailAuthentication ;
# Jack D. Pond added global $wgEmailAuthentication
require_once "$IP/includes/User.php";
# Jack D. Pond added email_sent and email_failed counters to array
$summary=array('all'=>0,'added'=>0,'updated'=>0,'email_sent'=>0,'email_failed'=>0);
$filedata=explode("\n",rtrim(file_get_contents($fileinfo['tmp_name'])));
$output='<h2>'.wfMsg( 'importusers_log' ).'</h2>';
foreach ($filedata as $line=>$newuserstr) {
$newuserarray=explode(',', trim( $newuserstr ) );
if (count($newuserarray)<2) {
$output.=sprintf(wfMsg( 'importusers_user_invalid_format' ) ,$line+1 ).'<br />';
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();
# Added line to import user group assignment too
$this->AddToGroup($NextUser,$newuserarray,$importusers_add_to_group);
# Extended by Jack D. Pond to send email notifications to users (if enabled)
if( $wgEmailAuthentication && $importusers_send_email && User::isValidEmailAddr( $NextUser->getEmail() ) )
{
global $wgOut;
$error = $NextUser->sendConfirmationMail();
# May want to delete here to end of extend if not using email addresses in import
if( WikiError::isError( $error ) )
{
$output.=sprintf(wfMsg( 'importusers_user_invalid_email' ) ,$line+1 ).'<br />';
$summary['email_failed']++;
} else
{
$summary['email_sent']++;
}
}
# End Extend
$output.=sprintf(wfMsg( 'importusers_user_added' ) ,$newuserarray[0] ).'<br />';
$summary['added']++;
}
else {
if ($replace_present) {
$NextUser->setPassword( $newuserarray[1] );
$NextUser->saveSettings();
# Added line to import user group assignment too
$this->AddToGroup($NextUser,$newuserarray,$importusers_add_to_group);
$output.=sprintf( wfMsg( 'importusers_user_present_update' ) ,$newuserarray[0] ).'<br />';
$summary['updated']++;
}
else $output.=sprintf(wfMsg( 'importusers_user_present_not_update' ) ,$newuserarray[0] ).'<br />';
}
$summary['all']++;
}
$output.='<b>'.wfMsg( 'importusers_log_summary' ).'</b><br />';
$output.=wfMsg( 'importusers_log_summary_all' ).': '.$summary['all'].'<br />';
$output.=wfMsg( 'importusers_log_summary_added' ).': '.$summary['added'].'<br />';
$output.=wfMsg( 'importusers_log_summary_updated' ).': '.$summary['updated'].'<br />';
# Extended by Jack D. Pond to inform importer if any email addresses didn't check out
$output.=wfMsg( 'importusers_log_summary_email_sent' ).': '.$summary['email_sent'].'<br />';
$output.=wfMsg( 'importusers_log_summary_email_failed' ).': '.$summary['email_failed'];
# Extension end
return $output;
}
# Extension by Jack D. Pond that adds imported user to the group in the last parameter in the line (4)
# under the following conditions:
#
# Logged on user importing must have 'userrights'
# The user must have checked the "Add users to groups" box
# The CSV line must have a group to add to(e.g. approved)
# The permission requested must be one of the available groups - does not create a new group
# The user has not already been assigned to that group
#
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] );
}
}
}
}
}
SpecialPage::addPage (new SpecialImportUsers());
}
?>
|
See below for a complete remastering of the code which integrates the changes presented here.
Notice for above code as of 10:02, 1 July 2013 done by Sm8ps (info added by Ency (talk) 07:10, 20 April 2014 (UTC))
Minor Code Edit
editThe line:
$output.='<dl><dt>User file format (csv): </dt><dd><login-name>,<password>,<email>,<real-name></dd></dl>';
was being rendered as:
$output.='<dl><dt>User file format (csv): </dt><dd><login-name>,<password>,<email>,<real-name></dd></dl>';
which, when copy-pasted out, was causing the user file format to appear as ",,," on the Special:ImportUsers page. I edited it to read:
$output.='<dl><dt>User file format (csv): </dt><dd>&lt;login-name&gt;,&lt;password&gt;,&aamp;mp;lt;email&gt;,&lt;real-name&gt;</dd></dl>';
so it would render correctly when copy-pasted.
Header information already sent bug. (WM 1.11) Solved
editMy mediawiki 1.11 gives errors:
Warning: Cannot modify header information - headers already sent by (output started at */wiki/extensions/ImportUser/SpecialImportUser.php:1) in */wiki/includes/WebResponse.php on line 10
Could not find anything relating to this warning... I know production servers have warnings mostly disabled, but imho good script should never yeld warnings.
Anybody suggestions?
Kaspera 12:10, 22 October 2007 (UTC)
- Solved after reading this article about a byte-order mark issue. This solved many other issues with headers already sent. Hope this helps other a lot of headache! Kaspera 09:25, 27 November 2007 (UTC)
Problem running with MW 1.11.0
editI copied the code from the extension page, and followed the installation instructions. I run and get the error below.
The website cannot display the page
HTTP 500
Most likely causes:
* The website is under maintenance. * The website has a programming error.
I checked the logs, but there are no details there. Any suggestions on how to fix this?
TIA Alex Abramov (alexa at avid-tech dot com)
Version Info: MediaWiki: 1.11.0 PHP: 5.2.3 (cgi-fcgi) MySQL: 5.0.24-community-nt
Problems with 1.12 final
editI have an incompatibility with my MW 1.12 final version. When I require the ImportUsers extension, my MediaWiki crashes every time, showing only a blank page. I verified the path... I don't think the problem is on my side.--Rmatt 17:14, 21 March 2008 (UTC)
Same here, it looks for ExtensionFunctions.php, does not find it and aborts. fix it by putting http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ExtensionFunctions.php into your Extensions folder. 193.175.73.201 14:28, 28 March 2008 (UTC)
- Thanks, it solved the problem for me too !
Importing with password field containing a comma (,)
edit--Hiloboys 15:44, 11 August 2009 (HST)
Media Wiki: 1.14.0
It works great, but I ran into this 1 problem which I think probably is something easy to figure out I just have been banging my head against my table so much and have probably lost the basic brain cells that contain the answer.
Id say about 3/4 of my users have a comma (,) in their assigned password they been using for years. (yes cant make em change it without confusing 1000s of them and getting tons of tech support emails.) and as you know having a comma in CSV format is just like telling it to move to the next block or field.
Normally I would use username,"example,password",email,name which would normally fix the cell when using CSV format. However, when I try to do this, there is still no avail and ceases to function properly.
The account is create and is added to my database, however when trying to log in using the password containing the comma, it will say incorrect password. Ive tried over 10 times as many differ ways as I can think of.
Then I created an account using this method without the comma in the password and it worked just fine.
Using Excel saved in .csv format (Comma Delimited not mac or MS-DOS) Also tried in just plain .txt format with comma's separating each field.
Password cannot be equal to username
editMedia Wiki 1.13
I tried to batch enter a lot of usernames, and since I wanted something simple for password, I just entered the username a second time. Apparently this does not work, as when I changed the passwords to a different value, suddenly it worked. Ironically if I only had one record in my .csv file, it did work, but then did not work for more than one. Unfortunately this made me think it was something involved with my file format which it turns out was not the case.
Troubleshooting
editI made my CVS file in Excel and the plugin didnt work cause the line break was '\r' and not '\n'. I edited the _body.php file and it worked perfect.
Installation
editWorks fine in MW 1.18 alpha, but the code in LocalSettings.php has to be
require_once("$IP/extensions/ImportUsers/ImportUsers.php");
(without the "Special") - or am I missing something? --TnEaM 16:40, 14 March 2011 (UTC)
I downloaded version fro MW 1.16 from Extension page linke, but was not able to get the posted require statement to work with MW 1.16. The error was that the script set was looking for a file named "SpecialImportUsers.php" instead of "ImportUsers.php". I changed the statement to this, and it worked:
require_once("$IP/extensions/ImportUsers/SpecialImportUsers.php");
Hope this helps. Should the main Extension Page for this tool be updated? I'm pretty much a yucker at coding, so changed main page, and then realized it may just be specific to my instance, so reverted and posted solution here. - Johncn (jconcilus@bssd.org)
Running 1.16 and the correct snapshot for that MW version. Mac users in our organization had issues with CSV files created in latest Excel. Only the first record would be imported. The rest were ignored. We suspected line break mismatches or encoding. The workaround for us was to open the file in BBEdit, and save it. This healed the file, and we used Line Breaks: Unix and Encoding Unicode UTF-8 setting under "Save As". Now works like a charm. - Johncn (jconcilus@bssd.org)
User Creation Log
editThis extension does not seem to make entries in the user creation log. Is this expected or is something broken in my installation? Would this functionality be hard to add?--John.james 03:10, 17 October 2010 (UTC)
Added Features
editMr. Pond posted a number of excellent added features in 2007. I see that this extension was updated a few months ago, but none of those features were added according to the Extension page. Do the developers have plans to add these features or to continue providing support for this extension? --Jlemley 05:53, 17 May 2011 (UTC)
Warning: Don't Quote Fields!
editAlthough it's fairly standard in CSV files, placing double-quotes around your field values results in unexpected behavior with this extension: the double-quotes are treated as part of the value! (This likely also explains the problem reported above with passwords containing commas: since the double-quotes aren't honored, the comma in the password is treated as a delimiter even if you quoted the password.) Szarka (talk) 21:58, 17 March 2012 (UTC)
Works with MW 1.20.4
editWorks with MW 1.20.4, but you have to choose UTF-8 without BOM encoding. If you use UTF-8, the user was created, but I wasn't able to log in or assign some rights. The username in the database looks like this: username. I don`t know if this is common knowledge out there, but it took me some time to figure it out, so I thought it might be helpful for some other newbie like me. :-) --Dadai12 14:23, 26 June 2013 (UTC)
Code remastering according to Jpond: add to group, notify by e-mail, MW-1.19
editI could successfully integrate the above code by Jpond into the original extension. The additional features seem to work well so far on MediaWiki 1.19.7.
As an additional feature it is now possible to add users to several groups. All entries after the user name will be interpreted as groups to which the newly created user will be added. The groups are checked for existence so that such a mistake would not result in a fatal error.
Furthermore it seems possible to add more groups to users by running the extension again with the "replace users" button checked. (The user does not have to be deleted or such. I just had it logged out of the wiki.) My test case ran fine but YMMV. Removing a group does not work, though.
I put the remastered code in a sub-page in order to keep things tidy around here. Please add comments here, especially if you found this code working on your particular version of MediaWiki.
19:45, 1st and 3rd July 2013 Sm8ps(info added by Ency (talk) 06:38, 20 April 2014 (UTC))
Jpond's/Sm8ps's improvements have been incorporated into the core of extension in version 1.3.0 of 16 June 2014 Ency (talk) 19:37, 7 January 2015 (UTC) |