Discussion:
[MediaWiki-commits] [Gerrit] pywikibot/core[master]: Add an add_only parameter to replaceCategoryInPlace in textlib
Eflyjason (Code Review)
2017-12-27 01:51:27 UTC
Permalink
Eflyjason has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/400264 )

Change subject: Add an add_only parameter to replaceCategoryInPlace in textlib
......................................................................

Add an add_only parameter to replaceCategoryInPlace in textlib

Bug: T183707
Change-Id: I67238337ee82e51ab6c876bbdfe32b05e7836cd2
---
M pywikibot/textlib.py
1 file changed, 11 insertions(+), 1 deletion(-)


git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/64/400264/1

diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index cedb30d..71abed2 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -1164,13 +1164,16 @@
return removeCategoryLinks(text, site, marker)


-def replaceCategoryInPlace(oldtext, oldcat, newcat, site=None):
+def replaceCategoryInPlace(oldtext, oldcat, newcat, site=None,
+ add_only=False):
"""
Replace old category with new one and return the modified text.

@param oldtext: Content of the old category
@param oldcat: pywikibot.Category object of the old category
@param newcat: pywikibot.Category object of the new category
+ @param add_only: If add_only is True, the old category won't
+ be replaced and the category given will be added after it.
@return: the modified text
@rtype: unicode
"""
@@ -1203,6 +1206,13 @@
text = replaceExcept(text, categoryR, '',
['nowiki', 'comment', 'math', 'pre', 'source'],
site=site)
+ elif add_only:
+ text = replaceExcept(oldtext, categoryR,
+ '{0}\n{1}'.format(
+ oldcat.title(asLink=True),
+ newcat.title(asLink=True)),
+ ['nowiki', 'comment', 'math', 'pre', 'source'],
+ site=site)
else:
text = replaceExcept(oldtext, categoryR,
'[[%s:%s\\2' % (site.namespace(14),
--
To view, visit https://gerrit.wikimedia.org/r/400264
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I67238337ee82e51ab6c876bbdfe32b05e7836cd2
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Eflyjason <***@gmail.com>
jenkins-bot (Code Review)
2017-12-31 10:44:10 UTC
Permalink
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/400264 )

Change subject: Add an add_only parameter to replaceCategoryInPlace in textlib
......................................................................


Add an add_only parameter to replaceCategoryInPlace in textlib

Bug: T183707
Change-Id: I67238337ee82e51ab6c876bbdfe32b05e7836cd2
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 20 insertions(+), 1 deletion(-)

Approvals:
Framawiki: Looks good to me, but someone else must approve
jenkins-bot: Verified
Xqt: Looks good to me, approved



diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index cedb30d..71abed2 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -1164,13 +1164,16 @@
return removeCategoryLinks(text, site, marker)


-def replaceCategoryInPlace(oldtext, oldcat, newcat, site=None):
+def replaceCategoryInPlace(oldtext, oldcat, newcat, site=None,
+ add_only=False):
"""
Replace old category with new one and return the modified text.

@param oldtext: Content of the old category
@param oldcat: pywikibot.Category object of the old category
@param newcat: pywikibot.Category object of the new category
+ @param add_only: If add_only is True, the old category won't
+ be replaced and the category given will be added after it.
@return: the modified text
@rtype: unicode
"""
@@ -1203,6 +1206,13 @@
text = replaceExcept(text, categoryR, '',
['nowiki', 'comment', 'math', 'pre', 'source'],
site=site)
+ elif add_only:
+ text = replaceExcept(oldtext, categoryR,
+ '{0}\n{1}'.format(
+ oldcat.title(asLink=True),
+ newcat.title(asLink=True)),
+ ['nowiki', 'comment', 'math', 'pre', 'source'],
+ site=site)
else:
text = replaceExcept(oldtext, categoryR,
'[[%s:%s\\2' % (site.namespace(14),
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index fb77566..476a7ba 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -244,6 +244,15 @@
# First and third categories are the same
self.assertEqual([cats[1], cats[3]], temp_cats)

+ # Testing adding categories
+ temp = textlib.replaceCategoryInPlace(
+ self.old, cats[0], cats[1], site=self.site,
+ add_only=True)
+ self.assertNotEqual(temp, self.old)
+ temp_cats = textlib.getCategoryLinks(temp, site=self.site)
+ self.assertEqual([cats[0], cats[1], cats[1],
+ cats[2], cats[1], cats[3]], temp_cats)
+
new_cats = textlib.getCategoryLinks(new, site=self.site)
self.assertEqual(cats, new_cats)
--
To view, visit https://gerrit.wikimedia.org/r/400264
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I67238337ee82e51ab6c876bbdfe32b05e7836cd2
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Eflyjason <***@gmail.com>
Gerrit-Reviewer: Framawiki <***@tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <***@gmail.com>
Gerrit-Reviewer: Xqt <***@gno.de>
Gerrit-Reviewer: Zoranzoki21 <***@gmail.com>
Gerrit-Reviewer: jenkins-bot <>
Loading...