User:Reza1615/BOT/Category redirect.py
< User:Reza1615 | BOT
At first we should get query for categories which has langlink in redirect_category pages.
You should replace 'رده_بهتر' names which are listed in Q5828850 for local wikis
SELECT DISTINCT page_title FROM page WHERE page_id IN (SELECT tl_from FROM templatelinks WHERE tl_title = 'رده_بهتر' AND tl_namespace = 10) and page_id IN (SELECT DISTINCT page_id
FROM langlinks LEFT JOIN page
ON page_id = ll_from
WHERE (ll_lang = "en" or ll_lang = "ar" or ll_lang = "fr" or ll_lang = "tr" or ll_lang = "de" or ll_lang = "it" or ll_lang = "ur" or ll_lang = "tk" or ll_lang = "az" or ll_lang = "sq" or ll_lang = "uz" or ll_lang = "uk" or ll_lang = "zh")
AND page_namespace = 14) and page_namespace = 14;
code edit
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Reza (User:reza1615)
# Distributed under the terms of the CC-BY-SA 3.0 .
# -*- coding: utf-8 -*-
import wikipedia,pagegenerators,sys
import category,query,login,config
resultdata=u'\n'
wikipedia.config.put_throttle = 0
wikipedia.put_throttle.setDelay()
faSite=wikipedia.getSite('fa')
def login_wiki(mode):
if mode==1:
dataSite=wikipedia.getSite('wikidata','wikidata')
if mode==2:
dataSite=wikipedia.getSite('fa','wikipedia')
passwords='16151615'
usernames='Rezabot'
botlog=login.LoginManager(password=passwords,username=usernames,site=dataSite)
botlog.login()
def check_user(username):
username=username.replace(u' ',u'_')
params = {
'action': 'query',
'list': 'users',
'ususers': username,
'usprop':'editcount'
}
try:
usernamequery = query.GetData(params,wikipedia.getSite('fa'), encodeTitle = True)
if usernamequery[u'query'][u'users'][0][u'editcount']>3000:
return True
else:
return False
except:
return False
def move (oldCatTitle,newCatTitle):
cat=category.CategoryMoveRobot (u'رده:'+oldCatTitle,u'رده:'+ newCatTitle, batchMode=False,
editSummary=u'[[وپ:دار|ربات:انتقال رده]] > [[:رده:'+oldCatTitle+u']] به [[:رده:'+ newCatTitle+u']]', inPlace=False, moveCatPage=True,
deleteEmptySourceCat=True, titleRegex=None,
useSummaryForDeletion=True, withHistory=False)
cat.run()
def runn(text):
errors=u'\n'
text=text.replace(u'\r',u'').replace(u']]',u'').replace(u'[[',u'').replace(u'*',u'').replace(u'==>',u'@').replace(u'=>',u'@').replace(u'-->',u'@').replace(u'->',u'@').replace(u'>>',u'@').replace(u'>',u'@').replace(u'رده:',u'').replace(u'category:',u'').replace(u'Category:',u'').replace(u'#',u'').strip()
lines=text.split(u'\n')
login_wiki(2)
login_wiki(1)
secondsecList=[]
if u'{{اصلاح آیتم}}' in text:
correct_item=True
else:
correct_item=False
for line in lines:
if line.strip():
if line.find(u'کاربر:')==-1 and line.find(u'ویکیپدیا:')==-1 and line.find(u'الگو:')==-1 and line.find(u'}}')==-1 and line.find(u'{{')==-1:
if correct_item:
firstsec=line.split(u'@')[0].strip()
secondsec=firstsec
fapage=wikipedia.Page(faSite,u'رده:'+firstsec)
try:
text_second=fapage.get()
text_second=text_second.replace(u' |',u'|').replace(u'| ',u'|').replace(u'{ ',u'{').replace(u' }',u'}')
text_second=text_second.replace(u' |',u'|').replace(u'| ',u'|').replace(u'{ ',u'{').replace(u' }',u'}')
if u'{{رده بهتر' in text_second:
secondsec=text_second.split(u'{{رده بهتر|')[1].split(u'}}')[0].replace(u'رده:',u'').replace(u'category:',u'').replace(u'Category:',u'').strip()
except:
wikipedia.output(u'Bot could not open رده:'+firstsec+' so it passed')
continue
else:
if line.find(u'@')!=-1:
firstsec=line.split(u'@')[0].strip()
secondsec=line.split(u'@')[1].strip()
else:
continue
if firstsec==secondsec:
continue
wikipedia.output(u'-----------------------------------')
wikipedia.output(u'\03{lightred}'+firstsec+u' > '+secondsec+u'\03{default}')
facat=wikipedia.Page(faSite,u'رده:'+firstsec)
#if facat:
try:
facat_text=facat.get()
facat_text=facat_text.replace(u'{{الگو:',u'{{').strip()
if facat_text.find(u'{{رده بهتر|')==-1:
facat.put(u'{{رده بهتر|'+secondsec+u'}}\n'+facat_text,u'ربات:برچسب رده بهتر برای جلوگیری از کار رباتهای دیگر')
secondsec_cat=u"رده:"+secondsec
secondsecList.append(secondsec_cat)
firstsec_page=wikipedia.Page(faSite,u'رده:'+firstsec)
wdata=wikipedia.DataPage(firstsec_page)
move(firstsec,secondsec)
if wdata.exists():
try:
items=wdata.get()
wdata.setitem(summary=u"update item",items={'type': u'sitelink', 'site': 'fa', 'title': secondsec_cat})
wdata.setitem(summary=u"update label",items={'type': u'item', 'label': 'fa', 'value': secondsec})
wikipedia.output(u"\03{lightblue}Wikidata is updated!\03{default}")
except:
wikipedia.output(u"\03{lightred}Category in wikidata had error or it dosen't have any item!\03{default}")
pass
facat2=wikipedia.Page(faSite,secondsec_cat)
facat2_text=facat2.get()
facat2_text=facat2_text.replace(u'{{الگو:',u'{{').strip()
if facat2_text.find(u'{{رده بهتر|')!=-1:
facat2.put(facat2_text.replace(u'{{رده بهتر|'+facat2_text.split(u'{{رده بهتر|')[1].split(u'}}')[0]+u'}}\n',u''),u'ربات:برداشتن برچسب رده بهتر')
facat=wikipedia.Page(faSite,u'رده:'+firstsec)
try:
facat_text=facat.get()
facat_text=facat_text.replace(u'{{الگو:',u'{{').replace(u'{{template:',u'{{').strip()
if correct_item:
facat.put("\n{{رده بهتر|"+secondsec+u"}}",u"ربات:اصلاح برچسب رده بهتر")
else:
facat.put(u"{{حذف سریع|{{قلم رنگ|قرمز تیره|ربات:انتقالیافته به [[:رده:"+secondsec+u"]] . '''مدیر محترم:'''}}اگر این رده تغییرمسیر نامناسب است، رده را حذف نمائید. در غیراین صورت برچسب حذف را بردارید تا {{الگو|رده بهتر}} بماند.}}\n{{رده بهتر|"+secondsec+u"}}",u"ربات:افزودن برچسب حذف سریع به ردهٔ انتقالیافته.")
wikipedia.output(u'\03{lightgreen}...delete tage added to رده:'+firstsec+u' !\03{default}')
except:
continue
except:
errors+=firstsec+u'@'+secondsec+u'\n'
for cat in secondsecList:
cat_page=wikipedia.Page(faSite,cat)
text=cat_page.get()
text=text.replace(u' |',u'|').replace(u'| ',u'|').replace(u'{ ',u'{').replace(u' }',u'}')
text=text.replace(u' |',u'|').replace(u'| ',u'|').replace(u'{ ',u'{').replace(u' }',u'}')
title=cat_page.title()
title2=title.replace(u'رده:',u'').replace(u'category:',u'').replace(u'Category:',u'')
if text.find(title2)!=-1:
new_text = text.replace(u'{{رده بهتر|'+title+u'}}',u'').replace(u'{{رده بهتر|'+title2+u'}}',u'')
if new_text!=text:
cat_page.put(new_text,u'ربات:حذف رده بهتر نادرست')
wikipedia.output(u'Bot:removing incorrect template')
if errors.strip():
fapage=wikipedia.Page(faSite,u'ویکیپدیا:درخواست انتقال رده')
text=fapage.get()
fapage.put(text+errors,u'ربات:افزودن موارد انجام نشده!')
def main():
fapage=wikipedia.Page(faSite,u'ویکیپدیا:درخواست انتقال رده')
try:
text=fapage.get()
page_history=fapage.getVersionHistory()
Last_user=page_history[0][2]
except wikipedia.IsRedirectPage:
fapage = fapage.getRedirectTarget()
try:
text=fapage.get()
page_history=fapage.getVersionHistory()
Last_user=page_history[0][2]
except:
wikipedia.output(u"requested page didn't find!")
wikipedia.stopme()
sys.exit()
except:
wikipedia.output(u"requested page didn't find!")
wikipedia.stopme()
sys.exit()
if Last_user!=u'Rezabot' and check_user(Last_user):
fapage.put(u'{{/بالا}}',u'ربات:انتقال رده انجام شد!')
runn (text)
else:
wikipedia.output(Last_user)
wikipedia.output(u'user was not valide')
if __name__ == "__main__":
main()