Exemplo n.º 1
0
def handle(site, page):
	result = site.complete(titles=page, generator='links', redirects=True, prop='revisions', rvprop='content', gpllimit='max')

	classes_per_title = {}

	for bsp in result['pages'].values():
		classes = []
		if not 'missing' in bsp:
			bspcode = mwbot.parse(bsp['revisions'][0]['*'])
			if bspcode.filter_templates(matches=lambda t: t.name.matches('ungelöst')):
				continue

			templates = bspcode.filter_templates(matches=lambda t: t.name.matches('Beispiel'))
			if len(templates):
				tpl = templates[0]

				if tpl.has('teils') and tpl.get('teils').value.strip() != '':
					classes.append('beispiel-teils')

				status = tpl.get('status').value.strip().lower() if tpl.has('status') else None

				if status in ('datei', 'extern'):
					classes.append('beispiel-{}'.format(status))
				elif tpl.has('1') and tpl.get('1').value.strip() != '':
					classes.append('beispiel-wikicode')
		if classes:
			classes_per_title[bsp['title']] = classes
			if 'redirects' in result:
				for r in result['redirects']:
					if r['to'] == bsp['title']:
						classes_per_title[r['from']] = classes
						if not bsp['title'].startswith(page):
							classes_per_title[r['from']].append('beispiel-redirect')

	idxpage = next(site.results(prop='revisions', titles=page, rvprop='content'))
	code = mwbot.parse(idxpage['revisions'][0]['*'])

	for table in code.filter_tags(matches=lambda t: t.tag.matches('table')):
		table.attributes.clear()
		table.attributes.append(' class=beispiel-table')
		for td in table.contents.filter_tags(matches=lambda t: t.tag.matches('td')):
			links = td.contents.filter_wikilinks()
			if len(links) == 1:
				if links[0].title.startswith('/'):
					td.attributes.clear()
					title = page + str(links[0].title)
					if title in classes_per_title:
						td.attributes.append('class="{}"'.format(' '.join(classes_per_title[title])))
				else:
					logging.info('found non-relative link')
			else:
				logging.info('found cell with {} links'.format(len(links)))

	mwbot.save(site, page, idxpage['revisions'][0]['*'], str(code), site.msg('update'))
Exemplo n.º 2
0
def check_category(tplclass, *additional_classes):
    errors = collections.defaultdict(list)
    kwargs = {}
    if hasattr(tplclass, 'NAMESPACES'):
        kwargs['gcmnamespace'] = mwbot.join(tplclass.NAMESPACES)
    for page in catmembers_with_revs(tplclass.CATEGORY, **kwargs):
        code = mwbot.parse(page['revisions'][0]['*'])
        for error in check_template(code, tplclass):
            errors[page['title']].append(error)
        for ac in additional_classes:
            for error in check_template(code, ac):
                errors[page['title']].append(error)
    return generate_section(tplclass.CATEGORY, errors)
Exemplo n.º 3
0
def handle_page(page):
    before = page['revisions'][0]['*']
    code = mwbot.parse(before)
    templates = code.filter_templates(matches=lambda t: t.name.matches('Room'))
    rcode = page['title'].split(':')[-1]
    if templates and rcode in rooms:
        msg = handle_template(templates[0], rooms[rcode], page['ns'])
        mwbot.save(site,
                   page['title'],
                   before,
                   str(code),
                   site.msg(msg),
                   strip_consec_nl=True)
Exemplo n.º 4
0
def handle_page(page):
    before = page['revisions'][0]['*']
    code = mwbot.parse(before)
    templates = code.filter_templates(
        matches=lambda t: t.name.matches('LVA-Daten'))
    if templates:
        msg = handle_template(templates[0], code, page['ns'])
        mwbot.save(site,
                   page['title'],
                   before,
                   str(code),
                   site.msg(msg),
                   strip_consec_nl=True)
Exemplo n.º 5
0
def handle_page(page):
    before = page['revisions'][0]['*']
    code = mwbot.parse(before)
    templates = code.filter_templates(
        matches=lambda t: t.name.matches('RaumCode'))
    if templates:
        msg = handle_template(templates[0], code, page['ns'])
        mwbot.save(site,
                   page['title'],
                   before,
                   str(code).replace('[[Kategorie:Raumcode]]', ''),
                   site.msg(msg),
                   strip_consec_nl=True)
Exemplo n.º 6
0
def handle_page(page):
    orig = page['revisions'][0]['*']
    code = mwbot.parse(orig)
    for div in code.ifilter_tags(matches=lambda x: x.tag == 'div'):
        code.replace(div, div.contents)
    new = str(code).strip()
    if not code.filter_templates(
            matches=lambda x: x.name.matches('Baustein footer')):
        new += '\n{{Baustein footer}}'
    if not code.filter_headings():
        new = '====== ' + page['title'].split(':')[1] + ' ======\n' + new
    mwbot.save(site, page['title'], orig, new,
               'fixe Textbaustein (bausteien_fixer.py)')
    if mwbot.santitle(page['title'].split(':')[1]) not in templates:
        undocumented_templates.append(page['title'].split(':')[1])
Exemplo n.º 7
0
#!/usr/bin/env python3
import mwbot
import vowi

if __name__ == '__main__':
    parser = mwbot.get_argparser()
    parser.add_argument('page')
    args = parser.parse_args()

    site = mwbot.getsite('bsp_convert_legacy.py', args)

    index = next(
        site.results(titles=args.page, prop='revisions', rvprop='content'))
    code = mwbot.parse(index['revisions'][0]['*'])
    moves = []

    for iw in code.ifilter_wikilinks():
        if not str(iw.title).startswith('/') and '/Übungen' in str(iw.title):
            moves.append(
                (str(iw.title), '/'.join(index['title'].split('/')[:2]) +
                 '/Beispiel ' + str(iw.text)))

    mwbot.moves(
        site, moves, 'verschiebe zu semester {}'.format(
            index['title'].split('/')[1].split()[1]))
Exemplo n.º 8
0
               'fixe Textbaustein (bausteien_fixer.py)')
    if mwbot.santitle(page['title'].split(':')[1]) not in templates:
        undocumented_templates.append(page['title'].split(':')[1])


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    overview = 'Hilfe:Vorlagen für Beispiele'
    mwbot.add_pagesel_args(parser,
                           categorydefault='Textbausteine für Beispiele')
    args = parser.parse_args()
    site = mwbot.getsite()

    page = next(
        site.query('pages',
                   prop='revisions',
                   titles=overview,
                   rvprop='content'))
    orig = page['revisions'][0]['*']
    code = mwbot.parse(orig)
    templates = [mwbot.santitle(t.name) for t in code.ifilter_templates()]
    undocumented_templates = []

    mwbot.handle_pagesel_args(site, args, (mwapi.NS_TEMPLATE, ), handle_page)
    undocumented_templates.sort()
    new = orig
    for t in undocumented_templates:
        new += '\n{{%s}}' % t
    mwbot.save(site, overview, orig, new,
               '+nichteingeordnete Vorlagen (baustein_fixer.py)')
Exemplo n.º 9
0
def handle(site, index):
    src_ns = next(site.results(prop='info', titles=index))['ns']

    for page in site.results(generator='allpages',
                             gapprefix=index.split(':')[1] + '/Beispiel ',
                             gaplimit='max',
                             prop='revisions',
                             rvprop='content',
                             gapnamespace=src_ns):
        orig = page['revisions'][0]['*']
        if mwbot.parse_redirect(orig):
            continue

        code = mwbot.parse(orig)
        templates = code.filter_templates(
            matches=lambda x: x.name.matches('Beispiel'))
        if len(templates) > 0:
            template = templates[0]
        else:
            template = Template(Wikicode([Text('Beispiel')]))
            code.insert(0, template)
            code.insert(1, '\n')

        # legacy format handling
        template.name = 'Beispiel'
        if template.has('1') and not template.get('1').value.startswith('\n'):
            template.get('1').value.insert(0, '\n')
        if template.has('status'):
            if str(template.get('status').value).strip() not in ('extern',
                                                                 'Datei'):
                print('unknown status: {}'.format(k))

        if template.has('1'):
            if str(template.get('1').value).strip() == 'teils':
                template.add('teils', '')
                template.remove('1')
            elif str(template.get('1').value).strip() == 'falsch':
                template.add('falsch', '')
                template.remove('1')

        if not template.has('1'):
            angabe_div = code.filter_tags(
                matches=lambda x: x.tag.matches('blockquote') or len([
                    x for x in x.attributes
                    if '{{Angabe}}' in x or '#EFEFEF' in x
                ]))
            if angabe_div:
                template.add('1',
                             '\n' + str(angabe_div[0].contents).strip() + '\n',
                             showkey=True)
                code.remove(angabe_div[0])
            else:
                angabe_sec = code.get_sections(matches='Angabe|Aufgabe')
                if angabe_sec:
                    code.remove(angabe_sec[0].nodes[0])
                    template.add('1',
                                 '\n' + str(angabe_sec[0]).strip() + '\n',
                                 showkey=True)
                    code.replace(angabe_sec[0], '\n')

        mwbot.save(site,
                   page['title'],
                   orig,
                   str(code),
                   'beispiel_fixer.py',
                   strip_consec_nl=True)