http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9196
Revision: 9196 Author: jayvdb Date: 2011-04-24 05:40:59 +0000 (Sun, 24 Apr 2011) Log Message: ----------- Allow lists of Page and User objects to be interogated
Modified Paths: -------------- trunk/pywikipedia/query.py trunk/pywikipedia/tests/test_query.py
Modified: trunk/pywikipedia/query.py =================================================================== --- trunk/pywikipedia/query.py 2011-04-24 04:23:12 UTC (rev 9195) +++ trunk/pywikipedia/query.py 2011-04-24 05:40:59 UTC (rev 9196) @@ -263,10 +263,21 @@
encList = '' # items may not have one symbol - '|' - for l in list: - if type(l) == str and u'|' in l: - raise wikipedia.Error("item '%s' contains '|' symbol" % l ) - encList += ToUtf8(l) + u'|' + for item in list: + if isinstance(item,basestring): + if u'|' in item: + raise wikipedia.Error(u"item '%s' contains '|' symbol" % item ) + encList += ToUtf8(item) + u'|' + elif isinstance(item,wikipedia.Page): + encList += ToUtf8(item.title()) + u'|' + elif item.__class__.__name__ == 'User': + # delay loading this until it is needed + import userlib + encList += ToUtf8(item.name()) + u'|' + else: + raise wikipedia.Error(u'unknown item class %s' % item.__class__.__name__) + + # strip trailing '|' before returning return encList[:-1]
def ToUtf8(s):
Modified: trunk/pywikipedia/tests/test_query.py =================================================================== --- trunk/pywikipedia/tests/test_query.py 2011-04-24 04:23:12 UTC (rev 9195) +++ trunk/pywikipedia/tests/test_query.py 2011-04-24 05:40:59 UTC (rev 9196) @@ -7,6 +7,8 @@ import unittest import tests.test_pywiki
+import wikipedia as pywikibot +import catlib, userlib import query
@@ -74,5 +76,72 @@ ]} self.assertEqualQueryResult(params, expectedresult)
+ def test_titles_Page(self): + params = { + 'action': 'query', + 'list': 'users', + 'usprop': ['registration'], + 'ususers': [pywikibot.Page(self.site, u'Example'), + pywikibot.Page(self.site, u'Example2')], + } + expectedresult = {u'users': [ + { + u'userid': 215131, + u'name': u'Example', + u'registration': u'2005-03-19T00:17:19Z' + }, + { + u'userid': 5176706, + u'name': u'Example2', + u'registration': u'2007-08-26T02:13:33Z' + }, + ]} + self.assertEqualQueryResult(params, expectedresult) + + def test_titles_User(self): + params = { + 'action': 'query', + 'list': 'users', + 'usprop': ['registration'], + 'ususers': [userlib.User(self.site, u'Example'), + userlib.User(self.site, u'Example2')], + } + expectedresult = {u'users': [ + { + u'userid': 215131, + u'name': u'Example', + u'registration': u'2005-03-19T00:17:19Z' + }, + { + u'userid': 5176706, + u'name': u'Example2', + u'registration': u'2007-08-26T02:13:33Z' + }, + ]} + self.assertEqualQueryResult(params, expectedresult) + + def test_titles_Category(self): + params = { + 'action': 'query', + 'prop': 'revisions', + 'rvprop': ['ids', 'timestamp', 'user'], + 'rvdir': 'newer', + 'rvlimit': 1, + 'titles': [catlib.Category(self.site, u'Category:Categories')], + } + expectedresult = {u'pages': {u'794823': + { + u'ns': 14, + u'pageid': 794823, + u'revisions': [{ + u'revid': 4494485, + u'user': u'SEWilco', + u'timestamp': u'2004-07-07T18:45:50Z', + }], + u'title': u'Category:Categories', + }, + }} + self.assertEqualQueryResult(params, expectedresult) + if __name__ == "__main__": unittest.main()
pywikipedia-svn@lists.wikimedia.org