跳去內容

模組:加權文長

出自維基百科,自由嘅百科全書
模組解[] [] [] []

概要

[編輯]

{{#invoke:加權文長| show | 維基數據號碼 }}

{{#invoke:加權文長| wrap | 維基數據號碼 }}

用途

[編輯]

呢個係模:加權文長嘅模組版,做嘅嘢基本一樣,但係介面唔兼容。舊模參數係文章名,呢個模組嘅參數係維基數據號碼(Q 字頭數字)。

因為原模參數係文章名,用原模嘅表要首先要叫用(#invoke)WikidataIBgetLabel 或者 getSiteLink 函數,將維基數據號碼轉換做文章名,先可以用個模,當中多數用 getLabel 攞維基數據標籤,少數用 getSiteLink 攞粵文維基百科連結[註 1],然後個模再根據文章名判斷有冇文,文章大細等等。其中一個問題係用 getLabel 嘅表,有啲文連咗維基數據,但係文章同標籤唔同,出來嘅結果變成誤導。另外,見到有問題嗰陣,因為冇寫維基數據號碼,好難判斷問題喺邊。再者,有啲表用 getLabel,有啲表用 getSiteLink,亦令到唔同嘅表嘅結果好難比較。

呢個模組解決嘅,主要係加維基數據連結(暫時係喺括弧後面用上標問號表示),方便cek1清楚係咪真係寫咗要寫嘅嘢,定係寫咗第樣嘢唔知。另外,係統一用維基數據號碼做參數,唔使喺將表度重覆WikidataIB 嘅邊個函數。

模組有兩個可以叫用嘅函數,分別係舊嘅 wrap 函數同新嘅 show 函數。wrap 係用 getLabel 攞標籤,然後叫用舊模;show 函數嘅輸出全部自己計,唔依頼舊模,亦統一首先用 getSiteLink 直接喺維基數據攞粵維文章連結,冇連結先用標籤,未來亦可以做舊模做唔到嘅其他分析。兩款函數都會加維基數據拎。

[編輯]
  • 打:{{#invoke:加權文長|show|Q11642}} [註 2]
  • 出:陶器製作1,728?
  • 打:{{#invoke:加權文長|wrap|Q11642}}(純粹比較,唔建議用)
  • 出:陶器製作無呢版?

注意標籤名同文章名唔同;維基數據標籤係 「陶器製作」,粵語維基文章名係 「陶器」,所以舊模(如果用 getLabel)會出 「無呢版」。

  • 打:{{#invoke:加權文長|show|Q190812}} [註 2]
  • 出:大調1,129有文但係唔係 Q190812?
  • 打:{{#invoke:加權文長|wrap|Q190812}}(純粹比較,唔建議用)
  • 出:大調1,129?

呢個係相反情形,指定嘅維基數據項目冇文,但係有文同名,用舊模而又用 getLabel 嘅表會顯示成一切正常,但係新模組就maak1低有嘢唔妥。

註釋

[編輯]
為咗簡化敍述,下面用咗 「連維基數據」 同 「同名文章」 嘅字眼;「連維基數據」 係指連去指定嘅維基數據項目,「同名文章」 係指文章嘅標題係指定維基數據項目嘅粵文或者英文標籤(冇粵文標籤先會用英文標籤)。

同舊模一樣,喺某啲情況下,會喺括弧入面,字數後面加註釋,但係有啲註釋舊模冇:

舊模準則 模組準則 顯示註釋
冇連維基數據,或者冇同名文章[註 3] 冇連維基數據,又冇同名文章 無呢版
唔適用 有同名文章,但係冇連維基數據 有文但係唔係 維基數據號碼
唔適用 系統話文章係跳轉 係跳轉頁,跳轉到 「文章名
唔適用 維基數據話文章係搞清楚版[註 4] 係搞清楚版
唔適用 文章有 {{搞清楚}} 或者類似字樣 可能係搞清楚版
文章長度調整後 ≤ 200 唔適用 可能係跳轉頁

注意

[編輯]

原模用 「Weighted page size」 模(組)計調整過嘅文章長度;礙於呢個模組直接睇文章內容,調整要自己計(hard code)。萬一某日粵文維基嘅調整唔再係乘以 3.7,模組會計錯。

有啲文喺 「基礎文章」 用某個維基數據號碼,但係實際上大多數維基百科(包括粵維)連咗去第個維基數據項目,即係誤導嘅情況冇可能完全避免,例子包括自然科學嘅 「畢宿五570有文但係唔係 Q12170?」。

睇埋

[編輯]

舊模同相關模組:

[編輯]
  1. 基礎文章等級4有四張表用 getSiteLink,分別係地理人類學(加心理學、日常生活)技術數學
  2. 2.0 2.1 例子出自基礎文章等級4,藝術
  3. 如果個表叫用 getSiteLink,就係冇連維基數據;如果叫用 getLabel,就係冇同名文章
  4. 因為 Wikibase(維基數據介面)系統錯誤,實際上已經冇再cek1

-- vim: set sw=4 ts=4 ai sm :

---- 按 「加權文長」 模解,正常顯示係:
---- 「無爾版,寫住無呢版
----  少過3000,個數係紅色 (red)
----  3000至9999,個數係橙色 (orange)
----  1萬至2萬9999,個數係綠色 (green)
----  3萬或以上,個數係藍色 (blue)」
----
---- 睇 code 實際上有第啲顏色,按上面模解,實際準則係:
---- brown: 「無呢版」,weighted page size|{{{1}}}|R = 0
---- red: 「可能係跳轉頁」,weighted page size|{{{1}}}|R <= 200
---- red: weighted page size|{{{1}}} <= 3000
---- orange: weighted page size|{{{1}}} <= 10000
---- green: weighted page size|{{{1}}} <= 30000
---- blue: otherwise
---- 顏色係指括弧入面字數或者錯誤訊息嘅顏色,實際次序係
---- (字數,可能係跳轉頁)
---- 
---- 「字數乘3.7」 係 call 「weighted page size」 模(參數1係標題)
----
---- 因為維基數據標籤未必等如文章標題,原模會間中出錯,
---- 例如 Wikipedia:基礎文章/等級/4/藝術,Visual arts, Concepts and forms,
---- Q11642 標題 「陶器製作」,文章名 「陶器」,有文但係原模出 「無呢版」

require ('strict');
local p = {};

local WikidataIB = require('Module:WikidataIB');
local styles = '模組:加權文長/styles.css';

if false then	-- true = debug, false = production
	local z = require('模組:書名');
	p.cvs = z.cvs;
end

-- Auxiliaries

-- weighted_length: 搵字串長度,然後乘以 3.7
-- 目的係避免叫用 weighted page size 模
local function weighted_length (s)
	return math.floor(mw.ustring.len(s) * 3.7 + 0.5);
end

local function disambig_p( q )
	local it;
	-- Wikibase is broken, don't bother, just say nil ("we don't know")
	-- it will crash on val=Q537963 (原諒), and pcall will not catch it
	return it;
end

local function get_wikidata_label (q)
	local it;
	if q then
		it = WikidataIB.getLabel({
						getParent = function ()
							return { args = {}; };
						end;
						args = {
							q;
						};
					});
	end
	return it;
end

local function get_wiki_link (q)
	local it;
	if q then
		it = WikidataIB.getSiteLink({
						getParent = function ()
							return { ['args'] = {}; };
						end;
						args = {
							qid = q;
						};
					});
	end
	return it;
end

-- mux: 解析 frame 入面嘅參數,然後叫用 f 函數,當中 f 函數叫用 「加權文長」 模或者做相同嘅嘢
-- 由 p.wrap factorize 而來
local function mux (frame, f)
	local parent = frame:getParent();
	local it = '';
	local q;

	-- 原模淨收一個參數(維基數據數碼)
	for _, a in pairs({frame, parent}) do
		if a then
			for k, v in pairs(a.args) do
				v = v:gsub('^%s+', ''):gsub('%s+$', '');
				if type(k) == 'number' then
					if q then
						error('唔應該有參數' .. k .. ',因為已經指定咗維基數據號碼係 「' .. q .. '」');
					elseif not v:match('^Q%d+$') then
						error('參數' .. k .. ' 格式錯誤,唔係維基數據號碼');
					else
						q = v;
					end
				else
					error('不明參數 「' .. k .. '」');
				end
			end
		end
	end
	if not q then
		error('冇指定維基數據號碼');
	end

	-- 模擬{{#invoke:WikidataIB|getLabel|Qxxxx}},攞維基數據名
	local label = get_wikidata_label(q);

	-- 執行 f 函數
	it = f(q, label);

	-- 加返維基數據號碼,等唔使人手數,又麻煩又易數錯
	it = it .. '<sup>[[wikidata:' .. q .. '|?]]</sup>';
	return it;
end

-- Entry point
--
-- wrap: 用 「加權文長」 模,輸出加咗維基數據連結嘅 「加權文長」 輸出
-- 原 wrapper 版;呢個唔係模組版,原因係因為 「Wikipedia:基礎文章」 入便完全跟唔��,
-- 試過好多次要人手數篇文係邊個維基數據號碼,先可以喺維基數據 check 究竟係邊樣嘢,
-- 係真係冇文定其實有但係連錯,費時失事
--
-- 因為 「Template:加權文長」 真係巨集,睇唔明啲 code,所以呢個模組只會 gen 碼出來 call 返個 macro,
-- 即係原模(加權文長)唔可以刪
--
p.wrap = function (frame)
	-- 模擬{{加權文長|{{#invoke:WikidataIB|getLabel|Qxxxx}}}}
	return mux(frame, function (q, label)
				return frame:expandTemplate({title = '加權文長', args = {label}});
			end);
end

-- Entry point
--
-- show: 模組自己計,輸出相等於加咗維基數據連結嘅 「加權文長」 輸出
-- 真模組版,按 「加權文長」 模解同 「Wikipedia:基礎文章/等級/4/藝術」 輸出重寫。
--
p.show = function (frame)
	return mux(frame, function (q, label)
				local it;
				local problem_code;
				local problem_message;
				local potential_problem;

				-- 喺維基數據搵出真嘅粵維文章名
				local title = get_wiki_link(q);
				local effective_title = title;
				local effective_q = q;
				local article;

				if not title then	-- 冇連維基數據
					problem_code = 'no-wikidata-link';
					problem_message = "冇連維基數據";

					-- 冇連維基數據都有可能有同名嘅文,要測下係咪咁
					article = mw.title.new(label);
					effective_title = label;
					effective_q = mw.wikibase.getEntityIdForTitle(label);
					potential_problem = { code = 'q-number-mismatch', message = '有文但係唔係 ' .. q };

				else
					article = mw.title.new(title);
				end

				if article then
					local content = article:getContent();

					if not content then		-- 連咗維基數據但係搵唔到篇文,理論上冇可能
						problem_code = 'no-wiki-article';
						problem_message = "連咗維基數據,但係冇文";

					else
						local metric = weighted_length(content);
						local redirect = article.redirectTarget;
						local downcased = mw.ustring.lower(content);
						local headword_class;
						local annotation_class;
						if metric <= 3000 then
							annotation_class = 'r1';
						elseif metric <= 10000 then
							annotation_class = 'r2';
						elseif metric <= 30000 then
							annotation_class = 'r3';
						else
							annotation_class = 'r4';
						end

						-- 先處理括弧入面嘅嘢,轉頭先補返標題
						it = '(<span class="gkmc-note-' .. annotation_class .. '">'
								.. mw.language.getContentLanguage():formatNum(metric)
								.. '</span>';

						if potential_problem then
							headword_class = 'gkmc-label-' .. potential_problem.code;
							it = it .. ',<span class=gkmc-note-' .. potential_problem.code .. '>'
								.. potential_problem.message .. '</span>';
						end

						if redirect then
							it = it .. ',<span class="gkmc-note-redirect">係跳轉頁,跳轉到 「[['
								.. redirect.prefixedText
								.. ']]」</span>';

						elseif disambig_p(effective_q) then
							it = it .. ',<span class="gkmc-note-disambig">係搞清楚版</span>';

						-- v. Special:邊度鏈去呢版?target=Template%3A搞清楚&namespace=10 (q.v.)
						elseif mw.ustring.match(content, '{{%s*搞清楚%s*}}')
						    or mw.ustring.match(content, '{{%s*分流%s*}}')
						    or mw.ustring.match(downcased, '{{%s*dab%s*}}')
						    or mw.ustring.match(downcased, '{{%s*disam%s*}}')
						    or mw.ustring.match(downcased, '{{%s*disambig%s*}}')
						    or mw.ustring.match(downcased, '{{%s*disambiguation%s*}}') then

							it = it .. ',<span class="gkmc-note-disambig">可能係搞清楚版</span>';
--						elseif metric <= 200 then
--							it = it .. ',<span class="gkmc-note-redirect">可能係跳轉頁</span>';
						end
						it = it .. ')';

						-- 補返標題
						local head = '';
						if headword_class then
							head = head .. '<span class="' .. headword_class .. '">';
						end
						head = head .. '[[';
						if effective_title == label then
							head = head .. label;
						else
							head = head .. effective_title .. '|' .. label;
						end
						head = head .. ']]';
						if headword_class then
							head = head .. '</span>';
						end
						it = head .. it;
					end
				end
				if problem_code and not it then
					it = '<span class="gkmc-label-' .. problem_code .. '">[['
						.. label .. ']]</span>(<span style="gkmc-note-' .. problem_code
						.. '" title="' .. problem_message .. '">無呢版</span>)'
				end

				-- request our style sheet
				it = table.concat ({ frame:extensionTag('templatestyles', '', {src=styles}), it });
				return it;
			end);
end

return p;