def setPageInfo(self): """Sets basic page info, required for almost everything""" followRedir = self.followRedir params = {'action': 'query'} if self.pageid: params['pageids'] = self.pageid else: params['titles'] = self.title if followRedir: params['redirects'] = '' req = api.APIRequest(self.site, params) response = req.query() self.pageid = response['query']['pages'].keys()[0] if self.pageid > 0: self.exists = True if 'missing' in response['query']['pages'][str(self.pageid)]: if not self.title: # Pageids are never recycled, so a bad pageid with no title will never work raise wiki.WikiError("Bad pageid given with no title") self.exists = False if 'invalid' in response['query']['pages'][str(self.pageid)]: raise BadTitle(self.title) if 'title' in response['query']['pages'][str(self.pageid)]: self.title = response['query']['pages'][str( self.pageid)]['title'].encode('utf-8') self.namespace = int(response['query']['pages'][str( self.pageid)]['ns']) if self.namespace is not 0: self.unprefixedtitle = self.title.split(':', 1)[1] else: self.unprefixedtitle = self.title self.pageid = int(self.pageid) if self.pageid < 0: self.pageid = 0 return self
def __getHistoryInternal(self, direction, content, limit, rvcontinue): if self.pageid == 0 and not self.title: self.setPageInfo() if not self.exists: raise NoPage if direction != 'newer' and direction != 'older': raise wiki.WikiError("direction must be 'newer' or 'older'") params = { 'action': 'query', 'prop': 'revisions', 'rvdir': direction, 'rvprop': 'ids|flags|timestamp|user|userid|size|sha1|comment', 'continue': '', 'rvlimit': limit } if self.pageid: params['pageids'] = self.pageid else: params['titles'] = self.title if content: params['rvprop'] += '|content' if rvcontinue: params['continue'] = rvcontinue['continue'] params['rvcontinue'] = rvcontinue['rvcontinue'] req = api.APIRequest(self.site, params) response = req.query(False) id = response['query']['pages'].keys()[0] if not self.pageid: self.pageid = int(id) revs = response['query']['pages'][id]['revisions'] rvc = None if 'continue' in response: rvc = response['continue'] return (revs, rvc)
def __init__(self, site, title=False, check=True, followRedir=True, section=False, sectionnumber=False, pageid=False, namespace=False): """ wiki - A wiki object title - The page title, as a string or unicode object check - Checks for existence, normalizes title, required for most things followRedir - follow redirects (check must be true) section - the section name sectionnumber - the section number pageid - pageid, can be in place of title namespace - use to set the namespace prefix *if its not already in the title* """ # Initialize instance vars from function args if not title and not pageid: raise wiki.WikiError("No title or pageid given") self.site = site if pageid: self.pageid = int(pageid) else: self.pageid = 0 self.followRedir = followRedir self.title = title self.unprefixedtitle = False # will be set later self.urltitle = '' self.wikitext = '' self.templates = [] self.links = [] self.categories = [] self.exists = True # If we're not going to check, assume it does self.protection = {} self.namespace = namespace # Things that need to be done before anything else if self.title: self.title = self.title.replace('_', ' ') if self.namespace: if namespace not in self.site.namespaces.keys(): raise BadNamespace(namespace) if self.title: self.unprefixedtitle = self.title self.title = ':'.join( (self.site.namespaces[self.namespace]['*'], self.title.decode('utf8'))) if int(self.namespace) is 0 and self.title: self.namespace = int(self.namespace) self.unprefixedtitle = self.title # Setting page info with API, should set: # pageid, exists, title, unprefixedtitle, namespace if check: self.setPageInfo() else: if self.namespace is False and self.title: self.namespace = namespaceDetect(self.title, self.site) if self.namespace is not 0: nsname = self.site.namespaces[self.namespace]['*'] self.unprefixedtitle = self.title.split(':', 1)[1] self.title = ':'.join((nsname, self.unprefixedtitle)) else: self.unprefixedtitle = self.title if section or sectionnumber is not False: self.setSection(section, sectionnumber) else: self.section = False if title: if not isinstance(self.title, unicode): self.title = unicode(self.title, 'utf-8') if not isinstance(self.unprefixedtitle, unicode): self.unprefixedtitle = unicode(self.unprefixedtitle, 'utf-8') self.urltitle = urllib.quote(self.title.encode('utf-8')).replace( '%20', '_').replace('%2F', '/')
def __init__(self, site, title=False, check=True, followRedir=True, section=False, sectionnumber=False, pageid=False): """ wiki - A wiki object title - The page title, as a string or unicode object check - Checks for existence, normalizes title, required for most things followRedir - follow redirects (check must be true) section - the section name sectionnumber - the section number pageid - pageid, can be in place of title """ if not title and not pageid: raise wiki.WikiError("No title or pageid given") self.site = site if pageid: self.pageid = int(pageid) else: self.pageid = 0 self.followRedir = followRedir self.title = title self.wikitext = '' self.templates = [] self.links = [] self.exists = True # If we're not going to check, assume it does self.protection = {} if check: self.setPageInfo() else: # Guess at some stuff self.namespace = False if self.title: self.title = self.title.replace('_', ' ') bits = self.title.split(':', 1) if len(bits) == 1 or bits[0] == '': self.namespace = 0 else: nsprefix = bits[0].lower( ) # wp:Foo and caTEGory:Foo are normalized by MediaWiki for ns in self.site.namespaces: if nsprefix == self.site.namespaces[ns]['*'].lower(): self.namespace = int(ns) self.title = self.site.namespaces[ns][ '*'] + ':' + bits[1] break else: if self.site.NSaliases: for ns in self.site.NSaliases: if nsprefix == ns.lower(): self.namespace = int( self.site.NSaliases[ns]) self.title = self.site.namespaces[ self.namespace]['*'] + ':' + bits[1] break if not self.namespace: self.namespace = 0 else: self.namespace = 0 if section or sectionnumber is not False: self.setSection(section, sectionnumber) else: self.section = False if title and not isinstance(self.title, unicode): self.title = unicode(self.title, 'utf-8') self.urltitle = urllib.quote(self.title.encode('utf-8')).replace( '%20', '_').replace('%2F', '/') elif title: self.urltitle = urllib.quote(self.title.encode('utf-8')).replace( '%20', '_').replace('%2F', '/') else: self.urltitle = False