Skip to content

Commit d4f08e8

Browse files
committed
Add better support for private wikis to fix #89
1 parent 5ec6574 commit d4f08e8

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

src/export_command/uri_function/editPage.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ export async function editPage(query: string): Promise<void> {
1414
// vscode-insiders://rowewilsonfrederiskholme.wikitext/PullPage?Title=1
1515
const pars: Record<string, string> = parseArgs(query);
1616

17+
const transferProtocolPar = pars["TransferProtocol"] ?? '';
18+
const siteHostPar = pars["SiteHost"] ?? '';
19+
const apiPathPar = pars["APIPath"] ?? '';
20+
1721
const tBot: MWBot | undefined = isRemoteBot(pars) ? new MWBot({
18-
apiUrl: pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
22+
apiUrl: transferProtocolPar + siteHostPar + apiPathPar
1923
}) : await getDefaultBot();
2024

2125
const title: string | undefined = pars['Title'];

src/export_command/uri_function/uri.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ export function baseUriProcess(uri: vscode.Uri): void {
3030
}
3131

3232
export function isRemoteBot(pars: Record<string, string>): boolean {
33-
return !!(pars['RemoteBot'] || pars['SiteHost']);
33+
const remoteBotPar: string | undefined = pars['RemoteBot']?.toLowerCase();
34+
switch (remoteBotPar) {
35+
case undefined:
36+
case '':
37+
case 'false':
38+
return false;
39+
default:
40+
return true;
41+
};
3442
}
3543

3644
export function parseArgs(query: string): Record<string, string> {

src/export_command/uri_function/viewPage.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ import { isRemoteBot, parseArgs } from './uri';
1212

1313
export async function viewPage(query: string): Promise<void> {
1414
function setArgs(par: string, defaultValue?: string): void {
15-
args[par.toLowerCase()] = pars[par] ?? defaultValue;
15+
const value = pars[par] ?? defaultValue;
16+
if (value !== undefined) {
17+
args[par.toLowerCase()] = value;
18+
}
1619
}
1720

1821
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("wikitext");
1922
const pars: Record<string, string> = parseArgs(query);
2023

24+
const transferProtocolPar = pars["TransferProtocol"] ?? '';
25+
const siteHostPar = pars["SiteHost"] ?? '';
26+
const apiPathPar = pars["APIPath"] ?? '';
27+
2128
const tBot: MWBot | undefined = isRemoteBot(pars) ? new MWBot({
22-
apiUrl: pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
29+
apiUrl: transferProtocolPar + siteHostPar + apiPathPar
2330
}) : await getDefaultBot();
2431

2532
if (!tBot) {
@@ -28,7 +35,7 @@ export async function viewPage(query: string): Promise<void> {
2835
}
2936

3037
const baseHref: string = isRemoteBot(pars)
31-
? pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
38+
? transferProtocolPar + siteHostPar + apiPathPar
3239
: config.get("transferProtocol") as string + config.get('host') + config.get("articlePath");
3340

3441
// args value

src/export_command/wikimedia_function/bot.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export async function getDefaultBot(): Promise<MWBot | undefined> {
8484
if (bot) {
8585
tBot = bot;
8686
} else {
87-
tBot = new MWBot({
87+
tBot = await getLoggedInBot() ?? new MWBot({
8888
apiUrl: config.get("transferProtocol") as string + config.get('host') + config.get("apiPath")
8989
});
9090
}
@@ -142,9 +142,9 @@ export async function compareVersion(tBot: MWBot, major: number, minor: number,
142142
return undefined;
143143
}
144144

145-
const siteMajor: number = parseInt(generatorInfo[1]);
146-
const siteMinor: number = parseInt(generatorInfo[2]);
147-
const siteRevision: number = parseInt(generatorInfo[3]);
145+
const siteMajor: number = parseInt(generatorInfo[1]!, 10);
146+
const siteMinor: number = parseInt(generatorInfo[2]!, 10);
147+
const siteRevision: number = parseInt(generatorInfo[3]!, 10);
148148

149149
if (isNaN(siteMajor + siteMinor + siteRevision)) {
150150
return undefined;

src/export_command/wikimedia_function/page.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ ${infoLine}
259259
const re: ReadPageResult = ReadPageConvert.toResult(result);
260260
if (re.query?.interwiki) {
261261
vscode.window.showWarningMessage(
262-
`Interwiki page "${re.query.interwiki[0].title}" in space "${re.query.interwiki[0].iw}" are currently not supported. Please try to modify host.`
262+
`Interwiki page "${re.query.interwiki[0]?.title}" in space "${re.query.interwiki[0]?.iw}" are currently not supported. Please try to modify host.`
263263
);
264264
}
265265

266266
// get first page
267-
const page: Page | undefined = re.query?.pages?.[Object.keys(re.query.pages)[0]];
267+
const page: Page | undefined = re.query?.pages?.[Object.keys(re.query.pages)[0]!];
268268
// need a page elements
269269
if (!page) { return undefined; }
270270

@@ -332,7 +332,7 @@ export function getContentInfo(content: string): ContentInfo {
332332
let pageInfo: Record<PageInfo, string | undefined> | undefined;
333333
if (info) {
334334
const getInfo = (infoName: PageInfo): string | undefined => {
335-
const nameFirst: string = infoName[0];
335+
const nameFirst: string = infoName[0]!;
336336
const nameRest: string = infoName.substring(1);
337337
const reg = new RegExp(`(?<=[${nameFirst.toLowerCase()}${nameFirst.toUpperCase()}]${nameRest}\\s*=\\s*#).*?(?=#)`);
338338
return info.match(reg)?.[0];

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"importHelpers": true,
2727
"experimentalDecorators": true,
2828
"forceConsistentCasingInFileNames": true,
29+
"noUncheckedIndexedAccess": true,
2930
},
3031
"exclude": [
3132
"./.vscode-test/*"

0 commit comments

Comments
 (0)