Skip to content

Commit 8d58fb0

Browse files
authored
Merge pull request #4 from bonk-dev/translated-worker
Make translated articles worker more robust
2 parents 9fe9244 + e4da6cf commit 8d58fb0

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arch-translator",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"description": "Useful tools for ArchWiki translators, now written in TypeScript.",
55
"main": "index.js",
66
"scripts": {

src/Tools/SidebarTools.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import {CustomSidebarTool, sideTool} from "./Utils/ToolManager";
22
import {getMwApi} from "../Utilities/MediaWikiJsApi";
3-
import {getCurrentPageInfo, getEnglishRevisionId, PageInfo, PageType} from "../Utilities/PageUtils";
3+
import {
4+
getCurrentPageContent,
5+
getCurrentPageInfo,
6+
getEnglishRevisionId,
7+
PageInfo,
8+
PageType
9+
} from "../Utilities/PageUtils";
410
import {getCurrentLanguage, setCurrentLanguage} from "../Storage/ScriptDb";
511
import {getLangInfoFor, LanguagesInfo} from "../Internalization/I18nConstants";
12+
import {TranslatedArticlesWorker} from "./Workers/TranslatedArticlesWorker";
13+
import {addWorkerResultToUi} from "./TranslatedArticlesUi";
14+
import {WikiTextParser} from "../Utilities/WikiTextParser";
615

716
export const copyCurrentRevisionIdTool = (): CustomSidebarTool => {
817
const showCallback = (info: PageInfo) => {
@@ -138,6 +147,34 @@ export const changeActiveLanguageTool = (): CustomSidebarTool => {
138147
});
139148
};
140149

150+
export const refreshTranslatedArticles = (): CustomSidebarTool => {
151+
const handler = async () => {
152+
console.debug('Rerunning translated articles worker');
153+
154+
const pageInfo = getCurrentPageInfo();
155+
const contentToParse = await getCurrentPageContent();
156+
157+
const parser = new WikiTextParser();
158+
parser.parse(contentToParse);
159+
160+
const translatedArticleWorker = new TranslatedArticlesWorker(pageInfo);
161+
translatedArticleWorker.run(parser)
162+
.then(r => {
163+
console.debug(r);
164+
console.debug('Translated articles worker done');
165+
166+
addWorkerResultToUi(r);
167+
});
168+
};
169+
170+
return sideTool({
171+
name: "refresh-translated-articles",
172+
displayText: "Refresh translated articles",
173+
handler: handler
174+
});
175+
};
176+
141177
export const allSidebarTools = [
142-
changeActiveLanguageTool(), copyCurrentRevisionIdTool(), copyEnglishRevisionIdTool(), createTranslationTool()
178+
changeActiveLanguageTool(), copyCurrentRevisionIdTool(), copyEnglishRevisionIdTool(), createTranslationTool(),
179+
refreshTranslatedArticles()
143180
];

src/Tools/Workers/TranslatedArticlesWorker.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ export class TranslatedArticlesWorker {
8080
const linksWithPostfixes = parser
8181
.localizableLinks
8282
.map(l => localizeLink(l, language));
83+
if (linksWithPostfixes.length <= 0) {
84+
return {
85+
existing: [],
86+
notExisting: [],
87+
redirects: []
88+
};
89+
}
90+
8391
const info = await this._getPageInfosFor(linksWithPostfixes);
8492
console.debug(info.filter(i => i.exists));
8593

@@ -100,9 +108,9 @@ export class TranslatedArticlesWorker {
100108

101109
const findRedirectSource = (localizedLink: string): string => {
102110
const englishLink = removeLanguagePostfix(localizedLink);
103-
if (englishLink === localizedLink) return localizedLink;
111+
if (englishLink.toLowerCase() === localizedLink.toLowerCase()) return localizedLink;
104112

105-
const original = redirectsAndOther.find(r => r.redirectsTo === englishLink)!;
113+
const original = redirectsAndOther.find(r => r.redirectsTo?.toLowerCase() === englishLink.toLowerCase())!;
106114
return original.link!;
107115
};
108116

src/Utilities/PageUtils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ import {EditMessage, getMwApi} from "./MediaWikiJsApi";
22
import {isTranslated, removeLanguagePostfix} from "../Internalization/I18nConstants";
33
import {getPageContent} from "./Api/MediaWikiApiClient";
44
import {getCachedPageInfo} from "../Storage/ScriptDb";
5+
import {CodeMirrorEditor} from "./CodeMirrorTypes";
56

67
/**
78
* Sometimes we do not need to fetch(...) for the raw page content (e.g. on edit pages).
89
*/
910
let cachedPageContent: string|null = null;
1011

12+
/**
13+
* Storing the CodeMirror editor instance allows us to easily get it's current content
14+
*/
15+
let codeMirrorInstance: CodeMirrorEditor|null = null;
16+
1117
export enum PageType {
1218
/**
1319
* Normal article read page
@@ -129,11 +135,22 @@ export function cacheCurrentPageContent(content: string) {
129135
cachedPageContent = content;
130136
}
131137

138+
/**
139+
* Stores the CodeMirror editor instance
140+
* @param cmEditor The editor instance
141+
*/
142+
export function storeCodeMirrorInstance(cmEditor: CodeMirrorEditor) {
143+
codeMirrorInstance = cmEditor;
144+
}
145+
132146
export async function getCurrentPageContent() {
133-
if (cachedPageContent != null) {
147+
if (codeMirrorInstance == null && cachedPageContent != null) {
134148
console.debug(`getCurrentPageContent: cache hit`);
135149
return cachedPageContent;
136150
}
151+
else if (codeMirrorInstance != null) {
152+
return codeMirrorInstance.getValue();
153+
}
137154

138155
const pageName = getMwApi()
139156
.config

src/Utilities/WikiTextParser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,17 @@ export class WikiTextParser {
253253
* @return string|null Redirect target page name or null if the content is not a redirect page
254254
*/
255255
export const findRedirect = (wikiText: string): WikiLink|null => {
256-
if (!wikiText.startsWith("#REDIRECT")) {
256+
const prefix = "#REDIRECT ";
257+
if (wikiText.length < prefix.length) {
258+
return null;
259+
}
260+
const probablePrefix = wikiText.substring(0, prefix.length);
261+
if (probablePrefix.toUpperCase() != prefix) {
257262
return null;
258263
}
259264

260265
const linkText = wikiText
261-
.substring("#REDIRECT ".length)
266+
.substring(prefix.length)
262267
.trim();
263268
return new WikiLink(linkText);
264269
};

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import {getMwApi} from "./Utilities/MediaWikiJsApi";
33
import {ToolManager} from "./Tools/Utils/ToolManager";
44
import {allSidebarTools} from "./Tools/SidebarTools";
55
import {getCachedPageInfo, getCurrentLanguage, setCachedPageInfo, setupDb} from "./Storage/ScriptDb";
6-
import {cacheCurrentPageContent, getCurrentPageContent, getCurrentPageInfo, PageType} from "./Utilities/PageUtils";
6+
import {
7+
cacheCurrentPageContent,
8+
getCurrentPageContent,
9+
getCurrentPageInfo,
10+
PageType,
11+
storeCodeMirrorInstance
12+
} from "./Utilities/PageUtils";
713
import {cacheCurrentPage} from "./Tools/CurrentPageDumper";
814
import {CodeMirrorEditor} from "./Utilities/CodeMirrorTypes";
915
import {NewArticleWorker} from "./Tools/Workers/NewArticleWorker";
@@ -70,6 +76,7 @@ setupDb()
7076

7177
// @ts-ignore
7278
const cmEditor = codeMirrorElement.get()[0].CodeMirror as CodeMirrorEditor;
79+
storeCodeMirrorInstance(cmEditor);
7380
cacheCurrentPageContent(cmEditor.getValue());
7481

7582
const pageInfo = getCurrentPageInfo();

src/uscript-header.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @namespace bb89542e-b358-4be0-8c01-3797d1f3a1e3
44
// @match https://wiki.archlinux.org/*
55
// @grant none
6-
// @version 2.0.3
6+
// @version 2.0.4
77
// @author bonk-dev
88
// @description Useful tools for ArchWiki translators, now written in TypeScript.
99
// @icon https://gitlab.archlinux.org/uploads/-/system/group/avatar/23/iconfinder_archlinux_386451.png

0 commit comments

Comments
 (0)