Jump to content

User:GreenC bot/Job 9/source

From Wikipedia, the free encyclopedia
#!/usr/bin/gawk -bE

#
# shorty - convert magic keyword {{SHORTDESC:<title>}} to {{Short description:<title>}}
#          and move to top of page.
#          https://en.wikipedia.org/wiki/User:GreenC_bot/Job_9
#
# The MIT License (MIT)
#
# Copyright (c) January 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.

# Dependency:
#  https://github.com/greencardamom/BotWikiAwk

BEGIN {
  BotName = "shorty"
}

@include "botwiki.awk"
@include "library.awk"

BEGIN {

  Email = "<email addr>"
  Exe["mailx"] = "/usr/bin/mailx"
  Log = Home "log/"
  Re1 = "[{][ ]*[{][ ]*SHORTDESC[ ]*[:][ ]*[^}]*[}][ ]*[}][ ]*[\n]?"
  Re2 = "^([{][ ]*[{][ ]*SHORTDESC[ ]*[:][ ]*)|([ ]*[}][ ]*[}][ ]*[\n]?)$"
  Sum = "Convert SHORTDESC magic keyword to template (via [[User:GreenC bot/Job 9|shorty]] bot)"

  main()

}

function main(  list,i,name,fp,c,field,seps,sdt,done,k,a) {

  list = sys2var(Exe["wikiget"] " -a insource:SHORTDESC")   # download list of articles from an insource: search
  if ( list) {
    for(i = 1; i <= splitn(list "\n", name, i); i++) {      # split list and iterate
      fp = sys2var(Exe["wikiget"] " -w " shquote(name[i]))  # download article wikisource
      if ( match(stripnowikicom(fp), Re1) ) {               # skip any in nowiki and wikicomment
        c = patsplit(fp, field, Re1, seps)                  # extract the magic word into field[1]
        if ( c == 1 ) {                                     # if more than 1 throw error and skip
          # stdErr("Processing " name[i])                   # debugging
          sdt = gsubi(Re2, "", field[1])                    # save magic word description
          field[1] = ""                                     # delete magic word
          fp = unpatsplit(field, seps)                      # rebuild article without it
          fp = "{{Short description|" sdt "}}\n" fp         # rebuild article with new template prepended at top
          IGNORECASE=1                                      # case insensitive
          upload(fp, name[i], Sum, Log, BotName, "en")      # upload new article
          done[name[i]] = 1                                 # log done
          # if(i==4) break                                  # debugging
        }
        else {
          print name[i] " --- Error: too many or too few SHORTDESC" >> Log "error"
        }
      }
    }
  }

  # Email result

  if(length(done) > 0) {

    print "\nPages processed: " length(name) > Log "email"
    print "\nPages fixed: \n" >> Log "email"
    for(k in done)
      print "\t" k >> Log "email"
    sleep(60)                                                # Give wiki servers time to catch up
    print "\nPages not fixed: " splitn(sys2var(Exe["wikiget"] " -a insource:SHORTDESC") "\n", a) >> Log "email"
    print "\nSearch for not fixed: insource:SHORTDESC\n" >> Log "email"
    close(Log "email")
    sys2var(Exe["mailx"] " -s \"NOTICE: Shorty bot fixed " length(done) " pages.\" " Email " < " Log "email")

  }
}