Revision: 7356
Author: alexsh
Date: 2009-10-02 11:53:53 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
statistics_in_wikitable: code normalize
Modified Paths:
--------------
trunk/pywikipedia/statistics_in_wikitable.py
Modified: trunk/pywikipedia/statistics_in_wikitable.py
===================================================================
--- trunk/pywikipedia/statistics_in_wikitable.py 2009-10-02 09:46:09 UTC (rev 7355)
+++ trunk/pywikipedia/statistics_in_wikitable.py 2009-10-02 11:53:53 UTC (rev 7356)
@@ -10,13 +10,13 @@
\03{lightred}-screen\03{default} If True, doesn't do any changes, but only shows the statistics.
-\03{lightgreen}-page\03{default} On what page statistics are rendered.
- If not existing yet, it is created.
- If existing, it is updated.
+\03{lightgreen}-page\03{default} On what page statistics are rendered.
+ If not existing yet, it is created.
+ If existing, it is updated.
"""
__version__ = '$Id$'
import wikipedia, pagegenerators, query
-import simplejson, time
+import time
# This is the title of the wikipage where to render stats.
your_page = "Logstats"
@@ -30,131 +30,126 @@
class StatisticsBot:
- def __init__ (self, screen, your_page):
- """
- Constructor. Parameter:
- * screen - If True, doesn't do any real changes, but only shows
- some stats.
- """
- self.screen = screen
- self.your_page = your_page
- self.dict = self.getdata() # Try to get data.
+ def __init__ (self, screen, your_page):
+ """
+ Constructor. Parameter:
+ * screen - If True, doesn't do any real changes, but only shows
+ some stats.
+ """
+ self.screen = screen
+ self.your_page = your_page
+ self.dict = self.getdata() # Try to get data.
+ self.site = wikipedia.getSite()
- def run(self):
- if self.screen:
- wikipedia.output("Bot is running to output stats.")
- self.idle(1) # Run a function to idle
- self.outputall()
- if not self.screen:
- self.outputall() # Output all datas on screen.
- wikipedia.output("\nBot is running. Going to treat \03{lightpurple}%s\03{default}..." % self.your_page )
- self.idle(2)
- self.treat()
+ def run(self):
+ if self.screen:
+ wikipedia.output("Bot is running to output stats.")
+ self.idle(1) # Run a function to idle
+ self.outputall()
+ if not self.screen:
+ self.outputall() # Output all datas on screen.
+ wikipedia.output("\nBot is running. Going to treat \03{lightpurple}%s\03{default}..." % self.your_page )
+ self.idle(2)
+ self.treat()
- def getdata(self): # getdata() returns a dictionnary of the query to api.php?action=query&meta=siteinfo&siprop=statistics
- # This method return data in a dictionnary format.
- # View data with: api.php?action=query&meta=siteinfo&siprop=statistics&format=jsonfm
- params = {
- 'action' :'query',
- 'meta' :'siteinfo',
- 'siprop' :'statistics',
- }
- wikipedia.output("\nQuerying api for json-formatted data...")
- try:
- data = query.GetData(params,
- useAPI = True, encodeTitle = False)
- except:
- site = wikipedia.getSite()
- url = site.protocol() + '://' + site.hostname() + site.api_address()
- wikipedia.output("The query has failed. Have you check the API? Cookies are working?")
- wikipedia.output(u"\n>> \03{lightpurple}%s\03{default} <<" % url)
- if data != None:
- wikipedia.output("Extracting statistics...")
- data = data['query'] # "query" entry of data.
- dict = data['statistics'] # "statistics" entry of "query" dict.
- return dict
+ def getdata(self): # getdata() returns a dictionnary of the query to api.php?action=query&meta=siteinfo&siprop=statistics
+ # This method return data in a dictionnary format.
+ # View data with: api.php?action=query&meta=siteinfo&siprop=statistics&format=jsonfm
+ params = {
+ 'action' :'query',
+ 'meta' :'siteinfo',
+ 'siprop' :'statistics',
+ }
+ wikipedia.output("\nQuerying api for json-formatted data...")
+ try:
+ data = query.GetData(params,self.site, encodeTitle = False)
+ except:
+ url = self.site.protocol() + '://' + self.site.hostname() + self.site.api_address()
+ wikipedia.output("The query has failed. Have you check the API? Cookies are working?")
+ wikipedia.output(u"\n>> \03{lightpurple}%s\03{default} <<" % url)
+ if data != None:
+ wikipedia.output("Extracting statistics...")
+ data = data['query'] # "query" entry of data.
+ dict = data['statistics'] # "statistics" entry of "query" dict.
+ return dict
- def treat(self):
- site = wikipedia.getSite()
- page = wikipedia.Page(site, self.your_page)
- if page.exists():
- wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be completed with:\n' % self.your_page )
- text = page.get()
- newtext = self.newraw()
- wikipedia.output(newtext)
- choice = wikipedia.inputChoice(u'Do you want to add these on wikitable?', ['Yes', 'No'], ['y', 'N'], 'N')
- text = text[:-3] + newtext
- summ = wikipedia.translate(site, summary_update)
- if choice == 'y':
- try:
- page.put(u''.join(text), summ)
- except:
- wikipedia.output(u'Impossible to edit. It may be an edit conflict... Skipping...')
- else:
- wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be created with:\n' % self.your_page )
- newtext = self.newtable()+self.newraw()
- wikipedia.output(newtext)
- summ = wikipedia.translate(site, summary_creation)
- choice = wikipedia.inputChoice(u'Do you want to accept this page creation?', ['Yes', 'No'], ['y', 'N'], 'N')
- if choice == 'y':
- try:
- page.put(newtext, summ)
- except wikipedia.LockedPage:
- wikipedia.output(u"Page %s is locked; skipping." % title)
- except wikipedia.EditConflict:
- wikipedia.output(u'Skipping %s because of edit conflict' % title)
- except wikipedia.SpamfilterError, error:
- wikipedia.output(u'Cannot change %s because of spam blacklist entry %s' % (title, error.url))
+ def treat(self):
+ page = wikipedia.Page(self.site, self.your_page)
+ if page.exists():
+ wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be completed with:\n' % self.your_page )
+ text = page.get()
+ newtext = self.newraw()
+ wikipedia.output(newtext)
+ choice = wikipedia.inputChoice(u'Do you want to add these on wikitable?', ['Yes', 'No'], ['y', 'N'], 'N')
+ text = text[:-3] + newtext
+ summ = wikipedia.translate(self.site, summary_update)
+ if choice == 'y':
+ try:
+ page.put(u''.join(text), summ)
+ except:
+ wikipedia.output(u'Impossible to edit. It may be an edit conflict... Skipping...')
+ else:
+ wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be created with:\n' % self.your_page )
+ newtext = self.newtable()+self.newraw()
+ wikipedia.output(newtext)
+ summ = wikipedia.translate(self.site, summary_creation)
+ choice = wikipedia.inputChoice(u'Do you want to accept this page creation?', ['Yes', 'No'], ['y', 'N'], 'N')
+ if choice == 'y':
+ try:
+ page.put(newtext, summ)
+ except wikipedia.LockedPage:
+ wikipedia.output(u"Page %s is locked; skipping." % title)
+ except wikipedia.EditConflict:
+ wikipedia.output(u'Skipping %s because of edit conflict' % title)
+ except wikipedia.SpamfilterError, error:
+ wikipedia.output(u'Cannot change %s because of spam blacklist entry %s' % (title, error.url))
- def newraw(self):
- newtext = ('\n|----\n!\'\''+ self.date() +'\'\'') # new raw for date and stats
- for name in self.dict:
- newtext += '\n|'+str(abs(self.dict[name]))
- newtext += '\n|----\n|}'
- return newtext
+ def newraw(self):
+ newtext = ('\n|----\n!\'\''+ self.date() +'\'\'') # new raw for date and stats
+ for name in self.dict:
+ newtext += '\n|'+str(abs(self.dict[name]))
+ newtext += '\n|----\n|}'
+ return newtext
- def newtable(self):
- newtext = ('\n{| class=wikitable style=text-align:center\n!'+ "date") # create table
- for name in self.dict:
- newtext += '\n|'+name
- return newtext
+ def newtable(self):
+ newtext = ('\n{| class=wikitable style=text-align:center\n!'+ "date") # create table
+ for name in self.dict:
+ newtext += '\n|'+name
+ return newtext
- def date(self):
- rightime = time.localtime(time.time())
- year = str(rightime[0])
- month = str(rightime[1])
- day = str(rightime[2])
- date = year+'/'+month+'/'+day
- return date
- def outputall(self):
- list = self.dict.keys()
- list.sort()
- for name in self.dict:
- wikipedia.output("There are "+str(self.dict[name])+" "+name)
- def idle(self, retry_idle_time):
- time.sleep(retry_idle_time)
- wikipedia.output(u"Starting in %i second..." % retry_idle_time)
- time.sleep(retry_idle_time)
+ def date(self):
+ return time.strftime('%Y/%m/%d', time.localtime(time.time()))
+
+ def outputall(self):
+ list = self.dict.keys()
+ list.sort()
+ for name in self.dict:
+ wikipedia.output("There are "+str(self.dict[name])+" "+name)
+
+ def idle(self, retry_idle_time):
+ time.sleep(retry_idle_time)
+ wikipedia.output(u"Starting in %i second..." % retry_idle_time)
+ time.sleep(retry_idle_time)
def main(your_page):
- screen = False # If True it would not edit the wiki, only output statistics
- _page = None
+ screen = False # If True it would not edit the wiki, only output statistics
+ _page = None
- wikipedia.output("\nBuilding the bot...")
- for arg in wikipedia.handleArgs(): # Parse command line arguments
- if arg.startswith('-page'):
- if len(arg) == 5:
- _page = wikipedia.input(u'On what page do you want to add statistics?')
- else:
- _page = arg[6:]
- if arg.startswith("-screen"):
- screen = True
- if not _page:
- _page = your_page
- if not screen:
- wikipedia.output("The bot will add statistics on %s.\n" % _page )
- bot = StatisticsBot(screen, _page) # Launch the instance of a StatisticsBot
- bot.run() # Execute the 'run' method
+ wikipedia.output("\nBuilding the bot...")
+ for arg in wikipedia.handleArgs(): # Parse command line arguments
+ if arg.startswith('-page'):
+ if len(arg) == 5:
+ _page = wikipedia.input(u'On what page do you want to add statistics?')
+ else:
+ _page = arg[6:]
+ if arg.startswith("-screen"):
+ screen = True
+ if not _page:
+ _page = your_page
+ if not screen:
+ wikipedia.output("The bot will add statistics on %s.\n" % _page )
+ bot = StatisticsBot(screen, _page) # Launch the instance of a StatisticsBot
+ bot.run() # Execute the 'run' method
if __name__ == "__main__":
try:
Revision: 7355
Author: siebrand
Date: 2009-10-02 09:46:09 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
[ #2818283 ] script to manage statistics. Contributed by almaghi.
Added Paths:
-----------
trunk/pywikipedia/statistics_in_wikitable.py
Added: trunk/pywikipedia/statistics_in_wikitable.py
===================================================================
--- trunk/pywikipedia/statistics_in_wikitable.py (rev 0)
+++ trunk/pywikipedia/statistics_in_wikitable.py 2009-10-02 09:46:09 UTC (rev 7355)
@@ -0,0 +1,163 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+
+
+\03{lightyellow}This bot renders statistics provided by [[Special:Statistics]] in a table on a wiki page.\03{default}
+Thus it creates and updates a Statistics wikitable.
+
+The following parameters are supported:
+
+\03{lightred}-screen\03{default} If True, doesn't do any changes, but only shows the statistics.
+
+\03{lightgreen}-page\03{default} On what page statistics are rendered.
+ If not existing yet, it is created.
+ If existing, it is updated.
+"""
+__version__ = '$Id$'
+import wikipedia, pagegenerators, query
+import simplejson, time
+
+# This is the title of the wikipage where to render stats.
+your_page = "Logstats"
+
+summary_update = {
+ 'en':u'Updating some statistics.',
+ }
+summary_creation = {
+ 'en':u'Creating statistics log page.',
+ }
+
+
+class StatisticsBot:
+ def __init__ (self, screen, your_page):
+ """
+ Constructor. Parameter:
+ * screen - If True, doesn't do any real changes, but only shows
+ some stats.
+ """
+ self.screen = screen
+ self.your_page = your_page
+ self.dict = self.getdata() # Try to get data.
+
+ def run(self):
+ if self.screen:
+ wikipedia.output("Bot is running to output stats.")
+ self.idle(1) # Run a function to idle
+ self.outputall()
+ if not self.screen:
+ self.outputall() # Output all datas on screen.
+ wikipedia.output("\nBot is running. Going to treat \03{lightpurple}%s\03{default}..." % self.your_page )
+ self.idle(2)
+ self.treat()
+
+ def getdata(self): # getdata() returns a dictionnary of the query to api.php?action=query&meta=siteinfo&siprop=statistics
+ # This method return data in a dictionnary format.
+ # View data with: api.php?action=query&meta=siteinfo&siprop=statistics&format=jsonfm
+ params = {
+ 'action' :'query',
+ 'meta' :'siteinfo',
+ 'siprop' :'statistics',
+ }
+ wikipedia.output("\nQuerying api for json-formatted data...")
+ try:
+ data = query.GetData(params,
+ useAPI = True, encodeTitle = False)
+ except:
+ site = wikipedia.getSite()
+ url = site.protocol() + '://' + site.hostname() + site.api_address()
+ wikipedia.output("The query has failed. Have you check the API? Cookies are working?")
+ wikipedia.output(u"\n>> \03{lightpurple}%s\03{default} <<" % url)
+ if data != None:
+ wikipedia.output("Extracting statistics...")
+ data = data['query'] # "query" entry of data.
+ dict = data['statistics'] # "statistics" entry of "query" dict.
+ return dict
+
+ def treat(self):
+ site = wikipedia.getSite()
+ page = wikipedia.Page(site, self.your_page)
+ if page.exists():
+ wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be completed with:\n' % self.your_page )
+ text = page.get()
+ newtext = self.newraw()
+ wikipedia.output(newtext)
+ choice = wikipedia.inputChoice(u'Do you want to add these on wikitable?', ['Yes', 'No'], ['y', 'N'], 'N')
+ text = text[:-3] + newtext
+ summ = wikipedia.translate(site, summary_update)
+ if choice == 'y':
+ try:
+ page.put(u''.join(text), summ)
+ except:
+ wikipedia.output(u'Impossible to edit. It may be an edit conflict... Skipping...')
+ else:
+ wikipedia.output(u'\nWikitable on \03{lightpurple}%s\03{default} will be created with:\n' % self.your_page )
+ newtext = self.newtable()+self.newraw()
+ wikipedia.output(newtext)
+ summ = wikipedia.translate(site, summary_creation)
+ choice = wikipedia.inputChoice(u'Do you want to accept this page creation?', ['Yes', 'No'], ['y', 'N'], 'N')
+ if choice == 'y':
+ try:
+ page.put(newtext, summ)
+ except wikipedia.LockedPage:
+ wikipedia.output(u"Page %s is locked; skipping." % title)
+ except wikipedia.EditConflict:
+ wikipedia.output(u'Skipping %s because of edit conflict' % title)
+ except wikipedia.SpamfilterError, error:
+ wikipedia.output(u'Cannot change %s because of spam blacklist entry %s' % (title, error.url))
+
+ def newraw(self):
+ newtext = ('\n|----\n!\'\''+ self.date() +'\'\'') # new raw for date and stats
+ for name in self.dict:
+ newtext += '\n|'+str(abs(self.dict[name]))
+ newtext += '\n|----\n|}'
+ return newtext
+
+ def newtable(self):
+ newtext = ('\n{| class=wikitable style=text-align:center\n!'+ "date") # create table
+ for name in self.dict:
+ newtext += '\n|'+name
+ return newtext
+
+ def date(self):
+ rightime = time.localtime(time.time())
+ year = str(rightime[0])
+ month = str(rightime[1])
+ day = str(rightime[2])
+ date = year+'/'+month+'/'+day
+ return date
+ def outputall(self):
+ list = self.dict.keys()
+ list.sort()
+ for name in self.dict:
+ wikipedia.output("There are "+str(self.dict[name])+" "+name)
+ def idle(self, retry_idle_time):
+ time.sleep(retry_idle_time)
+ wikipedia.output(u"Starting in %i second..." % retry_idle_time)
+ time.sleep(retry_idle_time)
+
+def main(your_page):
+ screen = False # If True it would not edit the wiki, only output statistics
+ _page = None
+
+ wikipedia.output("\nBuilding the bot...")
+ for arg in wikipedia.handleArgs(): # Parse command line arguments
+ if arg.startswith('-page'):
+ if len(arg) == 5:
+ _page = wikipedia.input(u'On what page do you want to add statistics?')
+ else:
+ _page = arg[6:]
+ if arg.startswith("-screen"):
+ screen = True
+ if not _page:
+ _page = your_page
+ if not screen:
+ wikipedia.output("The bot will add statistics on %s.\n" % _page )
+ bot = StatisticsBot(screen, _page) # Launch the instance of a StatisticsBot
+ bot.run() # Execute the 'run' method
+
+if __name__ == "__main__":
+ try:
+ main(your_page)
+ finally:
+ wikipedia.stopme()
Property changes on: trunk/pywikipedia/statistics_in_wikitable.py
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Revision: 7354
Author: siebrand
Date: 2009-10-02 09:38:11 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
* patch #2848560 reverted
* getting mediawiki-messages depending of current version
* solves various requests to r7267 like #2867449, #2865679, #2862265, #2860410
Patch by xqt
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2009-10-02 09:36:54 UTC (rev 7353)
+++ trunk/pywikipedia/wikipedia.py 2009-10-02 09:38:11 UTC (rev 7354)
@@ -886,7 +886,7 @@
def previousRevision(self):
"""Return the revision id for the previous revision of this Page."""
vh = self.getVersionHistory(revCount=2)
- return vh[0][0]
+ return vh[1][0]
def exists(self):
"""Return True if page exists on the wiki, even if it's a redirect.
@@ -5402,7 +5402,7 @@
retry_idle_time = 1
while True:
- if api and self.versionnumber() >= 12:
+ if api and self.versionnumber() >= 12 or self.versionnumber() >= 16:
params = {
'action':'query',
'meta':'allmessages',
Revision: 7353
Author: siebrand
Date: 2009-10-02 09:36:54 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
[ #2783408 ] weblinkchecker warning
Patch by xqt
Modified Paths:
--------------
trunk/pywikipedia/weblinkchecker.py
Modified: trunk/pywikipedia/weblinkchecker.py
===================================================================
--- trunk/pywikipedia/weblinkchecker.py 2009-10-02 09:35:30 UTC (rev 7352)
+++ trunk/pywikipedia/weblinkchecker.py 2009-10-02 09:36:54 UTC (rev 7353)
@@ -789,6 +789,7 @@
# that are also used by other scripts and that determine on which pages
# to work on.
genFactory = pagegenerators.GeneratorFactory()
+ global day
day = 7
for arg in wikipedia.handleArgs():
if arg == '-talk':
@@ -805,7 +806,6 @@
elif arg.startswith('-ignore:'):
HTTPignore.append(int(arg[8:]))
elif arg.startswith('-day:'):
- global day
day = int(arg[5:])
else:
if not genFactory.handleArg(arg):
Revision: 7350
Author: siebrand
Date: 2009-10-02 09:34:45 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
additional features copied from fixes.py
Patch by xqt
Modified Paths:
--------------
trunk/pywikipedia/cosmetic_changes.py
Modified: trunk/pywikipedia/cosmetic_changes.py
===================================================================
--- trunk/pywikipedia/cosmetic_changes.py 2009-10-02 09:34:16 UTC (rev 7349)
+++ trunk/pywikipedia/cosmetic_changes.py 2009-10-02 09:34:45 UTC (rev 7350)
@@ -188,6 +188,8 @@
text = self.validXhtml(text)
text = self.removeUselessSpaces(text)
text = self.removeNonBreakingSpaceBeforePercent(text)
+ text = self.fixSyntaxSave(text)
+ text = self.fixHtml(text)
try:
text = isbn.hyphenateIsbnNumbers(text)
except isbn.InvalidIsbnException, error:
@@ -424,6 +426,39 @@
text = wikipedia.replaceExcept(text, r'\{\{([mM][sS][gG]:)?' + template + '(?P<parameters>\|[^}]+|)}}', '', ['comment', 'math', 'nowiki', 'pre'])
return text
+ #from fixes.py
+ def fixSyntaxSave(self, text):
+ exceptions = ['nowiki', 'comment', 'math', 'pre', 'source', 'startspace']
+ # external link in double brackets
+ text = wikipedia.replaceExcept(text, r'\[\[(?P<url>https?://[^\]]+?)\]\]', r'[\g<url>]', exceptions)
+ # external link starting with double bracket
+ text = wikipedia.replaceExcept(text, r'\[\[(?P<url>https?://.+?)\]', r'[\g<url>]', exceptions)
+ # external link and description separated by a dash, with
+ # whitespace in front of the dash, so that it is clear that
+ # the dash is not a legitimate part of the URL.
+ text = wikipedia.replaceExcept(text, r'\[(?P<url>https?://[^\|\] \r\n]+?) +\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
+ # dash in external link, where the correct end of the URL can
+ # be detected from the file extension. It is very unlikely that
+ # this will cause mistakes.
+ text = wikipedia.replaceExcept(text, r'\[(?P<url>https?://[^\|\] ]+?(\.pdf|\.html|\.htm|\.php|\.asp|\.aspx|\.jsp)) *\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
+ return text
+
+ def fixHtml(self, text):
+ # Everything case-insensitive (?i)
+ # Keep in mind that MediaWiki automatically converts <br> to <br />
+ exceptions = ['nowiki', 'comment', 'math', 'pre', 'source', 'startspace']
+ text = wikipedia.replaceExcept(text, r'(?i)<b>(.*?)</b>', r"'''\1'''" , exceptions)
+ text = wikipedia.replaceExcept(text, r'(?i)<strong>(.*?)</strong>', r"'''\1'''" , exceptions)
+ text = wikipedia.replaceExcept(text, r'(?i)<i>(.*?)</i>', r"''\1''" , exceptions)
+ text = wikipedia.replaceExcept(text, r'(?i)<em>(.*?)</em>', r"''\1''" , exceptions)
+ # horizontal line without attributes in a single line
+ text = wikipedia.replaceExcept(text, r'(?i)([\r\n])<hr[ /]*>([\r\n])', r'\1----\2', exceptions)
+ # horizontal line with attributes; can't be done with wiki syntax
+ # so we only make it XHTML compliant
+ text = wikipedia.replaceExcept(text, r'(?i)<hr ([^>/]+?)>', r'<hr \1 />', exceptions)
+ # TODO: maybe we can make the bot replace <p> tags with \r\n's.
+ return text
+
class CosmeticChangesBot:
def __init__(self, generator, acceptall = False):
self.generator = generator
Revision: 7349
Author: siebrand
Date: 2009-10-02 09:34:16 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
set use_api default value to True
(solves various requests to r7267like #2867449, #2865679, #2862265,
#2860410)
Patch by xqt
Modified Paths:
--------------
trunk/pywikipedia/config.py
Modified: trunk/pywikipedia/config.py
===================================================================
--- trunk/pywikipedia/config.py 2009-10-02 09:33:34 UTC (rev 7348)
+++ trunk/pywikipedia/config.py 2009-10-02 09:34:16 UTC (rev 7349)
@@ -78,7 +78,7 @@
# Enable data recieve from all avalible API.
-use_api = False
+use_api = True
# Get the names of all known families, and initialize
# with empty dictionaries
Revision: 7348
Author: siebrand
Date: 2009-10-02 09:33:34 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
handle index error of page.previousRevision()
(solves #2848560)
Patch by xqt
Modified Paths:
--------------
trunk/pywikipedia/censure.py
Modified: trunk/pywikipedia/censure.py
===================================================================
--- trunk/pywikipedia/censure.py 2009-10-02 09:28:38 UTC (rev 7347)
+++ trunk/pywikipedia/censure.py 2009-10-02 09:33:34 UTC (rev 7348)
@@ -68,22 +68,26 @@
def checkPage(title, onlyLastDiff = False):
if title == logPages[site.language() + '.' + site.family.name]:
return
- wikipedia.output('Checking ' + title + ' for bad word list')
+ wikipedia.output(u'Checking %s for bad word list' %title)
page = wikipedia.Page(site, title)
try:
text = page.get()
if onlyLastDiff:
- oldver = page.getOldVersion(page.previousRevision())
+ try:
+ oldver = page.getOldVersion(page.previousRevision())
+ except IndexError:
+ wikipedia.output(u'Page %s has no version history, skipping' %title)
+ return
if len(text) > len(oldver):
bpos = seekbpos(oldver, text)
epos = seekepos(oldver, text, bpos)
diff = text[bpos:epos]
text = diff
except wikipedia.NoPage:
- wikipedia.output('Page ' + title + ' doesn\'t exist, skipping')
+ wikipedia.output(u'Page %s doesn\'t exist, skipping' %title)
return
except wikipedia.IsRedirectPage:
- wikipedia.output('Page ' + title + ' is a redirect, skipping')
+ wikipedia.output(u'Page %s is a redirect, skipping' %title)
return
report = False
@@ -98,11 +102,11 @@
log = logPage.get()
except:
pass
- wikipedia.output(title + ' matches the bad word list')
+ wikipedia.output(u'%s matches the bad word list' %title)
log = '* [' + page.permalink()+ ' ' + title + '] - ' + ' '.join(wordsIn) + '\n' + log
logPage.put(log, title)
else:
- wikipedia.output(title + ' doesn\'t match any of the bad word list')
+ wikipedia.output(u'%s doesn\'t match any of the bad word list' %title)
def main():
wikipedia.output('Warning: this script should not be run manually/directly, but automatically by maintainer.py')
Revision: 7347
Author: siebrand
Date: 2009-10-02 09:28:38 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
[ #2870792 ] Update blockpageschecker for the rewrite branch. Patch by stanlekub.
* have not testeed.
Modified Paths:
--------------
branches/rewrite/scripts/blockpageschecker.py
Modified: branches/rewrite/scripts/blockpageschecker.py
===================================================================
--- branches/rewrite/scripts/blockpageschecker.py 2009-10-02 09:22:46 UTC (rev 7346)
+++ branches/rewrite/scripts/blockpageschecker.py 2009-10-02 09:28:38 UTC (rev 7347)
@@ -63,7 +63,9 @@
#
import re, webbrowser
-import wikipedia, catlib, pagegenerators, config
+import pywikibot
+from pywikibot import pagegenerators
+from pywikibot import config
# This is required for the text that is shown when you run this script
# with the parameter -help.
@@ -115,7 +117,7 @@
'en': None,
'it': [r'\{\{(?:[Tt]emplate:|)[Pp]rotetta\}\}'],
}
-
+
# Array: 0 => Semi-block, 1 => Total Block, 2 => Semi-Move, 3 => Total-Move, 4 => template-unique
templateNoRegex = {
'it':['{{Avvisobloccoparziale}}', '{{Avvisoblocco}}', None, None, '{{Protetta}}'],
@@ -171,7 +173,7 @@
for catchRegex in TU:
resultCatch = re.findall(catchRegex, text)
if resultCatch:
- return ('unique', catchRegex)
+ return ('unique', catchRegex)
if TSMP != None and TTMP != None and TTP != TTMP and TSP != TSMP:
for catchRegex in TTMP:
resultCatch = re.findall(catchRegex, text)
@@ -184,9 +186,9 @@
return ('editable', r'\A\n') # If editable means that we have no regex, won't change anything with this regex
def debugQuest(site, page):
- quest = wikipedia.input(u'Do you want to open the page on your [b]rowser, [g]ui or [n]othing?')
+ quest = pywikibot.input(u'Do you want to open the page on your [b]rowser, [g]ui or [n]othing?')
pathWiki = site.family.nicepath(site.lang)
- url = 'http://%s%s%s?&redirect=no' % (wikipedia.getSite().hostname(), pathWiki, page.urlname())
+ url = 'http://%s%s%s?&redirect=no' % (pywikibot.getSite().hostname(), pathWiki, page.urlname())
while 1:
if quest.lower() in ['b', 'B']:
webbrowser.open(url)
@@ -199,7 +201,7 @@
elif quest.lower() in ['n', 'N']:
break
else:
- wikipedia.output(u'wrong entry, type "b", "g" or "n"')
+ pywikibot.output(u'wrong entry, type "b", "g" or "n"')
continue
def main():
@@ -207,7 +209,7 @@
# Loading the comments
global categoryToCheck; global comment; global project_inserted
if config.mylang not in project_inserted:
- wikipedia.output(u"Your project is not supported by this script. You have to edit the script and add it!")
+ pywikibot.output(u"Your project is not supported by this script. You have to edit the script and add it!")
return
# always, define a generator to understand if the user sets one, defining what's genFactory
always = False; generator = False; debug = False
@@ -215,9 +217,9 @@
# To prevent Infinite loops
errorCount = 0
# Load the right site
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
# Loading the default options.
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg == '-always':
always = True
elif arg == '-move':
@@ -237,48 +239,48 @@
type = 'move')
elif arg.startswith('-page'):
if len(arg) == 5:
- generator = [wikipedia.Page(wikipedia.getSite(), wikipedia.input(u'What page do you want to use?'))]
+ generator = [pywikibot.Page(pywikibot.getSite(), pywikibot.input(u'What page do you want to use?'))]
else:
- generator = [wikipedia.Page(wikipedia.getSite(), arg[6:])]
+ generator = [pywikibot.Page(pywikibot.getSite(), arg[6:])]
else:
genFactory.handleArg(arg)
# Take the right templates to use, the category and the comment
- TSP = wikipedia.translate(site, templateSemiProtection)
- TTP = wikipedia.translate(site, templateTotalProtection)
- TSMP = wikipedia.translate(site, templateSemiMoveProtection)
- TTMP = wikipedia.translate(site, templateTotalMoveProtection)
- TNR = wikipedia.translate(site, templateNoRegex)
- TU = wikipedia.translate(site, templateUnique)
+ TSP = pywikibot.translate(site, templateSemiProtection)
+ TTP = pywikibot.translate(site, templateTotalProtection)
+ TSMP = pywikibot.translate(site, templateSemiMoveProtection)
+ TTMP = pywikibot.translate(site, templateTotalMoveProtection)
+ TNR = pywikibot.translate(site, templateNoRegex)
+ TU = pywikibot.translate(site, templateUnique)
- category = wikipedia.translate(site, categoryToCheck)
- commentUsed = wikipedia.translate(site, comment)
+ category = pywikibot.translate(site, categoryToCheck)
+ commentUsed = pywikibot.translate(site, comment)
if not generator:
gen = genFactory.getCombinedGenerator()
if not generator:
generator = list()
- wikipedia.output(u'Loading categories...')
+ pywikibot.output(u'Loading categories...')
# Define the category if no other generator has been setted
for CAT in category:
- cat = catlib.Category(site, CAT)
+ cat = pywikibot.Category(site, CAT)
# Define the generator
gen = pagegenerators.CategorizedPageGenerator(cat)
for pageCat in gen:
generator.append(pageCat)
- wikipedia.output(u'Categories loaded, start!')
+ pywikibot.output(u'Categories loaded, start!')
# Main Loop
- preloadingGen = pagegenerators.PreloadingGenerator(generator, pageNumber = 60)
+ preloadingGen = pagegenerators.PreloadingGenerator(generator, step = 60)
for page in preloadingGen:
- pagename = page.aslink()
- wikipedia.output('Loading %s...' % pagename)
+ pagename = page.title(asLink=True)
+ pywikibot.output('Loading %s...' % pagename)
try:
text = page.get()
- restrictions = page.getRestrictions()
- except wikipedia.NoPage:
- wikipedia.output("%s doesn't exist! Skipping..." % pagename)
+ restrictions = site.page_restrictions(page)
+ except pywikibot.NoPage:
+ pywikibot.output("%s doesn't exist! Skipping..." % pagename)
continue
- except wikipedia.IsRedirectPage:
- wikipedia.output("%s is a redirect! Skipping..." % pagename)
+ except pywikibot.IsRedirectPage:
+ pywikibot.output("%s is a redirect! Skipping..." % pagename)
if debug:
debugQuest(site, page)
continue
@@ -287,15 +289,18 @@
# PreloadingGenerator cannot set correctly page.editRestriction
# (see bug #1949476 )
if not page.canBeEdited():
- wikipedia.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
+ pywikibot.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
continue
"""
- editRestr = restrictions['edit']
+ if restrictions.has_key('edit'):
+ editRestr = restrictions['edit']
+ else:
+ editRestr = None
if editRestr and editRestr[0] == 'sysop':
try:
config.sysopnames[site.family.name][site.lang]
except:
- wikipedia.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
+ pywikibot.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
continue
# Understand, according to the template in the page, what should be the protection
@@ -312,11 +317,11 @@
if TU != None:
replaceToPerform = u'|'.join(TTP + TSP + TU)
else:
- replaceToPerform = u'|'.join(TTP + TSP)
+ replaceToPerform = u'|'.join(TTP + TSP)
text, changes = re.subn('<noinclude>(%s)</noinclude>' % replaceToPerform, '', text)
if changes == 0:
- text, changes = re.subn('(%s)' % replaceToPerform, '', text)
- wikipedia.output(u'The page is editable for all, deleting the template...')
+ text, changes = re.subn('(%s)' % replaceToPerform, '', text)
+ pywikibot.output(u'The page is editable for all, deleting the template...')
elif editRestr[0] == 'sysop':
# total edit protection
@@ -324,9 +329,9 @@
msg = 'The page is protected to the sysop'
if not moveBlockCheck:
msg += ', skipping...'
- wikipedia.output(msg)
+ pywikibot.output(msg)
else:
- wikipedia.output(u'The page is protected to the sysop, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is protected to the sysop, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -338,9 +343,9 @@
msg = 'The page is editable only for the autoconfirmed users'
if not moveBlockCheck:
msg += ', skipping...'
- wikipedia.output(msg)
+ pywikibot.output(msg)
else:
- wikipedia.output(u'The page is editable only for the autoconfirmed users, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is editable only for the autoconfirmed users, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -348,7 +353,7 @@
if changes == 0:
# We tried to fix edit-protection templates, but it did not work.
- wikipedia.output('Warning : No edit-protection template could be found')
+ pywikibot.output('Warning : No edit-protection template could be found')
if moveBlockCheck:
# checking move protection now
@@ -356,7 +361,7 @@
changes = -1
if not moveRestr:
- wikipedia.output(u'The page is movable for all, deleting the template...')
+ pywikibot.output(u'The page is movable for all, deleting the template...')
# Deleting the template because the page doesn't need it.
if TU != None:
replaceToPerform = u'|'.join(TSMP + TTMP + TU)
@@ -368,9 +373,9 @@
elif moveRestr[0] == 'sysop':
# move-total-protection
if (TemplateInThePage[0] == 'sysop-move' and TTMP != None) or (TemplateInThePage[0] == 'unique' and TU != None):
- wikipedia.output(u'The page is protected from moving to the sysop, skipping...')
+ pywikibot.output(u'The page is protected from moving to the sysop, skipping...')
else:
- wikipedia.output(u'The page is protected from moving to the sysop, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is protected from moving to the sysop, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -379,9 +384,9 @@
elif TSMP != None or TU != None:
# implicitely moveRestr[0] = 'autoconfirmed', move-semi-protection
if TemplateInThePage[0] == 'autoconfirmed-move' or TemplateInThePage[0] == 'unique':
- wikipedia.output(u'The page is movable only for the autoconfirmed users, skipping...')
+ pywikibot.output(u'The page is movable only for the autoconfirmed users, skipping...')
else:
- wikipedia.output(u'The page is movable only for the autoconfirmed users, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is movable only for the autoconfirmed users, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -389,43 +394,43 @@
if changes == 0:
# We tried to fix move-protection templates, but it did not work.
- wikipedia.output('Warning : No move-protection template could be found')
+ pywikibot.output('Warning : No move-protection template could be found')
if oldtext != text:
# Ok, asking if the change has to be performed and do it if yes.
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
- wikipedia.showDiff(oldtext, text)
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
+ pywikibot.showDiff(oldtext, text)
if not always:
- choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
+ choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
if choice == 'a':
always = True
if always or choice == 'y':
while 1:
try:
page.put(text, commentUsed, force=True)
- except wikipedia.EditConflict:
- wikipedia.output(u'Edit conflict! skip!')
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Edit conflict! skip!')
break
- except wikipedia.ServerError:
+ except pywikibot.ServerError:
# Sometimes there is this error that's quite annoying because
# can block the whole process for nothing.
errorCount += 1
if errorCount < 5:
- wikipedia.output(u'Server Error! Wait..')
+ pywikibot.output(u'Server Error! Wait..')
time.sleep(3)
continue
else:
# Prevent Infinite Loops
- raise wikipedia.ServerError(u'Fifth Server Error!')
- except wikipedia.SpamfilterError, e:
- wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
+ raise pywikibot.ServerError(u'Fifth Server Error!')
+ except pywikibot.SpamfilterError, e:
+ pywikibot.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
break
- except wikipedia.PageNotSaved, error:
- wikipedia.output(u'Error putting page: %s' % (error.args,))
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u'Error putting page: %s' % (error.args,))
break
- except wikipedia.LockedPage:
- wikipedia.output(u'The page is still protected. Skipping...')
+ except pywikibot.LockedPage:
+ pywikibot.output(u'The page is still protected. Skipping...')
break
else:
# Break only if the errors are one after the other
@@ -436,4 +441,4 @@
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()