MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 3,207: Line 3,207:
         var hit = cacheGet(id);
         var hit = cacheGet(id);
         if (hit) return Promise.resolve(hit);
         if (hit) return Promise.resolve(hit);
        // The map page carries the names for every work this wiki uses, so
        // normally nothing is fetched from the library at all. That matters
        // because the library is someone else's server: we cannot add CORS
        // headers to it, and a cross-origin call we cannot configure is a
        // call that will not work. The live API stays as an enhancement for
        // whenever it IS reachable.
        return granthaMap().then(function (m) {
            var w = (m.works || {})[id];
            if (w && (w.name || w.nameIAST)) {
                var v = { id: id, name: w.name || '', nameIAST: w.nameIAST || '',
                          author: w.author || '', authorIAST: w.authorIAST || '',
                          category: w.category || '', prasthana: w.prasthana || '' };
                cacheSet(id, v);
                return v;
            }
            return fetchFromLibrary(id);
        });
    }
    function fetchFromLibrary(id) {
         if (libraryDown) return Promise.resolve(null);
         if (libraryDown) return Promise.resolve(null);
         if (inflight[id]) return inflight[id];
         if (inflight[id]) return inflight[id];
Line 3,355: Line 3,375:
         var want = [], seen = {};
         var want = [], seen = {};
         (ids || []).forEach(function (i) { if (i && !seen[i]) { seen[i] = 1; want.push(i); } });
         (ids || []).forEach(function (i) { if (i && !seen[i]) { seen[i] = 1; want.push(i); } });
         var missing = want.filter(function (i) { return !cacheGet(i); });
         // lookup() answers from the map page without a network call, so
         if (missing.length > 5) {
         // resolving fifty works costs one shared map fetch, not fifty
            return catalogue().then(function (by) {
        // requests. Only ids the map does not name reach the library.
                var out = {};
                want.forEach(function (i) { out[i] = by[i] || cacheGet(i) || null; });
                return out;
            });
        }
         return Promise.all(want.map(lookup)).then(function (vals) {
         return Promise.all(want.map(lookup)).then(function (vals) {
             var out = {};
             var out = {};