#!/usr/bin/python
# -*- coding: utf-8  -*-
#Scipt to update the status of articles in monthly stublists

import wikipedia, pagegenerators, catlib, config, codecs, re
comment = u'బాటు: మొలకల జాబితా తాజకరించా'
disambig = u"{{అయోమయ నివృత్తి}}"
stublistPageTitle = u"వికీపీడియా:మొలకల జాబితా/2013 జూన్"

# Replace the contents in the page 'pageTitle' with data 'pageData' 
# and add the comment 'comment'
def writeData(pageTitle, pageData, comment):
  page = wikipedia.Page(wikipedia.getSite(), pageTitle)
  try:
    # Load the page's text from the wiki
    data = page.get()
  except wikipedia.NoPage:
    data = u''
  data = pageData
  try:
    page.put(data, comment = comment)
  except wikipedia.EditConflict:
    wikipedia.output(u'Skipping %s because of edit conflict' % (page.title()))
  except wikipedia.SpamfilterError, url:
    wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), url))


def strikelink(pageTitle, spageData):
  titlelink = u'[['+pageTitle+u']]'
  struck_titlelink = u'<s>[['+pageTitle+u']]</s>'
  if spageData.find(struck_titlelink) == -1:
    spageData = spageData.replace(titlelink,struck_titlelink)
  return spageData  

#opening stublist wikipage and generating linked page links
stublistPage = wikipedia.Page(wikipedia.getSite(), stublistPageTitle)
stublistpageData = stublistPage.get()

pregen = pagegenerators.LinkedPageGenerator(stublistPage)
gen = pagegenerators.NamespaceFilterPageGenerator(pregen,0, site = None)
preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber = 500)

for page in preloadingGen:
    try:
      # Load the page's text from the wiki
      pageData = page.get()
      if not page.canBeEdited():
         wikipedia.output(u'Skipping locked page %s' % page.title())
         continue
    except wikipedia.NoPage:
       stublistpageData = strikelink(page.title(),stublistpageData)
       continue
    except wikipedia.IsRedirectPage:
       stublistpageData = strikelink(page.title(),stublistpageData)     
       continue
    # check for disambig template and strike if it is disambig page
    if pageData.find(disambig) >= 0: 
       stublistpageData = strikelink(page.title(),stublistpageData) 
       continue

    # checking the page length and strike if it is no longer a stub
    if (len(pageData) > 2048):
           stublistpageData = strikelink(page.title(),stublistpageData) 


# close stublist log handle
print stublistpageData

# upload the stublist to tewiki
writeData(stublistPageTitle, stublistpageData, comment)