Skip to content

Commit e63ac6f

Browse files
committed
Try to add an edit tag when post pages
1 parent 047610a commit e63ac6f

File tree

7 files changed

+81
-40
lines changed

7 files changed

+81
-40
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
},
201201
"wikitext.redirects": {
202202
"type": "boolean",
203-
"description": "When the input page is a redirect page, jump automatically.",
203+
"description": "When the input page is a redirect page, jump automatically. Note that posting command will **not** be redirected.",
204204
"default": true
205205
},
206206
"wikitext.apiPath": {
@@ -261,7 +261,7 @@
261261
],
262262
"description": "Whether to automatically log in when performing an action that requires an account to be logged in."
263263
},
264-
"wikitext.skipEnteringPageTitle":{
264+
"wikitext.skipEnteringPageTitle": {
265265
"type": "boolean",
266266
"default": false,
267267
"markdownDescription": "If `PageTitle` is filled in `PAGE_INFO`, skip entering the title when posting."
@@ -307,4 +307,4 @@
307307
"luxon": "^2.0.1",
308308
"node-fetch": "^2.6.5"
309309
}
310-
}
310+
}

src/export_command/cite_function/web.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export async function addWebCite(): Promise<void> {
3939
catch (error) {
4040
if (error instanceof Error) {
4141
vscode.window.showErrorMessage(`ErrorName: ${error.name}; ErrorMessage: ${error.message}.`);
42-
}
43-
else {
42+
} else {
4443
vscode.window.showErrorMessage(`addWebCite ERROR: ${JSON.stringify(error)}.`);
4544
}
4645
}
@@ -154,8 +153,7 @@ export function getReplacedString(formatStr: string, argStr: string, replaceStr:
154153
formatStr = formatStr.replace(new RegExp(`<\\/?!${argStr}>`, 'g'), '');
155154
// replace all {$arg}
156155
formatStr = formatStr.replace(argRegExp, replaceStr);
157-
}
158-
else {
156+
} else {
159157
// remove all substring between <!arg> and </!arg>
160158
// /\<\!arg\>[\s\S]*?\<\/\!arg\>/
161159
formatStr = formatStr.replace(new RegExp(`<!${argStr}>[\\s\\S]*?<\\/!${argStr}>`, 'g'), '');

src/export_command/uri_function/uri.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ export function parseArgs(query: string): Record<string, string> {
4040
const eq: number = item.indexOf("=");
4141
if (eq >= 0) {
4242
pars[item.substring(0, eq)] = item.substring(eq + 1);
43-
}
44-
else {
43+
} else {
4544
pars[item] = '';
4645
}
4746
}

src/export_command/wikimedia_function/args.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const enum Action {
2020
centralNoticeCdnCacheUpdateBanner = "centralnoticecdncacheupdatebanner",
2121
centralNoticeChoiceData = "centralnoticechoicedata",
2222
// ...
23+
edit = "edit",
24+
// ...
2325
login = "login",
2426
logout = "logout",
2527
// ...
@@ -56,6 +58,12 @@ export const enum Prop {
5658
parsetree = "parsetree"
5759
}
5860

61+
export const enum List {
62+
// ...
63+
tags = "tags",
64+
// ...
65+
}
66+
5967
export const enum RvProp {
6068
ids = "ids",
6169
flags = "flags",

src/export_command/wikimedia_function/bot.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ export async function getDefaultBot(): Promise<MWBot | undefined> {
7373
let tBot: MWBot;
7474
if (bot) {
7575
tBot = bot;
76-
}
77-
else {
76+
} else {
7877
// get host
7978
const host: string | undefined = await getHost();
8079
if (!host) { return undefined; }

src/export_command/wikimedia_function/err_msg.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { instanceOfMWError } from '../../interface_definition/commonInterface';
33

44
export function showMWErrorMessage(name: string, error: unknown, moreInfo: string = ''): void {
55
if (instanceOfMWError(error)) {
6-
vscode.window.showErrorMessage(`ErrorCode: ${error.code}; ErrorInfo: ${error.info}. ${moreInfo}`.trim());
7-
}
8-
else if (error instanceof Error) {
9-
vscode.window.showErrorMessage(`ErrorName: ${error.name}; ErrorMessage: ${error.message}. ${moreInfo}`.trim());
10-
}
11-
else {
12-
vscode.window.showErrorMessage(`${name} ERROR: ${JSON.stringify(error)}. ${moreInfo}`.trim());
6+
vscode.window.showErrorMessage(`ErrorCode: ${error.code}; ErrorInfo: ${error.info}; ${moreInfo}`.trim());
7+
} else if (error instanceof Error) {
8+
vscode.window.showErrorMessage(`ErrorName: ${error.name}; ErrorMessage: ${error.message}; ${moreInfo}`.trim());
9+
} else {
10+
vscode.window.showErrorMessage(`${name} ERROR: ${JSON.stringify(error)}; ${moreInfo}`.trim());
1311
}
1412
}

src/export_command/wikimedia_function/page.ts

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as vscode from 'vscode';
77
import type Bluebird from 'bluebird';
88
import type MWBot from 'mwbot';
9-
import { Action, Prop, RvProp, alterNativeValues } from './args';
9+
import { Action, Prop, RvProp, alterNativeValues, List } from './args';
1010
import { ReadPageConvert, ReadPageResult, Main, Revision, Jump, Page } from '../../interface_definition/readPageInterface';
1111
import { OldTokensConvert, OldTokensResult } from '../../interface_definition/oldTokensInterface';
1212
import { getDefaultBot, getLoggedInBot } from './bot';
@@ -59,11 +59,12 @@ export async function postPage(): Promise<void> {
5959
}
6060

6161
const error = Error('Could not get edit token:' +
62-
'NEW: ' + ((errors[0] instanceof Error) ? errors[0].message : "") +
63-
'OLD: ' + ((errors[1] instanceof Error) ? errors[1].message : ""));
62+
' NEW: ' + ((errors[0] instanceof Error) ? errors[0].message : '') +
63+
' OLD: ' + ((errors[1] instanceof Error) ? errors[1].message : ''));
6464
throw error;
6565
}
66-
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("wikitext");
66+
67+
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('wikitext');
6768
const tBot: MWBot | undefined = await getLoggedInBot();
6869

6970
if (tBot === undefined) {
@@ -78,7 +79,6 @@ export async function postPage(): Promise<void> {
7879
}
7980

8081
const contentInfo: ContentInfo = getContentInfo(wikiContent);
81-
// console.log(contentInfo);
8282

8383
const skip: boolean = config.get("skipEnteringPageTitle") as boolean;
8484

@@ -99,26 +99,41 @@ export async function postPage(): Promise<void> {
9999
if (wikiSummary === undefined) {
100100
return undefined;
101101
}
102-
wikiSummary = `{wikiSummary} // Edit via Wikitext Extension for VSCode`.trim();
103102
wikiSummary = `${wikiSummary} // Edit via Wikitext Extension for VSCode`.trim();
104103
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Posting...");
105104
try {
106-
tBot.editToken = await getEditToken(tBot);
107-
await tBot.edit(wikiTitle, contentInfo.content, wikiSummary).then(response => {
108-
if (response.edit.nochange !== undefined) {
109-
vscode.window.showWarningMessage(
110-
`No changes have occurred: "${response.edit.nochange}", Edit page "${response.edit.title}" (Page ID: "${response.edit.pageid}") action status is "${response.edit.result}" with Content Model "${response.edit.contentmodel}". Watched by: "${response.edit.watched}".`
111-
);
112-
}
113-
else {
114-
vscode.window.showInformationMessage(
115-
`Edit page "${response.edit.title}" (Page ID: "${response.edit.pageid}") action status is "${response.edit.result}" with Content Model "${response.edit.contentmodel}" (Version: "${response.edit.oldrevid}" => "${response.edit.newrevid}", Time: "${response.edit.newtimestamp}"). Watched by: "${response.edit.watched}".`
116-
);
117-
}
118-
});
105+
106+
const args: Record<string, string> = {
107+
action: Action.edit,
108+
title: wikiTitle,
109+
text: contentInfo.content,
110+
summary: wikiSummary,
111+
// tags: 'WikitextExtensionForVSCode',
112+
token: await getEditToken(tBot)
113+
};
114+
const wikitextTag: string = 'AWB';
115+
const tagList: string[] = await getValidTagList(tBot);
116+
if (tagList.includes(wikitextTag)) {
117+
args['tags'] = wikitextTag;
118+
}
119+
120+
// if (config.get("redirect")) {
121+
// args['redirect'] = "true";
122+
// }
123+
const result: any = await tBot.request(args);
124+
// TODO: Convert
125+
if (result.edit.nochange !== undefined) {
126+
vscode.window.showWarningMessage(
127+
`No changes have occurred: "${result.edit.nochange}", Edit page "${result.edit.title}" (Page ID: "${result.edit.pageid}") action status is "${result.edit.result}" with Content Model "${result.edit.contentmodel}". Watched by: "${result.edit.watched}".`
128+
);
129+
} else {
130+
vscode.window.showInformationMessage(
131+
`Edit page "${result.edit.title}" (Page ID: "${result.edit.pageid}") action status is "${result.edit.result}" with Content Model "${result.edit.contentmodel}" (Version: "${result.edit.oldrevid}" => "${result.edit.newrevid}", Time: "${result.edit.newtimestamp}"). Watched by: "${result.edit.watched}".`
132+
);
133+
}
119134
}
120135
catch (error) {
121-
showMWErrorMessage('postPage', error, `Your Token: ${tBot?.editToken}`);
136+
showMWErrorMessage('postPage', error, `Your Token: ${tBot?.editToken}.`);
122137
}
123138
finally {
124139
barMessage.dispose();
@@ -276,8 +291,6 @@ ${infoLine}
276291
}
277292
}
278293

279-
// TODO: uploadFile, deletedPage
280-
281294
export function getContentInfo(content: string): ContentInfo {
282295
const info: string | undefined = content.match(
283296
/(?<=<%--\s*\[PAGE_INFO\])[\s\S]*?(?=\[END_PAGE_INFO\]\s*--%>)/
@@ -304,3 +317,29 @@ export function getContentInfo(content: string): ContentInfo {
304317

305318
return { content: content, info: pageInfo };
306319
}
320+
321+
async function getValidTagList(tBot: MWBot): Promise<string[]> {
322+
const args: Record<string, string> = {
323+
action: Action.query,
324+
list: List.tags,
325+
tglimit: 'max',
326+
tgprop: alterNativeValues('active', 'defined')
327+
};
328+
329+
const tagList: string[] = [];
330+
// TODO: interface
331+
do {
332+
const result = await tBot.request(args);
333+
const tags: any[] = result.query.tags;
334+
tagList.push(
335+
...tags.filter(tag =>
336+
tag.active !== undefined && tag.defined !== undefined
337+
).map(tag => tag.name as string));
338+
if (result.continue !== undefined) {
339+
Object.keys(result.continue)
340+
.forEach(key => args[key] = result.continue[key]);
341+
} else { break; }
342+
} while (true);
343+
344+
return tagList;
345+
}

0 commit comments

Comments
 (0)