Skip to content

Commit

Permalink
Add reset method to Mediawiki, improve Renderer builder
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKovalenkoSNF committed Aug 18, 2023
1 parent 67eeeda commit 1b3ea1e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 35 deletions.
31 changes: 23 additions & 8 deletions src/MediaWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class MediaWiki {
public _base: string
public baseUrl: URL
public getCategories: boolean
public readonly namespaces: MWNamespaces = {}
public readonly namespacesToMirror: string[] = []
public mwArticleId = 'MediaWiki:Sidebar'
public namespaces: MWNamespaces = {}
public namespacesToMirror: string[] = []
public mwArticleId: string

#wikiPath: string
#restApiPath: string
Expand All @@ -48,8 +48,8 @@ class MediaWiki {
public webUrl: URL
public desktopRestApiUrl: URL

#hasWikimediaDesktopRestApi = false
#hasVisualEditorApi = false
#hasWikimediaDesktopRestApi: boolean | null
#hasVisualEditorApi: boolean | null

set username(value: string) {
this.#username = value
Expand Down Expand Up @@ -84,26 +84,37 @@ class MediaWiki {
this._modulePathOpt = value
}

private constructor() {
private initializeMediaWikiDefaults(): void {
this.#domain = ''
this.#username = ''
this.#password = ''
this.getCategories = false

this.namespaces = {}
this.namespacesToMirror = []

this.#apiPath = 'w/api.php'
this.#wikiPath = 'wiki/'
this.mwArticleId = 'MediaWiki:Sidebar'

this.#hasWikimediaDesktopRestApi = null
this.#hasVisualEditorApi = null
}

private constructor() {
this.initializeMediaWikiDefaults()
}

public async hasWikimediaDesktopRestApi(): Promise<boolean> {
if (!this.#hasWikimediaDesktopRestApi) {
if (this.#hasWikimediaDesktopRestApi === null) {
this.#hasWikimediaDesktopRestApi = await checkApiAvailability(this.wikimediaDesktopUrlDirector.buildArticleURL(this.mwArticleId))
return this.#hasWikimediaDesktopRestApi
}
return this.#hasWikimediaDesktopRestApi
}

public async hasVisualEditorApi(): Promise<boolean> {
if (!this.#hasVisualEditorApi) {
if (this.#hasVisualEditorApi === null) {
this.#hasVisualEditorApi = await checkApiAvailability(this.visualEditorURLDirector.buildArticleURL(this.mwArticleId))
return this.#hasVisualEditorApi
}
Expand Down Expand Up @@ -360,6 +371,10 @@ class MediaWiki {

return mwMetaData
}

public reset(): void {
this.initializeMediaWikiDefaults()
}
}

const mw = MediaWiki.getInstance()
Expand Down
1 change: 1 addition & 0 deletions src/mwoffliner.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ async function execute(argv: any) {
}
}

MediaWiki.reset()
redisStore.close()

Check notice on line 644 in src/mwoffliner.lib.ts

View check run for this annotation

codefactor.io / CodeFactor

src/mwoffliner.lib.ts#L65-L644

Complex Method
return dumps
Expand Down
33 changes: 19 additions & 14 deletions src/util/renderers/renderer.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,49 @@ export class RendererBuilder {
private renderApi: 'VisualEditor' | 'WikimediaDesktop' | 'WikimediaMobile'
private renderMode: 'auto' | 'desktop' | 'mobile' | 'specific'

public createRenderer(options: RendererBuilderOptions): Renderer {
public async createRenderer(options: RendererBuilderOptions): Promise<Renderer> {
const { RendererMode, RendererAPI } = options

this.renderMode = RendererMode
this.renderApi = RendererAPI

const [hasVisualEditorApi, hasWikimediaDesktopRestApi] = await Promise.all([MediaWiki.hasVisualEditorApi(), MediaWiki.hasWikimediaDesktopRestApi()])

switch (this.renderMode) {
case 'desktop':
if (MediaWiki.hasVisualEditorApi && !MediaWiki.hasWikimediaDesktopRestApi) {
return new VisualEditorRenderer()
} else if (MediaWiki.hasWikimediaDesktopRestApi) {
if (hasWikimediaDesktopRestApi) {
// Choose WikimediaDesktopRenderer if it's present, regardless of hasVisualEditorApi value
return new WikimediaDesktopRenderer()
} else if (hasVisualEditorApi) {
return new VisualEditorRenderer()
} else {
logger.error('No available renderer for desktop mode.')
process.exit(1)
}
logger.error('No available renderer for desktop mode.')
process.exit(1)
case 'mobile':
// TODO: return WikimediaMobile renderer
break
case 'auto':
// Auto mode is code driven and based on mw api capabilities of specific wiki
if (MediaWiki.hasVisualEditorApi && !MediaWiki.hasWikimediaDesktopRestApi) {
return new VisualEditorRenderer()
} else if (MediaWiki.hasWikimediaDesktopRestApi) {
if (hasWikimediaDesktopRestApi) {
// Choose WikimediaDesktopRenderer if it's present, regardless of hasVisualEditorApi value
return new WikimediaDesktopRenderer()
} else if (hasVisualEditorApi) {
return new VisualEditorRenderer()
} else {
logger.error('No available renderer for auto mode.')
process.exit(1)
}
logger.error('No available renderer for auto mode.')
process.exit(1)
case 'specific':
// renderApi argument is required for 'specific' mode
switch (this.renderApi) {
case 'WikimediaDesktop':
if (MediaWiki.hasWikimediaDesktopRestApi) {
if (hasWikimediaDesktopRestApi) {
return new WikimediaDesktopRenderer()
}
logger.error('Cannot create an instance of WikimediaDesktop renderer.')
process.exit(1)
case 'VisualEditor':
if (MediaWiki.hasVisualEditorApi) {
if (hasVisualEditorApi) {
return new VisualEditorRenderer()
}
logger.error('Cannot create an instance of VisualEditor renderer.')
Expand Down
4 changes: 2 additions & 2 deletions src/util/saveArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ export async function saveArticles(zimCreator: ZimCreator, downloader: Downloade
const rendererBuilderOptions: RendererBuilderOptions = {
RendererMode: 'auto',
}
const mainPageRenderer = rendererBuilder.createRenderer(rendererBuilderOptions)
const mainPageRenderer = await rendererBuilder.createRenderer(rendererBuilderOptions)
// TODO: article renderer will be switched to the mobiel mode later
const articlesRenderer = rendererBuilder.createRenderer(rendererBuilderOptions)
const articlesRenderer = await rendererBuilder.createRenderer(rendererBuilderOptions)

if (dump.customProcessor?.shouldKeepArticle) {
await getAllArticlesToKeep(downloader, articleDetailXId, dump, mainPageRenderer, articlesRenderer)
Expand Down
26 changes: 15 additions & 11 deletions test/unit/renderers/renderer.builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@ describe('RendererBuilder', () => {
it('should create a WikimediaDesktopRenderer for desktop mode', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia

const renderer = rendererBuilder.createRenderer({
const renderer = await rendererBuilder.createRenderer({
MediaWiki,
RendererMode: 'desktop',
} as RendererBuilderOptions)
expect(renderer).toBeInstanceOf(WikimediaDesktopRenderer)
})

it('should create a WikimediaDesktopRenderer for auto mode as this is default for en wikipedia', async () => {
it('should create a WikimediaDesktopRenderer for auto mode for en wikipedia', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia
await MediaWiki.hasWikimediaDesktopRestApi()
await MediaWiki.hasVisualEditorApi()

const renderer = rendererBuilder.createRenderer({
const renderer = await rendererBuilder.createRenderer({
MediaWiki,
RendererMode: 'auto',
} as RendererBuilderOptions)
Expand All @@ -38,38 +36,44 @@ describe('RendererBuilder', () => {
it('should throw error for unknown render mode', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia

expect(() => {
rendererBuilder.createRenderer({
expect(async () => {
await rendererBuilder.createRenderer({
MediaWiki,
RendererMode: 'unknownMode' as any,
} as RendererBuilderOptions)
}).toThrow('Unknown render mode: unknownMode')
}).rejects.toThrow('Unknown render mode: unknownMode')
})

it('should return VisualEditorRenderer for specific mode with RendererAPI as VisualEditor', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia

// Force MediaWiki to have capability for the VisualEditor for test purpose
jest.spyOn(MediaWiki, 'hasVisualEditorApi').mockResolvedValue(true)

const rendererBuilderOptions = {
MediaWiki,
RendererMode: 'specific',
RendererAPI: 'VisualEditor',
}

const renderer = rendererBuilder.createRenderer(rendererBuilderOptions as RendererBuilderOptions)
const renderer = await rendererBuilder.createRenderer(rendererBuilderOptions as RendererBuilderOptions)

expect(renderer).toBeInstanceOf(VisualEditorRenderer)
})

it('should return WikimediaDesktopRenderer for specific mode with RendererAPI as WikimediaDesktop', async () => {
const { MediaWiki } = await setupScrapeClasses() // en wikipedia

// Force MediaWiki to have capability for the WikimediaDesktop for test purpose
jest.spyOn(MediaWiki, 'hasWikimediaDesktopRestApi').mockResolvedValue(true)

const rendererBuilderOptions = {
MediaWiki,
RendererMode: 'specific',
RendererAPI: 'WikimediaDesktop',
}

const renderer = rendererBuilder.createRenderer(rendererBuilderOptions as RendererBuilderOptions)
const renderer = await rendererBuilder.createRenderer(rendererBuilderOptions as RendererBuilderOptions)

expect(renderer).toBeInstanceOf(WikimediaDesktopRenderer)
})
Expand All @@ -86,7 +90,7 @@ describe('RendererBuilder', () => {
RendererAPI: 'UnknownAPI', // Using an invalid RendererAPI for the test
}

expect(() => rendererBuilder.createRenderer(rendererBuilderOptions as RendererBuilderOptions)).toThrow(
expect(async () => await rendererBuilder.createRenderer(rendererBuilderOptions as RendererBuilderOptions)).rejects.toThrow(

Check notice on line 93 in test/unit/renderers/renderer.builder.test.ts

View check run for this annotation

codefactor.io / CodeFactor

test/unit/renderers/renderer.builder.test.ts#L93

Unnecessary 'await'. (no-return-await)
`Unknown RendererAPI for specific mode: ${rendererBuilderOptions.RendererAPI}`,
)
})
Expand Down

0 comments on commit 1b3ea1e

Please sign in to comment.