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.

101 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 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. __all__ = ["Category"]
  24. class Category(Page):
  25. """
  26. EarwigBot's Wiki Toolset: Category Class
  27. Represents a Category on a given Site, a subclass of Page. Provides
  28. additional methods, but Page's own methods should work fine on Category
  29. objects. Site.get_page() will return a Category instead of a Page if the
  30. given title is in the category namespace; get_category() is shorthand,
  31. because it accepts category names without the namespace prefix.
  32. Public methods:
  33. get_members -- returns a list of page titles in the category
  34. """
  35. def __repr__(self):
  36. """Returns the canonical string representation of the Category."""
  37. res = "Category(title={0!r}, follow_redirects={1!r}, site={2!r})"
  38. return res.format(self._title, self._follow_redirects, self._site)
  39. def __str__(self):
  40. """Returns a nice string representation of the Category."""
  41. return '<Category "{0}" of {1}>'.format(self.title(), str(self._site))
  42. def _get_members_via_sql(self, limit):
  43. """Return a list of tuples of (title, pageid) in the category."""
  44. query = """SELECT page_title, page_namespace, page_id FROM page
  45. JOIN categorylinks ON page_id = cl_from
  46. WHERE cl_to = ?"""
  47. title = self.title().replace(" ", "_").split(":", 1)[1]
  48. if limit:
  49. query += " LIMIT ?"
  50. result = self._site.sql_query(query, (title, limit))
  51. else:
  52. result = self._site.sql_query(query, (title,))
  53. members = []
  54. for row in result:
  55. base = row[0].replace("_", " ").decode("utf8")
  56. namespace = self._site.namespace_id_to_name(row[1])
  57. if namespace:
  58. title = u":".join((namespace, base))
  59. else: # Avoid doing a silly (albeit valid) ":Pagename" thing
  60. title = base
  61. members.append((title, row[2]))
  62. return members
  63. def _get_members_via_api(self, limit):
  64. """Return a list of page titles in the category using the API."""
  65. params = {"action": "query", "list": "categorymembers",
  66. "cmlimit": limit, "cmtitle": self._title}
  67. if not limit:
  68. params["cmlimit"] = 50 # Default value
  69. result = self._site.api_query(**params)
  70. members = result['query']['categorymembers']
  71. return [member["title"] for member in members]
  72. def get_members(self, use_sql=False, limit=None):
  73. """Returns a list of page titles in the category.
  74. If `use_sql` is True, we will use a SQL query instead of the API. Pages
  75. will be returned as tuples of (title, pageid) instead of just titles.
  76. If `limit` is provided, we will provide this many titles, or less if
  77. the category is smaller. `limit` defaults to 50 for API queries; normal
  78. users can go up to 500, and bots can go up to 5,000 on a single API
  79. query. If we're using SQL, the limit is None by default (returning all
  80. pages in the category), but an arbitrary limit can still be chosen.
  81. """
  82. if use_sql:
  83. return self._get_members_via_sql(limit)
  84. else:
  85. return self._get_members_via_api(limit)