Skip to content

Commit

Permalink
Optimize static files saving
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKovalenkoSNF committed Oct 3, 2023
1 parent 5a851c6 commit a5d6edf
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 55 deletions.
1 change: 0 additions & 1 deletion src/Dump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ interface DumpOpts {
keepEmptyParagraphs: boolean
tags?: string
filenameDate: string
isMobileRenderer: boolean
}

export class Dump {
Expand Down
4 changes: 2 additions & 2 deletions src/MediaWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class MediaWiki {
this.apiUrl = baseUrlDirector.buildURL(this.#apiActionPath)
this.apiUrlDirector = new ApiURLDirector(this.apiUrl.href)
this.visualEditorApiUrl = this.apiUrlDirector.buildVisualEditorURL()
this.desktopApiUrl = baseUrlDirector.buildDesktopApiUrl(this.#apiPath)
this.mobileApiUrl = baseUrlDirector.buildMobileApiUrl(this.#apiPath)
this.desktopApiUrl = baseUrlDirector.buildWikimediaDesktopApiUrl(this.#apiPath)
this.mobileApiUrl = baseUrlDirector.buildWikimediaMobileApiUrl(this.#apiPath)
this.modulePath = baseUrlDirector.buildModuleURL(this._modulePathOpt)
this.mobileModulePath = baseUrlDirector.buildMobileModuleURL()
this.wikimediaDesktopUrlDirector = new DesktopURLDirector(this.desktopApiUrl.href)
Expand Down
7 changes: 2 additions & 5 deletions src/mwoffliner.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ async function execute(argv: any) {
keepEmptyParagraphs,
tags: customZimTags,
filenameDate,
isMobileRenderer: false,
},
{ ...mwMetaData, mainPage },
customProcessor,
Expand Down Expand Up @@ -401,12 +400,10 @@ async function execute(argv: any) {
})
zimCreator.addArticle(scraperArticle)

logger.info('Copying Static Resource Files')
await saveStaticFiles(config, zimCreator)

// TODO: refactor sequence, this only needed for mobile renderer
logger.info('Copying Static PCS Override Files')
await saveStaticPCSFiles(config, zimCreator)
logger.info('Copying Static Resource Files')
await saveStaticFiles(config, zimCreator)

logger.info('Finding stylesheets to download')
const stylesheetsToGet = await dump.getRelevantStylesheetUrls(downloader)
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/visual-editor.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class VisualEditorRenderer extends DesktopRenderer {
articleDetail,
_moduleDependencies,
webp,
super.templateDesktopArticle(_moduleDependencies, articleId),
super.templateDesktopArticle.bind(this),
)
result.push({
articleId,
Expand Down
1 change: 0 additions & 1 deletion src/renderers/wikimedia-desktop.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class WikimediaDesktopRenderer extends DesktopRenderer {
public async render(renderOpts: RenderOpts): Promise<any> {
const result: RenderOutput = []
const { data, articleId, articleDetailXId, webp, _moduleDependencies, isMainPage, dump } = renderOpts
dump.opts.isMobileRenderer = false
const articleDetail = await renderOpts.articleDetailXId.get(articleId)

// Paginate when there are more than 200 subCategories
Expand Down
1 change: 0 additions & 1 deletion src/renderers/wikimedia-mobile.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export class WikimediaMobileRenderer extends MobileRenderer {
try {
const result: RenderOutput = []
const { data, articleId, webp, _moduleDependencies, dump } = renderOpts
dump.opts.isMobileRenderer = true
const articleDetail = await renderOpts.articleDetailXId.get(articleId)

const displayTitle = this.getStrippedTitle(renderOpts)
Expand Down
4 changes: 2 additions & 2 deletions src/util/builders/url/base.director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export default class BaseURLDirector {
.build(true, '/')
}

buildDesktopApiUrl(path?: string) {
buildWikimediaDesktopApiUrl(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'api/rest_v1/page/html')
.build(true, '/')
}

buildMobileApiUrl(path?: string) {
buildWikimediaMobileApiUrl(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'api/rest_v1/page/mobile-html')
Expand Down
51 changes: 17 additions & 34 deletions src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,49 +162,32 @@ export function interpolateTranslationString(str: string, parameters: { [key: st
return newString
}

export function saveStaticFiles(config: Config, zimCreator: ZimCreator) {
const cssPromises = config.output.cssResources.concat(config.output.mainPageCssResources).map(async (css) => {
function saveResourceFile(resource: string, type: 'css' | 'js', basePath: string, config: Config, zimCreator: ZimCreator) {
return async () => {
try {
const cssCont = await readFilePromise(pathParser.resolve(__dirname, `../../res/${css}.css`))
const article = new ZimArticle({ url: cssPath(css), data: cssCont, ns: '-' })
const content = await readFilePromise(pathParser.resolve(__dirname, `../../res/${basePath}${resource}.${type}`))
const article = new ZimArticle({
url: type === 'css' ? cssPath(resource) : jsPath(resource),
data: content,
ns: '-',
})
zimCreator.addArticle(article)
} catch (error) {
logger.warn(`Could not create ${css} file : ${error}`)
const fileType = type === 'css' ? (basePath.includes('pcs') ? 'style PCS override' : 'style') : 'script'
logger.warn(`Could not create ${fileType} ${resource} file : ${error}`)
}
})
}
}

const jsPromises = config.output.jsResources.map(async (js) => {
try {
const jsCont = await readFilePromise(pathParser.resolve(__dirname, `../../res/${js}.js`))
const article = new ZimArticle({ url: jsPath(js), data: jsCont, ns: '-' })
zimCreator.addArticle(article)
} catch (error) {
logger.warn(`Could not create ${js} file : ${error}`)
}
})
export function saveStaticFiles(config: Config, zimCreator: ZimCreator) {
const cssPromises = config.output.cssResources.concat(config.output.mainPageCssResources).map((css) => saveResourceFile(css, 'css', '', config, zimCreator)())
const jsPromises = config.output.jsResources.map((js) => saveResourceFile(js, 'js', '', config, zimCreator)())
return Promise.all([...cssPromises, ...jsPromises])
}

export function saveStaticPCSFiles(config: Config, zimCreator: ZimCreator) {
const pcsCssPromises = config.output.pcsCssResources.map(async (pcsCss) => {
try {
const cssCont = await readFilePromise(pathParser.resolve(__dirname, `../../res/pcs/${pcsCss}.css`))
const article = new ZimArticle({ url: cssPath(pcsCss), data: cssCont, ns: '-' })
zimCreator.addArticle(article)
} catch (error) {
logger.warn(`Could not create style PCS override ${pcsCss} file : ${error}`)
}
})

const pcsJsPromises = config.output.pcsJsResources.map(async (pcsJs) => {
try {
const jsCont = await readFilePromise(pathParser.resolve(__dirname, `../../res/pcs/${pcsJs}.js`))
const article = new ZimArticle({ url: jsPath(pcsJs), data: jsCont, ns: '-' })
zimCreator.addArticle(article)
} catch (error) {
logger.warn(`Could not create script PCS override ${pcsJs} file : ${error}`)
}
})
const pcsCssPromises = config.output.pcsCssResources.map((pcsCss) => saveResourceFile(pcsCss, 'css', 'pcs/', config, zimCreator)())
const pcsJsPromises = config.output.pcsJsResources.map((pcsJs) => saveResourceFile(pcsJs, 'js', 'pcs/', config, zimCreator)())
return Promise.all([...pcsCssPromises, ...pcsJsPromises])
}

Expand Down
5 changes: 3 additions & 2 deletions test/e2e/mobileRenderIntegrity.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dotenv/config.js'
import * as mwoffliner from '../../src/mwoffliner.lib.js'
import * as logger from '../../src/Logger.js'
import rimraf from 'rimraf'
import { execa } from 'execa'
import { jest } from '@jest/globals'
Expand Down Expand Up @@ -34,8 +35,8 @@ async function getOutFiles(testId: string, articleList: string, mwUrl: string):
const commonTreatmentTest = async (articleList: string, mwUrl: string) => {
if (!zimcheckIsAvailable || !zimdumpIsAvailable) {
const missingTool = !zimcheckIsAvailable ? 'Zimcheck' : 'Zimdump'
console.log(`${missingTool} not installed, skipping test`)
return
logger.error(`${missingTool} not installed, exiting test`)
process.exit(1)
}
const now = new Date()
const testId = `mwo-test-${+now}`
Expand Down
12 changes: 6 additions & 6 deletions test/unit/builders/url/base.director.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ describe('BaseURLDirector', () => {
})
})

describe('buildMobileApiUrl', () => {
describe('buildWikimediaMobileApiUrl', () => {
it('should return mobile rest URL with provided path and trailing char', () => {
const url = baseUrlDirector.buildMobileApiUrl('api/rest_v2/page/mobile-html')
const url = baseUrlDirector.buildWikimediaMobileApiUrl('api/rest_v2/page/mobile-html')

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v2/page/mobile-html/')
})

it('should return mobile rest URL with default path and trailing char', () => {
const url = baseUrlDirector.buildMobileApiUrl()
const url = baseUrlDirector.buildWikimediaMobileApiUrl()

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v1/page/mobile-html/')
})
})

describe('buildDesktopApiUrl', () => {
describe('buildWikimediaDesktopApiUrl', () => {
it('should return a desktop URL with provided path and trailing char', () => {
const url = baseUrlDirector.buildDesktopApiUrl('api/rest_v2/page/html')
const url = baseUrlDirector.buildWikimediaDesktopApiUrl('api/rest_v2/page/html')

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v2/page/html/')
})

it('should return a desktop URL with default path and trailing char', () => {
const url = baseUrlDirector.buildDesktopApiUrl()
const url = baseUrlDirector.buildWikimediaDesktopApiUrl()

expect(url.href).toBe('https://en.m.wikipedia.com/api/rest_v1/page/html/')
})
Expand Down

0 comments on commit a5d6edf

Please sign in to comment.