User:SebBOT/Filters

From Team Fortress Wiki
Jump to: navigation, search

How to disable a filter

If the bot is malfunctioning, chances are that the problem lies in one of these blocks of code. Thus, instead of shutting down the whole bot, it would be wiser to disable only the chunk of code that is misbehaving. To make the bot ignore a certain line, add a "#" in front of it:

 # This line will be ignored

If there are multiple lines, wrap them inside triple-quotes (you still need to put the two spaces at the beginning of the line):

 """This line will be ignored
 and this one as well
 and this one is cake
 and the previous one was a lie but it was still ignored"""

If all else fails, you can simply delete the block from the page. The bot can't come up with code by itself yet, so it won't run anything. Or, if the problem really is elsewhere, block the bot.

Page filters

 addPageFilter(r'^user:', r'(?:talk|help|wiki|template):')

Link filters

Category removal on pages using {{Item infobox}}

 def removeCategory(l, **kwargs):
     catsToRemove = [u'Category:Weapons', u'Category:Hats', u'Category:Primary weapons', u'Category:Secondary weapons', u'Category:Melee weapons', u'Category:PDA1 weapons', u'Category:PDA2 weapons', u'Category:Miscellaneous items', u'Category:Tools', u'Category:Action items', u'Category:Taunts', u'Community-contributed items']
     regLang = compileRegex('/[^/]+$')
     if 'article' not in kwargs or regLang.sub(u, l.getLink()) not in catsToRemove:
         return l
     if u'Category:Item infobox usage' not in kwargs['article'].getCategories():
         return l
     return None
 addLinkFilter(removeCategory)

Template filters

Manage all {{Item infobox}}es

 def infoboxFilter(t, **kwargs):
     filteredTemplates = ('weapon infobox', 'hat infobox', 'tool infobox', 'item infobox') # Only do stuff to these templates
     if t.getName().lower() not in filteredTemplates:
         return t # Skip
     t.setName('Item infobox')
     t.indentationMatters(True) # Reindents every time, not only when modifying values
     paramAliases = { # Parameter alias 'goodParam': 'badParam', or 'goodParam': [list of bad params].
         'name': ['weapon-name-override', 'hat-name-override', 'tool-name-override', 'NAME', 'name-override'],
         'image': ['weapon-image', 'hat-image', 'tool-image'],
         'kill-text-1': 'kill-text',
         'team-colors': 'has-team-colors',
         'two-models': 'has-two-models',
         'slot': ['weapon-slot', 'hat-slot', 'tool-slot'],
         'trade': 'tradable',
         'gift': 'giftable',
         'craft': 'craftable',
         'paint': ['paintable', 'Paint'],
         'rename': ['name-tag', 'nametag'],
         'loadout': 'display-loadout-stats',
         'level': 'level-and-type'
     }
     catstoCheck = { # Mapping 'templateAttribute': [List of 'Category|templateAttributeValue']
         'type': ['Taunts|taunt', 'Action items|action', 'Hats|hat', 'Miscellaneous items|misc item', 'Tools|tools', 'Weapons|weapon'],
         'slot': ['Primary weapons|primary', 'Secondary weapons|secondary', 'Melee weapons|melee', 'PDA1 weapons|pda 1', 'PDA2 weapons|pda 2']
     }
     catsDontCheck = [u'Category:Beta and unused content', 'Category:Taunts'] # Type and slot won't be modified on these pages
     preferedOrder = [ # Prefered order of keys inside template
         'name', 'game', 'type', 'beta', 'unused', 'image', 'imagewidth', 'team-colors', 'two-models', 'skin-image-red', 'skin-image-blu', 'TFC-model', 'QTF-model', 'kill-icon-#', 'kill-text-#', 'used-by', 'slot', 'crafting-slot', 'custom-slot', 'contributed-by', 'released', 'availability', 'trade', 'gift', 'craft', 'numbered', 'paint', 'rename', 'medieval', 'ammo-loaded', 'ammo-carried', 'ammo-type', 'show-ammo', 'reload', 'loadout', 'loadout-prefix', 'quality', 'level', '%ATTRIBUTES%', 'item-description', 'item-uses', 'item-flags', 'item-expiration'
     ]
     checkEnglish = ['trade', 'craft', 'paint', 'rename'] # If these attributes aren't yes or no, check the values on the english page
     attributeTypes = ['neutral', 'positive', 'negative'] # Possible loadout attribute types
     regLang = compileRegex('/[^/]+$')
     # Step 0 - Check categories:
     tCats = None
     isTFC = False # Assume false by default
     if 'article' in kwargs:
         if kwargs['article'] is not None:
             cats2 = kwargs['article'].getCategories()
             tCats = []
             for c in cats2:
                 tCats.append(u(regLang.sub(u, u(c))))
             isTFC = u'Category:Weapons (Classic)' in tCats
     # Step 1 - Rename obsolete attributes
     for p in paramAliases:
         if type(paramAliases[p]) is type([]):
             for a in paramAliases[p]:
                 t.renameParam(a, p)
         else:
             t.renameParam(paramAliases[p], p)
     # Step 2 - Fix ammo stuff
     if t.getParam('ammo-carried') == u'1' or t.getParam('ammo-loaded') == u'1':
         t.delParam('ammo-loaded', 'ammo-carried', 'ammo-type')
         t.setParam('show-ammo', 'off')
     # Step 3 - Fix more ammo stuff, delete pricing attributes
     t.delParam('ammo', 'price', 'show-price', 'purchasable', 'backpack-image')
     # Step 4 - Fix reload stuff
     if t.getParam('reload') is None:
         t.renameParam('reload-type', 'reload')
     # Step 5 - Count, split, order and fix loadout attributes
     attrNumber = 1
     regexAttrSplit = compileRegex(r'\s*<br[^<>]*>\s*')
     for a in attributeTypes:
         if t.getParam(a + '-attributes') is not None:
             attrs = regexAttrSplit.split(t.getParam(a + '-attributes'))
             for attr in attrs:
                 t.setParam('att-' + str(attrNumber) + '-' + a, attr)
                 attrNumber += 1
             t.delParam(a + '-attributes')
     if tCats is not None:
         # Step 6 - Lookup english fallback on certain attributes
         fetchEnglish = False
         values = {}
         for attr in checkEnglish:
             if t.getParam(attr) is not None and t.getParam(attr).lower() not in (u'yes', u'no'):
                 fetchEnglish = True
             elif t.getParam(attr) is not None:
                 values[attr] = t.getParam(attr).lower()
         if regLang.search(kwargs['article'].title):
             englishArticle = page(regLang.sub(, kwargs['article'].title))
             try:
                 englishContent = englishArticle.getWikiText()
             except:
                 englishContent = u
             englishContent, englishTemplates = templateExtract(englishContent)
             for englishT in englishTemplates:
                 if englishT.getName().lower() in filteredTemplates:
                     for p in paramAliases:
                         if type(paramAliases[p]) is type([]):
                             for a in paramAliases[p]:
                                 englishT.renameParam(a, p)
                         else:
                             englishT.renameParam(paramAliases[p], p)
                     for attr in checkEnglish:
                         if englishT.getParam(attr) is not None and englishT.getParam(attr).lower() in (u'yes', u'no'):
                             values[attr] = englishT.getParam(attr).lower()
                     break
         for attr in values:
             t.setParam(attr, values[attr])
         checkCats = True
         for c in catsDontCheck:
             if c in tCats:
                 checkCats = False
                 break
         if not isTFC and not t.getParam('custom-slot') and checkCats:
             # Step 7 - Set certains attributes based on page categories
             for cname in catstoCheck:
                 cname = u(cname)
                 found = None
                 foundmultiple = False
                 for c in catstoCheck[cname]:
                     cat, val = u(c).split(u'|')
                     cat = u'Category:' + u(regLang.sub(u, cat))
                     if cat in tCats:
                         if found is not None:
                             foundmultiple = True
                         found = val
                 if not foundmultiple:
                     t.setParam(cname, found)
             if u'Category:Weapons' not in tCats:
                 t.delParam('slot')
             # Step 8 - Convert neutral attributes - Disabled
             """desc = []
             if t.getParam('item-description') is not None:
                 desc = regexAttrSplit.split(t.getParam('item-description'))
             for i in range(1, 10):
                 if t.getParam('att-' + str(i) + '-neutral') is not None:
                     desc.append(t.getParam('att-' + str(i) + '-neutral'))
                     t.delParam('att-' + str(i) + '-neutral')
             desc2 = []
             for d in desc:
                 if d.strip():
                     desc2.append(d.strip())
             if len(desc2):
                 t.setParam('item-description', u'
'.join(desc2))""" # # Commenting out because of new parameter 'beta' # and there being no way to distinguish between beta items & unused content thus far # seb26 04:58, 12 February 2011 (UTC) # # if u'Category:Beta and unused content' in tCats: # t.setParam('unused', 'yes') # Step 9 - Do TFC stuff if isTFC: t.setParam('type', 'weapon') t.setParam('game', 'tfc') # Step 10 - Set correct preferred indentation for k in ['quality', 'level', 'item-description', 'item-uses', 'item-flags', 'level-and-type', 'loadout-name', 'loadout-prefix']: t.setPreferedIndentation(k, 2) for i in range(1, 10): for a in attributeTypes: t.setPreferedIndentation('att-' + str(i) + '-' + a, 2) # Step 11 - Build correct attribute order newOrder = [] for o in preferedOrder: if o.find('#') == -1: newOrder.append(o) continue if o == '%ATTRIBUTES%': for i in range(1, 10): for a in attributeTypes: newOrder.append('att-' + str(i) + '-' + a) for i in range(1, 10): newOrder.append(o.replace('#', str(i))) t.setPreferedOrder(newOrder) # Step 12 - There is no step 12 return t addTemplateFilter(infoboxFilter)