User:YiFeiBot/wikiindex size.py: Difference between revisions

upd
(Update -- somehow not working in date)
(upd)
Line 29: Line 29:
     # script (i.e. wikiindex-size.py in this case)
     # script (i.e. wikiindex-size.py in this case)


     def __init__(self, generator, summary, debug):
     def __init__(self, generator, summary, debug=False):
         """
         """
         Constructor. Parameters:
         Constructor. Parameters:
Line 40: Line 40:
         self.generator = generator
         self.generator = generator
         # init constants
         # init constants
         self.site = pywikibot.getSite(code=pywikibot.default_code)
         self.site = pywikibot.getSite("wikiindex", "wikiindex")
         # Set the edit summary message
         # Set the edit summary message
         if summary:
         if summary:
Line 46: Line 46:
         else:
         else:
             self.summary = i18n.twtranslate(self.site, 'basic-changing')
             self.summary = i18n.twtranslate(self.site, 'basic-changing')
         if debug:
         self.debug = debug
            self.debug = debug
        else:
            self.debug = True


     def run(self):
     def run(self):
Line 65: Line 62:
         newtext = text
         newtext = text
         size_r = None
         size_r = None
         size_r = re.compile(ur"""(?P<all>\{\{\s*Size\s*((\|\s*(?P<pages>pages|wiki[ _]pages)\s*=\s*(?P<pages_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<pagesurl>statistics[ _]URL|wiki[ _]statistics[ _]URL)\s*=\s*(?P<pagesurl_value>https?://[^ \|\}\<]*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactor>wikiFactor)\s*=\s*(?P<wikifactor_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactorurl>wikiFactor[ _]URL)\s*=\s*(?P<wikifactorurl_value>http://[^ \|\}\<]*)\s*[^\|\}]*\s*))+\s*\|?\s*\}\}((\s*\([Aa]s\s*of:?\s*(?P<day>\d*) (?P<month>(?=January|February|March|April|May|June|July|August|September|October|November|December)*\s*) (?P<year>\d*)\))\s*)*)""")
         size_r = re.compile(ur"""(?P<all>\{\{\s*Size\s*((\|\s*(?P<pages>pages|wiki[ _]pages)\s*=\s*(?P<pages_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<pagesurl>statistics[ _]URL|wiki[ _]statistics[ _]URL)\s*=\s*(?P<pagesurl_value>https?://[^ \|\}\<]*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactor>wikiFactor)\s*=\s*(?P<wikifactor_value>\d*)\s*[^\|\}]*\s*)|(\s*\|\s*(?P<wikifactorurl>wikiFactor[ _]URL)\s*=\s*(?P<wikifactorurl_value>http://[^ \|\}\<]*)\s*[^\|\}]*\s*))+\s*\|?\s*\}\}(\s*\([Aa]s\s*of:?\s*(?P<day>\d+)\s*(?P<month>[A-Z][a-z]+)\s*(?P<year>\d+)\s*\)\s*(\<!--manually add date when stats are verified and/or updated--\>)?)?)""")


         wtext = page.get()
         wtext = page.get()
Line 183: Line 180:
             pywikibot.showDiff(page.get(), text)
             pywikibot.showDiff(page.get(), text)
             pywikibot.output(u'Comment: %s' % comment)
             pywikibot.output(u'Comment: %s' % comment)
             choice = pywikibot.inputChoice(
             #choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?',
            #    u'Do you want to accept these changes?',
                ['Yes', 'No'], ['y', 'N'], 'N')
            #    ['Yes', 'No'], ['y', 'N'], 'N')
             if choice == 'y':
             if True:
                 try:
                 try:
                     # Save the page
                     # Save the page
Line 206: Line 203:




class AutoWiSizeBot(WiSizeBot):
    # Intended for usage e.g. as cronjob without prompting the user.
    _REGEX_eol = re.compile(u'\n')
    def __init__(self):
        WiSizeBot.__init__(self, None, None)
    ## @since  10326
    #  @remarks needed by various bots
    def save(self, page, text, comment=None, **kwargs):
        pywikibot.output(u'\03{lightblue}Writing to wiki on %s...\03{default}'
                        % page.title(asLink=True))
        comment_output = comment or pywikibot.action
        pywikibot.output(u'\03{lightblue}Comment: %s\03{default}'
                        % comment_output)
        #pywikibot.showDiff(page.get(), text)
        for i in range(3):
            try:
                # Save the page
                page.put(text, comment=comment, **kwargs)
            except pywikibot.LockedPage:
                pywikibot.output(
                    u"\03{lightblue}Page %s is locked; skipping.\03{default}"
                    % page.title(asLink=True))
            except pywikibot.EditConflict:
                pywikibot.output(
                    u'\03{lightblue}Skipping %s because of edit '
                    u'conflict\03{default}' % (page.title()))
            except pywikibot.SpamfilterError, error:
                pywikibot.output(
                    u'\03{lightblue}Cannot change %s because of spam blacklist '
                    u'entry %s\03{default}' % (page.title(), error.url))
            else:
                return True
        return False
    ## @since  10326
    #  @remarks needed by various bots
    def append(self, page, text, comment=None, section=None, **kwargs):
        if section:
            pywikibot.output(
                u'\03{lightblue}Appending to wiki on %s in section '
                u'%s...\03{default}' % (page.title(asLink=True), section))
            for i in range(3):
                try:
                    # Append to page section
                    page.append(text, comment=comment, section=section,
                                **kwargs)
                except pywikibot.PageNotSaved, error:
                    pywikibot.output(
                        u'\03{lightblue}Cannot change %s because of '
                        u'%s\03{default}' % (page.title(), error))
                else:
                    return True
        else:
            content = self.load(page)    # 'None' if not existing page
            if not content:                # (create new page)
                content = u''
            content += u'\n\n'
            content += text
            return self.save(page, content, comment=comment, **kwargs)
    ## @since  10326
    #  @remarks needed by various bots
    def loadTemplates(self, page, template, default={}):
        """Get operating mode from page with template by searching the template.
          @param page: The user (page) for which the data should be retrieved.
          Returns a list of dict with the templates parameters found.
        """
        self._content = self.load(page)  # 'None' if not existing page
        templates = []
        if not self._content:
            return templates  # catch empty or not existing page
        for tmpl in pywikibot.extract_templates_and_params(self._content):
            if tmpl[0] == template:
                param_default = {}
                param_default.update(default)
                param_default.update(tmpl[1])
                templates.append(param_default)
        return templates
    ## @since  10326
    #  @remarks common interface to bot job queue on wiki
    def loadJobQueue(self, page, queue_security, reset=True):
        """Check if the data queue security is ok to execute the jobs,
          if so read the jobs and reset the queue.
          @param page: Wiki page containing job queue.
          @type  page: page
          @param queue_security: This string must match the last edit
                              comment, or else nothing is done.
          @type  queue_security: string
          Returns a list of jobs. This list may be empty.
        """
        try:
            actual = page.getVersionHistory(revCount=1)[0]
        except:
            pass
        secure = False
        for item in queue_security[0]:
            secure = secure or (actual[2] == item)
        secure = secure and (actual[3] == queue_security[1])
        if not secure:
            return []
        data = self._REGEX_eol.split(page.get())
        if reset:
            pywikibot.output(u'\03{lightblue}Job queue reset...\03{default}')
            pywikibot.setAction(u'reset job queue')
            page.put(u'', minorEdit=True)
        queue = []
        for line in data:
            queue.append(line[1:].strip())
        return queue




Line 365: Line 231:
             pywikibot.output(u'Unknown argument: %s' % arg)
             pywikibot.output(u'Unknown argument: %s' % arg)


     cat = catlib.Category(pywikibot.getSite(), 'Category:MediaWiki')
     cat = catlib.Category(pywikibot.getSite("wikiindex", "wikiindex"), 'Category:MediaWiki')
     gen = pagegenerators.CategorizedPageGenerator(cat, start=start)
     gen = pagegenerators.CategorizedPageGenerator(cat, start=start)


checkuser, Administrators
2,635

edits