예제 #1
0
def main():
    import os
    index = None
    djvu = None
    pages = None
    # what would have been changed.
    dry = False
    ask = False
    overwrite = 'ask'

    # Parse command line arguments
    for arg in pywikibot.handleArgs():
        if arg.startswith("-dry"):
            dry = True
        elif arg.startswith("-ask"):
            ask = True
        elif arg.startswith("-overwrite:"):
            overwrite = arg[11:12]
            if overwrite != 'y' and overwrite != 'n':
                pywikibot.output(u"Unknown argument %s; will ask before overwriting" % arg)
                overwrite = 'ask'
        elif arg.startswith("-djvu:"):
            djvu = arg[6:]
        elif arg.startswith("-index:"):
            index = arg[7:]
        elif arg.startswith("-pages:"):
            pages = arg[7:]
        else:
            pywikibot.output(u"Unknown argument %s" % arg)

    # Check the djvu file exists
    if djvu:
        os.stat(djvu)

        if not index:
            import os.path
            index = os.path.basename(djvu)

    if djvu and index:
        site = pywikibot.getSite()
        index_page = pywikibot.Page(site, index)

        if site.family.name != 'wikisource':
            raise pywikibot.PageNotFound(u"Found family '%s'; Wikisource required." % site.family.name)

        if not index_page.exists() and index_page.namespace() == 0:
            index_namespace = site.mediawiki_message('Proofreadpage index namespace')

            index_page = pywikibot.Page(pywikibot.getSite(),
                                        u"%s:%s" % (index_namespace, index))
        if not index_page.exists():
            raise pywikibot.NoPage(u"Page '%s' does not exist" % index)
        pywikibot.output(u"uploading text from %s to %s"
                         % (djvu, index_page.title(asLink=True)) )
        bot = DjVuTextBot(djvu, index, pages, ask, overwrite, dry)
        if not bot.has_text():
            raise ValueError("No text layer in djvu file")
        bot.run()
    else:
        pywikibot.showHelp()
예제 #2
0
def appendtext(page, apptext):
    global always
    if page.isRedirectPage():
        page = page.getRedirectTarget()
    if not page.exists():
        if page.isTalkPage():
            text = u''
        else:
            raise pywikibot.NoPage(u"Page '%s' does not exist" % page.title())
    else:
        text = page.get()
    # Here you can go editing. If you find you do not
    # want to edit this page, just return
    oldtext = text
    text += apptext
    if text != oldtext:
        pywikibot.showDiff(oldtext, text)
        if not always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?', ['Yes', 'No', 'All'],
                ['y', 'N', 'a'], 'N')
            if choice == 'a':
                always = True
        if always or choice == 'y':
            page.put(text, pywikibot.translate(pywikibot.getSite(), comment))
예제 #3
0
def main():
    import os
    index = None
    djvu = None
    pages = None
    # what would have been changed.
    dry = False
    ask = False

    # Parse command line arguments
    for arg in wikipedia.handleArgs():
        if arg.startswith("-dry"):
            dry = True
        elif arg.startswith("-ask"):
            ask = True
        elif arg.startswith("-djvu:"):
            djvu = arg[6:]
        elif arg.startswith("-index:"):
            index = arg[7:]
        elif arg.startswith("-pages:"):
            pages = arg[7:]
        else:
            wikipedia.output(u"Unknown argument %s" % arg)

    # Check the djvu file exists
    if djvu:
        os.stat(djvu)

        if not index:
            import os.path
            index = os.path.basename(djvu)

    if djvu and index:
        site = wikipedia.getSite()
        index_page = wikipedia.Page(site, index)

        if site.family.name != 'wikisource':
            raise wikipedia.PageNotFound(
                u"Found family '%s'; Wikisource required." % site.family.name)

        if not index_page.exists() and index_page.namespace() == 0:
            index_namespace = wikipedia.Page(
                site, 'MediaWiki:Proofreadpage index namespace').get()

            index_page = wikipedia.Page(wikipedia.getSite(),
                                        u"%s:%s" % (index_namespace, index))

        if not index_page.exists():
            raise wikipedia.NoPage(u"Page '%s' does not exist" % index)

        wikipedia.output(u"uploading text from %s to %s" %
                         (djvu, index_page.aslink()))

        bot = DjVuTextBot(djvu, index, pages, ask, dry)
        if not bot.has_text():
            raise ValueError("No text layer in djvu file")

        bot.run()
    else:
        wikipedia.showHelp()
def _wikipedia_Page_getRedirectTarget(self):
    if not self.exists():
        raise wikipedia.NoPage(self)
    if not self.isRedirectPage():
        raise IsNotRedirectPage(self)
    try:
        return toolserver.Generators.getRedirect(self).next()
    except StopIteration:
        raise IsNotRedirectPage(self)
def _wikipedia_Page_latestRevision(self):
    """ Gets the latest revision from the database """
    if not self._permalink:
        ret = toolserver.query(
            """ SELECT page_latest
                                   FROM %s.page
                                   WHERE page_title=%%s AND page_namespace=%%s """
            % (self.site().dbName()),
            (self.titleWithoutNamespace(True), self.namespace()))
        if (len(ret) == 0):
            raise wikipedia.NoPage('No revisions found for page %%s' %
                                   self.__repr__())
        else:
            self._permalink = u'%i\n      ' % ret[0]['page_latest']
    return int(self._permalink)
def _wikipedia_Page_isEmpty(self):
    """
    True if the page has less than 4 characters, except for
    language links and category links, False otherwise.
    Categories from templates are counted!
    Can raise wikipedia.NoPage
    """
    # retrieve page id and page length
    ret = toolserver.query(
        """SELECT page_id, page_len
                              FROM %s.page
                              WHERE page_title=%%s AND page_namespace=%%s""" %
        self.site().dbName(),
        (self.titleWithoutNamespace(True), self.namespace()))
    if (len(ret) == 0):
        raise wikipedia.NoPage('No such page %%s' % self.__repr__())

    page_id = ret[0]['page_id']
    length = ret[0]['page_len']
    # remove length of langlink links
    ret = toolserver.query(
        """ SELECT COALESCE(SUM(LENGTH(ll_lang) + LENGTH(ll_title) + 5), 0) AS SUM
                               FROM %s.langlinks
                               WHERE ll_from=%%s
                               GROUP BY ll_from """ % self.site().dbName(),
        page_id)
    if (len(ret) > 0):
        length -= ret[0]['sum']

    # remove length of category links
    ret = toolserver.query(
        """ SELECT COALESCE(SUM(LENGTH(cl_to) + LENGHT(cl_sortkey) + 5), 0) AS SUM
                               FROM %s.categorylinks
                               WHERE cl_from=%%s
                               GROUP BY cl_from """ % self.site().dbName(),
        page_id)
    if (len(ret) > 0):
        length -= ret[0]['sum']

    return (length < 4)