User:WindBOT/Filters: Difference between revisions

Jump to navigation Jump to search
→‎Implement {{tl|Dictionary}}: Match tfwiki version
m (→‎Update checklists on list of subscribers: +improvements from tfwiki)
(→‎Implement {{tl|Dictionary}}: Match tfwiki version)
Line 132: Line 132:
         self.invalidParamError = <nowiki>"""<span class="error">Error: invalid param.</span>[[Category:ERROR]]"""</nowiki>
         self.invalidParamError = <nowiki>"""<span class="error">Error: invalid param.</span>[[Category:ERROR]]"""</nowiki>
         self.subpageTemplateID = <nowiki>"""%string%"""</nowiki>
         self.subpageTemplateID = <nowiki>"""%string%"""</nowiki>
        self.partialUpdateThreshold = 750 # Update SyncData every n edits
         self.dictionaries = {
         self.dictionaries = {
             u'Template:Dictionary/items': { # Dictionary page
             u'Template:Dictionary/items': { # Dictionary page
Line 228: Line 229:
         self.stringsExtract = compileRegex(r'(?:^[ \t]*#[ \t]*([^\r\n]*?)[ \t]*$\s*)?^[ \t]*([^\r\n]+?[ \t]*(?:\|[ \t]*[^\r\n]+?[ \t]*)*):[ \t]*([^\r\n]+?[ \t]*$|\s*[\r\n]+(?:\s*[ \t]+[-\w]+[ \t]*:[ \t]*[^\r\n]+[ \t]*$)+)', re.IGNORECASE | re.MULTILINE)
         self.stringsExtract = compileRegex(r'(?:^[ \t]*#[ \t]*([^\r\n]*?)[ \t]*$\s*)?^[ \t]*([^\r\n]+?[ \t]*(?:\|[ \t]*[^\r\n]+?[ \t]*)*):[ \t]*([^\r\n]+?[ \t]*$|\s*[\r\n]+(?:\s*[ \t]+[-\w]+[ \t]*:[ \t]*[^\r\n]+[ \t]*$)+)', re.IGNORECASE | re.MULTILINE)
         self.translationExtract = compileRegex(r'^[ \t]+([-\w]+)[ \t]*:[ \t]*([^\r\n]+)[ \t]*$', re.IGNORECASE | re.MULTILINE)
         self.translationExtract = compileRegex(r'^[ \t]+([-\w]+)[ \t]*:[ \t]*([^\r\n]+)[ \t]*$', re.IGNORECASE | re.MULTILINE)
        self.scheduler = BatchScheduler(16)
         addWhitelistPage(self.dictionaries.keys())
         addWhitelistPage(self.dictionaries.keys())
        self.editCounts = {}
    def updateSyncData(self, currentDict, syncData, note=''):
        # Build syncdata string representation
        syncKeys = syncData.keys()
        syncKeys.sort()
        syncLines = []
        for k in syncKeys:
            syncLines.append(k + u':' + syncData[k])
        if note:
            note = u' (' + u(note) + u')'
        editPage(self.dictionaries[currentDict]['sync'], u'\n'.join(syncLines), summary=<nowiki>u'Updated synchronization information for [[:' + currentDict + u']]' + note + u'.'</nowiki>, minor=True, nocreate=False)
     def generateSubpage(self, keyName, data, currentDict, syncData):
     def generateSubpage(self, keyName, data, currentDict, syncData):
         h = hashlib.md5()
         h = hashlib.md5()
Line 286: Line 297:
         if keyName in syncData and syncData[keyName] == h:
         if keyName in syncData and syncData[keyName] == h:
             return # Same hash
             return # Same hash
        syncData[keyName] = h # Update sync data
         subpage = subpage.replace(u'%dictionary%', currentDict)
         subpage = subpage.replace(u'%dictionary%', currentDict)
         subpage = subpage.replace(u'%dictionaryname%', self.dictionaries[currentDict]['name'])
         subpage = subpage.replace(u'%dictionaryname%', self.dictionaries[currentDict]['name'])
         subpage = subpage.replace(u'%keyname%', keyName)
         subpage = subpage.replace(u'%keyname%', keyName)
         self.scheduler.schedule(editPage, currentDict + self.subpageSeparator + keyName, subpage, summary=<nowiki>u'Pushed changes from [[:' + currentDict + u']] for string "' + keyName + u'".'</nowiki>, minor=True, nocreate=False)
         if editPage(currentDict + self.subpageSeparator + keyName, subpage, summary=<nowiki>u'Pushed changes from [[:' + currentDict + u']] for string "' + keyName + u'".'</nowiki>, minor=True, nocreate=False):
            syncData[keyName] = h # Update sync data
            if currentDict not in self.editCounts:
                self.editCounts[currentDict] = 0
            self.editCounts[currentDict] += 1
            if self.editCounts[currentDict] > self.partialUpdateThreshold:
                self.editCounts[currentDict] = 0
                self.updateSyncData(currentDict, syncData, 'Partial update')
     def processComment(self, commentString, currentDict, definedStrings, syncData):
     def processComment(self, commentString, currentDict, definedStrings, syncData):
         commentContents = []
         commentContents = []
Line 322: Line 339:
             validKeyNames = []
             validKeyNames = []
             for keyName in keyNames:
             for keyName in keyNames:
                 keyName = keyName.replace(u'_', u' ').strip()
                 keyName = keyName.replace(u'_', u' ').replace(u'#', u'').strip()
                 if keyName in definedStrings:
                 if keyName in definedStrings:
                     continue # Duplicate key
                     continue # Duplicate key
Line 330: Line 347:
             if len(validKeyNames):
             if len(validKeyNames):
                 commentContents.append(comment + u' | '.join(validKeyNames) + u':' + dataWriteback)
                 commentContents.append(comment + u' | '.join(validKeyNames) + u':' + dataWriteback)
        self.scheduler.execute()
         return u'\n\n'.join(commentContents)
         return u'\n\n'.join(commentContents)
     def __call__(self, content, **kwargs):
     def __call__(self, content, **kwargs):
Line 338: Line 354:
             return content
             return content
         currentDict = u(kwargs['article'].title)
         currentDict = u(kwargs['article'].title)
         syncPage = page(self.dictionaries[currentDict]['sync'])
         if random.randint(0, 50) == 0: # With probability 2%, ignore syncdata completely. Helps with stale syncdata and people overwriting things.
        try:
            syncDataText = u(syncPage.getWikiText()).split(u'\n')
        except: # Page probably doesn't exist
             syncDataText = u''
             syncDataText = u''
        else:
            try:
                syncDataText = u(page(self.dictionaries[currentDict]['sync']).getWikiText()).split(u'\n')
            except: # Page probably doesn't exist
                syncDataText = u''
         syncData = {}
         syncData = {}
         for sync in syncDataText:
         for sync in syncDataText:
Line 361: Line 379:
             newContent += u'<!--\n\n' + self.processComment(u(comment.group(1)).strip(), currentDict, definedStrings, syncData) + u'\n\n-->'
             newContent += u'<!--\n\n' + self.processComment(u(comment.group(1)).strip(), currentDict, definedStrings, syncData) + u'\n\n-->'
         newContent += content[previousIndex:]
         newContent += content[previousIndex:]
        # Check if we need to update sync data
        needUpdate = False
        for k in syncData:
            if k not in oldSyncData or oldSyncData[k] != syncData[k]:
                needUpdate = True
                break
         # Check for deleted strings
         # Check for deleted strings
         for k in oldSyncData:
         for k in oldSyncData:
Line 376: Line 388:
                 if k in syncData:
                 if k in syncData:
                     del syncData[k]
                     del syncData[k]
                needUpdate = True
         self.updateSyncData(currentDict, syncData, 'Full update')
         if needUpdate:
        self.editCounts[currentDict] = 0
            # Build syncdata string representation
            syncKeys = syncData.keys()
            syncKeys.sort()
            syncLines = []
            for k in syncKeys:
                syncLines.append(k + u':' + syncData[k])
            editPage(syncPage, u'\n'.join(syncLines), summary=<nowiki>u'Updated synchronization information for [[:' + currentDict + u']].'</nowiki>, minor=True, nocreate=False)
         return newContent
         return newContent
     def scheduledRun(self):
     def scheduledRun(self):

Navigation menu