MediaWiki:Gadget-queryWhatlinkshere.js

来自维阿百科
霓虹灯鱼讨论 | 贡献2023年10月6日 (五) 00:02的版本 (创建页面,内容为“<pre>: /** * ------------------------------------------------------------------------- * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN…”
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/* <pre> */
/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/queryWhatlinkshere/MediaWiki:Gadget-queryWhatlinkshere.js|user=[[U:星海子]]|co-authors=|longId=942f7e359fc8b12cdc91cd3cb0b38bfc4b537388|shortId=942f7e3|message=chore}}';

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
$(function () { return (function () { return __awaiter(void 0, void 0, void 0, function () {
    var wgRelevantPageName, upperFirstCase, api, nsids, wgNamespaceIds, _a, _b, _c, ns_1, nsid, pageinfo, _d, _e, target, ns, title, queryWhatlinkshere, queryWhatembeddedin, whatlinkshere, whatembeddedin;
    var e_1, _f;
    return __generator(this, function (_g) {
        switch (_g.label) {
            case 0:
                if (mw.config.get("wgCanonicalSpecialPageName") !== "Whatlinkshere") {
                    return [2];
                }
                wgRelevantPageName = mw.config.get("wgRelevantPageName");
                if (typeof wgRelevantPageName !== "string" || wgRelevantPageName.length === 0) {
                    return [2];
                }
                upperFirstCase = function (s) { return /^[a-z]/.test(s) ? s.substring(0, 1).toUpperCase() + s.substring(1) : s; };
                api = new mw.Api();
                nsids = {
                    0: "(主)",
                    1: "讨论",
                    2: "用户",
                    3: "用户讨论",
                    4: "萌娘百科",
                    5: "萌娘百科讨论",
                    6: "文件",
                    7: "文件讨论",
                    8: "MediaWiki",
                    9: "MediaWiki讨论",
                    10: "模板",
                    11: "模板讨论",
                    12: "帮助",
                    13: "帮助讨论",
                    14: "分类",
                    15: "分类讨论",
                    274: "Widget",
                    275: "Widget_talk",
                    710: "Timedtext",
                    711: "Timedtext_talk",
                    828: "模块",
                    829: "模块讨论",
                    2300: "Gadget",
                    2301: "Gadget_talk",
                    2302: "Gadget_definition",
                    2303: "Gadget_definition_talk"
                };
                wgNamespaceIds = {};
                try {
                    for (_a = __values(Object.entries(mw.config.get("wgNamespaceIds"))), _b = _a.next(); !_b.done; _b = _a.next()) {
                        _c = __read(_b.value, 2), ns_1 = _c[0], nsid = _c[1];
                        if (!Array.isArray(wgNamespaceIds[nsid])) {
                            wgNamespaceIds[nsid] = [ns_1];
                        }
                        else {
                            wgNamespaceIds[nsid].push(ns_1);
                        }
                    }
                }
                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                finally {
                    try {
                        if (_b && !_b.done && (_f = _a["return"])) _f.call(_a);
                    }
                    finally { if (e_1) throw e_1.error; }
                }
                _e = (_d = Object).values;
                return [4, api.post({
                        action: "query",
                        format: "json",
                        prop: "info",
                        titles: wgRelevantPageName
                    })];
            case 1:
                pageinfo = _e.apply(_d, [(_g.sent()).query.pages])[0];
                target = pageinfo.title;
                ns = pageinfo.ns;
                title = target.replace(ns === 0 ? "" : RegExp("^(?:".concat(wgNamespaceIds[ns].join("|"), "):"), "i"), "");
                $("#mw-content-text > form").after("<fieldset><legend class=\"mw-parser-output\">\u94FE\u5165\u60C5\u51B5 | <a href=\"/Special:Search/".concat(encodeURIComponent("linksto:\"".concat(target, "\" insource:\"").concat(target, "\"")), "\" target=\"_blank\" class=\"external text\">\u901A\u8FC7\u641C\u7D22\u67E5\u627E\u94FE\u5165</a></legend><span class=\"mw-ui-button mw-ui-progressive\" role=\"button\" id=\"queryWhatlinkshere\">\u70B9\u6B64\u67E5\u8BE2\u94FE\u5165\u60C5\u51B5</span><p id=\"whatlinkshere\" style=\"display: none;\">\u52A0\u8F7D\u4E2D\u2026\u2026</p></fieldset><fieldset><legend class=\"mw-parser-output\">\u5D4C\u5165\u60C5\u51B5 | <a href=\"/Special:Search/").concat(encodeURIComponent("hastemplate:\"".concat(target, "\" insource:\"").concat(title, "\"")), "\" target=\"_blank\" class=\"external text\">\u901A\u8FC7\u641C\u7D22\u67E5\u627E\u5D4C\u5165</a></legend><span class=\"mw-ui-button mw-ui-progressive\" role=\"button\" id=\"queryWhatembeddedin\">\u70B9\u6B64\u67E5\u8BE2\u5D4C\u5165\u60C5\u51B5</span><p id=\"whatembeddedin\" style=\"display: none;\">\u52A0\u8F7D\u4E2D\u2026\u2026</p></fieldset>"));
                queryWhatlinkshere = $("#queryWhatlinkshere");
                queryWhatembeddedin = $("#queryWhatembeddedin");
                whatlinkshere = $("#whatlinkshere");
                whatembeddedin = $("#whatembeddedin");
                queryWhatlinkshere.on("click", function () { return __awaiter(void 0, void 0, void 0, function () {
                    var list, nslist, global, ul;
                    return __generator(this, function (_a) {
                        switch (_a.label) {
                            case 0:
                                queryWhatlinkshere.hide();
                                whatlinkshere.show();
                                return [4, (function () { return __awaiter(void 0, void 0, void 0, function () {
                                        var result, eol, lhcontinue, _result;
                                        return __generator(this, function (_a) {
                                            switch (_a.label) {
                                                case 0:
                                                    result = [];
                                                    eol = Symbol();
                                                    lhcontinue = undefined;
                                                    _a.label = 1;
                                                case 1:
                                                    if (!(lhcontinue !== eol)) return [3, 3];
                                                    return [4, api.post({
                                                            action: "query",
                                                            format: "json",
                                                            prop: "linkshere",
                                                            titles: target,
                                                            lhprop: "redirect|title|pageid",
                                                            lhcontinue: lhcontinue,
                                                            lhlimit: "max"
                                                        })];
                                                case 2:
                                                    _result = _a.sent();
                                                    if (_result["continue"]) {
                                                        lhcontinue = _result["continue"].lhcontinue;
                                                        whatlinkshere.append("…");
                                                    }
                                                    else {
                                                        lhcontinue = eol;
                                                    }
                                                    result.push.apply(result, __spreadArray([], __read(Object.values(_result.query.pages)[0].linkshere || []), false));
                                                    return [3, 1];
                                                case 3: return [2, result];
                                            }
                                        });
                                    }); })()];
                            case 1:
                                list = _a.sent();
                                nslist = {
                                    0: { count: 0, redirect: 0 },
                                    1: { count: 0, redirect: 0 },
                                    2: { count: 0, redirect: 0 },
                                    3: { count: 0, redirect: 0 },
                                    4: { count: 0, redirect: 0 },
                                    5: { count: 0, redirect: 0 },
                                    6: { count: 0, redirect: 0 },
                                    7: { count: 0, redirect: 0 },
                                    8: { count: 0, redirect: 0 },
                                    9: { count: 0, redirect: 0 },
                                    10: { count: 0, redirect: 0 },
                                    11: { count: 0, redirect: 0 },
                                    12: { count: 0, redirect: 0 },
                                    13: { count: 0, redirect: 0 },
                                    14: { count: 0, redirect: 0 },
                                    15: { count: 0, redirect: 0 },
                                    274: { count: 0, redirect: 0 },
                                    275: { count: 0, redirect: 0 },
                                    710: { count: 0, redirect: 0 },
                                    711: { count: 0, redirect: 0 },
                                    828: { count: 0, redirect: 0 },
                                    829: { count: 0, redirect: 0 },
                                    2300: { count: 0, redirect: 0 },
                                    2301: { count: 0, redirect: 0 },
                                    2302: { count: 0, redirect: 0 },
                                    2303: { count: 0, redirect: 0 }
                                };
                                global = { redirect: 0 };
                                whatlinkshere.text("\u5171\u6709".concat(list.length, "\u4E2A\u9875\u9762\u542B\u6709\u5230\u672C\u9875\u9762\u7684\u7AD9\u5185\u94FE\u63A5\uFF08\u4E0D\u8003\u8651\u5D4C\u5165\uFF09\uFF0C\u5176\u4E2D\u6709").concat(list.filter(function (item) { return Reflect.has(item, "redirect"); }).length, "\u4E2A\u91CD\u5B9A\u5411\u9875\u9762\u3002"));
                                if (list.length > 0) {
                                    whatlinkshere.append("按名字空间划分如下:");
                                }
                                ul = $("<ul/>");
                                list.forEach(function (item) {
                                    nslist[item.ns].count++;
                                    if (Reflect.has(item, "redirect")) {
                                        nslist[item.ns].redirect++;
                                        global.redirect++;
                                    }
                                });
                                Object.entries(nslist).filter(function (_a) {
                                    var _b = __read(_a, 2), count = _b[1].count;
                                    return count > 0;
                                }).sort(function (_a, _b) {
                                    var _c = __read(_a, 1), a = _c[0];
                                    var _d = __read(_b, 1), b = _d[0];
                                    return a - b;
                                }).forEach(function (_a) {
                                    var _b = __read(_a, 2), nsnumber = _b[0], _c = _b[1], count = _c.count, redirect = _c.redirect;
                                    return ul.append("<li>".concat(upperFirstCase(nsids[nsnumber]), "\uFF1A").concat(count, "\u4E2A\u9875\u9762\uFF08\u5176\u4E2D\u6709").concat(redirect, "\u4E2A\u91CD\u5B9A\u5411\u9875\u9762\uFF09"));
                                });
                                whatlinkshere.after(ul);
                                return [2];
                        }
                    });
                }); });
                queryWhatembeddedin.on("click", function () { return __awaiter(void 0, void 0, void 0, function () {
                    var list, nslist, global, ul;
                    return __generator(this, function (_a) {
                        switch (_a.label) {
                            case 0:
                                queryWhatembeddedin.hide();
                                whatembeddedin.show();
                                return [4, (function () { return __awaiter(void 0, void 0, void 0, function () {
                                        var result, eol, eicontinue, _result, _result;
                                        var _a, _b, _c;
                                        return __generator(this, function (_d) {
                                            switch (_d.label) {
                                                case 0:
                                                    result = {
                                                        redirects: [],
                                                        all: []
                                                    };
                                                    eol = Symbol();
                                                    eicontinue = undefined;
                                                    _d.label = 1;
                                                case 1:
                                                    if (!(eicontinue !== eol)) return [3, 3];
                                                    return [4, api.post({
                                                            action: "query",
                                                            format: "json",
                                                            list: "embeddedin",
                                                            eititle: target,
                                                            einamespace: "*",
                                                            eifilterredir: "redirects",
                                                            eicontinue: eicontinue,
                                                            eilimit: "max"
                                                        })];
                                                case 2:
                                                    _result = _d.sent();
                                                    if (_result["continue"]) {
                                                        eicontinue = _result["continue"].eicontinue;
                                                        whatembeddedin.append("…");
                                                    }
                                                    else {
                                                        eicontinue = eol;
                                                    }
                                                    (_a = result.redirects).push.apply(_a, __spreadArray([], __read(_result.query.embeddedin), false));
                                                    (_b = result.all).push.apply(_b, __spreadArray([], __read(_result.query.embeddedin), false));
                                                    return [3, 1];
                                                case 3:
                                                    eicontinue = undefined;
                                                    _d.label = 4;
                                                case 4:
                                                    if (!(eicontinue !== eol)) return [3, 6];
                                                    return [4, api.post({
                                                            action: "query",
                                                            format: "json",
                                                            list: "embeddedin",
                                                            eititle: target,
                                                            einamespace: "*",
                                                            eifilterredir: "nonredirects",
                                                            eicontinue: eicontinue,
                                                            eilimit: "max"
                                                        })];
                                                case 5:
                                                    _result = _d.sent();
                                                    if (_result["continue"]) {
                                                        eicontinue = _result["continue"].eicontinue;
                                                        whatembeddedin.append("…");
                                                    }
                                                    else {
                                                        eicontinue = eol;
                                                    }
                                                    (_c = result.all).push.apply(_c, __spreadArray([], __read(_result.query.embeddedin), false));
                                                    return [3, 4];
                                                case 6: return [2, result];
                                            }
                                        });
                                    }); })()];
                            case 1:
                                list = _a.sent();
                                nslist = {
                                    0: { count: 0, redirect: [] },
                                    1: { count: 0, redirect: [] },
                                    2: { count: 0, redirect: [] },
                                    3: { count: 0, redirect: [] },
                                    4: { count: 0, redirect: [] },
                                    5: { count: 0, redirect: [] },
                                    6: { count: 0, redirect: [] },
                                    7: { count: 0, redirect: [] },
                                    8: { count: 0, redirect: [] },
                                    9: { count: 0, redirect: [] },
                                    10: { count: 0, redirect: [] },
                                    11: { count: 0, redirect: [] },
                                    12: { count: 0, redirect: [] },
                                    13: { count: 0, redirect: [] },
                                    14: { count: 0, redirect: [] },
                                    15: { count: 0, redirect: [] },
                                    274: { count: 0, redirect: [] },
                                    275: { count: 0, redirect: [] },
                                    710: { count: 0, redirect: [] },
                                    711: { count: 0, redirect: [] },
                                    828: { count: 0, redirect: [] },
                                    829: { count: 0, redirect: [] },
                                    2300: { count: 0, redirect: [] },
                                    2301: { count: 0, redirect: [] },
                                    2302: { count: 0, redirect: [] },
                                    2303: { count: 0, redirect: [] }
                                };
                                global = { redirect: 0 };
                                whatembeddedin.text("\u5171\u6709".concat(list.all.length, "\u4E2A\u9875\u9762\u5D4C\u5165\u4E86\u672C\u9875\u9762\uFF0C\u5176\u4E2D\u6709").concat(list.redirects.length, "\u4E2A\u91CD\u5B9A\u5411\u9875\u9762\u3002"));
                                if (list.all.length > 0) {
                                    whatembeddedin.append("按名字空间划分如下:");
                                }
                                ul = $("<ul/>");
                                list.all.forEach(function (_a) {
                                    var ns = _a.ns;
                                    nslist[ns].count++;
                                });
                                list.redirects.forEach(function (_a) {
                                    var ns = _a.ns, title = _a.title;
                                    nslist[ns].redirect.push(title);
                                    global.redirect++;
                                });
                                Object.entries(nslist).filter(function (_a) {
                                    var _b = __read(_a, 2), count = _b[1].count;
                                    return count > 0;
                                }).sort(function (_a, _b) {
                                    var _c = __read(_a, 1), a = _c[0];
                                    var _d = __read(_b, 1), b = _d[0];
                                    return a - b;
                                }).forEach(function (_a) {
                                    var _b = __read(_a, 2), nsnumber = _b[0], _c = _b[1], count = _c.count, redirect = _c.redirect;
                                    return ul.append("<li class=\"mw-parser-output\">".concat(upperFirstCase(nsids[nsnumber]), "\uFF1A").concat(count, "\u4E2A\u9875\u9762").concat(redirect.length > 0 ? "\uFF08\u5176\u4E2D\u6709".concat(redirect.length, "\u4E2A\u91CD\u5B9A\u5411\u9875\u9762\uFF1A").concat(redirect.map(function (title) { return "<a target=\"_blank\" rel=\"nofollow noreferrer noopener\" class=\"external text\" href=\"/index.php?title=".concat(encodeURIComponent(title), "&amp;redirect=no\">").concat(title, "</a>"); }).join("、"), "\uFF09") : ""));
                                });
                                whatembeddedin.after(ul);
                                return [2];
                        }
                    });
                }); });
                return [2];
        }
    });
}); })(); });

/* </pre> */