A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009, 2010, 2011 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from earwigbot.wiki.page import Page
  23. class Category(Page):
  24. """
  25. EarwigBot's Wiki Toolset: Category Class
  26. Represents a Category on a given Site, a subclass of Page. Provides
  27. additional methods, but Page's own methods should work fine on Category
  28. objects. Site.get_page() will return a Category instead of a Page if the
  29. given title is in the category namespace; get_category() is shorthand,
  30. because it accepts category names without the namespace prefix.
  31. Public methods:
  32. members -- returns a list of page titles in the category
  33. """
  34. def __repr__(self):
  35. """Returns the canonical string representation of the Category."""
  36. res = "Category(title={0!r}, follow_redirects={1!r}, site={2!r})"
  37. return res.format(self._title, self._follow_redirects, self._site)
  38. def __str__(self):
  39. """Returns a nice string representation of the Category."""
  40. return '<Category "{0}" of {1}>'.format(self.title(), str(self._site))
  41. def members(self, limit=50, use_sql=False):
  42. """Returns a list of page titles in the category.
  43. If `limit` is provided, we will provide this many titles, or less if
  44. the category is too small. `limit` defaults to 50; normal users can go
  45. up to 500, and bots can go up to 5,000 on a single API query.
  46. If `use_sql` is True, we will use a SQL query instead of the API. The
  47. limit argument will be ignored, and pages will be returned as tuples
  48. of (title, pageid) instead of just titles.
  49. """
  50. if use_sql:
  51. query = """SELECT page_title, page_namespace, page_id FROM page
  52. JOIN categorylinks ON page_id = cl_from
  53. WHERE cl_to = ?"""
  54. title = self.title().replace(" ", "_").split(":", 1)[1]
  55. result = self._site.sql_query(query, (title,))
  56. members = []
  57. for row in result:
  58. body = row[0].replace("_", " ")
  59. namespace = self._site.namespace_id_to_name(row[1])
  60. if namespace:
  61. title = ":".join((str(namespace), body))
  62. else: # Avoid doing a silly (albeit valid) ":Pagename" thing
  63. title = body
  64. members.append((title, row[2]))
  65. return members
  66. else:
  67. params = {"action": "query", "list": "categorymembers",
  68. "cmlimit": limit, "cmtitle": self._title}
  69. result = self._site._api_query(params)
  70. members = result['query']['categorymembers']
  71. return [member["title"] for member in members]