Wikidata:Infobox Tutorial/ja

From Wikidata
Jump to navigation Jump to search

これは、ウィキデータに接続されたウィキペディア及びその他のプロジェクトのためのウィキデータを活用したinfoboxの作成方法についてのチュートリアルです。現時点ではまだ作業中ですが、議論ページ(訳注:英語版議論ページ)に疑問や希望を書き込んで構いません。

Luaの知識[edit]

このチュートリアルではLuaの基礎知識を持つ人を対象としています。以下のページはウィキメディア・エコシステム内でのLuaの利用法を示しています:

テンプレートの知識[edit]

テンプレートはこのチュートリアルの一部ではありませんが、その動作の基本は理解しておく必要があります。テンプレート言語の動作を少し調べておいてください。

このチュートリアル向けにはTemplate:Infobox (Q5626735)も目を通しておいてください。多くのウィキメディア(及び他のプロジェクト)ではModule:Infobox (Q13107716)だけを呼び出してinfoboxを作っています。詳細は下記Module:Infoboxセクションで説明します。

モジュールの知識[edit]

en-wikiで新しいモジュールを作る際にはWikitextエディタの代わりにコードエディタが開きます。

このセクションでは、ウィキペディア及び関連プロジェクトでのモジュールについて基本的な知識を説明します。

モジュール[edit]

モジュールは特別な名前空間(特別なプロパティを持つ一連のページ)です。ウィキペディアのコンテンツの名前空間がWikitextのコンテンツ7を扱うのに対して、モジュール名前空間ではLuaで書かれたあらゆるテキストを解釈します。ウィキデータを活用したinfoboxを作成するには、モジュールをいくつか作成・編集しなければならなtため、その動作を理解する必要があります。

モジュール名前空間ではScribunto-Extensionを利用しており、これによりメディアウィキ内にLuaを埋め込むことができます。このチュートリアルであなたはさほど背景知識を必要とはしませんが、詳細を勉強したい場合にはScribunto についての文書を読むことができます。

私たちにとってはモジュールページを訪問した際に少し異なるインターフェースで見ることになるということだけが重要です。もしあなたが、例えばこのページ: https://test.wikidata.org/wiki/Module:Wikidata を訪問して、「編集」ボタンを押すと、コードエディタが開きます。他のウィキペディアの言語では、モジュールとな何か、そして関連文書がどこにあるかといったことについての有用な方法を参照する(右図を参照)こともできます。

モジュールの動作を見るためのデモンストレーション用モジュール:

デバッグコンソール[edit]

デバッグコンソールを使ってLuaのステートメントを実行し、書かれたコードをチェックすることができます

下記はモジュールのページが持つデバッグコンソールのコードエディタです。コンソールでLuaコメンドを実行して、コードエディタで書いたコードをパースします。つまり、あなたは自分のコードが正しい値を返しているかどうかをチェックすることができます。例えば、コードエディタで次のように書くことができます:

local p = {}

function p.hello()
    return 'This is a hold-up, not a botany lesson.'
end

return p

そして、コンソールから次のコマンドを走らせます:

> print(p.hello())

This is a hold-up, not a botany lesson.
> print(p.hello() == True)
false

Lua is actually not disagreeing with our bold statement, but only evaluating that string == boolean is false.

別のモジュールを自分のモジュールにインポートすると、その中身も検査することができます:

local p = {}
local wd = require('Module:Wikidata')

function p.returnModule()
    return wd
end

return p

そして、コンソール内でオブジェクト(またはLuaが呼び出す表)を検査することができます:

> wd_obj = p.returnModule()
> table.foreach(wd_obj, print)
pageId	function
getImages	function
labelIn	function
getQualifierDateValue	function
...

wikitext内でのモジュールの利用[edit]

A module that already contains functions can be called anywhere in Wikitext. This is called invoking the module and uses the invoke-template (Examples: Module:Wikidata). The version of the Wikidata:Module here on Wikidata contains a function p.getLabel() (a method of the p object/table). If we invoke the function here, using {{#invoke:Wikidata|getLabel|entity=Q212730}}, it will display the following:

Output: Steven Pinker

It is easy to reconstruct that we are calling the Wikidata module and within it, the getLabel function. This function needs to know at least which item we are interested in which we pass as an argument |entity=Q212730. We are not requesting a specific language, so the Module will default to English.

モジュール上でのプレビューの使用[edit]

The preview function in Module namespace allows to view a page running the module.

Example 1:

  • On Module:NewModule write your code. The code should include an option to use an entity other than the one associated with the page (e.g. Q24045706 in the sample). You could try the below sample.
local p = {}

function p.functionname(frame)
    local frame = mw.getCurrentFrame()
	local id = frame.args.id

	local data = mw.wikibase.getEntityObject(id)
	if not data then
		return "no data"
	end
	local name = data:getLabel( "en" )
	return '<b>' .. name .. '</b>'
end

return p
  • In "Preview page with this template" add the name of the page
    • (e.g. (for the first sample above) "Wikidata:Sandbox/test1"
    • or (for here, the second sample above) "Wikidata:Infobox Tutorial/ja")
  • The red error text "Script error: No such module "NewModule"" should now be replaced by the label of Q24045706

Module:Wikidata の知識[edit]

It is also recommended to read and understand the basic workings of your projects Wikidata-Module. The modules are developed separately and can have different approaches of retrieving certain information (e.g. local-specific date formats).

Take some time to review the Module and read the module in one of the languages and projects: Module:Wikidata (Q12069631).

Wikidata also has a Module:Wikidata which we will use as an example in this section. Depending on your local module the function names might have different names, arguments and return values. Module:Wikidata can be used on any Wikitext page on Wikidata. You can find examples here (Module:Wikidata#Examples) which you can try out in your sandbox. We will continue this tutorial by using Module:Wikidata from another Lua module. You can now visit test.wikidata.org and edit this page: https://test.wikidata.org/wiki/Module:Wikidata/Test. Module:Wikidata on test.wikidata is less documented (https://test.wikidata.org/wiki/Module:Wikidata) but has similar code to the normal Wikidata. And we are less likely to break anything while learning how to create infoboxes.

The first thing we will check is if we can load the Wikidata Module:

-- https://test.wikidata.org/wiki/Module:Wikidata/Test

local p = {}
local wd = require('Module:Wikidata')

function p.returnModule()
    return wd
end

return p

And in the console we inspect the p variable:

> wd_obj = p.returnModule()
> table.foreach(wd_obj, print)

stringTable	function
sortclaims	function
_formatAndCat	function
getEntityFromId	function
formatAndCat	function
...

We see that we now have access to all the methods of Module:Wikidata.

フレームオブジェクトへのアクセス[edit]

This is also a good time to introduce the frame object (mw:Extension:Scribunto/Lua_reference_manual#Frame_object). The frame object holds among other things arguments about the Wikidata item that is connected to the page (entity) and the language of the current page (lang). We can simulate this behaviour by pasting the following code at https://test.wikidata.org/wiki/Module:Wikidata/Test:

-- https://test.wikidata.org/wiki/Module:Wikidata/Test
local p = {}
local wd = require('Module:Wikidata')

function p.getFinchLabel()
    local frame = mw.getCurrentFrame()
    frame.args.lang = 'en'
    frame.args.entity = 'Q167'
    finchLabel = wd.getLabel(frame)
    return finchLabel
end

return p

The code gets the current frame, which does not have any values in args and adds the lang and entity manually. Then we can run this command in the console:

> print(p.getFrenchLabel())
Common chaffinch

We see that we get the English label from https://test.wikidata.org/wiki/Q167 which currently reads "Common chaffinch".

Module:Infobox の知識[edit]

Similar to Module:Wikidata there is usually a Module:Infobox on each Wikipedia (and other projects). This module is also developed separately on each project, so you will have to adapt your code to work with the local module (or adapt the Module to be more similar to the others).

There are basically two implementations. The first receives the infobox structure from any Infobox template (e.g. Template:Infobox book (Q5858283)), which calls Template:Infobox (Q5626735), which invokes Module:Infobox (Q13107716) (usually calling the infobox function within it). The template is then parsed and the headers, rows and other elements are styled accordingly.

The second implementation of Module:Infobox (Q13107716) also allows the infobox to be constructed from another Lua module. An example of this is fr:Module:Infobox/Tapis persan (Persian rug). The template fr:Modèle:Infobox Tapis persan invokes the following code: {{#invoke:Infobox|build|nom=Tapis persan}}. This calls Module:Infobox and the build function in that module. It only passes the name of the infobox, which the Module:Infobox uses to call the structure at fr:Modèle:Infobox Tapis persan. This return the structure to Module:Infobox and then constructs the layout for it.

Links to the different implementations can be found here:

ウィキデータinfoboxの紹介[edit]

このチュートリアルでは様々なデータ種別とその引用を参照する簡単なinfoboxを作成する方法を示します。情報はその項目に直接接続された項目や、さらにそこらから接続された項目から来ています。

ベストプラクティス[edit]

痛風infoboxの2012年及び2016年版。右の版の方が読者にトピックの要約をより適切に提供している。

Infoboxは利用者の画面上でとても目立つ位置にあり、そのため注意深く設計する必要があります。小さな画面では特にそうで、infoboxはヘッドラインの直下にあります。infoboxが長すぎると、利用者はテキストの最初の段落にたどり着くまでに2回あるいはそれ以上スクロールしなければなりません。そのため、infoboxを再構築する場合は、更新が必要なのかどうかチェックするのは良いアイデアです。

infoboxがあらゆる種類の識別子用の領域になる以前、読者への便益が少ないことの多かった頃はとりわけそうです。右側の図は痛風infoboxが2012年から2016年のうちにどう変わったのかを示しています。もちろんこれらの変更に際してはそれぞれのコミュニティとの議論が必要です。

Help:Designing infoboxes (Q8614811)参照

伝統的なinfoboxの仕組み[edit]

InfoboxはWikitextテンプレート(二重中括弧)を使ってあらゆる他のページから呼び出すことのできるテンプレートです。en:Capital in the Twenty-First Centuryという本についてのページでサンプルを探している場合は、このコードがinfoboxの呼び出しに使われているということがわかります:

{{infobox book
| name         = Capital in the Twenty-First Century
| title_orig   = Le Capital au XXIe siècle
| translator   = [[Arthur Goldhammer]]
| image        = File:Capital in the Twenty-First Century (front cover).jpg
| caption      = <small>Hardcover edition</small>
| author       = [[Thomas Piketty]]
| language     = French
| subject      = [[Capitalism]], [[economic history]], [[economic inequality]]
| genre        = Non fiction
| publisher    = [[Éditions du Seuil]],<br />[[Harvard University Press|Belknap Press]]
| pub_date     = August 2013
| english_pub_date = April 15, 2014
| media_type   = Print ([[Hardcover|Hardback]])
| pages        = 696 pp.
| isbn =  978-0674430006
}}

中括弧がen:Template:Infobox bookを呼び出していることがわかります。キー(例:|name) と値(Capital in the Twenty-First Century) はページ自身にストアされます。テンプレートがサポートしているキーだけが使用できます。en:Template:Infobox book のコードをよく見ると、それ自身がen:Module:Infoboxに依存しているen:Template:Infoboxを使用していることがわかります。ウィキデータ以前は、これがinfoboxを情報で穴埋めする唯一の方法でした。

ウィキデータinfoboxの仕組み[edit]

ウィキデータを活用したinfoboxはページ上にストアされたデータとウィキデータの情報のミックスであるか、あるいは完全にウィキデータから生成されたものであるかのいずれかです。これは上述のInfobox-本の例が次のように見えるという意味です:

{{infobox book}}

あるいは単に最小量のローカルデータを提供:

{{infobox book
|language = French and others
}}

伝統的なinfoboxであれば、これは空もしくはそれに近いinfoboxになるでしょう。しかし、ウィキデータのinfoboxであれば、テンプレート定義内の全てのキーをストアしたり、接続されたウィキデータ項目の値を全て取得することができます。

ウィキデータinfobox の作り方[edit]

ウィキデータinfoboxの作り方はいく通りもあります。Lua内でほぼ完全に動作するものもあれば、伝統的なinfoboxテンプレートが使っているコードのほとんどを持っているものもあります。以下の2セクションでは既存のテンプレートを元にしたものとLuaでやるものとでinfoboxの構築方法を示します。

既存のテンプレートを利用したウィキデータinfobox[edit]

ウィキデータinfoboxは次のように広く既存のテンプレート関数に基いています: ウィキペディアのページがロードされると、infoboxのコードをパースして、ローカルのデータ(及び入力を認めたり、ウィキデータから取り出すキーワード)を探します。そして、(1)ウィキデータ呼び出し({{#invoke:Wikidata|..}})のためにTemplate:Infoboxをチェックし、どの値の参照が必要か確認します。各値用に(2)接続されたウィキデータ項目を呼び出すModule:Wikidata が呼び出され(3)、それはデータをウィキペディアのページに送り戻します(4)。

ウィキデータを活用したInfoboxは既存のテンプレートを利用して、ある行に対してModule:Wikidataの呼び出しを追加することで作成できます。ウィキデータの情報を参照するのには、これが最もシンプルなやり方です。テンプレート言語を使ってやれることにはもちろん制限はあります。

このアプロートでやるべきことの全ては通常{{#invoke:}}を追加することで、これは通常Module:Wikidataを呼び出し、次のように見えます: {{#invoke:Wikidata}}。モジュールを呼び出すだけでは、モジュールの特定の関数を呼び出ししない限り何もしません。関数は引数を取ることができます。{{#invoke:Wikidata|getRawValue|argument1|argument2|argument3}}。Module:Wikidata内のこの関数を見ると、それがどのように動作するかがわかります:

p.getRawValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntityObject()
		local claims
		if entity and entity.claims then claims = entity.claims[propertyID] end
		if claims then
			local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
		
			-- if number type: remove thousand separators, bounds and units
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
				result = mw.ustring.gsub(result, "(%d),(%d)", "%1%2")
				result = mw.ustring.gsub(result, "(%d)±.*", "%1")
			end
			return result
		else
			return ""
		end
	else
		return input_parm
	end
end

この関数が引数をひとつ、Frameオブジェクトを取ることがわかります。{{#invoke:}}テンプレート内で受け渡す引数はframeオブジェクト内部で送信され、最初のものはframe.args[1]でアクセスできます。そして、関数はウィキデータの値がリクエストされているかどうかをチェックします。もし値がFETCH_WIKIDATAと何か異なるものであれば、元の値(ウィキペディアのページにストアされたローカルの値)が返されます。もしウィキデータの値がリクエストされていれば、関数はウィキデータから主張を取得します。

対象となっているModule:Wikidataをまだ読んでいなかったり文書が見つからない場合は、これがちょうどその機会です(Module:Wikidata (Q12069631))。 ローカルの文書を更新したり、不明点を議論ページで議論することも忘れないでください。

ウィキデータ・オンデマンドinfobox[edit]

最も保守的なアプローチは記事の作者がその値を参照するキーワードを使っている場合にのみウィキデータの値を取得するinfoboxです。人物のinfobox はこの種のinfoboxの例です。「ソースを編集」をクリックして下にスクロールすると、その動作を見ることができます:

<!-- Template:Infobox person/Wikidata -->

| label26    = Occupation
| class26    = role
| data26     = {{#invoke:Wikidata|getRawValue|P106|{{{occupation|FETCH_WIKIDATA}}}}}

infobox内にFETCH_WIKIDATAが書かれている場合のみにこれが見え、コードはウィキデータの文を取得します。

<!-- A Wikipedia Page using the template -->

{{Infobox person/Wikidata
| occupation = FETCH_WIKIDATA
}}

ウィキデータ・デフォルトのinfobox[edit]

infoboxの中には、フィルドが空だった場合にウィキデータの値を常に表示することを選ぶものもあります。これはたいていの場合、ローカルのデータが何もないが、ウィキデータの値が欲しくない場合に、フィールドを非表示にするのにこのキーワードが使われることを意味しています。上記と同じ例を使って、infoboxは次のように見えます:

<!-- Template:Infobox person/Wikidata -->

| label26    = Occupation
| class26    = role
| data26     = {{#invoke:Wikidata|getRawValue|P106|{{{occupation|}}}}}

これであらゆるウィキペディアのページ上で、次のコードがウィキデータの文を参照するようになります:

<!-- A Wikipedia Page using the template -->

{{Infobox person/Wikidata
| occupation =
}}

例:

Lua を利用したウィキデータinfobox[edit]

Lua also allows it to build infoboxes almost completely without having to deal with the Wikitext template language. This approach is a little more versatile when it comes to the data retrieval from Wikidata, but also means that more has to be learned and more Modules need to be adapted.

どのプロパティを使うべきか?[edit]

There are several lists at Wikidata:List of properties that can help you find applicable properties.

Some WikiProjects provide more targeted lists of properties (Category:Properties list in a WikiProject) and sometimes even mappings for Wikipedia infoboxes to Wikidata properties.

Sample: WikiProject Movies with its list of properties and Wikipedia infobox mapping.

To assess what properties are currently in use for a specific field, Related Properties (Q25337203) can be useful.

Sample: for a list of properties in use on items with instance of (P31) = lighthouse (Q39715), try claim[31:39715] on related_properties.php. Note that this may be slow and/or time-out.

リソース[edit]

文書化[edit]

他のチュートリアル[edit]

[edit]

Many examples can be found in this category:

For specific fields

プレゼンテーション資料[edit]