wikiadd 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. # wikiadd --- Add a comment to a wiki page on an Oddmuse wiki
  3. #
  4. # Copyright (C) 2004 Jorgen Schaefer <forcer@forcix.cx>
  5. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. # 02111-1307, USA.
  21. import httplib, urllib, re, urlparse, sys, getopt
  22. from time import time
  23. def main():
  24. """The main method of the wikiput script."""
  25. summary="*"
  26. recent_edit="no"
  27. try:
  28. opts, args = getopt.getopt(sys.argv[1:],
  29. "ht:s:m:",
  30. ["help", "summary=", "minor-edit="])
  31. except getopt.GetoptError:
  32. usage(sys.stderr)
  33. sys.exit(1)
  34. if len(args) != 1:
  35. usage(sys.stderr)
  36. sys.exit(1)
  37. for opt, arg in opts:
  38. if opt in ("-h", "--help"):
  39. usage(sys.stdout)
  40. if opt in ("-s", "--summary"):
  41. summary = arg
  42. if opt in ("-m", "--minor-edit"):
  43. recent_edit="yes"
  44. text = sys.stdin.read()
  45. wikiput(args[0], text, summary=summary, recent_edit=recent_edit)
  46. def usage(out):
  47. """Display the usage information for this script.
  48. Options:
  49. out -- The file descriptor where to write the info.
  50. """
  51. out.write("Usage: wikiadd [OPTIONS] wikipage\n"
  52. "Post the data on stdin on the wikipage described by wikipage\n"
  53. "as a comment.\n"
  54. "\n"
  55. "Options:\n"
  56. " -h --help Display this help\n"
  57. " -s --summary=S The summary line.\n"
  58. " -m --minor-edit=B Whether this is a minor edit.\n")
  59. def wikiput(where, text,
  60. summary="*", recent_edit="no"):
  61. """Submit some text to a wiki page.
  62. Keyword arguments:
  63. where -- A description of the wiki location
  64. text -- The text to submit
  65. summary -- The summary line to use (default *)
  66. recent_edit -- Wether this is a minor edit (default no)
  67. """
  68. (host, path, title) = parse_wiki_location(where)
  69. params = urllib.urlencode({'title': title,
  70. 'aftertext': text,
  71. 'summary': summary,
  72. 'recent_edit': recent_edit})
  73. headers = {'Content-Type': "application/x-www-form-urlencoded"}
  74. conn = httplib.HTTPConnection(host)
  75. conn.request("POST", path, params, headers)
  76. response = conn.getresponse()
  77. data = response.read()
  78. conn.close()
  79. if response.status != 302:
  80. raise RuntimeError, "We weren't redirected - something went wrong!"
  81. def parse_wiki_location(where):
  82. """Return a tuple of host, path and page name for the wiki page
  83. WHERE.
  84. """
  85. (scheme, host, path, params, query, fragment) = urlparse.urlparse(where)
  86. return (host, path+params, query)
  87. if __name__ == "__main__":
  88. main()