사용자:Respice post te/문서 대량 삭제 신청기.js

위키백과, 우리 모두의 백과사전.

참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다. 구글 크롬, 파이어폭스, 마이크로소프트 엣지, 사파리: ⇧ Shift 키를 누른 채 "새로 고침" 버튼을 클릭하십시오. 더 자세한 정보를 보려면 위키백과:캐시 무시하기 항목을 참고하십시오.

// <nowiki>
$.when(mw.loader.using('mediawiki.util'), $.ready).then(function () {

    // General usage pattern:
    // mw.util.addPortletLink(portletId, href, text /* Optional: , id, tooltip, accesskey, nextnode */);

    // Example: Add a link to your tool to the Tools area, above the "Special pages" link.
    mw.util.addPortletLink(
        'p-tb',  // Replace 'p-tb' with the appropriate portlet ID
        '#',  // Replace '#' with your tool's URL
        '문서 대량 삭제 신청기',  // Replace with the link text
        't-mytool',  // Replace with a unique ID for your link
        'Go to your tool',  // Replace with the tooltip text
        'm',
        '#t-specialpages'
    );

    // 클릭 이벤트 핸들러를 추가합니다
    $('#t-mytool').on('click', function(event) {
        event.preventDefault(); // 링크의 기본 동작을 막습니다
        run_script(); // run_script 함수를 호출하여 작업을 시작합니다
    });

});
// alert에서 사용자 이름과 삭제 신청 사유를 입력받으면 사용자가 만든 문서를 모두 삭제 신청하는 스크립트를 실행하는 함수를 정의합니다
async function run_script() {
    // alert에서 사용자 이름을 입력받습니다
    var username = prompt("사용자 이름을 입력하세요");

    // 사용자 이름이 비어있는지 확인합니다
    if (username === "" || username === null) {
        // 비어있는 경우 메시지를 출력합니다
        alert("오류: 사용자 이름이 입력되지 않았습니다");
        return;
    }

    // alert에서 삭제 신청 사유를 입력받습니다
    var reason = prompt("삭제 신청 사유를 입력하세요");

    // 삭제 신청 사유가 비어있는지 확인합니다
    if (reason === "" || reason === null) {
        // 비어있는 경우 메시지를 출력합니다
        alert("오류: 삭제 신청 사유가 입력되지 않았습니다");
        return;
    }

    // 위키백과 API를 이용하여 사용자가 만든 문서 목록을 가져옵니다
    var api_url = "https://ko.wikipedia.org/w/api.php";
    var params = {
        action: "query",
        list: "usercontribs",
        ucuser: username,
        ucshow: "new",
        uclimit: "max",
        format: "json"
    };

    // 문서 목록을 처리하는 함수를 정의합니다
    async function process_pages(data) {
        // 문서 목록을 가져옵니다
        var pages = data.query.usercontribs;
        // 각 문서에 대해 반복합니다
        for (var i = 0; i < pages.length; i++) {
            // 문서 제목을 가져옵니다
            var title = pages[i].title;

            // 확인하기 위해 해당 문서의 내용을 가져옵니다
            var content_params = {
                action: "parse",
                page: title,
                format: "json"
            };

            // API를 통해 해당 문서의 내용을 가져옵니다
            var contentData = await $.getJSON(api_url, content_params);
            // 문서의 내용을 가져왔습니다
            var content = contentData.parse.text['*'];

            // 이미 삭제 신청 틀이 있는지 확인합니다
            if (/\{\{\s*삭제\s+[신청|요청]\s*/i.test(content)) {
                // 이미 삭제 신청 틀이 있으므로 해당 문서를 건너뛰기
                console.log("건너뛰는 중: " + title + " 문서에 이미 삭제 신청 틀이 있습니다");
            } else {
                // 문서를 삭제 신청하는 API 요청을 만듭니다
                var csrf = mw.user.tokens.get("csrfToken");
                var delete_params = {
                    action: "edit",
                    title: title,
                    prependtext:"{{풀기:삭제 신청|"+reason+"}}\n",
                    summary: reason + "에 의한 삭제 신청",
                    token: csrf,
                    format: "json"
                };
                // API 요청을 보냅니다
                var response = await $.post(api_url, delete_params);
                // 응답을 확인합니다
                if (response.error) {
                    // 오류가 발생한 경우 메시지를 출력합니다
                    console.log("오류: " + response.error.info);
                } else {
                    // 성공한 경우 메시지를 출력합니다
                    console.log("성공: " + title + " 문서가 삭제 신청되었습니다");
                }
            }
        }
        // 다음 페이지가 있는지 확인합니다
        if (data.continue) {
            // 다음 페이지의 API 요청을 만듭니다
            var next_params = Object.assign({}, params, data.continue);
            // API 요청을 보냅니다
            await $.getJSON(api_url, next_params, process_pages);
        } else {
            // 더 이상 페이지가 없는 경우 메시지를 출력합니다
            console.log("완료: 모든 문서가 삭제 신청되었습니다");
        }
    }

    // 첫 번째 페이지의 API 요청을 보냅니다
    await $.getJSON(api_url, params, process_pages);
}

// </nowiki>