Эта статья входит в число хороших статей

Участник:Claymore/goodarticles.js

Материал из Википедии — свободной энциклопедии
if( typeof( wfAnchorEncode ) != "function" ) {
  function wfAnchorEncode(str) {
    str = encodeURIComponent( str );
    str = str.replace( /%/g, "." );
    str = str.replace( /\.20/g, "_" );
    str = str.replace( /\.3A/g, ":" );
    return str;
  }
}
 
function sortDays( a, b ) { return b.day - a.day; }
function ch( o ) { for( var i in o ) { return o[i] } }
 
function processNominationLog ( pgtext, message ) {
  var strings = pgtext.split('\n');
  var result = '';
  var end = 0;
  var start = 0;
  for( var i = 0; i < strings.length; ++i ) {
    var line = strings[i];
    if( line == '<!-- Последние записи журнала, показываемые на основной странице-->' ) {
      start = i;
    }
    if( line == '<!-- Конец Последние записи журнала, показываемые на основной странице-->' ) {
      end = i;
      break;
    }
  }
 
  for( var i = 0; i < strings.length; ++i ) {
    if( i == start + 1 ) {
      result += message;
    }
    if( i <= start ) {
      result += strings[i] + '\n';
    }
    else if( i > start && i < end - 1 ) {
      result += strings[i] + '\n';
    }
    else if( i == end - 1 ) {
      result += strings[end] + '\n';
      result += strings[end + 1] + '\n';
      result += strings[end + 2] + '\n';
      result += strings[end + 3] + '\n';
      result += strings[i] + '\n';
      i = end + 3;
    }
    else if( i > end ) {
      result += strings[i] + '\n';
    }
  }
  return result;
}
 
var wgArchivePage = {
  'rq' : null,
  'newtext' : '',
  'tabletext' : '',
  'editsummary' : '',
  'edittoken' : '',
  'sections' : new Array(),
  'archive' : '',
  'year' : '',
  'month' : '',
  'day' : '',
  'months' : {
    'января' : 'Январь',
    'февраля' : 'Февраль',
    'марта' : 'Март',
    'апреля' : 'Апрель',
    'мая' : 'Май',
    'июня' : 'Июнь',
    'июля' : 'Июль',
    'августа' : 'Август',
    'сентября' : 'Сентябрь',
    'октября' : 'Октябрь',
    'ноября' : 'Ноябрь',
    'декабря' : 'Декабрь'
  },
 
  'addArchiveTab' : function() {
    if( !document.getElementById( 'good_article_nominations_navigation' ) ) return;
 
 	mw.loader.using('mediawiki.util').done( function() {
    	mw.util.addPortletLink( 'p-cactions', 'javascript:wgArchivePage.archivePage()',
                    'Архивировать', 'ca-archive', 'Перенести страницу в архив' );
 	} );
  },
 
  'onTextDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    var dateRegex = /(\d{1,2}) ([а-я]+) (\d{4})$/;
    var match = mw.config.get('wgPageName').match( dateRegex );
    wgArchivePage.year = match[3];
    wgArchivePage.month = match[2];
    wgArchivePage.day = match[1];
    wgArchivePage.archive = 'Википедия:Кандидаты в хорошие статьи/Архив/' + wgArchivePage.months[match[2]] + ' ' + match[3];
    var sectionRegex = /^==\s*(<s>)*\s*\[{2}(.*)\]{2}\s*(<\/s>)*\s*==/;
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    var navtemplateRegex = /\{{2}Кандидаты в хорошие статьи - Навигация\}{2}/i;
    wgArchivePage.text = pgtext.replace( navtemplateRegex, '{{closed}}' );
    var strings = pgtext.split('\n');
 
    var index = 0;
    for( var i = 0; i < strings.length; ++i ) {
      var line = strings[i];
      var matches = line.match( sectionRegex );
      if( matches ) {
        wgArchivePage.sections[index] = new Array();
        wgArchivePage.sections[index]['name'] = matches[2];
        wgArchivePage.sections[index]['rejected'] = matches[1] ? true : false
        ++index;
      }
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( wgArchivePage.archive );
    wgArchivePage.rq.onreadystatechange = wgArchivePage.onArchiveDownload;
    wgArchivePage.rq.open( "GET", uri, true );
    wgArchivePage.rq.send( "" );
  },
 
  'onArchiveDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 404 && this.status != 200 ) return;
 
    var dateRegex = /(\d{1,2}) ([а-я]+) (\d{4})/;
    var days = new Array();
    var index = 0;
    if( this.status == 200) {
      result = eval('('+this.responseText+')');
      var pgtext = ch(result.query.pages).revisions[0]['*'];
      var strings = pgtext.split('\n');
 
      for( var i = 0; i < strings.length; ++i ) {
        var line = strings[i];
        if( line == '|-' ) {
          days[index] = new Array();
          days[index]['day'] = strings[i + 1].match( dateRegex )[1];
          days[index]['nominations'] = strings[i + 2].substring(1);
          ++index;
          i += 2;
        }
      }
    }
 
    var nominations = '';
    for( var i = 0; i < wgArchivePage.sections.length; ++i ) {
      nominations += ', ';
      if(  wgArchivePage.sections[i].rejected ) nominations += '<s>[[' + wgArchivePage.sections[i].name + ']]</s>';
      else nominations += '\'\'[[' + wgArchivePage.sections[i].name + ']]\'\'';
    }
 
    days[index] = new Array();
    days[index]['day'] = wgArchivePage.day;
    days[index]['nominations'] = nominations.substring( 2 );
    days.sort( sortDays );
    wgArchivePage.newtext = '{{Навигация по архиву КХС}}\n{| class="standard"\n! Дата&nbsp;номинации\n! Обсуждавшиеся&nbsp;статьи';
    for( var i = 0; i < days.length; ++i ) {
      wgArchivePage.newtext += '\n|-';
      wgArchivePage.newtext += '\n|[[Википедия:Кандидаты в хорошие статьи/' +
                               days[i].day + ' ' + wgArchivePage.month + ' ' +
                               wgArchivePage.year + '|' + days[i].day + ' ' + wgArchivePage.month + ']]';
      wgArchivePage.newtext += '\n|' + days[i].nominations;
    }
    wgArchivePage.newtext += '\n|}';
    wgArchivePage.editsummary = "архивация [[" + mw.config.get('wgPageName') + "]]";
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=info&intoken=edit&titles=' + encodeURIComponent( wgArchivePage.archive );
    wgArchivePage.rq.onreadystatechange = wgArchivePage.onTokenDownload;
    wgArchivePage.rq.open( "GET", uri, true );
    wgArchivePage.rq.send( "" );
  },
 
  'onTokenDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    wgArchivePage.edittoken = ch(result.query.pages).edittoken;
    if( !wgArchivePage.edittoken ) {
      alert( 'Редактирование невозможно' );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( wgArchivePage.archive );
    var rq = "summary=" + encodeURIComponent( wgArchivePage.editsummary ) +
             "&text=" + encodeURIComponent( wgArchivePage.newtext ) +
             "&token=" + encodeURIComponent( wgArchivePage.edittoken );
 
    wgArchivePage.rq.onreadystatechange = wgArchivePage.onEditSaved;
    wgArchivePage.rq.open( "POST", uri, true );
    wgArchivePage.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgArchivePage.rq.send( rq );
  },
 
  'onEditSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgArchivePage.editsummary ) +
             "&text=" + encodeURIComponent( wgArchivePage.text ) +
             "&token=" + encodeURIComponent( wgArchivePage.edittoken );
 
    wgArchivePage.rq.onreadystatechange = wgArchivePage.onNominationSaved;
    wgArchivePage.rq.open( "POST", uri, true );
    wgArchivePage.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgArchivePage.rq.send( rq );
  },
 
  'onNominationSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( 'Википедия:Кандидаты в хорошие статьи' );
    wgArchivePage.rq.onreadystatechange = wgArchivePage.onTableDownload;
    wgArchivePage.rq.open( "GET", uri, true );
    wgArchivePage.rq.send( "" );
  },
 
  'onTableDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    var strings = pgtext.split( '\n' );
    for( var i = 0; i < strings.length; ++i ) {
      var line = strings[i];
      if( line == '|-' && strings[i + 1].indexOf( mw.config.get('wgPageName') ) != -1 ) {
        i += 2;
      }
      else {
        wgArchivePage.tabletext += line + '\n';
      }
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( 'Википедия:Кандидаты в хорошие статьи' );
    var rq = "summary=" + encodeURIComponent( wgArchivePage.editsummary ) +
             "&section=1" +
             "&text=" + encodeURIComponent( wgArchivePage.tabletext ) +
             "&token=" + encodeURIComponent( wgArchivePage.edittoken );
 
    wgArchivePage.rq.onreadystatechange = wgArchivePage.onTableSaved;
    wgArchivePage.rq.open( "POST", uri, true );
    wgArchivePage.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgArchivePage.rq.send( rq );
  },
 
  'onTableSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    jsMsg( 'Страница была успешно заархивирована. <a href="javascript:location.reload()">Перезагрузить страницу</a>' );
  },
 
  'archivePage' : function() {
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    this.rq = sajax_init_object();
    this.rq.onreadystatechange = this.onTextDownload;
    this.rq.open( "GET", uri, true );
    this.rq.send( "" );
  }
}
 
var wgMakeGood = {
  'rq' : null,
  'newtext' : '',
  'talktext' : '',
  'editsummary' : '',
  'edittoken' : '',
  'date' : '',
  'logtext' : '',
 
  'addMakeGoodTab' : function() {
    if( !document.getElementById( 'good-candidate' ) ) return;
 
    mw.loader.using('mediawiki.util').done( function() {
    	mw.util.addPortletLink( 'p-cactions', 'javascript:wgMakeGood.makeGood()',
                    'Избрать', 'ca-make-good', 'Сделать статью хорошей' );
    } );
  },
 
  'onTextDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    var nominationRegex = /\{{2}Кандидат в хорошие статьи\|(\d{1,2}) ([а-я]+) (\d{4})\}{2}/i;
    var match = pgtext.match( nominationRegex );
    wgMakeGood.date = match[1] + ' ' + match[2] + ' ' + match[3];
    wgMakeGood.newtext = pgtext.replace( nominationRegex, '{{Хорошая статья}}' );
    var today = '{{' + 'subst:CURRENTDAY}} {{' + 'subst:CURRENTMONTHNAMEGEN}} {{' + 'subst:CURRENTYEAR}}';
    wgMakeGood.talktext = '{{Сообщение ХС|' + wgMakeGood.date + '|' + today + '}}\n';
    wgMakeGood.editsummary = "статья [[" + mw.config.get('wgPageName') + "]] избрана хорошей";
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=info&intoken=edit&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    wgMakeGood.rq.onreadystatechange = wgMakeGood.onTokenDownload;
    wgMakeGood.rq.open( "GET", uri, true );
    wgMakeGood.rq.send( "" );
  },
 
  'onTokenDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    wgMakeGood.edittoken = ch(result.query.pages).edittoken;
    if( !wgMakeGood.edittoken ) {
      alert( 'Редактирование невозможно' );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgMakeGood.editsummary ) +
             "&text=" + encodeURIComponent( wgMakeGood.newtext ) +
             "&token=" + encodeURIComponent( wgMakeGood.edittoken );
 
    wgMakeGood.rq.onreadystatechange = wgMakeGood.onEditSaved;
    wgMakeGood.rq.open( "POST", uri, true );
    wgMakeGood.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgMakeGood.rq.send( rq );
  },
 
  'onEditSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( 'Обсуждение:' + mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgMakeGood.editsummary ) +
             "&prependtext=" + encodeURIComponent( wgMakeGood.talktext ) +
             "&token=" + encodeURIComponent( wgMakeGood.edittoken );
 
    wgMakeGood.rq.onreadystatechange = wgMakeGood.onNominationSaved;
    wgMakeGood.rq.open( "POST", uri, true );
    wgMakeGood.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgMakeGood.rq.send( rq );
  },
 
  'onNominationSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' +
              encodeURIComponent( 'Википедия:Кандидаты_в_хорошие_статьи/Журнал_избраний' );
    wgMakeGood.rq.onreadystatechange = wgMakeGood.onLogDownload;
    wgMakeGood.rq.open( "GET", uri, true );
    wgMakeGood.rq.send( "" );
  },
 
  'onLogDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    wgMakeGood.logtext = processNominationLog( pgtext, '* Статье [[' + mw.config.get('wgPageName') + ']] присвоен статус хорошей. ~~' + '~~\n' );
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' +
              encodeURIComponent( 'Википедия:Кандидаты_в_хорошие_статьи/Журнал_избраний' );
    var rq = "summary=" + encodeURIComponent( wgMakeGood.editsummary ) +
             "&text=" + encodeURIComponent( wgMakeGood.logtext ) +
             "&token=" + encodeURIComponent( wgMakeGood.edittoken );
 
    wgMakeGood.rq.onreadystatechange = wgMakeGood.onLogSaved;
    wgMakeGood.rq.open( "POST", uri, true );
    wgMakeGood.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgMakeGood.rq.send( rq );
  },
 
  'onLogSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    jsMsg( 'Статья успешно избрана хорошей. <a href="javascript:location.reload()">Перезагрузить страницу</a>' );
  },
 
  'makeGood' : function() {
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    this.rq = sajax_init_object();
    this.rq.onreadystatechange = this.onTextDownload;
    this.rq.open( "GET", uri, true );
    this.rq.send( "" );
  }
}
 
var wgDeny = {
  'rq' : null,
  'newtext' : '',
  'talktext' : '',
  'editsummary' : '',
  'edittoken' : '',
  'date' : '',
  'logtext' : '',
 
  'addDenyTab' : function() {
    if( !document.getElementById( 'good-candidate' ) ) return;
 
    mw.loader.using('mediawiki.util').done( function() {
    	mw.util.addPortletLink( 'p-cactions', 'javascript:wgDeny.deny()',
                    'Отказать', 'ca-deny', 'Отказать в статусе хорошей' );
    } );
  },
 
  'onTextDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    nominationRegex = /\{{2}Кандидат в хорошие статьи\|(\d{1,2}) ([а-я]+) (\d{4})\}{2}/i;
    var match = pgtext.match( nominationRegex );
    wgDeny.date = match[1] + ' ' + match[2] + ' ' + match[3];
    wgDeny.newtext = pgtext.replace( nominationRegex, '' );
   var today = '{{' + 'subst:CURRENTDAY}} {{' + 'subst:CURRENTMONTHNAMEGEN}} {{' + 'subst:CURRENTYEAR}}';
    wgDeny.talktext = '{{Сообщение ХС|' + wgDeny.date + '|' + today + '|Кандидат}}\n';
    wgDeny.editsummary = "статья [[" + mw.config.get('wgPageName') + "]] отправлена на доработку";
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=info&intoken=edit&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    wgDeny.rq.onreadystatechange = wgDeny.onTokenDownload;
    wgDeny.rq.open( "GET", uri, true );
    wgDeny.rq.send( "" );
  },
 
  'onTokenDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    wgDeny.edittoken = ch(result.query.pages).edittoken;
    if( !wgDeny.edittoken ) {
      alert( 'Редактирование невозможно' );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgDeny.editsummary ) +
             "&text=" + encodeURIComponent( wgDeny.newtext ) +
             "&token=" + encodeURIComponent( wgDeny.edittoken );
 
    wgDeny.rq.onreadystatechange = wgDeny.onEditSaved;
    wgDeny.rq.open( "POST", uri, true );
    wgDeny.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgDeny.rq.send( rq );
  },
 
  'onEditSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( 'Обсуждение:' + mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgDeny.editsummary ) +
             "&prependtext=" + encodeURIComponent( wgDeny.talktext ) +
             "&token=" + encodeURIComponent( wgDeny.edittoken );
 
    wgDeny.rq.onreadystatechange = wgDeny.onNominationSaved;
    wgDeny.rq.open( "POST", uri, true );
    wgDeny.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgDeny.rq.send( rq );
  },
 
  'onNominationSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' +
              encodeURIComponent( 'Википедия:Кандидаты_в_хорошие_статьи/Журнал_избраний' );
    wgDeny.rq.onreadystatechange = wgDeny.onLogDownload;
    wgDeny.rq.open( "GET", uri, true );
    wgDeny.rq.send( "" );
  },
 
  'onLogDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    wgDeny.logtext = processNominationLog( pgtext, '* Статья [[' + mw.config.get('wgPageName') + ']] отправлена на доработку. ~~' + '~~\n' );
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' +
              encodeURIComponent( 'Википедия:Кандидаты_в_хорошие_статьи/Журнал_избраний' );
    var rq = "summary=" + encodeURIComponent( wgDeny.editsummary ) +
             "&text=" + encodeURIComponent( wgDeny.logtext ) +
             "&token=" + encodeURIComponent( wgDeny.edittoken );
 
    wgDeny.rq.onreadystatechange = wgDeny.onLogSaved;
    wgDeny.rq.open( "POST", uri, true );
    wgDeny.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgDeny.rq.send( rq );
  },
 
  'onLogSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    jsMsg( 'Статья успешно отравлена на доработку. <a href="javascript:location.reload()">Перезагрузить страницу</a>' );
  },
 
  'deny' : function() {
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    this.rq = sajax_init_object();
    this.rq.onreadystatechange = this.onTextDownload;
    this.rq.open( "GET", uri, true );
    this.rq.send( "" );
  }
}
 
var wgRevoke = {
  'rq' : null,
  'newtext' : '',
  'talktext' : '',
  'editsummary' : '',
  'edittoken' : '',
  'date' : '',
  'logtext' : '',
  'tabletext' : '',
 
  'addRevokeTab' : function() {
    if( !document.getElementById( 'revoke-good' ) ) return;
 
    mw.loader.using('mediawiki.util').done( function() {
    	mw.util.addPortletLink( 'p-cactions', 'javascript:wgRevoke.revoke()',
                    'Лишить', 'ca-revoke', 'Лишить статуса хорошей' );
    } );
  },
 
  'onTextDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    nominationRegex = /\{{2}К лишению статуса хорошей\|(\d{1,2}) ([а-я]+) (\d{4}).+\}{2}/i;
    var match = pgtext.match( nominationRegex );
    wgRevoke.date = match[1] + ' ' + match[2] + ' ' + match[3];
    wgRevoke.newtext = pgtext.replace( nominationRegex, '' );
    var today = '{{' + 'subst:CURRENTDAY}} {{' + 'subst:CURRENTMONTHNAMEGEN}} {{' + 'subst:CURRENTYEAR}}';
    wgRevoke.talktext = '{{Сообщение ХС|' + wgRevoke.date + '|' + today + '|Лишена}}\n';
    wgRevoke.editsummary = "статья [[" + mw.config.get('wgPageName') + "]] лишена статуса хорошей";
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=info&intoken=edit&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    wgRevoke.rq.onreadystatechange = wgRevoke.onTokenDownload;
    wgRevoke.rq.open( "GET", uri, true );
    wgRevoke.rq.send( "" );
  },
 
  'onTokenDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    wgRevoke.edittoken = ch(result.query.pages).edittoken;
    if( !wgRevoke.edittoken ) {
      alert( 'Редактирование невозможно' );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgRevoke.editsummary ) +
             "&text=" + encodeURIComponent( wgRevoke.newtext ) +
             "&token=" + encodeURIComponent( wgRevoke.edittoken );
 
    wgRevoke.rq.onreadystatechange = wgRevoke.onEditSaved;
    wgRevoke.rq.open( "POST", uri, true );
    wgRevoke.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgRevoke.rq.send( rq );
  },
 
  'onEditSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( 'Обсуждение:' + mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgRevoke.editsummary ) +
             "&prependtext=" + encodeURIComponent( wgRevoke.talktext ) +
             "&token=" + encodeURIComponent( wgRevoke.edittoken );
 
    wgRevoke.rq.onreadystatechange = wgRevoke.onNominationSaved;
    wgRevoke.rq.open( "POST", uri, true );
    wgRevoke.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgRevoke.rq.send( rq );
  },
 
  'onNominationSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' +
              encodeURIComponent( 'Википедия:Кандидаты_в_хорошие_статьи/Журнал_избраний' );
    wgRevoke.rq.onreadystatechange = wgRevoke.onLogDownload;
    wgRevoke.rq.open( "GET", uri, true );
    wgRevoke.rq.send( "" );
  },
 
  'onLogDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    wgRevoke.logtext = processNominationLog( pgtext, '* Статья [[' + mw.config.get('wgPageName') + ']] лишена статуса хорошей. ~~' + '~~\n' );
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' +
              encodeURIComponent( 'Википедия:Кандидаты_в_хорошие_статьи/Журнал_избраний' );
    var rq = "summary=" + encodeURIComponent( wgRevoke.editsummary ) +
             "&text=" + encodeURIComponent( wgRevoke.logtext ) +
             "&token=" + encodeURIComponent( wgRevoke.edittoken );
 
    wgRevoke.rq.onreadystatechange = wgRevoke.onLogSaved;
    wgRevoke.rq.open( "POST", uri, true );
    wgRevoke.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgRevoke.rq.send( rq );
  },
 
  'onLogSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' +
              encodeURIComponent( 'Википедия:Кандидаты в хорошие статьи/К лишению статуса/Список кандидатов' );
    wgRevoke.rq.onreadystatechange = wgRevoke.onTableDownload;
    wgRevoke.rq.open( "GET", uri, true );
    wgRevoke.rq.send( "" );
  },
 
  'onTableDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    var strings = pgtext.split('\n');
    for( var i = 0; i < strings.length; ++i ) {
      var line = strings[i];
      if( line == '|-' && strings[i + 2].indexOf( '[[' + mw.config.get('wgPageName') + ']]' ) != -1 ) {
        wgRevoke.tabletext += line + 'class=shadow\n' + strings[i + 1] + '\n' + strings[i + 2] + '\n' +
                                 strings[i + 3] + '\n' + strings[i + 4] + 'Лишена статуса\n';
        i += 4;
      }
      else {
        wgRevoke.tabletext += line + '\n';
      }
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( 'Википедия:Кандидаты в хорошие статьи/К лишению статуса/Список кандидатов' );
    var rq = "summary=" + encodeURIComponent( wgRevoke.editsummary ) +
             "&text=" + encodeURIComponent( wgRevoke.tabletext ) +
             "&token=" + encodeURIComponent( wgRevoke.edittoken );
 
    wgRevoke.rq.onreadystatechange = wgRevoke.onTableSaved;
    wgRevoke.rq.open( "POST", uri, true );
    wgRevoke.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgRevoke.rq.send( rq );
  },
 
  'onTableSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    jsMsg( 'Статья успешно лишена статуса хорошей. <a href="javascript:location.reload()">Перезагрузить страницу</a>' );
  },
 
  'revoke' : function() {
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    this.rq = sajax_init_object();
    this.rq.onreadystatechange = this.onTextDownload;
    this.rq.open( "GET", uri, true );
    this.rq.send( "" );
  }
}
 
var wgKeepStatus = {
  'rq' : null,
  'newtext' : '',
  'tabletext' : '',
  'editsummary' : '',
  'edittoken' : '',
  'date' : '',
 
  'addKeepStatusTab' : function() {
    if( !document.getElementById( 'revoke-good' ) ) return;
 
    mw.loader.using('mediawiki.util').done( function() {
    	mw.util.addPortletLink( 'p-cactions', 'javascript:wgKeepStatus.keepStatus()',
                    'Оставить', 'ca-keep-status', 'Оставить статье статус хорошей' );
    } );
  },
 
  'onTextDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    nominationRegex = /\{{2}К лишению статуса хорошей\|(\d{1,2}) ([а-я]+) (\d{4})/i;
    var match = pgtext.match( nominationRegex );
    wgKeepStatus.date = match[1] + ' ' + match[2] + ' ' + match[3];
    wgKeepStatus.newtext = pgtext.replace( nominationRegex, '{{Хорошая статья' );
    wgKeepStatus.editsummary = "статье [[" + mw.config.get('wgPageName') + "]] оставлен статус хорошей";
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=info&intoken=edit&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    wgKeepStatus.rq.onreadystatechange = wgKeepStatus.onTokenDownload;
    wgKeepStatus.rq.open( "GET", uri, true );
    wgKeepStatus.rq.send( "" );
  },
 
  'onTokenDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    wgKeepStatus.edittoken = ch(result.query.pages).edittoken;
    if( !wgKeepStatus.edittoken ) {
      alert( 'Редактирование невозможно' );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( mw.config.get('wgPageName') );
    var rq = "summary=" + encodeURIComponent( wgKeepStatus.editsummary ) +
             "&text=" + encodeURIComponent( wgKeepStatus.newtext ) +
             "&token=" + encodeURIComponent( wgKeepStatus.edittoken );
 
    wgKeepStatus.rq.onreadystatechange = wgKeepStatus.onEditSaved;
    wgKeepStatus.rq.open( "POST", uri, true );
    wgKeepStatus.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgKeepStatus.rq.send( rq );
  },
 
  'onEditSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( 'Википедия:Кандидаты в хорошие статьи/К лишению статуса/Список кандидатов' );
    wgKeepStatus.rq.onreadystatechange = wgKeepStatus.onTableDownload;
    wgKeepStatus.rq.open( "GET", uri, true );
    wgKeepStatus.rq.send( "" );
  },
 
  'onTableDownload' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    var pgtext = ch(result.query.pages).revisions[0]['*'];
    var strings = pgtext.split('\n');
    for( var i = 0; i < strings.length; ++i ) {
      var line = strings[i];
      if( line == '|-' && strings[i + 2].indexOf( '[[' + mw.config.get('wgPageName') + ']]' ) != -1 ) {
        wgKeepStatus.tabletext += line + 'class=shadow\n' + strings[i + 1] + '\n' + strings[i + 2] + '\n' +
                                 strings[i + 3] + '\n' + strings[i + 4] + 'Статус оставлен\n';
        i += 4;
      }
      else {
        wgKeepStatus.tabletext += line + '\n';
      }
    }
 
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=edit&title=' + encodeURIComponent( 'Википедия:Кандидаты в хорошие статьи/К лишению статуса/Список кандидатов' );
    var rq = "summary=" + encodeURIComponent( wgKeepStatus.editsummary ) +
             "&text=" + encodeURIComponent( wgKeepStatus.tabletext ) +
             "&token=" + encodeURIComponent( wgKeepStatus.edittoken );
 
    wgKeepStatus.rq.onreadystatechange = wgKeepStatus.onTableSaved;
    wgKeepStatus.rq.open( "POST", uri, true );
    wgKeepStatus.rq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    wgKeepStatus.rq.send( rq );
  },
 
  'onTableSaved' : function() {
    if( this.readyState != 4 ) return;
    if( this.status != 200 ) return;
 
    result = eval('('+this.responseText+')');
    if( typeof( result.error ) != 'undefined' ) {
      alert( "Не удалось сохранить страницу: " + result.error.info );
      return;
    }
 
    jsMsg( 'Статус успешно оставлен. <a href="javascript:location.reload()">Перезагрузить страницу</a>' );
  },
 
  'keepStatus' : function() {
    var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=' + encodeURIComponent( mw.config.get('wgPageName') );
    this.rq = sajax_init_object();
    this.rq.onreadystatechange = this.onTextDownload;
    this.rq.open( "GET", uri, true );
    this.rq.send( "" );
  }
}
 
$( wgArchivePage.addArchiveTab );
$( wgMakeGood.addMakeGoodTab );
$( wgDeny.addDenyTab );
$( wgRevoke.addRevokeTab );
$( wgKeepStatus.addKeepStatusTab );