koishi-plugin-oni-sync-bot 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -3
- package/dist/style.css +1 -1
- package/lib/config.d.ts +49 -0
- package/lib/index.d.ts +3 -9
- package/lib/index.js +1356 -1345
- package/lib/plugins/databaseExtension.d.ts +0 -1
- package/lib/plugins/queryCommands.d.ts +12 -17
- package/lib/plugins/routeRedirect.d.ts +4 -10
- package/lib/plugins/syncCommands.d.ts +9 -28
- package/lib/plugins/todoList.d.ts +5 -4
- package/lib/plugins/updateCommands.d.ts +4 -8
- package/lib/services/consoleLogProvider.d.ts +5 -1
- package/lib/services/messageRenderer.d.ts +58 -0
- package/lib/services/syncNotifier.d.ts +18 -0
- package/lib/services/wikiBotService.d.ts +37 -18
- package/lib/sync/imgSync.d.ts +36 -22
- package/lib/sync/moduleSync.d.ts +29 -16
- package/lib/sync/pageSync.d.ts +71 -44
- package/lib/utils/async.d.ts +19 -0
- package/lib/utils/matcher.d.ts +40 -0
- package/lib/utils/tools.d.ts +5 -1
- package/package.json +9 -3
- package/readme.md +225 -223
- package/lib/plugins/consoleLogProvider.d.ts +0 -13
- package/lib/plugins/synccontributor.d.ts +0 -15
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
import { Context
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Context } from "koishi";
|
|
2
|
+
import type { SiteConfig } from "../config";
|
|
3
|
+
/** 只用到共享站点配置里的 domain */
|
|
4
|
+
export type QueryCommandsConfig = Pick<SiteConfig, "domain">;
|
|
5
|
+
/** `x` / `查wiki` 命令:精准匹配 + 拼音模糊匹配 + 序号选择 */
|
|
5
6
|
export declare class QueryCommands {
|
|
6
7
|
static readonly inject: string[];
|
|
7
8
|
config: QueryCommandsConfig;
|
|
8
9
|
private ctx;
|
|
10
|
+
private renderer;
|
|
9
11
|
constructor(ctx: Context, config: QueryCommandsConfig);
|
|
10
12
|
private registerCommands;
|
|
11
|
-
|
|
12
|
-
private buildLinkButton;
|
|
13
|
-
private buildQQKeyboard;
|
|
14
|
-
private sendResponse;
|
|
15
|
-
private renderUsageMessage;
|
|
16
|
-
private buildResultKeyboard;
|
|
17
|
-
private renderResultMessage;
|
|
13
|
+
/** 标题 / 全拼 / 首字母三种精准匹配,命中即返回(带用于展示的前缀文案) */
|
|
18
14
|
private findPreciseMatch;
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
/**
|
|
16
|
+
* 处理多候选选择:
|
|
17
|
+
* - QQ:直接回按钮列表,结束;
|
|
18
|
+
* - 其它平台:提示输入序号并等待 15 秒。
|
|
19
|
+
*/
|
|
21
20
|
private handleSelection;
|
|
22
21
|
}
|
|
23
|
-
export declare namespace QueryCommands {
|
|
24
|
-
const Config: Schema<QueryCommandsConfig>;
|
|
25
|
-
}
|
|
26
|
-
export declare function apply(ctx: Context, config: QueryCommandsConfig): void;
|
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
import { Context
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
bwiki_site: string;
|
|
6
|
-
}
|
|
1
|
+
import { Context } from "koishi";
|
|
2
|
+
import type { SiteConfig } from "../config";
|
|
3
|
+
/** 站点配置来自共享分组,键名与默认值见 src/config.ts */
|
|
4
|
+
export type RouteRedirectConfig = SiteConfig;
|
|
7
5
|
export declare class RouteRedirect {
|
|
8
6
|
static readonly inject: string[];
|
|
9
7
|
config: RouteRedirectConfig;
|
|
10
8
|
constructor(ctx: Context, config: RouteRedirectConfig);
|
|
11
9
|
}
|
|
12
|
-
export declare namespace RouteRedirect {
|
|
13
|
-
const Config: Schema<RouteRedirectConfig>;
|
|
14
|
-
}
|
|
15
|
-
export declare function apply(ctx: Context, config: RouteRedirectConfig): void;
|
|
@@ -1,39 +1,20 @@
|
|
|
1
|
-
import { Context
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
domain: string;
|
|
9
|
-
main_site: string;
|
|
10
|
-
bwiki_site: string;
|
|
11
|
-
notifyGroupIds?: string[];
|
|
12
|
-
notifyBotPlatform?: string;
|
|
13
|
-
}
|
|
1
|
+
import { Context } from "koishi";
|
|
2
|
+
import type { NotifyConfig } from "../config";
|
|
3
|
+
/**
|
|
4
|
+
* 实际只消费日志地址与通知字段;账号与站点配置由其它子插件使用,
|
|
5
|
+
* 因此这里不再重复声明(详见 src/config.ts)。
|
|
6
|
+
*/
|
|
7
|
+
export type SyncCommandsConfig = NotifyConfig;
|
|
14
8
|
export declare class SyncCommands {
|
|
15
9
|
static readonly inject: string[];
|
|
16
10
|
config: SyncCommandsConfig;
|
|
17
11
|
private notifyFn?;
|
|
18
12
|
constructor(ctx: Context, config: SyncCommandsConfig);
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
* 当配置了 notifyGroupIds 时,将同步成功的页面/图片合并成一条消息推送到指定 QQ 群
|
|
22
|
-
*/
|
|
23
|
-
private buildNotifyFn;
|
|
24
|
-
/**
|
|
25
|
-
* 确保机器人就绪,如果未就绪则尝试重新登录
|
|
26
|
-
* @param ctx Koishi 上下文
|
|
27
|
-
* @param taskName 任务名称(用于日志)
|
|
28
|
-
* @returns 机器人是否已就绪
|
|
29
|
-
*/
|
|
13
|
+
/** 同步相关的定时任务;cron 表达式即调度策略,集中在一处便于对照 */
|
|
14
|
+
private registerScheduledTasks;
|
|
30
15
|
private ensureBotsReady;
|
|
31
16
|
private registerCommands;
|
|
32
17
|
private getContributorsAndTimestamp;
|
|
33
18
|
private syncSingleContributor;
|
|
34
19
|
private syncAllContributors;
|
|
35
20
|
}
|
|
36
|
-
export declare namespace SyncCommands {
|
|
37
|
-
const Config: Schema<SyncCommandsConfig>;
|
|
38
|
-
}
|
|
39
|
-
export declare function apply(ctx: Context, config: SyncCommandsConfig): void;
|
|
@@ -39,14 +39,15 @@ export declare class TodoList {
|
|
|
39
39
|
constructor(ctx: Context);
|
|
40
40
|
private registerCronJobs;
|
|
41
41
|
private getAllModulesFromGG;
|
|
42
|
-
private sleep;
|
|
43
42
|
private apiCallWithRetry;
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
/**
|
|
44
|
+
* 读取指定站点上模块页面的最新修订信息。
|
|
45
|
+
* 原先 gg / bwiki 各有一份逐行相同的实现,只有站点不同 —— 合并为按站点寻址。
|
|
46
|
+
*/
|
|
47
|
+
private getModuleRevision;
|
|
46
48
|
private syncModulesToTodos;
|
|
47
49
|
private checkSyncStatus;
|
|
48
50
|
private getCreatedBy;
|
|
49
51
|
private registerCommands;
|
|
50
52
|
private registerConsoleEvents;
|
|
51
53
|
}
|
|
52
|
-
export declare function apply(ctx: Context): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Context, Logger
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Context, Logger } from "koishi";
|
|
2
|
+
import type { NotifyConfig, ScheduleConfig } from "../config";
|
|
3
|
+
/** 日志地址来自 NotifyConfig,cron 表达式来自 ScheduleConfig(见 src/config.ts) */
|
|
4
|
+
export interface UpdateCommandsConfig extends Pick<NotifyConfig, "logsUrl">, ScheduleConfig {
|
|
5
5
|
}
|
|
6
6
|
export declare class UpdateCommands {
|
|
7
7
|
ctx: Context;
|
|
@@ -13,7 +13,3 @@ export declare class UpdateCommands {
|
|
|
13
13
|
private updatePageCache;
|
|
14
14
|
private registerCommands;
|
|
15
15
|
}
|
|
16
|
-
export declare namespace UpdateCommands {
|
|
17
|
-
const Config: Schema<UpdateCommandsConfig>;
|
|
18
|
-
}
|
|
19
|
-
export declare function apply(ctx: Context, config: UpdateCommandsConfig): void;
|
|
@@ -8,6 +8,10 @@ declare module "@koishijs/console" {
|
|
|
8
8
|
}
|
|
9
9
|
export declare class ConsoleLogProvider {
|
|
10
10
|
static readonly inject: string[];
|
|
11
|
+
/**
|
|
12
|
+
* 日志缓冲。刻意做成实例状态而非模块级变量:
|
|
13
|
+
* 模块级可变数组是全进程共享的单例,多实例或热重载时会互相串数据。
|
|
14
|
+
*/
|
|
15
|
+
private readonly buffer;
|
|
11
16
|
constructor(ctx: Context);
|
|
12
17
|
}
|
|
13
|
-
export declare function apply(ctx: Context): void;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Context, Session } from "koishi";
|
|
2
|
+
import type { MatchResult } from "../utils/matcher";
|
|
3
|
+
export interface LinkButton {
|
|
4
|
+
id: string;
|
|
5
|
+
render_data: {
|
|
6
|
+
label: string;
|
|
7
|
+
visited_label: string;
|
|
8
|
+
style: number;
|
|
9
|
+
};
|
|
10
|
+
action: {
|
|
11
|
+
type: number;
|
|
12
|
+
permission: {
|
|
13
|
+
type: number;
|
|
14
|
+
};
|
|
15
|
+
data: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** QQ 键盘(按钮组) */
|
|
19
|
+
export interface Keyboard {
|
|
20
|
+
content: {
|
|
21
|
+
rows: Array<{
|
|
22
|
+
buttons: LinkButton[];
|
|
23
|
+
}>;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** 渲染页面信息所需的最小结构(WikiPages 结构上兼容) */
|
|
27
|
+
export interface RenderablePage {
|
|
28
|
+
id: number;
|
|
29
|
+
title: string;
|
|
30
|
+
contributor?: string;
|
|
31
|
+
change_time?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 负责“把回复内容按平台渲染并发送”。
|
|
35
|
+
*
|
|
36
|
+
* 平台差异集中在这里:
|
|
37
|
+
* - **QQ**:走 markdown + 按钮键盘,通过 `bot.internal.sendMessage` 主动发送,
|
|
38
|
+
* 成功后命令不再返回内容;发送失败则降级为返回纯文本。
|
|
39
|
+
* - **其它平台**:只返回纯文本,由 Koishi 作为命令返回值发送。
|
|
40
|
+
*/
|
|
41
|
+
export declare class MessageRenderer {
|
|
42
|
+
private readonly ctx;
|
|
43
|
+
private readonly domain;
|
|
44
|
+
constructor(ctx: Context, domain: string);
|
|
45
|
+
isQQ(session?: Session): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* 发送(或返回)回复内容。
|
|
48
|
+
* @returns QQ 平台发送成功时返回 undefined;其它情况返回可直接发给用户的纯文本
|
|
49
|
+
*/
|
|
50
|
+
send(session: Session | undefined, content: string, keyboard?: Keyboard): Promise<string | undefined>;
|
|
51
|
+
usageMessage(session?: Session): Promise<string | undefined>;
|
|
52
|
+
resultMessage(session: Session | undefined, page: RenderablePage, prefix?: string): Promise<string | undefined>;
|
|
53
|
+
/** 命中多个候选时的按钮键盘(每行 2 个,跳 bwiki 镜像) */
|
|
54
|
+
matchListKeyboard(matches: MatchResult[]): Keyboard;
|
|
55
|
+
/** 单个页面结果的按钮键盘:原站 / 镜像 / 直链 */
|
|
56
|
+
pageKeyboard(page: RenderablePage): Keyboard;
|
|
57
|
+
private linkButton;
|
|
58
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Context } from "koishi";
|
|
2
|
+
import type { SyncNotifyItem } from "../sync/pageSync";
|
|
3
|
+
/**
|
|
4
|
+
* 把增量更新中同步成功的条目合并成一条消息,推送到配置的群频道。
|
|
5
|
+
*
|
|
6
|
+
* 未配置 `groupIds` 时视为关闭(`enabled` 为 false):调用方据此不注册回调,
|
|
7
|
+
* 避免在不使用通知时仍然产生「找不到可用 bot」之类的告警。
|
|
8
|
+
*/
|
|
9
|
+
export declare class SyncNotifier {
|
|
10
|
+
private readonly ctx;
|
|
11
|
+
private readonly groupIds;
|
|
12
|
+
private readonly platform;
|
|
13
|
+
constructor(ctx: Context, groupIds?: string[], platform?: string);
|
|
14
|
+
get enabled(): boolean;
|
|
15
|
+
notify(items: SyncNotifyItem[]): Promise<void>;
|
|
16
|
+
private activeBots;
|
|
17
|
+
private formatMessage;
|
|
18
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Context, Service
|
|
1
|
+
import { Context, Service } from "koishi";
|
|
2
|
+
import type { AccountConfig } from "../config";
|
|
2
3
|
import { Mwn } from "mwn";
|
|
3
4
|
declare module "koishi" {
|
|
4
5
|
interface Context {
|
|
@@ -12,18 +13,23 @@ export interface ISiteConfig {
|
|
|
12
13
|
password: string;
|
|
13
14
|
userAgent: string;
|
|
14
15
|
}
|
|
16
|
+
export type WikiSite = "gg" | "bwiki";
|
|
15
17
|
export declare class WikiBotService extends Service {
|
|
16
|
-
static readonly inject:
|
|
17
|
-
ggbot
|
|
18
|
-
bwikibot
|
|
19
|
-
|
|
18
|
+
static readonly inject: never[];
|
|
19
|
+
private ggbot;
|
|
20
|
+
private bwikibot;
|
|
21
|
+
private botProxies;
|
|
20
22
|
private botConfig;
|
|
21
23
|
private static readonly USER_AGENT;
|
|
22
24
|
private static readonly MAX_RETRIES;
|
|
23
25
|
private static readonly RETRY_DELAY;
|
|
24
26
|
constructor(ctx: Context, config: WikiBotServiceConfig);
|
|
27
|
+
/** 站点显示名,避免到处写 `site === "gg" ? "WIKIGG" : "bwiki"` */
|
|
28
|
+
private static siteLabel;
|
|
29
|
+
private botOf;
|
|
30
|
+
private setBot;
|
|
31
|
+
private siteConfigOf;
|
|
25
32
|
private getSitesConfig;
|
|
26
|
-
private delay;
|
|
27
33
|
private loginWithRetry;
|
|
28
34
|
private login;
|
|
29
35
|
start(): Promise<void>;
|
|
@@ -32,22 +38,35 @@ export declare class WikiBotService extends Service {
|
|
|
32
38
|
gg: boolean;
|
|
33
39
|
bwiki: boolean;
|
|
34
40
|
}>;
|
|
41
|
+
/**
|
|
42
|
+
* 确保两个站点都已登录:任一未就绪时先尝试重新登录。
|
|
43
|
+
* 就绪状态属于本服务的职责,调用方只消费结果。
|
|
44
|
+
*/
|
|
45
|
+
ensureReady(): Promise<{
|
|
46
|
+
gg: boolean;
|
|
47
|
+
bwiki: boolean;
|
|
48
|
+
}>;
|
|
49
|
+
/** 登录单个站点;失败不抛错、只返回是否成功(顺序调用以保持原有时序) */
|
|
50
|
+
private loginSite;
|
|
35
51
|
private reloginSite;
|
|
52
|
+
/** 两个站点是否至少有一个可用(派生状态,不需要手工同步) */
|
|
53
|
+
get isReady(): boolean;
|
|
36
54
|
isGGBotReady(): boolean;
|
|
37
55
|
isBWikiBotReady(): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* 生成一个"登录态永远有效"的客户端代理。
|
|
58
|
+
*
|
|
59
|
+
* 设计说明(为什么必须是代理而不是普通对象):
|
|
60
|
+
* Mwn 的方法很多,且调用方需要 `continuedQueryGen` 这类**异步生成器**以及
|
|
61
|
+
* `options` 这类**非函数属性**。用代理可以完整转发任意成员,同时把
|
|
62
|
+
* "assert=user 失败 → 重新登录 → 重放调用" 这件事对调用方完全隐藏。
|
|
63
|
+
*/
|
|
38
64
|
private createBotProxy;
|
|
39
|
-
|
|
40
|
-
private bwikibotProxy;
|
|
65
|
+
/** WIKIGG 客户端(透明处理登录过期) */
|
|
41
66
|
getGGBot(): Mwn;
|
|
67
|
+
/** bwiki 客户端(透明处理登录过期) */
|
|
42
68
|
getBWikiBot(): Mwn;
|
|
69
|
+
private getBot;
|
|
43
70
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
ggPassword: string;
|
|
47
|
-
bwikiusername: string;
|
|
48
|
-
bwikipassword: string;
|
|
49
|
-
}
|
|
50
|
-
export declare namespace WikiBotService {
|
|
51
|
-
const Config: Schema<WikiBotServiceConfig>;
|
|
52
|
-
}
|
|
53
|
-
export declare function apply(ctx: Context, config: WikiBotServiceConfig): void;
|
|
71
|
+
/** 账号配置来自共享分组,键名与默认值见 src/config.ts */
|
|
72
|
+
export type WikiBotServiceConfig = AccountConfig;
|
package/lib/sync/imgSync.d.ts
CHANGED
|
@@ -1,30 +1,44 @@
|
|
|
1
1
|
import { Mwn } from "mwn";
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
/** 图片同步的可调参数 */
|
|
3
|
+
export interface ImageSyncOptions {
|
|
4
|
+
/** 忽略的图片名 */
|
|
5
|
+
ignoredImages: string[];
|
|
6
|
+
/** 同步成功后等待毫秒数 */
|
|
7
|
+
intervalSuccess: number;
|
|
8
|
+
/** 同步失败后等待毫秒数 */
|
|
9
|
+
intervalFailed: number;
|
|
10
|
+
/** 上传时的注释 */
|
|
11
|
+
uploadComment: string;
|
|
12
|
+
/** 上传时的授权说明文本 */
|
|
13
|
+
uploadText: string;
|
|
14
|
+
}
|
|
15
|
+
export interface SyncResult {
|
|
16
|
+
success: boolean;
|
|
17
|
+
reason?: string;
|
|
18
|
+
}
|
|
10
19
|
interface ImageInfo {
|
|
11
20
|
url: string;
|
|
12
21
|
sha1: string;
|
|
13
22
|
size?: number;
|
|
14
23
|
}
|
|
24
|
+
/** 与 PageSyncer / ModuleSyncer 保持一致的构造签名 */
|
|
25
|
+
export interface ImageSyncerDependencies {
|
|
26
|
+
options?: Partial<ImageSyncOptions>;
|
|
27
|
+
}
|
|
15
28
|
/**
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
declare function getImageInfo(site: Mwn, fileName: string): Promise<ImageInfo | null>;
|
|
19
|
-
/**
|
|
20
|
-
* 同步单个图片
|
|
21
|
-
*/
|
|
22
|
-
declare function syncSingleImage(sourceBot: Mwn, targetBot: Mwn, fileName: string, config: Config): Promise<{
|
|
23
|
-
success: boolean;
|
|
24
|
-
reason?: string;
|
|
25
|
-
}>;
|
|
26
|
-
/**
|
|
27
|
-
* 批量同步所有图片(带失败重试)
|
|
29
|
+
* 把 wiki.gg 的图片文件同步到 bwiki。
|
|
30
|
+
* 依赖全部由构造函数注入,可安全地实例化多份。
|
|
28
31
|
*/
|
|
29
|
-
declare
|
|
30
|
-
|
|
32
|
+
export declare class ImageSyncer {
|
|
33
|
+
private readonly source;
|
|
34
|
+
private readonly target;
|
|
35
|
+
private readonly options;
|
|
36
|
+
constructor(source: Mwn, target: Mwn, deps?: ImageSyncerDependencies);
|
|
37
|
+
getImageInfo(site: Mwn, fileName: string): Promise<ImageInfo | null>;
|
|
38
|
+
private deleteOldVersions;
|
|
39
|
+
syncOne(fileName: string): Promise<SyncResult>;
|
|
40
|
+
listAll(): Promise<string[]>;
|
|
41
|
+
/** 批量同步所有图片:先串行同步一轮,再重试失败的 */
|
|
42
|
+
syncAll(): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
package/lib/sync/moduleSync.d.ts
CHANGED
|
@@ -1,21 +1,34 @@
|
|
|
1
1
|
import { Mwn } from "mwn";
|
|
2
|
-
|
|
3
|
-
* 同步单个模块
|
|
4
|
-
* @param oldSite 原站点
|
|
5
|
-
* @param newSite 新站点
|
|
6
|
-
* @param moduleTitle 模块标题
|
|
7
|
-
* @param user 触发同步的用户
|
|
8
|
-
* @returns
|
|
9
|
-
*/
|
|
10
|
-
declare function syncSingleModule(oldSite: Mwn, newSite: Mwn, moduleTitle: string, user?: string): Promise<{
|
|
2
|
+
export interface SyncResult {
|
|
11
3
|
success: boolean;
|
|
12
4
|
reason?: string;
|
|
13
|
-
}
|
|
5
|
+
}
|
|
6
|
+
/** 模块同步的可调参数 */
|
|
7
|
+
export interface ModuleSyncOptions {
|
|
8
|
+
/** 模块命名空间 ID */
|
|
9
|
+
moduleNamespace: number;
|
|
10
|
+
/** 忽略的模块标题 */
|
|
11
|
+
ignoredModules: string[];
|
|
12
|
+
/** 同步成功后等待毫秒数 */
|
|
13
|
+
intervalSuccess: number;
|
|
14
|
+
/** 同步失败后等待毫秒数 */
|
|
15
|
+
intervalFailed: number;
|
|
16
|
+
}
|
|
17
|
+
/** 与 PageSyncer / ImageSyncer 保持一致的构造签名 */
|
|
18
|
+
export interface ModuleSyncerDependencies {
|
|
19
|
+
options?: Partial<ModuleSyncOptions>;
|
|
20
|
+
}
|
|
14
21
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @param newSite 新站点
|
|
18
|
-
* @returns
|
|
22
|
+
* 把 wiki.gg 的 Module 命名空间页面同步到 bwiki。
|
|
23
|
+
* 依赖全部由构造函数注入,可安全地实例化多份。
|
|
19
24
|
*/
|
|
20
|
-
declare
|
|
21
|
-
|
|
25
|
+
export declare class ModuleSyncer {
|
|
26
|
+
private readonly source;
|
|
27
|
+
private readonly target;
|
|
28
|
+
private readonly options;
|
|
29
|
+
constructor(source: Mwn, target: Mwn, deps?: ModuleSyncerDependencies);
|
|
30
|
+
syncOne(moduleTitle: string, user?: string): Promise<SyncResult>;
|
|
31
|
+
listAll(): Promise<string[]>;
|
|
32
|
+
/** 批量同步所有模块:先串行同步一轮,再重试失败的 */
|
|
33
|
+
syncAll(): Promise<void>;
|
|
34
|
+
}
|
package/lib/sync/pageSync.d.ts
CHANGED
|
@@ -1,43 +1,18 @@
|
|
|
1
1
|
import { Mwn } from "mwn";
|
|
2
|
-
import { Config } from "../index";
|
|
3
|
-
export interface Database {
|
|
4
|
-
set(table: string, query: object, data: object): Promise<any>;
|
|
5
|
-
get<T = any>(table: string, query: object): Promise<T[]>;
|
|
6
|
-
create(table: string, data: object): Promise<any>;
|
|
7
|
-
upsert(table: string, data: object | object[]): Promise<any>;
|
|
8
|
-
}
|
|
9
2
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
3
|
+
* pageSync 需要的数据库能力(结构化的最小接口)。
|
|
4
|
+
*
|
|
5
|
+
* 说明:`wikipages` 已经通过 `src/plugins/databaseExtension.ts` 里的 `Tables`
|
|
6
|
+
* 接口增强声明过,但 `onitodos` 没有。这里刻意保留最小接口,避免与 Koishi
|
|
7
|
+
* 的表类型强耦合。`get<T = unknown>` 是唯一保留的宽松点。
|
|
12
8
|
*/
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 单页面同步
|
|
20
|
-
* @param oldSite 源站点机器人实例
|
|
21
|
-
* @param newSite 目标站点机器人实例
|
|
22
|
-
* @param pageTitle 同步的标题
|
|
23
|
-
* @param user 触发更改的用户
|
|
24
|
-
* @returns success: boolean;reason: string;
|
|
9
|
+
export interface Database {
|
|
10
|
+
set(table: string, query: object, data: object): Promise<unknown>;
|
|
11
|
+
get<T = unknown>(table: string, query: object): Promise<T[]>;
|
|
12
|
+
create(table: string, data: object): Promise<unknown>;
|
|
13
|
+
upsert(table: string, data: object | object[]): Promise<unknown>;
|
|
25
14
|
}
|
|
26
|
-
*/
|
|
27
|
-
declare function syncSinglePage(oldSite: Mwn, newSite: Mwn, pageTitle: string, user: string): Promise<{
|
|
28
|
-
success: boolean;
|
|
29
|
-
reason: string;
|
|
30
|
-
}>;
|
|
31
|
-
/**
|
|
32
|
-
* 同步所有页面
|
|
33
|
-
* @param oldSite 源站点机器人实例
|
|
34
|
-
* @param newSite 目标站点机器人实例
|
|
35
|
-
* @returns null
|
|
36
|
-
*/
|
|
37
|
-
declare function syncPages(oldSite: Mwn, newSite: Mwn): Promise<void>;
|
|
38
|
-
/**
|
|
39
|
-
* 同步成功事件信息
|
|
40
|
-
*/
|
|
15
|
+
/** 同步成功事件信息 */
|
|
41
16
|
export interface SyncNotifyItem {
|
|
42
17
|
title: string;
|
|
43
18
|
type: "page" | "image";
|
|
@@ -45,13 +20,65 @@ export interface SyncNotifyItem {
|
|
|
45
20
|
timestamp?: string;
|
|
46
21
|
comment?: string;
|
|
47
22
|
}
|
|
23
|
+
/** 页面同步的可调参数 */
|
|
24
|
+
export interface PageSyncOptions {
|
|
25
|
+
ignoredPages: Set<string>;
|
|
26
|
+
intervalSuccess: number;
|
|
27
|
+
intervalFailed: number;
|
|
28
|
+
namespace: number;
|
|
29
|
+
batchLimit: number | "max";
|
|
30
|
+
fileNamespacePrefix: string;
|
|
31
|
+
defaultUser: string;
|
|
32
|
+
dbRetryMax: number;
|
|
33
|
+
dbRetryDelay: number;
|
|
34
|
+
}
|
|
35
|
+
export interface PageSyncResult {
|
|
36
|
+
success: boolean;
|
|
37
|
+
reason: string;
|
|
38
|
+
}
|
|
39
|
+
export interface PageSyncerDependencies {
|
|
40
|
+
/** 数据库实例(可选),用于维护 wikipages 表 */
|
|
41
|
+
database?: Database;
|
|
42
|
+
/** 增量更新结束时一次性回调(仅包含 reason==="synced" 的条目) */
|
|
43
|
+
notify?: (items: SyncNotifyItem[]) => void;
|
|
44
|
+
options?: Partial<PageSyncOptions>;
|
|
45
|
+
}
|
|
48
46
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* @param onSynced 同步成功批量回调(仅 reason==="synced" 的条目,函数结束时一次调用)
|
|
54
|
-
* @param database 数据库实例(可选),用于更新贡献者信息
|
|
47
|
+
* 负责页面层面的同步:单页同步、全量同步、以及定时的增量更新。
|
|
48
|
+
*
|
|
49
|
+
* 依赖(源站 / 目标站 / 数据库 / 通知回调 / 选项)全部由构造函数注入,
|
|
50
|
+
* 不再依赖模块级可变单例;图片同步通过组合 `ImageSyncer` 复用。
|
|
55
51
|
*/
|
|
56
|
-
export declare
|
|
57
|
-
|
|
52
|
+
export declare class PageSyncer {
|
|
53
|
+
private readonly source;
|
|
54
|
+
private readonly target;
|
|
55
|
+
private readonly options;
|
|
56
|
+
private readonly database?;
|
|
57
|
+
private readonly notify?;
|
|
58
|
+
private readonly images;
|
|
59
|
+
constructor(source: Mwn, target: Mwn, deps?: PageSyncerDependencies);
|
|
60
|
+
/** 带重试的数据库操作 */
|
|
61
|
+
private retryDb;
|
|
62
|
+
/** 若数据库中不存在该页面,先写入基本数据(含拼音索引) */
|
|
63
|
+
private ensurePageInDatabase;
|
|
64
|
+
/** 更新页面贡献者信息到数据库 */
|
|
65
|
+
private updateContributorToDatabase;
|
|
66
|
+
syncOne(pageTitle: string, user: string): Promise<PageSyncResult>;
|
|
67
|
+
listAll(): Promise<string[]>;
|
|
68
|
+
private processPageWithStats;
|
|
69
|
+
private printProgress;
|
|
70
|
+
private printFinalReport;
|
|
71
|
+
/** 全量同步所有页面:先串行同步一轮,再重试失败的 */
|
|
72
|
+
syncAll(): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* 增量更新:取最近 3 小时的 recentchanges 并逐条同步。
|
|
75
|
+
*
|
|
76
|
+
* 结束时通过构造时注入的 `notify` 一次性回调成功条目
|
|
77
|
+
* (仅 reason==="synced" 的页面与图片)。
|
|
78
|
+
*/
|
|
79
|
+
incrementalUpdate(): Promise<void>;
|
|
80
|
+
/** 增量更新中遇到图片页面时的处理 */
|
|
81
|
+
private handleImageSync;
|
|
82
|
+
/** 增量更新中遇到普通页面时的处理 */
|
|
83
|
+
private handlePageSync;
|
|
84
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { sleep } from "koishi";
|
|
2
|
+
export { sleep };
|
|
3
|
+
/**
|
|
4
|
+
* 统一的带重试执行器。
|
|
5
|
+
*
|
|
6
|
+
* 语义约定(替代原先散落的三套实现):
|
|
7
|
+
* - `attempts` 是**总尝试次数(含首次)**,不是"额外重试次数";
|
|
8
|
+
* - 全部失败后抛出**最后一次的错误本身**,不做包装,保留原始错误类型与 message;
|
|
9
|
+
* - 只有"尝试次数未用尽"时才调用 `onRetry` 并等待。
|
|
10
|
+
*/
|
|
11
|
+
export interface RetryOptions {
|
|
12
|
+
/** 总尝试次数(含首次),默认 3,必须 >= 1 */
|
|
13
|
+
attempts?: number;
|
|
14
|
+
/** 每次重试前的等待毫秒数,默认 1000;<= 0 表示立即重试 */
|
|
15
|
+
delayMs?: number;
|
|
16
|
+
/** 每次失败后、下次尝试前的回调,用于记录日志 */
|
|
17
|
+
onRetry?: (error: unknown, attempt: number, attempts: number) => void;
|
|
18
|
+
}
|
|
19
|
+
export declare function withRetry<T>(fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 页面查询的匹配与打分逻辑。
|
|
3
|
+
*
|
|
4
|
+
* 这里只放**纯函数**:不依赖 Koishi Context、不做 I/O、不涉及消息渲染,
|
|
5
|
+
* 因此可以直接被单元测试覆盖(见 tests/matcher.spec.ts)。
|
|
6
|
+
*/
|
|
7
|
+
/** 结构上兼容 `WikiPages` */
|
|
8
|
+
export interface ScorablePage {
|
|
9
|
+
id: number;
|
|
10
|
+
title: string;
|
|
11
|
+
pinyin_full: string;
|
|
12
|
+
pinyin_first: string;
|
|
13
|
+
}
|
|
14
|
+
export interface MatchResult {
|
|
15
|
+
id: number;
|
|
16
|
+
title: string;
|
|
17
|
+
score: number;
|
|
18
|
+
}
|
|
19
|
+
export declare function getCommonPrefixLength(a: string, b: string): number;
|
|
20
|
+
/**
|
|
21
|
+
* 为单个页面打分,分数越大越相关,0 表示不匹配。
|
|
22
|
+
*
|
|
23
|
+
* 计分项(每一项都要求对应的查询串**非空**):
|
|
24
|
+
* - 标题包含关键词:+10
|
|
25
|
+
* - 全拼以关键词全拼开头:+9,仅包含:+8
|
|
26
|
+
* - 首字母包含关键词首字母:+6
|
|
27
|
+
* - 标题与关键词公共前缀 >= 2:加前缀长度
|
|
28
|
+
* - 全拼与关键词全拼公共前缀 >= 3:加其一半(向下取整)
|
|
29
|
+
*
|
|
30
|
+
* 为什么必须逐项判空:`"任意串".includes("")` 与 `"任意串".startsWith("")` 都恒为 true。
|
|
31
|
+
* - 纯拉丁查询(如 `Refinery`)的首字母是空串,不判空则**每个页面白得 6 分**;
|
|
32
|
+
* - 完全由符号组成的查询(如 `!!!`)全拼与首字母都为空,不判空则**每页白得 23 分**。
|
|
33
|
+
*
|
|
34
|
+
* 两种情况都会把"没有匹配"变成"返回若干随机页面"。
|
|
35
|
+
*/
|
|
36
|
+
export declare function calculateScore(page: ScorablePage, queryKey: string, queryPinyinFull: string, queryPinyinFirst: string): number;
|
|
37
|
+
/**
|
|
38
|
+
* 模糊匹配:打分后按分数降序、同分按标题短的优先,标题去重,最多取 `limit` 条。
|
|
39
|
+
*/
|
|
40
|
+
export declare function findFuzzyMatches(allPages: ScorablePage[], queryKey: string, queryPinyinFull: string, queryPinyinFirst: string, limit?: number): MatchResult[];
|
package/lib/utils/tools.d.ts
CHANGED
|
@@ -19,7 +19,11 @@ declare function generatePinyinInfo(text: string): {
|
|
|
19
19
|
};
|
|
20
20
|
declare function getErrorMessage(error: unknown): string;
|
|
21
21
|
/**
|
|
22
|
-
* 将 MediaWiki ISO 时间戳(如 "2024-06-19T10:30:45Z"
|
|
22
|
+
* 将 MediaWiki ISO 时间戳(如 "2024-06-19T10:30:45Z")转为北京时间可读字符串。
|
|
23
|
+
*
|
|
24
|
+
* 实现要点:先把时间戳整体平移 +8 小时,再统一用 UTC 取值。
|
|
25
|
+
* 这样结果与**运行机器的本地时区无关**,且 +8 造成的跨日/跨月/跨年进位由 Date 自动处理
|
|
26
|
+
* (早期实现混用 getFullYear 与 getUTCHours,只在 UTC+8 机器上碰巧正确)。
|
|
23
27
|
*/
|
|
24
28
|
declare function formatWikiTime(isoString: string): string;
|
|
25
29
|
/**
|