Exemplo n.º 1
0
 def test_edit_anonymous_parameter2(self):
     # Check that an anomyous parameter can be edited, preserving declaration
     text = 'Lorem ipsum {{lang|1=fr}} amet'
     dp = TemplateEditor(text)
     dp.templates['lang'][0].parameters[1] = 'no'
     tmp = text.replace('fr', 'no')
     self.assertEqual(dp.wikitext(), tmp)
 def test_edit_parameter_using_value_that_needs_xml_escape(self):
     # Check that "&" in values are properly handled
     text = 'Lorem ipsum {{kilde www}} amet'
     result = 'Lorem ipsum {{kilde www| url=http://www.skulabladid.fo/fo-FO/Tíðindasavn/Eldri-tíðindi.aspx?PID=30&M=NewsV2&Action=1&NewsId=3201&currentPage=83 }} amet'
     dp = TemplateEditor(text)
     dp.templates['kilde www'][0].parameters['url'] = 'http://www.skulabladid.fo/fo-FO/Tíðindasavn/Eldri-tíðindi.aspx?PID=30&M=NewsV2&Action=1&NewsId=3201&currentPage=83'
     self.assertEqual(dp.wikitext(), result)
Exemplo n.º 3
0
 def test_add_parameter3(self):
     # Check that a parameter can be added, and that an existing whitespace pattern is followed
     text = 'Lorem ipsum {{ Infoboks A | a = 2 }} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 }} dolor sit amet'
     dp = TemplateEditor(text)
     dp.templates['infoboks A'][0].parameters['b'] = '3'
     self.assertEqual(dp.wikitext(), text2)
Exemplo n.º 4
0
 def test_add_parameter2(self):
     # Check that a parameter can be added, and that an existing whitespace pattern is followed
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n| dato = TEST \n}} dolor sit amet'
     dp = TemplateEditor(text)
     dp.templates['infoboks A'][0].parameters['dato'] = 'TEST'
     self.assertEqual(dp.wikitext(), text2)
Exemplo n.º 5
0
    def add(self, name, fields):
        """Add a service.

        Args:
            name (string): The name of the service.
            fields (dict): The service fields to add.

        Returns:
            :obj:`enum 'ServiceErrorCode'`: A ServiceErrorCode enum for cases
                such as if the service already exists, etc.
            dict: the service information if fetch_bpmn_digital_steps is None,
        """
        page, page_exists = self._page(name)
        if page_exists:
            result = ErrorCode.ALREADY_EXISTS
        elif not page.can('edit'):
            result = ErrorCode.UNAUTHORIZED_ACTION
        else:
            templates_text = ''
            for tpl_name, tpl_instances in fields.items():
                for tpl_instance in tpl_instances:
                    templates_text += _template_text(tpl_name, tpl_instance)
            te = TemplateEditor(templates_text)
            if te.templates:
                page.edit(te.wikitext())
                page_name = page.page_title
                page_full_name = page.name
                result = self._service_dict(page_name, page_full_name, te)
            else:
                result = ErrorCode.INVALID_TEMPLATE
        return result
Exemplo n.º 6
0
 def test_delete_template(self):
     # Check that a parameter can be deleted
     text = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 | c = 4 }} dolor sit{{lang|fr}} amet'
     text2 = 'Lorem ipsum dolor sit{{lang|fr}} amet'
     dp = TemplateEditor(text)
     dp.templates['Infoboks A'][0].remove()  # Note that 'del dp.templates['Infoboks A'][0]' currently does not work
     self.assertEqual(dp.wikitext(), text2)
Exemplo n.º 7
0
 def test_alter_anonymous_param1(self):
     # Check that an anomyous parameter can be edited correctly
     text = 'Lorem ipsum {{lang|fr}} amet'
     dp = TemplateEditor(text)
     dp.templates['lang'][0].parameters[1] = 'no'
     tmp = text.replace('fr', 'no')
     self.assertEqual(dp.wikitext(), tmp)
Exemplo n.º 8
0
 def test_change_template_name(self):
     # Check that a template can be renamed, with whitespace preserved
     text = 'Lorem ipsum {{ Infoboks A\n| maks=2\n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.name = 'Mal B'
     tmp = text.replace('Infoboks A', 'Mal B')
     self.assertEqual(dp.wikitext(), tmp)
Exemplo n.º 9
0
def check_randompage(pagename):
    logger.debug('Page: %s', pagename)
    page = site.pages[pagename]
    inputtxt = page.edit(readonly=True)
    dp = TemplateEditor(inputtxt)
    outputtxt = dp.wikitext()

    assert inputtxt == outputtxt
Exemplo n.º 10
0
 def test_edit_empty_parameter_with_linebreak(self):
     # Check that an empty parameter can be edited, with linebreak preserved
     text = 'Lorem ipsum {{ Infoboks A \n| date= \n| title= \n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['date'] = 'test'
     tmp = text.replace('date= ', 'date= test')
     self.assertEqual(dp.wikitext(), tmp)
Exemplo n.º 11
0
 def test_edit_nonempty_parameter_alternative(self):
     # Check that a parameter can be renamed using 'parameter.value='
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['maks'].value = '3'
     tmp = text.replace('2', '3')
     self.assertEqual(dp.wikitext(), tmp)
Exemplo n.º 12
0
 def test_delete_parameter_alternative(self):
     # Check that a parameter can be deleted
     text = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 | c = 4 }} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A | a = 2 | c = 4 }} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters.remove('b')
     self.assertEqual(dp.wikitext(), text2)
Exemplo n.º 13
0
 def test_delete_parameter(self):
     # Check that a parameter can be renamed, with whitespace preserved
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A\n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     del templ.parameters['maks']
     self.assertEqual(dp.wikitext(), text2)
Exemplo n.º 14
0
 def test_edit_parameter_using_integer_alternative(self):
     # Check that a parameter can be renamed, with whitespace preserved
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['maks'].edit(3)
     tmp = text.replace('2', '3')
     self.assertEqual(dp.wikitext(), tmp)
Exemplo n.º 15
0
 def test_add_parameter3(self):
     # Check that a parameter can be added, when there's no existing parameters
     text = 'Lorem ipsum {{Infoboks A}} dolor sit amet'
     text2 = 'Lorem ipsum {{Infoboks A| a=2 | b=3 }} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['a'] = '2'
     templ.parameters['b'] = '3'
     self.assertEqual(dp.wikitext(), text2)
Exemplo n.º 16
0
    def __init__(self, page, interactive_mode):

        logger.info('Checking page: %s', page.name)

        self.checked = 0
        self.modified = []
        self.unresolved = []

        # te = page.text()
        txt = page.text()

        # Due to <https://github.com/danmichaelo/mwtemplates/issues/3>
        if re.search('<nowiki ?/>', txt, re.I) is not None:
            return

        te = TemplateEditor(txt)

        modified = False
        for k, v in te.templates.iteritems():
            if k in ['Kilde www', 'Kilde bok', 'Kilde artikkel', 'Kilde avhandling', 'Kilde avis', 'Cite web', 'Citeweb', 'Cite news', 'Cite journal', 'Cite book', 'Tidningsref', 'Webbref', 'Bokref']:
                for tpl in v:
                    t = Template(tpl, interactive_mode)
                    self.checked += t.checked
                    self.modified.extend(t.modified)
                    self.unresolved.extend(t.unresolved)

        for u in self.unresolved:
            u['page'] = page.name

        if len(self.modified) != 0:
            if len(self.modified) == 1:
                summary = 'CS1-kompatible datoer: %s' % (self.format_entry(self.modified[0]))
            elif len(self.modified) == 2:
                summary = 'CS1-kompatible datoer: %s, %s' % (self.format_entry(self.modified[0]), self.format_entry(self.modified[1]))
            else:
                summary = 'CS1-kompatible datoer: Fikset %d datoer' % (len(self.modified))

            logger.info('Saving %d fixed date(s)', len(self.modified))
            time.sleep(1)

            try:
                res = page.save(te.wikitext(), summary=summary)
            except mwclient.errors.ProtectedPageError:
                logger.error('ERROR: Page protected, could not save')

            if res.get('newrevid') is not None:
                with codecs.open('modified.txt', 'a', 'utf8') as f:
                    for x in self.modified:
                        ti = quote(page.name.replace(' ', '_').encode('utf8'))
                        difflink = '//no.wikipedia.org/w/index.php?title=%s&diff=%s&oldid=%s' % (ti, res['newrevid'], res['oldrevid'])
                        f.write('| [[%s]] ([%s diff]) || Endret %s fra %s til %s || %s\n|-\n' % (page.name, difflink, x['key'], x['old'], x['new'], 'kompleks' if x['complex'] else ''))

                with codecs.open('modified-simple.txt', 'a', 'utf8') as f:
                    for x in self.modified:
                        ti = quote(page.name.replace(' ', '_').encode('utf8'))
                        f.write('%s\t%s\t%s\n' % (page.name, x['old'], x['new']))
Exemplo n.º 17
0
 def test_nowiki_roundtrip(self):
     # Check that <nowiki/> is not changed to <nowiki>
     text = 'Lorem ipsum <nowiki/> dolor sit amet'
     dp = TemplateEditor(text)
     self.assertEqual(dp.wikitext(), text)
Exemplo n.º 18
0
 def test_blank_template_roundtrip(self):
     # Check that a parameter can be deleted
     text = 'Lorem ipsum {{ }} dolor sit amet'
     dp = TemplateEditor(text)
     self.assertEqual(dp.wikitext(), text)
Exemplo n.º 19
0
    def update(self, name, fields):
        """Update a service.

        Args:
            name (string): The name of the service.
            fields (dict): The service fields to update.

        Returns:
            :obj:`enum 'ServiceErrorCode'`: A ServiceErrorCode enum for cases
                such as if the service was not found, etc.
            dict: the service information if fetch_bpmn_digital_steps is None,
        """
        page, page_exists = self._page(name)
        if not page.can('edit'):
            result = ErrorCode.UNAUTHORIZED_ACTION
        elif page_exists:
            te = TemplateEditor(page.text())
            fields_updated = False
            for tpl_name, tpl_instances in fields.items():
                template_names = [tpl.lower() for tpl in te.templates.keys()]
                if tpl_name in template_names:
                    page_tpl_instances = te.templates[tpl_name]
                    # Update template instances
                    for instance_num_str, tpl_instance in \
                            tpl_instances.items():
                        if not instance_num_str.isdigit():
                            break
                        instance_num = int(instance_num_str)
                        try:
                            page_tpl = page_tpl_instances[instance_num - 1]
                        except IndexError:
                            # Template instance does not exist, create it
                            if instance_num > len(page_tpl_instances):
                                # Only if numbering is greater than the last
                                # existing instance number
                                new_tpl_text = _template_text(
                                    tpl_name, tpl_instance)
                                if new_tpl_text:
                                    new_wiki_text = ''
                                    for name, tpls in te.templates.items():
                                        for tpl in tpls:
                                            new_wiki_text += f'{str(tpl)}\n'
                                        if name.lower() == tpl_name:
                                            new_wiki_text += \
                                                f'{new_tpl_text}'
                                    if new_wiki_text:
                                        te = TemplateEditor(new_wiki_text)
                                        if not fields_updated:
                                            fields_updated = True
                        else:
                            for field_name, field_value in \
                                    tpl_instance.items():
                                # Update or create field
                                page_tpl.parameters[field_name] = \
                                    field_value
                                if not fields_updated:
                                    fields_updated = True
                else:
                    new_templates_text = ''
                    for instance_num_str, tpl_instance in \
                            tpl_instances.items():
                        new_templates_text += _template_text(
                            tpl_name, tpl_instance)
                    if new_templates_text:
                        # Append new templates to wiki text
                        new_wiki_text = \
                            f'{te.wikitext()}\n{new_templates_text}'
                        te = TemplateEditor(new_wiki_text)
                        if not fields_updated:
                            fields_updated = True
            if fields_updated:
                wikitext = te.wikitext().replace('\n\n', '\n')
                if wikitext[0] == '\n':
                    wikitext = wikitext[1:]
                page.edit(wikitext)
                page_name = page.page_title
                page_full_name = page.name
                result = self._service_dict(page_name, page_full_name,
                                            TemplateEditor(page.text()))
            else:
                result = ErrorCode.NO_FIELD_UPDATED
        else:
            result = ErrorCode.NOT_FOUND
        return result
Exemplo n.º 20
0
 def test_arabic(self):
     # Check handling of multibyte chars, unicode level 2
     text = 'Lorem ipsum {{Infoboks العربية}} dolor sit amet'
     dp = TemplateEditor(text)
     self.assertEqual(text, dp.wikitext())
Exemplo n.º 21
0
 def test_onlywhitespace(self):
     # Check that the parser doesn't strip off empty parameters:
     text = '{{some template |   | someval=true}}'
     dp = TemplateEditor(text)
     self.assertEqual(text, dp.wikitext())
Exemplo n.º 22
0
 def test_edit_nowiki(self):
     text = '<nowiki>{{flagg|no}}</nowiki>{{flagg|%s}}'
     dp = TemplateEditor(text % 'no')
     dp.templates['flagg'][0].parameters[1] = 'en'
     self.assertEqual(dp.wikitext(), text % 'en')
Exemplo n.º 23
0
 def test_nested_templates(self):
     text = 'Lorem{{templ1|arg={{templ2|20}}}}'
     dp = TemplateEditor(text)
     self.assertEqual(text, dp.wikitext())