jenkins-bot has submitted this change and it was merged.

Change subject: Do not remove sortkey using category.py move
......................................................................


Do not remove sortkey using category.py move

Add -keepsortkey param in category.py to explicitly state that sortkey
in the old category should be maintained when moving pages from a
category to another when using "move" action.

Bug: T95804
Change-Id: Icea0c87083c045d35e8ab26db135c39465b77f8d
---
M pywikibot/page.py
M scripts/category.py
2 files changed, 26 insertions(+), 12 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/page.py b/pywikibot/page.py
index 5f58dee..908b5a8 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -1744,6 +1744,7 @@
 
         @param sortKey: sortKey to use for the added category.
             Unused if newCat is None, or if inPlace=True
+            If sortKey=True, the sortKey used for oldCat will be used.
 
         @param inPlace: if True, change categories in place rather than
                       rearranging them.
@@ -1764,9 +1765,6 @@
             if cat not in cats:
                 cats.append(cat)
 
-        if not sortKey:
-            sortKey = oldCat.sortKey
-
         if not self.canBeEdited():
             pywikibot.output(u"Can't edit %s, skipping it..."
                              % self.title(asLink=True))
@@ -1781,17 +1779,21 @@
         if newCat in cats:
             newCat = None
 
+        oldtext = self.text
         if inPlace or self.namespace() == 10:
-            oldtext = self.get(get_redirect=True)
             newtext = textlib.replaceCategoryInPlace(oldtext, oldCat, newCat,
                                                      site=self.site)
         else:
+            old_cat_pos = cats.index(oldCat)
             if newCat:
-                cats[cats.index(oldCat)] = Category(self.site, newCat.title(),
-                                                    sortKey=sortKey)
+                if sortKey is True:
+                    # Fetch sortKey from oldCat in current page.
+                    sortKey = cats[old_cat_pos].sortKey
+                cats[old_cat_pos] = Category(self.site, newCat.title(),
+                                             sortKey=sortKey)
             else:
-                cats.pop(cats.index(oldCat))
-            oldtext = self.get(get_redirect=True)
+                cats.pop(old_cat_pos)
+
             try:
                 newtext = textlib.replaceCategoryLinks(oldtext, cats)
             except ValueError:
diff --git a/scripts/category.py b/scripts/category.py
index dd8fe46..3577ac7 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -47,6 +47,10 @@
  * -mvtogether  - Only move the pages/subcategories of a category, if the
                   target page (and talk page, if -allowsplit is not set)
                   doesn't exist.
+ * -keepsortkey - Use sortKey of the old category also for the new category.
+                  If not specified, sortKey is removed.
+                  An alternative method to keep sortKey is to use -inplace
+                  option.
 
 Options for "tidy" action:
  * -namespaces    Filter the arcitles in the specified namespaces. Separate
@@ -406,7 +410,8 @@
                  inplace=False, move_oldcat=True, delete_oldcat=True,
                  title_regex=None, history=False, pagesonly=False,
                  deletion_comment=DELETION_COMMENT_AUTOMATIC,
-                 wikibase=True, allow_split=False, move_together=False):
+                 wikibase=True, allow_split=False, move_together=False,
+                 keep_sortkey=None):
         """Store all given parameters in the objects attributes.
 
         @param oldcat: The move source.
@@ -460,6 +465,7 @@
         self.wikibase = wikibase and self.site.has_data_repository
         self.allow_split = allow_split
         self.move_together = move_together
+        self.keep_sortkey = keep_sortkey
 
         if not self.can_move_cats:
             repo = self.site.data_repository()
@@ -590,7 +596,8 @@
 
                 page.change_category(self.oldcat, self.newcat,
                                      comment=self.comment,
-                                     inPlace=self.inplace)
+                                     inPlace=self.inplace,
+                                     sortKey=self.keep_sortkey)
 
                 # Categories for templates can be included in <includeonly> 
section
                 # of Template:Page/doc subpage.
@@ -610,7 +617,8 @@
                     doc_page.change_category(self.oldcat, self.newcat,
                                              comment=self.comment,
                                              inPlace=self.inplace,
-                                             include=['includeonly'])
+                                             include=['includeonly'],
+                                             sortKey=self.keep_sortkey)
 
     @staticmethod
     def check_move(name, old_page, new_page):
@@ -1096,6 +1104,7 @@
     rebuild = False
     allow_split = False
     move_together = False
+    keep_sortkey = None
     depth = 5
 
     # Process global args and prepare generator args parser
@@ -1167,6 +1176,8 @@
             withHistory = True
         elif arg.startswith('-depth:'):
             depth = int(arg[len('-depth:'):])
+        elif arg == '-keepsortkey':
+            keep_sortkey = True
         else:
             genFactory.handleArg(arg)
 
@@ -1229,7 +1240,8 @@
                                 deletion_comment=deletion_comment,
                                 wikibase=wikibase,
                                 allow_split=allow_split,
-                                move_together=move_together)
+                                move_together=move_together,
+                                keep_sortkey=keep_sortkey)
     elif action == 'tidy':
         catTitle = pywikibot.input(u'Which category do you want to tidy up?')
         bot = CategoryTidyRobot(catTitle, catDB, genFactory.namespaces)

-- 
To view, visit https://gerrit.wikimedia.org/r/203565
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icea0c87083c045d35e8ab26db135c39465b77f8d
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.w...@gmail.com>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.w...@gmail.com>
Gerrit-Reviewer: Ricordisamoa <ricordisa...@openmailbox.org>
Gerrit-Reviewer: XZise <commodorefabia...@gmx.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
Pywikibot-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to