def getContributorsFromInformationTemplate(raw, title, wikidb): from mwlib.expander import find_template, get_templates, get_template_args, Expander from mwlib import uparser, parser, advtree def getUserLinks(raw): def isUserLink(node): return isinstance(node, parser.NamespaceLink) and node.namespace == 2 # NS_USER result = list(set([ u.target for u in uparser.parseString(title, raw=raw, wikidb=wikidb, ).filter(isUserLink) ])) result.sort() return result def get_authors_from_template_args(template): args = get_template_args(template, expander) author_arg = args.get('Author', None) if author_arg: userlinks = getUserLinks(author_arg) if userlinks: return userlinks node = uparser.parseString('', raw=args['Author'], wikidb=wikidb) advtree.extendClasses(node) txt = node.getAllDisplayText().strip() if txt: return [txt] if args.args: return getUserLinks('\n'.join([args.get(i, u'') for i in range(len(args.args))])) return [] expander = Expander(u'', title, wikidb) template = find_template(raw, 'Information') if template is not None: authors = get_authors_from_template_args(template) if authors: return authors authors = [] for template in get_templates(raw): t = find_template(raw, template) if t is not None: authors.extend(get_authors_from_template_args(t)) if authors: return authors return getUserLinks(raw)
def getContributorsFromInformationTemplate(raw, title, wikidb): from mwlib.expander import find_template, get_templates, get_template_args, Expander from mwlib import uparser, parser, advtree from mwlib.templ.parser import parse def getUserLinks(raw): def isUserLink(node): return isinstance(node, parser.NamespaceLink) and node.namespace == 2 # NS_USER result = list(set([ u.target for u in uparser.parseString(title, raw=raw, wikidb=wikidb, ).filter(isUserLink) ])) result.sort() return result def get_authors_from_template_args(template): args = get_template_args(template, expander) author_arg = args.get('Author', None) if author_arg: # userlinks = getUserLinks(author_arg) # if userlinks: # return userlinks node = uparser.parseString('', raw=args['Author'], wikidb=wikidb) advtree.extendClasses(node) txt = node.getAllDisplayText().strip() if txt: return [txt] if args.args: return getUserLinks('\n'.join([args.get(i, u'') for i in range(len(args.args))])) return [] expander = Expander(u'', title, wikidb) parsed_raw = [parse(raw, replace_tags=expander.replace_tags)] template = find_template(None, 'Information', parsed_raw[:]) if template is not None: authors = get_authors_from_template_args(template) if authors: return authors authors = [] for template in get_templates(raw): t = find_template(None, template, parsed_raw[:]) if t is not None: authors.extend(get_authors_from_template_args(t)) if authors: return authors return getUserLinks(raw)
def getContributorsFromInformationTemplate(raw, title, wikidb): from mwlib.expander import find_template, get_templates, get_template_args, Expander from mwlib import uparser, parser, advtree from mwlib.templ.parser import parse def getUserLinks(raw): def isUserLink(node): return isinstance(node, parser.NamespaceLink) and node.namespace == 2 # NS_USER result = list(set([u.target for u in uparser.parseString(title, raw=raw, wikidb=wikidb).filter(isUserLink)])) result.sort() return result def get_authors_from_template_args(template): args = get_template_args(template, expander) author_arg = args.get("Author", None) if author_arg: # userlinks = getUserLinks(author_arg) # if userlinks: # return userlinks node = uparser.parseString("", raw=args["Author"], wikidb=wikidb) advtree.extendClasses(node) txt = node.getAllDisplayText().strip() if txt: return [txt] if args.args: return getUserLinks("\n".join([args.get(i, u"") for i in range(len(args.args))])) return [] expander = Expander(u"", title, wikidb) parsed_raw = [parse(raw, replace_tags=expander.replace_tags)] template = find_template(None, "Information", parsed_raw[:]) if template is not None: authors = get_authors_from_template_args(template) if authors: return authors authors = [] for template in get_templates(raw): t = find_template(None, template, parsed_raw[:]) if t is not None: authors.extend(get_authors_from_template_args(t)) if authors: return authors return getUserLinks(raw)
def getImageTemplatesAndArgs(self, name, wikidb=None): from mwlib.expander import get_templates, get_template_args page = self.get_image_description_page(name) if page is not None: templates = get_templates(page.rawtext) from mwlib.expander import find_template from mwlib.templ.evaluate import Expander from mwlib.templ.parser import parse from mwlib.templ.misc import DictDB args = set() e = Expander('', wikidb=DictDB()) # avoid parsing with every call to find_template parsed_raw = [parse(page.rawtext, replace_tags=e.replace_tags)] for t in templates: tmpl = find_template(None, t, parsed_raw[:]) arg_list = tmpl[1] for arg in arg_list: if isinstance(arg, basestring) and len(arg) > 3 and ' ' not in arg: args.add(arg) templates.update(args) return templates return []
def getContributors(self, name, wikidb=None): """Return list of image contributors @param name: image name without namespace (e.g. without "Image:") @type name: unicode @param wikidb: WikiDB instance (optional) @type wikidb: object @returns: list of contributors @rtype: [unicode] or None """ desc_url = self.getDescriptionURL(name) if desc_url is None: return None # Note: We're always guessing the API helper b/c we'll get problems when # fetching from en.wp if we should've used commons.wikimedia.org instead. # A passed wikidb is only used as a fallback here. api_helper = get_api_helper(desc_url) if api_helper is None: if wikidb is None: return None else: wikidb = WikiDB(api_helper=api_helper) title = 'Image:%s' % name raw = wikidb.getRawArticle(title) if not raw: return None expander = Expander(u'', title, wikidb) def getUserLinks(raw): def isUserLink(node): return isinstance(node, parser.NamespaceLink) and node.namespace == namespace.NS_USER result = list(set([ u.target for u in uparser.parseString(title, raw=raw, wikidb=wikidb, ).filter(isUserLink) ])) result.sort() return result template = find_template(raw, 'Information') if template is not None: author = get_template_args(template, expander).get('Author', '').strip() if author: users = getUserLinks(author) if users: users = list(set(users)) users.sort() return users node = uparser.parseString('', raw=author, wikidb=wikidb) advtree.extendClasses(node) return [node.getAllDisplayText()] users = getUserLinks(raw) if users: return users return wikidb.getAuthors(title)