Implement language detection, simplify URLs

This commit is contained in:
Florine W. Dekker 2020-04-09 16:44:00 +02:00
parent 89a7224816
commit 551ecafce0
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
1 changed files with 34 additions and 14 deletions

View File

@ -40,7 +40,7 @@
<div class="column">
<form>
<label for="url" style="display: inline;">URL</label>
<input id="url" value="https://fallout.fandom.com/api" />
<input id="url" value="https://fallout.fandom.com/api.php" />
<br />
<label for="page" style="display: inline;">Page</label>
<input id="page" autofocus />
@ -183,7 +183,7 @@
* Wraps around the MediawikiJS library.
*
* @property baseUrl {string} the origin of the wiki's API
* @property apiPath {string} the path relative to the wiki's API
* @property apiPath {string} the path relative to the wiki's API; starts with a `/`
*/
class MediaWiki {
/**
@ -206,7 +206,7 @@
* @return {Promise<Object>} the API's response
*/
request(params) {
console.debug(`Requesting from ${this.baseUrl}/${this.apiPath} with param`, params);
console.debug(`Requesting from ${this.baseUrl}${this.apiPath} with params`, params);
return new Promise(resolve => this._mwjs.send(params, it => resolve(it)));
}
@ -234,9 +234,19 @@
}
/**
* Requests all interwiki abbreviations available on this wiki.
* Returns this wiki's general information.
*
* @return {Promise<InterwikiMap>} a mapping from
* @return {Object} this wiki's general information
*/
getGeneralInfo() {
return this.request({action: "query", meta: "siteinfo", siprop: "general"})
.then(response => response.query.general);
}
/**
* Requests this wiki's interwiki map.
*
* @return {Promise<InterwikiMap>} this wiki's interwiki map
*/
getIwMap() {
return this
@ -270,13 +280,16 @@
/**
* Initializes this `MediaWikiManager`.
*
* @param baseLang {string} the language for the `MediaWiki` that is used as a starting point
* @param baseMw {MediaWiki} the `MediaWiki` that is used as a starting point
* @return {MediaWikiManager} this `MediaWikiManager`
*/
async init(baseLang, baseMw) {
async init(baseMw) {
const general = await baseMw.getGeneralInfo();
this.articlePath = "" + general.articlepath;
this.apiPath = baseMw.apiPath;
this.mws[baseLang] = baseMw;
this.mws[general.lang] = baseMw;
await this._importIwMap(baseMw);
return this;
@ -298,7 +311,7 @@
return undefined;
const url = this._iwMap.getUrl(lang);
this.mws[lang] = new MediaWiki(url.slice(0, -7) + "/" + this.apiPath);
this.mws[lang] = new MediaWiki(url.slice(0, -this.articlePath.length) + this.apiPath);
await this._importIwMap(this.mws[lang]);
return this.mws[lang];
@ -370,14 +383,21 @@
// noinspection JSUnresolvedFunction Defined in `common.js`
doAfterLoad(async () => {
const url = "https://fallout.fandom.com/api.php";
const mw = new MediaWiki(url);
const mwm = await new MediaWikiManager().init("en", mw);
const urlInput = $("#url");
const pageInput = $("#page");
const checkButton = $("#check");
const submit = () => {
let previousUrl = undefined;
let mwm = undefined;
const submit = async () => {
if (urlInput.value !== previousUrl) {
console.debug("Creating new MWM for url", urlInput.value);
const mw = new MediaWiki(urlInput.value);
mwm = await new MediaWikiManager().init(mw);
previousUrl = urlInput.value;
}
InterlangNetwork.discoverNetwork(mwm, new InterlangLink("en", pageInput.value))
.then(it => console.log(it));
};