forked from nmlgc/musicroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiupdate.cpp
572 lines (459 loc) · 13.8 KB
/
wikiupdate.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Music Room Interface
// --------------------
// wikiupdate.cpp - Touhou Wiki Track Info Updating
// --------------------
// "©" Nmlgc, 2010-2011
// "©" DTM9025, 2024
#include <bgmlib/platform.h>
#include <bgmlib/list.h>
#include <bgmlib/config.h>
#include <bgmlib/infostruct.h>
#include <bgmlib/bgmlib.h>
#include <bgmlib/ui.h>
#include <bgmlib/utils.h>
#include <fxascii.h>
#include <FXRex.h>
#include <curl/curl.h>
// Wiki stuff to remove
static const FXString Face = "''";
static const FXString Tmpl[2] = {"{{", "}}"};
static const FXString Link[2] = {"[[", "]]"};
static const FXString HTML[2] = {"<", ">"};
static const FXString Space(" ");
static const FXint regexp_mode(FXRex::Normal|FXRex::Capture|FXRex::IgnoreCase|FXRex::Newline);
// Lots of wiki normalization functions
// Returns the end of the template starting after [t]
static FXint GetBounds(FXString& Str, FXint* s, FXint t, const FXString& markup_open, const FXString& markup_close)
{
FXint n = 0; // Template nest counter
if(s) *s = Str.find('|', t);
// Don't stumble over nested templates
for(n = 1; (t < Str.length() - 1) && (n != 0); t++)
{
if(!compare(&Str[t], markup_open, markup_open.length())) n++;
else if(!compare(&Str[t], markup_close, markup_close.length())) n--;
}
Str[t-1] = '|';
return t;
}
size_t write_data(char* File, size_t size, size_t nmemb, FXString* Page)
{
Page->append(File, size * nmemb);
return size * nmemb;
}
void RemoveWikiLinks(FXString& Str, const FXString& s_del = Link[0], const FXString& t_del = Link[1])
{
FXint s = 0, t = 0, c = 0, len;
while((s = Str.find(s_del, s)) != -1)
{
t = Str.find('|', s);
c = Str.find(t_del, s);
if((uint)t < (uint)c) len = t - s + 1;
else len = t_del.length();
Str.erase(s, len);
Str.erase(c - len, t_del.length());
}
}
FXRex::Error RemoveTemplate(FXString& Str, const FXString& Name)
{
FXint s = 0, t = 0;
FXRex Rex;
FXRex::Error Ret;
if(Ret = Rex.parse(Name, regexp_mode)) return Ret;
while(Rex.match(Str, &s, &t, FXRex::Forward, 1, s))
{
Str.erase(s, t-s);
t = GetBounds(Str, NULL, s, Tmpl[0], Tmpl[1]);
Str.erase(t-1, 2);
}
return Ret;
}
FXRex::Error RemoveLastElm(FXString& Str, const FXString& Name)
{
FXint s = 0, t = 0;
FXString Sub;
FXRex Rex;
FXRex::Error Ret;
if(Ret = Rex.parse(Name, regexp_mode)) return Ret;
while(Rex.match(Str, &s, &t, FXRex::Forward, 1, s))
{
Str.erase(s, t-s);
t = GetBounds(Str, NULL, s, Tmpl[0], Tmpl[1]);
if( -1 != (s = Str.find('|', s)) ) Str.erase(s, t+1-s);
else Str.erase(t-1, 2);
}
return Ret;
}
void RemoveHTMLPairs(FXString& Str, const FXString& markup_open, const FXString& markup_close)
{
FXint s = 0, t = 0;
FXRex regexp_html[2];
regexp_html[0].parse(markup_open + ".*?" + markup_close, regexp_mode);
regexp_html[1].parse(markup_open + "/.*?" + markup_close, regexp_mode);
for(ushort c = 0; c < 2; c++)
{
while(regexp_html[c].match(Str, &s, &t, FXRex::Forward, 1, s))
{
Str.erase(s, t-s);
}
}
}
// Translate MediaWiki indents to [Repl]
static void TranslateIndents(FXString& WD, const FXString& Repl = Space)
{
if(WD.empty()) return;
FXint s = 0, t;
const FXchar Indent = ':';
FXRex regexp_indent("^:", regexp_mode);
while(regexp_indent.match(WD, &s, &t, FXRex::Forward, 1, s))
{
while(WD[s] == Indent)
{
WD.replace(s, 1, Repl.text(), Repl.length());
s += Repl.length();
}
}
}
inline FXString TemplateElm(const FXString& WD, const FXString& Tag)
{
FXString Ret(NamedValue(WD, Tag, "=", "|"));
return Ret.trim();
}
inline FXString TemplateElm_Comment(const FXString& WD, const FXString& Tag)
{
FXString Ret(NamedValue(WD, Tag, "=", "|"));
TranslateIndents(Ret);
return Ret.trimEnd();
}
FXuint AskForUpdate(TrackInfo* TI, const FXString& Type, FXString* Old, FXString* New, FXuint* Ret, bool ShowStat = true)
{
if(New->empty() || *Ret == UPDATE_NOALL) return *Ret;
// Normalize new-lines.
FXString o = *Old, n = *New;
o.substitute("\r\n", "\n");
n.substitute("\r\n", "\n");
if(o == n) return *Ret;
FXString Msg;
if(*Ret != UPDATE_YESALL && !Old->empty())
{
Msg.format("Update %s on Track #%d?\n", Type, TI->Number);
*Ret = BGMLib::UI_Update(Msg, *Old, *New);
if(*Ret != UPDATE_YES && *Ret != UPDATE_YESALL) return *Ret;
}
if(ShowStat) Msg.format(" --> #%d: %s -> %s\n", TI->Number, *Old, *New);
else Msg.format(" --> #%d: updated %s\n", TI->Number, Type);
BGMLib::UI_Stat(Msg);
*Old = *New; // The actual assignment!
return *Ret;
}
// Compares two strings, returns false if one is empty
bool CompareEx(FXString& s1, FXString s2)
{
if(s1.empty() || s2.empty()) return false;
return !comparecase(s1,s2);
}
static TrackInfo* Eval_MusicRoom(GameInfo* GI, FXString& WD, FXuint* MsgRet)
{
static const FXString Wiki_Category("category");
static const FXString Wiki_Name[LANG_COUNT] = {"title", "titleEN"};
static const FXString Wiki_Comment[LANG_COUNT] = {"comment", "translation"};
static const FXString Wiki_Source("source");
FXString Category;
IntString Name, Comment;
FXuint TrackNo;
List<IntString> Afterword;
ListEntry<IntString>* TrackCmt, *NewCmt;
IntString* TC, *NC;
FXString SrcDesc, SrcVal;
TrackInfo* Cur = NULL;
ushort s = 1, l;
removeSub(WD, Tmpl[1]);
Category = TemplateElm(WD, Wiki_Category);
TrackNo = Category.after('#').toUInt();
for(l = 0; l < LANG_COUNT; l++)
{
Name[l] = TemplateElm(WD, Wiki_Name[l]); removeSub(Name[l], LineBreak[1]);
Comment[l] = TemplateElm_Comment(WD, Wiki_Comment[l]);
}
// Multiple source handling
do
{
// JP comment
SrcVal.format("%s%d", Wiki_Comment[LANG_JP], s);
SrcVal = TemplateElm_Comment(WD, SrcVal);
// Because some comments skip the first number.
if(SrcVal.empty() && s == 1)
{
s++;
continue;
}
if(SrcVal.empty()) break;
NC = &Afterword.Add()->Data;
NC->s[LANG_JP].assign(SrcVal);
// EN comment
SrcVal.format("%s%d", Wiki_Comment[LANG_EN], s);
NC->s[LANG_EN] = TemplateElm_Comment(WD, SrcVal);
// Source (if present)
SrcDesc.format("%s%d", Wiki_Source, s);
SrcDesc = TemplateElm(WD, SrcDesc);
if(!SrcDesc.empty())
{
SrcDesc.prepend('('); SrcDesc.append(")\n");
NC->s[LANG_JP].prepend(SrcDesc);
NC->s[LANG_EN].prepend(SrcDesc);
}
s++;
}
while(1);
ListEntry<TrackInfo>* CurTrack;
if(TrackNo) CurTrack = GI->Track.Get(TrackNo - 1);
else CurTrack = GI->Track.First();
for(ushort Temp = 0; (Temp < GI->TrackCount) && CurTrack; Temp++) // No spoilers for trial versions! :-)
{
Cur = &(CurTrack->Data);
if(CompareEx(Cur->Name[LANG_JP], Name[LANG_JP]) || CompareEx(Cur->Name[LANG_EN], Name[LANG_EN]))
{
// OK, found the right track
for(l = 0; l < LANG_COUNT; l++)
{
if(AskForUpdate(Cur, BGMLib::LI[l].GUILang + " title", &Cur->Name[l], &Name[l], MsgRet) == UPDATE_CANCEL) return Cur;
if(AskForUpdate(Cur, BGMLib::LI[l].GUILang + " comment", &Cur->Comment[l], &Comment[l], MsgRet, false) == UPDATE_CANCEL) return Cur;
}
NewCmt = Afterword.First();
TrackCmt = Cur->Afterword.First();
while(NewCmt)
{
NC = &NewCmt->Data;
SrcDesc = NC->s[0].left(NC->s[0].find('\n'));
if(!TrackCmt) TrackCmt = Cur->Afterword.Add();
TC = &TrackCmt->Data;
if(!SrcDesc.empty()) SrcDesc.prepend(" ");
SrcDesc.prepend(" supplementary comment");
for(l = 0; l < LANG_COUNT; l++)
{
if(AskForUpdate(Cur, BGMLib::LI[l].GUILang + SrcDesc, &TC->s[l], &NC->s[l], MsgRet, false) == UPDATE_CANCEL) return Cur;
}
NewCmt = NewCmt->Next();
TrackCmt = TrackCmt->Next();
}
CurTrack = NULL;
}
else CurTrack = CurTrack->Next();
}
return Cur;
}
static void Eval_MusicRoom_Cmt(TrackInfo* TI, const ushort& Index, FXString& WD, FXuint* MsgRet)
{
static const FXString Wiki_Cmt[LANG_COUNT] = {"ja", "en"};
FXString New;
FXString Caption;
ListEntry<IntString>* TrackCmt = TI->Afterword.Get(Index);
if(!TrackCmt) TrackCmt = TI->Afterword.Add();
Caption.format(" comment (paragraph #%d)", Index + 1);
for(ushort l = 0; l < LANG_COUNT; l++)
{
New = TemplateElm_Comment(WD, Wiki_Cmt[l]);
if(AskForUpdate(TI, BGMLib::LI[l].GUILang + Caption, &TrackCmt->Data.s[l], &New, MsgRet, false) == UPDATE_CANCEL) return;
}
return;
}
bool Download(CURL* C, const FXString& URL, FXString* Buffer)
{
FXString Str;
curl_easy_setopt(C, CURLOPT_WRITEDATA, Buffer);
curl_easy_setopt(C, CURLOPT_URL, URL);
CURLcode CRet = curl_easy_perform(C);
if(CRet != CURLE_OK)
{
Str = curl_easy_strerror(CRet);
Str.prepend("\nERROR: ");
Str.append('\n');
BGMLib::UI_Stat(Str);
return false;
}
return true;
}
static bool Update_Finish(CURL* C);
static bool Update_Cancel(CURL* C);
static bool Update_Complete(CURL* C);
static const FXString Wiki_MusicRoom(Tmpl[0] + "MusicRoom");
static const FXString Wiki_MusicRoom_Cmt(Wiki_MusicRoom + "/Cmt");
bool Update(GameInfo* GI, FXString& WikiURL)
{
if(GI->WikiPage.empty()) return false;
bool Ret;
FXuint MsgRet;
FXString URL, Page, Str;
FXint s = 0, t, Index = 0;
FXint p = 0;
CURL* C = NULL;
TrackInfo* TI = NULL;
ListEntry<IntString>* TrackCmt = NULL;
ulong RevID;
URL = WikiURL;
if( (s = URL.find("/wiki")) != -1) URL.trunc(s);
Str.format("Getting track info from (%s).\n (%s)... ", URL, GI->WikiPage);
BGMLib::UI_Stat(Str);
curl_global_init(CURL_GLOBAL_ALL);
C = curl_easy_init();
curl_easy_impersonate(C, "chrome116", 1);
curl_easy_setopt(C, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(C, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(C, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(C, CURLOPT_WRITEFUNCTION, write_data);
DL:
GI->WikiPage.substitute(' ', '_');
URL.format(WikiURL.text(), GI->WikiPage);
if(!Download(C, URL, &Page)) return Update_Finish(C);
// Get revision id
// ---------------
if((s = Page.find("<revision>")) != 1)
{
s = Page.find("<id>", s) + 4;
t = Page.find("</id>", s);
RevID = Page.mid(s, t - s).toULong();
}
if(RevID == GI->WikiRev)
{
BGMLib::UI_Stat("\nTrack info is already up-to-date.\n");
return Update_Finish(C);
}
// ---------------
// Prepare buffer
// --------------
if((s = Page.find("<text ", s)) == -1)
{
BGMLib::UI_Stat("\nWiki data is corrupted, update not possible!");
return Update_Finish(C);
}
Page = Page.right(Page.length() - s);
s = 0;
// Resolve Wiki redirects
// ----------------------
Str = NamedValue(Page, "#REDIRECT", "[[", "]]");
if(!Str.empty())
{
GI->WikiPage = Str;
Str.format("\nResolving #REDIRECT to (%s)...", GI->WikiPage);
BGMLib::UI_Stat(Str);
goto DL;
}
// ----------------------
BGMLib::UI_Stat("\n ...done, evaluating...\n");
removeSub(Page, Face);
RemoveTemplate(Page, "\\{\\{lang\\|.*?\\|");
// Remove references
{
FXRex regexp_ref;
if(!regexp_ref.parse("\\{\\{ref\\|.*?\\}\\}", regexp_mode))
{
while(regexp_ref.match(Page, &s, &t, FXRex::Forward, 1, s))
{
Page.erase(s, t-s);
}
}
}
RemoveLastElm(Page, "\\{\\{ruby-.*?\\|");
RemoveWikiLinks(Page);
// Empty space in some comments
const char Empty[2] = {LineBreak[1], ' '};
FXchar Space[4] = {' ', Moonspace[0], Moonspace[1], Moonspace[2]}; // STUPID "IDEOGRAPHIC WHITE SPACE"
Page.substitute(Empty, 2, &LineBreak[1], 1, true);
Page.substitute(Space, 4, Space, 1, true);
Space[0] = '='; Page.substitute(Space, 4, Space, 1, true);
Space[0] = LineBreak[1]; Page.substitute(Space, 4, Space, 1, true);
Page.substitute(" ", " ");
// HTML
Page.substitute(">", ">");
Page.substitute("<", "<");
// Remove other references
{
FXRex regexp_ref;
if(!regexp_ref.parse("<ref>.*?</ref>", regexp_mode))
{
while (regexp_ref.match(Page, &s, &t, FXRex::Forward, 1, s))
{
Page.erase(s, t-s);
}
}
}
// Too less time for a proper fix right now
// TODO: Investigate linebreak behavior in Touhou Wiki
Page.substitute("<br />\n", "\n"); // Hopefully this renders stuff correctly.
Page.substitute("<br />", "\n");
// Magical Astronomy...
{
FXRex regexp_sup("<sup>", regexp_mode);
FXRex regexp_external("\\[.*? ", regexp_mode);
s = 0;
// Closing tag gets deleted automatically below
while(regexp_sup.match(Page, &s, &t, FXRex::Forward, 1, s))
{
Page.replace(s, t-s, "^", 1);
}
s = 0;
while(regexp_external.match(Page, &s, &t, FXRex::Forward, 1, s))
{
Page.erase(s, t-s);
if( (s = Page.find(']', s)) != -1) Page.erase(s);
}
}
RemoveHTMLPairs(Page, HTML[0], HTML[1]);
Page.substitute(""", "\"");
// --------------
// Start processing...
s = t = p = 0;
FXRex regexp_musicroom("\\{\\{MusicRoom(?!/Cmt)", regexp_mode);
FXRex regexp_musicroom_cmt("\\{\\{MusicRoom/Cmt", regexp_mode);
Ret = regexp_musicroom.match(Page, &s, &t, FXRex::Forward, 1, p);
// s = start position of current template (temporary)
// t = (end) position of next MusicRoom template to evaluate
// p = end position of a MusicRoom/Cmt template
while(Ret)
{
t = GetBounds(Page, &s, t, Tmpl[0], Tmpl[1]);
TI = Eval_MusicRoom(GI, Page.mid(s, t-s), &MsgRet);
p = t;
if(MsgRet == UPDATE_CANCEL) return Update_Cancel(C);
else if(MsgRet == UPDATE_NOALL) break;
// Next MusicRoom
if(!(Ret = regexp_musicroom.match(Page, &s, &t, FXRex::Forward, 1, p))) t = Page.length();
if(!TI) continue;
// If we find a table end before the next MusicRoom template,
// we may probably have some {{MusicRoom/Cmt}}s there
s = Page.find("|}", p + 1);
if(s < t)
{
Index = 0;
while(regexp_musicroom_cmt.match(Page, &s, &p, FXRex::Forward, 1, p, t))
{
p = GetBounds(Page, &s, p, Tmpl[0], Tmpl[1]);
Eval_MusicRoom_Cmt(TI, Index, Page.mid(s, p-s), &MsgRet);
Index++;
if(MsgRet == UPDATE_CANCEL) return Update_Cancel(C);
else if(MsgRet == UPDATE_NOALL) Ret = false;
}
}
}
GI->WikiRev = RevID;
return Update_Complete(C);
}
static bool Update_Finish(CURL* C)
{
BGMLib::UI_Stat("\n");
curl_easy_cleanup(C);
curl_global_cleanup();
return false;
}
static bool Update_Cancel(CURL* C)
{
BGMLib::UI_Stat("Update canceled.\n");
Update_Finish(C);
return false;
}
static bool Update_Complete(CURL* C)
{
BGMLib::UI_Stat("Update completed.\n");
Update_Finish(C);
return true;
}