Skip to content

Commit

Permalink
Return to forward slash mandatory before paths to support root wikis
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Feb 18, 2025
1 parent 92435b2 commit 3b785ac
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/MediaWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ class MediaWiki {
this.#password = ''
this.getCategories = false

this.#actionApiPath = 'w/api.php'
this.#restApiPath = 'w/rest.php'
this.#wikiPath = 'wiki/'
this.#modulePathOpt = 'w/load.php'
this.#actionApiPath = '/w/api.php'
this.#restApiPath = '/w/rest.php'
this.#wikiPath = '/wiki/'
this.#modulePathOpt = '/w/load.php'

this.namespaces = {}
this.namespacesToMirror = []
Expand Down
4 changes: 4 additions & 0 deletions src/mwoffliner.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ async function execute(argv: any) {

if (customMainPage) {
mainPage = customMainPage
logger.log(`webURL: ${MediaWiki.webUrl}`)
logger.log(`mainPage: ${mainPage}`)
logger.log(`encodeURIComponent: ${encodeURIComponent(mainPage)}`)
const mainPageUrl = MediaWiki.webUrl + encodeURIComponent(mainPage)
logger.log(`mainPageUrl: ${mainPageUrl}`)
if (!(await checkApiAvailability(downloader, mainPageUrl))) {
throw new Error(`customMainPage doesn't return 200 status code for url ${mainPageUrl}`)
}
Expand Down
6 changes: 3 additions & 3 deletions src/sanitize-argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ export function sanitizeApiPathParam(apiPathParam: string) {
return
}

// No api params should start from forward slash
if (apiPathParam.startsWith('/')) {
apiPathParam = apiPathParam.slice(1)
// All API path must start with a forward slash
if (!apiPathParam?.startsWith('/')) {
apiPathParam = '/' + apiPathParam
}

return apiPathParam
Expand Down
8 changes: 4 additions & 4 deletions src/util/builders/url/base.director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class BaseURLDirector {
private baseDomain: string

constructor(baseDomain: string) {
this.baseDomain = baseDomain
this.baseDomain = baseDomain.endsWith('/') ? baseDomain.substring(0, baseDomain.length - 1) : baseDomain
}

buildURL(path: string) {
Expand All @@ -32,21 +32,21 @@ export default class BaseURLDirector {
buildModuleURL(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'w/load.php')
.setPath(path ?? '/w/load.php')
.build(false, '?')
}

buildMobileModuleURL(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'api/rest_v1/page/mobile-html-offline-resources')
.setPath(path ?? '/api/rest_v1/page/mobile-html-offline-resources')
.build(false, '/')
}

buildRestApiUrl(path?: string) {
return urlBuilder
.setDomain(this.baseDomain)
.setPath(path ?? 'w/rest.php')
.setPath(path ?? '/w/rest.php')
.build(true, '/')
}
}
2 changes: 1 addition & 1 deletion src/util/builders/url/basic.director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type DownloaderBaseUrlConditions = Array<{ condition: boolean; value: string }>
*/
class BasicURLDirector {
buildMediawikiBaseURL(domain: string) {
return urlBuilder.setDomain(domain).build(true, '/')
return urlBuilder.setDomain(domain).build(true, '')
}

buildDownloaderBaseUrl(conditions: DownloaderBaseUrlConditions): string | undefined {
Expand Down
2 changes: 1 addition & 1 deletion src/util/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const WEBP_HANDLER_URL = 'https://gist.githubusercontent.com/rgaudin/60bb
export const MAX_FILE_DOWNLOAD_RETRIES = 5
export const BLACKLISTED_NS = ['Story'] // 'Story' Wikipedia namespace is content, but not indgestable by Parsoid https://github.com/openzim/mwoffliner/issues/1853
export const RENDERERS_LIST = ['WikimediaDesktop', 'VisualEditor', 'WikimediaMobile', 'RestApi']
export const WIKIMEDIA_REST_API_PATH = 'api/rest_v1/'
export const WIKIMEDIA_REST_API_PATH = '/api/rest_v1/'

/*
Handle redirection pages for 3rd party wikis that have 200 response code
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/apiPathParamsSanitizing.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ const parameters = {
mwActionApiPath: sanitizeApiPathParam('/w/api.php'),
mwRestApiPath: sanitizeApiPathParam('/w/rest.php'),
mwModulePath: sanitizeApiPathParam('/w/load.php'),
mwWikiPath: sanitizeWikiPath('wiki'),
mwWikiPath: sanitizeWikiPath('/wiki'),
}

await testAllRenders(parameters, async (outFiles) => {
describe(`e2e test for api url params for en.wikipedia.org for ${outFiles[0]?.renderer} renderer`, () => {
test('Mediawiki actionApiPath option sanitized', () => {
expect(outFiles[0].mwMetaData.actionApiPath).toBe('w/api.php')
expect(outFiles[0].mwMetaData.actionApiPath).toBe('/w/api.php')
})

test('Mediawiki restApiPath option sanitized', () => {
expect(outFiles[0].mwMetaData.restApiPath).toBe('w/rest.php')
expect(outFiles[0].mwMetaData.restApiPath).toBe('/w/rest.php')
})

test('Mediawiki wikiPath option sanitized', () => {
expect(outFiles[0].mwMetaData.wikiPath).toBe('wiki/')
expect(outFiles[0].mwMetaData.wikiPath).toBe('/wiki/')
})

test('Mediawiki modulePathOpt option sanitized', () => {
expect(outFiles[0].mwMetaData.modulePathOpt).toBe('w/load.php')
expect(outFiles[0].mwMetaData.modulePathOpt).toBe('/w/load.php')
})

test('Mediawiki modulePath and actionApiUrl options', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/builders/url/base.director.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ describe('BaseURLDirector', () => {

describe('buildURL', () => {
it('should return URL object with path', () => {
const url = baseUrlDirector.buildURL('v1/test/api')
const url = baseUrlDirector.buildURL('/v1/test/api')

expect(url.href).toBe('https://en.m.wikipedia.com/v1/test/api')
})

it('should return URL object with mwActionApiPath param', () => {
const url = baseUrlDirector.buildURL('api.php')
const url = baseUrlDirector.buildURL('/api.php')

expect(url.href).toBe('https://en.m.wikipedia.com/api.php')
})
Expand All @@ -35,7 +35,7 @@ describe('BaseURLDirector', () => {

describe('buildModuleURL', () => {
it('should return a module URL with provided path and question mark as a trailing char', () => {
const url = baseUrlDirector.buildModuleURL('w/reload.php')
const url = baseUrlDirector.buildModuleURL('/w/reload.php')

expect(url).toBe('https://en.m.wikipedia.com/w/reload.php?')
})
Expand Down
6 changes: 3 additions & 3 deletions test/unit/mwApiCapabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Checking Mediawiki capabilities', () => {
test('test capabilities of minecraft.wiki with correct VisualEditor receipt', async () => {
MediaWiki.base = 'https://minecraft.wiki'
MediaWiki.wikiPath = '/'
MediaWiki.actionApiPath = 'api.php'
MediaWiki.actionApiPath = '/api.php'
const downloader = new Downloader({ uaString: `${config.userAgent} ([email protected])`, speed: 1, reqTimeout: 1000 * 60, webp: true, optimisationCacheUrl: '' })

expect(await MediaWiki.hasWikimediaDesktopApi(downloader)).toBe(false)
Expand All @@ -58,7 +58,7 @@ describe('Checking Mediawiki capabilities', () => {
test('test capabilities of pokemon.fandom.com with correct VisualEditor receipt', async () => {
MediaWiki.base = 'https://pokemon.fandom.com/'
MediaWiki.wikiPath = '/'
MediaWiki.actionApiPath = 'api.php'
MediaWiki.actionApiPath = '/api.php'
const downloader = new Downloader({ uaString: `${config.userAgent} ([email protected])`, speed: 1, reqTimeout: 1000 * 60, webp: true, optimisationCacheUrl: '' })

expect(await MediaWiki.hasWikimediaDesktopApi(downloader)).toBe(false)
Expand All @@ -80,7 +80,7 @@ describe('Checking Mediawiki capabilities', () => {
test('test capabilities of pokemon.fandom.com with RestApi receipt', async () => {
MediaWiki.base = 'https://pokemon.fandom.com/'
MediaWiki.wikiPath = '/'
MediaWiki.restApiPath = 'rest.php'
MediaWiki.restApiPath = '/rest.php'
const downloader = new Downloader({ uaString: `${config.userAgent} ([email protected])`, speed: 1, reqTimeout: 1000 * 60, webp: true, optimisationCacheUrl: '' })

expect(await MediaWiki.hasWikimediaDesktopApi(downloader)).toBe(false)
Expand Down

0 comments on commit 3b785ac

Please sign in to comment.