跳转到内容

User:Hyijun/MoveTool.js:修订间差异

维基百科,自由的百科全书
删除的内容 添加的内容
// Edit via Wikiplus
// Edit via Wikiplus 修正bug
第34行: 第34行:
console.log(old_title);
console.log(old_title);
new_title = prompt("请输入新标题:");
new_title = prompt("请输入新标题:");
reason = "通过内链替换工具自动修复条目内链:" + prompt("请输入移动理由:")
reason = "通过内链替换工具自动移动条目:" + prompt("请输入移动理由:")
let ls;
let ls;
ls = await get_pages_linkin(old_title)
ls = await get_pages_linkin(old_title)
第114行: 第114行:
replace_pages_text(links[each0][0], reason)
replace_pages_text(links[each0][0], reason)
}
}
alert(message)
alert("成功")
} else {
} else {
alert("用户取消操作")
alert("用户取消操作")
第161行: 第161行:
api.edit(title, function (revision) {
api.edit(title, function (revision) {
return {
return {
text: replace_pages_link(revision.content, old_title, title, true),
text: replace_pages_link(revision.content, old_title, new_title, true),
summary: summary
summary: summary
}
}

2021年8月26日 (四) 16:08的版本

//为什么js的异步和其他语言的不一样?我不习惯,故关闭之
// $.ajaxSetup({async: false});
"use strict";
// Create portlet link
var portletLinkOnline = mw.util.addPortletLink(
    'p-tb',
    'javascript:prepare()',
    '高级移动',
    't-movetool',
    '移动页面时修正内链',
    '',
    '#pt-userpage'
);

// $(portletLinkOnline).on('click', function (e) {
//     console.log('rinnnnnnnnnn');
//     e.preventDefault();
// });

var old_title;
var new_title;
var reason;
var links = [];
var do_keep_now = true

async function prepare() {
    var ot  = prompt("请输入旧标题,若此页面即为旧标题所在页则可以留空:");
    if (ot !== ""){
        old_title = ot;
    }else{
        old_title = mw.config.get('wgTitle');
    }

    console.log(old_title);
    new_title = prompt("请输入新标题:");
    reason = "通过内链替换工具自动移动条目:" + prompt("请输入移动理由:")
    let ls;
    ls = await get_pages_linkin(old_title)
    console.log(ls)
    let e;
    for (e in ls) {
        console.log(ls)
        if (ls[e].ns === 0) {
            var l = ls[e].title;
        }else{
            continue;
        }
        let wt;
        wt = await get_page_wikitext(l)
        var pr = "暂未能加载预览,请检查源代码,确认后可发布"
        // var pr = await get_preview(replace_pages_link(wt, title,new_title,do_keep_now));
        links.push([l, replace_pages_link(wt, old_title, new_title, do_keep_now), pr])
    }
    console.log(links)
    start()
}


function start() {
    var s1 = document.getElementById("content").innerHTML;
    s1 = document.getElementById("content").innerHTML = "<h1>移动工具 v1.0</h1>\n" +
        "    <h2>即将对该条目进行移动<!--,请填写并确认以下信息--></h2>\n" +
        "    <fieldset>\n" +
        // "        <legend>操作</legend>\n" + "        <label for=\"new_title\">新标题:</label>\n" +
        // "        <input id=\"new_title\" class=\"mw-searchInput\" name=\"new_title\" size=\"40\">"
        // +
        // "        <label for=\"move_reason\">移动理由:</label>\n" +
        // "        <input id=\"move_reason\" class=\"mw-searchInput\" name=\"move_reason\" size=\"40\">\n" +
        // "\n" +
        // "        <label for=\"edit_reason\">修改内链摘要:</label>\n" +
        // "        <input id=\"edit_reason\" class=\"mw-searchInput\" name=\"edit_reason\" size=\"40\" value=\"通过工具自动修复内链\">\n" +
        // "\n" +
        // "        <input name=\"loadpreview\" type=\"submit\" value=\"加载预览\">\n" +
        "        <input name=\"postedit\" onclick=end() type=\"submit\" value=\"一键发布编辑\">\n" +
        "    </fieldset>\n" +
        "    <hr/>\n" +
        "    <p>以下是链入该页面的页面,移动后将自动修复内链</p>" +
        "   <ul>"

    var eac;
    for (eac in links) {
        s1 = document.getElementById("content").innerHTML = s1 + "<li><a href=\"" + get_link(links[eac][0]) + "\">" + links[eac][0] + "</a></li>\n" +
            "        <details>\n" +
            "        <summary>展开预览</summary>\n" +
            "            <fieldset>\n" +
            "                <legend>预览</legend>\n" +
            "<div id=\"preview" + eac.toString() + "\">" +
            links[eac][2] +
            "<!--        预览内容-->\n" +
            " </div>>" +
            "            </fieldset>\n" +
            "            <fieldset style=\"height:300px\">\n" +
            "                <legend>wikitext</legend>\n" +
            "                <textarea style=\"height:100%; width: 100%\">" +
            links[eac][1] +
            "</textarea>\n" +
            "            </fieldset>\n" +
            // "            <button id=\"target\" onclick=\"reload_preview(" + eac.toString() + ", links[" + eac.toString() + "][0])\">发布更改</button>\n" +
            "\n" +
            "        </details>";
    }
}

async function end() {
    let message;
    if (confirm("确定吗?")) {
        if (is_redirect()){
            //do nothing
        }else {
            var token = await get_token();
            message = await move_pages(old_title, new_title, reason, false, token);
        }
        for (var each0 in links){
            replace_pages_text(links[each0][0], reason)
        }
        alert("成功")
    } else {
        alert("用户取消操作")
    }
}

function get_link(title) {
    return mw.util.getUrl(title)
}

function reload_preview(number, title) {
    console.log(number)
    let str;
    str = get_preview(title)
    var s2 = document.getElementById("preview" + number.toString).innerHTML;
    s2 = document.getElementById("preview" + number.toString).innerHTML = str;
}


function get_pages_linkin(title) {
    return new Promise((resolve, reject) => {
        var api = new mw.Api();
        let items = [];
        api.get({
            format: 'json',
            action: 'query',
            prop: 'linkshere',
            titles: old_title,
            formatversion: '2',

        }).done(data => {
            console.log(data)
            var p;
            for (p in data) {
                items.push(data[p]);
            }
            console.log(items);
            resolve(items[1].pages[0].linkshere)
        }, err => reject(err))
    })
}

function replace_pages_text(title, summary) {
    return new Promise((resolve, reject) => {
        var api = new mw.Api();
        api.edit(title, function (revision) {
            return {
                text: replace_pages_link(revision.content, old_title, new_title, true),
                summary: summary
            }
        }).then(() => {
            resolve("succeed")
        }, err => reject(err))
    })
}


function get_token() {
    return new Promise((resolve, reject) => {
        var params = {
            action: 'query',
            meta: 'tokens',
            type: 'csrf',
            formatversion: '2',
            format: 'json'
        };
        var api = new mw.Api();
        api.get(params).then(function (data) {
            var token = data.query.tokens.csrftoken;
            resolve(token)
        }, err => reject(err))
    })

}

function is_redirect(){
    return new Promise((resolve, reject) => {
        var params = {
            action: 'query',
            prop: 'info',
            titles: old_title,
            formatversion: '2',
            format: 'json'
        };
        var api = new mw.Api();
        api.get(params).then(function (data) {
            var result = data.query.pages[0].redirect;
            if (result === undefined){
                resolve(false);
            }else {
                resolve(result);
            }
        }, err => reject(err))
    })
}

function replace_pages_link(item, old_link, new_link, do_keep_now) {
    var reg1 = new RegExp('\\[\\[(' + old_link + ')\\]\\]')
    // var reg2 = new RegExp('\\[\\[([^\\[\\[]*?)\\|(' + old_link + ')\\]\\]', "g");
    var reg2 = new RegExp('\\[\\[(' + old_link + ')\\|([^\\[\\[]*?)\\]\\]');
    while (reg1.test(item)) {
        item = item.replace(reg1, '[[' + new_link + ']]');
    }
    if (do_keep_now) {
        while (reg2.test(item)) {
            item = item.replace(reg2, '[[' + new_link + '|' + reg2.exec(item)[1] + ']]');
        }
    } else {
        while (reg2.test(item)) {
            item = item.replace(reg2, '[[' + new_link + ']]');
        }
    }
    return item
}

function get_preview(text) {
    return new Promise((resolve, reject) => {
        console.log(text)
        const params = {
            action: 'parse',
            prop: "text",
            page: text,
            contentmodel: "wikitext",
            format: 'json',
            async: false,
            formatversion: '2',
        };
        var api = new mw.Api();
        api.get(params).then(data => {
            console.log(data)
            resolve(data.parse.text);
        }, err => reject(err));
    })
}


function move_pages(old_title, new_title, reason, do_not_direct, token) {
    return new Promise((resolve, reject) => {
        const params = {
            action: 'move',
            form: old_title,
            to: new_title,
            reason: reason,
            movetalk: true,
            movesubpages: true,
            noredirect: do_not_direct,
            token: token,
            format: 'json',
            formatversion: '2',
        };
        var api = new mw.Api();
        api.get(params).then(data => {
            resolve(data);
        }, err => reject(err));
    })
}


function get_page_wikitext(title) {
    return new Promise((resolve, reject) => {
        const params = {
            action: 'query',
            prop: 'revisions',
            titles: title,
            async: false,
            rvprop: 'content',
            rvslots: 'main',
            formatversion: '2',
            format: 'json'
        };
        var api = new mw.Api();
        api.get(params).then(data => {
            const pages = data.query.pages;
            resolve(pages[0].revisions[0].slots.main.content);
        }, err => reject(err))
    })
}