بەکارھێنەر:Aram/ShowPageLintError.js

لە ئینسایکڵۆپیدیای ئازادی ویکیپیدیاوە
تێبینی: دوای پاشەکەوتکردن، پێویستە کاشی وێبگەڕەکەت پاک بکەیتەوە تا گۆڕانکارییەکان ببینیت. بۆ گووگڵ کڕۆم، فایەرفۆکس، مایکرۆسۆفت ئێج و سافاری: پەنجە لەسەر دوگمەی ⇧ Shift ڕاگرە و کرتە لەسەر Reload بکە. بۆ وردەکاری و ڕێنمایییەکان لەسەر وێبگەڕەکانی تر، بڕوانە ئێرە.
/*
 * [[ja:利用者:MawaruNeko/ShowPageLintError.js]]
 * Lint errorsの詳細情報を表示するカスタムJS
 * Custom JS to show detailed information of "lint errors"
 * 
 * 説明:
 *   カスタムJSとして導入して下さい。
 *   1. ページ情報の画面のLint errorsの章に詳細を表示します。
 *      Lint errorsの章があると'Show Lint errors'というリンクが表示されます。
 *      'Show Lint errors'リンクをクリックすると、詳細なエラー内容を表示します。
 *      Extension:Linter に記録されたものを表示するので、古い場合があります。
 *   2. 編集画面で、編集中の内容のLint errorsを表示する
 *      'Test Lint errors'リンクを表示します。クリックすると、
 *      リンクの下に現在の編集中の内容のLint errorsを表示します。
 *   3. 上記の2箇所の下に'Edit to check lint errors in text here'
 *      というテキストエリアを表示します。ここでは、任意のテキストに対して、
 *      テンプレートの展開とLint errorsの確認を行うことができます。
 * 
 * Description:
 *   Use this file as custom JS.
 *   1. This script creates 'Show Lint errors' link in
 *      'Lint errors' chapter of information page.
 *      By clicking the link, detained error information is shown.
 *      These errors are recorded by Extension:Linter.
 *   2. This script creates 'Show Lint errors' link in edit pages.
 *      By clicking the link, you can test lint errors of editing wiki text.
 *   3. You can expand or check lint errors of any wiki text
 *      in the textarea 'Edit to check lint errors in text here'.
 * 
 * Global variables:
 *   以下のグローバル変数を、このスクリプトを読み込むより前に設定することで、
 *   このスクリプトの動作を制御できます。
 *   (default)
 *   mw.libs.ShowPageLintError = {
 *     testsLinterErrorsOnEditInit: false,
 *     summaryMessage: null,
 *   };
 *   mw.libs.ShowPageLintError.testsLinterErrorsOnEditInit:
 *     編集画面がロードされた時に自動的にLint errorsを表示します。
 *   mw.libs.ShowPageLintError.summaryMessage:
 *     要約欄に定型句を追加する'Add summary'リンクを追加します。
 *     この変数に、追加したい定型句を設定してください。
 *     e.g. "[[Wikipedia:井戸端/subj/RemexHTML移行に関する合意形成]]に基づき修正"
 * 
 * このファイルはパブリックドメインとします。
 * This file is public domain.
 */

(function () {
    'use strict';

    /* Api functions */
    function getWikiSource(wgArticleId) {
        return $.get(mw.util.wikiScript(), {
            action: 'raw',
            curid: wgArticleId,
        });
    }

    function getApiLintErrorsOfArticle(wgArticleId) {
        return (new mw.Api()).get({
            action: 'query',
            list: 'linterrors',
            lntlimit: 'max',
            lntpageid: wgArticleId,
        }).then(function (data) {
            return data.query.linterrors;
        });
    }

    function getRestLintErrorsOfText(text, title) {
        return $.ajax({
            type: 'POST',
            url: '/api/rest_v1/transform/wikitext/to/lint',
            data: {
                wikitext: text,
                title: title,
            },
            dataType: 'json',
        });
    }

    function expandTemplates(text, title, revid) {
        return (new mw.Api()).post({
            action: 'expandtemplates',
            prop: 'wikitext',
            text: text,
            title: title,
            revid: revid,
        }).then(function (data) {
            return data.expandtemplates.wikitext;
        });
    }

    /* format functions */
    function createCategoryHelpLink(category) {
        return $('<a>').addClass('mw-helplink').text(category).
          attr('href', '//www.mediawiki.org/wiki/Special:MyLanguage/Help:Extension:Linter/' + category).
          attr('target', '_blank');
    }

    function createEditLink(wgArticleId, lintId) {
        var editLinkPath = mw.util.wikiScript() + '?' + $.param({
            action: 'edit',
            curid: wgArticleId,
            lintid: lintId,
        });
        return $('<a>').text('edit').attr('href', editLinkPath);
    }

    function selectTextarea($textarea, start, end) {
        var textarea = $textarea[0];
        textarea.focus();
        textarea.setSelectionRange(start, end);
    }

    function createLocationLink($textarea, start, end) {
        return $('<a>').text(start + ' - ' + end).
          attr('href', 'javascript:void(0)').
          on('click', function (event) {
              selectTextarea($textarea, start, end);
          });
    }

    function createTemplateLink(templateInfo) {
        var $templateLink;
        if (templateInfo) {
            if (templateInfo.name) {
                var linkPath = mw.util.getUrl(templateInfo.name);
                $templateLink = $('<a>').text(templateInfo.name).attr('href', linkPath);
            } else if ('multiPartTemplateBlock' in templateInfo) {
                $templateLink = 'multi-part-template-block';
            } else {
                $templateLink = '-';
            }
        } else {
            $templateLink = '-';
        }
        return $templateLink;
    }

    /* show linter errors */
    function showApiLinterErrors(apiLinterErrors, config, $table, $wikiSourceTextarea) {
        var $thead = $('<thead>').appendTo($table);
        $('<tr>').
          append($('<th>').text('ناونیشانی شاش')).
          append($('<th>').text('دەستکاری')).
          append($('<th>').text('پۆل')).
          append($('<th>').text('شوێن')).
          append($('<th>').text('پارامەترەکان')).
          append($('<th>').text('داڕێژە')).
          append($('<th>').text('زانیاریی داڕێژە')).
          appendTo($thead);
        var $tbody = $('<tbody>').appendTo($table);

        var appendLintError = function (linterror) {
            $('<tr>').
              append($('<td>').append(linterror.lintId)).
              append($('<td>').append(createEditLink(config.wgArticleId, linterror.lintId))).
              append($('<td>').append(createCategoryHelpLink(linterror.category))).
              append($('<td>').append(createLocationLink($wikiSourceTextarea, linterror.location[0], linterror.location[1]))).
              append($('<td>').text(JSON.stringify(linterror.params))).
              append($('<td>').append(createTemplateLink(linterror.templateInfo))).
              append($('<td>').text(JSON.stringify(linterror.templateInfo))).
              appendTo($tbody);
        };

        if (apiLinterErrors.length >= 1) {
            $.each(apiLinterErrors, function (index, linterror) {
                appendLintError(linterror);
            });
        } else {
            $('<tr>').
              append($('<td>').attr('colspan', 7).text('ھەڵەی شاش نییە')).
              appendTo($tbody);
        }
    }

    function showApiLinterErrorsForTextarea(config, $table, $wikiSourceTextarea) {
        $table.empty();
        var $loadingMsg = $('<span>').text('ھەڵە شاشەکان لە ئەی پی ئاییەوە باردەکرێن...').insertAfter($table);
        getApiLintErrorsOfArticle(config.wgArticleId).then(function (apiLinterErrors) {
            $loadingMsg.remove();
            showApiLinterErrors(apiLinterErrors, config, $table, $wikiSourceTextarea);
        });
    }

    function showRestLinterErrors(restLinterErrors, config, $table, $wikiSourceTextarea) {
        var $thead = $('<thead>').appendTo($table);
        $('<tr>').
          append($('<th>').text('پۆل')).
          append($('<th>').text('شوێن')).
          append($('<th>').text('پارامەترەکان')).
          append($('<th>').text('داڕێژە')).
          append($('<th>').text('زانیاریی داڕێژە')).
          appendTo($thead);
        var $tbody = $('<tbody>').appendTo($table);

        var appendLintError = function (linterror) {
            $('<tr>').
              append($('<td>').append(createCategoryHelpLink(linterror.type))).
              append($('<td>').append(createLocationLink($wikiSourceTextarea, linterror.dsr[0], linterror.dsr[1]))).
              append($('<td>').text(JSON.stringify(linterror.params))).
              append($('<td>').append(createTemplateLink(linterror.templateInfo))).
              append($('<td>').text(JSON.stringify(linterror.templateInfo))).
              appendTo($tbody);
        };

        if (restLinterErrors.length >= 1) {
            $.each(restLinterErrors, function (index, linterror) {
                appendLintError(linterror);
            });
        } else {
            $('<tr>').
              append($('<td>').attr('colspan', 5).text('ھەڵەی شاش نییە')).
              appendTo($tbody);
        }
    }

    function expandTemplatesInTextarea(config, $textarea) {
        expandTemplates($textarea.val(), config.wgPageName).then(function (result) {
            $textarea.val(result);
        });
    }

    function showRestLinterErrorsForTextarea(config, $table, $wikiSourceTextarea) {
        $table.empty();
        var $loadingMsg = $('<span>').text('ھەڵە شاشەکان لە ئەی پی ئایی ماوەوە باردەکرێن...').insertAfter($table);
        getRestLintErrorsOfText($wikiSourceTextarea.val(), config.wgPageName).then(function (restLinterErrors) {
            $loadingMsg.remove();
            showRestLinterErrors(restLinterErrors, config, $table, $wikiSourceTextarea);
        });
    }

    function createEditableTextarea(config, $div) {
        var $editableTextarea = $('<textarea>').css('width', '100%').css('height', '10em');
        $('<label>').append('بۆ پشکنینی ھەڵە شاشەکانی نووسینەکەی ئێرە دەستکاری بکە:').
          append($editableTextarea).appendTo($div);

        var $expandTemplatesLink = $('<a>').text('داڕێژەکان فراوان بکەرەوە').
          attr('href', 'javascript:void(0)').
          on('click', function () {
              expandTemplatesInTextarea(config, $editableTextarea);
          }).
          appendTo($div);

        $div.append(' | ');

        var $restLinterErrorsTable = $('<table>').addClass('linter-details wikitable mw-page-info').appendTo($div);
        var $showRestLinterErrorsLink = $('<a>').text('ھەڵە شاشەکان پیشان بدە').
          attr('href', 'javascript:void(0)').
          on('click', function () {
              showRestLinterErrorsForTextarea(config, $restLinterErrorsTable, $editableTextarea);
          }).
          insertBefore($restLinterErrorsTable);
    }

    function createApiLinterErrorsTableAndEditableTextarea(config, $linterDetailsTable, $wikiSourceTextarea) {
        getWikiSource(config.wgArticleId).then(function (wikiSource) {
            $wikiSourceTextarea.text(wikiSource);
            showApiLinterErrorsForTextarea(config, $linterDetailsTable, $wikiSourceTextarea);

            var $div = $('<div>').insertAfter($linterDetailsTable);
            createEditableTextarea(config, $div);
        });
    }

    function mainForInfo(config, $linterTable) {
        var $showLinterErrorsLink = $('<a>').text('ھەڵە شاشەکان پیشان بدە').
          attr('href', 'javascript:void(0)').
          on('click', function () {
              var $linterDetailsTable = $('<table>').addClass('linter-details wikitable mw-page-info').insertAfter($linterTable);
              var $wikiSourceTextarea = $('<textarea>').attr('readonly', true).css('width', '100%').css('height', '10em').insertAfter($linterTable);
              $showLinterErrorsLink.remove();

              createApiLinterErrorsTableAndEditableTextarea(config, $linterDetailsTable, $wikiSourceTextarea);
          }).
          insertAfter($linterTable);
    }

    function mainForEdit(config, $wpTextbox1, $editOptionsDiv, $editButtonsDiv, testsLinterErrorsOnEditInit, summaryMessage) {
        var $linterDetailsTable = $('<table>').addClass('linter-details wikitable mw-page-info').insertAfter($editOptionsDiv);
        var $div = $('<div>').insertAfter($linterDetailsTable);
        createEditableTextarea(config, $div);

        var $testLinterErrorsLink = $('<a>').text('ھەڵە شاشەکان تاقی بکەرەوە').
            attr('href', 'javascript:void(0)').
            on('click', function () {
                showRestLinterErrorsForTextarea(config, $linterDetailsTable, $wpTextbox1);
            }).
            insertAfter($editButtonsDiv);

        if (testsLinterErrorsOnEditInit) {
            /* initial display of lint errors */
            showRestLinterErrorsForTextarea(config, $linterDetailsTable, $wpTextbox1);
        }

        if (summaryMessage) {
            var $addSummaryMessageLink = $('<a>').text('کورتەی دەستکاری زیاد بکە').
                attr('href', 'javascript:void(0)').
                on('click', function () {
                    $('#wpSummary').val($('#wpSummary').val() + summaryMessage);
                }).
                insertAfter($testLinterErrorsLink);
            $testLinterErrorsLink.after(' | ');
        }
    }

    $(function () {
        if (!('ShowPageLintError' in mw.libs)) {
            mw.libs.ShowPageLintError = {
                testsLinterErrorsOnEditInit: false,
                summaryMessage: null,
            };
        }
        mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
            var config = mw.config.get(['wgArticleId', 'wgAction', 'wgPageName']);
            if (config.wgAction === 'info') {
                var $linterTable = $('#mw-pageinfo-linter + table');
                if ($linterTable[0]) {
                    mainForInfo(config, $linterTable);
                }
            } else if ($('#wpTextbox1')[0]) {
                mainForEdit(config, $('#wpTextbox1'), $('.editOptions'), $('.editButtons'),
                    mw.libs.ShowPageLintError.testsLinterErrorsOnEditInit, mw.libs.ShowPageLintError.summaryMessage);
            }
        });
    });
})();