-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikitrek-DTFunzioniComuni.lua
389 lines (363 loc) · 12.1 KB
/
wikitrek-DTFunzioniComuni.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
-- [P2G] Auto upload by PageToGitHub on 2024-07-18T23:32:03+02:00
-- [P2G] This code from page Modulo:wikitrek-DTFunzioniComuni
-- Keyword: wikitrek
local LabelOrLink = require('Modulo:DTBase').LabelOrLink
local p = {}
function p.ImmagineDaFile(Frame)
local item = mw.wikibase.getEntityIdForCurrentPage()
if not item then return '' end
local results = {}
local statements = mw.wikibase.getBestStatements(item, 'P3')
for _, statement in pairs(statements) do
if statement.mainsnak.snaktype == 'value' then
local Immagine = statement.mainsnak.datavalue.value
if Immagine then
results[#results + 1] = '[[File:' .. Immagine .. '|' .. Frame.args.OpzioniImmagine .. ']]'
else
results[#results + 1] = 'VUOTO'
end
else
results[#results + 1] = 'ERRORE'
end
end
return table.concat(results, ', ')
end
function p.Date(frame)
local entity = mw.wikibase.getEntity()
-- local snak = entity['claims']['P2'][1]
-- return entity['claims']['P2'][1].value
return entity.claims['P2'].value
end
function p.Network(frame)
local entity = mw.wikibase.getEntity()
local snak = entity['claims']['P2'][1]['qualifiers']['P4'][1]
return mw.wikibase.renderSnak( snak )
end
function p.URL(frame)
return mw.wikibase.getEntityUrl()
end
function p.List(frame)
local entity = mw.wikibase.getEntity()
local snak = entity['claims']['P2']
local result = ''
for index, value in next, snak do
result = result .. mw.wikibase.renderSnak(value)
end
end
function p.LinkFromPage(frame)
local item = mw.wikibase.getEntityIdForCurrentPage()
if not item then return '' end
local results = {}
local statements = mw.wikibase.getBestStatements(item, 'P7')
for _, statement in pairs(statements) do
if statement.mainsnak.snaktype == 'value' then
local value = statement.mainsnak.datavalue.value
local sitelink = value
local label = null
if sitelink then
if label then
results[#results + 1] = '[[' .. sitelink .. '|' .. label .. ']]'
else
results[#results + 1] = '[[' .. sitelink .. ']]'
end
elseif label then
results[#results + 1] = label
end
end
end
return table.concat(results, ', ')
end
--------------------------------------------------------------------------------
-- Function to convert ISO code to language name in Italian
--
-- @param isoCode The two-letters anguage code according to ISO 639-1
-- @return String with language full name
--------------------------------------------------------------------------------
function p.getLanguageName(isoCode)
-- Define the lookup table
local language_codes = {
["en"] = "Inglese",
["fr"] = "Francese",
["es"] = "Spagnolo",
["de"] = "Tedesco",
["it"] = "Italiano",
["pt"] = "Portoghese",
["ru"] = "Russo",
["zh"] = "Cinese",
["ar"] = "Arabo",
["ja"] = "Giapponese",
["ko"] = "Coreano",
["hi"] = "Hindi",
["nl"] = "Olandese",
["pl"] = "Polacco",
["sv"] = "Svedese",
["da"] = "Danese",
["no"] = "Norvegese",
["fi"] = "Finlandese",
["el"] = "Greco",
["he"] = "Ebraico",
["vi"] = "Vietnamita",
["th"] = "Tailandese",
["id"] = "Indonesiano",
["tr"] = "Turco",
["cs"] = "Ceco",
["sk"] = "Slovacco",
["hu"] = "Ungherese",
["ro"] = "Rumeno",
["bg"] = "Bulgaro",
["hr"] = "Croatiano",
["sl"] = "Sloveno",
["et"] = "Estoniano",
["lt"] = "Lituano",
["lv"] = "Lettone",
["fa"] = "Persiano",
["ms"] = "Malese",
["ca"] = "Catalano",
["gl"] = "Galiziano",
["oc"] = "Occitano",
["br"] = "Bretone",
["ga"] = "Irlandese",
["cy"] = "Gallese",
["mt"] = "Maltese",
["sq"] = "Albanese",
["mk"] = "Macedone",
["sr"] = "Serbo",
["bs"] = "Bosniaco",
["hr"] = "Croatiano",
["sh"] = "Serbo-Croato",
["uk"] = "Ucraino",
["be"] = "Bielorusso",
["kk"] = "Kazako",
["ky"] = "Kirghiso",
["uz"] = "Uzbeko",
["az"] = "Azerbaigiano",
["tk"] = "Turkmeno",
["mn"] = "Mongolo",
["tt"] = "Tataro",
["ba"] = "Bashkir",
["ce"] = "Ceceno",
["cr"] = "Croatiano",
["cs"] = "Ceco",
["da"] = "Danese",
["de"] = "Tedesco",
["el"] = "Greco",
["en"] = "Inglese",
["es"] = "Spagnolo",
["et"] = "Estoniano",
["fa"] = "Persiano",
["fi"] = "Finlandese",
["fr"] = "Francese",
["gl"] = "Galiziano",
["he"] = "Ebraico",
["hi"] = "Hindi",
["hr"] = "Croatiano",
["hu"] = "Ungherese",
["id"] = "Indonesiano",
["it"] = "Italiano",
["ja"] = "Giapponese",
["ka"] = "Georgiano",
["kk"] = "Kazako",
["ko"] = "Coreano",
["ky"] = "Kirghiso",
["lt"] = "Lituano",
["lv"] = "Lettone",
["mk"] = "Macedone",
["ms"] = "Malese",
["nl"] = "Olandese",
["no"] = "Norvegese",
["pl"] = "Polacco",
["pt"] = "Portoghese",
["ro"] = "Rumeno",
["ru"] = "Russo",
["sk"] = "Slovacco",
["sl"] = "Sloveno",
["sq"] = "Albanese",
["sr"] = "Serbo",
["sv"] = "Svedese",
["th"] = "Tailandese",
["tr"] = "Turco",
["tt"] = "Tataro",
["uk"] = "Ucraino",
["uz"] = "Uzbeko",
["vi"] = "Vietnamita",
["zh"] = "Cinese"
}
local languageName = language_codes[isoCode]
if languageName then
return languageName
else
return isoCode
end
end
--------------------------------------------------------------------------------
-- Return a specific Property from Item and/or Instance and/or
-- Instance of Instance.
-- Return the single or multiple values of the Property
--
-- @param Property The property whose values are returned
-- @param Depth How far to go on the tree: 1 - item only,
-- 2 - item and Instance,
-- 3 - item, Instance and
-- Instance of Instance
-- @param Aggregate Wether to aggregate results or return upon first match
-- @param[opt=false] SkipItem Don't return value for current item,
-- return Instance and Instance of Instance only
-- @param[opt=false] ForceString Force to return string even in case of Page
-- that should return link
-- @return Table with single value or array of values
--------------------------------------------------------------------------------
function p.PropertiesOnTree(Property, Depth, Aggregate, SkipItem, ForceString)
local CurrentItem = mw.wikibase.getEntity()
local InstanceItem = nil
local InstanceInstanceItem = nil
local ResultsArray = nil
if not CurrentItem then
CurrentItem = mw.wikibase.getEntity('Q1')
end
if Depth == nil or Depth < 1 then
Depth = 1
end
if Depth > 3 then
Depth = 3
end
if SkipItem == nil then
SkipItem = false
end
ForceString = ForceString or false
if Depth > 1 and CurrentItem['claims']['P14'] then
--Set instance of
InstanceItem = mw.wikibase.getEntity(CurrentItem['claims']['P14'][1].mainsnak.datavalue.value['id'])
if Depth > 2 and InstanceItem['claims']['P14'] then
--Set instance of instance
InstanceInstanceItem = mw.wikibase.getEntity(InstanceItem['claims']['P14'][1].mainsnak.datavalue.value['id'])
end
end
--[=[
ResultsArray[#ResultsArray + 1] = Property .. " - " .. Depth .. " - " .. tostring(Aggregate)
ResultsArray[#ResultsArray + 1] = Property .. " - " .. Depth .. " - " .. tostring(InstanceInstanceItem)
if CurrentItem.claims[Property] then
ResultsArray[#ResultsArray + 1] = LabelOrLink(CurrentItem.claims[Property][1].mainsnak.datavalue.value.id)
if not Aggregate then
return resultsArray
end
end]=]
local QList = {}
if SkipItem then
QList = {InstanceItem, InstanceInstanceItem}
else
QList = {CurrentItem, InstanceItem, InstanceInstanceItem}
end
for _, Item in pairs(QList) do
--ResultsArray[#ResultsArray + 1] = "For - " .. Item.id
if Item ~= nil and Item.claims[Property] then
local Values = Item.claims[Property]
-- Only initialize ResultsArray if the Property exist at least once along the tree
if ResultsArray == nil then
ResultsArray = {}
end
for _, SnakValue in pairs(Values) do
if SnakValue.mainsnak.datavalue.value.amount ~= nil then
--ResultsArray[#ResultsArray + 1] = string.format('%u', SnakValue.mainsnak.datavalue.value.amount)
table.insert(ResultsArray, string.format('%u', SnakValue.mainsnak.datavalue.value.amount))
elseif SnakValue.mainsnak.datavalue.value.id ~= nil then
--ResultsArray[#ResultsArray + 1] = LabelOrLink(SnakValue.mainsnak.datavalue.value.id)
table.insert(ResultsArray, LabelOrLink(SnakValue.mainsnak.datavalue.value.id, nil, nil, nil, ForceString))
else
--ResultsArray[#ResultsArray + 1] = SnakValue.mainsnak.datavalue.value
table.insert(ResultsArray, SnakValue.mainsnak.datavalue.value)
end
end
if not Aggregate then
return table.concat(ResultsArray)
end
end
end
return ResultsArray
end
function p.SeriesTree(frame)
--return table.concat(p.PropertiesOnTree("P16", 3, false), "</br>")
return p.PropertiesOnTree("P16", 3, false)
end
--------------------------------------------------------------------------------
-- Build and return the list of categories for a specific page
--
-- @param {Frame} Info from MW session
-- @return {string} List of properties in Wikitext
--------------------------------------------------------------------------------
function p.CategoryTree(frame)
local AZInstancesMember = {Q23 = "Personaggi", Q18 = "Specie", Q95 = "Pianeti", Q19 = "Cast", Q52 = "Cast"}
--local CurrentItem = mw.wikibase.getEntity()
local CurrentQ
local UpperCategories
local AZCategory = ''
local SpeciesCategory = ''
if mw.wikibase.getEntity() then
CurrentQ = mw.wikibase.getEntity().claims['P14'][1].mainsnak.datavalue.value.id
else
return ""
end
if AZInstancesMember[CurrentQ] ~= nil then
local FirstLetter
if mw.wikibase.getEntity().claims['P8'] ~= nil then
--Manual criteria has precedence
FirstLetter = string.upper(string.sub(mw.wikibase.getEntity().claims['P8'][1].mainsnak.datavalue.value, 1, 1))
else
local Label = mw.wikibase.getLabel()
if AZInstancesMember[CurrentQ] == "Personaggi" or AZInstancesMember[CurrentQ] == "Cast" then
--Person or character: process surname
local Match = string.match(Label, "[^%s]+$")
FirstLetter = string.upper(string.sub(Match, 1, 1))
else
--No person: take first letter
FirstLetter = string.upper(string.sub(mw.wikibase.getLabel(), 1, 1))
end
end
if string.find(FirstLetter, "%d") ~= nil then
FirstLetter = "0-9"
end
AZCategory = "[[Category:" .. AZInstancesMember[CurrentQ] .. " - " .. FirstLetter .. "]]"
-- Check if item has Species (P65) property
if mw.wikibase.getEntity().claims['P65'] ~= nil then
SpeciesCategory = "[[Category:" .. AZInstancesMember[CurrentQ] .. " - " .. mw.wikibase.getLabelByLang(mw.wikibase.getEntity().claims['P65'][1].mainsnak.datavalue.value.id, "it") .. "]]"
end
return (p.PropertiesOnTree("P68", 1, false) or "") .. AZCategory .. SpeciesCategory
else
UpperCategories = p.PropertiesOnTree("P68", 2, true)
if type(UpperCategories) == "table" then
return table.concat(UpperCategories)
else
return UpperCategories
end
end
end
function p.UpperCategoryTree(frame)
return p.PropertiesOnTree("P69", 1, false)
end
function p.IconTree(frame)
local ImageName
--return table.concat(p.PropertiesOnTree("P3", 3, false), "</br>")
--ImageName = p.PropertiesOnTree("P3", 3, false)[1]
ImageName = p.PropertiesOnTree("P3", 3, false)
if ImageName == nil or ImageName == '' then
local CurrentItem
CurrentItem = mw.wikibase.getEntity()
-- Takes icon from SERIES P16
if CurrentItem.claims['P16'] ~= nil then
ImageName = mw.wikibase.getEntity(CurrentItem.claims['P16'][1].mainsnak.datavalue.value.id).claims['P3'][1].mainsnak.datavalue.value
end
end
return ImageName
end
function p.SeasonTree(frame)
--return table.concat(p.PropertiesOnTree("P18", 3, false), "</br>")
return p.PropertiesOnTree("P18", 3, false)
end
function p.AffiliationTree(frame)
--return table.concat(p.PropertiesOnTree("P40", 3, false), "</br>")
return p.PropertiesOnTree("P40", 3, false)
end
function p.OperatorTree(frame)
--return table.concat(p.PropertiesOnTree("P41", 3, false), "</br>")
return p.PropertiesOnTree("P41", 3, false)
end
return p