Module:GetFrontCoverImg

From Bodhicitta
Revision as of 15:48, 19 March 2026 by Jeremi (talk | contribs) (VSCode edit)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:GetFrontCoverImg/doc

local p = {}

function p.main(frame)
    local pArgs = frame:getParent().args
    local smw = mw.smw
    local fulltitle = mw.title.getCurrentTitle().text
    
    -- Define stock photos to skip
    local stockPhotos = {
        ['File:Noimagefound.jpg'] = true,
        ['File:Khyentse-pecha-stock.jpg'] = true,
        ['File:Sutra portrait.jpg'] = true,
        ['File:Sutra alt.jpeg'] = true,
        ['File:Dissertations.jpeg'] = true,
        ['File:Media.jpeg'] = true
    }
    
    -- Helper function to check if a value is not empty and not a stock photo
    local function hasValue(val)
        return val and val ~= '' and not stockPhotos[val]
    end
    
    -- Helper function to get FirstImage from a person page
    local function getPersonImage(people)
        if not people or people == '' then
            return nil
        end
        
        -- Convert table to semicolon-separated string if needed
        local peopleStr = people
        if type(people) == 'table' then
            peopleStr = table.concat(people, ';')
        end
        
        -- Handle empty string after conversion
        if peopleStr == '' then
            return nil
        end
        
        for person in string.gmatch(peopleStr, "([^;]+)") do
            -- Trim whitespace
            person = mw.text.trim(person)
            -- Get full page name
            person = frame:preprocess('{{SUBPAGENAME:' .. person .. '}}')
            
            local perImgQuery = table.concat({
                '[[People/' .. person .. ']]',
                '|?FirstImage#'
            })
            
            local perImgData = smw.ask(perImgQuery)
            if perImgData and #perImgData > 0 then
                local imgData = perImgData[1]
                if hasValue(imgData.FirstImage) then
                    return imgData.FirstImage
                end
            end
        end
        
        return nil
    end
    
    -- Step 1: Check if parent argument FrontCoverImg has a value
    if hasValue(pArgs.FrontCoverImg) then
        return pArgs.FrontCoverImg
    end
    
    -- Step 2: Check remote wiki data using GetData module
    -- Load the library data module
    local libraryData = mw.loadData('Module:GetData/library')
    
    -- Check for FrontCoverImg in library data
    if libraryData and libraryData.FrontCoverImg and hasValue(libraryData.FrontCoverImg) then
        return libraryData.FrontCoverImg
    end
    
    -- Check for FirstImage in library data
    if libraryData and libraryData.FirstImage and hasValue(libraryData.FirstImage) then
        return libraryData.FirstImage
    end
    
    -- Step 3: Get associated people and check their FirstImage properties
    -- Query for all the relevant author/translator/editor properties
    local query = table.concat({
        '[[' .. fulltitle .. ']]',
        '|?BcaAuthor#',
        '|?AllAuthors#',
        '|?Translator#',
        '|?AllTranslators#',
        '|?Editor#',
        '|valuesep=;'
    })
    
    local queryData = smw.ask(query)
    if not (queryData and #queryData) then
        -- If SMW query fails, try to get data from library module
        if libraryData then
            -- Try BcaAuthor (mapped from DrlAuthor in library)
            if libraryData.DrlAuthor then
                local img = getPersonImage(libraryData.DrlAuthor)
                if img then return img end
            end
            
            -- Try AllAuthors (if it exists in library data)
            if libraryData.AllAuthors then
                local img = getPersonImage(libraryData.AllAuthors)
                if img then return img end
            end
            
            -- Try Translator
            if libraryData.Translator then
                local img = getPersonImage(libraryData.Translator)
                if img then return img end
            end
            
            -- Try AllTranslators
            if libraryData.AllTranslators then
                local img = getPersonImage(libraryData.AllTranslators)
                if img then return img end
            end
            
            -- Try Editor
            if libraryData.Editor then
                local img = getPersonImage(libraryData.Editor)
                if img then return img end
            end
        end
    else
        -- SMW query succeeded, check the data
        local data = queryData[1]
        
        -- Check each property in priority order
        -- 1. BcaAuthor
        if data.BcaAuthor then
            local img = getPersonImage(data.BcaAuthor)
            if img then return img end
        end
        
        -- 2. AllAuthors
        if data.AllAuthors then
            local img = getPersonImage(data.AllAuthors)
            if img then return img end
        end
        
        -- 3. Translator
        if data.Translator then
            local img = getPersonImage(data.Translator)
            if img then return img end
        end
        
        -- 4. AllTranslators
        if data.AllTranslators then
            local img = getPersonImage(data.AllTranslators)
            if img then return img end
        end
        
        -- 5. Editor
        if data.Editor then
            local img = getPersonImage(data.Editor)
            if img then return img end
        end
    end
    
    -- Step 4: No specific image found - determine stock photo based on classification
    local function getStockPhoto()
        -- Query for Classification and Subclass
        local classQuery = table.concat({
            '[[' .. fulltitle .. ']]',
            '|?Classification#',
            '|?Subclass#'
        })
        
        local classData = smw.ask(classQuery)
        
        -- Try to get from SMW query first
        local classification = nil
        local subclass = nil
        
        if classData and #classData > 0 then
            classification = classData[1].Classification
            subclass = classData[1].Subclass
        end
        
        -- Fall back to library data if needed
        if not classification and libraryData then
            classification = libraryData.Classification
            subclass = libraryData.Subclass
        end
        
        -- Determine stock photo based on classification
        if classification then
            -- Handle Classical Works with subclasses
            if classification == 'Classical Works' then
                if subclass == 'Tibetan Original Work' then
                    return 'File:Khyentse-pecha-stock.jpg'
                elseif subclass == 'Chinese Original Work' then
                    return 'File:Handwritten_diamond_sutra_zhang_jizhi_song_dynasty_1253.jpg'
                else
                    return 'File:Sutra portrait.jpg'
                end
            -- Handle other classifications
            elseif classification == 'Books' then
                return 'File:Chris-lawton-zvKx6ixUhWQ-unsplash.jpg'
            elseif classification == 'Articles' then
                return 'File:Articles.jpeg'
            elseif classification == 'Dissertations' then
                return 'File:Dissertations.jpeg'
            elseif classification == 'Sanskrit Publications' then
                return 'File:Sutra alt.jpeg'
            elseif classification == 'Tibetan Publications' then
                return 'File:Pecha.jpg'
            elseif classification == 'Multimedia' then
                return 'File:Media.jpeg'
            end
        end
        
        -- If no classification matches, return empty
        return ''
    end
    
    -- Return the appropriate stock photo
    return getStockPhoto()
end

return p