server/helpers/wikimedia.js

"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.symbol.iterator.js");
require("core-js/modules/es.array.from.js");
require("core-js/modules/es.array.slice.js");
require("core-js/modules/es.function.name.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getAvailableWikipediaArticles = getAvailableWikipediaArticles;
exports.getWikipediaExtract = getWikipediaExtract;
exports.selectWikipediaPage = selectWikipediaPage;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.find.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.object.values.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.regexp.to-string.js");
require("core-js/modules/es.string.ends-with.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.string.replace.js");
require("core-js/modules/es.string.search.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/web.url.js");
require("core-js/modules/web.url.to-json.js");
require("core-js/modules/web.url-search-params.js");
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _uniq2 = _interopRequireDefault(require("lodash/uniq"));
var _toLower2 = _interopRequireDefault(require("lodash/toLower"));
var _cache = require("../../common/helpers/cache");
var _dateFns = require("date-fns");
var _superagent = _interopRequireDefault(require("superagent"));
var _info = require("../info");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /*
 * Copyright (C) 2023  David Kellner
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
// incomplete, only the parts we need

/** Maximum age of cached results in seconds. */
var cacheMaxAge = {
  articles: (0, _dateFns.hoursToSeconds)(24 * 7),
  extract: (0, _dateFns.hoursToSeconds)(24 * 3)
};

/**
 * Fetches a list of Wikipedia articles in all available languages for the given Wikidata item.
 * @param {string} wikidataId - Wikidata item ID.
 */
function getAvailableWikipediaArticles(_x) {
  return _getAvailableWikipediaArticles.apply(this, arguments);
}
/**
 * Tries to find a Wikipedia article for the given Wikidata item in the first preferred language which is available.
 * @param {string} wikidataId - Wikidata item ID.
 * @param {string[]} preferredLanguages - List of language codes, preference in descending order.
 */
function _getAvailableWikipediaArticles() {
  _getAvailableWikipediaArticles = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee(wikidataId) {
    var _result$entities;
    var _ref,
      _ref$forceCache,
      forceCache,
      cacheKey,
      cachedArticles,
      apiUrl,
      response,
      result,
      item,
      articles,
      _args = arguments;
    return _regenerator.default.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _ref = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}, _ref$forceCache = _ref.forceCache, forceCache = _ref$forceCache === void 0 ? false : _ref$forceCache;
          cacheKey = "wiki:articles:".concat(wikidataId);
          _context.next = 1;
          return (0, _cache.getCachedJSON)(cacheKey);
        case 1:
          cachedArticles = _context.sent;
          if (!(cachedArticles || forceCache)) {
            _context.next = 2;
            break;
          }
          return _context.abrupt("return", cachedArticles || []);
        case 2:
          apiUrl = new URL('https://www.wikidata.org/w/api.php');
          apiUrl.search = new URLSearchParams({
            action: 'wbgetentities',
            format: 'json',
            ids: wikidataId,
            props: 'sitelinks'
          }).toString();
          _context.next = 3;
          return _superagent.default.get(apiUrl.href).set('User-Agent', _info.userAgent);
        case 3:
          response = _context.sent;
          result = response.body;
          item = (_result$entities = result.entities) === null || _result$entities === void 0 ? void 0 : _result$entities[wikidataId];
          if (item) {
            _context.next = 4;
            break;
          }
          throw new Error("Failed to fetch Wikidata item ".concat(wikidataId));
        case 4:
          articles = Object.values(item.sitelinks)
          // only keep Wikipedia pages
          .filter(function (link) {
            return link.site.endsWith('wiki');
          }).map(function (page) {
            return {
              // drop project suffix
              language: page.site.replace(/wiki$/, ''),
              title: page.title
            };
          });
          (0, _cache.cacheJSON)(cacheKey, articles, {
            expireTime: cacheMaxAge.articles
          });
          return _context.abrupt("return", articles);
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _getAvailableWikipediaArticles.apply(this, arguments);
}
function selectWikipediaPage(_x2) {
  return _selectWikipediaPage.apply(this, arguments);
}
/**
 * Fetches the page extract of the given Wikipedia article.
 * @param {object} article - Title and language of the article.
 */
function _selectWikipediaPage() {
  _selectWikipediaPage = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2(wikidataId) {
    var _ref2,
      _ref2$forceCache,
      forceCache,
      _ref2$preferredLangua,
      preferredLanguages,
      articles,
      result,
      _iterator,
      _step,
      _loop,
      _args3 = arguments,
      _t;
    return _regenerator.default.wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          _ref2 = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {}, _ref2$forceCache = _ref2.forceCache, forceCache = _ref2$forceCache === void 0 ? false : _ref2$forceCache, _ref2$preferredLangua = _ref2.preferredLanguages, preferredLanguages = _ref2$preferredLangua === void 0 ? ['en'] : _ref2$preferredLangua;
          _context3.next = 1;
          return getAvailableWikipediaArticles(wikidataId, {
            forceCache: forceCache
          });
        case 1:
          articles = _context3.sent;
          _iterator = _createForOfIteratorHelper((0, _uniq2.default)(preferredLanguages));
          _context3.prev = 2;
          _loop = /*#__PURE__*/_regenerator.default.mark(function _loop() {
            var language;
            return _regenerator.default.wrap(function (_context2) {
              while (1) switch (_context2.prev = _context2.next) {
                case 0:
                  language = _step.value;
                  result = articles.find(function (page) {
                    return page.language === (0, _toLower2.default)(language);
                  });
                  if (!result) {
                    _context2.next = 1;
                    break;
                  }
                  return _context2.abrupt("return", 1);
                case 1:
                case "end":
                  return _context2.stop();
              }
            }, _loop);
          });
          _iterator.s();
        case 3:
          if ((_step = _iterator.n()).done) {
            _context3.next = 6;
            break;
          }
          return _context3.delegateYield(_loop(), "t0", 4);
        case 4:
          if (!_context3.t0) {
            _context3.next = 5;
            break;
          }
          return _context3.abrupt("continue", 6);
        case 5:
          _context3.next = 3;
          break;
        case 6:
          _context3.next = 8;
          break;
        case 7:
          _context3.prev = 7;
          _t = _context3["catch"](2);
          _iterator.e(_t);
        case 8:
          _context3.prev = 8;
          _iterator.f();
          return _context3.finish(8);
        case 9:
          return _context3.abrupt("return", result);
        case 10:
        case "end":
          return _context3.stop();
      }
    }, _callee2, null, [[2, 7, 8, 9]]);
  }));
  return _selectWikipediaPage.apply(this, arguments);
}
function getWikipediaExtract(_x3) {
  return _getWikipediaExtract.apply(this, arguments);
}
function _getWikipediaExtract() {
  _getWikipediaExtract = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee3(article) {
    var _result$query;
    var _ref3,
      _ref3$forceCache,
      forceCache,
      cacheKey,
      cachedExtract,
      apiUrl,
      response,
      result,
      pageExtract,
      _args4 = arguments;
    return _regenerator.default.wrap(function (_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          _ref3 = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {}, _ref3$forceCache = _ref3.forceCache, forceCache = _ref3$forceCache === void 0 ? false : _ref3$forceCache;
          cacheKey = "wiki:extract:".concat(article.language, ":").concat(article.title);
          _context4.next = 1;
          return (0, _cache.getCachedJSON)(cacheKey);
        case 1:
          cachedExtract = _context4.sent;
          if (!(cachedExtract || forceCache)) {
            _context4.next = 2;
            break;
          }
          return _context4.abrupt("return", cachedExtract);
        case 2:
          apiUrl = new URL("https://".concat(article.language, ".wikipedia.org/w/api.php"));
          apiUrl.search = new URLSearchParams({
            action: 'query',
            format: 'json',
            formatversion: '2',
            prop: 'extracts',
            // eslint-disable-next-line sort-keys -- `exintro` only allowed with `prop: 'extracts'`
            exintro: '1',
            redirects: '1',
            titles: article.title
          }).toString();
          _context4.next = 3;
          return _superagent.default.get(apiUrl.href).set('User-Agent', _info.userAgent);
        case 3:
          response = _context4.sent;
          result = response.body;
          pageExtract = (_result$query = result.query) === null || _result$query === void 0 || (_result$query = _result$query.pages) === null || _result$query === void 0 ? void 0 : _result$query[0];
          (0, _cache.cacheJSON)(cacheKey, pageExtract, {
            expireTime: cacheMaxAge.extract
          });
          return _context4.abrupt("return", pageExtract);
        case 4:
        case "end":
          return _context4.stop();
      }
    }, _callee3);
  }));
  return _getWikipediaExtract.apply(this, arguments);
}
//# sourceMappingURL=wikimedia.js.map