MediaWiki:Common.js/Wikinews:Dialog/diagnostic panel

/* For. See Special:WhatLinksHere/. */

function panelVersion { return '0.9 (2019-10-11)'; }

var modeSelectRegion = { // setup // expertFlag // simpleOnly // expertOnly // handleComplicateButton // handleSimplifyButton };

var idSelectRegion = { // setup // handleMenuChange // handleExamineButton };

var basicDataRegion = { // setup // handleShowButton // handleWriteButton };

var detailedDataRegion = { // collect };

function safeHtml(s) { return s.replace(/\/g,'&gt;'); };

detailedDataRegion.collect = function (buildEntry) { //   // assemble a string describing all the detailed data, // by calling buildEntry with info about each datum //   var id = $('.wikidialog-panel-data-id').text; var s = ''; $('.wikidialog-panel-check-param').each(function {        var param = $(this);        if (param[0].checked) {            var n = param.attr('id').substring(9);            var v = sessionStorage['wikidialog:' + id + ':in:' + n];            s += buildEntry('incoming', n, v);        }    }); $('.wikidialog-panel-check-field').each(function {        var save = $(this);        if (save[0].checked) {            var n = save.attr('id').substring(8);            var v = sessionStorage['wikidialog:' + id + ':sv:wikidialog-' + n];            s += buildEntry('field', n, v);        }    }); $('.wikidialog-panel-rbparam').each(function {        var rollback = $(this);        if (rollback[0].checked) {            var n = rollback.attr('id').substring(12);            var v = sessionStorage['wikidialog:' + id + ':rb-in:' + n];            s += buildEntry('rollback incoming', n, v);        }    }); $('.wikidialog-panel-check-rbfield').each(function {        var saverb = $(this);        if (saverb[0].checked) {            var n = saverb.attr('id').substring(11);            var v = sessionStorage['wikidialog:' + id + ':sv-rb:wikidialog-' + n];            s += buildEntry('rollback field', n, v);        }    }); return s; }

basicDataRegion.handleShowButton = function (event) { $('.wikidialog-panel-region-detail').replaceWith(function {        return ' ' +            detailedDataRegion.collect(function (c, n, v) { return ' ' + c + ' ' + n + (v ? (': " "')                       : ' null'); }) + ' ';   }); }

basicDataRegion.handleWriteButton = function (event) { function stepFour(header, add, pageName, existing) { //       // check for duplication //       if (existing) { if (existing.indexOf(add) >= 0) { alert('The data to be written appears to be\nalready present on your user talk page'); return; }       }        //        // confirm //       if (! confirm('Save dialog data to user talk page?')) return; //       // save to page //       var apidata = { format:       'json', action:      'edit', text:        (header + '\n' + add), title:       pageName, section:     'new', sectiontitle: 'Dialog data (saved via diagnostic panel)', summary:     '/* Dialog data (saved via diagnostic panel) */ new section', };       apidata.token = mw.user.tokens.get('csrfToken'); // always the last field added $.ajax({           type: 'POST',            url:  mw.util.wikiScript('api'),            data: apidata,            datatype: 'json'        }).done(function(data) {            if (data && data.edit && data.edit.result && (data.edit.result == 'Success'))                alert('Dialog data saved');            else if (data && data.error)                alert('Dialog data save error ' + data.error.code);            else                alert('Dialog data save unknown error');        }).fail(function  {            alert('Dialog data save misfired');        }); }   function stepThree(header, add, pageName) { //       // check status of talk page //       $.getJSON(            mw.util.wikiScript('api'),            {                format: 'json',                action: 'query',                prop:   'revisions',                rvprop: 'content',                rvlimit: 1,                titles:  pageName,            }        ).done(function(data) {            var error = ;            var existing = ;            if (! (data.query && data.query.pages)) {               error = 'page query misfired';            }            else for (var p in data.query.pages) {                if ('invalid' in data.query.pages[p]){                    error = 'page name invalid';                } else if ('missing' in data.query.pages[p]) {                    stepFour(header, add, pageName);                    return;                } else {                    if (data.query.pages[p].revisions && data.query.pages[p].revisions[0] && data.query.pages[p].revisions[0]['*'] ) {                       stepFour(header, add, pageName, data.query.pages[p].revisions[0]['*']);                        return;                    }                    else error = 'page query misfired';                }            }            alert('encountered an error while preparing to write data\n    ' + error);        }).fail(function  {            alert('encountered an error while preparing to write data:\n    page query misfired');        }); }   function stepTwo(header, add) { //       // work out page to write to        // var userName = mw.config.get('wgUserName'); if (! userName) { // should prevent the button from being created, but check here anyway alert('You have to be logged in to write the data'); return; }       var pageName = 'User talk:' + userName; stepThree(header, add, pageName); }   function stepOne { //       // what to write //       var id = $('.wikidialog-panel-data-id').text; var header = sessionStorage['wikidialog:' + id + ':incoming']; if (header) { header = 'page address .'; } else { header = 'no page address.'; }       header = 'ID ' + id + ', ' + header; var add = detailedDataRegion.collect(function (c, n, v) {           return ('\n\n\n' + c + ' ' + n + (v ? (':\n\n' + v)                  : ' null'));        }); if (! add) { alert('There appears to be nothing to write.'); return; }       stepTwo(header, add); }   stepOne; }

basicDataRegion.setup = function (id) { $('.wikidialog-panel-region-basic').replaceWith(function {        var s = '';        //        // stuff for expert mode        //        var incoming = sessionStorage['wikidialog:' + id + ':incoming'];        if (incoming) {            s += 'Page address for this ID: ' + safeHtml(incoming);        } else {            s += 'Warning: No page address for this ID.';        }        //        s += ' Incoming request ';        var origin = sessionStorage['wikidialog:' + id + ':origin'];        if (origin) {            s += 'authenticated from: ' + safeHtml(origin);            var proxy = sessionStorage['wikidialog:' + id + ':proxy'];            if (proxy) {                s += '                ' +                          '                ' +                          '                 via proxy: ' + safeHtml(proxy);            }        } else {            var unauth = sessionStorage['wikidialog:' + id + ':unauth']; if (unauth) { s += 'raw from: ' + safeHtml(sessionStorage['wikidialog:' + id + ':unauth']); } else { s += 'not found.'; }       }        //        var prevurl = sessionStorage['wikidialog:' + id + ':prevurl']; var dravail = sessionStorage['wikidialog:' + id + ':rb-available']; if (prevurl) { s += ' Simple rollback available, to: ' + prevurl; } else if (dravail) { s += ' Delegating rollback available.'; var drincoming = sessionStorage['wikidialog:' + id + ':rb-incoming']; if (drincoming) { s += '   restore incoming: ' + drincoming; } else { s += "   no incoming to restore (something's wrong)."; }           var drunauth = sessionStorage['wikidialog:' + id + ':rb-unauth']; if (drunauth) { s += '   restore unauthorized-origin: ' + drunauth; } else { s += '   null unauthorized-origin.'; }           var drorigin = sessionStorage['wikidialog:' + id + ':rb-origin']; if (drorigin) { s += '   restore authorized-origin: ' + drorigin; } else { s += '   null authorized-origin.'; }           var drproxy = sessionStorage['wikidialog:' + id + ':rb-proxy']; if (drproxy) { s += '   restpre authorized-proxy: ' + drproxy; } else { s += '   null authorized-proxy.'; }       } else { s += ' Rollback unavailable.'; }       //        var notes = sessionStorage['wikidialog:' + id + ':notes']; if (notes) { s += ' Dialog notes for this ID: ' + notes + ' '; } else { s += ' No dialog notes for this ID. ';       }        //        // all modes //       s = ' Data for dialog ID ' + id + ' : ' + modeSelectRegion.expertOnly(s); //       var params = sessionStorage['wikidialog:' + id + ':params']; if (params) { s += 'Incoming parameters to this ID: '; params = params.split(','); for (var k=0; (k < params.length); k += 1) if (params[k]) { s += '   ' + params[k]; }       } else { s += 'No incoming parameters to this ID.'; }       //        s += ' '; var saves = sessionStorage['wikidialog:' + id + ':saves']; if (saves) { s += 'Saved fields for this ID: '; saves = saves.split(','); for (var k=0; (k < saves.length); k += 1) if (saves[k]) { saves[k] = saves[k].substring(11); s += '   ' + saves[k]; }       } else { s += 'No saved fields for this ID.'; }       //        if ((! prevurl) && dravail) { s += ' '; var dradded = sessionStorage['wikidialog:' + id + ':rb-added']; if (dradded) { s += 'Added parameters to remove on rollback for this ID:   ' + dradded; } else { s += 'No added parameters to remove on rollback for this ID.'; }           s += ' '; var drchanged = sessionStorage['wikidialog:' + id + ':rb-changed']; if (drchanged) { s += 'Changed parameters to restore on rollback for this ID: '; drchanged = drchanged.split(','); for (var k=0; (k < drchanged.length); k += 1) if (drchanged[k]) { s += '   ' + drchanged[k]; }           } else { s += 'No changed parameters to restore on rollback for this ID.'; }           s += ' '; var drsaves = sessionStorage['wikidialog:' + id + ':saves-rb']; if (drsaves) { s += 'Saved fields to restore on rollback for this ID: '; drsaves = drsaves.split(','); for (var k=0; (k < drsaves.length); k += 1) if (drsaves[k]) { drsaves[k] = drsaves[k].substring(11); s += '   ' + drsaves[k]; }           } else { s += 'No saved fields to restore on rollback for this ID.'; }       }        //        s += '   '; if (mw.config.get('wgUserName')) { s += ' '; }       s += ' '; //       s += ' '; return s;   });    $('.wikidialog-panel-check-param').replaceWith(function  { var i = $(this).attr('id'); return $(document.createElement('input')).attr('type','checkbox').addClass('wikidialog-panel-check-param').attr('id',i); });   $('.wikidialog-panel-check-field').replaceWith(function  { var i = $(this).attr('id'); return $(document.createElement('input')).attr('type','checkbox').addClass('wikidialog-panel-check-field').attr('id',i).attr('checked','yes'); });   $('.wikidialog-panel-rbparam').replaceWith(function  { var i = $(this).attr('id'); return $(document.createElement('input')).attr('type','checkbox').addClass('wikidialog-panel-rbparam').attr('id',i); });   $('.wikidialog-panel-check-rbfield').replaceWith(function  { var i = $(this).attr('id'); return $(document.createElement('input')).attr('type','checkbox').addClass('wikidialog-panel-check-rbfield').attr('id',i); });   $('.wikidialog-panel-button-show').replaceWith(function  { return $(document.createElement('input')).attr('type','button').addClass('wikidialog-panel-button-show') .val('show').attr('title','show selected values for this ID').click(basicDataRegion.handleShowButton); });   $('.wikidialog-panel-button-write').replaceWith(function  { return $(document.createElement('input')).attr('type','button').addClass('wikidialog-panel-button-write') .val('write').attr('title','write selected values for this ID').click(basicDataRegion.handleWriteButton); }); }

idSelectRegion.handleExamineButton = function (event) { var id = $.trim($('.wikidialog-panel-input-id').val); if (id != id.match(/\d+/)) { alert('Not a valid ID (must be one or more digits): ' + safeHtml(id)); return; }   basicDataRegion.setup(id); }

idSelectRegion.handleMenuChange = function (event) { var select = event.target; if (select.selectedIndex < 0) return; $('.wikidialog-panel-input-id').val(select.options[select.selectedIndex].value); }

idSelectRegion.setup = function { // assumes sessionStorage is available //   // prepare content for page //   $('.wikidialog-panel-region-selectid').replaceWith(      ' ' +      modeSelectRegion.expertOnly( (sessionStorage.wikidialogSequenceBound        ? ('Action-sequence in progress; bound = ' + sessionStorage.wikidialogSequenceBound + '. ')        : 'No action-sequence in progress. ')) +      modeSelectRegion.expertOnly( (sessionStorage.wikidialogNextID        ? ('Unused dialog ID available for next use: ' + (Number(sessionStorage.wikidialogNextID) - 1))         : 'No record of a dialog ID available for next use.') + ' ') +     (sessionStorage.wikidialogActiveList ? (modeSelectRegion.simpleOnly('Dialog states available for examination, listed by state ID from') +         modeSelectRegion.expertOnly('Dialog IDs now listed as in use,') +          ' most recent to least recent: ' +          sessionStorage.wikidialogActiveList +          modeSelectRegion.simpleOnly((function  {                  var active = sessionStorage.wikidialogActiveList;                  active = active.split(',');                  var stack = sessionStorage.wikidialogStack;                  stack = (stack ? stack.split('&') : []);                 var result = ;                  for (var j = (stack.length - 2); (j >= 0); j -= 2) {                      for (var k = (active.length - 1); (k >= 0); k--)                          if (stack[j] == active[k]) {                              stack[j] = ;                              break;                          }                      if (stack[j]) { result += ',' + stack[j]; }                  }                  return result;              })) +          modeSelectRegion.expertOnly('; ' + (sessionStorage.wikidialogStack              ? (function  { var stack = sessionStorage.wikidialogStack; stack = stack.split('&'); var result = ''; for (var j = (stack.length - 2); (j >= 0); j -= 2) { result += (result ? (',' + stack[j]) : stack[j]); }                    return 'stack, top to bottom: ' + result; })              : 'no stack')) +          '. Select an active ID: ' +          modeSelectRegion.expertOnly(' or')) : modeSelectRegion.simpleOnly(sessionStorage.wikidialogStack          ? ("Dialog states available for examination, listed by state ID from most recent to least recent: " + (function {                   var stack = sessionStorage.wikidialogStack;                   stack = stack.split('&');                   var result = '';                   for (var j = (stack.length - 2); (j >= 0); j -= 2) {                       result += (result ? (',' + stack[j]) : stack[j]);                  }                   return result + '.';               }))           : "No dialog states available for examination.") + modeSelectRegion.expertOnly("No dialog IDs listed as in use; " +          (sessionStorage.wikidialogStack ? (function {                   var stack = sessionStorage.wikidialogStack;                   stack = stack.split('&');                   var result = '';                   for (var j = (stack.length - 2); (j >= 0); j -= 2) {                       result += (result ? (',' + stack[j]) : stack[j]);                  }                   return 'stack, top to bottom: ' + result;               }) : 'no stack') +          ". You may")) +      modeSelectRegion.expertOnly( ' enter a dialog ID manually: ') +     ((sessionStorage.wikidialogActiveList || sessionStorage.wikidialogStack) ? ' '      : modeSelectRegion.expertOnly(' ')) +      ' ' +      '  panel version ' + panelVersion + ' '); var first = ''; $('.wikidialog-panel-menu-id').replaceWith(function {        var e = document.createElement('select');        var active = sessionStorage.wikidialogActiveList;        active = (active ? active.split(',') : []);       for (var k=0; (k < active.length); k += 1) if (active[k]) {            if (! first) first = active[k];           e.appendChild(new Option(active[k],active[k],false,false));        }        var stack = sessionStorage.wikidialogStack;        stack = (stack ? stack.split('&') : []);       for (var j = (stack.length - 2); (j >= 0); j -= 2) {            for (var k = (active.length - 1); (k >= 0); k--)                if (stack[j] == active[k]) {                    stack[j] = '';                    break;                }            if (stack[j]) { e.appendChild(new Option(stack[j],stack[j],false,false)); }        }        return $(e).addClass('wikidialog-panel-menu-id');    }); $('.wikidialog-panel-input-id').replaceWith(function {        return $(document.createElement('input')).attr('type','text').addClass('wikidialog-panel-input-id').val(first).attr('size',3);    }); $('.wikidialog-panel-button-examine').replaceWith(function {        return $(document.createElement('input')).attr('type','button').addClass('wikidialog-panel-button-examine')            .val('examine').attr('title','examine data for this ID').click(idSelectRegion.handleExamineButton);    }); $('.wikidialog-panel-menu-id').change(idSelectRegion.handleMenuChange); }

modeSelectRegion.expertFlag = false;

modeSelectRegion.simpleOnly = function (s) { return '' + s + ' '; }

modeSelectRegion.expertOnly = function (s) { return '' + s + ' '; }

modeSelectRegion.handleComplicateButton = function (event) { $('.wikidialog-panel-mode-simple').css('display', 'none'); $('.wikidialog-panel-mode-expert').css('display', 'inline'); modeSelectRegion.expertFlag = true; }

modeSelectRegion.handleSimplifyButton = function (event) { $('.wikidialog-panel-mode-simple').css('display', 'inline'); $('.wikidialog-panel-mode-expert').css('display', 'none'); modeSelectRegion.expertFlag = false; }

modeSelectRegion.setup = function { $('div.wikidialog-diagnostic-panel').replaceWith(' ' +       modeSelectRegion.simpleOnly( 'This is the simple version of the panel; there is also an expert version. ' +           ' ') +        modeSelectRegion.expertOnly( 'This is the expert version of the panel; there is also a simple version. ' +           ' ') +        ' ' +        ' ' +        ' ');    $('.wikidialog-panel-button-complicate').replaceWith(function  {        return $(document.createElement('input')).attr('type','button').addClass('wikidialog-panel-button-complicate')            .val('toggle expert mode').attr('title','switch to expert panel mode').click(modeSelectRegion.handleComplicateButton);    }); $('.wikidialog-panel-button-simplify').replaceWith(function {        return $(document.createElement('input')).attr('type','button').addClass('wikidialog-panel-button-simplify')            .val('simplify').attr('title','switch to simple panel mode').click(modeSelectRegion.handleSimplifyButton);    }); idSelectRegion.setup; }

if (sessionStorage) { modeSelectRegion.setup; } else { $('div.wikidialog-diagnostic-panel').replaceWith(       ' ' +        "No access to dialog storage from here.  If you are viewing this page through the same browser session as a dialog whose data you're trying to recover, something strange is going on.  The most likely reason for no dialog storage is a very old browser, and if that were so, it shouldn't have been possible to run a dialog in it at all." +        ' '    ); }