Example #1
0
def writeEntireTemplate(validatedPage, viafnum):
    """Find the lowest index of the list of matches"""
    #dictionary is of matches and their lowest index position in the pageWikiText
    dictOfMatches = {'<!-- Metadata: see [[Wikipedia:Persondata':'',
                    '{{Persondata': '',
                    '{{DEFAULTSORT':'',
                    '[[Category:': ''}
    
    pageWikiText = validatedPage.get()
    
    #iterater over the keys put their lowest position
    lowestOccuringMatch = ''
    lowestOccuringMatchIndex = ''
    for match in dictOfMatches:
        try: 
            dictOfMatches[match] = pageWikiText.index(match)
            if dictOfMatches[match] < lowestOccuringMatchIndex:
                lowestOccuringMatch = match
                lowestOccuringMatchIndex = dictOfMatches[match]
        except ValueError:
            pass 
    #if there was nothing to put the template above just log otherwise call replaceExcept
    if lowestOccuringMatch == '<!-- Metadata: see [[Wikipedia:Persondata':
        replacementText = textlib.replaceExcept(pageWikiText, '<!-- Metadata: see \[\[Wikipedia:Persondata\]\] -->' , '{{Authority control|VIAF=' + str(viafnum) + '}}' + '\n' + '<!-- Metadata: see [[Wikipedia:Persondata]] -->', exceptions=[], caseInsensitive=True,
                  allowoverlap=False, marker = '', site = enwp)
    elif lowestOccuringMatch == '{{Persondata':
        replacementText = textlib.replaceExcept(pageWikiText, '{{Persondata' , '{{Authority control|VIAF=' + str(viafnum) + '}}' + '\n' + '{{Persondata', exceptions=[], caseInsensitive=True,
                  allowoverlap=False, marker = '', site = enwp)
    elif lowestOccuringMatch == '{{DEFAULTSORT':
        replacementText = textlib.replaceExcept(pageWikiText, '{{DEFAULTSORT' , '{{Authority control|VIAF=' + str(viafnum) + '}}' + '\n' + '{{DEFAULTSORT', exceptions=[], caseInsensitive=True,
                  allowoverlap=False, marker = '', site = enwp)
    elif lowestOccuringMatch == '[[Category:':
        replacementText = textlib.replaceExcept(pageWikiText, '\[\[Category:' , '{{Authority control|VIAF=' + str(viafnum) + '}}' +'\n' + '[[Category:', exceptions=[], caseInsensitive=True,
                  allowoverlap=False, marker = '', site = enwp)
    elif lowestOccuringMatch == '':
        logging.warning("writeEntireTemplate on page had no persondata, category, default sort for page: %s", validatedPage.title())   
        replacementText = pageWikiText
    
    try:
        validatedPage.put(newtext = replacementText, comment = 'Added the {{[[Template:Authority control|Authority control]]}} template with' + ' VIAF number ' + str(viafnum)+ ': ' + 'http://viaf.org/viaf/' + str(viafnum)+' . Please [[WP:VIAF/errors|report any errors]].', 
                          watchArticle = False , minorEdit=True, force=False, sysop=False, botflag=True, maxTries = 5)
    except exceptions.LockedPage:
        raise exceptions.LockedPage
    except exceptions.EditConflict:
        raise exceptions.EditConflict
    except exceptions.ServerError:
        logging.warning("writeEntireTemplate gave a generic server error for %s",  validatedPage.title())
    except exceptions.SpamfilterError:
        raise exceptions.SpamfilterError
    except exceptions.PageNotSaved:
        raise exceptions.PageNotSaved       
Example #2
0
def writeVIAFparamOnly(validatedPage,viafnum):
    """Uses the textlib method replaceExcept"""

    pageWikiText = validatedPage.get()

    replacementText = textlib.replaceExcept(pageWikiText, '{{Authority control' , '{{Authority control|VIAF=' + str(viafnum), exceptions=[], caseInsensitive=False,
                  allowoverlap=False, marker = '', site = enwp)

    try:
        validatedPage.put(newtext = replacementText, comment = 'Added VIAF number ' + str(viafnum) + ' to {{[[Template:Authority control|Authority control]]}} template. http://viaf.org/viaf/' + str(viafnum) +' .Please [[WP:VIAF/errors|report any errors]].',
                           watchArticle = False , minorEdit=True, force=False, sysop=False, botflag=True, maxTries = 5)
    except exceptions.LockedPage:
        raise exceptions.LockedPage
    except exceptions.EditConflict:
        raise exceptions.EditConflict
    except exceptions.ServerError:
        logging.warning("writeVIAFparam on page gave a generic server error for page %s",  validatedPage.title())
    except exceptions.SpamfilterError:
        raise exceptions.SpamfilterError
    except exceptions.PageNotSaved:
        raise exceptions.PageNotSaved