ویکی‌پدیا:درخواست‌های ربات/افزودن الگو ناوباکس به مقالات

از ویکی‌پدیا، دانشنامهٔ آزاد

این ربات برای افزودن الگوی ناوباکس به مقالاتی است که درون ناوباکس پیوند دارند.

آرگومان‌ها[ویرایش]

این ربات با همه آرگومان‌های استاندارد پای ویکی‌پدیا کار می کند مانند

  • -start:template:!
  • -newtem:100
  • -file:text.txt

نمونه دستور[ویرایش]

این دستور به ۱۰۰ الگوی جدید ایجاد شده در صورتی که ناوباکس باشند مراجعه می‌کند و به مقالاتشان آن ناوباکس‌ها را می افزاید

python navebox.py -newtem:100

نمونه ۲[ویرایش]

این دستور به مقالات موجود در ناوباکس‌های فهرست شده در یک فایل متنی الگو ناوباکس می افزاید.

همه الگوها را به صورت زیر

[[الگو:تست]]
[[الگو:تست۱]]
[[الگو:تست۲]]
[[الگو:تست۳]]

در فایل text.txt ذخیره کنید

python navebox.py -file:text.txt

نکته[ویرایش]

برای جلوگیری از خرابکاری در مقالات و نیافزودن ناوباکس‌های اشتباه، این ربات فقط ناوباکس‌هایی که توسط کاربران بالای ۱۰۰۰ ویرایش ساخته می‌شود را می‌افزاید.

#!/usr/bin/python
# Reza (User:reza1615)
# -*- coding: utf-8 -*-
import pywikibot, json, sys, re, os
from pywikibot import pagegenerators
fasite = pywikibot.Site('fa','wikipedia')
_cache={}
botversion='1706'
boxes=['infobox','Geobox','Taxobo', 'جعبه']

def solve_redirect(tem,fapage,fapage_redi,delink):
    tem = pywikibot.Page(fasite,tem.title())  
    try:
        text=tem.get()
        text=text.replace('[[ ','[[').replace(' ]]',']]').replace(' |','|').replace('[['+fapage.title()+']]','[['+fapage_redi.title()+'|'+fapage.title()+']]')
        text=text.replace('[['+fapage.title()+'|','[['+fapage_redi.title()+'|')
        tem.put(text,'ربات:اصلاح تغییرمسیر')
        
    except:
        pass

def link_filtering(tem_text,links):
    mytext='\n'
    tem_text_f=tem_text.replace('\r','')
    tem_text_f=tem_text_f.replace('\n*',' ')
    tem_text_f=tem_text_f.replace('    |','|').replace('   |','|').replace('  |','|').replace(' |','|')
    tem_text_f=tem_text_f.replace('    =','=').replace('   =','=').replace('  =','=').replace(' =','=')
    
    our_test_text=re.sub(r'\{\{ *[Nn]ational.*?squad','',tem_text_f)
    if our_test_text!=tem_text_f:
        for i in tem_text_f.split('\n|'):
            if 'p1' not in i and 'p2' not in i and 'p3' not in i and 'p4' not in i and 'p5' not in i and 'p6' not in i and 'p7' not in i and 'p8' not in i and 'p9' not in i and 'p0' not in i and 'coach' not in i:
                mytext+=i
    elif tem_text_f!=re.sub(r'\{\{ *[nN]avbox with columns','',tem_text_f):
        for i in tem_text_f.split('\n|'):
            if re.sub(r'^ *col\d{1,2} *\=','',i)==i:
                mytext+=i
    elif tem_text_f!=re.sub(r'\{\{ *[Yy]ear [Nn]obel [pP]rize [Ww]inners','',tem_text_f):
        for i in tem_text_f.split('\n|'):
            if re.sub(r'(chemistry|physics|medicine|economy|literature|peace)','',i)==i:
                
                mytext+=i
    elif tem_text_f!=re.sub(r'\{\{ *([Cc]ounty|جستارهای کشور)','',tem_text_f):
        for i in tem_text_f.split('\n|'):
            if re.sub(r'^ *(cr|md|ua|bd|ct|rv|cl|history|geography|politics|military|economy|society|culture|symbols) *\=','',i)==i:
                
                mytext+=i
    elif tem_text_f!=re.sub(r'\{\{ *([Uu]S state navigation box|[uU]S county navigation box)','',tem_text_f):
        for i in tem_text_f.split('\n|'):
            for i in range(1, 9):
                if 'body{}'.format(i) not in i:
                    mytext+=i
    else:
        list_ = ['list ', 'list=', 'list1', 'list2', 'list3', 'list4', 'list5', 'list6', 'list7', 'list8', 'list9', 'list0', 'فهرست۰', 'فهرست۹', 'فهرست۸', 'فهرست۷', 'فهرست۶', 'فهرست۵', 'فهرست۴', 'فهرست۳', 'فهرست۲', 'فهرست۱', 'content1', 'content2', 'content3', 'content4', 'content5', 'content6', 'content7', 'content8', 'content9', 'content0']
        for i in tem_text_f.split('\n|'):
            for j in list_:
                if j in i:
                    break
            else:
                mytext+=i
    black_text=' '
    dict={'<noinclude>':'</noinclude>','{{یادکرد':'}}','<ref':'</ref','{{cite':'}}','{{Cite':'}}'}
    for a in dict:
        count=0
        for i in mytext.split(a):
            count+=1
            if count>1:
               black_text+=i.split(dict[a])[0]
    black_links2 = re.findall(r'\[\[(.*?)(?:\||\]\])',black_text+mytext, re.S)
    new_links,delink=[],[]
    for i in links:
        itest=i.split('|')[0].replace('_',' ').replace('&nbsp;',' ')
        if not itest.strip():
            delink.append(i)
        if itest in black_links2 or itest in new_links or ':' in i:
            continue
        else:  
            if itest=='آذرشهر':    
                new_links.append(i)
                continue
            itest=itest.strip()
            itest=re.sub(r'[۱۲۳۴۵۶۷۸۹۰]','',itest)
            itest=re.sub(r'\((میلادی|قمری|پیش از میلاد|قبل از میلاد)\)','',itest)
            b_list=['کاپیتان (فوتبال)','استان','دهستان','کشور','شهر','شهرستان','بخش','فروردین','اردیبهشت','خرداد',
                'تیر','مرداد','شهریور','مهر','آبان','آذر','دی','بهمن','اسفند','ژانویه','فوریه','مارس','ژوئیه','ژوئن',
                'آوریل','اوت','سپتامبر','نوامبر','دسامبر','می','اکتبر']
            itest=re.sub(r'('+'|'.join(b_list).replace(')','\)').replace('(','\(')+')','',itest)
            if not itest.strip():
                delink.append(i)
                continue
            itest=re.sub(r'[^صثقفغعهخحجچشسیبلاتنمکگظطزرذدپوژآيئؤًٌٍَُِّْٔ]','',itest)
            if not itest.strip():
                continue
            new_links.append(i)
    return new_links,delink

def boxfind(text_en):
    text_en=text_en.replace('{{ ','{{').replace('{{ ','{{').replace('{{template:','{{').replace('{{Template:','{{').replace('\r','')
    start=False    
    box='\n'
    diff=1
    linebaz,linebasteh=0,0
    for our_box in boxes:
        our_box=our_box.strip()
        up_our_box=our_box[0].upper()+our_box[1:]
        lower_our_box=our_box[0].lower()+our_box[1:]
        regex_result=re.findall('(\{\|([\n\s]+|)\{\{([\s]+|)'+our_box+')',text_en, re.IGNORECASE)
        if regex_result:
            if regex_result[0][0].strip():
                pre_template='{|'
                post_tempate='|}'
                text_en=text_en.replace('{| ','{|').replace('{| ','{|').replace('{|\n','{|').replace('{|\n','{|')
                text_en=text_en.replace(' |}','|}').replace(' |}','|}').replace('\n|}','|}').replace('\n|}','|}')
        else:
            pre_template,post_tempate='',''
        lines=text_en.split('\n')
        for line in lines:
            if line=='':
                continue
            if line.find(pre_template+'{{'+lower_our_box)!=-1 :# lower case    
                start=True
                linebaz,linebasteh=0,0
                box+=pre_template+'{{'+lower_our_box+line.split(pre_template+'{{'+lower_our_box)[1]+'\n'
                linebaz += line.count( pre_template+"{{" )
                linebasteh += line.count( "}}"+post_tempate )    
                diff=linebaz-linebasteh
                continue
            if line.find(pre_template+'{{'+up_our_box)!=-1 :# upper case
                start=True
                linebaz,linebasteh=0,0
                box+=pre_template+'{{'+up_our_box+line.split(pre_template+'{{'+up_our_box)[1]+'\n'
                linebaz += line.count( pre_template+"{{" )
                linebasteh += line.count( "}}" +post_tempate)
                diff=linebaz-linebasteh
                continue
            if start==True and diff!=0:
                linebaz += line.count( pre_template+"{{" )
                linebasteh += line.count( "}}"+post_tempate )
                diff=linebaz-linebasteh
                box+=line+'\n'
            if diff==0 and start==True:
                break
        if box.strip():
            break
    return box.replace('}}|}','}}\n|}')

def Get_box (txt):
    my_box=boxfind(txt)
    if my_box.strip():
        return my_box.strip()
    txt=txt.replace('\r','')
    lines=txt.split('\n')
    matn=' '
    for line in lines:
        linebaz=line.count(line,'{{')
        linebaste=line.count(line,'}}')
        diff=linebaz-linebaste
        if diff==0:
            line=line.replace('{{','$AAAA$').replace('}}','!BBBB!')
        linebaz=0
        linebaste=0
        matn+=line+'\n'
    my_box=''
    for our_box in boxes:
        our_box=our_box.strip()
        try:
            my_box= re.search(r'(\{\{\s*['+our_box[0].lower()+our_box[0].upper()+r']'+our_box[1:]+r'[_\s](?:\{\{.*?\}\}|[^\}])*\}\})',matn, re.S).group(1)# if Template box has other name please chang this regex
            my_box=my_box.replace('$AAAA$','{{').replace('!BBBB!','}}')
            break
        except:
            continue
    if not my_box.strip():
        return False
    return my_box.strip()

def addtext (fapage,text,addtemplate,addtemplate2,msg_clean,username_r,tempetype):
    text_t=text.replace('_',' ')
    if '{{ابهام‌زدایی' in text_t or '{{نمایه' in text_t or '{{نام کوچک' in text_t or '{{نام خانوادگی' in text_t or '{{مقالات مجموعه‌نمایه' in text_t:
        return False
    text=text.replace(addtemplate+'\n','')
    if tempetype=='navbox':
        if 'رده:' in text:
            num=text.find('[[رده:')
            text=text[:num]+addtemplate+'\n'+text[num:]
        else:    
            text+='\n'+addtemplate
    elif tempetype=='sidebar':
        ourbox=Get_box (text)
        if not ourbox:
            text=addtemplate+'\n'+text
            my_text_result=re.findall(r'\{\{(?:ویکی[‌ ]?سازی|منبع|بدون منبع|لحن|تمیزکاری|طفره‌آمیز|نامفهوم|تبلیغات|بهبود منبع|طرفداری|درستی|ادغام با|ادغام از|ادغام|در دست ویرایش ۲|تازه درگذشته|اصلاح ترجمه|رده-نیاز)(?:.*?)?\}\}',text, re.IGNORECASE)
            if my_text_result:
               for i in my_text_result:
                   text=i+'\n'+text.replace(i+'\n','').replace(i,'')
        else:
            return False
    else:
        return False

    try:                
        fapage.put(text,'[[وپ:ابزارک|افزودن ناوباکس]] '+botversion+'> '+addtemplate2+' (درخواست [['+username_r+']])'+msg_clean)
        
        return True
    except:
        pass    
    return False

def templatequery(enlink):
    temps=[]
    try:
        enlink=str(enlink).replace('[[','').replace(']]','').replace('en:','').replace('fa:','')
    except:
        enlink=enlink.replace('[[','').replace(']]','').replace('en:','').replace('fa:','')
    enlink=enlink.split('#')[0].strip()
    enlink=enlink.replace(' ','_')
    if _cache.get(tuple([enlink, 'templatequery'])):
        return _cache[tuple([enlink, 'templatequery'])]
    if enlink=='':
        _cache[tuple([enlink, 'templatequery'])]=False
        return False    

    params = {
            'action': 'query',
            'prop':'templates',
            'titles': enlink,
            'redirects': 1,
            'tllimit':500,
    }
 
    try:
        categoryname = pywikibot.data.api.Request(
                site=fasite, parameters=params)
        categoryname = categoryname.submit()
        for item in categoryname['query']['pages']:
            templateha=categoryname['query']['pages'][item]['templates']
            break
        for temp in templateha:
            temps.append(temp['title'].replace('_',' ').replace('الگو:','').replace('template:','').strip())  
        _cache[tuple([enlink, 'templatequery'])]=temps
        return temps
    except: 
        _cache[tuple([enlink, 'templatequery'])]=False
        return False


def check_user_edits(username):
    username=username.replace(' ','_')
    if _cache.get(tuple([username, 'check_user_edits'])):
        return _cache[tuple([username, 'check_user_edits'])]
    params = {
        'action': 'query',
        'list': 'users',
        'ususers': username,
        'usprop':'editcount'    
    }
    try:
        usernamequery = pywikibot.data.api.Request(
                site=fasite, parameters=params)
        usernamequery = usernamequery.submit()
        if usernamequery['query']['users'][0]['editcount']>1000:
            _cache[tuple([username, 'check_user_edits'])]=True
            return True
        else:
            _cache[tuple([username, 'check_user_edits'])]=False
            return False
    except:
        _cache[tuple([username, 'check_user_edits'])]=False
        return False    


def check_user(fapage):
    First_user=''
    try:
        page_history=fapage.getVersionHistory()
        First_user=page_history[-1][2]
        if check_user_edits(First_user):
            return True,First_user
        else:
            return False,First_user
    except:
        return False,First_user

def add_nav(preloadingGen,username_r):
    for tem in preloadingGen:
        tem_title=tem.title().replace('الگو:','')
        if not re.sub(r'[^صثقفغعهخحجچشسیبلاتنمکگظطزرذدپوژآيئؤًٌٍَُِّْٔ]','',tem_title).strip():
            continue
        if '/' in tem_title or '\\' in tem_title:
            continue  
        try:
            tem_text=tem.get()
        except:
            continue
        tem_text=tem_text.replace('{{ ','{{').replace('{{ ','{{').replace('{{الگو:','{{').replace('{{Template:','{{').replace('{{template:','{{')
        TempTemplates=templatequery(tem.title())
        if not TempTemplates:
            continue

        if not 'Navbox' in TempTemplates and not 'نوار جانبی' in TempTemplates:
            continue
        if 'Navbox' in TempTemplates:
            tempetype='navbox'
        if 'نوار جانبی' in TempTemplates:
            tempetype='sidebar'

        added_template=tem.title().replace('الگو:','').replace('template:','').replace('Template:','')
        if tem.namespace()!=10:
            continue
        redirects=tem.getReferences(redirectsOnly=True)
        redirect_list=[]
        for i in redirects:
            redirect_list.append(i.title().replace('الگو:','').replace('template:','').replace('Template:',''))
        links=tem.linkedPages()
        link_t=[]
        for i in links:
            link_t.append(i.title())
        links_ref=tem.getReferences()
        link_t2=[]
        for i in links_ref:
            link_t2.append(i.title())
        links=[x for x in link_t if x not in link_t2]
        links,delink=link_filtering(tem_text,links)
        
        old_tem_text=tem_text
        for nonlink in delink:
            tem_text=tem_text.replace('[['+nonlink+']]',nonlink.split('|')[0])
        if old_tem_text!=tem_text:
            tem.put(tem_text,'ربات:برداشتن پیوندهای نالازم')
        added_links=[]
        for faTitle in links:
            
            try:
                fapage=pywikibot.Page(fasite, faTitle)
                text=fapage.get()
            except pywikibot.exceptions.IsRedirectPageError:
                fapage_redi = fapage.getRedirectTarget()
                try:
                    text=fapage_redi.get()
                    solve_redirect(tem,fapage,fapage_redi)
                    fapage=fapage_redi
                except:
                    continue
            except:
                
                continue

            old_text=text
            for i in redirect_list: 
                text=text.replace('{{'+i+'}}','{{'+added_template+'}}').replace('{{'+i+'|','{{'+added_template+'|')
            fatemplates=templatequery(fapage.title())
            text=text.replace('{{ ','{{').replace(' }}','}}').replace('{{الگو:','{{').strip()
            if not fatemplates:
                continue
            if '{{'+added_template+'}}' not in text and (not added_template in fatemplates):
                addtemplate2='[[الگو:'+added_template+']]'
                addtemplate='{{'+added_template+'}}'
                addtext_result= addtext (fapage,text,addtemplate,addtemplate2,' ',username_r,tempetype)
                if addtext_result:
                   added_links.append(faTitle)
                   
                continue
 
            if old_text!=text:
                try:
                    fapage.put(text,'ربات:اصلاح تغییرمسیر ناوباکس‌')
                except:
                    pass    
                continue
        my_result={}
        if added_links:
            my_result['msg']='الگو به «'+'»، «'.join(added_links)+'» افزوده شده!'
        else:
            my_result['msg']='الگو در همهٔ مقالات موجود است!'
        print(json.dumps(my_result, ensure_ascii=False))

def main():    
    gen = None
    file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'navbox_transfer.json')
    with open(file_path, 'r') as f:
        run_data = json.loads(f.read())
    if not run_data:
        return
    if '-newtem' in sys.argv:    
        gen = pagegenerators.NewpagesPageGenerator(site=fasite, total=100, namespaces=(10))
    else:
        gen = [pywikibot.Page(fasite, run_data['template'])]
    with open(file_path, 'w') as f:
        f.write('{}')
    add_nav(gen,run_data['user'])


if __name__ == "__main__":
    main()
else:
    faPage = pywikibot.Page(fasite, sys.argv[1])
    add_nav([faPage],sys.argv[2])