jenkins-bot has submitted this change and it was merged.
Change subject: Category.py script handles empty and redirect pages
......................................................................
Category.py script handles empty and redirect pages
This patch fixes three problems: First if the to-be-edited page is empty
it was skipped even when "-create" is set, because the actual content
("", an empty string) was never returned. The same problem was there
when the to-be-edited page is a redirect, but that doesn't fix all
problems there.
The second part only affects adding categories to redirect targets.
If the same fix is applied that "-redirect" actually returns the
target contents it still crash because it's unable to save the page.
This is due to the fact that it only checks if the currently edited
page is a redirect when it's loading the content but nowhere else.
The fix there is simply to change the Page instance from the redirect
page to the redirect target page (if "-redirect" is given) and skip it
otherwise.
And lastly when creating a page, it can't of course get the text
afterwards to show the difference. So the third part of that changeset
is using the cached content from the beginning to show the difference.
Bug: 69185
Bug: 69186
Change-Id: I4bfd4db128e8398df43f847cc3cbd8143088686d
---
M scripts/category.py
1 file changed, 23 insertions(+), 14 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/category.py b/scripts/category.py
index 38dddc9..d5f5492 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -294,7 +294,9 @@
pywikibot.output(u"%d page(s) processed." % counter)
def load(self, page):
- """Load the given page, do some changes, and save
it."""
+ """Load the given page's content.
+
+ If page doesn't exists returns an empty string."""
try:
# Load the page
text = page.get()
@@ -302,26 +304,20 @@
if self.create:
pywikibot.output(u"Page %s doesn't exist yet; creating."
% (page.title(asLink=True)))
- text = ''
+ return ''
else:
pywikibot.output(u"Page %s does not exist; skipping."
% page.title(asLink=True))
- except pywikibot.IsRedirectPage:
- redirTarget = page.getRedirectTarget()
- if self.follow_redirects:
- text = redirTarget.get()
- else:
- pywikibot.warning(u"Page %s is a redirect to %s; skipping."
- % (page.title(asLink=True),
- redirTarget.title(asLink=True)))
else:
return text
- def save(self, text, page, newcatTitle, minorEdit=True, botflag=True):
+ def save(self, text, page, newcatTitle, minorEdit=True, botflag=True,
old_text=None):
+ if old_text is None:
+ old_text = self.load(page)
# only save if something was changed
- if text != page.get():
+ if text != old_text:
# show what was changed
- pywikibot.showDiff(page.get(), text)
+ pywikibot.showDiff(old_text, text)
comment = self.editSummary
if not comment:
comment = i18n.twtranslate(page.site, 'category-adding',
@@ -364,9 +360,22 @@
return False
def treat(self, page):
+ if page.isRedirectPage():
+ # if it's a redirect use the redirect target instead
+ redirTarget = page.getRedirectTarget()
+ if self.follow_redirects:
+ page = redirTarget
+ else:
+ pywikibot.warning(u"Page %s is a redirect to %s; skipping."
+ % (page.title(asLink=True),
+ redirTarget.title(asLink=True)))
+ # loading it will throw an error if we don't jump out before
+ return
text = self.load(page)
if text is None:
return
+ # store old text, so we don't have reload it every time
+ old_text = text
cats = [c for c in page.categories()]
# Show the title of the page we're working on.
# Highlight the title in purple.
@@ -389,7 +398,7 @@
pywikibot.output(u'Adding %s' % catpl.title(asLink=True))
cats.append(catpl)
text = pywikibot.replaceCategoryLinks(text, cats, site=page.site)
- if not self.save(text, page, newcatTitle):
+ if not self.save(text, page, newcatTitle, old_text=old_text):
pywikibot.output(u'Page %s not saved.'
% page.title(asLink=True))
--
To view, visit
https://gerrit.wikimedia.org/r/152107
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4bfd4db128e8398df43f847cc3cbd8143088686d
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>