Документация

Модуль используется в шаблоне {{Красные ссылки}}.

local p = {}

-- Функция для подсчета красных ссылок на указанной странице
function p.countRedLinks(frame)
    -- Получаем название страницы из аргумента
    local pageName = frame.args[1] or ""
    
    -- Получаем текст страницы по названию
    local pageTitle = mw.title.new(pageName)
    
    -- Проверяем, существует ли страница
    if not pageTitle or not pageTitle.exists then
        return "Страница не найдена."
    end

    local pageContent = pageTitle:getContent()

    -- Ищем все красные ссылки
    local redLinkCount = 0

    -- Регулярное выражение для поиска обычных красных ссылок
    for link in pageContent:gmatch('%[%[%s*([^%]|]+)%s*%|?%s*([^%]]+)%s*%]%]') do
        local linkName = link:match('^%s*([^%|]+)%s*$')
        if linkName then
            local linkTitle = mw.title.new(linkName)

            -- Проверяем, является ли ссылка красной
            if linkTitle and not linkTitle.exists then
                redLinkCount = redLinkCount + 1
            end
        end
    end

    -- Обработка ссылок без текста
    for link in pageContent:gmatch('%[%[%s*([^%]]+)%s*%]%]') do
        local linkName = link:match('^%s*([^%|]+)%s*$')
        if linkName then
            local linkTitle = mw.title.new(linkName)

            -- Проверяем, является ли ссылка красной
            if linkTitle and not linkTitle.exists then
                redLinkCount = redLinkCount + 1
            end
        end
    end

    -- Обработка шаблона {{iw}}
    for iwLink in pageContent:gmatch('{{iw|%s*([^|}]+)|?%s*([^|}]+)|?%s*([^|}]+)|?%s*([^|}]+)%s*}}') do
        local iwLinkName = iwLink:match('^%s*([^|}]+)%s*$')  -- Извлекаем только первый параметр
        if iwLinkName then
            local iwLinkTitle = mw.title.new(iwLinkName)

            -- Проверяем, является ли ссылка красной
            if iwLinkTitle and not iwLinkTitle.exists then
                redLinkCount = redLinkCount + 1
            end
        end
    end

    return redLinkCount
end

return p