RandomSec/extensions/wikidata/module/scripts/wikibase-manager.js
Jathin Sreenivas f5b35a99b3
#3186 : Wikibase login form shows Wikibase instance logo (#3702)
* #3186 : Wikibase login form shows Wikibase instance logo

* Logos shown in the UI during selection of instance to work against

* CSS for list of instances, error handling

* Cache wikibase logo urls.

* Replacing logo alt text as translation string and removing the duplicate property

* Replacing logo alt text as translation string and removing the duplicate property

Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
2021-04-20 21:05:50 +02:00

248 lines
7.3 KiB
JavaScript

/**
* Manages Wikibase instances.
*/
const WikibaseManager = {
selected: "Wikidata",
wikibases: {
"Wikidata": WikidataManifestV1_0 // default one
}
};
WikibaseManager.getSelectedWikibase = function () {
return WikibaseManager.wikibases[WikibaseManager.selected];
};
WikibaseManager.getSelectedWikibaseRoot = function () {
return WikibaseManager.getSelectedWikibase().mediawiki.root;
};
WikibaseManager.getSelectedWikibaseMainPage = function () {
return WikibaseManager.getSelectedWikibase().mediawiki.main_page;
};
WikibaseManager.getSelectedWikibaseApi = function () {
return WikibaseManager.getSelectedWikibase().mediawiki.api;
};
WikibaseManager.getSelectedWikibaseName = function () {
return WikibaseManager.selected;
};
WikibaseManager.getSelectedWikibaseSiteIri = function () {
return WikibaseManager.getSelectedWikibase().wikibase.site_iri;
};
WikibaseManager.getSelectedWikibaseMaxlag = function() {
return WikibaseManager.getSelectedWikibase().wikibase.maxlag;
};
WikibaseManager.getSelectedWikibaseOAuth = function() {
return WikibaseManager.getSelectedWikibase().oauth;
};
WikibaseManager.getSelectedWikibaseEditGroupsURLSchema = function() {
let editgroups = WikibaseManager.getSelectedWikibase().editgroups;
return editgroups ? editgroups.url_schema : null;
};
/**
* Returns the default reconciliation service URL of the Wikibase,
* such as "https://wdreconcile.toolforge.org/${lang}/api".
*
* Notice that there is a "${lang}" variable in the URL, which should
* be replaced with the actual language code.
*/
WikibaseManager.getSelectedWikibaseReconEndpoint = function () {
return WikibaseManager.getSelectedWikibase().reconciliation.endpoint;
};
WikibaseManager.selectWikibase = function (wikibaseName) {
if (WikibaseManager.wikibases.hasOwnProperty(wikibaseName)) {
WikibaseManager.selected = wikibaseName;
}
};
WikibaseManager.getAllWikibases = function () {
return WikibaseManager.wikibases;
};
WikibaseManager.addWikibase = function (manifest) {
WikibaseManager.wikibases[manifest.mediawiki.name] = manifest;
WikibaseManager.saveWikibases();
};
WikibaseManager.removeWikibase = function (wikibaseName) {
delete WikibaseManager.wikibases[wikibaseName];
WikibaseManager.saveWikibases();
};
WikibaseManager.saveWikibases = function () {
let manifests = [];
for (let wikibaseName in WikibaseManager.wikibases) {
if (WikibaseManager.wikibases.hasOwnProperty(wikibaseName)) {
manifests.push(WikibaseManager.wikibases[wikibaseName])
}
}
Refine.wrapCSRF(function (token) {
$.ajax({
async: false,
type: "POST",
url: "command/core/set-preference?" + $.param({
name: "wikibase.manifests"
}),
data: {
"value": JSON.stringify(manifests),
csrf_token: token
},
dataType: "json"
});
});
};
WikibaseManager.loadWikibases = function (onDone) {
$.ajax({
url: "command/core/get-preference?" + $.param({
name: "wikibase.manifests"
}),
success: function (data) {
if (data.value && data.value !== "null" && data.value !== "[]") {
let manifests = JSON.parse(data.value);
manifests.forEach(function (manifest) {
if (manifest.custom && manifest.custom.url && manifest.custom.last_updated
&& ((Date.now() - new Date(manifest.custom.last_updated)) > 7 * 24 * 60 * 60 * 1000)) {
// If the manifest was fetched via URL and hasn't been updated for a week,
// fetch it again to keep track of the lasted version
WikibaseManager.fetchManifestFromURL(manifest.custom.url, function (newManifest) {
WikibaseManager.wikibases[newManifest.mediawiki.name] = newManifest;
WikibaseManager.saveWikibases();
}, function () {
// fall back to the current one if failed to fetch the latest one
WikibaseManager.wikibases[manifest.mediawiki.name] = manifest;
}, true)
} else {
WikibaseManager.wikibases[manifest.mediawiki.name] = manifest;
}
});
WikibaseManager.selected = WikibaseManager.selectDefaultWikibaseAccordingToSavedSchema();
if (onDone) {
onDone();
}
}
},
dataType: "json"
});
};
WikibaseManager.selectDefaultWikibaseAccordingToSavedSchema = function () {
let schema = theProject.overlayModels.wikibaseSchema || {};
if (!schema.siteIri) {
return "Wikidata";
}
for (let wikibaseName in WikibaseManager.wikibases) {
if (WikibaseManager.wikibases.hasOwnProperty(wikibaseName)) {
let wikibase = WikibaseManager.wikibases[wikibaseName];
if (schema.siteIri === wikibase.wikibase.site_iri) {
return wikibase.mediawiki.name;
}
}
}
return "Wikidata";
};
WikibaseManager.fetchManifestFromURL = function (manifestURL, onSuccess, onError, silent) {
let dismissBusy = function() {};
if (!silent) {
dismissBusy = DialogSystem.showBusy($.i18n("wikibase-management/contact-service") + "...");
}
let _onSuccess = function (data) {
// record custom information in the manifest
data.custom = {};
data.custom.url = manifestURL;
data.custom.last_updated = Date.now();
if (onSuccess) {
onSuccess(data);
}
};
// The manifest host must support CORS.
$.ajax(manifestURL, {
"dataType": "json",
"timeout": 5000
}).success(function (data) {
dismissBusy();
_onSuccess(data);
}).error(function (jqXHR, textStatus, errorThrown) {
dismissBusy();
if (!silent) {
alert($.i18n("wikibase-management/error-contact")+": " + textStatus + " : " + errorThrown + " - " + manifestURL);
}
if (onError) {
onError();
}
});
};
var wikibaseLogoURLCache = {
data: {},
exist: function (url) {
return wikibaseLogoURLCache.data.hasOwnProperty(url);
},
get: function (url) {
return wikibaseLogoURLCache.data[url];
},
set: function (url, cachedData) {
wikibaseLogoURLCache.data[url] = cachedData.responseJSON.query.general.logo;
}
};
// Retrives the wikibaseName instance site info, if wikibaseName is empty returns the selected wikibase site info
WikibaseManager.retrieveLogoUrlFromSiteInfo = function(onSuccess, onError, wikibaseName) {
var params = {
action: 'query',
meta: 'siteinfo',
format: 'json'
};
let wikibase = (wikibaseName) ? WikibaseManager.wikibases[wikibaseName] : WikibaseManager.getSelectedWikibase();
const url = wikibase.mediawiki.api;
return $.ajax({
url: url,
data: params,
dataType: "jsonp",
timeout: 1000,
cache: true,
beforeSend: function () {
if (wikibaseLogoURLCache.exist(url)) {
onSuccess(wikibaseLogoURLCache.get(url));
return false;
}
return true;
},
complete: function (jqXHR, textStatus) {
wikibaseLogoURLCache.set(url, jqXHR);
},
success: function(response) {
onSuccess(response.query.general.logo);
},
error: function(xhr, status, error) {
onError(xhr, status, error);
},
});
};
// Retrives the logo url of wikibaseName, if wikibaseName is empty returns the selected wikibase logo url
WikibaseManager.getSelectedWikibaseLogoURL = function(onDone, wikibaseName) {
WikibaseManager.retrieveLogoUrlFromSiteInfo(function(data) {
onDone(data);
}, function(xhr, status, error) {
onDone("extension/wikidata/images/Wikibase_logo.png");
}, wikibaseName);
};