2 # wikiupload --- Put a wiki page on an Oddmuse wiki
4 # Copyright (C) 2004 Jorgen Schaefer <forcer@forcix.cx>
5 # Copyright (C) 2005 Alex Schroeder <alex@emacswiki.org>
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.
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.
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
22 # $Id: wikiupload,v 1.8 2010/02/08 13:40:59 as Exp $
24 import mimetypes
, httplib
, urllib
, re
, urlparse
, sys
, getopt
34 opts
, args
= getopt
.getopt(sys
.argv
[1:],
36 ["help", "summary=", "user=", "password=", "type=", "minor-edit="])
37 except getopt
.GetoptError
:
44 if opt
in ("-h", "--help"):
46 if opt
in ("-s", "--summary"):
48 if opt
in ("-u", "--user"):
50 if opt
in ("-p", "--password"):
52 if opt
in ("-t", "--type"):
54 if opt
in ("-m", "--minor-edit"):
56 wikiput(args
[1], args
[0], summary
=summary
, username
=username
, password
=password
,
57 type=type, recent_edit
=recent_edit
)
60 """Display the usage information for this script.
63 out -- The file descriptor where to write the info.
65 out
.write("Usage: wikiupload [OPTIONS] file wikipage\n"
66 "Post the data in the file onto the wikipage described by wikipage.\n"
68 "Example: wikiupload foaf.rdf http://your.domain.here/cgi-bin/wiki.pl/FOAF\n"
71 " -h --help Display this help\n"
72 " -s --summary=S The summary line (default: upload)\n"
73 " -u --user=U The username to use (default: none)\n"
74 " -p --password=P The password to use (default: none)\n"
75 " -t --type=T The MIME type to use (default: guessed)\n"
76 " -m --minor-edit=B Whether this is a minor edit (default: no)\n")
78 def wikiput(where
, filename
,
79 summary
="upload", username
="", password
="", type="", recent_edit
="no"):
80 (host
, path
, title
) = parse_wiki_location(where
)
81 content
= open(filename
, "rb").read()
82 files
= [['file', filename
, content
]]
83 params
= [[ 'title', title
],
84 [ 'summary', summary
],
85 [ 'username', username
],
88 [ 'recent_edit', recent_edit
]]
89 (content_type
, body
) = encode_multipart_formdata(params
, type, files
);
90 headers
= {'Content-Type': content_type
}
91 conn
= httplib
.HTTPConnection(host
)
92 conn
.request("POST", path
, body
, headers
)
93 response
= conn
.getresponse()
94 data
= response
.read()
96 if response
.status
!= 302:
97 print "Uploading", filename
, get_content_type(filename
, type)
98 print response
.status
, response
.reason
100 if response
.status
== 415:
101 print "Check your MIME types in one of these files:"
102 print mimetypes
.knownfiles
105 def encode_multipart_formdata(fields
, type, files
):
107 fields is a sequence of (name, value) elements for regular form fields.
108 files is a sequence of (name, filename, value) elements for data to be uploaded as files.
109 Return (content_type, body) ready for httplib.HTTP instance
111 BOUNDARY
= '----------ThIs_Is_tHe_bouNdaRY_$'
114 for (key
, value
) in fields
:
115 L
.append('--' + BOUNDARY
)
116 L
.append('Content-Disposition: form-data; name="%s"' % key
)
119 for (key
, filename
, value
) in files
:
120 L
.append('--' + BOUNDARY
)
121 L
.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key
, filename
))
122 L
.append('Content-Type: %s' % get_content_type(filename
, type))
125 L
.append('--' + BOUNDARY
+ '--')
128 content_type
= 'multipart/form-data; boundary=%s' % BOUNDARY
129 return content_type
, body
131 def get_content_type(filename
, type):
132 return type or mimetypes
.guess_type(filename
)[0] or 'application/octet-stream'
134 def parse_wiki_location(where
):
135 """Return a tuple of host, path and page name for the wiki page
138 (scheme
, host
, path
, params
, query
, fragment
) = urlparse
.urlparse(where
)
140 list = path
.split("/")
142 path
= "/".join(list)
143 return (host
, path
+params
, query
)
145 if __name__
== "__main__":