' This file's extension should be changed to vbs before use option explicit ' This vbscript uploads pages and files to Deki ' Syntax: cscript upload.vbs ' most parameters are passed as named values like this /user:jgable ' only values you want to override the default need to be passed. ' Any unamed parameters are images or documents that should be uploaded as attachments to the desired page. dim user, pw, server, path, pagename, title, pagefile dim cookiedata, fso, readtmp, datafile, upldFile ' set the default values user = "" pw = "" server = "wiki.developer.mindtouch.com" path = "User:Jgable/test" pagename = "uploaded" title = "Uploaded File" pagefile = "test.html" ' override the default value with the named arguments if wscript.Arguments.Named.Exists("user") then user = wscript.Arguments.Named.Item("user") if wscript.Arguments.Named.Exists("pw") then pw = wscript.Arguments.Named.Item("pw") if wscript.Arguments.Named.Exists("server") then server = wscript.Arguments.Named.Item("server") if wscript.Arguments.Named.Exists("path") then path = wscript.Arguments.Named.Item("path") if wscript.Arguments.Named.Exists("pagename") then pagename = wscript.Arguments.Named.Item("pagename") if wscript.Arguments.Named.Exists("title") then title = wscript.Arguments.Named.Item("title") if wscript.Arguments.Named.Exists("pagefile") then html = wscript.Arguments.Named.Item("pagefile") ' log into deki cookiedata = login(user, pw, server) 'upload the page if one is presented for upload if pagefile <> "" then uploadPage cookiedata, server, path, pagename, title, pagefile end if ' upload all the files passed as unanmed arguments For Each upldFile in WScript.Arguments.unnamed uploadFile cookiedata, server, path, pagename, title, upldFile Next ' ############################################################################################## ' Start of Functions and subs ' ############################################################################################## ' create a timestamp function editTime () dim rightnow rightnow = now() editTime = right("0000" & Year(rightnow),4) & _ right("00" & month(rightnow),2) & _ right("00" & day(rightnow),2) & _ right("00" & hour(rightnow),2) & _ right("00" & minute(rightnow),2) & _ right("00" & second(rightnow),2) end function ' ############################################################################################## ' This will URI Encode the value sent to it Function URIEncode(Text) Dim X, buffer, Char, Temp, Charset Charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" Text = Replace(Text, " ", " ") Text = Trim(Text) For X = 1 To Len(Text) Char = Mid(Text, X, 1) If InStr(1, Charset, Char) < 1 Then Temp = Trim(Hex(Asc(Char))) buffer = buffer & "%" & UCase(Temp) Else buffer = buffer & Char End If Next URIEncode = buffer End Function ' ############################################################################################## ' This function reads a file as binary and returns its contents function readfile(filename) dim inStream const adTypeText=2 const adTypeBinary=1 set inStream=WScript.CreateObject("ADODB.Stream") inStream.Open inStream.type=adTypeBinary inStream.LoadFromFile(filename) readfile=inStream.Read() inStream.Close() end function ' ############################################################################################## ' this function logs you into deki or exits script with an errorcode of 1 function login (username, password, dekiserver) dim message, xmlreq ' Create the authentication message message = "http://" & userName & ":" & password & "@" & dekiServer & "/@api/deki/users/authenticate" ' Create an xmlhttp object: set xmlreq = CreateObject("MSXML2.XMLHTTP") ' Opens the connection to the remote server. xmlreq.Open "POST", message, False 'Actually Sends the request and returns the data: xmlreq.send() ' See if this was successful if xmlreq.status = 200 then login = xmlreq.responsetext wscript.echo "200 Authentication successful." else wscript.echo "" wscript.echo "#####################################################################" wscript.echo " ERROR: Setting cookie failed for the following reason:" wscript.echo " Code " &xmlreq.status & " = " & xmlreq.StatusText wscript.echo "#####################################################################" wscript.quit(1) end if ' Clear the object we created set xmlreq = nothing end function ' ############################################################################################## ' upload the page sub uploadPage(cookiedata, server, path, pagename, title, filename) dim params, pathpage, xmlreq, pagecontents ' get the page file pagecontents = readfile(filename) ' put the path and pagename together if right(path, 1) = "/" then pathpage = path & pagename else pathpage = path & "/" & pagename end if 'double encode the pathpage pathpage = URIEncode(pathpage) pathpage = URIEncode(pathpage) ' Encode the title in case it has special characters title = URIEncode(title) ' Set the parameters that we will pass params = "edittime=" & editTime & "&title=" & title & "&abort=never" ' create the object set xmlreq = CreateObject("MSXML2.XMLHTTP") 'Create a new page request xmlreq.Open "POST", "http://" & server & "/@api/deki/pages/=" & pathpage & "/contents/?" & params, False 'Set the cookie data xmlreq.setrequestheader "Cookie", cookiedata 'Set our formatting xmlreq.setrequestheader "Content-Type", "text/plain" 'Send the page. xmlreq.send(pagecontents) ' See if this was successful if xmlreq.status = 200 then wscript.echo "200 Page upload of '" & pagename & "' successful." else wscript.echo "" wscript.echo "#####################################################################" wscript.echo " ERROR: Page upload failed for the following reason:" wscript.echo " Code " & xmlreq.status & " = " & xmlreq.StatusText wscript.echo " Page " & pagename wscript.echo "#####################################################################" wscript.quit(2) end if 'Clean up . set xmlreq = Nothing end sub ' ############################################################################################## ' upload a file attachment to a page sub uploadFile(cookiedata, server, path, pagename, title, filename) dim pathpage, URIFilename, xmlreq, datafile, fsobject, nopathname ' Read the file to upload datafile = readfile(filename) 'Get the name without a path set fsobject= CreateObject("Scripting.FileSystemObject") nopathname = fsobject.GetFileName(filename) set fsobject = Nothing ' put the path and pagename together if right(path, 1) = "/" then pathpage = path & pagename else pathpage = path & "/" & pagename end if 'double encode the characters in the page name pathpage = URIEncode(pathpage) pathpage = URIEncode(pathpage) ' do the same for the filename URIFilename = URIEncode(nopathname) URIFilename = URIEncode(URIFilename) set xmlreq = CreateObject("MSXML2.XMLHTTP") 'Set the upload file command wscript.echo "http://" & server & "/@api/deki/pages/=" & pathpage & "/files/="& URIFilename xmlreq.Open "PUT", "http://" & server & "/@api/deki/pages/=" & pathpage & "/files/=" & URIFilename, False 'Because of the way Microsoft works, this must be set twice 'http://support.microsoft.com/kb/234486 xmlreq.setrequestheader "Cookie", cookieData 'Set our formatting xmlreq.setrequestheader "Content-Type", "text/plain" 'Send the page. xmlreq.send(datafile) if xmlreq.status = 200 then wscript.echo "200 File upload of '" & filename & "' successful." else wscript.echo "" wscript.echo "#####################################################################" wscript.echo " ERROR: Page upload failed for the following reason:" wscript.echo " Code " & xmlreq.status & " = " & xmlreq.StatusText wscript.echo " File " & filename wscript.echo "#####################################################################" wscript.quit(2) end if set xmlreq = Nothing end sub