Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Tests.gs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ function _runTests() {
var checkResult = function(caller, result) {
Logger.log(caller + ' ' + (result.length ?
'✅ OK: ' + JSON.stringify(result) : '❌ Error'));
}

var check2dArrayInputResult = function(caller, result) {
var pass = result.map(row => row.every(col => col)).every(row => row)
Logger.log(caller + ' 2D array check ' + (pass ?
'✅ 2D array OK: ' + JSON.stringify(result) : '❌ 2D array Error'));
};

var project = 'en.wikipedia';
Expand Down Expand Up @@ -65,6 +71,7 @@ function _runTests() {
article.replace('en:', '')));

checkResult('WIKIDATAQID', WIKIDATAQID(article));
check2dArrayInputResult('WIKIDATAQID', WIKIDATAQID([[article], [article]]));
checkResult('WIKIDATAQID', WIKIDATAQID(article.replace('en:', '')));

checkResult('WIKIDATALOOKUP', WIKIDATALOOKUP('P298', 'AUT'));
Expand Down
71 changes: 38 additions & 33 deletions Wikipedia.gs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1408,45 +1408,50 @@ function WIKISEARCH(query, opt_didYouMean, opt_namespaces) {
/**
* Returns the Wikidata qid of the corresponding Wikidata item for a Wikipedia article.
*
* @param {string} article The article in the format "language:Query" ("de:Berlin") to get the Wikidata qid for.
* @return {string} The Wikidata qid.
* @param {string|string[]} article The article(s) in the format "language:Query" ("de:Berlin") to get the Wikidata qid for.
* @return {string|string[]} The Wikidata qid(s).
* @customfunction
*/
function WIKIDATAQID(article) {
'use strict';
if (!article) {
return '';
}
var results = [];
try {
var language;
var title;
if (article.indexOf(':') !== -1) {
language = article.split(/:(.+)?/)[0];
title = article.split(/:(.+)?/)[1];
} else {
language = DEFAULT_LANGUAGE;
title = article;
}
if (!title) {
function WIKIDATAQID(input) {
function process(article) {
'use strict';
if (!article) {
return '';
}
var url = 'https://' + language + '.wikipedia.org/w/api.php' +
'?action=query' +
'&format=json' +
'&formatversion=2' +
'&redirects=1' +
'&prop=pageprops' +
'&ppprop=wikibase_item' +
'&titles=' + encodeURIComponent(title);
var json = JSON.parse(UrlFetchApp.fetch(url, HEADERS).getContentText());
if (json.query.pages[0] && json.query.pages[0].pageprops.wikibase_item) {
results[0] = json.query.pages[0].pageprops.wikibase_item;
var results = '';
try {
var language;
var title;
if (article.indexOf(':') !== -1) {
language = article.split(/:(.+)?/)[0];
title = article.split(/:(.+)?/)[1];
} else {
language = DEFAULT_LANGUAGE;
title = article;
}
if (!title) {
return '';
}
var url = 'https://' + language + '.wikipedia.org/w/api.php' +
'?action=query' +
'&format=json' +
'&formatversion=2' +
'&redirects=1' +
'&prop=pageprops' +
'&ppprop=wikibase_item' +
'&titles=' + encodeURIComponent(title);
var json = JSON.parse(UrlFetchApp.fetch(url, HEADERS).getContentText());
if (json.query.pages[0] && json.query.pages[0].pageprops.wikibase_item) {
results = json.query.pages[0].pageprops.wikibase_item;
}
} catch (e) {
// no-op
}
} catch (e) {
// no-op
return results;
}
return results.length > 0 ? results : '';
return Array.isArray(input) ?
input.map(row => row.map(cell => process(cell))) :
process(input);
}

/**
Expand Down