Module:DiscussionIndex

From Outreach Wiki
Jump to navigation Jump to search
--[[

This module provides a formatted index of the discussions that have occured on 
a target page.

]]

p = {};
pp = require( 'Module:ParsePage' );

function p.summary( frame )
    local target = frame.args[1] or frame.args.target or '';
    local use_excerpts = frame.args.excerpts or false;
    if type( use_excerpts ) == 'string' then
        use_excerpts = use_excerpts:lower();
        if use_excerpts == 'false' or use_excerpts == '' or
                use_excerpts == '0' or use_excerpts == 'no' then
            use_excerpts = false;
        else
            use_excerpts = true;
        end
    end   
    local recent_time = ( tonumber( frame.args.recent_time ) or 120 )*60;
    local old_time = ( tonumber( frame.args.old_time ) or 60*24*2 )*60;

    if target == '' then 
        return '';
    end
    local text = frame:preprocess( '{{:' .. target .. '}}' );

    return frame:preprocess( p._summary( text, target, use_excerpts, recent_time, old_time ) );
end
    
function p._summary( text, target, use_excerpts, recent_time, old_time )
    local lang = mw.getContentLanguage();
    local now = lang:formatDate( 'U' );
        
    local sections, headings = pp.getSections( text, 2 );
    local result;
    
    result = '== Discussion summary report for [[' .. target .. ']] ==\n\n'
    result = result .. 'This is an automated summary of the discussions occurring on ' ..
        target .. '.\n\n' ..
        'It was last generated at ' .. lang:formatDate( 'l, j F Y, H:i:s' ) .. ' UTC ' ..
        '([{{fullurl:{{FULLPAGENAME}}|action=purge}} update now]).\n\n' ..
        '<u>The current version of this report is still experimental and may contain ' ..
        'significant inaccuracies.</u>\n\n'
    
    if use_excerpts then
        result = result .. '{| class="wikitable" \n';
    else
        result = result .. '{| class="wikitable sortable" \n';
    end
    
    result = result .. '! Section title !! data-sort-type="number" | Age ' ..
        ' !! data-sort-type="number" | ' ..
        ' Last Comment !! Bytes !! Originator !! data-sort-type="number" | Other Participants \n|-\n|';
    local result_lines = {};
    
    for k, tt in ipairs( sections ) do
        local users = pp.getUsers( tt, false, true );
        local times = pp.getTimestamps( tt );
        local min_time, max_time
        local min_time_string, max_time_string
        local line_item = {};
        
        for k, v in ipairs( times ) do
            if min_time == nil or min_time > v[2] then
                min_time = v[2];
            end
            if max_time == nil or max_time < v[2] then
                max_time = v[2];
            end
        end        
    
        for k, v in ipairs( times ) do
            if min_time == v[2] then
                min_time_string = v[1];
            end
            if max_time == v[2] then
                max_time_string = v[1];
            end
        end
        min_time = now - (min_time or now);
        max_time = now - (max_time or now);

        local user_list = {}
        for _, tt in pairs( users ) do
            table.insert( user_list, tt[2] );
        end
        if headings[k] ~= '' and user_list[1] ~= nil then
            line_item = { pp.formatSectionLink( target, headings[k] ), 
                p._formatTimeDiff(min_time, recent_time, old_time ), 
                p._formatTimeDiff(max_time, recent_time, old_time ), 
                tostring( #tt ), user_list[1] };

            table.remove( user_list, 1 );
            table.sort( user_list );
            table.insert( line_item, tostring( #user_list ) .. ': ' .. table.concat( user_list, ', ' ) )
            if use_excerpts then
                table.insert( line_item, pp.getExcerpt( tt, 500 ) )
            end                    
            
            table.insert( result_lines, line_item );
        end        
    end

    comp = function( a, b )        
        local a2, b2
        a2 = mw.ustring.gsub( a[3], '%b<>', '' );
        a2 = mw.ustring.gsub( a2, '.*|%s*', '' );
        b2 = mw.ustring.gsub( b[3], '%b<>', '' );
        b2 = mw.ustring.gsub( b2, '.*|%s*', '' );
        a2 = tonumber( mw.ustring.match( a2, '^(%d-)_' ) ) or 0;
        b2 = tonumber( mw.ustring.match( b2, '^(%d-)_' ) ) or 0;
        return a2 < b2;
    end
    table.sort( result_lines, comp );

    local result_lines2 = {};
    local row;
    for k, tt in ipairs( result_lines ) do
        if use_excerpts then
            local excerpt = tt[#tt];
            table.remove( tt );
            row = table.concat( tt, '||' );
            table.insert( result_lines2, row );
            table.insert( result_lines2, 
                ' colspan=6 style="padding-left:2em; padding-top:0.5em; padding-bottom:0.5em;" | <span style="font-size:90%">' ..excerpt .. '</span>' );
        else
            row = table.concat( tt, '||' );
            table.insert( result_lines2, row );
        end            
    end

    result = result .. table.concat( result_lines2, '\n|-\n|' ) .. "\n|}";
        
    return result;
end


function p._formatTimeDiff( tv, recent_time, old_time )
    local tv_string;
    tv_string = '<span style="display:none">' .. tostring(tv) .. '_</span>' .. mw.ustring.gsub( pp.formatDateDiff(tv), ' ', '&nbsp;' );
    if tv < recent_time then
        tv_string = ' style="background:#EFE" | ' .. tv_string;
    elseif tv > old_time then
        tv_string = ' style="background:#FEE" | ' .. tv_string;
    end
    return tv_string;
end

return p;