getDocs: function()

in modules/services/osm_wikibase.js [238:332]


    getDocs: function(params, callback) {
        var that = this;
        var langCodes = localizer.localeCodes().map(function(code) {
            return code.toLowerCase();
        });
        params.langCodes = langCodes;

        this.getEntity(params, function(err, data) {
            if (err) {
                callback(err);
                return;
            }

            var entity = data.rtype || data.tag || data.key;
            if (!entity) {
                callback('No entity');
                return;
            }

            var i;
            var description;
            for (i in langCodes) {
                let code = langCodes[i];
                if (entity.descriptions[code] && entity.descriptions[code].language === code) {
                    description = entity.descriptions[code];
                    break;
                }
            }
            if (!description && Object.values(entity.descriptions).length) description = Object.values(entity.descriptions)[0];

            // prepare result
            var result = {
                title: entity.title,
                description: description ? description.value : '',
                descriptionLocaleCode: description ? description.language : '',
                editURL: 'https://wiki.openstreetmap.org/wiki/' + entity.title
            };

            // add image
            if (entity.claims) {
                var imageroot;
                var image = that.claimToValue(entity, 'P4', langCodes[0]);
                if (image) {
                    imageroot = 'https://commons.wikimedia.org/w/index.php';
                } else {
                    image = that.claimToValue(entity, 'P28', langCodes[0]);
                    if (image) {
                        imageroot = 'https://wiki.openstreetmap.org/w/index.php';
                    }
                }
                if (imageroot && image) {
                    result.imageURL = imageroot + '?' + utilQsString({
                        title: 'Special:Redirect/file/' + image,
                        width: 400
                    });
                }
            }

            // Try to get a wiki page from tag data item first, followed by the corresponding key data item.
            // If neither tag nor key data item contain a wiki page in the needed language nor English,
            // get the first found wiki page from either the tag or the key item.
            var rtypeWiki = that.monolingualClaimToValueObj(data.rtype, 'P31');
            var tagWiki = that.monolingualClaimToValueObj(data.tag, 'P31');
            var keyWiki = that.monolingualClaimToValueObj(data.key, 'P31');

            var wikis = [rtypeWiki, tagWiki, keyWiki];
            for (i in wikis) {
                var wiki = wikis[i];
                for (var j in langCodes) {
                    var code = langCodes[j];
                    var referenceId = (langCodes[0].split('-')[0] !== 'en' && code.split('-')[0] === 'en') ? 'inspector.wiki_en_reference' : 'inspector.wiki_reference';
                    var info = getWikiInfo(wiki, code, referenceId);
                    if (info) {
                        result.wiki = info;
                        break;
                    }
                }
                if (result.wiki) break;
            }

            callback(null, result);


            // Helper method to get wiki info if a given language exists
            function getWikiInfo(wiki, langCode, tKey) {
                if (wiki && wiki[langCode]) {
                    return {
                        title: wiki[langCode],
                        text: tKey,
                        url: 'https://wiki.openstreetmap.org/wiki/' + wiki[langCode]
                    };
                }
            }
        });
    },