def delete_page(page, comment): for i in range(11): try: page.delete(comment) return except APIError as e: if i == 10: raise e errandmsg("APIError, try #%s: %s" % (i + 1, e))
def process_page(page, index, parsed): pagetitle = unicode(page.title()) def pagemsg(txt): msg("Page %s %s: %s" % (index, pagetitle, txt)) pagemsg("Processing") notes = [] for t in parsed.filter_templates(): origt = unicode(t) tn = tname(t) if tn in templates_to_rename: template_specs = templates_to_rename[tn] new_name, new_params = template_specs[0], template_specs[1:] main_entry_param = "adj" if new_name == "bg-adj form of" else "noun" blib.set_template_name(t, new_name) # Fetch all params. params = [] old_1 = getparam(t, "1") for param in t.params: pname = unicode(param.name) if pname.strip() in ["1", "lang", "sc"]: continue if pname.strip() in ["2", "3", "4"]: errandmsg("WARNING: Found %s= in %s" % (pname.strip(), origt)) params.append((pname, param.value, param.showkey)) # Erase all params. del t.params[:] # Put back basic params for param_index, paramval in enumerate(new_params): t.add(str(param_index + 1), paramval) if not old_1: errandmsg("WARNING: No 1= in %s" % origt) else: t.add(main_entry_param, old_1) # Put remaining parameters in order. for name, value, showkey in params: t.add(name, value, showkey=showkey, preserve_spacing=False) notes.append( "rename {{%s}} to {{%s|%s|%s={{{1}}}}}" % (tn, new_name, "|".join(new_params), main_entry_param)) if unicode(t) != origt: pagemsg("Replaced <%s> with <%s>" % (origt, unicode(t))) return unicode(parsed), notes
def errandpagemsg(txt): errandmsg("Page %s %s: %s" % (index, pagetitle, txt))
def do_process_page(page, index, parsed=None): pagetitle = unicode(page.title()) def pagemsg(txt): msg("Page %s %s: %s" % (index, pagetitle, txt)) pagetext = blib.safe_page_text(page, pagemsg) return process_text_on_page_for_full_conj(index, pagetitle, pagetext, verbs) page = pywikibot.Page(site, verb) blib.do_edit(page, lineno, do_process_page, save=args.save, verbose=args.verbose, diff=args.diff) elif args.mode == "generate": verbs = {} for line in codecs.open(args.direcfile, "r", encoding="utf-8"): line = line.strip() if line.startswith("#"): continue if " " not in line: errandmsg("WARNING: No space in line: %s" % line) continue verb, spec = line.split(" ", 1) verbs[verb] = spec def do_process_page(page, index): return process_page_for_generate(page, index, verbs) blib.do_pagefile_cats_refs(args, start, end, do_process_page) else: lineno = 0 for line in codecs.open(args.direcfile, "r", encoding="utf-8"): lineno += 1 line = line.strip() if line.startswith("#"): continue if " " not in line: errandmsg("WARNING: No space in line: %s" % line)
def process_page(page, index, parsed, move_dot, rename): pagetitle = unicode(page.title()) def pagemsg(txt): msg("Page %s %s: %s" % (index, pagetitle, txt)) pagemsg("Processing") notes = [] text = unicode(page.text) if ":" in pagetitle and not re.search( "^(Citations|Appendix|Reconstruction|Transwiki|Talk|Wiktionary|[A-Za-z]+ talk):", pagetitle): pagemsg("WARNING: Colon in page title and not a recognized namespace to include, skipping page") return None, None if move_dot: templates_to_replace = [] for t in parsed.filter_templates(): tn = tname(t) if tn in all_he_form_of_templates: dot = getparam(t, ".") if dot: origt = unicode(t) rmparam(t, ".") newt = unicode(t) + dot templates_to_replace.append((origt, newt)) for curr_template, repl_template in templates_to_replace: found_curr_template = curr_template in text if not found_curr_template: pagemsg("WARNING: Unable to locate template: %s" % curr_template) continue found_repl_template = repl_template in text if found_repl_template: pagemsg("WARNING: Already found template with period: %s" % repl_template) continue newtext = text.replace(curr_template, repl_template) newtext_text_diff = len(newtext) - len(text) repl_curr_diff = len(repl_template) - len(curr_template) ratio = float(newtext_text_diff) / repl_curr_diff if ratio == int(ratio): if int(ratio) > 1: pagemsg("WARNING: Replaced %s occurrences of curr=%s with repl=%s" % (int(ratio), curr_template, repl_template)) else: pagemsg("WARNING: Something wrong, length mismatch during replacement: Expected length change=%s, actual=%s, ratio=%.2f, curr=%s, repl=%s" % (repl_curr_diff, newtext_text_diff, ratio, curr_template, repl_template)) text = newtext notes.append("move .= outside of {{he-*}} template") if rename: parsed = blib.parse_text(text) for t in parsed.filter_templates(): origt = unicode(t) tn = tname(t) if tn in all_he_form_of_template_map: newname, add_nocap = all_he_form_of_template_map[tn] add_nocap_msg = "|nocap=1" if add_nocap else "" newspecs = None if "|" in newname: newname, newspecs = newname.split("|") blib.set_template_name(t, newname) # Fetch all params. params = [] old_1 = getparam(t, "1") for param in t.params: pname = unicode(param.name) if pname.strip() in ["1", "lang", "sc"]: continue if pname.strip() in ( newname == "he-infinitive of" and ["3", "4"] or ["2", "3", "4"] ): errandmsg("WARNING: Found %s= in %s" % (pname.strip(), origt)) params.append((pname, param.value, param.showkey)) # Erase all params. del t.params[:] # Put back basic params t.add("1", old_1) if newname == "he-verb form of": assert newspecs t.add("2", newspecs) notes.append("rename {{%s}} to {{%s|{{{1}}}|%s%s}}" % (tn, newname, newspecs, add_nocap_msg)) elif newname == "he-noun form of" and newspecs: newparam, newval = newspecs.split("=") t.add(newparam, newval) notes.append("rename {{%s}} to {{%s|{{{1}}}|%s=%s%s}}" % (tn, newname, newparam, newval, add_nocap_msg)) else: notes.append("rename {{%s}} to {{%s%s}}" % (tn, newname, add_nocap_msg)) # Put remaining parameters in order. for name, value, showkey in params: # More hacking for 'he-form of sing cons': p -> pp, g -> pg, n -> pn if newname == "he-noun form of" and newspecs: if name in ["p", "g", "n"]: name = "p" + name t.add(name, value, showkey=showkey, preserve_spacing=False) # Finally add nocap=1 if requested. if add_nocap: t.add("nocap", "1") if unicode(t) != origt: pagemsg("Replaced <%s> with <%s>" % (origt, unicode(t))) text = unicode(parsed) return text, notes
def errandpagemsg(txt): errandmsg("Page %s %s: form %s %s: %s" % (index, lemma, formind, formval, txt))
def errandpagemsg(txt): errandmsg("Page %s %s: %s" % (index, lemma, txt))
return text, notes parser = blib.create_argparser( "Push manual {{fr-IPA}} replacements for {{IPA|fr}}") parser.add_argument("--direcfile", help="File of directives", required=True) args = parser.parse_args() start, end = blib.parse_start_end(args.start, args.end) for index, line in blib.iter_items( codecs.open(args.direcfile, "r", encoding="utf-8"), start, end): line = line.strip() m = re.search( r"^(.*?)\|Page [0-9]+ (.*?): WARNING: Can't replace (\{\{IPA\|fr\|.*?\}\}) with (\{\{.*?\}\}) because auto-generated pron .*$", line) if not m: errandmsg("Unrecognized line: %s" % line) continue respelling, page, orig_template, repl_template = m.groups() def do_process_page(page, index, parsed): return process_page(page, index, line, respelling, orig_template, repl_template, args) blib.do_edit(pywikibot.Page(site, page), index, do_process_page, save=args.save, verbose=args.verbose, diff=args.diff)
parser = blib.create_argparser( "Clean up various form-of templates needing text moved outside of template into separate template" ) parser.add_argument('--move-dot', help="Move .= outside of template", action="store_true") parser.add_argument("--pagefile", help="List of pages to process.") args = parser.parse_args() start, end = blib.parse_start_end(args.start, args.end) init_all_templates(args.move_dot) for template in all_templates: errandmsg("Processing references to Template:%s" % template) for i, page in blib.references("Template:%s" % template, start, end): blib.do_edit(page, i, process_page, save=args.save, verbose=args.verbose) if args.pagefile: pages = [x.rstrip('\n') for x in codecs.open(args.pagefile, "r", "utf-8")] for i, page in blib.iter_items(pages, start, end): blib.do_edit(pywikibot.Page(site, page), i, process_page_2, save=args.save, verbose=args.verbose)
def errandpagemsg(txt): errandmsg("Page %s %s: %s" % (nextpage, tempname, txt))
blib.safe_page_save(page, comment, errandpagemsg) else: pagemsg("Would save with comment = %s" % comment) while True: try: line = next(lines) except StopIteration: break if in_multiline and re.search("^-+ end text -+$", line): in_multiline = False save_template_doc(tempname, "".join(templines), args.save) elif in_multiline: if line.rstrip('\n').endswith(':'): errandmsg("WARNING: Possible missing ----- end text -----: %s" % line.rstrip('\n')) templines.append(line) else: line = line.rstrip('\n') if line.endswith(':'): tempname = line[:-1] in_multiline = True templines = [] else: m = re.search('^(.*?):(.*)$', line) assert m tempname = m.group(1) tempparams = m.group(2).split(",") params = {} for tp in tempparams: if tp in [
doc_comment = "Delete documentation page of " + re.sub( "^([Dd]elete|[Rr]emove) ", "", comment) def delete_page(page, comment): for i in range(11): try: page.delete(comment) return except APIError as e: if i == 10: raise e errandmsg("APIError, try #%s: %s" % (i + 1, e)) for i, pagename in blib.iter_items(pages_to_delete, start, end): page = pywikibot.Page(site, pagename) if page.exists(): msg("Deleting %s (comment=%s)" % (page.title(), comment)) delete_page(page, '%s (content was "%s")' % (comment, unicode(page.text))) errandmsg("Page [[%s]] deleted" % page.title()) if args.delete_docs: doc_page = pywikibot.Page(site, "%s/documentation" % pagename) if doc_page.exists(): msg("Deleting %s (comment=%s)" % (doc_page.title(), doc_comment)) delete_page( doc_page, '%s (content was "%s")' % (doc_comment, unicode(doc_page.text))) errandmsg("Page [[%s]] deleted" % doc_page.title())