利用者:Syunsyunminmin/script/syunmin-update.js

出典:『ウィキニュース』(ベータ版)

注意: 保存した後、ブラウザのキャッシュをクリアする必要があります。Mozilla / Firefox / Safari: [Shift] を押しながら [再読み込み] をクリック、または [Shift]-[Ctrl]-[R] (Macでは [Cmd]-[Shift]-[R]); IE: [Ctrl] を押しながら [更新] をクリック、または [Ctrl]-[F5]; Konqueror: [再読み込み] をクリック、または [F5]; Opera: 「ツール」→「設定」からキャッシュをクリア。

//<nowiki>
if (mw.config.get('wgPageName') == '利用者:Syunsyunminmin/controlpanel') {
  var api = new mw.Api(), summary = '月次更新 ([[利用者:Syunsyunminmin/script/syunmin-update.js|script]]による)';
  var controlpage = document.getElementById('syunmin-wikinews-ja-update');
  var form_type_of_create = '<div><label for="type_of_create">作成する名前空間: </label><input type="checkbox" id="projectNS">ウィキニュース<input type="checkbox" id="tanshinNS">短信<input type="checkbox" id="categoryNS">カテゴリ</div>';
  var form_year = '<div><label for="thisyear">今年: </label><input type="text" id="thisyear" required></div>';
  var form_month = '<div><label for="thismonth">今月: </label><input type="text" id="thismonth" required></div>';
  var form_DayOfWeek = '<div><label for="firstdayOftheweek">始まる曜日: </label><input type="text" id="firstdayOftheweek" required maxlength="1"></div>';
  var form_numOfdays = '<div><label for="numOfdays">日数: </label><input type="text" id="numOfdays" required></div>';
  var submit = '<input type="button" value="実行" id="dorun" onclick="update()">';
  var form_stop = '<input type="button" value="停止" onclick="stoprun(\'stop\')">';
  var content = '<form name="syunmin_update">' + form_type_of_create + form_year + form_month + form_DayOfWeek + form_numOfdays + submit + form_stop +'</form>';
  var statusinfo = '<div id="statusinfo" style="display: none;"><h3 id="statusinfotext" style="text-align: center;">実行中<img src="https://upload.wikimedia.org/wikipedia/commons/2/2a/Loading_Key.gif" alt="グルグル"></h3><div id="statusinfobox" style="width: 80%; border-style: ridge; margin:0 auto; background-color: white; height: 200px; overflow: scroll;"></div></div>';
  controlpage.innerHTML = content;
  console.log('script done');
  var count = 0,runcount=0, maxcount, thisyear, lastyear, nextyear, thismonth, lastmonth, nextmonth, lastmonthyear, nextmonthyear, firstdayOftheweek, numOfdays, doproject = false, dotanshin = false, docategory = false;
}
function update() {
    if ((document.getElementById('projectNS').checked == false && document.getElementById('tanshinNS').checked == false && document.getElementById('categoryNS').checked == false) || document.getElementById('thisyear').value == '' || document.getElementById('thismonth').value == '' || document.getElementById('firstdayOftheweek').value == '' || document.getElementById('numOfdays').value == '') {
      alert('入力不備があります');
      return;
    }
    inputboxdisabled ('projectNS');
    inputboxdisabled ('tanshinNS');
    inputboxdisabled ('categoryNS');
    inputboxdisabled ('thisyear');
    inputboxdisabled ('thismonth');
    inputboxdisabled ('firstdayOftheweek');
    inputboxdisabled ('numOfdays');
    inputboxdisabled ('dorun');
    maxcount = document.syunmin_update.numOfdays.value;
    if (document.getElementById('projectNS').checked) {
      doproject = true;
    }
    if (document.getElementById('tanshinNS').checked) {
      dotanshin = true;
    }
    if (document.getElementById('categoryNS').checked) {
      docategory = true;
    }
    thisyear = document.getElementById('thisyear').value;
    thisyear = parseInt(thisyear);
    lastyear = thisyear - 1;
    nextyear = thisyear + 1;
    thismonth = document.getElementById('thismonth').value;
    thismonth = parseInt(thismonth);
    if (thismonth == 1) {
      lastmonth = 12;
      nextmonth = 2;
      lastmonthyear = thisyear - 1;
      nextmonthyear = thisyear;
    } else if (thismonth == 12) {
        lastmonth = 11;
        nextmonth = 1;
        lastmonthyear = thisyear;
        nextmonthyear = thisyear + 1;
    } else {
      lastmonth = thismonth - 1;
      nextmonth = thismonth + 1;
      lastmonthyear = thisyear;
      nextmonthyear = thisyear;
    }
    firstdayOftheweek = document.getElementById('firstdayOftheweek').value;
    numOfdays = document.getElementById('numOfdays').value;
    controlpage.insertAdjacentHTML('beforeend', statusinfo);
    document.getElementById('statusinfo').style.display='';
    running = setInterval(onTimer, 5000);
  }
  function stoprun(status) {
    clearInterval(running);
    inputboxabled ('projectNS');
    inputboxabled ('tanshinNS');
    inputboxabled ('categoryNS');
    inputboxabled ('thisyear');
    inputboxabled ('thismonth');
    inputboxabled ('firstdayOftheweek');
    inputboxabled ('numOfdays');
    inputboxabled ('dorun');
    console.log('stoped');
    if (status == 'stop') {
      document.getElementById('statusinfotext').innerText = '中止しました';
    }
  }
  function onTimer() {
    var title,main,postcontent;
    var month1 = thismonth, date1 = count;
    if (thismonth.toString().length == 1) {
      month1 = '0' + thismonth;
    }
    if (count.toString().length == 1) {
      date1 = '0' + count;
    }
    if (count == 0) {
      if (doproject) {
        title = 'ウィキニュース:' + thisyear + '年/' + thismonth + '月';
        postcontent = '{{メインページお知らせ}}\n{{' + firstdayOftheweek + '曜からの月|\n|日数=' + numOfdays + '\n|年=' + thisyear + '\n|月=' + thismonth + '\n|前月=' + lastmonth + '\n|翌月=' + nextmonth + '\n|前月の年=' + lastmonthyear + '\n|翌月の年=' + nextmonthyear + '\n|前年=' + lastyear + '\n|翌年=' + nextyear + '\n}}\n';
      } else if (dotanshin) {
        title = '短信:' + thisyear + '年/' + thismonth + '月';
        postcontent = '{{' + firstdayOftheweek + '曜からの月|\n|日数=' + numOfdays + '\n|年=' + thisyear + '\n|月=' + thismonth + '\n|前月=' + lastmonth + '\n|翌月=' + nextmonth + '\n|前月の年=' + lastmonthyear + '\n|翌月の年=' + nextmonthyear + '\n|前年=' + lastyear + '\n|翌年=' + nextyear + '\n|分類=短信\n}}\n';
      } else if (docategory) {
        title = 'カテゴリ:' + thisyear + '年' + thismonth + '月';
        postcontent = '[[Category:' + thisyear + '年]]';
      } else {
        error();
        return;
      }
    } else {
      if (doproject) {
        title = 'ウィキニュース:' + thisyear + '年/' + thismonth + '月/' + count + '日';
        postcontent = '<onlyinclude>{|style="clear:right; float:right; background:transparent; border-spacing:0; width:100px"\n<!-- 画像は100px幅以内で -->\n<!--[[ファイル:ファイル名.拡張子|right|100px|代替文]]-->\n|}\n<DynamicPageList>\ncategory=公開中\ncategory=' + thisyear + '年' + thismonth + '月' + count + '日\nnotcategory=議論中\nsuppresserrors=true\nnamespace=0\n</DynamicPageList></onlyinclude>\n\n[[カテゴリ:' + thisyear + '年' + thismonth + '月' + count + '日|*]]\n';
      } else if (dotanshin) {
        title = '短信:' + thisyear + '年/' + thismonth + '月/' + count + '日';
        postcontent = '{{短信}}{{短信ヘッダ|' + thisyear + '|' + thismonth + '|' + count + '}}\n<onlyinclude>\n<!--↓短信フォーマット↓\n*(政治/経済/社会/文化/スポーツ/学術/ひと/気象/脇ニュース)短信本文 - [http://(出典URL) 発行者]\n-->\n</onlyinclude>\n{{短信フッタ|' + thisyear + '|' + thismonth + '|' + count + '}}\n';
      } else if (docategory) {
        title = 'カテゴリ:' + thisyear + '年' + thismonth + '月' + count + '日';
        postcontent = '{{カテゴリ日付|' + thisyear + month1 + date1 + '}}';
      } else {
        error();
        return;
      }
    }
    var params = {
				action: 'edit',
				title: title,
				text: postcontent,
				summary: summary,
				format: 'json'
				};
				api.postWithToken( 'csrf', params ).done( function ( data ) {
					if ( data.error && data.error.info ) {
						mw.notify( $('<div style="color:red;">' + data.error.info + '</div>'), { tag: 'update-info-bar' } );
            document.getElementById('statusinfobox').insertAdjacentHTML('beforeend', runcount + '. ' + '<a href="/wiki/ ' + title + '" target="_blank" rel="noopener">' + title + '</a>' + ': 失敗 <div style="color:red;">' + data.error.info + '</div><br>');
					} else {
						mw.notify( $('<div style="color:green;">' + title + ': 完了</div>'), { tag: 'update-info-bar' } );
            console.log(postcontent);
            console.log(title + ': posted');
            runcount++;
            document.getElementById('statusinfobox').insertAdjacentHTML('beforeend', runcount + '. ' + '<a href="/wiki/ ' + title + '" target="_blank" rel="noopener">' + title + '</a>' + ': 完了<br>');
          }
        } );
    if (count == maxcount) {
      count = 0;
      if (doproject) {
        doproject = false;
      } else if (dotanshin) {
        dotanshin = false;
        if (docategory == false) {
          error('done');
          return;
        }
      } else if (docategory) {
        docategory = false;
        error('done');
        return;
      } else {
        error('done');
        return;
      }
    } else {
      count++;
    }
  }
  function error(type) {
    if (type == null) {
      alert('処理中にトラブルが発生しました。');
    } else if (type == 'done') {
      document.getElementById('statusinfotext').innerText = '完了';
      stoprun();
      alert('完了しました');
    }
    stoprun();
  }
  function inputboxdisabled (idname) {
    var disableelement = document.getElementById(idname);
    disableelement.disabled = true;
  }
  function inputboxabled (idname) {
    var disableelement = document.getElementById(idname);
    disableelement.disabled = false;
  }
//</nowiki>