/*
The code is based on MediaWiki VisualEditor extension mediawiki-extensions-Citoid: [https://github.com/wikimedia/mediawiki-extensions-Citoid/blob/master/modules/ve/ve.ui.CitoidInspector.js].
The deriviative work is also pubished under the terms of the MIT license.
Modifications to Citoid extension, copyright (c) 2023 D6194c-1cc.
Citoid extension, copyright (c) 2014-2015 VisualEditor team and others under the terms
of The MIT License (MIT), as follows:
Citoid extension consists of voluntary contributions made by several
individuals. For exact contribution history, see the revision history
and logs, available at https://gerrit.wikimedia.org
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function injectCitoidInspectorWD() {
$.ajax({
url: '/w/index.php?title=Участник:D6194c-1cc/ve.ui.CitoidInspectorWD.l10n.json',
data: {
action: 'raw',
ctype: 'text/json'
},
dataType: 'json',
success: function(messages) {
var lang = mw.config.get('wgUserLanguage') || 'en';
mw.messages.set(messages[lang] || messages['en']);
}
});
mw.loader.load('/w/index.php?title=Участник:D6194c-1cc/ve.ui.CitoidInspectorWD.css&action=raw&ctype=text/css', 'text/css');
ve.ui.CitoidInspectorWD = function(config) {
ve.ui.CitoidInspector.super.call(this, ve.extendObject({ padded: false }, config));
};
OO.inheritClass(ve.ui.CitoidInspectorWD, ve.ui.CitoidInspector);
ve.ui.CitoidInspectorWD.prototype.initialize = function() {
ve.ui.CitoidInspector.prototype.initialize.call(this);
// Search Wikidata field
this.searchInputWD = new OO.ui.TextInputWidget({
placeholder: new OO.ui.deferMsg('citoid-citoiddialog-search-wd-placeholder'),
});
this.searchButtonWD = new OO.ui.ButtonWidget({
label: new OO.ui.deferMsg('citoid-citoiddialog-search-wd-button'),
});
var searchActionFieldLayoutWD = new OO.ui.ActionFieldLayout(this.searchInputWD, this.searchButtonWD, {
align: 'top',
label: new OO.ui.deferMsg('citoid-citoiddialog-search-wd-action-field'),
});
this.autoProcessPanels.lookup.$element.append(searchActionFieldLayoutWD.$element, this.$noticeLabel);
this.menuWD = new OO.ui.SelectWidget({
widget: this.searchButtonWD,
classes: [ 'wikidataReferencesMenu' ],
});
this.autoProcessPanels.lookup.$element.append(this.menuWD.$element, this.$noticeLabel);
this.searchInputWD.connect(this, {
enter: 'onSearchInputWDEnter',
});
this.searchButtonWD.connect(this, { click: 'onSearchButtonWDClick' });
this.menuWD.connect(this, { choose: 'onMenuWDChoose' });
};
function addEntity(inspector, entity) {
var menu = inspector.menuWD;
$.ajax({
url: '//ru.wikipedia.org/w/api.php',
data: {
action: 'parse',
prop: 'text',
text: '{{Источник информации|' + entity + '}}',
disablelimitreport: true,
contentmodel: 'wikitext',
format: 'json',
},
success: function(data) {
var parsedText = data.parse.text['*'].replace(/^<div class="mw-parser-output"><p>/, '<div class="mw-parser-output">').replace(/<\/p><\/div>$/, '').replace(/\n$/, '</div>');
menu.addItems(
new OO.ui.OptionWidget({
data: entity,
$element: $(parsedText),
})
);
inspector.updateSize();
}
});
}
ve.ui.CitoidInspector.prototype.onSearchInputWDEnter = function() {
if (!this.searchInputWD.isDisabled()) {
this.onSearchButtonWDClick();
}
};
function filterAndAddEntities(inspector, entities) {
// Get editions or entities that has "published in" property set:
var sparqlQuery = `SELECT DISTINCT ?item
WHERE
{
VALUES ?item { ` + entities.join(' ') + ` }
VALUES ?edition { wd:Q3331189 wd:Q2568454 wd:Q57933693 wd:Q1238720 wd:Q28869365 wd:Q60534442 wd:Q13442814 }
{ ?item wdt:P31 ?edition. }
UNION
{ ?item wdt:P1433 ?_. }
}
ORDER BY ?item
LIMIT 20`;
$.ajax({
url: 'https://query.wikidata.org/bigdata/namespace/wdq/sparql',
data: {
query: sparqlQuery,
format: 'json',
},
dataType: 'json',
success: function(reply) {
for (var i in reply.results.bindings) {
var binding = reply.results.bindings[i];
var entity = binding.item.value.replace('http://www.wikidata.org/entity/', '');
addEntity(inspector, entity);
}
}
});
}
ve.ui.CitoidInspector.prototype.onSearchButtonWDClick = function() {
// FIXME: Перенести в отдельное действие
this.menuWD.clearItems();
this.updateSize();
var searchString = this.searchInputWD.getValue();
if (!searchString) {
return;
}
var inspector = this;
$.ajax({
url: '//www.wikidata.org/w/api.php',
data: {
action: 'query',
list: 'search',
srsearch: searchString,
srlimit: 20,
srprop: 'titlesnippet|snippet',
format: 'json'
},
dataType: 'jsonp',
success: function (results) {
var prefixedEntities = [];
for (var i in results.query.search) {
var entity = results.query.search[i].title;
prefixedEntities.push('wd:' + entity);
}
filterAndAddEntities(inspector, prefixedEntities);
}
});
};
function getRefTemplate(entity) {
var template = [
{
type: 'mwTransclusionInline',
attributes: {
mw: {
parts: [
{
template: {
target: {
wt: 'Источник информации',
},
params: {
1: { wt: entity },
},
},
},
],
},
},
},
{
type: '/mwTransclusionInline',
},
];
return template;
}
ve.ui.CitoidInspector.prototype.onMenuWDChoose = function(item, selected) {
var surfaceModel = ve.init.target.getSurface().getModel();
// Create reference into which the template would be inserted:
var refModel = new ve.dm.MWReferenceModel(surfaceModel.getDocument());
refModel.insertInternalItem(surfaceModel);
var fragment = this.getFragment();
refModel.insertReferenceNode(fragment);
// Get an empty paragraph that the newly created reference holds:
var emptyParagraph = refModel.findInternalItem(surfaceModel).getChildren()[0];
// Get fragment that is inside the reference:
var refFragment = surfaceModel.getFragment(new ve.dm.LinearSelection(emptyParagraph.getRange()));
// Template data to be inserted:
var template = getRefTemplate(item.data);
refFragment.insertContent(template);
// Without applying changes they will be lost while closing the dialog:
fragment.getSurface().applyAllStaging();
// Place cursor after the inserted reference to continue editing:
fragment.collapseToEnd().select();
ve.track('activity.' + this.constructor.static.name, { action: 'auto-choose' });
// Close the CitoidInspector dialog:
this.close({ action: 'auto-lookup' });
};
ve.ui.windowFactory.unregister(ve.ui.CitoidInspector);
ve.ui.windowFactory.register(ve.ui.CitoidInspectorWD);
}
$(document).ready(function() {
mw.hook('ve.activationComplete').add(function() {
injectCitoidInspectorWD();
});
});