コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

利用者:Syunsyunminmin/script/Check-vote.js

お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。

多くの WindowsLinux のブラウザ

  • Ctrl を押しながら F5 を押す。

Mac における Safari

  • Shift を押しながら、更新ボタン をクリックする。

Mac における ChromeFirefox

  • Cmd Shift を押しながら R を押す。

詳細についてはWikipedia:キャッシュを消すをご覧ください。

if (typeof Syunmin === 'undefined') {
	var Syunmin = {};
}
Syunmin.voterVerification = {};
Syunmin.voterVerification.formSubpage = new OO.ui.TextInputWidget( {
	label: 'サブページ名',
	value: 'Wikipedia:管理者への立候補/',
	id: 'check-vote-subpage',
	required: true,
	validate: 'non-empty'
} );
Syunmin.voterVerification.formUser = new OO.ui.TextInputWidget( {
	label: '利用者名',
	id: 'check-vote-user',
	placeholder: mw.config.get('wgUserName')
} );
Syunmin.voterVerification.formbutton = new OO.ui.ButtonWidget( {
	label: '実行',
		id: 'check-vote-submit',
	flags: [
			'primary',
			'progressive'
		]
} );
Syunmin.voterVerification.ProgressBarProgress = 0;
Syunmin.voterVerification.ProgressBar = new OO.ui.ProgressBarWidget( {
	progress: Syunmin.voterVerification.ProgressBarProgress
} );
Syunmin.voterVerification.return = function () {
	Syunmin.voterVerification.formSubpage.setReadOnly(false);
	Syunmin.voterVerification.formbutton.setDisabled(false);
	Syunmin.voterVerification.formUser.setReadOnly(false);
};
Syunmin.voterVerification.error = {};
Syunmin.voterVerification.error.missing = new OO.ui.MessageWidget( {
	id: 'check-vote-error-missing',
	type: 'error',
	inline: true,
	label: '存在しないサブページです。'
} );

Syunmin.voterVerification.form = function () {
	document.title = '有効票確認';
	$('#firstHeading').text('有効票確認');
	$('#mw-content-text').html('<p>このページでは管理者信任投票や権限申請で、投票者に投票権があるのか確認できます。</p>');
	$('#mw-content-text').append('<p><strong>あくまでも参考として使用してください。このツールによる判定の正確性は保証しません。</strong></p>');
	$('#mw-content-text').append('<br />');
	$('#mw-content-text').append( new OO.ui.FieldsetLayout( {
		label: '有効票チェッカー',
		id: 'check-vote-form'
		} ).addItems( [
			new OO.ui.ActionFieldLayout(Syunmin.voterVerification.formSubpage,
				Syunmin.voterVerification.formbutton,
			{
				label: '管理者立候補/権限申請サブページを入力してください。',
				align: 'top'
				}
			),
			new OO.ui.FieldLayout(Syunmin.voterVerification.formUser,
			{
				label: '単一の利用者に投票権があるか確認する場合は利用者名も入力してください。',
				align: 'top'
				}
			)
		] ).$element);
	$('#check-vote-submit').click( function ( e ) {
		e.preventDefault();
		/* 既存のエラー等を除去 */
		$('#check-vote-error-missing').remove();
		$('#check-vote-result').remove();
		$('#check-vote-progress').remove();

		Syunmin.voterVerification.formSubpage.setReadOnly(true);
		Syunmin.voterVerification.formbutton.setDisabled(true);
		Syunmin.voterVerification.formUser.setReadOnly(true);
		(Syunmin.voterVerification.formUser.value.length == 0) ? Syunmin.voterVerification.doCheck(e) : Syunmin.voterVerification.doCheckUser(e);
	});
};
Syunmin.voterVerification.doCheck = async function ( e ) {
	console.log('VoterCheckStart');

	Syunmin.voterVerification.subpage = Syunmin.voterVerification.formSubpage.getValue();
	$('#check-vote-form').append('<div id="check-vote-progress">立候補時間を取得中</div>');
	$('#check-vote-form').append(Syunmin.voterVerification.ProgressBar.$element);

	var api = new mw.Api();
	Syunmin.voterVerification.params = {};

	/* 立候補のタイムスタンプ取得 */
	Syunmin.voterVerification.timestamp = await Syunmin.voterVerification.getTimestamp(Syunmin.voterVerification.subpage);
	Syunmin.voterVerification.timestamp = new Date(Syunmin.voterVerification.timestamp);
	Syunmin.voterVerification.timestampOffsetDate = new Date(Syunmin.voterVerification.timestamp);
	Syunmin.voterVerification.timestamp.setSeconds(Syunmin.voterVerification.timestamp.getSeconds() - 1);
	Syunmin.voterVerification.timestamp = Syunmin.voterVerification.timestamp.toJSON();
	Syunmin.voterVerification.timestamp = Syunmin.voterVerification.timestamp.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*/, '$1Z');
	console.log(Syunmin.voterVerification.timestamp);
	Syunmin.voterVerification.timestampOffsetDate.setDate(Syunmin.voterVerification.timestampOffsetDate.getDate() - 90);
	Syunmin.voterVerification.timestampOffsetDate = Syunmin.voterVerification.timestampOffsetDate.toJSON();
	Syunmin.voterVerification.timestampOffsetDate = Syunmin.voterVerification.timestampOffsetDate.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*/, '$1Z');
	console.log(Syunmin.voterVerification.timestampOffsetDate);
	Syunmin.voterVerification.ProgressBar.setProgress(Syunmin.voterVerification.ProgressBarProgress += 10);
	$('#check-vote-progress').text('立候補時間の取得完了');

	/* サブページの内容取得 */
	$('#check-vote-progress').text('サブページ内容を取得中');
	Syunmin.voterVerification.params.subpage = {
		"action": "parse",
		"format": "json",
		"page": Syunmin.voterVerification.subpage,
		"prop": "wikitext",
		"formatversion": "2"
	};
	console.log(Syunmin.voterVerification.params.subpage);
	api.get(Syunmin.voterVerification.params.subpage).done(function(data_subpage){
		Syunmin.voterVerification.ProgressBar.setProgress(Syunmin.voterVerification.ProgressBarProgress += 10);
		$('#check-vote-progress').text('サブページ内容の取得完了');
		console.log(data_subpage.parse.wikitext);

		/* 投票者を抽出 */
		$('#check-vote-progress').text('投票者を抽出中');
		Syunmin.voterVerification.voters = data_subpage.parse.wikitext.match(/\{\{管理者への立候補\/sig\-l\|[^\}]*\|\d{14}\}\}/g);
		console.log(Syunmin.voterVerification.voters);
		Syunmin.voterVerification.RegExpVoter = new RegExp('\\{\\{管理者への立候補\\/sig\\-l\\|\([^\\}]*\)\\|\\d{14}\\}\\}', 'g');
		for (var u=0; u<Syunmin.voterVerification.voters.length; u++) {
			Syunmin.voterVerification.voters[u] = Syunmin.voterVerification.voters[u].replace(Syunmin.voterVerification.RegExpVoter, '$1');
		}
		console.log(Syunmin.voterVerification.voters);
		Syunmin.voterVerification.ProgressBar.setProgress(Syunmin.voterVerification.ProgressBarProgress +=  10);
		$('#check-vote-progress').text('投票者の抽出完了');

		/* 投票資格の確認 */
		Syunmin.voterVerification.checkVoter(Syunmin.voterVerification.voters);
	});
};
Syunmin.voterVerification.doCheckUser = async function ( e ) {
	e.preventDefault();
	console.log('VoterCheckUserStart');
	$('#check-vote-form').append('<div id="check-vote-progress">立候補時間を取得中</div>');
	$('#check-vote-form').append(Syunmin.voterVerification.ProgressBar.$element);
	var user = Syunmin.voterVerification.formUser.value;
	Syunmin.voterVerification.subpage = Syunmin.voterVerification.formSubpage.getValue();
	/* 立候補のタイムスタンプ取得 */
	Syunmin.voterVerification.timestamp = await Syunmin.voterVerification.getTimestamp(Syunmin.voterVerification.subpage);
	Syunmin.voterVerification.timestamp = new Date(Syunmin.voterVerification.timestamp);
	Syunmin.voterVerification.timestampOffsetDate = new Date(Syunmin.voterVerification.timestamp);
	Syunmin.voterVerification.timestamp.setSeconds(Syunmin.voterVerification.timestamp.getSeconds() - 1);
	Syunmin.voterVerification.timestamp = Syunmin.voterVerification.timestamp.toJSON();
	Syunmin.voterVerification.timestamp = Syunmin.voterVerification.timestamp.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*/, '$1Z');
  Syunmin.voterVerification.Text1 = Syunmin.voterVerification.timestamp //Syunmin.voterVerification.timestamp.getFullYear() + '年' + Syunmin.voterVerification.timestamp.getMonth() + '月' + Syunmin.voterVerification.timestamp.getDate() + '日(' + Syunmin.voterVerification.timestamp.getDay() + ')' + Syunmin.voterVerification.timestamp.getHours() + '時' + Syunmin.voterVerification.timestamp.getMinutes() + '分' + Syunmin.voterVerification.timestamp.getSeconds() + '秒';
	console.log(Syunmin.voterVerification.timestamp);
	Syunmin.voterVerification.timestampOffsetDate.setDate(Syunmin.voterVerification.timestampOffsetDate.getDate() - 90);
	Syunmin.voterVerification.timestampOffsetDate = Syunmin.voterVerification.timestampOffsetDate.toJSON();
	Syunmin.voterVerification.timestampOffsetDate = Syunmin.voterVerification.timestampOffsetDate.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*/, '$1Z');
  Syunmin.voterVerification.Text2 = Syunmin.voterVerification.timestampOffsetDate//Syunmin.voterVerification.timestampOffsetDate.getFullYear() + '年' + Syunmin.voterVerification.timestampOffsetDate.getMonth() + '月' + Syunmin.voterVerification.timestampOffsetDate.getDate() + '日(' + Syunmin.voterVerification.timestampOffsetDate.getDay() + ')' + Syunmin.voterVerification.timestampOffsetDate.getHours() + '時' + Syunmin.voterVerification.timestampOffsetDate.getMinutes() + '分' + Syunmin.voterVerification.timestampOffsetDate.getSeconds() + '秒';
	console.log(Syunmin.voterVerification.timestampOffsetDate);
	Syunmin.voterVerification.ProgressBar.setProgress(Syunmin.voterVerification.ProgressBarProgress += 30);
	$('#check-vote-progress').text('立候補時間の取得完了');

	/* 投票権確認 */
	console.log([user]);
	Syunmin.voterVerification.checkVoter([user]);
};
Syunmin.voterVerification.getTimestamp = async function (subpage) {
	var data_timestamp = await new mw.Api().get({
		action: "query",
		format: "json",
		prop: "revisions",
		titles: subpage,
		formatversion: "2",
		rvprop: "timestamp",
		rvslots: "*",
		rvlimit: "1",
		rvdir: "newer"
	});
	var p, q;
	for ( p in data_timestamp.query.pages ) {
		if (data_timestamp.query.pages[p].missing) {
			$('#check-vote-form').append(Syunmin.voterVerification.error.missing.$element);
			Syunmin.voterVerification.return();
		} else {
			for (q in data_timestamp.query.pages[p].revisions) {
				console.log(data_timestamp.query.pages[p].revisions[q]);
				return data_timestamp.query.pages[p].revisions[q].timestamp;
			}
		}
	}
};

Syunmin.voterVerification.checkVoter = async function (voterlist) {
	$('#check-vote-progress').text('投票資格を確認中');
	$('#check-vote-form').append('<table id="check-vote-result" border="1" style="border-collapse: collapse;"><tr><th>利用者</th><th>初編集から30日経過</th><th>標準名前空間の編集回数が50回以上</th><th>直近90日における標準名前空間の編集回数が5回以上<br />' + Syunmin.voterVerification.timestampOffsetDate + 'から' + Syunmin.voterVerification.timestamp + '</th></tr></table>');
	for await (var voterdata of Syunmin.voterVerification.checkVoterscontrib(voterlist)) {
		console.log(voterdata);

		var result = '<tr><td>' + voterdata.username + '</td>';

		// 初編集
		voterdata.usercontribs1 = new Date(voterdata.usercontribs1);
		var timestampFirstEdit = new Date(Syunmin.voterVerification.timestampOffsetDate);
		result += '<td>' + ((voterdata.usercontribs1 <= timestampFirstEdit) ? 'レ' : '☓');

		//総編集回数
		result += '</td><td>' + ((voterdata.usercontribs2.length >= 50) ? 'レ' : '☓');

		// 直近編集回数
		console.log(voterdata.usercontribs2);
		var timestampLastEdits,latestEdits;
		for (var uc = 0; uc < voterdata.usercontribs2.length; uc++) {

			timestampLastEdits = new Date(voterdata.usercontribs2[uc].timestamp);
			console.log(timestampLastEdits);
			console.log(timestampFirstEdit);
			console.log(timestampLastEdits < timestampFirstEdit);
			if (timestampLastEdits < timestampFirstEdit) {
				latestEdits = voterdata.usercontribs2.slice(0, uc);
				console.log(latestEdits);
				break;
			} else if (uc == voterdata.usercontribs2.length - 1) {
				latestEdits = voterdata.usercontribs2;
			}
		}
		result += '</td><td>' + ((latestEdits.length >= 5) ? 'レ' : '☓');
		result += '</td></tr>';
		console.log(result);

		$('#check-vote-result').append(result);
		Syunmin.voterVerification.ProgressBar.setProgress(Syunmin.voterVerification.ProgressBarProgress +=  (70 / voterlist.length));
		await new Promise(resolve => setTimeout(resolve, 1*1000));
	}
	$('#check-vote-progress').text('投票資格の確認完了');
	Syunmin.voterVerification.return();
};
Syunmin.voterVerification.checkVoterscontrib = async function* (voterlist) {
	var api = new mw.Api();
	for (var v = 0; v < voterlist.length; v++) {
		console.log(voterlist[v]);
		var usercontribs = {};
		var usercontribs1 = await api.get({
			"action": "query",
			"format": "json",
			"list": "usercontribs",
			"formatversion": "2",
			"uclimit": "1",
			"ucuser": voterlist[v],
			"ucdir": "newer",
			"ucprop": "timestamp|title"
		});
		var usercontribs2 = await api.get({
			"action": "query",
			"format": "json",
			"list": "usercontribs",
			"formatversion": "2",
			"uclimit": "50",
			"ucstart": Syunmin.voterVerification.timestamp,
			"ucuser": voterlist[v],
			"ucnamespace": "0",
			"ucprop": "timestamp|title"
		});
		usercontribs.usercontribs1 = usercontribs1.query.usercontribs[0].timestamp;
		usercontribs.usercontribs2 = usercontribs2.query.usercontribs;
		usercontribs.username = voterlist[v];
		yield usercontribs;
	}
};

if (mw.config.get('wgNamespaceNumber') == -1 && (
	mw.config.get('wgTitle') == 'VoterCheck' ||
	mw.config.get('wgTitle') == 'Votercheck' ||
	mw.config.get('wgTitle') == '有効票確認'
)){
	Syunmin.voterVerification.form();
}