ഉപയോക്താവ്:UltraBot/catmover.py

വിക്കിപീഡിയ, ഒരു സ്വതന്ത്ര വിജ്ഞാനകോശം.

ഈ പ്രോഗ്രാം ഇവിടെ നല്‍കുന്നത് ഇതുപയോഗിച്ച് വിക്കിപീഡിയയെ മെച്ചപ്പെടുത്തുന്നതിന്റെ ഭാഗമായി മറ്റുവള്ളവര്‍ക്ക് ഉപയോഗിക്കാനാകും എന്ന വിശ്വാസത്തിലാണ്‌. ഈ പ്രോഗ്രാം പ്രവര്‍ത്തിപ്പിച്ച് വല്ല കുഴപ്പവുമുണ്ടാകുന്നുവെങ്കില്‍ അതിന്റെ പൂര്‍ണ്ണ ഉത്തരവാദിത്തം പ്രോഗ്രാം പ്രവര്‍ത്തിപ്പിച്ചവര്‍ക്ക് തന്നെയായിരിക്കും, ഇതിന്റെ നിര്‍മ്മാതാവ് അതിന്റെ യാതൊരു ഉത്തരവാദിത്തവും ഏറ്റെടുക്കുന്നില്ല.

"""
ഒരു വർഗ്ഗം മുഴുവൻ മറ്റൊനിലേക്ക് മാറ്റുന്നതിനുള്ള് ബോട്ട് സ്ക്രിപ്റ്റ്
വർഗ്ഗത്തിലെ താളുകളും ഉപവര്‍ഗ്ഗങ്ങളും പുതിയ വർഗ്ഗത്തിലേക്ക് മാറ്റും.
Author: Junaid P V (http://ml.wikipedia.org/wiki/User:Junaidpv)

"""
import wikipedia
import catlib
import unicodedata
import codecs
import re

CAT = ur"വര്‍ഗ്ഗം"
# This is function was copied from pywikipedia framework,
# and altered little to handle new category alias.
def replaceCategoryInPlace(oldtext, oldcat, newcat, site=None):
    """Replace the category oldcat with the category newcat and return
       the modified text.

    """
    if site is None:
        site = getSite()

    catNamespace = '|'.join(site.category_namespaces()+ (ur"വർഗ്ഗം", ))
    title = oldcat.titleWithoutNamespace()
    if not title:
        return
    # title might contain regex special characters
    title = re.escape(title)
    # title might not be capitalized correctly on the wiki
    if title[0].isalpha() and not site.nocapitalize:
        title = "[%s%s]" % (title[0].upper(), title[0].lower()) + title[1:]
    # spaces and underscores in page titles are interchangeable, and collapsible
    title = title.replace(r"\ ", "[ _]+").replace(r"\_", "[ _]+")
    categoryR = re.compile(r'\[\[\s*(%s)\s*:\s*%s\s*((?:\|[^]]+)?\]\])'
                            % (catNamespace, title), re.I)
    if newcat is None:
        text = wikipedia.replaceExcept(oldtext, categoryR, '',
                             ['nowiki', 'comment', 'math', 'pre', 'source'])
    else:
        text = wikipedia.replaceExcept(oldtext, categoryR,
                             '[[%s:%s\\2' % (site.namespace(14),
                                             newcat.titleWithoutNamespace()),
                             ['nowiki', 'comment', 'math', 'pre', 'source'])
    return text

def changeCat(page, oldCatName, newCatName, wikiSite):
    wikipedia.output( "Processing page: "+page.title().encode('utf-8'))
    pageContent = page.get(get_redirect=True)
    oldCatPage = catlib.Category(site=wikiSite, title=oldCatName)
    newCatPage = catlib.Category(site=wikiSite, title=newCatName)
    newContent = replaceCategoryInPlace(oldtext=pageContent, oldcat=oldCatPage, newcat=newCatPage, site=wikiSite)
    global log
    if(pageContent != newContent):
        page.put(newContent,ur"വർഗ്ഗം മാറ്റുന്നു")
        log.write(">>"+page.title()+">> Done.\n")
        wikipedia.output(ur"Done.")  
    else:
        wikipedia.output("not doing for "+page.title())
        log.write(">>"+page.title()+">> No change.\n")
        wikipedia.output(ur"Not Done.") 

def changeCatOfPages(pageList, oldCatName, newCatName, wikiSite):
    for page in pageList:
        changeCat(page, oldCatName, newCatName, wikiSite)
# ഉപയോഗിക്കുന്ന മീഡിയവിക്കി സൈറ്റ്
siteFamily = "wikipedia"
# സൈറ്റിന്റെ ഭാഷ
siteLangCode = "ml"

# പഴയ വര്‍ഗ്ഗത്തിന്റെ പേര് ഇവിടെ നല്‍കുക
oldCatName = ur"കേരള സാഹിത്യ അക്കാദമി അവാര്‍ഡ്"
# പുതിയ വര്‍ഗ്ഗത്തിന്റെ പേര്‍ ഇവിടെ നല്‍കുക
newCatName = ur"കേരള സാഹിത്യ അക്കാദമി പുരസ്കാരം"

wikiSite = wikipedia.Site(code = siteLangCode, fam = siteFamily)

log = codecs.open("catmover.log", mode="at", encoding = 'utf-8')


catPage = catlib.Category(site = wikiSite, title = CAT+":"+oldCatName )

subCats = catPage.subcategoriesList()
articles = catPage.articlesList()
changeCatOfPages(subCats, oldCatName, newCatName, wikiSite)
changeCatOfPages(articles, oldCatName, newCatName, wikiSite)

catPageContent = catPage.get(get_redirect = True)
newCatPage = catlib.Category(site = wikiSite, title = CAT+":"+newCatName )

if(not newCatPage.exists()):
    newCatPage.put(catPageContent, newCatName +ur"എന്ന വർഗ്ഗം ഇതിലേക്ക് മാറ്റുന്നു" )

deleteRequest = ur"{{SD|എല്ലാം [[:"+CAT+":"+newCatName+ur"]] എന്ന വർഗ്ഗത്തിലേക്ക് മാറ്റി}}"+catPageContent
if(catPage.exists()):
    catPage.put(deleteRequest, ur"എല്ലാം [[:"+CAT+":"+newCatName+ur"]] എന്ന വർഗ്ഗത്തിലേക്ക് മാറ്റി")
    
catTalkPage = catPage.toggleTalkPage()
newCatTalkPage = newCatPage.toggleTalkPage()
if((not newCatTalkPage.exists()) and catTalkPage.exists()):
    oldTalk = catTalkPage.get();
    newTalk = ur"[[:"+CAT+":"+oldCatName+"]]"+ur" എന്ന വർഗ്ഗം ഇതിലേക്ക് മാറ്റിയതിനാൽ അവിടുത്തെ സംവാദങ്ങളും ഇതിലേക്ക് മാറ്റുന്നു<br />"
    newTalk = newTalk+ur"----<br />" + oldTalk + ur"<br />----<br />--~~~~"
    newCatTalkPage.put(newTalk, ur"സംവാദം മാറ്റുന്നു")

# So what would be next? :) let's stop
wikipedia.stopme()

log.flush()
log.close()
"https://ml.wikipedia.org/w/index.php?title=ഉപയോക്താവ്:UltraBot/catmover.py&oldid=605898" എന്ന താളിൽനിന്ന് ശേഖരിച്ചത്