def check(revision): title = revision["title"] pywikibot.output(u"check page %s @ %s" % (title, wp.getTime())) revid = revision["revid"] oldrevid = revision["old_revid"] page = wp.Page(title) textnew = page.getOldVersion(revid) textold = u"" if oldrevid == 0 else page.getOldVersion(oldrevid) if site.getRedirectText(textnew): return if site.getRedirectText(textold): textold = "" addedlinks = (set(site.pagelinks_by_text(textnew, title)) - set(site.pagelinks_by_text(textold, title))) disamlinks = [] for link in addedlinks: if ":" in link.title(): continue if link.title().startswith(u"#"): continue try: lname = link.title() if link.isRedirectPage(): link = link.getRedirectTarget() if link.isDisambig(): disamlinks.append(lname) except: wp.error() if disamlinks: save(revision["user"], title, set(disamlinks))
def _pagelinks_by_text(self, text, title=None, expand=False): """ This function extract lins. """ if not expand: links = [] for link in lre.pats["link"].finditer(text): links.append("[[" + link.group("title") + "]]") text = "".join(links) r = api.Request(site=self, action="parse", text=text, prop="links") if title: r["title"] = title try: pages = [wp.Page(item['*']) for item in r.submit()['parse']['links']] except: wp.error() return [] else: return pages
def main(): def receive_signal(signum, stack): pywikibot.output("Flush immediately!") flush() signal.signal(signal.SIGUSR2, receive_signal) prevday = pywikibot.Timestamp.today().day for rev in lrepeat.repeat(site, site.recentchanges, lambda x: x["revid"], 60, showRedirects=False, showBot=False, changetype=["edit", "new"], namespaces=conf.namespaces): try: check(rev) except: wp.error() if ((prevday != pywikibot.Timestamp.today().day) and (pywikibot.Timestamp.today().day % 3 == 1)): try: flush() except: wp.error() prevday = pywikibot.Timestamp.today().day
def flush(): pywikibot.output("begin flushing") global container insertDisamT = wp.Page(conf.messageTemplate).get() for user in container: try: notify(wp.User(user), container[user], insertDisamT) except: wp.error() container = {} pywikibot.output("end flushing")
def _parse(self, text): r = api.Request(site=self, action="parse", text=text, contentmodel="wikitext") try: result = r.submit() except: wp.error() else: return result['parse']['text']['*']
def main(): gen = lrepeat.repeat(site, site.recentchanges, lambda x: x["revid"], 60, showRedirects=False, changetype=["edit", "new"], showBot=False, namespaces=[0], start=site.getcurrenttime() - ltime.td(hours=1)) for rev in gen: try: check(rev) except: wp.error() pass
def main(): #tl = wp.Page(u"Template:บาเบล") for page in site.allpages(filterredir=False, content=True): #for page in tl.embeddedin(content=True): #for page in tl: #page = wp.Page(u"รายชื่อวัดในจังหวัดชัยนาท") pywikibot.output(">>>" + page.title()) text = fix(page.get()) if page.get() != text: pywikibot.showDiff(page.get(), text) if raw_input("...") == "y": try: page.put(text, u"โรบอต: เก็บกวาด", async=True) except: wp.error() pass
def main(): title = u"ผู้ใช้:Nullzerobot/บริการจัดหมวดหมู่/หมวดหมู่ที่รอการจัด" confpage = u"ผู้ใช้:Nullzerobot/บริการจัดหมวดหมู่/" header, table, disable = lservice.service( page=wp.Page(title), confpage=wp.Page(confpage), operation="major", verify=lambda x: True, summary=summaryWithTime, # debug=True, ) for line in table: try: doall(lre.pats["name"].find(line[1], 1), lre.pats["name"].find(line[2], 1)) except: wp.error()
def subthread(pagep, catinp, cat, page): if not page: pywikibot.output(pagep.title() + " not found") return pywikibot.output("thread %s >>> adding..." % page.title()) try: if page.namespace() not in wp.conf.nstl: oldcat = set(page.categories(fromtext=True)) newcat = (oldcat - set([catinp])) | set([cat]) if oldcat != newcat: pywikibot.output("Change!") text = pywikibot.replaceCategoryLinks(page.get(), list(newcat), site) page.put(text, u"autoCategory") else: pywikibot.output("Nothing changes!") except: wp.error()
def main(): page = wp.handlearg("page", args) if page: page = wp.Page(page) dic = page.getVersionHistory(reverseOrder=True, total=1) gen = [{"user": dic[0][2], "title": page.title()}] check = wp.handlearg("check", args) if check: check = wp.Page(check) else: gen = lrepeat.repeat(site, site.recentchanges, lambda x: x["revid"], 60, showRedirects=False, changetype=["new"], showBot=False, namespaces=[0]) for rev in gen: try: process(wp.Page(rev["title"]), check, wp.User(rev["user"])) except: wp.error()
def main(): """Main function""" if wp.handlearg("pending", args): pywikibot.output("move pending entry") title = conf.pageMinor operation = "minor" else: title = conf.pageMajor operation = "major" header, table, disable = lservice.service(page=wp.Page(title), confpage=wp.Page(conf.datwiki), operation=operation, verify=verify, summary=summaryWithTime, #debug=True, ) report = [] pending = [] for i, line in enumerate(table): putline = "|-\n| " + " || ".join(line) if (operation == "minor") or (not disable[i]): templateStat = conf.notDoneTemplate try: domove(lre.pats["name"].find(line[1], 1), lre.pats["name"].find(line[2], 1)) except: wp.error() else: templateStat = conf.doneTemplate putline += " || %s %s" % (templateStat, wp.getTime()) report.append(putline) else: pending.append(putline) appendTable(conf.pageReport, report) appendTable(conf.pageMinor, pending)
def main(): global site site = site.data_repository() prop = pywikibot.Page(site, "Property:P140") #print prop #print prop.get() for page in prop.backlinks(): try: if page.namespace() == 0 and page.title().startswith("Q"): page = pywikibot.ItemPage(site, page.title()) content = page.get() print ">>>", page.title() for claim in content["claims"]["p140"]: target = claim.getTarget() links = target.get()["sitelinks"] if "thwiki" in links: print "thwiki", links["thwiki"] else: print "enwiki", links["enwiki"] except: wp.error()
def _appendpage(self, page, text, comment="", minorEdit=True, botflag=True, nocreate=True): token = self.token(page, "edit") r = api.Request(site=self, action="edit", title=page.title(), appendtext=text, summary=comment, token=token) if minorEdit: r["minor"] = "" if botflag: r["bot"] = "" if nocreate: r["nocreate"] = "" try: r.submit() except: wp.error() else: pywikibot.output(u"Page {} is appended".format(page.title(asLink=True)))
def callback(page, err): if isinstance(err, pywikibot.LockedPage) or isinstance(err, pywikibot.PageNotSaved) or repr(err) == """APIError("protectednamespace-interface", "You're not allowed to edit interface messages", {})""": if hasattr(page, 'u_nomore'): page.u_err = True else: page.u_nomore = True page.put(page.u_text, u"ปรับปรุงหน้าอัตโนมัติโดยบอต", as_group='sysop') return elif err: page.u_err = True if hasattr(page, 'u_err'): pywikibot.output("<!-- Begin error -->") pywikibot.output(page.text) pywikibot.output("<!-- End error -->") page.u_elist.append(u"ผิดพลาด: ไม่เกิดการเขียนหน้า <pre>{}</pre>".format(sys.exc_info()[0])) wp.error() return """ if page.u_checkcat: time.sleep(30) for cat in page.u_checkcat: if cat.isEmptyCategory(): page.u_elist.append((u"[[:{}]] ว่างลงแล้ว " u"แสดงว่าไม่มีพารามิเตอร์ล้าสมัย " u"คุณอาจเขียนคู่มือการใช้งานใหม่" u"และลบการตั้งค่าพารามิเตอร์ล้าสมัยออก").format(cat.title())) """ #for user in config["notifyuser"]: for user in ["Nullzero"]: lnotify.notify("updatePage", wp.User(user).getUserTalkPage(), { "page": page.title(), "error": "".join(map(lambda x: "* " + x + "\n", page.u_elist)), "warn_module": u"และดู [[:หมวดหมู่:หน้าที่มีสคริปต์ผิดพลาด]] " if # มีเดียวิกิ:Scribunto-common-error-category page.namespace() == 828 else "", "page_config": conf.title, "revision": page.latestRevision(), }, u"แจ้งการปรับปรุงหน้าอัตโนมัติ")
def notify(user, dic, insertDisamT): for title, linkset in dic.items(): pagenow = wp.Page(title) if pagenow.exists(): alllinks = [link.title() for link in pagenow.linkedPages()] dic[title] = filter(lambda link: link in alllinks, list(linkset)) else: dic[title] = [] if not dic[title]: del dic[title] if not dic: return pywikibot.output("notifying %s..." % user.title()) usertalk = user.getUserTalkPage() def checkrefuse(fun): if not checkrefuse.val: checkrefuse.val |= fun() checkrefuse.val = False checkrefuse(lambda: not user.isRegistered()) checkrefuse(lambda: not usertalk.exists()) checkrefuse(lambda: usertalk.isRedirectPage()) checkrefuse(lambda: "bot" in user.groups()) checkrefuse(lambda: conf.nonotifycat in usertalk.get()) if checkrefuse.val: notifyreport("\n\n" + dict2str(dic)) else: try: lnotify.notify("dpl", usertalk, {"links": dict2str(dic)}, conf.summary) except: wp.error() pywikibot.output(">>> done!")
def main(): exlist = [exc.group(1) for exc in lre.pats["exc"].finditer(wp.Page(conf.pageConf).get())] pages1, pages2, pages3 = [], [], [] if not args: pywikibot.output("quickscan mode") t = site.getcurrenttime() if t.day == 1: if t.month == 1: t = pywikibot.Timestamp(year=t.year - 1, month=12, day=31) else: t = pywikibot.Timestamp(year=t.year, month=t.month - 1, day=28) else: t = pywikibot.Timestamp(year=t.year, month=t.month, day=t.day - 1) gen1 = site.recentchanges( start=t, reverse=True, showRedirects=False, showBot=False, changetype=["new", "edit"], namespaces=conf.namespaces, ) pages1 = [page["title"] for page in gen1] gen2 = site.logevents(start=t, reverse=True, logtype="move") pages2 = [page.new_title().title() for page in gen2] elif args[0] == "-all": pywikibot.output("fullscan mode") gen3 = () for i in conf.namespaces: gen3 = itertools.chain(gen3, site.allpages(filterredir=False, start=u"ก", namespace=i)) pages3 = [page.title() for page in gen3] pywikibot.output("load all!") else: pages1 = [u"หมวดหมู่:ชาววิกิพีเดียรักองค์โสมฯ"] pywikibot.output("unknown argument") allpages = list(set(filter(lambda x: (ord(u"ก") <= ord(x[0]) <= ord(u"๛")), pages1 + pages2 + pages3))) datasite = site.data_repository() cnti = 0 pywikibot.output("processing %d pages" % len(allpages)) for check in conf.checklist: if check["detectFromTitle"] is None: check["detectFromTitle"] = "[]" # dummy string which invalid for title for checkClaim in check["claims"]: checkClaim["nameItem"] = pywikibot.ItemPage(datasite, checkClaim["nameItem"]) if checkClaim["refItem"] is not None: checkClaim["refItem"] = pywikibot.ItemPage(datasite, checkClaim["refItem"]) for pages in itergroup(allpages, 100): cnti += 1 pywikibot.output("round %d" % cnti) dat = datasite.loadcontent({"sites": site.dbName(), "titles": "|".join(pages)}) for i, qitem in enumerate(dat): pywikibot.output("item %d: %s" % (i, qitem)) if not qitem.lower().startswith("q"): continue item = pywikibot.ItemPage(datasite, qitem) item._content = dat[qitem] super(pywikibot.ItemPage, item).get() # For getting labels data = item.get() editdict = {} page = wp.Page(item.getSitelink(site)) if page.title() in exlist: continue for check in conf.checklist: passCriteria = False description = None if check["detectFromTitle"] in page.title(): passCriteria = True if check["detectFromNamespace"] == page.namespace(): passCriteria = True passAlItem = True for claimCheck in check["claims"]: passItem = False if claimCheck["name"] in data["claims"]: for claim in data["claims"][claimCheck["name"]]: if claim.getTarget() == claimCheck["nameItem"]: passItem = True break if not passItem: passAllItem = False if passCriteria: claim = pywikibot.Claim(datasite, claimCheck["name"]) claim.setTarget(claimCheck["nameItem"]) item.addClaim(claim) if claimCheck["ref"] is not None: claim2 = pywikibot.Claim(datasite, claimCheck["ref"]) claim2.setTarget(claimCheck["refItem"]) claim.addSource(claim2) pywikibot.output("added claim!") passCriteria = passCriteria or passAllItem if (description is None) and passCriteria: description = check["description"] if passCriteria: break oldlabels = None if "th" in data["labels"]: oldlabels = data["labels"]["th"] labels = lre.pats["rmdisam"].sub("", page.title()) if not lre.pats["thai"].search(labels): continue if labels != oldlabels: pywikibot.output("old label: " + unicode(oldlabels)) pywikibot.output("new label: " + unicode(labels)) editdict["labels"] = labels if passCriteria and ( ("th" in data["descriptions"] and data["descriptions"]["th"] != description) or ("th" not in data["descriptions"]) ): editdict["descriptions"] = description out = transform(editdict) if not out: continue pywikibot.output("item: " + qitem) pywikibot.output("title: " + page.title()) try: # raw_input("prompt: ...") item.editEntity(out) except: wp.error() pass
def process(text, page_config): global putWithSysop params = {} for key in conf.seriesKey: params[conf.seriesKey[key]] = [] errorlist = [] deprecated = [] checkcat = [] try: for param in lre.pats["param"].finditer(text + "|"): param = param.group(1) key, dat = param.split("=", 1) key = key.strip() dat = dat.strip() if key in conf.translateKey: params[conf.translateKey[key]] = dat else: num = lre.pats["num"].find(key) key = lre.pats["num"].sub("", key) if key in conf.seriesKey: params[conf.seriesKey[key]].append((num, dat)) else: error("unknown parameter", param) except: wp.error() if "source" in params: pywikibot.output(u"Error when updating %s" % params["source"]) else: pywikibot.output(u"Error when processing page %s" % page_config) return if not checkparams(params): error("something wrong") return if "disable" in params and params["disable"] == conf.yes: return source = wp.Page(params["source"]) page = wp.Page(params["page"]) today = site.getcurrenttime() originalText = page.get() if page.exists() else None if ("stable" in params and (today - pywikibot.Timestamp.fromISOformat( source.getVersionHistory(total=1)[0][1])).days < int(params["stable"])): return params["notifyuser"] = [lre.pats["user0"].find(x.strip(), 1) for x in params["notifyuser"].split("\n")] text = source.get() #======= for i, (num, sfind) in enumerate(params["find"]): newtext = text.replace(parse(sfind), parse(params["replace"][i][1])) if newtext == text and sfind != params["replace"][i][1]: errorlist.append(u"คำเตือน: ไม่เกิดการแทนที่ข้อความที่ {0}".format(num)) text = newtext def matchbrace(s, i): lv = 0 for i in xrange(i, len(s)): if s[i] == "{": lv += 1 elif s[i] == "}": lv -= 1 if lv == 0: return i # not return i + 1 to avoid index out of range for irep, (num, sp) in enumerate(params["param"]): lst = [] for i in lre.finditer(r"\{\{\{\s*" + sp + "\s*[\|\}]", text): begin, end = i.span() end = matchbrace(text, begin) lst.append((begin, "begin")) lst.append((end, "end")) lst = sorted(lst) lst.append((sys.maxint, sys.maxint)) ilst = 0 out = [] for i in xrange(len(text)): if i == lst[ilst][0]: if lst[ilst][1] == "begin": out.append("{{{" + params["translate"][irep][1] + "|") else: out.append("}}}") # we should put text[i] before "}}}", # but since text[i] == "}", there is no problem :) ilst += 1 out.append(text[i]) newtext = "".join(out) if newtext == text: errorlist.append(u"คำเตือน: ไม่เกิดการแปลพารามิเตอร์ที่ {0}".format(num)) text = newtext for i, (num, sdepr) in enumerate(params["depr"]): category = wp.Category("Category:" + page.title().replace(":", "") + u" ที่ใช้พารามิเตอร์ " + sdepr) checkcat.append(category) deprecated.append(u'<includeonly>{{{{#if:{{{{{{{depr}|}}}}}}|[[{cat}]]' .format(depr=sdepr, cat=category.title()) + ((u'<span class="error">พารามิเตอร์ {depr} ' u'ล้าสมัยแล้ว โปรดใช้ {rdepr} แทนที่</span><br />') .format(depr=sdepr, rdepr=params["rdepr"][i][1]) if (params["errordepr"][i][1] == conf.yes) else u'') + u'}}</includeonly>') text = "".join(deprecated) + text #======= if (not errorlist) and (text == originalText): pywikibot.output(u"ไม่มีการเปลี่ยนแปลงในหน้า %s; " u"ยกเลิกการปรับปรุงและแจ้งเตือน" % source.title()) return if debug: pywikibot.showDiff(originalText or "", text) return if "sandbox" in params and params["sandbox"] == conf.yes: page = wp.Page(page.title() + "/sandbox") foundError = False try: page.put(text, u"ปรับปรุงหน้าอัตโนมัติโดยบอต") except (pywikibot.LockedPage, pywikibot.PageNotSaved): try: page.put(text, u"ปรับปรุงหน้าอัตโนมัติโดยบอต", as_group='sysop') except: foundError = True except: foundError = True if foundError: pywikibot.output("<!-- Begin error -->") pywikibot.output(text) pywikibot.output("<!-- End error -->") errorlist.append(u"ผิดพลาด: ไม่เกิดการเขียนหน้า <pre>{}</pre>".format(sys.exc_info()[0])) wp.error() if checkcat: time.sleep(30) for cat in checkcat: if cat.isEmptyCategory(): errorlist.append(u"[[:%s]] ว่างลงแล้ว " u"แสดงว่าไม่มีพารามิเตอร์ล้าสมัย " u"คุณอาจเขียนคู่มือการใช้งานใหม่" u"และลบการตั้งค่าพารามิเตอร์ล้าสมัยออก" % cat.title()) #for user in params["notifyuser"]: for user in ["Nullzero"]: lnotify.notify("updatePage", wp.User(user).getUserTalkPage(), { "page": page.title(), "error": "".join(map(lambda x: "* " + x + "\n", errorlist)), "warn_module": u"และดู [[:หมวดหมู่:หน้าที่มีสคริปต์ผิดพลาด]] " if # มีเดียวิกิ:Scribunto-common-error-category page.namespace() == 828 else "", "page_config": page_config, "revision": page.latestRevision(), }, u"แจ้งการปรับปรุงหน้าอัตโนมัติ")
def _wrap_run(self): try: self._real_run() except: wp.error() self.error = True
def service(page, confpage, operation, verify, summary, debug=False): """ Get: Service page Key to read from config page, Function to verify user Config page Site Summary function. Function: Clear service's page Return: Header of table List of rows Suspicious entry(/row) """ lastrev = int(linfo.getdat(confpage, operation)) oldcontent = page.get() header, table = lwikitable.wiki2table(oldcontent) disable = [False] * len(table) hist = page.fullVersionHistory(total=100) # There is no need to get all revisions, just 100 is fine. for i, version in enumerate(hist): if version[0] == lastrev: hist = hist[:i+1] break hist.reverse() pywikibot.output("Processing %d revision(s)" % len(hist)) for i in xrange(len(hist) - 1): oldv = hist[i][3] newv = hist[i + 1][3] usernew = hist[i + 1][2] try: dummy, cold = lwikitable.wiki2table(oldv) dummy, cnew = lwikitable.wiki2table(newv) except: wp.error() continue oldvc = set([wp.toutf(x) for x in cold]) newvc = set([wp.toutf(x) for x in cnew]) difference = [eval(x) for x in (newvc - oldvc)] if not verify(usernew): for entry in difference: for cnt, fentry in enumerate(table): if entry == fentry: disable[cnt] = True break newcontent = patclear.sub(r"\1\2", oldcontent) if oldcontent != newcontent: if not debug: page = wp.Page(page.title()) page.put(newcontent, summary()) pywikibot.output(page.getVersionHistory(total=1)[0][0]) linfo.putdat(confpage, operation, page.getVersionHistory()[0][0]) return header, table, disable
def process(text, page_config): global putWithSysop params = {} for key in conf.seriesKey: params[conf.seriesKey[key]] = [] errorlist = [] deprecated = [] checkcat = [] try: for param in lre.pats["param"].finditer(text + "|"): param = param.group(1) key, dat = param.split("=", 1) key = key.strip() dat = dat.strip() if key in conf.translateKey: params[conf.translateKey[key]] = dat else: num = lre.pats["num"].find(key) key = lre.pats["num"].sub("", key) if key in conf.seriesKey: params[conf.seriesKey[key]].append((num, dat)) else: error("unknown parameter", param) except: wp.error() if "source" in params: pywikibot.output(u"Error when updating %s" % params["source"]) else: pywikibot.output(u"Error when processing page %s" % page_config) return """ if not checkparams(params): error("something wrong") return """ params["users"] = "'" + "', '".join([lre.pats["user0"].find(x.strip(), 1) for x in params["notifyuser"].split("\n")]) + "'" def changeYes(k): params[k] = u" '{}': True,\n".format(k) if (k in params and params[k] == conf.yes) else "" def changeString(k): params[k] = u" '{}': u'{}',\n".format(k, params[k]) if (k in params) else "" changeYes("disable") changeYes("sandbox") changeYes("stable") changeString("note") changeString("message") output1 = ( u"""\ ############################### page = u'{page}' config[page] = {{ 'source': u'{source}', {disable}{note}{sandbox}{stable}{message} 'users': [{users}], 'findText': [], 'addParam': [], 'obsolete': [], }} """.format(**params)) def space(a): a = unicode(a) return " " * (len(a) + 35) output = "" global signal for key in params: if key == "find": for kk, (i, j) in enumerate(params[key]): output += u"""config[page]['findText'].append(({}, u"{}",\n{}u"{}"))\n""".format(i, parse(j), space(i), parse(params['replace'][kk][1])) if signal: #print output #raw_input("...") signal = False for key in params: if key == "param": for kk, (i, j) in enumerate(params[key]): output += u"""config[page]['addParam'].append(({}, u"{}",\n{}u"{}"))\n""".format(i, j, space(i), params['translate'][kk][1]) for key in params: if key == "depr": for kk, (i, j) in enumerate(params[key]): output += u"""config[page]['obsolete'].append(({}, u"{}", u"{}", u"{}"))\n""".format(i, j, params['rdepr'][kk][1], params['errordepr'][kk][1]) ''' elif key == "param" or key == "translate": print ">>>", key for (i, j) in params[key]: print i, j elif key == "notifyuser": print ">>>", key for user in [lre.pats["user0"].find(x.strip(), 1) for x in params["notifyuser"].split("\n")]: print user else: if isinstance(params[key], list): print ">>>", key, ":" for i in params[key]: print i, params[key] else: print ">>>", key, params[key] ''' if output: output += "\n" if page_config not in allpages: allpages[page_config] = wp.Page(page_config + '!') allpages[page_config].text = "" output = output1 + output + "###############################\n" #allpages[page_config].text += output #print output global allo allo += output