Revision: 4275
Author: wikipedian
Date: 2007-09-13 18:15:18 +0000 (Thu, 13 Sep 2007)
Log Message:
-----------
Page.getRedirectTarget() now returns a Page object, no longer a string.
Modified Paths:
--------------
trunk/pywikipedia/casechecker.py
trunk/pywikipedia/category.py
trunk/pywikipedia/disambredir.py
trunk/pywikipedia/editarticle.py
trunk/pywikipedia/featured.py
trunk/pywikipedia/makecat.py
trunk/pywikipedia/redirect.py
trunk/pywikipedia/solve_disambiguation.py
trunk/pywikipedia/us-states.py
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/casechecker.py
===================================================================
--- trunk/pywikipedia/casechecker.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/casechecker.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python
# -*- coding: utf-8 -*-
""" Script to enumerate all pages on the wiki and find all titles
with mixed latin and cyrilic alphabets.
@@ -403,7 +403,7 @@
if not dst.exists():
pagesDontExist.append(newTitle)
elif dst.isRedirectPage():
- pagesRedir[newTitle] = dst.getRedirectTarget()
+ pagesRedir[newTitle] = dst.getRedirectTarget().title()
else:
pagesExist.append(newTitle)
Modified: trunk/pywikipedia/category.py
===================================================================
--- trunk/pywikipedia/category.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/category.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -276,7 +276,7 @@
except wikipedia.NoPage:
wikipedia.output(u'%s could not be loaded from the server.' % listpage.aslink())
except wikipedia.IsRedirectPage:
- wikipedia.output(u'%s is a redirect to %s.' % (listpage.aslink(), listpage.getRedirectTarget()))
+ wikipedia.output(u'%s is a redirect to %s.' % (listpage.aslink(), listpage.getRedirectTarget().aslink()))
else:
referredPage = wikipedia.input(u'Wikipedia page that is now linked to:')
page = wikipedia.Page(wikipedia.getSite(), referredPage)
Modified: trunk/pywikipedia/disambredir.py
===================================================================
--- trunk/pywikipedia/disambredir.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/disambredir.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -109,7 +109,7 @@
wikipedia.getall(mysite,links)
for page2 in links:
try:
- target = wikipedia.Page(mysite,page2.getRedirectTarget())
+ target = page2.getRedirectTarget()
except (wikipedia.Error,wikipedia.SectionError):
continue
text = treat(text, page2, target)
Modified: trunk/pywikipedia/editarticle.py
===================================================================
--- trunk/pywikipedia/editarticle.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/editarticle.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -143,7 +143,7 @@
pageTitle = self.options.page or wikipedia.input(u"Page to edit:")
self.page = wikipedia.Page(site, pageTitle)
if not self.options.edit_redirect and self.page.isRedirectPage():
- self.page = wikipedia.Page(site, self.page.getRedirectTarget())
+ self.page = self.page.getRedirectTarget()
def handle_edit_conflict(self):
fn = os.path.join(tempfile.gettempdir(), self.page.title())
Modified: trunk/pywikipedia/featured.py
===================================================================
--- trunk/pywikipedia/featured.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/featured.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -166,7 +166,7 @@
oursite=wikipedia.getSite()
wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"% page.title())
if page.isRedirectPage():
- page=wikipedia.Page(page.site(), page.getRedirectTarget())
+ page = page.getRedirectTarget()
try:
iw=page.interwiki()
except:
@@ -184,7 +184,7 @@
wikipedia.output(u"Our page doesn't exist: "+ourpage.title())
return None
if ourpage.isRedirectPage():
- ourpage=wikipedia.Page(ourpage.site(),ourpage.getRedirectTarget())
+ ourpage = ourpage.getRedirectTarget()
wikipedia.output(u"Corresponding page is "+ourpage.title())
if ourpage.namespace() != 0:
wikipedia.output(u"...not in the main namespace, skipping")
@@ -211,7 +211,7 @@
# everything is ok
return ourpage
if backpage.isRedirectPage():
- backpage=wikipedia.Page(backpage.site(),backpage.getRedirectTarget())
+ backpage = backpage.getRedirectTarget()
if backpage==page:
# everything is ok
return ourpage
Modified: trunk/pywikipedia/makecat.py
===================================================================
--- trunk/pywikipedia/makecat.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/makecat.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -120,7 +120,7 @@
if pl.site() != mysite:
return
if pl.isRedirectPage():
- pl2 = wikipedia.Page(mysite,pl.getRedirectTarget())
+ pl2 = pl.getRedirectTarget()
if needcheck(pl2):
tocheck.append(pl2)
checked[pl2]=pl2
Modified: trunk/pywikipedia/redirect.py
===================================================================
--- trunk/pywikipedia/redirect.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/redirect.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -206,15 +206,14 @@
# Highlight the title in purple.
wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir_page.title())
try:
- target_name = redir_page.getRedirectTarget()
+ targetPage = redir_page.getRedirectTarget()
except wikipedia.IsNotRedirectPage:
wikipedia.output(u'%s is not a redirect.' % redir_page.title())
except wikipedia.NoPage:
wikipedia.output(u'%s doesn\'t exist.' % redir_page.title())
else:
try:
- target_page = wikipedia.Page(wikipedia.getSite(), target_name)
- target_page.get()
+ targetPage.get()
except wikipedia.NoPage:
redir_page.delete(reason, prompt = False)
except wikipedia.IsRedirectPage:
@@ -233,28 +232,28 @@
# Highlight the title in purple.
wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir.title())
try:
- target = redir.getRedirectTarget()
+ secondRedir = redir.getRedirectTarget()
except wikipedia.IsNotRedirectPage:
wikipedia.output(u'%s is not a redirect.' % redir.aslink())
except wikipedia.NoPage:
wikipedia.output(u'%s doesn\'t exist.' % redir.aslink())
else:
try:
- second_redir = wikipedia.Page(mysite, target)
- second_target = second_redir.getRedirectTarget()
- anchor = re.search(u'#(.*)$', target)
- if anchor and not u'#' in second_target:
- second_target += u'#' + anchor.group(1)
+ secondTargetPage = secondRedir.getRedirectTarget()
+ anchorMatch = re.search(u'#(?P<section>.*)$', target)
+ if anchorMatch and not u'#' in secondTargetPage.title():
+ secondTarget = wikipedia.Page(mysite, '%s#%s' % (secondTargetPage.sectionFreeTitle(), anchorMatch.group('section')))
except wikipedia.SectionError:
- wikipedia.output(u'Warning: Redirect target section %s doesn\'t exist.' % second_redir.aslink())
+ wikipedia.output(u'Warning: Redirect target section %s doesn\'t exist.' % secondRedir.aslink())
except wikipedia.IsNotRedirectPage:
- wikipedia.output(u'Redirect target %s is not a redirect.' % second_redir.aslink())
+ wikipedia.output(u'Redirect target %s is not a redirect.' % secondRedir.aslink())
except wikipedia.NoPage:
- wikipedia.output(u'Redirect target %s doesn\'t exist.' % second_redir.aslink())
+ wikipedia.output(u'Redirect target %s doesn\'t exist.' % secondRedir.aslink())
else:
- wikipedia.output(u'%s is a redirect to %s, which is a redirect to [[%s]]. Fixing...' % (redir.aslink(), second_redir.aslink(), second_target))
- # TODO: make this case-insensitive
- txt = redir.get(get_redirect=True).replace('[['+target,'[['+second_target)
+ wikipedia.output(u'%s is a redirect to %s, which is a redirect to %s. Fixing...' % (redir.aslink(), secondRedir.aslink(), secondTargetPage.aslink()))
+ txt = mysite.redirectRegex().sub('#REDIRECT [[%s]]')
+ txt = redir.get(get_redirect=True).replace('[['+target,'[['+secondTargetPage.title())
+ wikipedia.showDiff(redir.get(get_redirect=True), txt)
try:
redir.put(txt)
except wikipedia.LockedPage:
Modified: trunk/pywikipedia/solve_disambiguation.py
===================================================================
--- trunk/pywikipedia/solve_disambiguation.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/solve_disambiguation.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -617,9 +617,8 @@
elif choice in ['d', 'D']:
editor = editarticle.TextEditor()
if disambPage.isRedirectPage():
- disambredir1 = disambPage.getRedirectTarget()
- disambredir2 = wikipedia.Page(wikipedia.getSite(), disambredir1)
- disambigText = editor.edit(disambredir2.get(), jumpIndex = m.start(), highlight = disambredir2.title())
+ disambredir = disambPage.getRedirectTarget()
+ disambigText = editor.edit(disambredir.get(), jumpIndex = m.start(), highlight = disambredir.title())
else:
disambigText = editor.edit(disambPage.get(), jumpIndex = m.start(), highlight = disambPage.title())
elif choice in ['l', 'L']:
@@ -734,7 +733,7 @@
def findAlternatives(self, disambPage):
if disambPage.isRedirectPage() and not self.primary:
try:
- target = disambPage.getRedirectTarget()
+ target = disambPage.getRedirectTarget().title()
self.alternatives.append(target)
except wikipedia.NoPage:
wikipedia.output(u"The specified page was not found.")
Modified: trunk/pywikipedia/us-states.py
===================================================================
--- trunk/pywikipedia/us-states.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/us-states.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -1,4 +1,4 @@
-#coding: utf-8
+#coding: utf-8
"""
Check pages on the English Wikipedia whether they are in the form
Something, State, and if so, create a redirect from Something, ST.
@@ -99,7 +99,7 @@
# A bit hacking here - the real work is done in the 'except wikipedia.NoPage'
# part rather than the 'try'.
try:
- goal = pl.getRedirectTarget()
+ goal = pl.getRedirectTarget().title()
if wikipedia.Page(mysite, goal):
wikipedia.output(u"Not creating %s - redirect already exists." % goal)
else:
@@ -109,7 +109,7 @@
except wikipedia.NoPage:
change=''
if p.isRedirectPage():
- p2 = wikipedia.Page(mysite, p.getRedirectTarget())
+ p2 = p.getRedirectTarget()
wikipeda.ouput(u'Note: goal page is redirect. Creating redirect to "%s" to avoid double redirect.'%p2.title().replace("%2C",",").replace("_"," "))
else:
p2 = p
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2007-09-13 16:40:57 UTC (rev 4274)
+++ trunk/pywikipedia/wikipedia.py 2007-09-13 18:15:18 UTC (rev 4275)
@@ -932,8 +932,7 @@
# make sure this is really a redirect to this page
# (MediaWiki will mark as a redirect any link that follows
# a #REDIRECT marker, not just the first one).
- if Page(self.site(), p.getRedirectTarget()
- ).sectionFreeTitle() == self.sectionFreeTitle():
+ if p.getRedirectTarget().sectionFreeTitle() == self.sectionFreeTitle():
isredirect = True
if self.site().has_mediawiki_message("Istemplate") \
and self._istemplatemessage in textafter:
@@ -1488,19 +1487,19 @@
def getRedirectTarget(self):
"""
- If the page is a redirect page, gives the title of the page it
- redirects to. Otherwise it will raise an IsNotRedirectPage exception.
+ If the page is a redirect page, gives the page it redirects to.
+ Otherwise it will raise an IsNotRedirectPage exception.
This function can raise a NoPage exception.
"""
try:
self.get()
except NoPage:
- raise NoPage(self)
+ raise
except IsRedirectPage, arg:
if '|' in arg:
warnings.warn("%s has a | character, this makes no sense", Warning)
- return arg[0]
+ return Page(self.site(), arg[0])
else:
raise IsNotRedirectPage(self)
@@ -4017,9 +4016,9 @@
# no localized keyword for redirects
redirKeywordsR = r'redirect'
# A redirect starts with hash (#), followed by a keyword, then
- # arbitrary stuff, then a wikilink. The link target ends before
- # either a | or a ].
- return re.compile(r'# *' + redirKeywordsR + '.*?\[\[(.*?)(?:\]|\|)', re.IGNORECASE | re.UNICODE | re.DOTALL)
+ # arbitrary stuff, then a wikilink. The wikilink may contain
+ # a label, although this is not useful.
+ return re.compile(r'# *' + redirKeywordsR + '.*?\[\[(.*?)(?:\|.*?)\]\]', re.IGNORECASE | re.UNICODE | re.DOTALL)
# The following methods are for convenience, so that you can access
# methods of the Family class easier.