Extension talk:ImportUsers/Remastering MW1-19

= Code remastering according to Jpond =

All credits go to Jpond as well as the original authors of this extension. Many thanks to them!

There are four files in the original extension. I downloaded them manually from the SVN checkout-url. The files 'ImportUser_body.php' and 'ImportUsers.i18n.php' will need to be changed.

Sorry for the messy code! This is my very first encounter with PHP and I was insecure about what goes together and why not, especially when it comes to those dots everywhere. I learned quite a few things while tinkering; among them that I hate the complexity that HTML+PHP induce. :)

This is a slightly updated version allowing to add a user to multiple groups which is what I need for my purposes.

ImportUsers.i18n.php
Change lines 11-35, containing the array '$messages['en']' by adding five lines, high-lighted in the following code.

Most likely the changes could all be grouped into one place but since I do not have much of an idea about PHP in general, I will just keep it as is.

ImportUser_body.php
Replace the file by the code below or adapt it accordingly. ';		$output .= ' '; $output .= ' '; return $output; }

function analyzeUsers( $fileinfo, $replace_present, $importusers_send_email, $importusers_add_to_group ) {
 * sm: changed function signature (two additional parameters)

global $wgEmailAuthentication ;
 * 1) Jack D. Pond added global $wgEmailAuthentication

$summary=array('all'=>0,'added'=>0,'updated'=>0,'email_sent'=>0,'email_failed'=>0);
 * 1) 	       $summary=array('all'=>0,'added'=>0,'updated'=>0);
 * 2) 		$summary = array(
 * 3) 			'all' => 0,
 * 4) 			'added' => 0,
 * 5) 			'updated' => 0
 * 6) Jack D. Pond added email_sent and email_failed counters to array
 * 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 .= 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);
 * 1) Added line to import user group assignment too

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']++; }       	                }
 * 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

$output .= 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);
 * 1) Added line to import user group assignment too

$output .= wfMsg( 'importusers-user-present-update', $newuserarray[0] ).' ';					$summary['updated']++; } else { ';					$output .= wfMsg( 'importusers-user-present-not-update', $newuserarray[0] ). ' ';				}			}			$summary['all']++; }
 * sm: fix typo in original code		$output .= wfMsg( 'importusers-user-present-no-update', $newuserarray[0] ) . '

$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'];
 * sm: line break added
 * 1) Extended by Jack D. Pond to inform importer if any email addresses didn't check out
 * 1) Extension end

return $output; }

function AddToGroup($u,$user_array,$add_to_group_checked){ global $wgOut, $wgUser; if( $wgUser->isAllowed( 'userrights' ) && $add_to_group_checked && IsSet($user_array[4])) { for( $i = 4 ; $i < sizeof( $user_array) ; $i++) { if ( in_array($user_array[ $i],User::getAllGroups)){ if ( !in_array($user_array[ $i],$u->getGroups)){ $u->addGroup( $user_array[ $i] ); }                               }                        }                }        } }
 * 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
 * sm: added loop over array-size for multiple groups