import pywikibot
import sys
from pywikibot import pagegenerators, Timestamp, textlib
from pywikibot.logging import stdout, warning, error, log, debug, error
from pywikibot.diff import PatchManager
import datetime
import re
import mwparserfromhell

site = pywikibot.Site('zh', 'wikisource')
repo = site.data_repository()
def replace_proofread_parameter(page):
    # if header
    parsed = mwparserfromhell.parse(page.text)
    for template in parsed.filter_templates():
        if template.name.matches(r":MediaWiki:Proofreadpage_index_template"):
            template.
    return ("%s" % parsed)
 
pagelimit = 2000
count = 0
checked_page = 0
update=True

referredTemplate = pywikibot.Page(site, 'Template:Edition')
transclusionGen = pagegenerators.ReferringPageGenerator(referredTemplate, followRedirects=False,
                           withTemplateInclusion=True,
                           onlyTemplateInclusion=True,
                           total=None, content=False)

#namespace filter

for page in transclusionGen:
    checked_page+=1
    if (count >= pagelimit):
        stdout("已更新%s页,现在退出..." % count)
        break
    #Do something with the page object, for example:
    nextstr = replace_edition(page)
    if nextstr == page.text:
        stdout("==skip %s, nothing to update==" % page.title())
        continue
    stdout("==执行第(%s/%s)项更新==" % (count, pagelimit))
    stdout("%s" % page.title())
    saveMessage="移除{{edition}},设置{{header}}的edition=yes"
    stdout(saveMessage)
    diff = PatchManager(page.text.split('\n'),
                        nextstr.split('\n'),
                        by_letter=True)
    diff.print_hunks()
    page.text = nextstr
    if(update==True):
        page.save(saveMessage, asynchronous=True, minor=True)
    else:
        print("test mode, change not saved.")
    count +=1
print("Checked page %s" % checked_page)
==skip Wikisource:正文品质, nothing to update==
==skip 吶喊, nothing to update==
==skip 十六國春秋/卷06, nothing to update==
==skip 十六國春秋/卷07, nothing to update==
==skip 十六國春秋/卷02, nothing to update==
==skip 十六國春秋/卷03, nothing to update==
==skip 十六國春秋/卷04, nothing to update==
==skip 十六國春秋/卷05, nothing to update==
==skip 商务部、国家工商总局、国家版权局、国家知识产权局令, nothing to update==
==skip 十六國春秋/卷11, nothing to update==
==skip 十六國春秋/卷14, nothing to update==
==skip 十六國春秋/卷10, nothing to update==
==skip 十六國春秋/卷12, nothing to update==
==skip 十六國春秋/卷13, nothing to update==
==skip 十六國春秋/卷08, nothing to update==
==skip 十六國春秋/卷16, nothing to update==
==skip 十六國春秋/卷15, nothing to update==
==skip 十六國春秋/卷09, nothing to update==
==skip 官場現形記/01, nothing to update==
==skip Talk:詞論 (李清照), nothing to update==
==skip 官場現形記/02, nothing to update==
==skip 官場現形記/03, nothing to update==
==skip 官場現形記/04, nothing to update==
==skip 官場現形記/05, nothing to update==
==skip 官場現形記/06, nothing to update==
==skip 官場現形記/07, nothing to update==
==skip 官場現形記/08, nothing to update==
==skip 官場現形記/09, nothing to update==
==skip 官場現形記/10, nothing to update==
==skip 官場現形記/11, nothing to update==
==skip 官場現形記/12, nothing to update==
==skip 官場現形記/13, nothing to update==
==skip 官場現形記/14, nothing to update==
==skip 官場現形記/15, nothing to update==
==skip 官場現形記/16, nothing to update==
==skip 官場現形記/17, nothing to update==
==skip 官場現形記/18, nothing to update==
==skip 官場現形記/19, nothing to update==
==skip 官場現形記/20, nothing to update==
==skip 官場現形記/21, nothing to update==
==skip 官場現形記/22, nothing to update==
==skip 脈經/0000, nothing to update==
==skip 全漢文/卷八, nothing to update==
==skip 全漢文/卷十, nothing to update==
==skip 全漢文/卷九, nothing to update==
==skip 全漢文/卷十三, nothing to update==
==skip 全漢文/卷十四, nothing to update==
==skip 全漢文/卷十八, nothing to update==
==skip 全漢文/卷二十, nothing to update==
==skip 全漢文/卷二十一, nothing to update==
==skip 涑水記聞/卷04, nothing to update==
==skip 徐偃王志, nothing to update==
==skip 花邊文學, nothing to update==
==执行第(0/1000)项更新==
科學精神与東西文化
移除{{edition}},设置{{header}}的edition=yes
@@ -9 +9,2 @@ ***
- | notes    = {{Textquality|25%}}{{edition}} ***
+ | notes    = {{Textquality|25%}} ***
+ | edition= yes ***

Page [[wikisource:zh:野議]] saved
Sleeping for 59.5 seconds, 2019-08-03 18:00:22
@@ -10 +10,2 @@ ***
- | notes    ={{Textquality|100%}} <!--请将“参数”字样修改为25%,50%,75%或100%,分别代表录入,整理格式,校对和多人校对-->{{Edition}} <!--此处输入备注,如作品的撰写日期,地点,或其他需要注明的信息,如果没有,请删除本行--> ***
+ | notes    ={{Textquality|100%}} <!--请将“参数”字样修改为25%,50%,75%或100%,分别代表录入,整理格式,校对和多人校对--> <!--此处输入备注,如作品的撰写日期,地点,或其他需要注明的信息,如果没有,请删除本行--> ***
+ | edition=yes ***

==执行第(1/1000)项更新==
琉蘭修好条約
移除{{edition}},设置{{header}}的edition=yes
Page [[wikisource:zh:天史]] saved
Sleeping for 57.4 seconds, 2019-08-03 18:01:24
@@ -9 +9,2 @@ ***
- | notes    ={{Textquality|100%}} <!--请将“参数”字样修改为25%,50%,75%或100%,分别代表录入,整理格式,校对和多人校对-->{{Edition}} <!--此处输入备注,如作品的撰写日期,地点,或其他需要注明的信息,如果没有,请删除本行--> ***
+ | notes    ={{Textquality|100%}} <!--请将“参数”字样修改为25%,50%,75%或100%,分别代表录入,整理格式,校对和多人校对--> <!--此处输入备注,如作品的撰写日期,地点,或其他需要注明的信息,如果没有,请删除本行--> ***
+ | edition    =yes ***

==执行第(2/1000)项更新==
中国2010年上海世博会音乐作品征集规则
移除{{edition}},设置{{header}}的edition=yes
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-24-08a5cbf05c6a> in <module>
     33     page.text = nextstr
     34     if(update==True):
---> 35         page.save(saveMessage, asynchronous=True, minor=True)
     36     else:
     37         print("test mode, change not saved.")

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*__args, **__kw)
   1736                              cls, depth)
   1737                     del __kw[old_arg]
-> 1738             return obj(*__args, **__kw)
   1739 
   1740         if not __debug__:

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*__args, **__kw)
   1736                              cls, depth)
   1737                     del __kw[old_arg]
-> 1738             return obj(*__args, **__kw)
   1739 
   1740         if not __debug__:

/srv/paws/pwb/pywikibot/page.py in save(self, summary, watch, minor, botflag, force, asynchronous, callback, apply_cosmetic_changes, quiet, **kwargs)
   1280         self._save(summary=summary, watch=watch, minor=minor, botflag=botflag,
   1281                    asynchronous=asynchronous, callback=callback,
-> 1282                    cc=apply_cosmetic_changes, quiet=quiet, **kwargs)
   1283 
   1284     @allow_asynchronous

/srv/paws/pwb/pywikibot/page.py in wrapper(self, *args, **kwargs)
    124     def wrapper(self, *args, **kwargs):
    125         if kwargs.get('asynchronous'):
--> 126             pywikibot.async_request(handle, func, self, *args, **kwargs)
    127         else:
    128             handle(func, self, *args, **kwargs)

/srv/paws/pwb/pywikibot/__init__.py in async_request(request, *args, **kwargs)
   1410         finally:
   1411             page_put_queue.mutex.release()
-> 1412     page_put_queue.put((request, args, kwargs))
   1413 
   1414 

/usr/lib/python3.6/queue.py in put(self, item, block, timeout)
    131                 elif timeout is None:
    132                     while self._qsize() >= self.maxsize:
--> 133                         self.not_full.wait()
    134                 elif timeout < 0:
    135                     raise ValueError("'timeout' must be a non-negative number")

/usr/lib/python3.6/threading.py in wait(self, timeout)
    293         try:    # restore state no matter what (e.g., KeyboardInterrupt)
    294             if timeout is None:
--> 295                 waiter.acquire()
    296                 gotit = True
    297             else:

KeyboardInterrupt: