wikiadd 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (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, see <http://www.gnu.org/licenses/>.
  19. import httplib, urllib, re, urlparse, sys, getopt
  20. from time import time
  21. def main():
  22. """The main method of the wikiput script."""
  23. summary="*"
  24. recent_edit="no"
  25. try:
  26. opts, args = getopt.getopt(sys.argv[1:],
  27. "ht:s:m:",
  28. ["help", "summary=", "minor-edit="])
  29. except getopt.GetoptError:
  30. usage(sys.stderr)
  31. sys.exit(1)
  32. if len(args) != 1:
  33. usage(sys.stderr)
  34. sys.exit(1)
  35. for opt, arg in opts:
  36. if opt in ("-h", "--help"):
  37. usage(sys.stdout)
  38. if opt in ("-s", "--summary"):
  39. summary = arg
  40. if opt in ("-m", "--minor-edit"):
  41. recent_edit="yes"
  42. text = sys.stdin.read()
  43. wikiput(args[0], text, summary=summary, recent_edit=recent_edit)
  44. def usage(out):
  45. """Display the usage information for this script.
  46. Options:
  47. out -- The file descriptor where to write the info.
  48. """
  49. out.write("Usage: wikiadd [OPTIONS] wikipage\n"
  50. "Post the data on stdin on the wikipage described by wikipage\n"
  51. "as a comment.\n"
  52. "\n"
  53. "Options:\n"
  54. " -h --help Display this help\n"
  55. " -s --summary=S The summary line.\n"
  56. " -m --minor-edit=B Whether this is a minor edit.\n")
  57. def wikiput(where, text,
  58. summary="*", recent_edit="no"):
  59. """Submit some text to a wiki page.
  60. Keyword arguments:
  61. where -- A description of the wiki location
  62. text -- The text to submit
  63. summary -- The summary line to use (default *)
  64. recent_edit -- Wether this is a minor edit (default no)
  65. """
  66. (host, path, title) = parse_wiki_location(where)
  67. params = urllib.urlencode({'title': title,
  68. 'aftertext': text,
  69. 'summary': summary,
  70. 'recent_edit': recent_edit})
  71. headers = {'Content-Type': "application/x-www-form-urlencoded"}
  72. conn = httplib.HTTPConnection(host)
  73. conn.request("POST", path, params, headers)
  74. response = conn.getresponse()
  75. data = response.read()
  76. conn.close()
  77. if response.status != 302:
  78. raise RuntimeError, "We weren't redirected - something went wrong!"
  79. def parse_wiki_location(where):
  80. """Return a tuple of host, path and page name for the wiki page
  81. WHERE.
  82. """
  83. (scheme, host, path, params, query, fragment) = urlparse.urlparse(where)
  84. return (host, path+params, query)
  85. if __name__ == "__main__":
  86. main()