Jump to content

Extension:LinkedWiki/lua

From mediawiki.org
This page is a translated version of the page Extension:LinkedWiki and the translation is 0% complete.
This infobox is able to read, save and check data with an external knowledge base such as Wikidata. That is one of many possible use cases with the LinkedWiki Lua class.

LinkedWiki provides a Lua Scribunto interface that implements functions to access data from RDF databases with SPARQL endpoints. If Scribunto is installed in your wiki, Lua modules and wiki templates can invoke these functions and the Linkedwiki Class.

Quick start

Example 1: create an infobox that is able to save directly in a RDF database

Usually, the part Lua of LinkedWiki used in infoboxes. Here, you can see an example.

The objective of this module is to create an infobox that is able to save directly in a RDF database and where data are marked with a red flag when data are different of another RDF database such as Wikidata.

{{#invoke:Pape|infobox
| Wikidata ID = Q132845
| Bnf ID =
| Titre = Adrien IV
| Image = http://commons.wikimedia.org/wiki/Special:FilePath/Pope%20Hadrian%20IV.jpg
| Nom de naissance = Nicolas Breakspear
| Date de naissance = 1100-1-1
| Naissance précision (années) = 10
| Lieu de naissance = Abbots Langley
| Date de décès = 1159-09-1
| Lieu de décès = Anagni
| Election au pontificat = 1154-12-4
| Intronisation = 1154-12-5
| Fin du pontificat = 1159-9-1
}}

The module uses the Extension:Capiunto.

The first part saves the infobox's parameters. The second part searches the data of Wikidata. The last part builds an infobox and compare data of Wikidata with the parameters.

local p = {}

function p.infobox(f)
    local capiunto = require 'capiunto'
    local linkedwiki = require 'linkedwiki'
    linkedwiki.setCurrentFrame(mw.getCurrentFrame())

    -- init the prefix
    local rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    local rdfs = 'http://www.w3.org/2000/01/rdf-schema#'
    local ri = 'http://gregorius.fr/wiki/'
    local rp = 'http://gregorius.fr/wiki/Property:'
    local xsd = 'http://www.w3.org/2001/XMLSchema#'
    local wdt = 'http://www.wikidata.org/prop/direct/'
    local wd = 'http://www.wikidata.org/entity/'
    local p = 'http://www.wikidata.org/prop/'
    local pq = 'http://www.wikidata.org/prop/qualifier/'
    local ps = 'http://www.wikidata.org/prop/statement/'

    local dateFormat = "d M Y"

    local subject = f.args.iri or linkedwiki.getCurrentIRI();  --find the iri of the current page

    local object = linkedwiki.new(subject)
    --object:setDebug(true)
    object:removeSubject() -- delete all triples of this subject

    -- Save the data in the infobox in a knowledge base
    mw.log(object:addPropertyWithIri(rdf .. 'type', ri .. 'Pape'))
    mw.log(object:addPropertyString(rdfs .. 'label', f.args["Titre"]))
    mw.log(object:addPropertyWithIri(rp .. 'picture', f.args["Image"]))
    mw.log(object:addProperty(rp .. 'nomDeNaissance', f.args['Nom de naissance']))
    mw.log(object:addProperty(rp .. 'naissance', f.args['Date de naissance'], xsd .. 'date', ''))
    mw.log(object:addProperty(rp .. 'naissancePrecision', f.args['Naissance précision (années)'], xsd .. 'integer', ''))
    mw.log(object:addPropertyString(rp .. 'lieuDeNaissance', f.args['Lieu de naissance']))
    mw.log(object:addProperty(rp .. 'deces', f.args['Date de décès'], xsd .. 'date', ''))
    mw.log(object:addPropertyString(rp .. 'lieuDeDeces', f.args['Lieu de décès']))
    mw.log(object:addProperty(rp .. 'electionAuPontificat', f.args['Election au pontificat'], xsd .. 'date', ''))
    mw.log(object:addProperty(rp .. 'intronisation', f.args['Intronisation'], xsd .. 'date', ''))
    mw.log(object:addProperty(rp .. 'finDuPontificat', f.args['Fin du pontificat'], xsd .. 'date', ''))

    --compare data with Wikidata
    local iriWikidata = ""
    local objWikidata = nil
    local objWikidataLieuNaissance = nil
    local objWikidataLieuDeces = nil
    local objPapeFonction = nil
    local linkWikidata = ""

    if not linkedwiki.isEmpty(f.args['Wikidata ID']) then
        local idConfigWikidata = 'http://www.wikidata.org'
        local taglang = 'fr'

        iriWikidata = wd .. f.args['Wikidata ID']
        objWikidata = linkedwiki.new(iriWikidata, idConfigWikidata, taglang)
        mw.log(object:addPropertyWithIri(rp .. 'WikidataID', iriWikidata))
        objWikidataLieuNaissance = linkedwiki.new(objWikidata:getValue(wdt .. 'P19'), idConfigWikidata, taglang)
        objWikidataLieuDeces = linkedwiki.new(objWikidata:getValue(wdt .. 'P20'), idConfigWikidata, taglang)

        listIri = linkedwiki.explode(";", objWikidata:getValue(p .. 'P39'))
        objPapeFonction = nil
        for i, iri in ipairs(listIri) do
            objPapeFonction = linkedwiki.new(iri, idConfigWikidata, taglang)
            if objPapeFonction:getValue(ps .. 'P39') == wd .. "Q19546" then
                break
            end
        end

        linkWikidata = '[' .. iriWikidata .. ' ' .. f.args['Wikidata ID'] .. ']'
    end

    --Make infobox with capiunto
    local infobox = capiunto.create({
        bodyStyle = 'width : 50px',
        bodyClass = 'gregoriusPape',
        title = 'Pape',
        top = objWikidata:checkString(rdfs .. 'label', f.args.Title),
        topStyle = 'background:#FFD200;height:50px;font-size: 25px;vertical-align:middle'
    })

    infobox:addImage(objWikidata:checkImage(wdt .. 'P18', f.args["Image"], 200), "", "Image")
    infobox:addRow('Nom de naissance', objWikidata:checkString(wdt .. "P1477", f.args['Nom de naissance']))
    infobox:addRow('Lieu de naissance', objWikidataLieuNaissance:checkString(rdfs .. "label", f.args['Lieu de naissance']))
    infobox:addRow('Naissance', objWikidata:checkDate(wdt .. "P569", f.args['Date de naissance'], dateFormat))
    infobox:addRow('Lieu de décès', objWikidataLieuDeces:checkString(rdfs .. "label", f.args['Lieu de décès']))
    infobox:addRow('Date de décès', objWikidata:checkDate(wdt .. "P570", f.args['Date de décès'], dateFormat))
    infobox:addRow('Élection au pontificat', f.args['Election au pontificat'])
    infobox:addRow('Intronisation', objPapeFonction:checkDate(pq .. "P580", f.args['Intronisation'], dateFormat))
    infobox:addRow('Fin du pontificat', objPapeFonction:checkDate(pq .. "P582", f.args['Fin du pontificat'], dateFormat))
    infobox:addRow('Source Wikidata', linkWikidata)

    return infobox
end

return p

You can test your module in the lua console with this code :

frame = mw.getCurrentFrame() -- Get a frame object
newFrame = frame:newChild{ -- Get one with args
	title = 'Adrien IV' ,
 args = {
 iri = 'http://gregorius.fr/wiki/Adrien_IV' ,
["Wikidata ID"] = 'Q132845' ,
["Bnf ID"] = '' ,
["Titre"] = 'Adrien IV' ,
["Image"] = 'http://commons.wikimedia.org/wiki/Special:FilePath/Pope%20Hadrian%20IV.jpg' ,
['Nom de naissance'] = 'Nicolas Breakspear' ,
['Lieu de naissance'] = 'Abbots Langley' ,
['Date de naissance'] = '1159-09-1' ,
['Naissance précision (années)'] = '10' ,
['Lieu de décès'] = 'Anagni' ,
['Date de décès'] = '1159-09-01' ,
['Election au pontificat'] = '1154-12-4' ,
['Intronisation'] = '1154-12-5' ,
['Fin du pontificat'] = '1159-9-1'
    }
}
mw.log(p.infobox( newFrame ) )

Example 2: create a module to show the data of Wikidata via a SPARQL query

You can also use Lua of LinkedWiki to print in the wiki the result of SPARQL query. Here, you can see an example.

The objective of this module is to print the list authority notices about an author.

{{#invoke:Authority Notice|section
| Wikidata ID = Q666551
}}

Definition of "Module:Authority Notice":

local p = {}
local linkedwiki = require 'linkedwiki'
function p.section(f)
	local object =  linkedwiki.new()
    object:setConfig("http://www.wikidata.org")
    mw.log(object:getConfig())

	local result = object:query( [[
PREFIX bd: <http://www.bigdata.com/rdf#> 
PREFIX wikibase: <http://wikiba.se/ontology#> 
PREFIX wd: <http://www.wikidata.org/entity/> 
PREFIX wdt: <http://www.wikidata.org/prop/direct/> 

select DISTINCT ?propLabel ?ID ?formater # ?logo
where {
 ?prop wdt:P31/wdt:P279* wd:Q18614948 .
 ?prop  wikibase:directClaim ?propClaim .
 ?prop wdt:P1630 ?formater . 
 wd:]] .. f.args["Wikidata ID"] .. [[ ?propClaim ?ID .
  
 SERVICE wikibase:label {
       bd:serviceParam wikibase:language "fr,en" .
 }
} 
ORDER BY ?propLabel
LIMIT 100
		]])
-- linkedwiki.print_r(result)

local text = [[
== Authority Notice ==
]]
	local newline=[[

]]
	local i = 0
	local row = nill
	local link = nil
	local logo = nil
	row = result['result']['rows'][i]
	while( row ~= nil )
	do
	 --linkedwiki.print_r(row)
	  link = string.gsub(row["formater"], "$1",row["ID"])
	  text = text .. "* <span class='plainlinks module-oeuvres-vignettes'>" .. "[".. link .. " ".. row["propLabel"] .. "]".. "</span>" .. newline
	  
	  i = i + 1
	  row = result['result']['rows'][i]
	end
  return text
end
 
return p

You can test your module in the lua console with this code :

frame = mw.getCurrentFrame() -- Get a frame object
newFrame = frame:newChild{ -- Get one with args
	title = 'Alfred Baudrillart' ,
 args = { 
 iri = 'https://gregorius.dsi.universite-paris-saclay.fr/wiki/Alfred_Baudrillart' ,
["Wikidata ID"] = 'Q666551' 
    }
}
mw.log(p.section( newFrame ) )

Example 3: create a module to show the data of the BNF via a SPARQL query

Here another example with a SPARQL query with the endpoint of the w:BnF.

The objective of this module is to print the list works about an author with a numeric version available online.

{{#invoke:Author works|section
| Bnf ID = 121308651
}}

Definition of "Module:Author works":

local p = {}

function p.section(f)
    local linkedwiki = require 'linkedwiki'
	local object =  linkedwiki.new()
    object:setConfig("http://data.bnf.fr")
    mw.log(object:getConfig())

	local result = object:query( [[
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdarelationships: <http://rdvocab.info/RDARelationshipsWEMI/>

SELECT DISTINCT
  (CONCAT("<span class='plainlinks module-oeuvres-vignettes'>[",?urlPDF," ",?url,".jpg]</span>") as ?vignette)
  ?date
  (CONCAT("<span class='plainlinks'>[",?urlPDF," ",?label,"]") as ?titre) 
  (CONCAT("<span class='plainlinks'>[",?edition," notice]") as ?gallica) 
WHERE {    
  ?edition dcterms:creator <http://data.bnf.fr/ark:/12148/cb]] .. f.args["Bnf ID"] .. [[#about> .
  ?edition rdfs:label ?label .
  ?edition foaf:depiction  ?url .
  ?edition dcterms:date ?date.
  BIND(IRI(REPLACE(STR(?url),".thumbnail", ".pdf?download=1&pdfdownload__accept-box=on")) as ?urlPDF)
} 
ORDER BY  ?date
		]] 	)
-- linkedwiki.print_r(result)
	
	local text = [[
== Works ==
]]
	
	local newline=[[

]]
	local i = 0
	local row = nill
	row = result['result']['rows'][i]
	while( row ~= nil )
	do
	 --linkedwiki.print_r(row)
	  
	  text = text .. "* ".. row["vignette"] .. " ".. row["date"] .. " ".. row["titre"].." (".. row["gallica"].. ")".. newline
	  
	  i = i + 1
	  row = result['result']['rows'][i]
	end

  return text
end
 
return p

You can test your module in the lua console with this code :

frame = mw.getCurrentFrame() -- Get a frame object
newFrame = frame:newChild{ -- Get one with args
	title = 'Alfred Baudrillart' ,
 args = { 
 iri = 'https://gregorius.dsi.universite-paris-saclay.fr/wiki/Alfred_Baudrillart' ,
["Bnf ID"] = '121308651' 
    }
}
mw.log(p.section( newFrame ) )

Example 4: Loop through key/value object of a SPARQL results

Again an example with a SPARQL query but with another manner to loop through key/value object of a SPARQL results.

The objective of this module is to create links to the predecessor and the successor of a pope. (There may be several if the pope has to occupy his post several times)

{{#invoke:Carrousel|pape
| Wikidata ID = Q132845
}}

Definition of "Module:Carrousel":

local p = {}

function p.pape(f)
    local linkedwiki = require 'linkedwiki'
    linkedwiki.setCurrentFrame(mw.getCurrentFrame())
    --linkedwiki.setDebug(true)
    
   local subject = f.args.iri or linkedwiki.getCurrentIRI();
   local idConfigWikidata ='http://www.wikidata.org' 
   linkedwiki.setConfig(idConfigWikidata)

  local queryStr = [[
PREFIX p: <http://www.wikidata.org/prop/> 
PREFIX pq: <http://www.wikidata.org/prop/qualifier/> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX wd: <http://www.wikidata.org/entity/> 

select distinct ?precedent ( COALESCE(?nomPrecedent, "")  as ?nomp ) ?suivant ( COALESCE(?nomSuivant, "")  as ?noms )
where { 

    OPTIONAL {
          <http://www.wikidata.org/entity/]] .. f.args['Wikidata ID'] ..   [[> p:P39 ?stat .
          ?stat pq:P1365 ?precedent .
          ?precedent rdfs:label ?nomPrecedent .
          FILTER (langMatches(lang(?nomPrecedent), "fr"))
    }
    OPTIONAL {
          <http://www.wikidata.org/entity/]] .. f.args['Wikidata ID'] ..   [[> p:P39 ?stat .
          ?stat pq:P1366 ?suivant .
          ?suivant rdfs:label ?nomSuivant .
          FILTER (langMatches(lang(?nomSuivant), "fr"))
    }
  }
  ]]
   
  local result = linkedwiki.query(queryStr)
  --linkedwiki.print_r(result)
   
	local newline = [[

]]

  local wikitext = ''
  local rows = result['result']['rows']
  for key,value in pairs(rows) do --actualcode
    local suivantStr =  rows[key]['noms'] 
	local precedentStr =   rows[key]['nomp'] 
	wikitext = wikitext .. '{| class="wikitable" style="float:right;clear: right;width: 350px;"'..newline
          .. '|- '..newline
          .. '| style="width:150px;text-align:left;border: none;"| '
	if not linkedwiki.isEmpty(precedentStr) then
		 wikitext = wikitext .. '[['.. precedentStr .. '|< '.. precedentStr .. ']] [[File:Notification-icon-Wikidata-logo.svg|16px|link='.. rows[key]['precedent'] .. ']]'
	end
	wikitext = wikitext ..newline
          .. '| style="width:150px;text-align:right;border: none;"| '
	if not linkedwiki.isEmpty(suivantStr) then
		wikitext = wikitext .. '[[File:Notification-icon-Wikidata-logo.svg|16px|link='.. rows[key]['suivant'] .. ']] [[' .. suivantStr ..'|' .. suivantStr ..' > ]]'
	end
	wikitext = wikitext ..newline
          .. '|}'..newline
  end
  return wikitext
end

return p

You can test your module in the lua console with this code :

frame = mw.getCurrentFrame() -- Get a frame object
newFrame = frame:newChild{ -- Get one with args
 title = 'Adrien IV' ,
 args = { 
 iri = 'https://gregorius.dsi.universite-paris-saclay.fr/wiki/Adrien_VI',
["Wikidata ID"] = 'Q132845'
}
}
mw.log(p.pape( newFrame ) )

Best usage with the Linkedwiki Class

Constructor : build a new object

linkedwiki.new(subject,config=nil,tagLang=nil,debug=nil)
linkedwiki.new(subject) uses the configuration by default
linkedwiki.new(subject,config) uses the configuration of config
linkedwiki.new(subject,config,tagLang) uses the configuration of config but with the language of tagLang

Returns a LinkedWiki object .

subject : string
select the subject of triples

config : string : id of endpoint in the file extension.json
select the configuration in order to read/write or delete the triples of this object in the database.If nil, it uses the configuration by default.

tagLang : string : tag lang for example : fr or en
select the default language for the function getString and addPropertyString. If nil, it uses the language in the configuration.

debug : boolean
Enable the debug mode.

Examples :

local linkedwiki = require 'linkedwiki'
local subject = linkedwiki.getCurrentIRI(); -- read the iri of the current page
local object =  linkedwiki.new(subject)
mw.log(object:getConfig()) -- show the configuration id uses by this object

Configuration

Config

obj:getConfig()

Returns configuration id (string) used by this instance.(see details about configurations)


obj:setConfig(iriConfiguration)

Returns nil. It changes the configuration of this instance.

iriConfiguration : configuration id (see details about configurations)

Example :

local linkedwiki = require 'linkedwiki'
local subject = "http://www.example.org/mysubject"; 
local object =  linkedwiki.new(subject)
mw.log(object:getConfig()) -- show the configuration id uses by this object

object:setConfig("http://www.example.org") -- change the configuration of this object
mw.log(object:getConfig()) -- show the configuration id uses by this object

Lang

obj:getLang()

Returns lang tag (string) used by this instance.


obj:setLang(tagLang)

Returns nil. It changes the lang tag of this instance.

tagLang : lang tag

Example :

local linkedwiki = require 'linkedwiki'
local subject = "http://www.example.org/mysubject"; 
local object =  linkedwiki.new(subject)
mw.log(object:getLang()) -- show the current lang tag

object:setLang("fr") -- change the configuration of this object with the french lang tag
mw.log(object:getLang())

Subject

obj:getSubject()

Returns subject (iri/string) used by this instance.


obj:setSubject(iriSubject)

Returns nil. It changes the subject of this instance.

Example :

local linkedwiki = require 'linkedwiki'
local subject = "http://www.example.org/mysubject"; 
local object =  linkedwiki.new(subject)
mw.log(object:getSubject()) -- show the current subject

object:setSubject("http://www.example.org/mysubject2") -- change the subject 
mw.log(object:getSubject())

Read data

Table with a SPARQL query

obj:query(query)

Returns a table. For example:

{
            ["result"] = {
                ["variables"] = {
                  "s",
                  "o1",
                  "o2"
                },
                ["rows"] = {
                        {
                            ["s type"] = "uri",
                            ["s"] = "http://example.org/a",
                            ["o1 type"] = "uri",
                            ["o1"] = "http://example.org/b",
                            ["o2 type"] = "uri",
                            ["o2"] = "http://example.org/b"
                        },
                        {
                            ["s type"] = "uri",
                            ["s"] = "http://example.org/a",
                            ["o1 type"] = "uri",
                            ["o1"] = "http://example.org/b",
                            ["o2 type"] = "uri",
                            ["o2"] = "http://example.org/b"
                        }
                    }
                }
}

Example :

local result = linkedwiki.query("select * where {?s ?p ?v.} limit 5")
linkedwiki.print_r(result)
mw.log(result['result']['rows']["s"])

Iri or literal without language tag

obj:getValue(iriProperty)

Returns the value IRI or literal without language tag of current subject with this property.

iriProperty : IRI of the property

Example :

local linkedwiki = require 'linkedwiki'
local wd = 'http://www.wikidata.org/entity/'
local wdt = 'http://www.wikidata.org/prop/direct/'
local subject = wd.."Q1"; 
local propertyImage = wdt.."P18"; 

local univers =  linkedwiki.new(subject,"http://www.wikidata.org")
mw.log(object:getValue(propertyImage))

literal with language tag

obj:getString(iriProperty, tagLang=nil)

Returns the literal with this language tag in function of current subject with this property.

iriProperty : IRI of the property

tagLang : lang tag. If nil, it uses the lang tag by default.

Example :

local linkedwiki = require 'linkedwiki'
local wd = 'http://www.wikidata.org/entity/'
local rdfs = 'http://www.w3.org/2000/01/rdf-schema#'
local subject = wd.."Q1"; 
local propertyLabel = rdfs.."label"; 

local univers =  linkedwiki.new(subject,"http://www.wikidata.org","fr") --uses Wikidata's config and the french tag 
mw.log(object:getString(propertyLabel))

Write data

Iri

obj:addPropertyWithIri(iriProperty, iriValue)

Returns the response of the database. It saves one IRI in function of the storage method defined in the configuration.

iriProperty : IRI of the property

iriValue : IRI of the value

Example :

local ex = 'http://example.com/ont#'
local rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
local subject = "http://www.example.org/mysubject"; 

local linkedwiki = require 'linkedwiki'
local object =  linkedwiki.new(subject)
object:addPropertyWithIri(rdf..'type',ex..'MyType') -- record one information
mw.log(linkedwiki.object:getValue(rdf..'type')) -- read this information

Literal

Value without language tag

obj:addProperty(iriProperty, value, type=nil)

Returns the response of the database. It saves one value without a lang tag in function of the storage method defined in the configuration.

iriProperty : IRI of the property

value : string or number or date or etc.

type : IRI of type (see type available in XSD in SPARQL). If nil, the database will use the SPARQL standard in order to choose the type. For example, if the value is 7, the database will use xsd:integer but with the value 7.1, it will use xsd:float or xsd:double.

Example without explicit type :

local ex = 'http://example.com/ont#'
local subject = "http://www.example.org/mysubject"; 

local linkedwiki = require 'linkedwiki'
local object =  linkedwiki.new(subject)
object:addProperty(ex..'age',35) -- record one integer
mw.log(linkedwiki.object:getValue(ex..'age')) -- read this information

Example with a date :

local ex = 'http://example.com/ont#'
local xsd = 'http://www.w3.org/2001/XMLSchema#'
local subject = "http://www.example.org/mysubject"; 

local linkedwiki = require 'linkedwiki'
local object =  linkedwiki.new(subject)
object:addProperty(ex..'birth',"1955-01-15",xsd..'date') -- record one date
mw.log(linkedwiki.object:getValue(ex..'birth')) -- read this date

Example with a double :

local ex = 'http://example.com/ont#'
local xsd = 'http://www.w3.org/2001/XMLSchema#'
local subject = "http://www.example.org/mysubject"; 

local linkedwiki = require 'linkedwiki'
local object =  linkedwiki.new(subject)
object:addProperty(ex..'price',"5",xsd..'double') -- record a price
mw.log(linkedwiki.object:getValue(ex..'price')) -- read this price


String

obj:addPropertyString(iriProperty, value, tagLang=nil)

Returns the response of the database. It saves one string with a lang tag in function of the storage method defined in the configuration.

iriProperty : IRI of the property

value : string

tagLang : language tag to use with this text. If nil, the default lang tag in the configuration will be used.

Example :

local ex = 'http://example.com/ont#'
local subject = "http://www.example.org/mysubject"; 
local rdfs = 'http://www.w3.org/2000/01/rdf-schema#'

local linkedwiki = require 'linkedwiki'
local object =  linkedwiki.new(subject)

object:addPropertyString(rdfs..'label',"Title") -- write "Title"@en if English is the language by default 
object:addPropertyString(rdfs..'label',"Title",nil) -- write "Title"@en if English is the language by default 
object:addPropertyString(rdfs..'label',"Title", "fr") -- write "Title"@fr 
object:addPropertyString(rdfs..'label',"Title", "") -- write "Title" without tag
mw.log(linkedwiki.object:getString(rdfs..'label')) -- read this information
Other cases

obj:addPropertyWithLiteral(iriProperty, value, type, tagLang)

Returns the response of the database. It saves one literal with a specific type and tag language tag in function of the storage method defined in the configuration.

Load data

..TODO Example :

local linkedwiki = require 'linkedwiki'
    local subject = linkedwiki.getCurrentIRI()
    local object =  linkedwiki.new(subject)
--local config = 'http://database-test'
--object:setConfig(config)
object:loadData(f.args.Titles)
mw.log(object:getLastQuery())

Delete data

obj:removeSubject()

Returns the response of the database. It deletes all triples with the current subject of the instance in function of the storage method defined in the configuration.

Example :

local subject = "http://www.example.org/mysubject"; 

local linkedwiki = require 'linkedwiki'
local object =  linkedwiki.new(subject)

object:removeSubject() -- delete all triples with the current subject

Functions for infoboxes

Literal without language tag

obj:checkValue(property, valueInWiki)

Returns wiki text. This function compares the value of this property with the parameter valueInWiki.

Literal with language tag

obj:checkString(property, valueInWiki, tagLang=nil)

Returns wiki text. This function compares the value of this property with the parameter valueInWiki.

tagLang Lang tag of value with this property. If nil, it uses the lang tag of the current configuration.

Label with language tag

obj:checkTitle(property, labelInWiki, tagLang)

Returns wiki text. This function compares the rdfs:label of object linked via property with the parameter labelInWiki.

tagLang Lang tag of label. If nil, it uses the lang tag of the current configuration.

obj:checkLabelOfInternLink(link, propertyOfLabel, labelInWiki, tagLang=nil)

Returns wiki text. This function inserts an intern link.

link Url of link

propertyOfLabel Property in order to read the label

labelInWiki label of link

tagLang Lang tag of label. If nil, it uses the lang tag of the current configuration.

obj:checkIriOfExternLink(labelOfExternLink, propertyOfExternLink, externLinkInWiki)

Returns wiki text. This function inserts an external link.

labelOfExternLink label of link

propertyOfLabel Property in order to read the url of link in the database

externLinkInWiki Url of link

tagLang Lang tag of label. If nil, it uses the lang tag of the current configuration.

User

obj:checkUser(property, valueInWiki, tagLang)

Returns wiki text. This function compares the list of users in the database with the parameter valueInWiki. This function creates links to the user's pages and add a link to emails when its exist in the database with the property vcard:email.

Date

obj:checkDate(property, valueInWiki, format)

Returns wiki text. This function compares the date of this property with the parameter valueInWiki.

format Format in output the date with the parser #Time

Image

obj:checkImage(property, valueInWiki)

Returns wiki text. This function compares the value of this property with the parameter valueInWiki.

Warning Warning: To use this functionality, you need to enable in your localsettings $wgUseInstantCommons = true;, $wgAllowExternalImages = true; and $wgExternalLinkTarget = '_blank';.

How to set the size of the image and give the caption ?

The function obj:checkImage returns only the wikitext of an external image to the Capiunto's infobox. You need to specify the css class when you call the Capiunto function infobox:addImage( obj:checkImage(property, valueInWiki), 'my caption', 'infobox' ). To define this class name, here "infobox", you can use the page Mediawiki:common.css of your wiki.

For example, this css code limits the size of images in the div container of Capiunto with the class infobox

.infobox img {
    max-width: 200px;
    max-height: 200px;
}

Wikidata item

obj:checkItem(property, valueInWiki, tagLang)

Returns wiki text. This function compares the list of Wikidata ID in the database with the parameter valueInWiki.

Tools

getCurrentTitle

linkedwiki.getCurrentTitle()

Returns Title of the page

getCurrentIRI

linkedwiki.getCurrentIRI()

Returns IRI of the page

isEmpty

linkedwiki.isEmpty(s)

Returns Boolean. Check if s is empty (nil or "").

explode

linkedwiki.explode(div, str)

Returns a list.

Example :

        listIri = linkedwiki.explode(";", objWikidata:getValue(p .. 'P39'))
        for i, iri in ipairs(listIri) do
            mw.log(iri)
        end

concatWithComma

linkedwiki.concatWithComma(list)

Returns a string.

Example :

        listIri = linkedwiki.explode(";", objWikidata:getValue(p .. 'P39'))
        mw.log(linkedwiki.concatWithComma(listIri))

trim

For information in Scribunto, remove the spaces before and after the text.

mw.text.trim(str)

fullUrl

For information in Scribunto, read the IRI of an article.

mw.uri.decode(mw.title.new(title):fullUrl())

Styles

In version >=3.6.0, the "check" functions add different css styles depending on the difference between the value passed in infobox parameter and the value in the DB.

The module inserts the css style:

  • mw-ext-linkedwiki-value-equal when the infobox parameter and the value in the DB are equal
  • mw-ext-linkedwiki-new-value when the infobox parameter and the value in the DB are different
  • mw-ext-linkedwiki-tooltip when the infobox parameter and the value in the DB are different and the the value in the DB already exists.

The css styles of "red flag" is defined in the file Linkedwiki/lua/css/common.css.

Restore database RDF

If you want to restore your database RDF, you can use the feature "refresh all" in the special page RDFSave (with user right data).

Debug

obj:setDebug(boolDebug)
obj:isDebug()

You can use setDebug in order to see the errors in the SPARQL queries.

obj:getLastQuery()

With this function, you can see the last SPARQL query used by the precedent function.