Skip to content

Commit

Permalink
Merge pull request #2041 from openzim/visual-editor-check
Browse files Browse the repository at this point in the history
Check for application/json when checking visual editor api
  • Loading branch information
kelson42 authored Jun 18, 2024
2 parents 67c8cdf + 7d680e8 commit 46b2d44
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/MediaWiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ class MediaWiki {
public async hasVisualEditorApi(): Promise<boolean> {
if (this.#hasVisualEditorApi === null) {
this.visualEditorUrlDirector = new VisualEditorURLDirector(this.visualEditorApiUrl.href)
this.#hasVisualEditorApi = await checkApiAvailability(this.visualEditorUrlDirector.buildArticleURL(this.apiCheckArticleId))
this.#hasVisualEditorApi = await checkApiAvailability(
this.visualEditorUrlDirector.buildArticleURL(this.apiCheckArticleId),
'' /* empty login cookie */,
this.visualEditorUrlDirector.validMimeTypes,
)
return this.#hasVisualEditorApi
}
return this.#hasVisualEditorApi
Expand Down
4 changes: 4 additions & 0 deletions src/util/builders/url/visual-editor.director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export default class VisualEditorURLDirector {
buildArticleURL(articleId: string) {
return urlBuilder.setDomain(this.baseDomain).setQueryParams({ page: articleId }, '&').build()
}

get validMimeTypes() {
return ['application/json']
}
}
26 changes: 24 additions & 2 deletions src/util/mw-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,16 @@ export function mwRetToArticleDetail(obj: QueryMwRet): KVS<ArticleDetail> {
return ret
}

export async function checkApiAvailability(url: string, loginCookie = ''): Promise<boolean> {
/**
* Check for API availability at the given URL.
*
* @param url The URL to check.
* @param loginCookie A string representing a cookie for login, if necessary.
* @param allowedMimeTypes An array of allowed mime types for the response. If this is set, the check is only considered a
* success if the response has a mime type in this array. Set to null to disable this filter.
* @returns Promise resolving to true if the API is available.
*/
export async function checkApiAvailability(url: string, loginCookie = '', allowedMimeTypes = null): Promise<boolean> {
try {
const resp = await axios.get(decodeURI(url), { maxRedirects: 0, headers: { cookie: loginCookie } })

Expand All @@ -268,7 +277,20 @@ export async function checkApiAvailability(url: string, loginCookie = ''): Promi
// the 'mediawiki-api-error' === 'rest-permission-error' exception
const isSuccess = resp.status === 200 && (!resp.headers['mediawiki-api-error'] || resp.headers['mediawiki-api-error'] === 'rest-permission-error')

return !isRedirectPage && isSuccess
let validMimeType = false
if (!allowedMimeTypes) {
// No MIME types to check, so consider the check passed.
validMimeType = true
} else {
for (const mimeType of allowedMimeTypes) {
if (resp.headers['content-type'].includes(mimeType)) {
validMimeType = true
break
}
}
}

return !isRedirectPage && isSuccess && validMimeType
} catch (err) {
return false
}
Expand Down
6 changes: 2 additions & 4 deletions test/unit/mwApiCapabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ describe('Checking Mediawiki capabilities', () => {
expect(await MediaWiki.hasVisualEditorApi()).toBe(true)
})

// https://github.com/openzim/mwoffliner/issues/2035
test.skip('test capabilities of pokemon.fandom.com with default receipt', async () => {
test('test capabilities of pokemon.fandom.com with default receipt', async () => {
MediaWiki.base = 'https://pokemon.fandom.com/'

expect(await MediaWiki.hasWikimediaDesktopApi()).toBe(false)
Expand All @@ -67,8 +66,7 @@ describe('Checking Mediawiki capabilities', () => {
expect(await MediaWiki.hasVisualEditorApi()).toBe(false)
})

// https://github.com/openzim/mwoffliner/issues/2035
test.skip('test capabilities of pokemon.fandom.com with RestApi receipt', async () => {
test('test capabilities of pokemon.fandom.com with RestApi receipt', async () => {
MediaWiki.base = 'https://pokemon.fandom.com/'
MediaWiki.wikiPath = '/'
MediaWiki.restApiPath = 'rest.php'
Expand Down

0 comments on commit 46b2d44

Please sign in to comment.