Модуль:Autosorting: различия между версиями
Buto (обсуждение | вклад) Новая страница: «require( 'strict' ) local p = {} local mwLang = mw.getContentLanguage() local getArgs = require( 'Module:Arguments' ).getArgs local config = mw.loadData( 'Module:Autosorting/config' ) local currentTitle = mw.title.getCurrentTitle() local function isEmpty( val ) return val == nil or val == '' end -- Получить название категории local function getCategoryName( str, name, ... ) if isEmpty( name ) then return error( 'Autosorting:...» |
Buto (обсуждение | вклад) Нет описания правки |
||
| Строка 5: | Строка 5: | ||
local getArgs = require( 'Module:Arguments' ).getArgs | local getArgs = require( 'Module:Arguments' ).getArgs | ||
local config = mw.loadData | -- Безопасная загрузка конфига (чтобы не было ошибки, если файла конфига нет на сервере) | ||
local hasConfig, config = pcall(mw.loadData, 'Module:Autosorting/config') | |||
if not hasConfig then | |||
config = { limits = {} } | |||
end | |||
local currentTitle = mw.title.getCurrentTitle() | local currentTitle = mw.title.getCurrentTitle() | ||
| Строка 51: | Строка 55: | ||
end | end | ||
return propertyLimits[ name ] or propertyLimits.default | return propertyLimits[ name ] or propertyLimits.default or -1 | ||
end | end | ||
| Строка 73: | Строка 77: | ||
end | end | ||
-- | -- БЕЗОПАСНАЯ ФУНКЦИЯ-ЗАГЛУШКА | ||
-- Так как базы Викиданных на локальном сервере нет, мы всегда возвращаем пустой массив. | |||
-- Это заставит модуль использовать только локальные дефолтные параметры (например, default-type) без выдачи ошибок. | |||
local function getWikidataProperty( frame, property, entityId, num ) | local function getWikidataProperty( frame, property, entityId, num ) | ||
return {} | |||
end | end | ||
| Строка 158: | Строка 124: | ||
end | end | ||
if validCatsCounter >= 3 then | if validCatsCounter >= 3 then | ||
break | break | ||
| Строка 165: | Строка 130: | ||
end | end | ||
local args = getArgs( frame ) | local args = getArgs( frame ) | ||
local defaultOccupation = args[ 'default-occupation' ] | local defaultOccupation = args[ 'default-occupation' ] | ||
| Строка 178: | Строка 141: | ||
end | end | ||
if result == '' and occupationExists then | if result == '' and occupationExists then | ||
catName = getCategoryName( 'Тептар:%s (тип: человек; род занятий: не распределён)', name ) | catName = getCategoryName( 'Тептар:%s (тип: человек; род занятий: не распределён)', name ) | ||
| Строка 210: | Строка 171: | ||
for k, val in pairs( propValues ) do | for k, val in pairs( propValues ) do | ||
catName = getCategoryName( 'Тептар:%s (тип: %s)', name, mwLang:lcfirst( val ) ) | catName = getCategoryName( 'Тептар:%s (тип: %s)', name, mwLang:lcfirst( val ) ) | ||
if val == 'человек' or val == 'human' then | if val == 'человек' or val == 'human' then | ||
local occupations = p._byOccupation( frame, name, entityId ) | local occupations = p._byOccupation( frame, name, entityId ) | ||
| Строка 218: | Строка 178: | ||
result = result .. occupations | result = result .. occupations | ||
end | end | ||
validCatsCounter = validCatsCounter + 1 | validCatsCounter = validCatsCounter + 1 | ||
else | else | ||
if isValidCategory( name, catName, property ) then | if isValidCategory( name, catName, property ) then | ||
| Строка 230: | Строка 190: | ||
end | end | ||
if validCatsCounter >= 3 then | if validCatsCounter >= 3 then | ||
break | break | ||
| Строка 236: | Строка 195: | ||
end | end | ||
if result == '' and not isEmpty( defaultType ) then | if result == '' and not isEmpty( defaultType ) then | ||
if defaultType == 'человек' and not isEmpty( defaultOccupation ) then | if defaultType == 'человек' and not isEmpty( defaultOccupation ) then | ||
catName = getCategoryName( 'Тептар:%s (тип: человек; род занятий: %s)', name, defaultOccupation ) | catName = getCategoryName( 'Тептар:%s (тип: человек; род занятий: %s)', name, defaultOccupation ) | ||
if isValidCategory( name, catName, property ) then | if isValidCategory( name, catName, property ) then | ||
result = result .. getCategory( catName, key ) | result = result .. getCategory( catName, key ) | ||
else | else | ||
result = result .. getCategory( 'Тептар:%s (тип: человек)', key, name ) | result = result .. getCategory( 'Тептар:%s (тип: человек)', key, name ) | ||
end | end | ||
elseif defaultType == 'человек' then | elseif defaultType == 'человек' then | ||
result = result .. getCategory( 'Тептар:%s (тип: человек)', key, name ) | result = result .. getCategory( 'Тептар:%s (тип: человек)', key, name ) | ||
else | else | ||
catName = getCategoryName( 'Тептар:%s (тип: %s)', name, defaultType ) | catName = getCategoryName( 'Тептар:%s (тип: %s)', name, defaultType ) | ||
if isValidCategory( name, catName, property ) then | if isValidCategory( name, catName, property ) then | ||
| Строка 258: | Строка 213: | ||
end | end | ||
local defaultCatName = getCategoryName( 'Тептар:%s (не распределённые по типам)', name ) | local defaultCatName = getCategoryName( 'Тептар:%s (не распределённые по типам)', name ) | ||
if result == '' and not isEmpty( defaultCatKey ) then | if result == '' and not isEmpty( defaultCatKey ) then | ||
| Строка 360: | Строка 314: | ||
frame = frame or mw.getCurrentFrame() | frame = frame or mw.getCurrentFrame() | ||
if file ~= nil and mw.text.trim( file ) == '-' then | if file ~= nil and mw.text.trim( file ) == '-' then | ||
file = nil | file = nil | ||
end | end | ||
if not isEmpty( file ) then | if not isEmpty( file ) then | ||
local success, title = pcall( mw.title.new, 'File:' .. file ) | local success, title = pcall( mw.title.new, 'File:' .. file ) | ||
if success and not isEmpty( title ) and title.exists then | if success and not isEmpty( title ) and title.exists then | ||
return nil | return nil | ||
end | end | ||
local catName = 'Тептар:Статьи с изображениями: | local catName = 'Тептар:Статьи с изображениями: добавить локально' | ||
local result = '' | local result = '' | ||
local p18, p18Category = getFileCategory( frame, catName, 'p18', entityId ) | local p18, p18Category = getFileCategory( frame, catName, 'p18', entityId ) | ||
| Строка 388: | Строка 339: | ||
end | end | ||
for _, val in pairs( localFileProps ) do | for _, val in pairs( localFileProps ) do | ||
local propValue = getWikidataProperty( frame, val, entityId, 3 ) | local propValue = getWikidataProperty( frame, val, entityId, 3 ) | ||
| Строка 396: | Строка 346: | ||
end | end | ||
local catName = 'Тептар:Статьи без изображений' | |||
local catName = 'Тептар:Статьи без изображений | |||
local result = p._byCountry( frame, 'статьи без изображений', entityId ) | local result = p._byCountry( frame, 'статьи без изображений', entityId ) | ||
| Строка 429: | Строка 378: | ||
end | end | ||
local uses = args[ 'uses' ] | local uses = args[ 'uses' ] | ||
if isEmpty( uses ) then | if isEmpty( uses ) then | ||