#!/usr/bin/gawk -bE # # batavg - Convert instances of [[Batting average]] to either [[Batting average (cricket)]] or [[Batting average (baseball)]] # # Dependencies: BotWikiAwk (via Github) # # The MIT License (MIT) # # Copyright (c) April 2019 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. BEGIN { BotName = "batavg" } @include "botwiki.awk" @include "library.awk" BEGIN { Mode = "bot" # set to "find" and it will search only and exit with a 1 (found something) or 0 (found nothing) # in "find" mode, run via 'project -s' to search local cache for articles containing actionable matches # set to anything else and it will process the article. IGNORECASE = 1 Optind = Opterr = 1 while ((C = getopt(ARGC, ARGV, "hs:l:n:")) != -1) { opts++ if(C == "s") # -s <file> article.txt source to process. articlename = verifyval(Optarg) if(C == "l") # -l <dir/> Directory where logging is sent.. end with "/" logdir = verifyval(Optarg) if(C == "n") # -n <name> Wikipedia name of article wikiname = verifyval(Optarg) if(C == "h") { usage() exit } } if( ! opts || articlename == "" ) { stdErr("Error in batavg.awk (1)") print "0" exit } if(wikiname == "" || logdir == "") Logfile = "/dev/null" else { if(substr(logdir, length(logdir), 1) != "/") logdir = logdir "/" Logfile = logdir "logbatavg" } Count = 0 main() } function main( article,articlenew,articlenewname,editsummaryname,bn) { checkexists(articlename, "batavg.awk main()", "exit") article = readfile(articlename) if(length(article) < 10) { print "0" exit } articlenew = batavg(article) if(article != articlenew && length(articlenew) > 10 && Count > 0) { articlenewname = editsummaryname = articlename bn = basename(articlename) "$" gsub(bn, "article.batavg.txt", articlenewname) printf("%s", articlenew) > articlenewname close(articlenewname) gsub(bn, "editsummary.batavg.txt", editsummaryname) if(Count == 1) printf("Disambiguate [[batting average]] (1 change) (via [[User:GreenC bot/Job 13|batavg]])") > editsummaryname else printf("Disambiguate [[batting average]] (%s changes) (via [[User:GreenC bot/Job 13|batavg]])", Count) > editsummaryname close(editsummaryname) print Count exit } else { sendlog(Logfile, wikiname, " errorĀ : batting average wikilink not found") } print "0" exit } # # batavg - main function # function batavg(article, c,i,field,sep,mode,a,orig,B) { # sendlog is concurrency-aware on Toolforge # . sendlog(Logfile, wikiname, data1 " ---- " data2 " ---- in batavg.awk") # increase global Count for each change # Count++ if(article ~ /baseball/ && article ~ /cricket/) { sendlog(Logfile, wikiname, " error Contains baseball and cricket") return article } else if(article !~ /baseball/ && article !~ /cricket/) { sendlog(Logfile, wikiname, " error No baseball or cricket") return article } else if(article ~ /baseball/ && article !~ /cricket/) { mode = "baseball" } else if(article !~ /baseball/ && article ~ /cricket/) { mode = "cricket" } else { sendlog(Logfile, wikiname, " errorĀ : cricket or baseball string not found") return article } # For non-pipe links of [[batting average]] c = patsplit(article, field, /[[][[][ ]*Batting[_ ]average[ ]*[]][]]/, sep) for(i = 1; i <= c; i++) { B = Bcase(field[i]) orig = field[i] field[i] = "[[" B "atting average (" mode ")|" B "atting average]]" Count++ sendlog(Logfile, wikiname, orig " ---- " field[i] " ---- convert A" ) } article = unpatsplit(field, sep) # For piped links of [[batting average|something]] or [[batting average#something|something]] c = patsplit(article, field, /[[][[][ ]*Batting[_ ]average([#](Major[_ ]League[_ ]Baseball|Baseball|Cricket))?[ ]*[|][^]]*[]][]]/, sep) for(i = 1; i <= c; i++) { B = Bcase(field[i]) orig = field[i] split(field[i], a, /[|]/) if(length(strip(gsubi("[ ]*[]][]]$", "", a[2]))) > 0) # wikilink title not empty field[i] = "[[" B "atting average (" mode ")|" a[2] else field[i] = "[[" B "atting average (" mode ")|" B "atting average]]" Count++ sendlog(Logfile, wikiname, orig " ---- " field[i] " ---- convert B" ) } article = unpatsplit(field, sep) return article } # Capitalize "Batting"? function Bcase(fieldi) { IGNORECASE = 0 if(fieldi ~ /[[][[][ ]*B/) { IGNORECASE = 1 return "B" } else { IGNORECASE = 1 return "b" } }