Skip to content
Snippets Groups Projects
Unverified Commit 23678fe2 authored by Seth Falco's avatar Seth Falco Committed by GitHub
Browse files

Add Wikiapiary Extension Badge [WikiapiaryInstalls] (#6678)


* feat: add wikiapiary extension badge

* fix: refactor wikiapiary badge

* fix: display correct message when not found

Co-authored-by: default avatarchris48s <chris48s@users.noreply.github.com>

* fix: weird behavior with casing

* fix: test malformed api response

* chore: use options.qs for query parameters

* chore: rename file to match class name

Co-authored-by: default avatarchris48s <chris48s@users.noreply.github.com>
Co-authored-by: default avatarrepo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
parent 90831e61
No related branches found
No related tags found
No related merge requests found
'use strict'
const Joi = require('joi')
const { metric } = require('../text-formatters')
const { BaseJsonService, NotFound } = require('..')
const documentation = `
<p>
The name of an extension is case-sensitive excluding the first character.
</p>
<p>
For example, in the case of <code>ParserFunctions</code>, the following are
valid:
<ul>
<li><code>ParserFunctions</code></li>
<li><code>parserFunctions</code></li>
</ul>
However, the following are invalid:
<ul>
<li><code>parserfunctions</code></li>
<li><code>Parserfunctions</code></li>
<li><code>pARSERfUNCTIONS</code></li>
</ul>
</p>
`
const schema = Joi.object({
query: Joi.object({
results: Joi.alternatives([
Joi.object()
.required()
.pattern(/^\w+:.+$/, {
printouts: Joi.object({
'Has website count': Joi.array()
.required()
.items(Joi.number().required()),
}).required(),
}),
Joi.array().required(),
]).required(),
}).required(),
}).required()
/**
* This badge displays the total installations of a MediaWiki extensions, skins,
* etc via Wikiapiary.
*
* {@link https://www.mediawiki.org/wiki/Manual:Extensions MediaWiki Extensions Manual}
*/
module.exports = class WikiapiaryInstalls extends BaseJsonService {
static category = 'downloads'
static route = {
base: 'wikiapiary',
pattern: ':variant(extension|skin|farm|generator|host)/installs/:name',
}
static examples = [
{
title: 'Wikiapiary installs',
namedParams: { variant: 'extension', name: 'ParserFunctions' },
staticPreview: this.render({ usage: 11170 }),
documentation,
keywords: ['mediawiki'],
},
]
static defaultBadgeData = { label: 'installs', color: 'informational' }
static render({ usage }) {
return { message: metric(usage) }
}
static validate({ results }) {
if (Array.isArray(results))
throw new NotFound({ prettyMessage: 'not found' })
}
async fetch({ variant, name }) {
return this._requestJson({
schema,
url: `https://wikiapiary.com/w/api.php`,
options: {
qs: {
action: 'ask',
query: `[[${variant}:${name}]]|?Has_website_count`,
format: 'json',
},
},
})
}
async handle({ variant, name }) {
const response = await this.fetch({ variant, name })
const { results } = response.query
this.constructor.validate({ results })
const keyLowerCase = `${variant}:${name.toLowerCase()}`
const resultKey = Object.keys(results).find(
key => keyLowerCase === key.toLowerCase()
)
if (resultKey === undefined)
throw new NotFound({ prettyMessage: 'not found' })
const [usage] = results[resultKey].printouts['Has website count']
return this.constructor.render({ usage })
}
}
'use strict'
const t = (module.exports = require('../tester').createServiceTester())
const { isMetric } = require('../test-validators')
t.create('Extension')
.get('/extension/installs/ParserFunctions.json')
.expectBadge({ label: 'installs', message: isMetric })
t.create('Skins')
.get('/skin/installs/Vector.json')
.expectBadge({ label: 'installs', message: isMetric })
t.create('Extension Not Found')
.get('/extension/installs/FakeExtensionThatDoesNotExist.json')
.expectBadge({ label: 'installs', message: 'not found' })
t.create('Name Lowercase')
.get('/extension/installs/parserfunctions.json')
.expectBadge({ label: 'installs', message: 'not found' })
t.create('Name Title Case')
.get('/extension/installs/parserFunctions.json')
.expectBadge({ label: 'installs', message: isMetric })
t.create('Malformed API Response')
.get('/extension/installs/ParserFunctions.json')
.intercept(nock =>
nock('https://wikiapiary.com')
.get('/w/api.php')
.query({
action: 'ask',
query: '[[extension:ParserFunctions]]|?Has_website_count',
format: 'json',
})
.reply(200, {
query: {
results: {
'Extension:Malformed': { printouts: { 'Has website count': [0] } },
},
},
})
)
.expectBadge({ label: 'installs', message: 'not found' })
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment