jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/434997 )
Change subject: [IMPR] Move main categories to top in cc ......................................................................
[IMPR] Move main categories to top in cc
+ re-enable Personendaten on dewiki + fix nnwiki interwiki comments (there is a comment, but no code, weird) + minor improvements to standardizePageFooter
Bug: T76298 Change-Id: Ie9457953701f98e8c952f289b5a5d91934f2a99b --- M pywikibot/cosmetic_changes.py M pywikibot/textlib.py M tests/cosmetic_changes_tests.py 3 files changed, 81 insertions(+), 36 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py index 7752809..6194ae2 100755 --- a/pywikibot/cosmetic_changes.py +++ b/pywikibot/cosmetic_changes.py @@ -328,26 +328,22 @@ """ Standardize page footer.
- Makes sure that interwiki links and categories are put to the correct - position and into the right order. This combines the old instances - standardizeInterwiki and standardizeCategories. - The page footer has the following section in that sequence: + Makes sure that interwiki links and categories are put + into the correct position and into the right order. This + combines the old instances of standardizeInterwiki + and standardizeCategories. + + The page footer consists of the following parts + in that sequence: 1. categories - 2. ## TODO: template beyond categories ## - 3. additional information depending on local site policy - 4. interwiki links - + 2. additional information depending on the local site policy + 3. interwiki """ - categories = None - interwikiLinks = None + categories = [] + interwiki_links = []
- # Pywikibot is no longer allowed to touch categories on the - # German Wikipedia. See - # https://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/1#Positi... - # ignoring nn-wiki of cause of the comment line above iw section - if not self.template and '{{Personendaten' not in text and \ - '{{SORTIERUNG' not in text and '{{DEFAULTSORT' not in text and \ - self.site.code not in ('et', 'it', 'bg', 'ru'): + # get categories + if not self.template: categories = textlib.getCategoryLinks(text, site=self.site)
if not self.talkpage: @@ -360,26 +356,34 @@ loc = None if loc is not None and loc in self.title: subpage = True - interwikiLinks = textlib.getLanguageLinks( + + # get interwiki + interwiki_links = textlib.getLanguageLinks( text, insite=self.site, template_subpage=subpage)
- # Removing the interwiki + # remove interwiki text = textlib.removeLanguageLinks(text, site=self.site)
- # Adding categories + # add categories, main to top if categories: - # TODO: Sorting categories in alphabetic order. - # e.g. using categories.sort() - - # TODO: Taking main cats to top + # TODO: Sort categories in alphabetic order, e.g. using + # categories.sort()? (T100265) + # TODO: Get main categories from Wikidata? + main = pywikibot.Category(self.site, 'Category:' + self.title, + sortKey=' ') + if main in categories: + categories.pop(categories.index(main)) + categories.insert(0, main) text = textlib.replaceCategoryLinks(text, categories, site=self.site) - # Adding the interwiki - if interwikiLinks: - text = textlib.replaceLanguageLinks(text, interwikiLinks, + + # add interwiki + if interwiki_links: + text = textlib.replaceLanguageLinks(text, interwiki_links, site=self.site, template=self.template, template_subpage=subpage) + return text
def translateAndCapitalizeNamespaces(self, text): diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index 9180a35..a688f59 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -1066,7 +1066,29 @@ newtext = s2.replace(marker, '').strip() + separator + s else: newtext = s2.replace(marker, '') - return newtext + + # special parts above interwiki + above_interwiki = [] + + if site.sitename == 'wikipedia:nn': + comment = re.compile( + r'<!--interwiki \(no(\/nb)?, *sv, *da first; then other languages ' + r'alphabetically by name\)-->') + above_interwiki.append(comment) + + if above_interwiki: + interwiki = _get_regexes(['interwiki'], site)[0] + first_interwiki = interwiki.search(newtext) + for reg in above_interwiki: + special = reg.search(newtext) + if special and not isDisabled(newtext, special.start()): + newtext = (newtext[:special.start()].strip() + + newtext[special.end():]) + newtext = (newtext[:first_interwiki.start()].strip() + + special.group() + '\n' + + newtext[first_interwiki.start():]) + + return newtext.strip()
def interwikiFormat(links, insite=None): @@ -1299,14 +1321,6 @@ marker = findmarker(oldtext) if site is None: site = pywikibot.Site() - if site.sitename == 'wikipedia:de' and '{{Personendaten' in oldtext: - pywikibot.error( - 'The Pywikibot is no longer allowed to touch categories on the ' - 'German\nWikipedia on pages that contain the Personendaten ' - 'template because of the\nnon-standard placement of that template.' - '\nSee https://de.wikipedia.org/wiki/Hilfe:Personendaten' - '#Kopiervorlage') - return oldtext if re.search(r'{{ *(' + r'|'.join(site.getmagicwords('defaultsort')) + r')', oldtext, flags=re.I): separator = config.line_separator @@ -1350,6 +1364,27 @@ langs_removed_text, interwiki, site, addOnly=True) else: newtext = cats_removed_text.replace(marker, '') + + # special parts under categories + under_categories = [] + + if site.sitename == 'wikipedia:de': + personendaten = re.compile(r'{{ *Personendaten.*?}}', + re.I | re.DOTALL) + under_categories.append(personendaten) + + if under_categories: + category = _get_regexes(['category'], site)[0] + for last_category in category.finditer(newtext): + pass + for reg in under_categories: + special = reg.search(newtext) + if special and not isDisabled(newtext, special.start()): + newtext = (newtext[:special.start()].strip() + + newtext[special.end():]) + newtext = (newtext[:last_category.end()].strip() + '\n' * 2 + + special.group() + newtext[last_category.end():]) + return newtext.strip()
diff --git a/tests/cosmetic_changes_tests.py b/tests/cosmetic_changes_tests.py index 59779a6..36b41db 100644 --- a/tests/cosmetic_changes_tests.py +++ b/tests/cosmetic_changes_tests.py @@ -49,6 +49,12 @@ self.assertEqual('Foo\n{{any template}}\n\n[[Category:Foo]]', self.cct.standardizePageFooter( 'Foo\n[[category:foo]]\n{{any template}}')) + self.assertEqual('Foo\n\n[[Category:Test| ]]\n[[Category:Baz]]', + self.cct.standardizePageFooter( + 'Foo\n\n[[category:baz]]\n[[category:test]]')) + self.assertEqual('Foo\n\n[[Category:Foo]]\n\n{{Personendaten}}', + self.cct.standardizePageFooter( + 'Foo\n[[category:foo]]\n{{Personendaten}}'))
def test_resolveHtmlEntities(self): """Test resolveHtmlEntities method."""
pywikibot-commits@lists.wikimedia.org