Изменения
исправление категорий «нет изображений»
local config = mw.loadData( 'Module:Autosorting/config' )
local config = mw.loadData( 'Module:Autosorting/config' )
local currentTitle = mw.title.getCurrentTitle()
local function isEmpty( val )
local function isEmpty( val )
end
end
-- Получить назвние категории
-- Получить название категории
local function getCategoryName( str, name, val )
local function getCategoryName( str, name, ... )
if isEmpty( name ) then
return string.format( str, mwLang:ucfirst( name ), val )
return error( 'Autosorting: getCategoryName без name' )
end
return string.format( str, mwLang:ucfirst( name ), unpack( arg ) )
end
-- Получить категорию
local function getCategory( name, key, ... )
if isEmpty( name ) then
return error( 'Autosorting: getCategory без name' )
end
local catKey = ''
if not isEmpty( key ) then
catKey = string.format( '|%s %s', key, currentTitle.text )
end
if #arg > 0 then
local title = mwLang:ucfirst( arg[ 1 ] )
arg[ 1 ] = nil
name = string.format( name, title, unpack( arg ) )
end
return string.format( '[[Category:%s%s]]', name, catKey )
end
end
-- Получить num (по умолчанию все) первых значений из свойства в Викиданных
-- Получить num (по умолчанию все) первых значений из свойства в Викиданных
local function getWikidataProperty( frame, property, entityId, num )
local function getWikidataProperty( frame, property, entityId, num )
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
if isEmpty( property ) then
return error( 'Autosorting: getWikidataProperty без property' )
if not isEmpty( entityId ) then
end
end
property = string.upper( property )
local success, result = pcall( frame.callParserFunction, frame, '#property', callArgs )
if isEmpty( entityId ) then
if success and not isEmpty( result ) then
entityId = mw.wikibase.getEntityIdForCurrentPage()
end
local result = {}
local success, statements = pcall( mw.wikibase.getBestStatements, entityId, property )
if success then
for i, propVal in ipairs( statements ) do
if not isEmpty( num ) and #result >= num then
break
end
local val = propVal[ 'mainsnak' ]
local hasValue = val[ 'snaktype' ] == 'value'
-- Media file values: P18 etc.
if hasValue and val[ 'datatype' ] == 'commonsMedia' then
local value = val[ 'datavalue' ][ 'value' ]
if not isEmpty( value ) then
table.insert( result, value )
end
end
-- Link values: P17 etc.
if hasValue and val[ 'datatype' ] == 'wikibase-item' then
local valId = val[ 'datavalue' ][ 'value' ][ 'id' ]
local success, label = pcall( mw.wikibase.getLabel, valId )
if success and not isEmpty( label ) then
table.insert( result, label )
end
end
end
end
end
end
return {}
return result
end
end
local propValues = getWikidataProperty( frame, property, entityId, 3 )
local propValues = getWikidataProperty( frame, property, entityId, 3 )
local instanceOf = getWikidataProperty( frame, 'p31', entityId, 3 )
local instanceOf = getWikidataProperty( frame, 'p31', entityId, 3 )
instanceOf = #instanceOf > 0 and instanceOf[ 1 ] or ''
instanceOf = #instanceOf > 0 and instanceOf[ 1 ] or ''
local result = string.format( '[[Category:%s|%s%s]]', catName, instanceOf, pageTitle )
local result = getCategory( name, instanceOf, property )
return #propValues, result
return #propValues, result
end
end
-- Сортировка по профессиям
-- Сортировка по профессиям
function p._byOccupation( frame, name, entityId )
function p._byOccupation( frame, name, entityId, key )
if isEmpty( name ) then
if isEmpty( name ) then
return ''
return ''
local occupationExists = false
local occupationExists = false
local validCatsCounter = 0
local validCatsCounter = 0
for key, val in pairs( propValues ) do
for k, val in pairs( propValues ) do
local value = mwLang:lcfirst( val )
local value = mwLang:lcfirst( val )
catName = getCategoryName( 'Википедия:%s (тип: человек; род занятий: %s)', name, value )
catName = getCategoryName( 'Википедия:%s (тип: человек; род занятий: %s)', name, value )
if isValidCategory( name, catName, property ) then
if isValidCategory( name, catName, property ) then
result = result .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
validCatsCounter = validCatsCounter + 1
validCatsCounter = validCatsCounter + 1
end
end
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 .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
end
end
end
end
catName = getCategoryName( 'Википедия:%s (тип: человек; род занятий: не распределён)', name )
catName = getCategoryName( 'Википедия:%s (тип: человек; род занятий: не распределён)', name )
if isValidCategory( name, catName, property ) then
if isValidCategory( name, catName, property ) then
result = result .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
end
end
end
end
-- Сортировка по типам
-- Сортировка по типам
function p._byType( frame, name, entityId )
function p._byType( frame, name, entityId, key )
if isEmpty( name ) then
if isEmpty( name ) then
return ''
return ''
local catName = ''
local catName = ''
local validCatsCounter = 0
local validCatsCounter = 0
for key, 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 ) )
-- TODO: переделать на получение Q-элементов, чтобы не зависеть от языка
-- TODO: переделать на получение Q-элементов, чтобы не зависеть от языка
local occupations = p._byOccupation( frame, name, entityId )
local occupations = p._byOccupation( frame, name, entityId )
if isEmpty( occupations ) then
if isEmpty( occupations ) then
result = result .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
else
else
result = result .. occupations
result = result .. occupations
else
else
if isValidCategory( name, catName, property ) then
if isValidCategory( name, catName, property ) then
result = result .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
validCatsCounter = validCatsCounter + 1
validCatsCounter = validCatsCounter + 1
else
else
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 .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
else
else -- человек, есть занятие, но категория для занятия не прошла проверки
-- человек, есть занятие, но категория для занятия не прошла проверки
result = result .. getCategory( 'Википедия:%s (тип: человек)', key, name )
end
end
elseif defaultType == 'человек' then
elseif defaultType == 'человек' then -- человек, нет занятия
-- человек, нет занятия
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
result = result .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
end
end
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
if not isEmpty( key ) then
result = result .. string.format( '[[Category:%s|%s%s]]', defaultCatName, defaultCatKey, pageTitle )
defaultCatKey = key .. defaultCatKey
end
result = result .. getCategory( defaultCatName, defaultCatKey )
end
end
if result == '' then
if result == '' then
return getCategoryName( '[[Category:Википедия:%s (тип: не указан)]]', name )
return getCategory( 'Википедия:%s (тип: не указан)', key, name )
end
end
return result
return result
end
-- Шаблон сортировки по типам
function p.byType( frame )
local args = getArgs( frame )
local name = args[ 1 ]
local key = args[ 'key' ]
local entityId = args[ 'from' ]
if isEmpty( name ) or not isEmpty( args.nocat ) then
return nil
end
if mw.ustring.find( name, 'статьи' ) and currentTitle.namespace ~= 0 then
return nil
end
return p._byType( frame, name, entityId, key )
end
end
-- Сортировка по АТЕ
-- Сортировка по АТЕ
function p._bySubdivision( frame, name, entityId )
function p._bySubdivision( frame, name, entityId, key )
if isEmpty( name ) then
if isEmpty( name ) then
return ''
return ''
local result = ''
local result = ''
for key, val in pairs( propValues ) do
for k, val in pairs( propValues ) do
local catName = getCategoryName( 'Википедия:%s (АТЕ: %s)', name, val )
local catName = getCategoryName( 'Википедия:%s (АТЕ: %s)', name, val )
if isValidCategory( name, catName, property, false ) then
if isValidCategory( name, catName, property, false ) then
result = result .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
end
end
end
end
-- Сортировка по странам
-- Сортировка по странам
function p._byCountry( frame, name, entityId )
function p._byCountry( frame, name, entityId, key )
if isEmpty( name ) then
if isEmpty( name ) then
return ''
return ''
local result = ''
local result = ''
for key, val in pairs( propValues ) do
for k, val in pairs( propValues ) do
if isEmpty( val ) then
if isEmpty( val ) then
break
break
local catName = getCategoryName( 'Википедия:%s (страна: %s)', name, val )
local catName = getCategoryName( 'Википедия:%s (страна: %s)', name, val )
result = result .. string.format( '[[Category:%s]]', catName )
result = result .. getCategory( catName, key )
if isValidCategory( name, catName, property, false ) then
if isValidCategory( name, catName, property, false ) then
result = result .. p._bySubdivision( frame, name, entityId )
result = result .. p._bySubdivision( frame, name, entityId, key )
end
end
end
end
return result
return result
end
-- Шаблон сортировки по странам
function p.byCountry( frame )
local args = getArgs( frame )
local name = args[ 1 ]
local key = args[ 'key' ]
local entityId = args[ 'from' ]
if isEmpty( name ) or not isEmpty( args.nocat ) then
return nil
end
if mw.ustring.find( name, 'статьи' ) and mw.title.getCurrentTitle().namespace ~= 0 then
return nil
end
return p._byCountry( frame, name, entityId, key )
end
end
return result
return result
end
end