-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataModulesExporter.cs
164 lines (142 loc) · 6.33 KB
/
DataModulesExporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Luaon;
using Luaon.Json;
using Newtonsoft.Json;
using Serilog;
using WikibaseClientLite.ModuleExporter.ObjectModel;
using WikiClientLibrary.Wikibase;
using WikiClientLibrary.Wikibase.DataTypes;
using Formatting = Luaon.Formatting;
namespace WikibaseClientLite.ModuleExporter
{
public class DataModulesExporter
{
private static readonly string[] defaultLanguages = {"en-us", "en"};
public DataModulesExporter(ILogger logger)
{
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public string ClientSiteName { get; set; }
public IList<string> Languages { get; set; }
public ILogger Logger { get; }
private void WriteProlog(TextWriter writer, string prologText)
{
writer.WriteLine("----------------------------------------");
writer.WriteLine("-- Powered by WikibaseClientLite");
writer.Write("-- ");
writer.WriteLine(prologText);
writer.WriteLine("----------------------------------------");
writer.WriteLine();
writer.Write("local data = ");
}
private void WriteEpilog(TextWriter writer)
{
writer.WriteLine();
writer.WriteLine();
writer.WriteLine("return data");
}
private static WbMonolingualTextCollection FilterMonolingualTexts(WbMonolingualTextCollection collection, ICollection<string> languages)
{
var c = new WbMonolingualTextCollection();
foreach (var l in languages)
{
c[l] = collection[l];
}
return c;
}
private static WbMonolingualTextsCollection FilterMonolingualTexts(WbMonolingualTextsCollection collection, ICollection<string> languages)
{
var c = new WbMonolingualTextsCollection();
foreach (var l in languages)
{
c[l] = collection[l];
}
return c;
}
public async Task ExportItemsAsync(TextReader itemsDumpReader, LuaModuleFactory moduleFactory)
{
if (itemsDumpReader == null) throw new ArgumentNullException(nameof(itemsDumpReader));
if (moduleFactory == null) throw new ArgumentNullException(nameof(moduleFactory));
var languages = new List<string>(Languages ?? defaultLanguages);
int items = 0, properties = 0;
foreach (var entity in SerializableEntity.LoadAll(itemsDumpReader))
{
if (entity.Type == EntityType.Item) items++;
else if (entity.Type == EntityType.Property)
properties++;
// Preprocess
entity.Labels = FilterMonolingualTexts(entity.Labels, languages);
entity.Descriptions = FilterMonolingualTexts(entity.Descriptions, languages);
entity.Aliases = FilterMonolingualTexts(entity.Aliases, languages);
// Persist
using (var module = moduleFactory.GetModule(entity.Id))
{
using (var writer = module.Writer)
{
WriteProlog(writer, $"Entity: {entity.Id} ({entity.Labels["en"]})");
using (var luawriter = new JsonLuaWriter(writer) {CloseOutput = false})
{
entity.WriteTo(luawriter);
}
WriteEpilog(writer);
}
await module.SubmitAsync($"Export entity {entity.Id}.");
}
if ((items + properties) % 500 == 0)
{
Logger.Information("Exported LUA modules for {Items} items and {Properties} properties.", items, properties);
}
}
Logger.Information("Exported LUA modules for {Items} items and {Properties} properties.", items, properties);
}
public async Task ExportSiteLinksAsync(TextReader itemsDumpReader, LuaModuleFactory moduleFactory, int shardCount)
{
if (itemsDumpReader == null) throw new ArgumentNullException(nameof(itemsDumpReader));
if (moduleFactory == null) throw new ArgumentNullException(nameof(moduleFactory));
if (shardCount <= 0) throw new ArgumentOutOfRangeException(nameof(shardCount));
if (ClientSiteName == null) throw new ArgumentNullException(nameof(ClientSiteName));
var shards = Enumerable.Range(0, shardCount).Select(index =>
{
var module = moduleFactory.GetModule(index.ToString());
WriteProlog(module.Writer, $"Shard: {index + 1}/{shardCount}");
return module;
}).ToList();
var shardLuaWriters = shards.Select(m =>
new LuaTableTextWriter(m.Writer) {CloseWriter = false, Formatting = Formatting.Prettified})
.ToList();
foreach (var writer in shardLuaWriters) writer.WriteStartTable();
try
{
foreach (var entity in SerializableEntity.LoadAll(itemsDumpReader))
{
var siteLink = entity.SiteLinks.FirstOrDefault(l => l.Site == ClientSiteName);
if (siteLink == null) continue;
var shardIndex = Utility.HashString(siteLink.Title) % shardCount;
var writer = shardLuaWriters[shardIndex];
writer.WriteKey(siteLink.Title);
writer.WriteLiteral(entity.Id);
}
Logger.Information("Exporting LUA modules. Shards = {Shards}", shards.Count);
for (var i = 0; i < shards.Count; i++)
{
shardLuaWriters[i].WriteEndTable();
shardLuaWriters[i].Close();
WriteEpilog(shards[i].Writer);
await shards[i].SubmitAsync($"Export SiteLink table. Shard {i + 1}/{shards.Count}.");
}
}
finally
{
foreach (var s in shards) s.Dispose();
}
}
}
}