-
Notifications
You must be signed in to change notification settings - Fork 4
/
accdate.awk
executable file
·371 lines (300 loc) · 11.3 KB
/
accdate.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/local/bin/gawk -bE
#
# Remove |accessdate= in CS1|2 templates in certain scenarios
#
# https://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/GreenC_bot_5
#
# The MIT License (MIT)
#
# Copyright (c) 2018 by User:GreenC (at en.wikipedia.org)
#
# 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 { # Bot cfg
_defaults = "home = /home/admin/bots/accdate/ \
email = sample@example.com \
version = 1.0 \
copyright = 2024"
asplit(G, _defaults, "[ ]*[=][ ]*", "[ ]{9,}")
BotName = "accdate"
Home = G["home"]
Agent = "Ask me about " BotName " - " G["email"]
Engine = 0
}
@include "botwiki.awk"
@include "library.awk"
BEGIN { # Bot run
Mode = "bot" # set to "find" and it will search only and exit with a 1 (found something) or 0 (found nothing)
# run via 'project -s' to search cache for articles containing actionable matches
# set to anything else and it will process the article.
IGNORECASE = 1
ReSpace = "[\n\r\t]*[ ]*[\n\r\t]*[ ]*[\n\r\t]*"
ReCites = "[{][ ]{0,}[{]" ReSpace "(Citation|Cite speech|Cite conference|Cite newsgroup|Cite techreport|Cite journal|Cite interview|Cite thesis|Cite bioRxiv|Cite serial|Cite episode|Cite arXiv|Cite report|Cite press release|Cite map|Cite magazine|Cite book|Cite encyclopedia|Cite sign|Cite news|Cite AV media notes|Cite AV media)" ReSpace "[|][^}]+[}][ ]{0,}[}]"
ReCites2 = "[{][ ]{0,}[{]" ReSpace "(Cite journal|Cite books|Cite news|Cite magazine|Cite techreport|Cite thesis|Cite press release|Cite encyclopedia|Cite bioRxiv)" ReSpace "[|][^}]+[}][ ]{0,}[}]"
ReTail = "[^|}<\n\r\t]*[^|}<\n\r\t]?"
ReIdents = "[|]" ReSpace "(arxiv|asin|bibcode|biorxiv|citeseerx|doi|eprint|eissn|hdl|issn|jfm|jstor|lccn|mr|oclc|ol|osti|pmc|pmid|rfc|ssrn|zbl|id)" ReSpace "[=]"
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 accdate.awk (1)")
print "0"
exit
}
if(wikiname == "" || logdir == "")
Logfile = "/dev/null"
else {
if(substr(logdir, length(logdir), 1) != "/")
logdir = logdir "/"
Logfile = logdir "logaccdate"
}
Count = 0
main()
}
function main(article,c,i,field,sep,re,articlenew,loop,dt,count,articlenewname,editsummaryname,bn) {
checkexists(articlename, "accdate.awk main()", "exit")
article = readfile(articlename)
if(length(article) < 10) {
print "0"
exit
}
article = deflate(article)
articlenew = accdate(article)
if(article != articlenew && length(articlenew) > 10 && Count > 0) {
articlenew = inflate(articlenew)
articlenewname = editsummaryname = articlename
bn = basename(articlename) "$"
gsub(bn, "article.accdate.txt", articlenewname)
printf("%s", articlenew) > articlenewname
close(articlenewname)
gsub(bn, "editsummary.accdate.txt", editsummaryname)
printf("Remove %s stray access-date. ([[User:GreenC bot/Job 5|GreenC bot job #5]])", Count) > editsummaryname
close(editsummaryname)
print Count
exit
}
print "0"
exit
}
#
# Parse article for CS1|2 templates and modify article if needed and log if needed
#
function accdate(article, c,i,field,sep,orig,newarticle,newcite,j,reCites) {
for(j = 1; j < 3; j++) {
if(j == 1)
reCites = ReCites
else
reCites = ReCites2
c = patsplit(article, field, reCites, sep)
for(i = 1; i <= c; i++) {
if(length(field[i]) > 0) {
delete G
if(j == 1)
newcite = accdate1(field[i])
else if(j == 2)
newcite = accdate2(field[i])
if(newcite != field[i]) {
orig = field[i]
field[i] = newcite
Count ++
if(Mode == "find") {
print "1"
exit
}
else
sendlog(Logfile, wikiname, gsubs("\n", "", orig) " ---- " gsubs("\n","",newcite) " ---- accdate" j " in accdate.awk")
}
}
}
article = unpatsplit(field,sep)
}
return article
}
#
# Remove |accessdate= in {{Cite book}}, {{Cite news}} and {{Cite journal}} with no |url=. Per the documentation,
# "Access dates are not required for links to published research papers, published books, or news articles with publication dates."
# If a publication date is provided, remove |access date=.
#
function accdate2(tl, debug,tls) {
debug = 0
if(debug) print "\n_______________accdate2___________________\n"
if(isembedded(tl)) {
if(debug) print tl "\nSkipped: contains an unknown embedded template missed during deflate()\n----"
return tl
}
tls = stripwikicomments(tl)
# Fill G[] with |arg status
getstatus(tls, "url")
getstatus(tls, "access-date")
getstatus(tls, "date")
getstatus(tls, "year")
if(G["status url"] == "yes") {
if(debug) print tl "\nSkipped: status url = yes\n----"
return tl
}
if(G["status accessdate"] == "no") {
if(debug) print tl "\nSkipped: status accessdate = no\n----"
return tl
}
if(debug) print tl "\nstatus url\t\t= " G["status url"] "\nstatus access-date\t= " G["status access-date"] "\nstatus date\t\t= " G["status date"] "\nstatus year\t\t= " G["status year"]
if(G["status url"] == "no" && G["status access-date"] == "yes" && (G["status date"] == "yes" || G["status year"] == "yes") )
tl = deleteargs(tl,debug)
if(debug) print "----"
return tl
}
#
# Remove |accessdate= in CS1|2 templates that don't have a |url= but do have a value assigned to any of the various 'permanent-record' identifiers.
# Excluding templates {{cite web}}, {{cite podcast}}, and {{cite mailing list}}. Normally |isbn= would be excluded from the identifier list,
# but if a {{cite book}} it would be included.
#
function accdate1(tl, tls,re,dest,reIdents,debug) {
debug = 0
if(debug) print "\n_______________accdate1___________________\n"
if(isembedded(tl)) {
if(debug) print tl "\nSkipped: contains an unknown embedded template missed during deflate()\n----"
return tl
}
tls = stripwikicomments(tl)
# Fill G[] with |url and |access-date status
getstatus(tls, "url")
getstatus(tls, "access-date")
if(G["status url"] == "yes") {
if(debug) print tl "\nSkipped: status url = yes\n----"
return tl
}
if(G["status access-date"] == "no") {
if(debug) print tl "\nSkipped: status access-date = no\n----"
return tl
}
# special case if a {{cite book}} then include |isbn identifier
re = "[{][{]" ReSpace "[Cc]ite [Bb]ook" ReSpace "[|]"
if(tl ~ re) {
reIdents = subs("|eissn", "|isbn|eissn", ReIdents)
}
else
reIdents = ReIdents
# Get identifier status
G["status identifier"] = "no"
re = reIdents ReSpace ReTail
if(match(tls, re, dest)) {
# special case for |id= identifier
re = "[|]" ReSpace "id" ReSpace "[=]"
if(dest[0] ~ re && istemplate(dest[0])) { # special case for id={{template}} assume it's an external link
G["status identifier"] = "yes"
}
else if(dest[0] ~ re && ! istemplate(dest[0])) {
G["status identifier"] = "no"
}
# every other identifier type
else {
gsub(reIdents,"", dest[0])
dest[0] = strip(strip(dest[0]))
if(dest[0] !~ /^[<][!]/) {
if(!empty(dest[0]))
G["status identifier"] = "yes"
}
}
}
if(debug) print tl "\nstatus url\t\t= " G["status url"] "\nstatus access-date\t= " G["status access-date"] "\nstatus identifier\t= " G["status identifier"]
if(G["status url"] == "no" && G["status access-date"] == "yes" && G["status identifier"] == "yes")
tl = deleteargs(tl,debug)
if(debug) print "----"
return tl
}
#
# Fill G[] status for an |arg in a citation tls
#
# getstatus(tls, "date") -->
# G["status date"] = "yes"
# G["arg date"] = "exists"
#
# status = yes means the arg has a valid value. No means it's an empty or invalid value or no arg exists
# arg = exists means the argument exists in the template regardless of value. Notexists means it doesn't
#
function getstatus(tls,arg, statusidx,argidx,argre,dest,re,dest2) {
statusidx = "status " arg
argidx = "arg " arg
argre = subs("-", "[-]?", arg)
G[statusidx] = "yes"
if(arg == "url") { # special handling of URL
# check for existing |url but empty or invalid data
re = "[|]" ReSpace argre ReSpace "[=]" ReSpace ReTail
if(match(tls, re, dest)) {
G[argidx] = "exists"
re = "[|]" ReSpace argre ReSpace "[=]" ReSpace
if(match(dest[0], re, dest2)) {
if(empty(urlElement(strip(gsubs(dest2[0],"",dest[0])), "netloc"))) { # no discernable domain name in url field
if(! istemplate(dest[0])) # URL not templated
G[statusidx] = "no"
}
}
}
else
G[argidx] = "notexists"
}
else {
# check for existing |arg but empty or invalid data
re = "[|]" ReSpace argre ReSpace "[=]" ReSpace ReTail
if(match(tls, re, dest)) {
G[argidx] = "exists"
if(dest[0] !~ /[0-9]/) # might improve date check here
G[statusidx] = "no"
}
else
G[argidx] = "notexists"
}
# check for missing |arg
if(G[statusidx] == "yes") {
re = "[|]" ReSpace argre ReSpace "[=]"
if(!match(tls, re, dest)) {
G[statusidx] = "no"
G[argidx] = "notexists"
}
else
G[argidx] = "exists"
}
}
#
# Delete |access-date and |url from tl including key and value
#
function deleteargs(tl,debug, re,dest) {
re = "[|]" ReSpace "access[-]?date" ReSpace "[=]" ReSpace ReTail ReSpace
if(match(tl, re, dest)) {
tl = gsubs(dest[0], "", tl)
if(debug) print "Change:\tdeleted access-date"
}
if(G["arg url"] == "exists") {
re = "[|]" ReSpace "url" ReSpace "[=]" ReSpace ReTail ReSpace
if(match(tl, re, dest)) {
tl = gsubs(dest[0], "", tl)
if(debug) print "Change:\tdeleted url"
}
}
return tl
}