User:MauchoBot/source1


 * 1) !/usr/bin/env python
 * 2) -*- coding: utf-8  -*-

""" This script goes over multiple pages, searches for pages where ',                                         re.IGNORECASE|re.DOTALL)        try:            self.referencesTemplates = referencesTemplates[                wikipedia.getSite.family.name][wikipedia.getSite.lang]        except KeyError:            self.referencesTemplates = []        try:            self.referencesText = referencesSubstitute[                wikipedia.getSite.family.name][wikipedia.getSite.lang]        except KeyError:            self.referencesText = u' '

def lacksReferences(self, text, verbose = True): """       Checks whether or not the page is lacking a references tag.        """ oldTextCleaned = wikipedia.removeDisabledParts(text) if self.referencesR.search(oldTextCleaned) or \ self.referencesTagR.search(oldTextCleaned): if verbose: wikipedia.output(u'No changes necessary: references tag found.') return False elif self.referencesTemplates: templateR = u'\s*' commentPattern  = r').)*?-->\s*' metadataR = re.compile(r'(\r\n)?(%s|%s|%s|%s)$'                              % (categoryPattern, interwikiPattern, templatePattern, commentPattern), re.DOTALL) tmpText = oldText while True: match = metadataR.search(tmpText) if match: tmpText = tmpText[:match.start] else: break wikipedia.output(           u'Found no section that can be preceeded by a new references section. Placing it before interwiki links, categories, and bottom templates.') index = len(tmpText) return self.createReferenceSection(oldText, index)

def createReferenceSection(self, oldText, index, ident = '=='): newSection = u'\n%s %s %s\n%s\n' % (ident, wikipedia.translate(self.site, referencesSections)[0], ident, self.referencesText) return oldText[:index] + newSection + oldText[index:]

def save(self, page, newText): """       Saves the page to the wiki, if the user accepts the changes made.        """ wikipedia.showDiff(page.get, newText) if not self.always: choice = wikipedia.inputChoice(               u'Do you want to accept these changes?',                ['Yes', 'No', 'Always yes'], ['y', 'N', 'a'], 'Y') if choice == 'n': return elif choice == 'a': self.always = True

if self.always: try: page.put(newText) except wikipedia.EditConflict: wikipedia.output(u'Skipping %s because of edit conflict'                                % (page.title,)) except wikipedia.SpamfilterError, e:               wikipedia.output(                    u'Cannot change %s because of blacklist entry %s'                    % (page.title, e.url)) except wikipedia.LockedPage: wikipedia.output(u'Skipping %s (locked page)' % (page.title,)) else: # Save the page in the background. No need to catch exceptions. page.put_async(newText) return

def run(self): comment = wikipedia.translate(self.site, msg) wikipedia.setAction(comment)

for page in self.generator: # Show the title of the page we're working on. # Highlight the title in purple. wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"                            % page.title) try: text = page.get except wikipedia.NoPage: wikipedia.output(u"Page %s does not exist?!"                                % page.title(asLink=True)) continue except wikipedia.IsRedirectPage: wikipedia.output(u"Page %s is a redirect; skipping."                                % page.title(asLink=True)) continue except wikipedia.LockedPage: wikipedia.output(u"Page %s is locked?!"                                % page.title(asLink=True)) continue if wikipedia.getSite.sitename == 'wikipedia:en' and page.isIpEdit: wikipedia.output(                   u"Page %s is edited by IP. Possible vandalized" % page.title(asLink=True)) continue if self.lacksReferences(text): newText = self.addReferences(text) self.save(page, newText)

def main: #page generator gen = None # This temporary array is used to read the page title if one single # page to work on is specified by the arguments. pageTitle = [] # Which namespaces should be processed? # default to [] which means all namespaces will be processed namespaces = [] # Never ask before changing a page always = False # This factory is responsible for processing command line arguments # that are also used by other scripts and that determine on which pages # to work on. genFactory = pagegenerators.GeneratorFactory

for arg in wikipedia.handleArgs: if arg.startswith('-xml'): if len(arg) == 4: xmlFilename = wikipedia.input(u'Please enter the XML dump\'s filename:') else: xmlFilename = arg[5:] gen = XmlDumpNoReferencesPageGenerator(xmlFilename) elif arg.startswith('-namespace:'): try: namespaces.append(int(arg[11:])) except ValueError: namespaces.append(arg[11:]) elif arg == '-always': always = True else: if not genFactory.handleArg(arg): pageTitle.append(arg)

if pageTitle: page = wikipedia.Page(wikipedia.getSite, ' '.join(pageTitle)) gen = iter([page]) if not gen: gen = genFactory.getCombinedGenerator if not gen: wikipedia.showHelp('noreferences') else: if namespaces != []: gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = NoReferencesBot(preloadingGen, always) bot.run

if __name__ == "__main__" and wikipedia.handleUrlAndHeader: try: wikipedia.startContent(form=True) main finally: wikipedia.endContent wikipedia.stopme