Was this page helpful?

Programming the MindTouch API with VBScript

    Table of contents
    No headers

    Here is a sample script (thanks to JGable) which you can use to add content to the wiki with VBScript.

    Save the attached file as upload.vbs and run it like so:

    cscript upload.vbs /user:foo /pw:bar /server:my.wik.is /path:"Page/Path" /pagename:New /title:"New title" 
        /pagefile:mypage.html  image1.gif image2.gif

     

    ' 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  <parameters>
    ' 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


     

    With the 9.02 release the mime type must be set correctly for images that are uploaded.  The following upload sub does that.  You may want to expand the case statement to cover more file types. 

     

    ' ##############################################################################################
    ' upload a file attachment to a page
    sub uploadFile(cookiedata, server, path, pagename, title, filename)
        'Read the binary File
        dim inStream, pathpage, URIFilename, xmlreq, datafile, fsobject, nopathname, fileExtension, mimeType
        const adTypeText=2
        const adTypeBinary=1
        set inStream=WScript.CreateObject("ADODB.Stream")
        inStream.Open
        inStream.type=adTypeBinary
        inStream.LoadFromFile(filename)
        
        datafile=inStream.Read()
        inStream.Close()
        
        'Get the name without a path
        set fsobject= CreateObject("Scripting.FileSystemObject")
        nopathname = fsobject.GetFileName(filename)
        
        ' Get the mime type from the extension
        fileExtension = ucase(fsobject.GetExtensionName(filename))
                select case fileExtension
                case "GIF"
                            mimeType = "image/gif"
                case "JPG"
                            mimeType = "image/jpeg"
                case "PNG"
                            mimeType = "image/png"
                case Else
                            mimeType = "text/plain"
                end select
        
        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)
     
        URIFilename = URIEncode(nopathname)
        URIFilename = URIEncode(URIFilename)
     
        set xmlreq = CreateObject("MSXML2.XMLHTTP")
        'Set the upload file command
        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", mimeType
        'Send the page...Similar to Curl -H
        xmlreq.send(datafile)
        
        if xmlreq.status <> 200 then
            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
    Was this page helpful?
    Tag page

    Files 1

    FileVersionSizeModified 
    Viewing 1 of 1 comments: view all
    If anyone has any luck converting this script over to VB.NET please let me know.
    Posted 08:55, 15 Apr 2009
    Viewing 1 of 1 comments: view all
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by