Jump to content

User:Funa-enpitu/Forum/installer.js

From mediawiki.org

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function(){
mw.loader.using('util');
mw.fpage = function(){
	const u=new URL(window.location.href);
	u.searchParams.set('finst',(Number(mw.util.getParamValue('finst')) + 1));
	location.href = u;
};

if(mw.util.getParamValue('finst')){
	const mc = document.querySelector('#mw-content-text');
	mw.loader.load(['mediawiki.ui.button','mediawiki.ui.input','mediawiki.ui.checkbox']);
	document.querySelector('#firstHeading').innerHTML = 'Forum.jsのインストール';
	document.title = 'Forum.jsのインストール - ' + mw.config.get('wgSiteName');
	const pagecont = (function(){
		switch(mw.util.getParamValue('finst')){
			case '1':
				return [
					'<p>Forum.jsインストーラへようこそ!</p><p>Forum.jsは、MediaWiki用掲示板システムです。</p><p>ここではアップデートも可能です。</p><h2>環境の確認</h2><div><ul><li>Scribunto: <span id="f-Scribunto"></span></li><li>ParserFunctions: <span id="f-ParserFunctions"></span></li><li>Echo: <span id="f-Echo"></span></li><li>WikiEditor: <span id="f-WikiEditor"></span></li><li>TemplateStyles: <span id="f-TemplateStyles"></span></li></ul></div><h2>権限の確認</h2><div><ul><li>editsitejs: <span id="f-editsitejs"></span></li><li>purge: <span id="f-purge"></span></li></ul></div><div style="text-align:center;"><input type="button" id="f-next" class="mw-ui-button" value="次へ" onclick="mw.fpage();" disabled></div><style>.mw-ui-button:disabled{background-color:#c8ccd1 !important;color:#ffffff !important;border-color:#c8ccd1 !important;cursor:not-allowed;}</style>',
					function(){
						new mw.Api().get({
							action: "query",
							format: "json",
							meta: "siteinfo|userinfo",
							formatversion: "2",
							siprop: "extensions",
							uiprop: "rights"
						}).then(function(res){
							const ext = function(extname){
								const inf = res.query.extensions.find(({name}) => name == extname) || false;
								const fin = (inf ? ['<span style="color:#008a00;">OK</span>',true] : ['<span style="color:#f00;">NG</span>',false]);
								document.querySelector('#f-'+extname).innerHTML = `${fin[0]} (${inf.version || '-'})`;
								return fin[1];
							};
							const user = function(right){
								const inf = res.query.userinfo.rights.includes(right);
								document.querySelector('#f-'+right).innerHTML = inf ? '<span style="color:#008a00;">OK</span>' : '<span style="color:#f00;">NG</span>';
								return inf;
							};
							if(ext('Scribunto') & ext('ParserFunctions') & ext('Echo') & ext('WikiEditor') & ext('TemplateStyles') & user('editsitejs') & user('purge')){
								document.querySelector("#f-next").disabled = false;
							}
						});
					}
				];
			case '2':
				return [
					'<h2>設定</h2><div><form id="f-cfg"><label>フォーラムのトップページ: <input type="text" name="toppage" class="mw-ui-input" required></label><label>スレッドを一覧に一度に表示する数: <input type="number" name="threads" class="mw-ui-input" value="20" required></label><input name="finst" value="3" hidden><div style="text-align:center;"><input type="submit" value="次へ" class="mw-ui-button"></div></form></div><style>#f-cfg>label{margin-bottom:.5em;width:50%;display:block;}</style>',
					function(){
						if(mw.forum){
							mw.fpage();
						}
					}
				];
			case '3':
				return [
					'<p>インストールの準備ができました。</p><div style="text-align:center;"><input type="button" id="f-next" class="mw-ui-button mw-ui-progressive" value="インストール" onclick="mw.fpage();"></div>',
					function(){}
				];
				case '4':
					return [
						`<table class="wikitable" style="width:100%;"><tbody><tr><th>ページ</th><th>ファイル</th><th>状態</th></tr><tr><td><a href="${mw.util.getUrl('Template:Post')}">Template:Post</a></td><td><a href="https://www.mediawiki.org/w/index.php?title=User:Funa-enpitu/Forum/post.tpl&action=raw&ctype=text/x-wiki">post.tpl</a></td><td id="f-post">Loading...</td></tr><tr><td><a href="${mw.util.getUrl('Template:Post/styles.css')}">Template:Post/styles.css</a></td><td><a href="https://www.mediawiki.org/w/index.php?title=User:Funa-enpitu/Forum/post-styles.css&action=raw&ctype=text/x-wiki">post-styles.css</a></td><td id="f-post-styles">Waiting...</td></tr><tr><td><a href="${mw.util.getUrl('Module:Thread')}">Module:Thread</a></td><td><a href="https://www.mediawiki.org/w/index.php?title=User:Funa-enpitu/Forum/thread.lua&action=raw&ctype=text/x-wiki">thread.lua</a></td><td id="f-thread">Waiting</td></tr><tr><td><a href="${mw.util.getUrl('MediaWiki:Common.js')}">MediaWiki:Common.js</a></td><td><a href="https://www.mediawiki.org/w/index.php?title=User:Funa-enpitu/Forum/common.js&action=raw&ctype=text/javascript">common.js</a></td><td id="f-commonjs">Waiting</td></tr><tr><td><a href="${mw.util.getUrl(mw.util.getParamValue('toppage'))}">${mw.util.getParamValue('toppage')}</a></td><td><a href="https://www.mediawiki.org/w/index.php?title=User:Funa-enpitu/Forum/toppage&action=raw&ctype=text/javascript">toppage</a></td><td id="f-toppage">Waiting</td></tr></tbody></table>`,
						async() => {
							const getedit = async(file, page, id, append) => {
								document.querySelector('#f-' + id).innerHTML = 'Loading...';
								return await new mw.Api().postWithToken('csrf', {
									action: 'edit',
									title: page,
									summary: (mw.forum ? 'update Forum.js' : 'install Forum.js'),
									[(append || 'text')]: await fetch('https://www.mediawiki.org/w/rest.php/v1/page/User:Funa-enpitu%2FForum%2F' + file).then((res)=>{return res.json();}).then((json)=>{document.querySelector('#f-'+id).innerHTML='Editing...';return (append ? json.source.replace(/\$(toppage|threads)/g, (match, p1)=>{return mw.util.getParamValue(p1);}) : json.source);}),
									format: 'json'
								}).done((resolve) => {
									document.querySelector('#f-' + id).innerHTML = '<span style="color:#008a00;">Done</span>';
								}).fail((reject) => {
									document.querySelector('#f-' + id).innerHTML = '<span style="color:#f00;">Fail</span>';
								});
							};
							try{
								await getedit('post.tpl', 'Template:Post', 'post');
								await getedit('thread.lua', 'Module:Thread', 'thread');
								await getedit('post-styles.css', 'Template:Post/styles.css', 'post-styles')
								if(mw.forum){
									document.querySelector('#f-commonjs').innerHTML = '-';
									document.querySelector('#f-toppage').innerHTML = '-';
								}else{
									await getedit('common.js', 'MediaWiki:Common.js', 'commonjs', 'appendtext');
									await getedit('toppage', mw.util.getParamValue('toppage'), 'toppage');
								}
							}catch{
								mc.innerHTML += '<div style="text-align:center;"><input type="button" id="f-next" class="mw-ui-button" value="再試行" onclick="location.reload();"></div>';
								return;
							}
							await new mw.Api().post({
								action: 'purge',
								titles: 'Template:Post|Module:Thread|MediaWiki:Common.js',
								format: 'json'
							});
							mc.innerHTML += `<p>インストールが完了しました。すぐに反映されない場合、掲示板にアクセスし数回キャッシュを破棄してください。</p><p>installer.jsの読み込み部分はコメントアウトしておいてください。</p><div style="text-align:center;"><a href="${mw.util.getUrl((mw.forum ? mw.forum.toppage : mw.util.getParamValue('toppage')))}"><input type="button" id="f-next" class="mw-ui-button mw-ui-progressive" value="掲示板に行く"></a></div>`;
						}
					];
			default : 
				return [
					'<span class="error">無効なページです</span>',
					function(){}
				];
		}
	})();
	mc.innerHTML = pagecont[0];
	pagecont[1]();
}else{
	mw.util.addPortletLink('p-tb', 'javascript: mw.fpage();', 'Install Forum.js', 't-f', 'Install Forum.js');
}

})();