User:GreenC bot/Job 20/source

This is a static version, most recent version is on Toolforge in

peerr.awk


 * 1) !/usr/bin/gawk -bE


 * 1) peerr removes the template  from talk pages when no longer needed. ie. the template was added more than 7
 * 2) days ago indicating the peer review processes has stalled or was not properly initiated.
 * 3) https://en.wikipedia.org/wiki/User:GreenC_bot/Job_20
 * 1) https://en.wikipedia.org/wiki/User:GreenC_bot/Job_20
 * 1) https://en.wikipedia.org/wiki/User:GreenC_bot/Job_20


 * 1) The MIT License (MIT)
 * 2) Copyright (c) April 2021 User:GreenC at en.wikipedia.org
 * 3) Permission is hereby granted, free of charge, to any person obtaining a copy
 * 4) of this software and associated documentation files (the "Software"), to deal
 * 5) in the Software without restriction, including without limitation the rights
 * 6) to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * 7) copies of the Software, and to permit persons to whom the Software is
 * 8) furnished to do so, subject to the following conditions:
 * 9) The above copyright notice and this permission notice shall be included in
 * 10) all copies or substantial portions of the Software.
 * 11) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * 12) IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * 13) FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * 14) AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * 15) LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * 16) OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * 17) THE SOFTWARE.
 * 1) AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * 2) LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * 3) OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * 4) THE SOFTWARE.

@include "library.awk"
 * 1) library.awk : https://github.com/greencardamom/BotWikiAwk/tree/master/lib

function pageExists(entity  ,url,jsonin) {
 * 1) entity_exists - see if a page on Wikipedia exists
 * 2)   eg. if ( ! entity_exists("Gutenberg author") ) print "Unknown page"
 * 1)   eg. if ( ! entity_exists("Gutenberg author") ) print "Unknown page"

url = "https://en.wikipedia.org/w/api.php?action=query&titles=" urlencodeawk(entity) "&format=json" jsonin = http2var(url) if(jsonin ~ "\"missing\"") return 0 return 1 }

function addList(page, mainPage,archive) {
 * 1) Add a new entry to list
 * 1) Add a new entry to list

archive = getArchive(page) if(empty(archive)) { stdErr(dateeight ": Unable to determine archive number (template missing?): " page) print(dateeight ": Unable to determine archive number (template missing?): " page) >> Logerror return 0 } L[page]["page"] = page mainPage = L[page]["page"] sub(/^Talk:/, "", mainPage) L[page]["prpage"] = "Wikipedia:Peer review/" mainPage "/" archive L[page]["date"] = dateeight # default to todays date return 1

}

function inList(page, k) { for(k in L) { if(L[k]["page"] == page) return 1 } return 0 }
 * 1) Check if page exists in list
 * 1) Check if page exists in list

function loadList(file, a,b,page,i) { if(!checkexists(file)) return for(i = 1; i <= splitn(file, a, i); i++) { if(split(a[i], b, " ") == 3) { page = strip(b[1]) L[page]["page"] = page L[page]["date"] = strip(b[2]) L[page]["prpage"] = strip(b[3]) } } }

function saveList(file) { if(checkexists(file)) sys2var(Exe["rm"] " -r " file) for(page in L)    print L[page]["page"] "  " L[page]["date"] "  " L[page]["prpage"] >> file }

function numberDays(page,  old,new,c,a) {
 * 1) Number of days between today and date in L[page]["date"] rounded down
 * 1) Number of days between today and date in L[page]["date"] rounded down

old = sys2var(Exe["date"] " '+%s' -d " L[page]["date"]) new = sys2var(Exe["date"] " '+%s' -d " dateeight) c = (new - old) / 86400 split(c, a, /[.]/) return a[1]

}

function getArchive(page, fp,d,dd,ddd) {
 * 1) Retrieve 'archive1' or 'archive2', etc.. defined in
 * 1) Retrieve 'archive1' or 'archive2', etc.. defined in

fp = sys2var(Exe["wikiget"] " -w " shquote(page)) if(match(fp, templateRE, d) > 0) { if(match(d[0], /archivespace:*=space:*[0-9]{1,2}/, dd) > 0) { match(dd[0], /[0-9]{1,2}/, ddd) return "archive" ddd[0] }   return "archive1" } }

function deleteTemplate(page, temp,fp,d,command,result,comres) {
 * 1) Delete template from page
 * 1) Delete template from page

result = 0 temp = Home "temp.txt" fp = sys2var(Exe["wikiget"] " -w " shquote(page)) if(match(fp, templateRE "[ ]*(\\n)?", d) > 0) { fp = subs(d[0], "", fp) print fp > temp close(temp) command = Exe["wikiget"] " -E " shquote(page) " -S " shquote("Removing after " MaxDays " days without creation of " L[page]["prpage"] " per User:GreenC bot/Job 20") " -P " shquote(temp) comres = sys2var(command) if(comres != "Success") { stdErr(dateeight ": Unable to upload (" comres "): " command) print(dateeight ": Unable to upload (" comres "): " command) >> Logerror }   else { print(dateeight ": Successful upload: " command) >> Logfile result = 1 } }  else { stdErr(dateeight ": Unable to find template in page: " page) print(dateeight ": Unable to find template in page: " page) >> Logerror } return result }

function main( cat,a,i,watch,b) {

loadList(Home "list.txt") cat = sys2var(Exe["wikiget"] " -c \"Peer review requests not opened\"")

# Remove template from page and from list, if date expired # Add to list if in category but not in list

for(i = 1; i <= splitn(cat "\n", a, i); i++) { a[i] = strip(a[i]) if(inList(a[i])) { if(int(numberDays(a[i])) >= MaxDays) {   # number of days between now and L[page]["date"] >= MaxDays if(!pageExists(L[a[i]]["prpage"])) { if(deleteTemplate(a[i])) delete L[a[i]] }     }          }    else addList(a[i]) }

# Remove from list if not in category

for(b in L) { watch = 0 for(i = 1; i <= length(a); i++) { if(L[b]["page"] == a[i]) watch = 1 }   if(watch == 0) delete L[b] }

saveList(Home "list.txt")

}

BEGIN {

IGNORECASE = 1

Home = "/data/project/botwikiawk/peerr/"

MaxDays = 7   # Number of days to wait before removing template

# Template:Peer review templateRE = "[{][{]space:*(peer review|peerreview|pr)space:*[|][^}]*[}][}]"

# wikiget : https://github.com/greencardamom/Wikiget Exe["wikiget"] = "/data/project/botwikiawk/BotWikiAwk/bin/wikiget" Exe["wget"]   = "/usr/bin/wget" Exe["date"]   = "/bin/date" Exe["rm"]     = "/bin/rm"

Logfile = Home "logpeerr.log" Logerror = Home "logerror.log"

main

}