利用者:Naggy Nagumo/言語関連テンプレートの一貫性チェック.js

出典: フリー多機能辞典『ウィクショナリー日本語版(Wiktionary)』
ナビゲーションに移動 検索に移動

注意: 保存後、変更を確認するにはブラウザーのキャッシュを消去する必要がある場合があります。

  • Firefox / Safari: Shift を押しながら 再読み込み をクリックするか、Ctrl-F5 または Ctrl-R を押してください (Mac では ⌘-R)
  • Google Chrome: Ctrl-Shift-R を押してください (Mac では ⌘-Shift-R)
  • Internet Explorer / Microsoft Edge: Ctrl を押しながら 最新の情報に更新 をクリックするか、Ctrl-F5 を押してください
  • Opera: Ctrl-F5を押してください
// <nowiki>
{
	const action = mw.config.get("wgAction");
	const api = new mw.Api();
	
	const 言語表記テンプレートの一覧を取得 = function(テンプレートリスト){
		return mw.loader.using("mediawiki.api", function () {
			function getSingle(cmcontinue) {
				let parameters = {
					"action": "query",
					"list": "categorymembers",
					"cmlimit": 500,
					"cmtitle": "カテゴリ:言語表記テンプレート",
					"cmprop": "title",
					"maxlag": 5
				};
				if(cmcontinue){
					parameters.cmcontinue = cmcontinue;
				}
				else{
					parameters.cmstarthexsortkey = "41"; // "A"のUTF-8コード列
				}
				const result = api.get(parameters).done(function (data) {
					console.log(data);
					const members = data.query.categorymembers;
					let sortkey;
					members.forEach(function(v){
						テンプレートリスト.push(v.title);
					});
					if(data.continue && data.continue.cmcontinue){
						getSingle(data.continue.cmcontinue);
					}
					else{
						更新(テンプレートリスト);
					}
				});
			}

			return getSingle(null);
		});
	};
	
	const 更新 = function (テンプレートリスト) {
		const now = new Date();
		const 説明 = 
`このページでは「[[:カテゴリ:言語表記テンプレート]]」に対応する各種データに一貫性があるかチェックします。生成日時: ${now.getFullYear()}/${now.getMonth() + 1}/${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}

[[カテゴリ:メンテナンス|けんこめいいつかんせいちえつく]]

==一貫性チェック==
`;
		let テキスト = 説明
			+ '{| class="wikitable"\n'
			+ "! コード !! <nowiki>{{○○}}</nowiki> !! <nowiki>{{language|○○}}</nowiki> !! モジュール:languages !! 一貫性\n";
		テンプレートリスト.forEach(function(v){
			if(v.indexOf("テンプレート:") !== -1 && v !== "テンプレート:T" && v !== "テンプレート:L" && v !== "テンプレート:language"){
				const コード = v.split("テンプレート:")[1];
				テキスト += "|-\n";
				テキスト += "|" + コード;
				テキスト += "||{{" + コード + "}}";
				テキスト += "||{{language|" + コード + "}}";
				テキスト += "||{{#invoke:languages/templates|get_name_by_code|カテゴリ|" + コード + "}}";
				テキスト += "||{{#ifeq:{{" + コード + "}}|{{language|" + コード + "}}|{{#ifeq:{{" + コード + "}}|{{#invoke:languages/templates|get_name_by_code|カテゴリ|" + コード + "}}||'''×'''}}|'''×'''}}";
				テキスト += "\n";
			}
		});
		テキスト += "|}";

		console.log(テキスト);

		const title = "Wiktionary:言語名一貫性チェック";
		const summary = "更新";
	
		// 含まれるテンプレートが多すぎて一気に展開するとタイムアウトになる。ちーん。
		if(false){
			return mw.loader.using("mediawiki.api", function () {
				return api.get({
					action: "expandtemplates",
					text: テキスト,
					prop: "wikitext",
				}).done(function (data) {
					const 展開済みテキスト = data.expandtemplates.wikitext;
					console.log(展開済みテキスト);
					// return api.edit(title, function () {
					//	 return {
					//		 text: 展開済みテキスト,
					//		 summary: summary,
					//	 };
					// }).done(function (data) {
					//	 if (data.nochange) {
					//		 mw.notify(title + " は更新の必要はありません。");
					//	 } else {
					//		 mw.notify(title + " を更新しました。");
					//	 }
					// }).fail(function(...args) {
					//	 mw.notify("投稿中にエラーが発生しました。");
					//	 console.log(...args);
					// });
				});
			});
		}
	};
	
	const button = $("<button>");
	
	button
		.html("一貫性チェック");
	
	button.on("click", function () {
		const テンプレートリスト = [];
		言語表記テンプレートの一覧を取得(テンプレートリスト);
	});
	
	const p = $(".mw-parser-output p:first-of-type");
	p.before(button);
	
}
	
// </nowiki>