http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9400
Revision: 9400
Author: multichill
Date: 2011-07-16 18:32:20 +0000 (Sat, 16 Jul 2011)
Log Message:
-----------
First shot at implementing globalusage in Pywikipedia
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2011-07-16 16:17:24 UTC (rev 9399)
+++ trunk/pywikipedia/wikipedia.py 2011-07-16 18:32:20 UTC (rev 9400)
@@ -3723,6 +3723,7 @@
getFileVersionHistoryTable: Return the version history in the form of a
wiki table.
usingPages : Yield Pages on which the image is displayed.
+ globalUsage : Yield Pages on which the image is used globally
"""
def __init__(self, site, title, insite = None):
@@ -3965,8 +3966,53 @@
output(
u"Image description page %s contains invalid reference to [[%s]]."
% (self.title(), match.group('title')))
+
+ def globalUsage(self):
+ '''
+ Yield Pages on which the image is used globally.
+ Currently this probably only works on Wikimedia Commonas.
+ '''
+
+ if not self.site().has_api() or self.site().versionnumber() < 11:
+ # Not supported, just return none
+ return
+ params = {
+ 'action': 'query',
+ 'prop': 'globalusage',
+ 'titles': self.title(),
+ 'gulimit': config.special_page_limit,
+ #'': '',
+ }
+ while True:
+ data = query.GetData(params, self.site())
+ if 'error' in data:
+ raise RuntimeError("%s" % data['error'])
+
+ for (page, globalusage) in data['query']['pages'].items():
+ for gu in globalusage['globalusage']:
+ #FIXME : Should have a cleaner way to get the wiki where the image is used
+ siteparts = gu['wiki'].split('.')
+ if len(siteparts)==3:
+ if siteparts[0] in self.site().fam().alphabetic and siteparts[1] in ['wikipedia', 'wiktionary', 'wikibooks', 'wikiquote','wikisource']:
+ code = siteparts[0]
+ fam = siteparts[1]
+ elif siteparts[0] in ['meta', 'incubator'] and siteparts[1]==u'wikimedia':
+ code = code = siteparts[0]
+ fam = code = siteparts[0]
+ else:
+ code = None
+ fam = None
+ if code and fam:
+ site = getSite(code=code, fam=fam)
+ yield Page(site, gu['title'])
+
+ if 'query-continue' in data:
+ params['gucontinue'] = data['query-continue']['globalusage']['gucontinue']
+ else:
+ break
+
class _GetAll(object):
"""For internal use only - supports getall() function"""
def __init__(self, site, pages, throttle, force):
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9399
Revision: 9399
Author: multichill
Date: 2011-07-16 16:17:24 +0000 (Sat, 16 Jul 2011)
Log Message:
-----------
Check if the added category actually exists.
Modified Paths:
--------------
trunk/pywikipedia/imageuncat.py
Modified: trunk/pywikipedia/imageuncat.py
===================================================================
--- trunk/pywikipedia/imageuncat.py 2011-07-16 16:17:18 UTC (rev 9398)
+++ trunk/pywikipedia/imageuncat.py 2011-07-16 16:17:24 UTC (rev 9399)
@@ -1294,9 +1294,12 @@
pywikibot.output(u'Working on '+ page.title())
for category in page.categories():
- if category.title() not in ignoreCategories:
- pywikibot.output(u'Got category ' + category.title())
- return False
+ # Check if it's not a red link category
+ if category.exists():
+ # Check if it's not a category to ignore
+ if category.title() not in ignoreCategories:
+ pywikibot.output(u'Got category ' + category.title())
+ return False
#FIXME: Add check if the category is hidden. If hidden -> ignore
for templateWithTrail in page.templates():
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9398
Revision: 9398
Author: xqt
Date: 2011-07-16 16:17:18 +0000 (Sat, 16 Jul 2011)
Log Message:
-----------
remove i18n code duplication for language fallback
Modified Paths:
--------------
trunk/pywikipedia/pywikibot/__init__.py
trunk/pywikipedia/pywikibot/i18n.py
trunk/pywikipedia/pywikibot/textlib.py
Modified: trunk/pywikipedia/pywikibot/__init__.py
===================================================================
--- trunk/pywikipedia/pywikibot/__init__.py 2011-07-16 12:49:57 UTC (rev 9397)
+++ trunk/pywikipedia/pywikibot/__init__.py 2011-07-16 16:17:18 UTC (rev 9398)
@@ -3,7 +3,7 @@
The initialization file for the Pywikibot framework.
"""
#
-# (C) Pywikipedia bot team, 2010
+# (C) Pywikipedia bot team, 2010-2011
#
# Distributed under the terms of the MIT license.
#
@@ -13,6 +13,7 @@
import difflib
from exceptions import *
+from i18n import translate
from textlib import *
from throttle import *
Modified: trunk/pywikipedia/pywikibot/i18n.py
===================================================================
--- trunk/pywikipedia/pywikibot/i18n.py 2011-07-16 12:49:57 UTC (rev 9397)
+++ trunk/pywikipedia/pywikibot/i18n.py 2011-07-16 16:17:18 UTC (rev 9398)
@@ -196,6 +196,16 @@
if hasattr(code, 'lang'):
code = code.lang
+ # If xdict attribute is wikipedia, define the xdite had multiple projects
+ if 'wikipedia' in xdict:
+ if pywikibot.default_family in xdict:
+ xdict = xdict[pywikibot.default_family]
+ else:
+ xdict = xdict['wikipedia']
+
+ if type(xdict) != dict:
+ return xdict
+
if code in xdict:
return xdict[code]
for alt in _altlang(code):
Modified: trunk/pywikipedia/pywikibot/textlib.py
===================================================================
--- trunk/pywikipedia/pywikibot/textlib.py 2011-07-16 12:49:57 UTC (rev 9397)
+++ trunk/pywikipedia/pywikibot/textlib.py 2011-07-16 16:17:18 UTC (rev 9398)
@@ -17,7 +17,6 @@
import wikipedia as pywikibot
import re
-
def unescape(s):
"""Replace escaped HTML-special characters by their originals"""
if '&' not in s:
@@ -877,212 +876,3 @@
# Add it to the result
result.append((name, params))
return result
-
-""" Various i18n functions for the internal translation system
-"""
-# Languages to use for comment text after the actual language but before
-# en:. For example, if for language 'xx', you want the preference of
-# languages to be:
-# xx:, then fr:, then ru:, then en:
-# you let altlang return ['fr','ru'].
-# This code is used by translate() below.
-
-def _altlang(code):
- """Define fallback languages for particular languages.
-
- If no translation is available to a specified language, translate() will
- try each of the specified fallback languages, in order, until it finds
- one with a translation, with 'en' and '_default' as a last resort.
-
- For example, if for language 'xx', you want the preference of languages
- to be: xx > fr > ru > en, you let altlang return ['fr', 'ru'].
- """
- #Amharic
- if code in ['aa', 'om']:
- return ['am']
- #Arab
- if code in ['arc', 'arz']:
- return ['ar']
- if code == 'kab':
- return ['ar', 'fr']
- #Bulgarian
- if code in ['cu', 'mk']:
- return ['bg', 'sr', 'sh']
- #Czech
- if code in ['cs', 'sk']:
- return ['cs', 'sk']
- #German
- if code in ['bar', 'frr', 'ksh', 'pdc', 'pfl']:
- return ['de']
- if code == 'lb':
- return ['de', 'fr']
- if code == 'als':
- return ['gsw', 'de']
- if code == 'nds':
- return ['nds-nl', 'de']
- if code in ['dsb', 'hsb']:
- return ['hsb', 'dsb', 'de']
- if code == 'rm':
- return ['de', 'it']
- if code =='stq':
- return ['nds', 'de']
- #Greek
- if code == 'pnt':
- return ['el']
- #Esperanto
- if code in ['io', 'nov']:
- return ['eo']
- #Spanish
- if code in ['an', 'ast', 'ay', 'ca', 'ext', 'lad', 'nah', 'nv', 'qu']:
- return ['es']
- if code in ['gl', 'gn']:
- return ['es', 'pt']
- if code == ['eu']:
- return ['es', 'fr']
- if code in ['bcl', 'cbk-zam', 'ceb', 'ilo', 'pag', 'pam', 'tl', 'war']:
- return ['es', 'tl']
- #Estonian
- if code == 'fiu-vro':
- return ['et']
- #Latvian
- if code == 'ltg':
- return ['lv']
- #Persian (Farsi)
- if code in ['glk', 'mzn']:
- return ['ar']
- #French
- if code in ['bm', 'br', 'ht', 'kab', 'kg', 'ln', 'mg', 'nrm', 'oc',
- 'pcd', 'rw', 'sg', 'ty', 'wa']:
- return ['fr']
- if code == 'co':
- return ['fr', 'it']
- #Hindi
- if code in ['bh', 'pi', 'sa']:
- return ['hi']
- if code in ['ne', 'new']:
- return ['ne', 'new', 'hi']
- #Indonesian and Malay
- if code in ['ace', 'bug', 'bjn', 'id', 'jv', 'ms', 'su']:
- return ['id', 'ms', 'jv']
- if code == 'map-bms':
- return ['jv', 'id', 'ms']
- #Inuit languages
- if code in ['ik', 'iu']:
- return ['iu', 'kl']
- if code == 'kl':
- return ['iu', 'da', 'no']
- #Italian
- if code in ['eml', 'fur', 'lij', 'lmo', 'nap', 'pms', 'roa-tara', 'sc',
- 'scn', 'vec']:
- return ['it']
- if code == 'frp':
- return ['it', 'fr']
- #Lithuanian
- if code in ['bat-smg', 'ltg']:
- return ['lt']
- #Dutch
- if code in ['fy', 'li', 'pap', 'srn', 'vls', 'zea']:
- return ['nl']
- if code == ['nds-nl']:
- return ['nds', 'nl']
- #Polish
- if code in ['csb', 'szl']:
- return ['pl']
- #Portuguese
- if code in ['fab', 'mwl', 'tet']:
- return ['pt']
- #Romanian
- if code in ['mo', 'roa-rup']:
- return ['ro']
- #Russian and Belarusian
- if code in ['ab', 'av', 'ba', 'bxr', 'ce', 'cv', 'kbd', 'kk', 'koi', 'ky',
- 'lbe', 'mdf', 'mhr', 'mrj', 'myv', 'os', 'rue', 'sah', 'tg',
- 'udm', 'uk', 'xal']:
- return ['ru']
- if code == 'tt':
- return ['tt-cyrl', 'ru']
- if code in ['be', 'be-x-old']:
- return ['be', 'be-x-old', 'ru']
- if code == 'kaa':
- return ['uz', 'ru']
- #Serbocroatian
- if code in ['bs', 'hr', 'sh',]:
- return ['sh', 'hr', 'bs', 'sr', 'sr-el']
- if code == 'sr':
- return ['sr-el', 'sh', 'hr', 'bs']
- #Turkish and Kurdish
- if code in ['diq', 'ku']:
- return ['ku', 'ku-latn', 'tr']
- if code == 'gag':
- return ['tr']
- if code == 'ckb':
- return ['ku', 'ar']
- #Chinese
- if code in ['minnan', 'zh', 'zh-classical', 'zh-min-nan', 'zh-tw',
- 'zh-hans', 'zh-hant']:
- return ['zh', 'zh-tw', 'zh-cn', 'zh-classical']
- if code in ['cdo', 'gan', 'hak', 'ii', 'wuu', 'za', 'zh-cdo',
- 'zh-classical', 'zh-cn', 'zh-yue']:
- return ['zh', 'zh-cn', 'zh-tw', 'zh-classical']
- #Scandinavian languages
- if code in ['da', 'sv']:
- return ['da', 'no', 'nb', 'sv', 'nn']
- if code in ['fo', 'is']:
- return ['da', 'no', 'nb', 'nn', 'sv']
- if code == 'nn':
- return ['no', 'nb', 'sv', 'da']
- if code in ['nb', 'no']:
- return ['no', 'nb', 'da', 'nn', 'sv']
- if code == 'se':
- return ['sv', 'no', 'nb', 'nn', 'fi']
- #Other languages
- if code in ['bi', 'tpi']:
- return ['bi', 'tpi']
- if code == 'yi':
- return ['he', 'de']
- if code in ['ia', 'ie']:
- return ['ia', 'la', 'it', 'fr', 'es']
- if code == 'xmf':
- return ['ka']
- #Default value
- return []
-
-def translate(code, xdict):
- """Return the most appropriate translation from a translation dict.
-
- Given a language code and a dictionary, returns the dictionary's value for
- key 'code' if this key exists; otherwise tries to return a value for an
- alternative language that is most applicable to use on the Wikipedia in
- language 'code'.
-
- The language itself is always checked first, then languages that
- have been defined to be alternatives, and finally English. If none of
- the options gives result, we just take the first language in the
- list.
-
- """
- # If a site is given instead of a code, use its language
- if hasattr(code, 'lang'):
- code = code.lang
-
- # If xdict attribute is wikipedia, define the xdite had multiple projects
- if 'wikipedia' in xdict:
- if pywikibot.default_family in xdict:
- xdict = xdict[pywikibot.default_family]
- else:
- xdict = xdict['wikipedia']
-
- if type(xdict) != dict:
- return xdict
-
- if code in xdict:
- return xdict[code]
- for alt in _altlang(code):
- if alt in xdict:
- return xdict[alt]
- if '_default' in xdict:
- return xdict['_default']
- elif 'en' in xdict:
- return xdict['en']
- return xdict.values()[0]
-