Index: trunk/extensions/ImportUsers/SpecialImportUsers.php |
— | — | @@ -0,0 +1,128 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * |
| 5 | + * @package MediaWiki |
| 6 | + * @subpackage Extensions |
| 7 | + * |
| 8 | + * @author RouslanZenetl |
| 9 | + * @author YuriyIlkiv |
| 10 | + * @license You are free to use this extension for any reason and mutilate it to your heart's liking. |
| 11 | + */ |
| 12 | + |
| 13 | +if (!defined('MEDIAWIKI')) die(); |
| 14 | +require_once "$IP/includes/SpecialPage.php"; |
| 15 | + |
| 16 | +$wgExtensionFunctions[] = 'wfSpecialImportUsers'; |
| 17 | +$wgExtensionCredits['specialpage'][] = array( |
| 18 | + 'name' => 'Import Users', |
| 19 | + 'author' => 'Yuriy Ilkiv, Rouslan Zenetl', |
| 20 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:ImportUsers', |
| 21 | + 'description' => 'Imports users in bulk from CSV-file; encoding: UTF-8', |
| 22 | +); |
| 23 | + |
| 24 | +$wgAvailableRights[] = 'import_users'; |
| 25 | +$wgGroupPermissions['bureaucrat']['import_users'] = true; |
| 26 | + |
| 27 | +function wfSpecialImportUsers() { |
| 28 | + global $IP, $wgMessageCache; |
| 29 | + |
| 30 | + $wgMessageCache->addMessages( |
| 31 | + array( |
| 32 | + 'importusers' => 'Import Users' , |
| 33 | + 'importusers_form_caption' => 'Input CSV-file (UTF-8)' , |
| 34 | + 'importusers_form_replace_present' => 'Replace existing users' , |
| 35 | + 'importusers_form_button' => 'Import' , |
| 36 | + 'importusers_user_added' => 'User <b>%s</b> has been added.' , |
| 37 | + 'importusers_user_present_update' => 'User <b>%s</b> already exists. Updated.' , |
| 38 | + 'importusers_user_present_not_update' => 'User <b>%s</b> already exists. Did not update.' , |
| 39 | + 'importusers_user_invalid_format' => 'User data in the line #%s has invalid format or is blank. Skipped.' , |
| 40 | + 'importusers_log' => 'Import log' , |
| 41 | + 'importusers_log_summary' => 'Summary' , |
| 42 | + 'importusers_log_summary_all' => 'All' , |
| 43 | + 'importusers_log_summary_added' => 'Added' , |
| 44 | + 'importusers_log_summary_updated' => 'Updated' )); |
| 45 | + |
| 46 | + class SpecialImportUsers extends SpecialPage { |
| 47 | + |
| 48 | + function SpecialImportUsers() { |
| 49 | + SpecialPage::SpecialPage('ImportUsers' , 'import_users' ); |
| 50 | + } |
| 51 | + |
| 52 | + function execute( $par ) { |
| 53 | + global $wgOut, $wgUser; |
| 54 | + $wgOut->setArticleRelated( false ); |
| 55 | + if( !$wgUser->isAllowed( 'import_users' ) ) { |
| 56 | + $wgOut->permissionRequired( 'import_users' ); |
| 57 | + return; |
| 58 | + } |
| 59 | + $wgOut->setPagetitle( wfMsg( 'importusers' ) ); |
| 60 | + if (IsSet($_FILES['users_file'])) { |
| 61 | + $wgOut->addHTML( $this->AnalizeUsers($_FILES['users_file'],IsSet($_POST['replace_present'])) ); |
| 62 | + } else { |
| 63 | + $wgOut->addHTML( $this->MakeForm() ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + function MakeForm() { |
| 68 | + $titleObj = Title::makeTitle( NS_SPECIAL, 'ImportUsers' ); |
| 69 | + $action = $titleObj->escapeLocalURL(); |
| 70 | + $output ='<form enctype="multipart/form-data" method="post" action="'.$action.'">'; |
| 71 | + $output.='<dl><dt>User file format (csv): </dt><dd>&lt;login-name&gt;,&lt;password&gt;,&lt;email&gt;,&lt;real-name&gt;</dd></dl>'; |
| 72 | + $output.='<fieldset><legend>Upload file</legend>'; |
| 73 | + $output.='<table border=0 a-valign=center width=100%>'; |
| 74 | + $output.='<tr><td align=right width=160>'.wfMsg( 'importusers_form_caption' ).': </td><td><input name="users_file" type="file" size=40 /></td></tr>'; |
| 75 | + $output.='<tr><td align=right></td><td><input name="replace_present" type="checkbox" />'.wfMsg( 'importusers_form_replace_present' ).'</td></tr>'; |
| 76 | + $output.='<tr><td align=right></td><td><input type="submit" value="'.wfMsg( 'importusers_form_button' ).'" /></td></tr>'; |
| 77 | + $output.='</table>'; |
| 78 | + $output.='</fieldset>'; |
| 79 | + $output.='</form>'; |
| 80 | + return $output; |
| 81 | + } |
| 82 | + |
| 83 | + function AnalizeUsers($fileinfo,$replace_present) { |
| 84 | + global $IP, $wgOut; |
| 85 | + require_once "$IP/includes/User.php"; |
| 86 | + $summary=array('all'=>0,'added'=>0,'updated'=>0); |
| 87 | + $filedata=explode("\n",rtrim(file_get_contents($fileinfo['tmp_name']))); |
| 88 | + $output='<h2>'.wfMsg( 'importusers_log' ).'</h2>'; |
| 89 | + foreach ($filedata as $line=>$newuserstr) { |
| 90 | + $newuserarray=explode(',', trim( $newuserstr ) ); |
| 91 | + if (count($newuserarray)<2) { |
| 92 | + $output.=sprintf(wfMsg( 'importusers_user_invalid_format' ) ,$line+1 ).'<br />'; |
| 93 | + continue; |
| 94 | + } |
| 95 | + if (!IsSet($newuserarray[2])) $newuserarray[2]=''; |
| 96 | + if (!IsSet($newuserarray[3])) $newuserarray[3]=''; |
| 97 | + $NextUser=User::newFromName( $newuserarray[0] ); |
| 98 | + $NextUser->setEmail( $newuserarray[2] ); |
| 99 | + $NextUser->setRealName( $newuserarray[3] ); |
| 100 | + $uid=$NextUser->idForName(); |
| 101 | + if ($uid===0) { |
| 102 | + $NextUser->addToDatabase(); |
| 103 | + $NextUser->setPassword( $newuserarray[1] ); |
| 104 | + $NextUser->saveSettings(); |
| 105 | + $output.=sprintf(wfMsg( 'importusers_user_added' ) ,$newuserarray[0] ).'<br />'; |
| 106 | + $summary['added']++; |
| 107 | + } |
| 108 | + else { |
| 109 | + if ($replace_present) { |
| 110 | + $NextUser->setPassword( $newuserarray[1] ); |
| 111 | + $NextUser->saveSettings(); |
| 112 | + $output.=sprintf( wfMsg( 'importusers_user_present_update' ) ,$newuserarray[0] ).'<br />'; |
| 113 | + $summary['updated']++; |
| 114 | + } |
| 115 | + else $output.=sprintf(wfMsg( 'importusers_user_present_not_update' ) ,$newuserarray[0] ).'<br />'; |
| 116 | + } |
| 117 | + $summary['all']++; |
| 118 | + } |
| 119 | + $output.='<b>'.wfMsg( 'importusers_log_summary' ).'</b><br />'; |
| 120 | + $output.=wfMsg( 'importusers_log_summary_all' ).': '.$summary['all'].'<br />'; |
| 121 | + $output.=wfMsg( 'importusers_log_summary_added' ).': '.$summary['added'].'<br />'; |
| 122 | + $output.=wfMsg( 'importusers_log_summary_updated' ).': '.$summary['updated']; |
| 123 | + return $output; |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + SpecialPage::addPage (new SpecialImportUsers()); |
| 129 | +} |
Property changes on: trunk/extensions/ImportUsers/SpecialImportUsers.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 130 | + native |