import pywikibot
from pywikibot import pagegenerators
import mwparserfromhell
from mwparserfromhell.nodes import Template
import datetime
from datetime import date
import re

language = 'nl'
allowed_namespaces = [0,14]
findtheseCATtemplates=['Catweg']                             #categories have WP:TVC with it's own structure
findtheseTEMPLATEtemplates=['Sjabloonweg']
findtheseDELETEtemplates=['Ne','Wiu','Weg','Wb','Reclame']   #these templates are on WP:TBP
findtheseOTHERtemplates=['Bron?','Nocat','Wikify','Beginnetje','Feit']           #these have a date, but are *not* on WP:TBP
changemail=u''



skiptheselemmata=[
#'',
#'Dries Van Langenhove', #6 feb
#'Het Bouwens van der Boijecollege',#6 feb
#'Giovanni Latooy', #feb
#'Sylvana IJsselmuiden', #9 feb
'Antroposofische evolutieleer', #14 feb
'Letteren en Wijsbegeerte Kring',
'Apartheidsstaat Israƫl', #15 maart
'',
'',
'',
'',
'',
'',

] 

def genPagesWhatLinksHere(site,pagename):
 if (False):
  refPage = pywikibot.Page(pywikibot.Link(pagename,site))
  gen = pagegenerators.ReferringPageGenerator(refPage)
  for onepage in gen:
    yield(onepage)
 else:
  page = pywikibot.Page(pywikibot.Link(site,pagename))
  gen = page.backlinks()
  for onepage in gen:
    yield(onepage)

def genPagesWithTemplate(site,template):   #get all pages with a template
   refPage = pywikibot.Page(pywikibot.Link('template:'+template,site))
   gen = pagegenerators.ReferringPageGenerator(refPage)
   for onepage in gen:
     if ('{{'+template.upper() in onepage.text.upper()):
       if (onepage.namespace().id in allowed_namespaces):
         yield(onepage)

def getTemplate(page,template):                #find one template on a page
  wikicode=mwparserfromhell.parse(page.text)     
  templates=wikicode.filter_templates()
  mytemplates = [x for x in templates if x.name.matches(template)]
  if (len(mytemplates)>0):
    for returntemplate in mytemplates:
      yield  returntemplate                    #return template we searched for  
  else:
    return Template('')                        #template not found on page, return emtpy template

def templatewithdate(template):    
  dategood = False  #initially

  #check format 2=year|3=month|4=day
  dayfound=monfound=yearfound=False
  for oneparam in template.params:
    if '4=' in oneparam.replace(' ',''): dayfound = True
    if '3=' in oneparam.replace(' ',''): monfound = True
    if '2=' in oneparam.replace(' ',''): yearfound= True
  if (dayfound and monfound and yearfound):  
    dategood = True

  #check format year|month|day
  elif (len(template.params)==4):
    
    try:
      year=int(str(template.params[1]))
      month=int(str(template.params[2]))
      day=int(str(template.params[3]))
      datetime.date(year,month,day)
      dategood = True
      #print('3) %s' % template.params[1]) 
    except ValueError:
      print('ValueError 2) %s %s' % (template.name,template.params))  
      dategood = False
      print('Date not good')
  #print('5: %s' % dategood)
  return dategood
  
def findnewestevaluation(site,page):
  newest = datetime.date(1900,1,1)
  #print('FIND: %s [%s]'% (page.namespace().id,page.title()))
  for whatlinkshere in page.backlinks(): #genPagesWhatLinksHere(site,page.title()):
    if (page.namespace().id in [0,1]):  #page/talk
      pagetitle = whatlinkshere.title()
      if (pagetitle[:43]==u'Wikipedia:Te beoordelen pagina\'s/Toegevoegd'):
        datestr = pagetitle[44:52]
        year = int(datestr[0:4]) 
        month= int(datestr[4:6])
        day  = int(datestr[6:8])
        datevalue = datetime.date(year,month,day)
        if abs(datevalue-newest)>datetime.timedelta(14): 
          newest = datevalue
    if (page.namespace().id in [14,15]):  #category/category talk
      pagetitle = whatlinkshere.title()
      if (pagetitle[0:45]==u'Wikipedia:Te beoordelen categorieƫn/Toegevoegd'):
        print('cat')
        datestr = pagetitle[48:60]
        year=int(datestr[0:4])
        week=int(datestr[10:12])
        datevalue = 0
        print('week: [%s] - [%s] - [%s]' %(datestr,year,week))
  return newest.year,newest.month,newest.day   

"""  
def actiononeTBP(site,mypage,findtemplate):
  actiononeTBP(site,mypage,findtemplate)
"""

def actiononeCAT(site,mypage,findtemplate):
  pass

def actionBEGINNETJE(site,mypage,findtemplate):
  print('BEGinnETJe')
  uitleg  = ''
  category= ''
  for onetemplate in getTemplate(page,findtemplate):
    nrparams = len(onetemplate.params)
    if (nrparams>0):
      uitleg = str(onetemplate.params[0])
    if (nrparams>1):
      category = str(onetemplate.params[1])      
    if (nrparams>=4):
      pass
    
def actiononeOTHER(site,mypage,findtemplate):
 if (findtemplate.lower()=='beginnetje'):
   actionBEGINNETJE(site,mypage,findtemplate)  #andere parameterstructuur
 else:
  for onetemplate in getTemplate(page,findtemplate):
    nrparams = len(onetemplate.params)
    if (nrparams==0) or (nrparams==1):
      today = date.today()
      uitleg = u''
      oldstr = '{{'+findtemplate+'}}'
      if nrparams==1:
        uitleg = str(onetemplate.params[0])
        if (uitleg[:2]=='1='):
          uitleg = uitleg[2:]
        oldstr='{{'+findtemplate+'|'+uitleg+'}}'
      newstr='{{'+findtemplate+'|1='+uitleg+'|2='+number2string(today.year,4)+'|3='+number2string(today.month,2)+'|4='+number2string(today.day,2)+'}}'
      #print('Will change <%s> to [%s]' % (oldstr,newstr)) 
      mypage.text = re.sub('(?i)'+re.escape(oldstr),newstr,mypage.text)      
      pywikibot.Page(site, mypage.title()).put(mypage.text, summary='datumparameters toevoegen.') #Save page
      #print('\nWill be:\n%s' % mypage.text)
    elif (nrparams>=3):
      if not templatewithdate(onetemplate):
        print('Op [%s]: %s <%s>, %s parameters' % (mypage.title(),onetemplate.name,onetemplate.params,nrparams))  
    else:
       print('Other/huh? how many parameters? page: [%s], [%s]' % (mypage.title(),oentemplate.params)) 

def number2string(mynumber,length):
  mystr = str(mynumber)
  while (len(mystr)<length):
    mystr = '0'+mystr
  return mystr    
  
def actiononeTBP(site,mypage,findtemplate):  
  global changemail
  
  today = date.today()
  one=two=thr=False
  if not(mypage.title() in skiptheselemmata):
   if mypage.namespace().id in allowed_namespaces:
    for template in getTemplate(mypage,findtemplate):
        #print('Page [%s]: Template: {{%s}}' % (mypage.title(),template.name))
        year,month,day = findnewestevaluation(site,mypage)
        parcount = len(template.params)

        if (template.name.upper().strip()==findtemplate.upper().strip()) and (not (templatewithdate(template))):
          if (parcount==0):
            mypage.text = mypage.text.replace('{{'+findtemplate+'}}','{{'+findtemplate+'}}')
            fromstr = '{{'+findtemplate+'}}'
            tostr   = fromstr[:len(fromstr)-2]+'|1=|2='+str(year)+'|3='+number2string(month,2)+'|4='+number2string(day,2)+'}}'
            mypage.text = re.sub('(?i)'+re.escape(fromstr),tostr,mypage.text)  #replace template with template+empty description+DATE
            one=two=thr=True
          elif (parcount==1):
            fromstr='{{'+findtemplate+'|'+str(template.params[0])+'}}'
            tostr  =fromstr[:len(fromstr)-2]+'|2='+str(year)+'|3='+number2string(month,2)+'|4='+number2string(day,2)+'}}'
            mypage.text = re.sub('(?i)'+re.escape(fromstr),tostr,mypage.text)   #replace template+param with template+param+DATE
          elif (parcount==4):
            pass
          elif (parcount==5):
            pass
          else:
            changemail=changemail+ "1) Huh? Hoeveel parameters? %s - %s???" % (mypage.title,parcount)
          print('--ADD: %s: %s [%s-%s-%s]' % (mypage.title(),template.name, year,month,day))
          if (year>1900):   # 1900 means invalid date, usually means page is not on any of the pages
            pywikibot.Page(site, mypage.title()).put(mypage.text, summary='datumparameters toevoegen.') #Save page
          else:
            changemail=changemail+ 'yCHANGE: %s: %s\n' % (mypage.title(),findtemplate)
        else:
          #there is a date ... check with newest list        
          if ((parcount==3) or (parcount==4)):
            one = (str(year) in str(template.params[parcount-3]))
            two =(str(month) in str(template.params[parcount-2]))
            thr = (str(day) in str(template.params[parcount-1]))
          elif (parcount==5):
            one=two=thr=True
          else:
            print("2) Huh? Hoeveel parameters? %s %s???" % (mypage.title(),parcount))
            one=two=thr=True  #skip printing next line, due to indices in # params
          if not(one and two and thr):
            changemail=changemail+'xCHANGE: %s: %s (%s) <%s-%s-%s> [%s-%s-%s]\n' % (mypage.title(),findtemplate,template.name, template.params[parcount-3],template.params[parcount-2],template.params[parcount-1],year,month,day)
            print('xCHANGE: %s: %s (%s) <%s-%s-%s> [%s-%s-%s]'% (mypage.title(),findtemplate,template.name, template.params[parcount-3],template.params[parcount-2],template.params[parcount-1],year,month,day))

def actiononetemplate(site,template) :         
  print('Template: %s' % template)
  for mypage in genPagesWithTemplate(site,template):
    yield(mypage)
    
    
site = pywikibot.Site(language)    

if (True):
  #"""
  for findtemplate in findtheseDELETEtemplates:
    for page in actiononetemplate(site,findtemplate):
      actiononeTBP(site,page,findtemplate)
  """    
  for findtemplate in findtheseCATtemplates:
    for page in actiononetemplate(site,findtemplate):
      actiononeCAT(site,page,findtemplate)
  ""

  for findtemplate in findtheseOTHERtemplates:
    for page in actiononetemplate(site,findtemplate):
      actiononeOTHER(site,page,findtemplate)    
  """
  
  if (changemail!=''):
    mailuser=pywikibot.User(pywikibot.Site('nl'),'Edoderoo')
    mailuser.sendMail(subject='datumTBP', text=changemail)

else:
  print('Justonepage:')  
  page=pywikibot.Page(pywikibot.Link(u'Afdeling Misbruikpreventie van Dreuzelvoorwerpen',site))
  #actiononeTBP(site,page,u'weg')
  actiononeTBP(site,page,'weg')
print('Klaar')
Template: Ne
xCHANGE: Sophie Milzink: Ne (ne) <2=2019-3=06-4=29> [2019-6-21]
xCHANGE: Koen Kardashian: Ne (ne) <2=2019-3=06-4=29> [2019-6-28]
xCHANGE: Ikwilvanmijnautoaf: Ne (Ne) <2=2019-3=07-4=10> [2019-7-6]
--ADD: Groente en Fruitrace: ne [2019-8-15]
Page [[nl:Groente en Fruitrace]] saved
Template: Wiu
--ADD: Instapmodel: wiu [2019-8-15]
Page [[nl:Instapmodel]] saved
--ADD: Believe In Me (album van Duff McKagan): wiu [2019-8-15]
Page [[nl:Believe In Me (album van Duff McKagan)]] saved
Template: Weg
xCHANGE: Auto55: Weg (Weg) <2=2019-3=07-4=11> [2019-7-10]
xCHANGE: Keytec: Weg (weg) <2=2019-3=07-4=24> [2019-7-23]
xCHANGE: Lijst van rijksmonumenten in de voormalige gemeente Littenseradeel: Weg (Weg) <2019-07-18> [2018-2-16]
xCHANGE: Friese Merenproject: Weg (Weg) <2=2019-3=06-4=16> [2015-2-12]
--ADD: Lijst van interlands Belgisch voetbalelftal 2020-2029: weg [2019-8-15]
Page [[nl:Lijst van interlands Belgisch voetbalelftal 2020-2029]] saved
Template: Wb
Template: Reclame
--ADD: Cyb3rpvnk: reclame [2019-8-15]
Page [[nl:Cyb3rpvnk]] saved
WARNING: /srv/paws/lib/python3.6/site-packages/ipykernel_launcher.py:249: DeprecationWarning: pywikibot.page.User.sendMail is deprecated for 4 years and 8 months; use send_email instead.

---------------------------------------------------------------------------
UserRightsError                           Traceback (most recent call last)
<ipython-input-1-ab1dd9e63ede> in <module>
    247   if (changemail!=''):
    248     mailuser=pywikibot.User(pywikibot.Site('nl'),'Edoderoo')
--> 249     mailuser.sendMail(subject='datumTBP', text=changemail)
    250 
    251 else:

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*args, **kwargs)
   1615             depth = get_wrapper_depth(wrapper) + 1
   1616             issue_deprecation_warning(name, instead, depth, since=since)
-> 1617             return obj(*args, **kwargs)
   1618 
   1619         def add_docstring(wrapper):

/srv/paws/pwb/pywikibot/page.py in sendMail(self, subject, text, ccme)
   3493         @rtype: bool
   3494         """
-> 3495         if self.send_email(subject, text, ccme=ccme):
   3496             pywikibot.output('Email sent.')
   3497             return True

/srv/paws/pwb/pywikibot/page.py in send_email(self, subject, text, ccme)
   3456 
   3457         if not self.site.has_right('sendemail'):
-> 3458             raise UserRightsError("You don't have permission to send mail")
   3459 
   3460         params = {

UserRightsError: You don't have permission to send mail