Module:Selected recent additions
Appearance
From Wikipedia, the free encyclopedia
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page semi-protected Editing of this module by new or unregistered users is currently disabled.
See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request , discuss changes on the talk page, request unprotection, log in, or create an account.
See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request , discuss changes on the talk page, request unprotection, log in, or create an account.
This module depends on the following other modules:
Usage
{{#invoke:Selected recent additions|main}}
Arguments come from the parent template {{Transclude selected recent additions }}; see documentation there for details.
See also
The above documentation is transcluded from Module:Selected recent additions/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.
localrandomModule=require('Module:Random') functioncleanupArgs(argsTable) localcleanArgs={} forkey,valinpairs(argsTable)do iftype(val)=='string'then val=val:match('^%s*(.-)%s*$') ifval~=''then cleanArgs[key]=val end else cleanArgs[key]=val end end returncleanArgs end functionisAffirmed(val) ifnot(val)thenreturnfalseend localaffirmedWords=' add added affirm affirmed include included on true yes y ' returnstring.find(affirmedWords,' '..string.lower(val)..' ',1,true)andtrueorfalse end functionmakeOutput(allItems,maxItems,more,notRandom) localoutput ifnotRandomthen output='' localitemIndex=1 localmaxCount=math.min(#allItems,maxItems) whileitemIndex<=maxCountdo output=output..allItems[itemIndex]..'\n' itemIndex=itemIndex+1 end else localrandomiseArgs={ ['t']=allItems, ['limit']=maxItems } localrandomisedItems=randomModule.main('array',randomiseArgs) output=table.concat(randomisedItems,'\n') end ifmorethen output=output..more end returnmw.text.trim(output) end functioncleanForPatternMatching(wikitext) -- remove wikilink brackets localcleaned=mw.ustring.gsub(wikitext,"%[%[(.-)%]%]","%1") -- remove pipes that would have been in piped links cleaned=mw.ustring.gsub(cleaned,"%|"," ") -- remove external links cleaned=mw.ustring.gsub(cleaned,"%[.-%]"," ") returncleaned end functionmakeCollapsed(outerText,innerText) return"{{Hidden begin | titlestyle = font-weight:normal | title = "..outerText.."}}"..innerText.."{{Hidden end}}" end -- Get current events for a "YYYY Month D" date. Returns a table of list items. functiongetRecentAdditions(subpage,keepPatterns,skipPatterns,showWikitext) localtitle=mw.title.new('Wikipedia:Recent additions'..subpage) localraw=title:getContent() localitemPattern='%*%s?%.%.%.[%S ]*' localitems={} foriteminmw.ustring.gmatch(raw,itemPattern)do localkeep=false localskip=false localisListItem=(string.sub(item,0,1)=='*') ifisListItemthen localtext=cleanForPatternMatching(item) forii,keepPattinpairs(keepPatterns)do ifnotkeepandmw.ustring.find(text,keepPatt)then keep=true end end if#skipPatterns>0then foriii,skipPattinpairs(skipPatterns)do ifnotskipandmw.ustring.find(text,skipPatt)then skip=true end end end end ifkeepandnotskipthen -- remove (pictured) inline note localcleanItem=mw.ustring.gsub(item,"%s*''%(.-pictured.-%)''","") -- remove (illustrated) inline note cleanItem=mw.ustring.gsub(cleanItem,"%s*''%(.-illustrated.-%)''","") ifshowWikitextthen -- remove html comments cleanItem=mw.ustring.gsub(cleanItem,"%<%!%-%-(.-)%-%-%>","") localitemWikitext="<pre>"..mw.text.nowiki(cleanItem).."</pre>" cleanItem=makeCollapsed(cleanItem,itemWikitext) end table.insert(items,cleanItem) end end returnitems end functiongetItems(maxMonths,patterns,skipPatterns,showWikitext) localallItems={} locallang=mw.language.new('en') localcurrentYear=tonumber(lang:formatDate('Y','now')) localcurrentMonth=tonumber(lang:formatDate('n','now')) localmonthsAgo=0 whilemonthsAgo<maxMonthsdo localsubpage ifmonthsAgo==0then subpage='' else localyear=currentYear-math.modf((monthsAgo+12-currentMonth)/12) localmonth=math.fmod(12+currentMonth-math.fmod(monthsAgo,12),12) month=(month~=0)andmonthor12 subpage=lang:formatDate('/Y/F',year..'-'..month) end localmonthlyItems=getRecentAdditions(subpage,patterns,skipPatterns,showWikitext) fori,iteminipairs(monthlyItems)do table.insert(allItems,item) end monthsAgo=monthsAgo+1 end returnallItems end functiongetPatterns(args,prefix) localpatterns={} localii=1 whileargs[prefixandprefix..iiorii]do patterns[ii]=args[prefixandprefix..iiorii] ii=ii+1 end returnpatterns end localp={} p.main=function(frame) localparent=frame.getParent(frame) localparentArgs=parent.args localargs=cleanupArgs(parentArgs) ifargs['not']andnotargs['not1']then args['not1']=args['not'] end localpatterns=getPatterns(args) if#patterns<1then returnerror("Search pattern not set") end localskipPatterns=getPatterns(args,'not') localmonths=tonumber(args.months)or30 localshowWikitext=isAffirmed(args.wikitext) localallItems=getItems(months,patterns,skipPatterns,showWikitext) if#allItems<1then returnargs.headerand''orargs.noneor'No recent additions' end localmaxItems=tonumber(args.max)or6 localmore=args.more ifisAffirmed(args.more)then more="'''[[Wikipedia:Recent additions|More recent additions...]]'''" end localnonRandom=isAffirmed(args.latest) localoutput=makeOutput(allItems,maxItems,more,nonRandom) ifargs.headerthen output=args.header..'\n'..output..'\n'..(args.footeror'{{Box-footer}}') end localneedsExpansion=mw.ustring.find(output,'{{',0,true) ifneedsExpansionthen returnframe:preprocess(output) else returnoutput end end returnp