Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// See [[User:Mr.Z-man/closeAFD]] for documentation
/*
The MIT License (MIT)

Copyright (c) 2013 Mr.Z-man

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

mw.loader.using( ['mediawiki.util', 'mediawiki.api', 'jquery.ui'], function () {
  if (mw.config.get('wgNamespaceNumber') === 4 &&
  mw.config.get('wgTitle').indexOf('Articles for deletion/') === 0 &&
  mw.config.get('wgTitle').indexOf('Wikipedia:Articles for deletion/Log/20' === -1) ) {
    $(document).ready( function () {
      mw.util.addPortletLink( 'p-cactions', '#', 'Close', 'ca-closeAFD', 'Close AFD' );
      mw.util.addPortletLink( 'p-cactions', '#', 'Relist', 'ca-relistAFD', 'Relist AFD' );
    } );

    CloseAFD = {
      relisting: false,
      action: false,
      speedy: false,
      NAC: false,
      api: false,
      actionmap: {
        'delete':'delete',
        'keep':'keep',
        'nocon':'no consensus',
        'merge':'merge to ',
        'redir':'redirect to ',
        'delandred':'delete and redirect to '
      },
      token: false,
      titles: [],
      wikitext: false,
      actionsStarted: {
        'notdoneyet':0,
        'afdpage':0,
        'delete':0,
        'edit':0,
        'edittalk':0,
        'redirects':0
      },
      actionsFinished: {
        'notdoneyet':0,
        'afdpage':0,
        'delete':0,
        'edit':0,
        'edittalk':0,
        'redirects':0
      },
      months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
      debate: mw.config.get('wgTitle').substr(mw.config.get('wgTitle').indexOf('/')+1),
      logs: {}
    };

    if (jQuery.inArray( "sysop",  mw.config.get('wgUserGroups') ) === -1) {
      CloseAFD.NAC = true;
    }

    // Handler for the close link in the top bar
    CloseAFD.close = function() {
      $( "#closeAFD-dialog" ).show().dialog( { title: "Closing "+mw.config.get('wgTitle'),
        width:400,
        buttons: [ { text: "Close AFD", click: CloseAFD.startCloseAction },
        { text: "Cancel", click: function() {
          $( this ).dialog( "close" );
        } }]
      } );
      // We get the token now just to make the code less convoluted later
      CloseAFD.api = new mw.Api();
      params = {
        'action':'query',
        'meta':'tokens',
        'type': 'csrf'
      };
      CloseAFD.api.get(params)
      .done ( function (data) {
        CloseAFD.token = data.query.tokens.csrftoken;
      });
      return false;
    };

    // Handler for the relist link in the top bar
    CloseAFD.relist = function() {
      CloseAFD.relisting = true;
      $( "#closeAFD-relist-dialog" ).show().dialog( { title: "Relisting "+mw.config.get('wgTitle'),
        width:600,
        buttons: [ { text: "Relist", click: CloseAFD.startRelistAction },
        { text: "Cancel", click: function() {
          $( this ).dialog( "close" );
        } }]
      } );
      $('#closeAFD-relist-number').val( $('.xfd_relist').length + 1 );
      $('#closeAFD-relist-logs').text("Getting AfD logs...");
      CloseAFD.api = new mw.Api();
      params = {
        'action':'tokens',
        'type':'edit'
      };
      CloseAFD.api.get(params)
      .done ( function (data) {
        CloseAFD.token = data.tokens.edittoken;
      });
      params = {
        'action':'query',
        'list':'embeddedin',
        'eifilterredir':'nonredirects',
        'einamespace':'4',
        'eilimit':'500',
        'eititle':mw.config.get('wgPageName')
      };
      CloseAFD.api.get(params)
      .done ( function (data) {
        var oldlogtitle = '';
        for (var i=0; i<data.query.embeddedin.length; i++) {
          if (data.query.embeddedin[i].title.indexOf('Wikipedia:Articles for deletion/Log/') != -1) {
            oldlogtitle = data.query.embeddedin[i].title;
          }
        }
        var today = new Date();
        var year = today.getUTCFullYear().toString();
        var day = today.getUTCDate().toString();
        var month = CloseAFD.months[today.getUTCMonth()];
        var newlogtitle = 'Wikipedia:Articles for deletion/Log/'+year+' '+month+' '+day;
        var logs = $('<ul></ul>');
        var oldlog = $('<li></li>').text("Old log: ");
        var oll = $('<a></a>').attr('href', mw.config.get('wgArticlePath').replace('$1', oldlogtitle.replace(/_/g, ' ')))
        .text(oldlogtitle);
        $(oldlog).append(oll);
        $(logs).append(oldlog);
        var newlog = $('<li></li>').text("New log: ");
        var nll = $('<a></a>').attr('href', mw.config.get('wgArticlePath').replace('$1', newlogtitle.replace(/_/g, ' ')))
        .text(newlogtitle);
        $(newlog).append(nll);
        $(logs).append(newlog);
        $('#closeAFD-relist-logs').text('');
        $('#closeAFD-relist-logs').append(logs);
        CloseAFD.logs.old = oldlogtitle;
        CloseAFD.logs.new = newlogtitle;
      })
      .fail( function (error) {
        $('#closeAFD-relist-logs').css('color', 'red').text("Error: "+error);
      });
      return false;
    };

    // Check if we're completely done
    CloseAFD.checkDone = function() {
      if (CloseAFD.actionsStarted.notdoneyet == CloseAFD.actionsFinished.notdoneyet &&
      CloseAFD.actionsStarted.afdpage == CloseAFD.actionsFinished.afdpage &&
      CloseAFD.actionsStarted.delete == CloseAFD.actionsFinished.delete &&
      CloseAFD.actionsStarted.edit == CloseAFD.actionsFinished.edit &&
      CloseAFD.actionsStarted.edittalk == CloseAFD.actionsFinished.edittalk &&
      CloseAFD.actionsStarted.redirects == CloseAFD.actionsFinished.redirects) {
        elem = $('#closeAFD-action5');
        if (CloseAFD.relisting) {
          elem = $('#closeAFD-relist-done');
        }
        $(elem).css('color', 'darkgreen').css('font-weight', 'bold').text("Done – ");
        link = $("<a></a>").attr('href', "//en.wikipedia.org/wiki/"+mw.config.get('wgPageName')+"?action=purge")
        .text("Reload page").css('color', '#0645AD');
        $(elem).append(link);
      }

    };

    // Edit a page
    // pageid/title - page to edit
    // newxtext - new text for page
    // summary - edit summary
    // callback - function to call on success
    // errorElem - element to put an error message in
    // incr - Variable to increment
    CloseAFD.edit = function(pageid, title, newtext, summary, callback, incr) {
      if (/^\s*$/.test(newtext)) { // making sure we don't accidentally blank the page
        if (CloseAFD.relisting) {
          $('#closeAFD-relist-error').css('color', 'red').text('Error when trying to edit "'+title+'"');
        } else {
          $('#closeAFD-error').css('color', 'red').text('Error when trying to edit "'+title+'"');
        }
        CloseAFD.actionsFinished[incr]++;
        CloseAFD.checkDone();
        return false;
      }
      CloseAFD.actionsStarted[incr]++;
      editparams = {'action':'edit',
        'watchlist':'nochange'
      };
      if (pageid) {
        editparams.pageid = pageid;
      } else if (title) {
        editparams.title = title;
      }
      editparams.text = newtext;
      editparams.summary = summary;
      editparams.token = CloseAFD.token;
      CloseAFD.api.post(editparams)
      .fail( function (error) {
        if (CloseAFD.relisting) {
          $('#closeAFD-relist-error').css('color', 'red').text("Error: "+error);
        } else {
          $('#closeAFD-error').css('color', 'red').text("Error: "+error);
        }
        CloseAFD.actionsFinished[incr]++;
        CloseAFD.checkDone();
      })
      .done(function() {
        CloseAFD.actionsFinished[incr]++;
        callback();
        CloseAFD.checkDone();
      });
    };

    // Parameters same as .edit
    CloseAFD.deletePage = function(pageid, title, summary, callback, incr) {
      CloseAFD.actionsStarted[incr]++;
      delparams = {'action':'delete',
        'reason':summary,
        'token':CloseAFD.token,
        'watchlist':'nochange'
      };
      if (pageid) {
        delparams.pageid = pageid;
      } else if (title) {
        delparams.title = title;
      }
      CloseAFD.api.post(delparams)
      .fail( function (error) {
        $('#closeAFD-error').css('color', 'red').text("Error: "+error);
        CloseAFD.actionsFinished[incr]++;
        CloseAFD.checkDone();
      })
      .done(function(data) {
        CloseAFD.actionsFinished[incr]++;
        callback(data);
        CloseAFD.checkDone();
      });
    };

    // This gets called when you click the Relist button
    CloseAFD.startRelistAction = function() {
      $('#closeAFD-relist-comment').prop('disabled', true);
      $('#closeAFD-relist-number').prop('disabled', true);
      $('#closeAFD-relist-old-status').text('Getting logs...');
      $('#closeAFD-relist-afd-status').text('Getting AfD page...');
      params = {
        'action':'query',
        'prop':'revisions',
        'rvprop':'content',
        'titles':CloseAFD.logs.old+'|'+CloseAFD.logs.new+'|'+mw.config.get('wgPageName'),
        'indexpageids':'1'
      };
      CloseAFD.api.get(params)
      .done ( function (data) {
        var oldlogtext = '';
        var newlogtext = '';
        var afdtext = '';
        for (var i=0; i<data.query.pageids.length; i++) {
          pageid = data.query.pageids[i];
          title = data.query.pages[pageid].title;
          if (title == CloseAFD.logs.old) {
            oldlogtext = data.query.pages[pageid].revisions[0]['*'];
          } else if (title == CloseAFD.logs.new) {
            newlogtext = data.query.pages[pageid].revisions[0]['*'];
          } else {
            afdtext = data.query.pages[pageid].revisions[0]['*'];
          }
        }
        // Check the old log first because it's the easiest way to ensure no one already relisted it
        var t = RegExp.quote(mw.config.get('wgPageName').replace(/_/g, ' '));
        var patt = new RegExp("<!-- ?\\{\\{"+t+"\\}\\} ?-->", 'i' );
        var res = patt.exec(oldlogtext);
        if (res) {
          $('#closeAFD-relist-old-status').css('color', 'red').text("AfD appears to be relisted already!");
          $('#closeAFD-relist-afd-status').text('');
          return false;
        }
        // Now we also need to check that the AfD isn't closed
        if (afdtext.indexOf('xfd-closed') != -1) {
          $('#closeAFD-relist-afd-status').css('color', 'red').text("AFD is already closed!");
          $('#closeAFD-relist-old-status').text('');
          return false;
        }
        // Okay, now we can actually make the edits
        $('#closeAFD-relist-old-status').text('Editing logs...');
        $('#closeAFD-relist-afd-status').text('Editing AfD page...');
        summary = "Relisting [["+mw.config.get('wgPageName').replace(/_/g, ' ')+"]]";
        // New log:
        var newlogreg = new RegExp("<!-- Add new entries to the TOP of the following list -->", "i");
        newlogtext = newlogtext.replace(newlogreg, "<!-- Add new entries to the TOP of the following list -->\n{"+"{"+mw.config.get('wgPageName').replace(/_/g, ' ')+"}}<!--Relisted-->");
        // Old log:
        var oldlogreg = new RegExp("(\\{\\{"+t+"\\}\\})", 'i' );
        oldlogtext = oldlogtext.replace(oldlogreg, "<!-- $1 -->");
        // Afd page:
        var relistcomment = false;
        if ($('#closeAFD-relist-comment').val().length !== 0) {
          relistcomment = $('#closeAFD-relist-comment').val();
        }
        var relistnumber = $('#closeAFD-relist-number').val().toString();
        afdtext += '\n{'+'{subst:relist';
        if (relistcomment) {
          if (relistcomment.indexOf('~~'+'~') === -1) {
            relistcomment += ' ~~'+'~~';
          }
          afdtext += '|'+relistcomment+'|'+relistnumber;
        } else {
          afdtext += '|2='+relistnumber;
        }
        afdtext += '}}';
        afdtext = afdtext.replace(/Wikipedia:Articles for deletion\/Log\/\d{4} \D{4,9} \d{1,2}#/i, CloseAFD.logs.new+"#");
        CloseAFD.edit('', CloseAFD.logs.new, newlogtext, summary, function(data) {
          $('#closeAFD-relist-new-status').css('color', 'darkgreen').text("New log updated");
        }, 'edit');
        CloseAFD.edit('', CloseAFD.logs.old, oldlogtext, summary, function(data) {
          $('#closeAFD-relist-old-status').css('color', 'darkgreen').text("Old log updated");
        }, 'edit');
        CloseAFD.edit('', mw.config.get('wgPageName'), afdtext, summary, function(data) {
          $('#closeAFD-relist-afd-status').css('color', 'darkgreen').text("AfD page edited");
        }, 'edit');
      })
      .fail( function (error) {
        $('#closeAFD-relist-old-status').text('');
        $('#closeAFD-relist-afd-status').css('color', 'red').text("Error: "+error);
      });
    };

    // This gets called when you click the Close button
    CloseAFD.startCloseAction = function() {
      $( this ).dialog( "close" );
      $( "#closeAFD-closestatus-dialog" ).show().dialog( { title: "Closing "+mw.config.get('wgTitle'),
        width:400,
        close: function( event, ui ) { CloseAFD.reset(); }
      } );
      var selected = $('input[name="closeAFD-action"]:checked');
      if (typeof $(selected).val() === "undefined") {
        $( "#closeAFD-closestatus-dialog" ).dialog( "close" );
        return false;
      }
      var patt = /closeAFD-([a-z]+)/;
      CloseAFD.action = patt.exec($(selected).attr('id'))[1];
      CloseAFD.speedy = $('#closeAFD-speedy').prop('checked');
      // Get wikitext of AFD page
      $('#closeAFD-AFDpage').text("Fetching AFD page...");
      var params = {
        'action':'query',
        'prop':'revisions',
        'titles':mw.config.get('wgPageName'),
        'rvprop':'timestamp|content'
      };
      CloseAFD.api.get(params)
      .fail( function (error) {
        $('#closeAFD-error').text("Error: "+error);
      })
      .done( function (data) {
        var pageid = mw.config.get('wgArticleId').toString();
        CloseAFD.wikitext = data.query.pages[pageid].revisions[0]['*'];
        // Check to make sure it's not already closed
        if (CloseAFD.wikitext.indexOf('xfd-closed') != -1) {
          $('#closeAFD-AFDpage').css('color', 'red').text("AFD is already closed!");
          return false;
        }
        // Get the articles involved
        var patt = /\{\{la\|([^\}]+)\}\}/gim;
        var res = patt.exec(CloseAFD.wikitext);
        while (res !== null) {
          CloseAFD.titles.push(res[1]);
          res = patt.exec(CloseAFD.wikitext);
        }
        // Can't find any templates? Use the subpage
        if (CloseAFD.titles.length === 0) {
          CloseAFD.titles.push(CloseAFD.debate.replace(/ \(\w+ nomination\)/, '')  );
        }
        // More than 1 title, verify we want them all
        if (CloseAFD.titles.length > 1) {
          CloseAFD.checkMultipleTitles();
        } else {
          CloseAFD.finishCloseAction();
        }
      });
    };

    // If we find multiple titles check that we want to apply the result to all of them
    CloseAFD.checkMultipleTitles = function() {
      $('#closeAFD-multititles').append($('<span>').css('color', '#ED4200')
      .html('Note: There appear to be multiple articles nominated. Uncheck any you do not want the closing action(s) to apply to, then click "Continue"<br />'));
      for (var i=0; i<CloseAFD.titles.length; i++) {
        var c = '<input type="checkbox" checked=checked id="closeAFD-title-'+i.toString()+'" />'+
        '<label id="closeAFD-tlabel-'+i.toString()+'" for="closeAFD-title-'+i.toString()+'"></label><br />';
        $('#closeAFD-multititles').append(c);
        var href = mw.config.get('wgArticlePath').replace('$1', CloseAFD.titles[i].replace(/_/g, ' '));
        $('#closeAFD-tlabel-'+i.toString()).append($('<a>').attr('href', href).text(CloseAFD.titles[i]));
      }
      $('#closeAFD-multititles').append($('<br></br>'));
      $('#closeAFD-multititles').append($('<a>').css('color', 'darkblue').css('font-weight','bold').text('Continue').attr('id', 'closeAFD-multititles-ready').attr('href','#'));
      $('#closeAFD-multititles').slideDown();
      $('#closeAFD-multititles-ready').on('click', function() {
        var num = 0;
        for (var i=0; i<CloseAFD.titles.length; i++)  {
          if (!$('#closeAFD-title-'+i.toString()).prop('checked') ) {
            CloseAFD.titles[i] = false;
          } else {
            num++;
          }
        }
        if (num === 0) {
          $( "#closeAFD-closestatus-dialog" ).dialog( "close" );
          return false;
        }
        $('#closeAFD-multititles').slideUp();
        CloseAFD.finishCloseAction();
        return false;
      });
    };

    // Now that we know what we want to do and where, actually do it
    CloseAFD.finishCloseAction = function() {
      // Assemble the closing statement
      var resultstr = "'''";
      var summary = '';
      if (CloseAFD.speedy) {
        resultstr += 'speedy ';
        summary += 'speedy ';
      }
      if (CloseAFD.action != 'other') {
        resultstr += CloseAFD.actionmap[CloseAFD.action];
        summary += CloseAFD.actionmap[CloseAFD.action];
        if (CloseAFD.action === 'merge' || CloseAFD.action === 'redir' || CloseAFD.action === 'delandred') {
          resultstr += '[['+ $('#closeAFD-target').val() + ']]';
          summary += '[['+ $('#closeAFD-target').val() + ']]';
        }
      } else {
        resultstr += $("#closeAFD-other-action").val();
        summary += $("#closeAFD-other-action").val();
      }
      var editsum = "Closing debate, result was "+summary;
      resultstr += "'''. ";
      resultstr += $('#closeAFD-rationale').val();
      if (CloseAFD.NAC) {
        resultstr += ' {'+'{subst:nac}}';
        editsum+= ' (non-admin closure)';
      }
      resultstr += ' ~~'+'~~\n';
      // Remove the template and add the closing templates
      var newtext = CloseAFD.wikitext.replace(/\{\{REMOVE THIS TEMPLATE WHEN CLOSING THIS AfD\|\w+\}\}/, '');
      newtext = '{'+'{subst:Afd top}}'+resultstr+newtext+'\n{{subst:Afd bottom}'+'}';
      $('#closeAFD-AFDpage').text("Saving "+wgTitle+'...');
      CloseAFD.actionsStarted.notdoneyet = 1;
      CloseAFD.edit(mw.config.get('wgArticleId').toString(), '', newtext, editsum, function (data) {
        $('#closeAFD-AFDpage').css('color', 'darkgreen').text(wgTitle+" closed.");
      }, 'afdpage');
      // Get the nomination date
      var params = {
        'action':'query',
        'prop':'revisions',
        'pageids':mw.config.get('wgArticleId'),
        'rvprop':'timestamp',
        'rvdir':'newer',
        'rvlimit':'1'
      };
      CloseAFD.api.get(params)
      .fail( function (error) {
        $('#closeAFD-error').text("Error: "+error);
      })
      .done( function (result) {
        timestamp = result.query.pages[mw.config.get('wgArticleId').toString()].revisions[0].timestamp;
        var d = new Date(timestamp);
        var nomdate = d.getUTCDate().toString()+' '+CloseAFD.months[d.getUTCMonth()]+' '+d.getUTCFullYear().toString();
        var d2 = new Date();
        var curdate = d2.getUTCDate().toString()+' '+CloseAFD.months[d2.getUTCMonth()]+' '+d2.getUTCFullYear().toString();
        // Set edit summary
        var editsum =  '[['+wgPageName.replace(/_/g, ' ')+']] closed as '+summary;
        // Figure out which pages/talk pages exist
        if (CloseAFD.action === 'delete' || CloseAFD.action === 'delandred') {
          $('#closeAFD-action2').text('Finding page(s) to delete...');
        } else {
          $('#closeAFD-action1').text('Finding page(s) to edit...');
        }
        var tlist = '';
        for (var i=0; i<CloseAFD.titles.length; i++) {
          if (CloseAFD.titles[i]) {
            p = CloseAFD.titles[i];
            t = 'Talk:'+CloseAFD.titles[i];
            tlist+=p+'|'+t;
            if (i != (CloseAFD.titles.length-1)) {
              tlist+='|';
            }
          }
        }
        params = {
          'action':'query',
          'titles':tlist,
          'indexpageids': '1',
          'prop':'revisions',
          'rvprop':'content'
        };
        // don't redirect target of redirect to itself
        // if(CloseAFD.action !== 'redir') {
        //   params.redirect = 1;
        // }
        CloseAFD.api.get(params)
        .fail( function (error) {
          $('#closeAFD-error').text("Error: "+error);
        })
        .done( function (data) {
          if (CloseAFD.action === 'delete' || CloseAFD.action === 'delandred') {
            $('#closeAFD-action2').text('Deleting page(s)...');
          } else {
            $('#closeAFD-action1').text('Editing page(s)...');
            $('#closeAFD-action3').text('Tagging talk page(s)...');
          }
        // Now figure out what to do with the article
          switch(CloseAFD.action) {
          case 'delete':
            // Delete the pages that exist
            for (var i=0; i<data.query.pageids.length; i++) {
              id = parseInt(data.query.pageids[i]);
              if (id > 0) {
                CloseAFD.deletePage(id, '', editsum, function() {
                  if (CloseAFD.actionsStarted.delete == CloseAFD.actionsFinished.delete) {
                    $('#closeAFD-action2').css('color', 'darkgreen').text("Page(s) deleted");
                  }
                }, 'delete');
              }
            }
            break;
          case 'merge':
          case 'keep':
          case 'nocon':
          case 'other':
            var mtitles = [];
            for (var i=0; i<data.query.pageids.length; i++) {
              var id = parseInt(data.query.pageids[i]);
              var p = data.query.pages[id.toString()];
              if (p.ns === 1) {
                var wikitext = '';
                if (parseInt(id) > 0) {
                  wikitext = p.revisions[0]['*'];
                }
                CloseAFD.oldafd(p.title, editsum, summary, wikitext, nomdate);
              } else if (p.ns === 0 && id > 0) {
                var newtext = p.revisions[0]['*'].replace(/<!-- Please(.|\n)+this\ point -->\s*/m, '');
                if (CloseAFD.action === 'merge') {
                  var target = $('#closeAFD-target').val()
                  newtext = '{'+'{Afd-merge to|'+target+'|'+CloseAFD.debate+'|'+curdate+'}}\n' + newtext
                  CloseAFD.edit(id, '', newtext, editsum, function(data) {
                    if (CloseAFD.actionsStarted.edit == CloseAFD.actionsFinished.edit) {
                      $('#closeAFD-action1').css('color', 'darkgreen').text("Merge tags added");
                    }
                  }, 'edit')
                  mtitles.push(p.title)
                } else {
                  CloseAFD.edit(id, '', newtext, editsum, function(data) {
                    if (CloseAFD.actionsStarted.edit == CloseAFD.actionsFinished.edit) {
                      $('#closeAFD-action1').css('color', 'darkgreen').text("AFD tag(s) removed");
                    }
                  }, 'edit')
                }
              }
            }
            if (CloseAFD.action === 'merge') {
              CloseAFD.mergefrom(mtitles, editsum, curdate)
            }
            break;
          case 'delandred':
            // Delete the non-talk pages
            for (var i=0; i<data.query.pageids.length; i++) {
              var id = parseInt(data.query.pageids[i]);
              var p = data.query.pages[id.toString()];
              if (p.ns === 0 && id > 0) {
                CloseAFD.deletePage(id, '', editsum, function(data) {
                  var target = $('#closeAFD-target').val();
                  var templatedate = CloseAFD.months[d2.getUTCMonth()]+' '+d2.getUTCFullYear().toString();
                  var text = '#REDIRECT [['+target+']]\n{'+'{uncategorized|date='+templatedate+'}}'
                  var title = data.delete.title;
                  CloseAFD.edit('', title, text, editsum, function(d) {
                    if (CloseAFD.actionsStarted.edit == CloseAFD.actionsFinished.edit) {
                      $('#closeAFD-action1').css('color', 'darkgreen').text("Page(s) redirected");
                    }
                  }, 'edit')
                  if (CloseAFD.actionsStarted.delete == CloseAFD.actionsFinished.delete) {
                    $('#closeAFD-action2').css('color', 'darkgreen').text("Page(s) deleted");
                  }
                }, 'delete');
              } else if (p.ns === 1) {
                var wikitext = ''
                if (p > 0) {
                  wikitext = p.revisions[0]['*'];
                }
                CloseAFD.oldafd(p.title, editsum, summary, wikitext, nomdate);
              }
            }
            break;
          case 'redir':
            var target = $('#closeAFD-target').val();
            var templatedate = CloseAFD.months[d2.getUTCMonth()]+' '+d2.getUTCFullYear().toString();
            var text = '#REDIRECT [['+target+']]\n{'+'{uncategorized|date='+templatedate+'}}'
            for (var i=0; i<data.query.pageids.length; i++) {
              var id = parseInt(data.query.pageids[i]);
              var p = data.query.pages[id.toString()];
              if (p.ns === 1) {
                var wikitext = ''
                if (id > 0) {
                  wikitext = p.revisions[0]['*'];
                }
                CloseAFD.oldafd(p.title, editsum, summary, wikitext, nomdate);
              } else if (p.ns === 0) {
                CloseAFD.edit('', p.title, text, editsum, function(data) {
                  if (CloseAFD.actionsStarted.edit == CloseAFD.actionsFinished.edit) {
                    $('#closeAFD-action1').css('color', 'darkgreen').text("Page(s) redirected");
                  }
                }, 'edit')
              }
            }
            break;
          }
          // Delete redirects
          if ($('#closeAFD-delredir').prop('checked')) {
            $('#closeAFD-action4').text('Finding redirect(s) to delete...');
            for (var i=0; i<data.query.pageids.length; i++) {
              id = data.query.pageids[i];
              t = data.query.pages[id].title
              var total = 0
              params = {
                'action':'query',
                'prop':'redirects',
                'titles':t,
                'rdlimit':'100',
                'indexpageids':'1'
              }
              CloseAFD.api.get(params)
              .fail( function (error) {
                $('#closeAFD-error').css('color', 'red').text("Error: "+error);
              })
              .done(function(res) {
                var pid = res.query.pageids[0];
                if (res.query.pages[pid].redirects) {
                  var redirs = res.query.pages[pid].redirects
                  for (var j=0; j<redirs.length; j++) {
                    var pageid = redirs[j].pageid;
                    CloseAFD.deletePage(pageid, '', editsum, function() {
                      if (CloseAFD.actionsStarted.redirects == CloseAFD.actionsFinished.redirects) {
                        $('#closeAFD-action4').css('color', 'darkgreen').text("Redirect(s) deleted");
                      }
                    }, 'redirects');
                  }
                  total+=redirs.length;
                }
                if (total === 0 && i == data.query.pageids.length) {
                  $('#closeAFD-action4').css('color', 'darkgreen').text("No redirects to delete");
                }
              })
            }
            CloseAFD.actionsFinished.notdoneyet = 1;
            CloseAFD.checkDone();
          } else {
            CloseAFD.actionsFinished.notdoneyet = 1;
            CloseAFD.checkDone();
          }
        });
      });
    }

    // Add the Afd-merge-from template to the talk page of the target article
    CloseAFD.mergefrom = function(fromtitles, summary, date) {
      var target = $('#closeAFD-target').val()
      t = 'Talk:'+target;
      var prepend = '';
      for (var i=0; i<fromtitles.length; i++) {
        prepend+= '{'+'{Afd-merge from|'+fromtitles[i]+'|'+CloseAFD.debate+'|'+date+'}}\n'
      }
      CloseAFD.actionsStarted.edittalk++;
      editparams = {'action':'edit',
        'title': t,
        'summary': summary,
        'prependtext': prepend,
        'token': CloseAFD.token
      };
      CloseAFD.api.post(editparams)
      .fail( function (error) {
        $('#closeAFD-error').css('color', 'red').text("Error: "+error);
        CloseAFD.actionsFinished.edittalk++;
        CloseAFD.checkDone();
      })
      .done(function() {
        CloseAFD.actionsFinished.edittalk++;
        if (CloseAFD.actionsStarted.edittalk == CloseAFD.actionsFinished.edittalk) {
          $('#closeAFD-action1').css('color', 'darkgreen').text("Merge tags added");
        }
        CloseAFD.checkDone();
      })
    }

    // Add the oldafdmulti template to the talk page
    CloseAFD.oldafd = function(title, summary, action, wikitext, date) {
      var oldafdmulti = '{{Old AfD multi';
      // Try to find old AFD template
      var patt = /\n?\{\{(old|afd) ?(old|afd) ?(multi|full)?((.|\n)*?)\}\}\n?/i;
      old = patt.exec(wikitext);
      var count = 0;
      if (old) {
        count = 1;
        var parts = old[4].split('|');
        var params = {};
        var numtest = /[A-z]+([0-9])/i;
        for (var i=0; i<parts.length; i++) {
          if (!parts[i].trim()) {
            continue;
          }
          pieces = parts[i].split('=');
          params[pieces[0].trim()] = pieces[1].trim();
          var num = numtest.exec(pieces[0].trim());
          if (num) {
            res = parseInt(num[1]);
            if (res > count) {
              count = res;
            }
          }
        }
        for (var p in params) {
          oldafdmulti += ' | '+p+' = '+params[p];
        }
      }
      count++;
      c = count.toString();
      if (count === 1) {
        c = '';
      }
      oldafdmulti += ' | date'+c+' = '+date+' | result'+c+" = '''"+action+"''' | page"+c+' = '+CloseAFD.debate;
      oldafdmulti += '}}';
      if (old) {
        newtext = wikitext.replace(patt, '\n'+oldafdmulti+'\n');
      } else { // [[Wikipedia:Talk page layout]] is too complicated to automate, so I'm not even going to try
        newtext = oldafdmulti+'\n'+wikitext;
      }
      CloseAFD.edit('', title, newtext, summary, function(data) {
        if (CloseAFD.actionsStarted.edittalk == CloseAFD.actionsFinished.edittalk) {
          $('#closeAFD-action3').css('color', 'darkgreen').text("Talk page(s) tagged");
        }
      }, 'edittalk');
    };

    $(document).on('click', '#ca-closeAFD', CloseAFD.close);
    $(document).on('click', '#ca-relistAFD', CloseAFD.relist);

    CloseAFD.reset = function() {
      $('#closeAFD-AFDpage').empty();
      $('#closeAFD-multititles').empty();
      $('#closeAFD-action1').empty();
      $('#closeAFD-action2').empty();
      $('#closeAFD-action3').empty();
      $('#closeAFD-action4').empty();
      $('#closeAFD-action5').empty();
      $('#closeAFD-error').empty();
      CloseAFD.titles = [];
    };
    // HTML for closing dialog
    $('#bodyContent').before('<div id="closeAFD-dialog" style="width:400px;display:none;">'+
      '<div style="padding-bottom:2%;"><input type="checkbox" name="closeAFD-speedy" id="closeAFD-speedy" /><label for="closeAFD-speedy">Speedy:</label></div>'+
      '<div style="float:left; width:150px; padding-bottom:1%;">'+
      '<span class="closeAFD-nac"><input type="radio" name="closeAFD-action" id="closeAFD-delete" /><label for="closeAFD-delete">Delete</label><br /></span>'+
      '<input type="radio" name="closeAFD-action" id="closeAFD-keep" /><label for="closeAFD-keep">Keep</label><br />'+
      '<input type="radio" name="closeAFD-action" id="closeAFD-nocon" /><label for="closeAFD-nocon">No consensus</label><br />'+
      '<input type="radio" name="closeAFD-action" id="closeAFD-merge" /><label for="closeAFD-merge">Merge</label><br />'+
      '</div><div style="float:left; width:210px; padding-bottom:1%;">'+
      '<input type="radio" name="closeAFD-action" id="closeAFD-redir" /><label for="closeAFD-redir">Redirect</label><br />'+
      '<span class="closeAFD-nac"><input type="radio" name="closeAFD-action" id="closeAFD-delandred" /><label for="closeAFD-delandred">Delete & redirect</label><br /></span>'+
      '<input type="radio" name="closeAFD-action" id="closeAFD-other" /><label for="closeAFD-other">Other:</label>'+
      '<input type="text" size=15 disabled=disabled id="closeAFD-other-action" /><br />'+
      '</div><div style="clear:both;padding-bottom:1%" id="closeAFD-delrediropt">'+
      '<input type="checkbox" name="closeAFD-delredir" id="closeAFD-delredir" /><label for="closeAFD-delredir">Delete redirects</label></div>'+
      '<div style="clear:both;padding-bottom:1%" id="closeAFD-targetopt">'+
      '<label for="closeAFD-delredir">Target page:</label><input type="text" name="closeAFD-target" id="closeAFD-target" /></div>'+
      '<div style="clear:both;">Additional closing rationale (optional):</div>'+
      '<textarea id="closeAFD-rationale" rows=4 cols=100></textarea>'+
    '</div>');
    $('#closeAFD-delrediropt').hide();
    $('#closeAFD-targetopt').hide();

    // HTML for closing status dialog
    $('#bodyContent').before('<div id="closeAFD-closestatus-dialog" style="width:400px;display:none;">'+
    '<div id="closeAFD-AFDpage" style="width:380px;"></div>'+
    '<div id="closeAFD-multititles" style="width:380px;display:none;"></div>'+
    '<div id="closeAFD-action2" style="width:380px;"></div>'+
    '<div id="closeAFD-action1" style="width:380px;"></div>'+
    '<div id="closeAFD-action3" style="width:380px;"></div>'+
    '<div id="closeAFD-action4" style="width:380px;"></div>'+
    '<div id="closeAFD-action5" style="width:380px;"></div>'+
    '<div id="closeAFD-error" style="width:380px;color:red;"></div>'+
    '</div>');

    // HTML for relist dialog
    $('#bodyContent').before('<div id="closeAFD-relist-dialog" style="width:600px;display:none;">'+
    '<div id="closeAFD-relist-logs"></div>'+
    '<div><ul><li><label for="closeAFD-relist-comment">Optional comments:&nbsp;</label>'+
    '<input style="width:70%" type="text" id="closeAFD-relist-comment"></input></li>'+
    '<li><label for="closeAFD-relist-number">Relist number:&nbsp;</label>'+
    '<input size="2" maxlength="1" type="text" id="closeAFD-relist-number"></input></li></ul></div>'+
    '<div id="closeAFD-relist-afd-status"></div>'+
    '<div id="closeAFD-relist-old-status"></div>'+
    '<div id="closeAFD-relist-new-status"></div>'+
    '<div id="closeAFD-relist-error"></div>'+
    '<div id="closeAFD-relist-done"></div>'+
    '</div>');

    // Hide deletion options from non-admins
    if ( CloseAFD.NAC ) {
      $(".closeAFD-nac").hide();
    }

    // Adjust visibility of delete redirects and target page options based on what close option is selected
    $('input[name="closeAFD-action"]').change(function() {
      if ((!CloseAFD.NAC) && ($('#closeAFD-delete').prop('checked') || $('#closeAFD-delandred').prop('checked') || $('#closeAFD-other').prop('checked'))) {
        $('#closeAFD-delrediropt').slideDown();
        if ($('#closeAFD-delete').prop('checked') ) {
          $('#closeAFD-delredir').prop('checked', true);
        } else {
          $('#closeAFD-delredir').prop('checked', false);
        }
      } else {
        $('#closeAFD-delredir').prop('checked', false);
        $('#closeAFD-delrediropt').slideUp();
      }
      if ($('#closeAFD-other').prop('checked') ) {
        $('#closeAFD-other-action').prop('disabled', false);
      } else {
        $('#closeAFD-other-action').prop('disabled', true);
      }
      if ($('#closeAFD-merge').prop('checked') || $('#closeAFD-delandred').prop('checked') || $('#closeAFD-redir').prop('checked')) {
        $('#closeAFD-targetopt').slideDown();
      } else {
        $('#closeAFD-targetopt').slideUp();
      }
    });
  }

  RegExp.quote = function(str) {
    return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
  };
});