Модуль:WD: различия между версиями
Перейти к навигации
Перейти к поиску
Buto (обсуждение | вклад) Нет описания правки |
Buto (обсуждение | вклад) Нет описания правки |
||
| Строка 80: | Строка 80: | ||
local MSG_NO_LABEL = "нет метки"; | local MSG_NO_LABEL = "нет метки"; | ||
local MSG_NO_DESCRIPTION = "нет описания"; | local MSG_NO_DESCRIPTION = "нет описания"; | ||
-- | -- | ||
---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ||
| Строка 233: | Строка 225: | ||
end; | end; | ||
end; | end; | ||
return ""; | return ""; | ||
end; | end; | ||
-- | -- | ||
| Строка 241: | Строка 233: | ||
-- | -- | ||
---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ||
-- Возвращает метку элемента или свойства | -- Возвращает метку элемента или свойства (БЕЗ ОШИБОК) | ||
-- | -- | ||
function wd.label (frame) | function wd.label (frame) | ||
local s_id = tostring(frame.args[1]); | local s_id = tostring(frame.args[1] or ""); | ||
local s_lang = tostring(frame.args[2]); | local s_lang = tostring(frame.args[2] or ""); | ||
local entity = get_entity_safe(s_id); | local entity = get_entity_safe(s_id); | ||
if entity == nil then | if entity == nil then | ||
return s_id; -- Возвращаем ID без ошибки | |||
end; | end; | ||
return wd.get_label(entity, s_lang, MSG_NO_LABEL); | return wd.get_label(entity, s_lang, MSG_NO_LABEL); | ||
| Строка 260: | Строка 246: | ||
-- | -- | ||
function wd.description (frame) | function wd.description (frame) | ||
local s_id = tostring(frame.args[1]); | local s_id = tostring(frame.args[1] or ""); | ||
local s_lang = tostring(frame.args[2]); | local s_lang = tostring(frame.args[2] or ""); | ||
local entity = get_entity_safe(s_id); | local entity = get_entity_safe(s_id); | ||
if entity == nil then | if entity == nil then | ||
return ""; -- Возвращаем пустоту без ошибки | |||
end; | end; | ||
return wd.get_description(entity, s_lang, MSG_NO_DESCRIPTION); | return wd.get_description(entity, s_lang, MSG_NO_DESCRIPTION); | ||
| Строка 276: | Строка 256: | ||
-- | -- | ||
function wd.datatype (frame) | function wd.datatype (frame) | ||
local s_id = tostring(frame.args[1]); | local s_id = tostring(frame.args[1] or ""); | ||
local entity = get_entity_safe(s_id); | local entity = get_entity_safe(s_id); | ||
if | if entity == nil then | ||
return ""; | |||
end; | end; | ||
return entity.datatype; | return entity.datatype or ""; | ||
end; | end; | ||
-- | -- | ||
function wd.sitelink (frame) | function wd.sitelink (frame) | ||
local s_item_id = tostring(frame.args[1]); | local s_item_id = tostring(frame.args[1] or ""); | ||
local s_code = tostring(frame.args[2]); | local s_code = tostring(frame.args[2] or ""); | ||
local entity = get_entity_safe(s_item_id); | local entity = get_entity_safe(s_item_id); | ||
if | if entity == nil then | ||
return ""; | |||
end; | end; | ||
local sitelink = nil; | local sitelink = nil; | ||
local s_lang = frame.args[3]; | local s_lang = frame.args[3]; | ||
if s_lang == nil then | if s_lang == nil then | ||
sitelink = wd.get_sitelink(entity, s_code); | sitelink = wd.get_sitelink(entity, s_code); | ||
else | else | ||
local langs = {}; | local langs = {}; | ||
langs[3] = tostring(s_lang); | langs[3] = tostring(s_lang); | ||
local n_index = 4; | local n_index = 4; | ||
| Строка 308: | Строка 288: | ||
end; | end; | ||
if sitelink == nil then | if sitelink == nil then | ||
return | return ""; | ||
else | else | ||
return wd.map_sitelink_to_prefix(sitelink.site) .. sitelink.title; | return wd.map_sitelink_to_prefix(sitelink.site) .. sitelink.title; | ||
| Строка 314: | Строка 294: | ||
end; | end; | ||
-- | -- | ||
function wd.get_multilingual_sitelink(entity, s_suffix, lang_properties) | function wd.get_multilingual_sitelink(entity, s_suffix, lang_properties) | ||
if (entity == nil) or (entity["sitelinks"] == nil) then | if (entity == nil) or (entity["sitelinks"] == nil) then | ||
| Строка 324: | Строка 303: | ||
elseif lang_properties == nil then | elseif lang_properties == nil then | ||
return nil; | return nil; | ||
end; | end; | ||
return nil; | return nil; | ||
| Строка 341: | Строка 308: | ||
-- | -- | ||
---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ||
-- Возвращает строку вида: [[:d:Property:Pxxx|<метка> (Pxxx)]] | -- Возвращает строку вида: [[:d:Property:Pxxx|<метка> (Pxxx)]] (БЕЗ ОШИБОК) | ||
-- | -- | ||
function wd.format_property (frame) | function wd.format_property (frame) | ||
local s_property_id = tostring(frame.args[1]); | local s_property_id = tostring(frame.args[1] or ""); | ||
local s_lang = tostring(frame.args[2]); | local s_lang = tostring(frame.args[2] or ""); | ||
local entity = get_entity_safe(s_property_id); | local entity = get_entity_safe(s_property_id); | ||
if | if entity == nil then | ||
return s_property_id; -- Просто возвращает P301 или P1753 вместо красной ошибки | |||
end; | end; | ||
return "[[:d:Property:" .. entity.id .. "|" .. wd.get_label(entity, s_lang, MSG_NO_LABEL) .. " (" .. entity.id .. ")]]"; | return "[[:d:Property:" .. entity.id .. "|" .. wd.get_label(entity, s_lang, MSG_NO_LABEL) .. " (" .. entity.id .. ")]]"; | ||
| Строка 354: | Строка 321: | ||
-- | -- | ||
---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ||
-- Возвращает строку вида: [[:d:Qxxx|<метка> (Qxxx)]] | -- Возвращает строку вида: [[:d:Qxxx|<метка> (Qxxx)]] (БЕЗ ОШИБОК) | ||
-- | -- | ||
function wd.format_item (frame) | function wd.format_item (frame) | ||
local s_item_id = tostring(frame.args[1]); | local s_item_id = tostring(frame.args[1] or ""); | ||
local s_lang = tostring(frame.args[2]); | local s_lang = tostring(frame.args[2] or ""); | ||
local entity = get_entity_safe(s_item_id); | local entity = get_entity_safe(s_item_id); | ||
if | if entity == nil then | ||
return s_item_id; -- Возвращаем ID | |||
end; | end; | ||
return "[[:d:" .. entity.id .. "|" .. wd.get_label(entity, s_lang, MSG_NO_LABEL) .. " (" .. entity.id .. ")]]"; | return "[[:d:" .. entity.id .. "|" .. wd.get_label(entity, s_lang, MSG_NO_LABEL) .. " (" .. entity.id .. ")]]"; | ||
| Строка 367: | Строка 334: | ||
-- | -- | ||
---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ||
-- Сервисная функция | -- Сервисная функция | ||
-- | -- | ||
function wd.table_to_string(t, s_indent) | function wd.table_to_string(t, s_indent) | ||
| Строка 374: | Строка 341: | ||
for key, value in pairs(t) do | for key, value in pairs(t) do | ||
if b_first then | if b_first then | ||
s_result = s_result .. "<br/>"; | s_result = s_result .. "<br/>"; | ||
b_first = false; | b_first = false; | ||
else | else | ||
s_result = s_result .. ",<br/>"; | s_result = s_result .. ",<br/>"; | ||
end; | end; | ||
local s_str = ""; | local s_str = ""; | ||
| Строка 386: | Строка 353: | ||
elseif type(value) == "string" then | elseif type(value) == "string" then | ||
s_str = "\"" .. value .. "\""; | s_str = "\"" .. value .. "\""; | ||
else | else | ||
s_str = tostring(value); | s_str = tostring(value); | ||
end; | end; | ||
| Строка 396: | Строка 363: | ||
end; | end; | ||
if not b_first then | if not b_first then | ||
s_result = s_result .. "<br/>"; | s_result = s_result .. "<br/>"; | ||
end; | end; | ||
return s_result .. s_indent .. "}"; | return s_result .. s_indent .. "}"; | ||
| Строка 402: | Строка 369: | ||
-- | -- | ||
---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | ||
-- | -- Отладочные функции (БЕЗ ОШИБОК) | ||
-- | -- | ||
function wd.dump_entity(frame) | function wd.dump_entity(frame) | ||
local s_id = tostring(frame.args[1]); | local s_id = tostring(frame.args[1] or ""); | ||
local entity = get_entity_safe(s_id); | local entity = get_entity_safe(s_id); | ||
if entity == nil then | if entity == nil then return "" end; | ||
return wd.table_to_string(entity, ""); | return wd.table_to_string(entity, ""); | ||
end; | end; | ||
-- | -- | ||
function wd.dump_sitelinks(frame) | function wd.dump_sitelinks(frame) | ||
local s_id = tostring(frame.args[1]); | local s_id = tostring(frame.args[1] or ""); | ||
local entity = get_entity_safe(s_id); | local entity = get_entity_safe(s_id); | ||
if | if entity == nil or entity["sitelinks"] == nil then return "" end; | ||
return wd.table_to_string(entity.sitelinks, ""); | |||
end; | end; | ||
-- | -- | ||
function wd.dump_statements(frame) | function wd.dump_statements(frame) | ||
return "" -- Полностью заглушили, чтобы не вызывало ошибок поиска claim | |||
end; | end; | ||
return wd; | return wd; | ||
Текущая версия от 15:32, 11 октября 2026
Для документации этого модуля может быть создана страница Модуль:WD/doc
local wd = {
--
----------------------------------------------------------------------------------------------------------
-- Константы ET_xxx (entity types)
--
ET_ITEM = "item",
ET_PROPERTY = "property",
ET_STATEMENT = "statement",
--
----------------------------------------------------------------------------------------------------------
-- Константы DT_xxx (data types)
--
DT_ITEM = "wikibase-item",
DT_PROPERTY = "wikibase-property",
DT_DATETIME = "time",
DT_COMMONS = "commonsMedia",
DT_STRING = "string",
DT_URL = "url",
DT_ML_STRING = "monolingualtext",
DT_COORD = "globe-coordinate",
DT_QUANTITY = "quantity",
--
----------------------------------------------------------------------------------------------------------
-- Константы VT_xxx (value types)
--
VT_ENTITY_ID = "wikibase-entityid",
VT_DATETIME = "time",
VT_COORD = "globecoordinate",
VT_STRING = "string",
VT_ML_STRING = "monolingualtext",
VT_QUANTITY = "quantity",
--
----------------------------------------------------------------------------------------------------------
-- Константы ST_xxx (snak types)
--
ST_VALUE = "value", -- "конкретное значение"
ST_UNKNOWN = "somevalue", -- "значение неизвестно"
ST_NO_VALUE = "novalue", -- "значение отсутствует"
--
----------------------------------------------------------------------------------------------------------
-- Константы RANK_xxx (ranks)
--
RANK_NORNAL = "normal", -- "нормальный ранг"
RANK_PREFERRED = "preferred", -- "предпочтительный ранг"
RANK_DEPRECATED = "deprecated", -- "нерекомендуемый ранг"
--
----------------------------------------------------------------------------------------------------------
-- Константы TP_xxx (time precision)
--
TP_10E8_YEARS = 1, -- 100 млн лет
TP_10E7_YEARS = 2, -- 10 млн лет
TP_10E6_YEARS = 3, -- 1 млн лет
TP_10E5_YEARS = 4, -- 100 тыс лет
TP_10E4_YEARS = 5, -- 10 тыс лет
TP_10E3_YEARS = 6, -- 1 тыс лет
TP_CENTURY = 7, -- 100 лет
TP_DECADE = 8, -- 10 лет
TP_YEAR = 9, -- 1 год
TP_MONTH = 10, -- 1 месяц
TP_DAY = 11, -- 1 день
--
----------------------------------------------------------------------------------------------------------
-- Константы CM_xxx (calendar model)
--
CM_GREGORIAN = "http://www.wikidata.org/entity/Q1985727", -- пролептический григорианский календарь
CM_JULIAN = "http://www.wikidata.org/entity/Q1985786", -- пролептический юлианский календарь
--
----------------------------------------------------------------------------------------------------------
-- Константа GLOBE_xxx (небесное тело, к которому относятся координаты)
--
GLOBE_EARTH = "http://www.wikidata.org/entity/Q2", -- Земля
GLOBE_MOON = "http://www.wikidata.org/entity/Q405" -- Луна
--
};
--
----------------------------------------------------------------------------------------------------------
-- Константы
--
local LANG_EN = "en";
local MSG_NO_LABEL = "нет метки";
local MSG_NO_DESCRIPTION = "нет описания";
--
----------------------------------------------------------------------------------------------------------
-- БЕЗОПАСНАЯ ФУНКЦИЯ ПОЛУЧЕНИЯ ДАННЫХ (Предотвращает ошибку "a nil value")
--
local function get_entity_safe(id)
if mw.wikibase and mw.wikibase.getEntity then
return mw.wikibase.getEntity(id)
end
return nil
end
--
----------------------------------------------------------------------------------------------------------
--
function wd.is_property(entity)
return (entity ~= nil) and (entity["type"] == wd.ET_PROPERTY);
end;
--
function wd.is_item(entity)
return (entity ~= nil) and (entity["type"] == wd.ET_ITEM);
end;
--
function wd.is_statement(statement)
return (statement ~= nil) and (statement["type"] == wd.ET_STATEMENT);
end;
--
function wd.get_statement_value(statement, s_datatype, s_value_type)
if statement == nil then
return nil;
end;
local snak = statement["mainsnak"];
if (snak == nil) or (snak["datatype"] ~= s_datatype) or (snak["snaktype"] ~= wd.ST_VALUE) then
return nil;
end;
local datavalue = snak["datavalue"];
if (datavalue == nil) or (datavalue["type"] ~= s_value_type) then
return nil;
end;
return datavalue["value"];
end;
--
function wd.get_item_id(statement)
local value = wd.get_statement_value(statement, wd.DT_ITEM, wd.VT_ENTITY_ID);
if (value == nil) or (value["entity-type"] ~= wd.ET_ITEM) then
return nil;
end;
return value["numeric-id"];
end;
--
function wd.get_item_qid(statement)
local n_id = wd.get_item_id(statement);
if n_id == nil then
return nil;
else
return "Q" .. tostring(n_id);
end;
end;
--
function wd.has_valid_item_value (entity, s_property_id, n_item_id)
local claim = nil;
if (entity ~= nil) and (entity.claims ~= nil) then
claim = entity.claims[s_property_id];
end;
if claim ~= nil then
for key, value in pairs(claim) do
if (value.rank ~= wd.RANK_DEPRECATED) and (wd.get_item_id(value) == n_item_id) then
return true;
end;
end;
end;
return false;
end;
--
function wd.get_label(entity, s_lang, s_default)
local s_label = s_default;
if entity["labels"] ~= nil then
local l = entity.labels[s_lang];
if (l == nil) and (s_lang ~= LANG_EN) then
l = entity.labels[LANG_EN];
end;
if l ~= nil then
s_label = l.value;
end;
end;
return s_label;
end;
--
function wd.get_description(entity, s_lang, s_default)
local s_description = s_default;
if entity["descriptions"] ~= nil then
local l = entity.descriptions[s_lang];
if (l == nil) and (s_lang ~= LANG_EN) then
l = entity.descriptions[LANG_EN];
end;
if l ~= nil then
s_description = l.value;
end;
end;
return s_description;
end;
--
----------------------------------------------------------------------------------------------------------
-- Функции для получения ссылки на локальный проект
--
function wd.get_sitelink(entity, s_code)
if entity["sitelinks"] == nil then
return nil;
end;
return entity.sitelinks[s_code];
end;
--
function wd.get_sitelink_by_lang(entity, s_suffix, langs)
if entity["sitelinks"] == nil then
return nil;
end;
local sitelink = nil;
for key, s_lang in pairs(langs) do
sitelink = entity.sitelinks[s_lang .. s_suffix];
if sitelink ~= nil then
break;
end;
end;
return sitelink;
end;
--
local map = {
wiki = ""
};
--
local lang_map = {
wiki = ""
};
--
function wd.map_sitelink_to_prefix(s_site)
local s_iw_prefix = map[s_site];
if s_iw_prefix ~= nil then
return s_iw_prefix;
end;
local n_len = mw.ustring.len(s_site);
for s_suffix, s_iw_prefix in pairs(lang_map) do
local n_suffix_len = mw.ustring.len(s_suffix);
local n_lang_len = n_len - n_suffix_len;
if (n_lang_len > 0) and (mw.ustring.sub(s_site, -n_suffix_len) == s_suffix) then
return s_iw_prefix .. mw.ustring.sub(s_site, 1, n_lang_len) .. ":";
end;
end;
return "";
end;
--
function wd.map_sitelink (frame)
return "[["..wd.map_sitelink_to_prefix(tostring(frame.args[1])).."]]";
end;
--
----------------------------------------------------------------------------------------------------------
-- Возвращает метку элемента или свойства (БЕЗ ОШИБОК)
--
function wd.label (frame)
local s_id = tostring(frame.args[1] or "");
local s_lang = tostring(frame.args[2] or "");
local entity = get_entity_safe(s_id);
if entity == nil then
return s_id; -- Возвращаем ID без ошибки
end;
return wd.get_label(entity, s_lang, MSG_NO_LABEL);
end;
--
function wd.description (frame)
local s_id = tostring(frame.args[1] or "");
local s_lang = tostring(frame.args[2] or "");
local entity = get_entity_safe(s_id);
if entity == nil then
return ""; -- Возвращаем пустоту без ошибки
end;
return wd.get_description(entity, s_lang, MSG_NO_DESCRIPTION);
end;
--
function wd.datatype (frame)
local s_id = tostring(frame.args[1] or "");
local entity = get_entity_safe(s_id);
if entity == nil then
return "";
end;
return entity.datatype or "";
end;
--
function wd.sitelink (frame)
local s_item_id = tostring(frame.args[1] or "");
local s_code = tostring(frame.args[2] or "");
local entity = get_entity_safe(s_item_id);
if entity == nil then
return "";
end;
local sitelink = nil;
local s_lang = frame.args[3];
if s_lang == nil then
sitelink = wd.get_sitelink(entity, s_code);
else
local langs = {};
langs[3] = tostring(s_lang);
local n_index = 4;
s_lang = frame.args[3];
while s_lang ~= nil do
langs[n_index] = tostring(s_lang);
n_index = n_index + 1;
s_lang = frame.args[n_index];
end;
sitelink = wd.get_sitelink_by_lang(entity, s_code, langs);
end;
if sitelink == nil then
return "";
else
return wd.map_sitelink_to_prefix(sitelink.site) .. sitelink.title;
end;
end;
--
function wd.get_multilingual_sitelink(entity, s_suffix, lang_properties)
if (entity == nil) or (entity["sitelinks"] == nil) then
return nil;
end;
local sitelink = entity.sitelinks["ru" .. s_suffix];
if sitelink ~= nil then
return wd.map_sitelink_to_prefix(sitelink.site) .. sitelink.title;
elseif lang_properties == nil then
return nil;
end;
return nil;
end;
--
----------------------------------------------------------------------------------------------------------
-- Возвращает строку вида: [[:d:Property:Pxxx|<метка> (Pxxx)]] (БЕЗ ОШИБОК)
--
function wd.format_property (frame)
local s_property_id = tostring(frame.args[1] or "");
local s_lang = tostring(frame.args[2] or "");
local entity = get_entity_safe(s_property_id);
if entity == nil then
return s_property_id; -- Просто возвращает P301 или P1753 вместо красной ошибки
end;
return "[[:d:Property:" .. entity.id .. "|" .. wd.get_label(entity, s_lang, MSG_NO_LABEL) .. " (" .. entity.id .. ")]]";
end;
--
----------------------------------------------------------------------------------------------------------
-- Возвращает строку вида: [[:d:Qxxx|<метка> (Qxxx)]] (БЕЗ ОШИБОК)
--
function wd.format_item (frame)
local s_item_id = tostring(frame.args[1] or "");
local s_lang = tostring(frame.args[2] or "");
local entity = get_entity_safe(s_item_id);
if entity == nil then
return s_item_id; -- Возвращаем ID
end;
return "[[:d:" .. entity.id .. "|" .. wd.get_label(entity, s_lang, MSG_NO_LABEL) .. " (" .. entity.id .. ")]]";
end;
--
----------------------------------------------------------------------------------------------------------
-- Сервисная функция
--
function wd.table_to_string(t, s_indent)
local s_result = "{";
local b_first = true;
for key, value in pairs(t) do
if b_first then
s_result = s_result .. "<br/>";
b_first = false;
else
s_result = s_result .. ",<br/>";
end;
local s_str = "";
if value == nil then
s_str = "nil";
elseif type(value) == "table" then
s_str = wd.table_to_string(value, s_indent .. " ");
elseif type(value) == "string" then
s_str = "\"" .. value .. "\"";
else
s_str = tostring(value);
end;
if (type(key) == "number") or (type(key) == "boolean") then
s_result = s_result .. s_indent .. tostring(key) .. ": " .. s_str;
else
s_result = s_result .. s_indent .. "\"" .. tostring(key) .. "\": " .. s_str;
end;
end;
if not b_first then
s_result = s_result .. "<br/>";
end;
return s_result .. s_indent .. "}";
end;
--
----------------------------------------------------------------------------------------------------------
-- Отладочные функции (БЕЗ ОШИБОК)
--
function wd.dump_entity(frame)
local s_id = tostring(frame.args[1] or "");
local entity = get_entity_safe(s_id);
if entity == nil then return "" end;
return wd.table_to_string(entity, "");
end;
--
function wd.dump_sitelinks(frame)
local s_id = tostring(frame.args[1] or "");
local entity = get_entity_safe(s_id);
if entity == nil or entity["sitelinks"] == nil then return "" end;
return wd.table_to_string(entity.sitelinks, "");
end;
--
function wd.dump_statements(frame)
return "" -- Полностью заглушили, чтобы не вызывало ошибок поиска claim
end;
return wd;