From 73fcaa7ae7f14bc2c3c3907a65cbd5b4c91d7fb7 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Fri, 18 Jun 2010 21:51:23 +0000 Subject: [PATCH] Users importer implemented; --- Builder/IISMainHandler/build.txt | 2 +- Builder/IISMainHandler/product.wxs | 18 ++++- Common/actions/AbstractChange.cs | 2 +- Common/actions/AbstractFieldValue.cs | 2 +- Common/actions/ChangeSet.cs | 1 + Common/actions/ChangeSetUtil.cs | 8 +- Common/actions/InsertChange.cs | 2 +- Common/actions/ReferenceFieldValue.cs | 2 +- Common/actions/ScalarFieldValue.cs | 2 +- Common/dataobjects/User.cs | 10 +++ FLocal.sln | 8 ++ .../handlers/request/MigrateAccountHandler.cs | 2 +- ImportConsole/ImportConsole.csproj | 74 +++++++++++++++++++ ImportConsole/Program.cs | 56 ++++++++++++++ ImportConsole/Properties/AssemblyInfo.cs | 36 +++++++++ Importer/ShallerGateway.cs | 47 +++++++++++- MySQLConnector/PostgresDBTraits.cs | 2 +- 17 files changed, 258 insertions(+), 16 deletions(-) create mode 100644 ImportConsole/ImportConsole.csproj create mode 100644 ImportConsole/Program.cs create mode 100644 ImportConsole/Properties/AssemblyInfo.cs diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index 565f1b0..eaea6f5 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -231 \ No newline at end of file +240 \ No newline at end of file diff --git a/Builder/IISMainHandler/product.wxs b/Builder/IISMainHandler/product.wxs index 2e49201..eb4d903 100644 --- a/Builder/IISMainHandler/product.wxs +++ b/Builder/IISMainHandler/product.wxs @@ -55,6 +55,12 @@ + + + + + + + \ No newline at end of file diff --git a/ImportConsole/Program.cs b/ImportConsole/Program.cs new file mode 100644 index 0000000..fd27d8f --- /dev/null +++ b/ImportConsole/Program.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using FLocal.Importer; +using FLocal.Common.actions; +using FLocal.Common.dataobjects; +using FLocal.Core; +using System.Configuration; + +namespace FLocal.ImportConsole { + class Program { + static void Main(string[] args) { + + if(!FLocal.Common.Config.isInitialized) { + lock(typeof(FLocal.Common.Config)) { + if(!FLocal.Common.Config.isInitialized) { + FLocal.Common.Config.Init(ConfigurationManager.AppSettings); + } + } + } + + for(int i=1; i<800; i++) { + Console.Write("[" + i + "]"); + foreach(string userName in ShallerGateway.getUserNames(i)) { + Console.Write("."); + Dictionary userData = ShallerGateway.getUserInfo(userName); + AbstractChange addUser = new InsertChange( + User.TableSpec.instance, + new Dictionary() { + { User.TableSpec.FIELD_NAME, new ScalarFieldValue(userName) }, + { User.TableSpec.FIELD_REGDATE, new ScalarFieldValue(DateTime.Parse(userData["regDate"]).ToUTCString()) }, + { User.TableSpec.FIELD_LOCATION, new ScalarFieldValue(userData["location"]) }, + { User.TableSpec.FIELD_SHOWPOSTSTOUSERS, new ScalarFieldValue("All") }, + { User.TableSpec.FIELD_SIGNATURE, new ScalarFieldValue(userData["signature"]) }, + { User.TableSpec.FIELD_TITLE, new ScalarFieldValue(userData["title"]) }, + { User.TableSpec.FIELD_TOTALPOSTS, new ScalarFieldValue("0") }, + { User.TableSpec.FIELD_USERGROUPID, new ScalarFieldValue("1") }, + { User.TableSpec.FIELD_BIOGRAPHY, new ScalarFieldValue(userData["biography"]) }, + } + ); + AbstractChange addAccount = new InsertChange( + Account.TableSpec.instance, + new Dictionary() { + { Account.TableSpec.FIELD_NAME, new ScalarFieldValue(userName) }, + { Account.TableSpec.FIELD_NEEDSMIGRATION, new ScalarFieldValue("1") }, + { Account.TableSpec.FIELD_PASSWORDHASH, new ScalarFieldValue("*") }, + { Account.TableSpec.FIELD_USERID, new ReferenceFieldValue(addUser) }, + } + ); + ChangeSetUtil.ApplyChanges(addUser, addAccount); + } + } + } + } +} diff --git a/ImportConsole/Properties/AssemblyInfo.cs b/ImportConsole/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..68f7cd7 --- /dev/null +++ b/ImportConsole/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ImportConsole")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("ImportConsole")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2010")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("6c8ecdee-da08-44af-b450-2af25abf48cf")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Importer/ShallerGateway.cs b/Importer/ShallerGateway.cs index ae5fb56..eb006c5 100644 --- a/Importer/ShallerGateway.cs +++ b/Importer/ShallerGateway.cs @@ -2,13 +2,58 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; +using System.Web; namespace FLocal.Importer { public class ShallerGateway { - public static string getUserInfo(string userName) { + public static string getUserInfoAsString(string userName) { return ShallerConnector.getPageContent("showprofile.php?User=" + userName + "&What=login&showlite=l", new Dictionary(), new System.Net.CookieContainer()); } + private static Dictionary regexInfoCache = new Dictionary(); + private static Regex getInfoRegexByCaption(string caption) { + if(!regexInfoCache.ContainsKey(caption)) { + lock(caption) { + if(!regexInfoCache.ContainsKey(caption)) { + regexInfoCache[caption] = new Regex("]*>\\s*" + caption + "\\s*\\s*\\s*([^<>]*)\\s*", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline); + } + } + } + return regexInfoCache[caption]; + } + + private static Dictionary userImportStructure { + get { + return new Dictionary() { + { "regDate", "Дата\\s+регистрации" }, + { "signature", "Подпись" }, + { "title", "Титул" }, + { "location", "Расположение" }, + { "biography", "Биография" }, + }; + } + } + + public static Dictionary getUserInfo(string userName) { + string content = getUserInfoAsString(userName); + return userImportStructure.ToDictionary, string, string>( + kvp => kvp.Key, + kvp => HttpUtility.HtmlDecode(getInfoRegexByCaption(kvp.Value).Match(content).Groups[1].Value).Trim() + ); + } + + public static IEnumerable getUserNames(int pageNum) { + string content = ShallerConnector.getPageContent("showmembers.php?Cat=&sb=13&page=" + pageNum + "&showlite=l", new Dictionary(), new System.Net.CookieContainer()); + Regex matcher = new Regex(";User=([^&]+)&", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase); + MatchCollection matches = matcher.Matches(content); + HashSet result = new HashSet(); + foreach(Match match in matches) { + result.Add(match.Groups[1].Value); + } + return result; + } + } } diff --git a/MySQLConnector/PostgresDBTraits.cs b/MySQLConnector/PostgresDBTraits.cs index 067aef1..33400df 100644 --- a/MySQLConnector/PostgresDBTraits.cs +++ b/MySQLConnector/PostgresDBTraits.cs @@ -22,7 +22,7 @@ namespace FLocal.MySQLConnector { newCommand.Transaction = command.Transaction; } newCommand.CommandType = System.Data.CommandType.Text; - newCommand.CommandText = "SELECT CURRVAL(" + this.escapeIdentifier(sequenceName) + ")"; + newCommand.CommandText = "SELECT CURRVAL('" + this.escapeIdentifier(sequenceName) + "')"; return (long)newCommand.ExecuteScalar(); } }