Module:Citation/CS1/Date validation: Difference between revisions

    (sync from sandbox;)
    (lua script error fix for non-Latin digits in |year=;)
    Line 5: Line 5:
    local is_set, in_array; -- imported functions from selected Module:Citation/CS1/Utilities
    local is_set, in_array; -- imported functions from selected Module:Citation/CS1/Utilities
    local cfg; -- table of tables imported from selected Module:Citation/CS1/Configuration
    local cfg; -- table of tables imported from selected Module:Citation/CS1/Configuration
    --[[--------------------------< F I L E - S C O P E  D E C L A R A T I O N S >--------------------------------
    File-scope variables are declared here
    ]]
    local lang_object = mw.getContentLanguage(); -- used by is_valid_accessdate(), is_valid_year(), date_name_xlate(); TODO: move to ~/Configuration?
    local year_limit; -- used by is_valid_year()




    Line 27: Line 37:


    local function is_valid_accessdate (accessdate)
    local function is_valid_accessdate (accessdate)
    local lang = mw.getContentLanguage();
    local good1, good2;
    local good1, good2;
    local access_ts, tomorrow_ts; -- to hold unix time stamps representing the dates
    local access_ts, tomorrow_ts; -- to hold unix time stamps representing the dates
     
    good1, access_ts = pcall( lang.formatDate, lang, 'U', accessdate ); -- convert accessdate value to unix timesatmp  
    good1, access_ts = pcall (lang_object.formatDate, lang_object, 'U', accessdate ); -- convert accessdate value to unix timesatmp  
    good2, tomorrow_ts = pcall( lang.formatDate, lang, 'U', 'today + 2 days' ); -- today midnight + 2 days is one second more than all day tomorrow
    good2, tomorrow_ts = pcall (lang_object.formatDate, lang_object, 'U', 'today + 2 days' ); -- today midnight + 2 days is one second more than all day tomorrow
    if good1 and good2 then -- lang.formatDate() returns a timestamp in the local script which which tonumber() may not understand
    if good1 and good2 then -- lang.formatDate() returns a timestamp in the local script which which tonumber() may not understand
    access_ts = tonumber (access_ts) or lang:parseFormattedNumber (access_ts); -- convert to numbers for the comparison;
    access_ts = tonumber (access_ts) or lang_object:parseFormattedNumber (access_ts); -- convert to numbers for the comparison;
    tomorrow_ts = tonumber (tomorrow_ts) or lang:parseFormattedNumber (tomorrow_ts);
    tomorrow_ts = tonumber (tomorrow_ts) or lang_object:parseFormattedNumber (tomorrow_ts);
    else
    else
    return false; -- one or both failed to convert to unix time stamp
    return false; -- one or both failed to convert to unix time stamp
    Line 138: Line 147:


    ]]
    ]]
    local year_limit;
     
    local function is_valid_year(year)
    local function is_valid_year(year)
    if not is_set(year_limit) then
    if not is_set(year_limit) then
    year_limit = tonumber(os.date("%Y"))+1; -- global variable so we only have to fetch it once
    year_limit = tonumber(os.date("%Y"))+1; -- global variable so we only have to fetch it once
    end
    end
    return tonumber(year) <= year_limit; -- false if year is in the future more than one year
     
    year = tonumber (year) or lang_object:parseFormattedNumber (year); -- convert to numbers for the comparison;
    return year and (year <= year_limit) or false;
    end
    end


    Line 1,008: Line 1,019:
    if mode then -- might be a season
    if mode then -- might be a season
    xlate = mw.getContentLanguage():formatDate(mode, '1' .. month); -- translate the month name to this local language
    xlate = lang_object:formatDate(mode, '1' .. month); -- translate the month name to this local language
    date = mw.ustring.gsub (date, month, xlate); -- replace the English with the translation
    date = mw.ustring.gsub (date, month, xlate); -- replace the English with the translation
    date_parameters_list[param_name].val = date; -- save the translated date
    date_parameters_list[param_name].val = date; -- save the translated date