Template:MDN:Common

  • Revision slug: Template:mdn:common
  • Revision title: Template:MDN:Common
  • Revision id: 353395
  • Created:
  • Creator: lmorchard
  • Is current revision? No
  • Comment

Revision Content

<% module.exports = { /** * Given a set of strings like this: * { "en-US": "Foo", "de": "Bar", "es": "Baz" } * Return the one which matches the current locale. */ localString: function (strings) { var lang = env.locale; if (!(lang in strings)) lang = 'en-US'; return strings[lang]; }, /** * Accepts a relative URL or an attachment object * Returns the content of a given file. */ getFileContent: function(fileObjOrUrl) { var url = fileObjOrUrl.url || fileObjOrUrl; if(!url) return ''; var result = '', base_url = ''; // New file urls include attachment host, so we don't need to prepend it var fUrl = kuma.url.parse(url); if (!fUrl.host) { var p = kuma.url.parse(env.url, true), base_url = p.protocol + '//' + p.host; } url = base_url + url; return cacheFn('kuma:get_attachment_content:' + md5(url.toLowerCase()), 3600, function(next) { try { request({ method: 'GET', headers: { 'Cache-Control': env.cache_control }, url: url }, function(err, resp, body) { if(resp && 200 == resp.statusCode) { next(body); } else if(err) { next(null); } }); } catch(e) { next("error: " + e); } }); }, /** * #### cacheFnIgnoreCacheControl * Cache a function, and use cached results no matter what * Cache-Control we get from a shift-refresh. */ cacheFnIgnoreCacheControl: function (key, tm_out, to_cache) { var result = null, err_result = null, f = new Future(), mc = memcached; mc.get(key, function (err, c_result) { if (c_result) { result = c_result; f['return'](); } else { try { to_cache(function (val) { mc.set(key, val, tm_out, function (err, c_result) { result = val; f['return'](); }); }); } catch (e) { err_result = e; result = ''; f['return'](); } } }); f.wait(); if (err_result) { throw err_result; } return result; }, // #### fetchJSONResource // Fetch an HTTP resource with JSON representation, parse the JSON and // return a JS object. fetchJSONResource: function (url, cache_tmout) { if (!cache_tmout) cache_tmout = 3600; var key = 'kuma:json_resource:' + md5(url.toLowerCase()); return cacheFn(key, 600, function (next) { var opts = { method: 'GET', headers: { 'Cache-Control': env.cache_control }, url: url }; try { request(opts, function (err, resp, body) { var result = null; if (resp && 200 == resp.statusCode) { result = JSON.parse(body); } next(result); }); } catch (e) { next(null); } }); }, // #### fetchHTTPResource // Fetch an HTTP resource, return the response body. fetchHTTPResource: function (url, cache_tmout) { if (!cache_tmout) cache_tmout = 3600; var key = 'kuma:http_resource:' + md5(url.toLowerCase()); env.poo = "TEST"; return cacheFn(key, 600, function (next) { var opts = { method: 'GET', headers: { 'Cache-Control': env.cache_control }, url: url }; try { request(opts, function (err, resp, body) { var result = null; env.poo2 = "TEST"; if (resp && 200 == resp.statusCode) { result = body; } next(result); }); } catch (e) { next(null); } }); }, /* Returns the appropriate string (usually a comma+space) for usage in a enumeration list for the current locale */ listSeparator: function() { var listSeparators = { "en-US": ", ", "ar": "،", "fa": "،", "ja": "、", "ko": "·", "mn": "᠂", "ur": "،", "zh−TW": "、" }; return mdn.localString(listSeparators); } } %>

Revision Source

<% module.exports = {
    
    /**
     * Given a set of strings like this:
     *     { "en-US": "Foo", "de": "Bar", "es": "Baz" }
     * Return the one which matches the current locale.
     */
    localString: function (strings) {
        var lang = env.locale;
        if (!(lang in strings)) lang = 'en-US';
        return strings[lang];
    },

    /**
     * Accepts a relative URL or an attachment object
     * Returns the content of a given file.
     */
    getFileContent: function(fileObjOrUrl) {
        var url = fileObjOrUrl.url || fileObjOrUrl;
        if(!url) return '';
        
        var result = '',
            base_url = '';
        
        // New file urls include attachment host, so we don't need to prepend it
        var fUrl = kuma.url.parse(url);
        if (!fUrl.host) {
            var p = kuma.url.parse(env.url, true),
                base_url = p.protocol + '//' + p.host;
        }
        url = base_url + url;
        return cacheFn('kuma:get_attachment_content:' + md5(url.toLowerCase()), 3600, function(next) {
            try {
                request({
                    method: 'GET',
                    headers: { 'Cache-Control': env.cache_control },
                    url: url
                }, function(err, resp, body) {
                    if(resp && 200 == resp.statusCode) {
                        next(body);
                    }
                    else if(err) {
                        next(null);
                    }
                });
            } catch(e) {
                next("error: " + e);
            }
        });
    },

    /**
     * #### cacheFnIgnoreCacheControl
     * Cache a function, and use cached results no matter what 
     * Cache-Control we get from a shift-refresh.
     */
    cacheFnIgnoreCacheControl: function (key, tm_out, to_cache) {
        var result = null,
            err_result = null,
            f = new Future(),
            mc = memcached;
        mc.get(key, function (err, c_result) {
            if (c_result) {
                result = c_result;
                f['return']();
            } else {
                try {
                    to_cache(function (val) {
                        mc.set(key, val, tm_out, function (err, c_result) {
                            result = val;
                            f['return']();
                        });
                    });
                } catch (e) {
                    err_result = e;
                    result = '';
                    f['return']();
                }
            }
        });
        f.wait();
        if (err_result) { throw err_result; }
        return result;
    },
    
    // #### fetchJSONResource
    // Fetch an HTTP resource with JSON representation, parse the JSON and 
    // return a JS object.
    fetchJSONResource: function (url, cache_tmout) {
        if (!cache_tmout) cache_tmout = 3600;
        var key = 'kuma:json_resource:' + md5(url.toLowerCase());
        return cacheFn(key, 600, function (next) {
            var opts = {
                method: 'GET',
                headers: { 'Cache-Control': env.cache_control },
                url: url
            };
            try {
                request(opts, function (err, resp, body) {
                    var result = null;
                    if (resp && 200 == resp.statusCode) {
                        result = JSON.parse(body);
                    }
                    next(result);
                });
            } catch (e) {
                next(null);
            }            
        });
    },
    
    // #### fetchHTTPResource
    // Fetch an HTTP resource, return the response body.
    fetchHTTPResource: function (url, cache_tmout) {
        if (!cache_tmout) cache_tmout = 3600;
        var key = 'kuma:http_resource:' + md5(url.toLowerCase());
        env.poo = "TEST";
        return cacheFn(key, 600, function (next) {
            var opts = {
                method: 'GET',
                headers: { 'Cache-Control': env.cache_control },
                url: url
            };
            try {
                request(opts, function (err, resp, body) {
                    var result = null;
                            env.poo2 = "TEST";

                    if (resp && 200 == resp.statusCode) {
                        result = body;
                    }
                    next(result);
                });
            } catch (e) {
                next(null);
            }            
        });
    },
    
    /* Returns the appropriate string (usually a comma+space) for usage in a enumeration list for the current locale */
    listSeparator: function() {
        var listSeparators = { "en-US": ", ", "ar": "،", "fa": "،", "ja": "、", "ko": "·", "mn": "᠂", "ur": "،", "zh−TW": "、" };
        return mdn.localString(listSeparators);
    }
} %>
Revert to this revision