Module:Database report/facade
Appearance
From Wikipedia, the free encyclopedia
(Redirected from Module:Database report/validate)
This module is used from {{Database report }}.
The above documentation is transcluded from Module:Database report/facade/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
localArguments=require('Module:Arguments') localTableTools=require('Module:TableTools') localp={} localfunctionerror(text) return'\n*'..require('Module:Error').error{text,tag='p'} end localfunctionstr_split(inputstr,sep) localt={} forstrinstring.gmatch(inputstr,"([^"..sep.."]+)")do table.insert(t,str) end returnt end localfunctionis_integer(str) ifstr:match("%D")then returnfalse end returntrue end localfunctiontrim(s) returns:match("^%s*(.-)%s*$") end p.main=function(frame) localargs=Arguments.getArgs(frame) localtitle=mw.title.getCurrentTitle() locallua_config,lua_err=invoke_lua_source(args,frame) localheadContent=lua_configandlua_config.head_contentor'' localisSilent=args.silent~=nilor(lua_configandlua_config.silent~=nil) localisPeriodic=args.interval~=nilor(lua_configandlua_config.interval~=nil) returnheadContent.. (isSilentand''or "<div style='float: right; padding-left: 15px;'>".. '[https://sdzerobot.toolforge.org/database-report?page='..mw.uri.encode(title.prefixedText)..' Update the table now]'.. '</div>'.. "<div style='margin-bottom: 5px; border-bottom: 3px solid #2F74D0; padding-right: 10px;'>".. "This table is generated by querying the [[wikitech:Help:Toolforge/Database|database replica]] "..(isPeriodicand'and is periodically updated'or'').." by [[User:SDZeroBot|a bot]].<br> ".. '<mark>Edits made within the table area will be removed on the next update!</mark>'.. '</div>') ..'[[Category:SDZeroBot database report subscriptions]]' ..(lua_errorvalidate(args)) end functionvalidate(args) -- sql param is required, except if the config is lua-generated ifnotargs.sqlandnotargs.lua_sourcethen returnerror('Invalid config: required parameter "sql" is missing') end localdeprecated_params={ ['frequency']='interval' } -- check for invalid parameters forparam,_inpairs(args)do ifdeprecated_params[param]~=nilthen returnerror(param..' is deprecated, please use '..deprecated_params[param]..' instead') end ifnotTableTools.inArray({ 'sql','wikilinks','excerpts','comments','widths','hide','silent', 'table_style','table_class','remove_underscores','pagination','output_page', 'max_pages','interval','row_template','skip_table','header_template', 'footer_template','row_template_named_params','postprocess_js', 'lua_source','lua_function' },param)andnotparam:find('^lua_arg_')==nilthen returnerror('Unknown parameter "'..param..'" used, ignoring') end end -- check incompatible param combinations ifnotargs.row_templatethen ifargs.skip_tablethen returnerror('Invalid config: skip_table can only be used with row_template') end end ifargs.output_pagethen ifargs.paginationthen returnerror('pagination and output_page cannot be used together; output_page will be ignored') end localt1=mw.title.getCurrentTitle() localt2=mw.title.new(args.output_page) ift1.namespace~=t2.namespaceort2.text:find(t1.text..'/',1,true)~=1then returnerror('output_page must be a subpage of the current page') end end -- check wikilinks config ifargs.wikilinksthen localconfigs=str_split(args.wikilinks,',') for_,configinipairs(configs)do localparts=str_split(trim(config),':') localsrcColNum=parts[1] ifnotis_integer(srcColNum)then returnerror('Invalid wikilink config: Non-numeral source column number: '..parts[1]..'. Will be ignored.') end localns=parts[2] ifnsandns:match("^c?%d+$")==nilthen returnerror('Invalid namespace number "'..ns..'" in wikilinks parameter: refer to [[WP:NS]] for namespace numbers, or use "cN" to take namespace number from column number N') end end end -- check excerpts config ifargs.excerptsthen localconfigs=str_split(args.excerpts,',') for_,configinipairs(configs)do localparts=str_split(trim(config),':') localsrcColNum=parts[1] ifnotis_integer(srcColNum)then returnerror('Invalid excerpts config: Non-numeral source column number: '..parts[1]..'. Will be ignored.') end localdstColNum=parts[2] ifdstColNumandnotis_integer(dstColNum)then returnerror('Invalid excerpts config: Non-numeral destination column number: '..parts[2]..'. Will be ignored.') end localns=parts[3] ifnsandns:match("^c?%d+$")==nilthen returnerror('Invalid namespace number "'..ns..'" in excerpts parameter: refer to [[WP:NS]] for namespace numbers, or use "cN" to take namespace number from column number N') end localcharLimit=parts[4] ifcharLimitandnotis_integer(charLimit)then returnerror('Invalid excerpts config: Non-numeral in charLimit: '..parts[4]..'. Will be ignored.') end localhardCharLimit=parts[5] ifhardCharLimitandnotis_integer(hardCharLimit)then returnerror('Invalid excerpts config: Non-numeral in hardCharLimit: '..parts[5]..'. Will be ignored.') end end end -- check column numbers in widths param ifargs.widthsthen localconfigs=str_split(args.widths,',') for_,configinipairs(configs)do localparts=str_split(trim(config),':') localcolumn=parts[1] ifnotis_integer(column)then returnerror('Invalid widths config: Non-numeral column number: '..parts[1]..'. Will be ignored.') end end end -- check numeric configs ifargs.commentsthen localcolumns=str_split(args.comments,',') for_,columninipairs(columns)do ifnotis_integer(trim(column))then returnerror('Invalid comments parameter: Non-numeral column number: '..column..'. Will be ignored.') end end end ifargs.remove_underscoresthen localcolumns=str_split(args.remove_underscores,',') for_,columninipairs(columns)do ifnotis_integer(trim(column))then returnerror('Invalid remove_underscores parameter: Non-numeral column number: '..column..'. Will be ignored.') end end end ifargs.hidethen localcolumns=str_split(args.hide,',') for_,columninipairs(columns)do ifnotis_integer(trim(column))then returnerror('Invalid hide parameter: Non-numeral column number: '..column..'. Will be ignored.') end end end ifargs.paginationandnotis_integer(args.pagination)then returnerror('pagination should be an integer. Will be ignored.') end ifargs.max_pagesandnotis_integer(args.max_pages)then returnerror('max_pages should be an integer. Will be ignored.') end ifargs.intervalandnotis_integer(args.interval)then returnerror('interval should be an integer. Will be ignored.') end ifargs.row_template_named_paramsandnotargs.row_templatethen returnerror('row_template_named_params is only applicable when row_template is used') end return'' end functioninvoke_lua_source(args,frame) ifnotargs.lua_sourcethen returnnil,nil end localmodule_title=mw.title.new(args.lua_source,828) ifmodule_title==nilormodule_title.namespace~=828then returnnil,error('lua_source should be a valid Module title') end localmodule_name=module_title.text locallua_params='' forkey,valinpairs(args)do ifkey:find('^lua_arg_')then localargName=string.sub(key,string.len('lua_arg_')+1) localargValue=val lua_params=lua_params..'|'..argName..'='..argValue end end localinvoke='{{#invoke:'..module_name..'|'..(args.lua_functionor'main')..lua_params..'}}' localmoduleOutput=frame:preprocess(invoke) -- FIXME: more robust error detection ifmoduleOutput:find('error')~=nilthen returnnil,moduleOutput end ifnotmoduleOutput:match('^{{')ornotmoduleOutput:match('}}$')then -- XXX: Module is returning something other than report:generate(), error out? return{},nil end localconfig={} -- Parse the configuration text to extract parameter values forlineinmoduleOutput:gmatch("[^\n]+")do localparam,value=line:match("^|([^=]+) = (.*)$") ifparamandvaluethen value=trim(value) ifvalue~=""andvalue~="nil"then config[param]=value end end end returnconfig,nil end returnp