Automatic mediawiki page import powershell script
If you are using a mediawiki for internal documentation or as a team knowledge base you might want to automate page creation from a shell. I wrote a powershell script which offers a simple function to create a new wiki page with a title, text and a category as parameters. It works with HTTP - so it should work from most of your servers without firewall problems. I use HttpWebRequest instead of Webclient in the powershell script because you need to modify the "filename" attribute. I couldn't figure out how to change that with WebClient.UploadFile() method ... so I wrote a fully customizable HttpWebRequest with multipart/form-data ....
Mediwiki offers a Special page where you can import pages. But this is not a webservice API or something like that - it's an html form with an xml file upload. So you have to create a mediawiki-compliant xml file and fake an html form submission with the correct parameters (thats why I have this strange parameters in $wikiURL - without them it won't work).
Contents |
Script code
#################################################################################### ## Author: OM / slash4.de ## Version: v0.1 - 11/2009 ## ## Copyright (C) 2009 slash4.de ## Permission is granted to copy, distribute and/or modify this document ## under the terms of the GNU Free Documentation License, Version 1.3 ## or any later version published by the Free Software Foundation; ## with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. ## A copy of the license is included in the section entitled "GNU ## Free Documentation License". #################################################################################### <# .SYNOPSIS The function new-WikiPage imports a page to a mediawiki. It names the page with $pageTitle and puts $text in $category. .DESCRIPTION It creates a local temporary XML file, which is uploaded to the wiki import page via HTTP-File-Upload. You can also specify another wiki URL. .NOTES Author : om ( at ) slash4.de Requires : PowerShell V2 (CTP3) Tested in : PowerShell V2 (CTP3) .LINK Published on: http://slash4.de/tutorials/Automatic_mediawiki_page_import_powershell_script .EXAMPLE PS: new-WikiPage "My Page Title" "== My Headline == `n* my iteration1 `n*my iteration2 `n infobox with blank after newline " "My Category" #> function new-WikiPage { param ( [string] $pageName, [string] $text, [string] $category, [string] $wikiURL = "http://mydomain.com/myMediaWiki/index.php?title=Special:Import&action=submit&source=upload&editToken=12345" ) $url = $wikiURL # XML Code that will be uploaded to mediawiki $wikiXML = '<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd" version="0.3" xml:lang="de"><siteinfo><sitename>slash4 Wiki</sitename><base>http://mydomain.com/myMediaWiki/index.php/Main_Page</base><case>first-letter</case></siteinfo> <page><title>' + $pageName + '</title><revision> <text xml:space="preserve">' + $text + ' [[Category: ' + $category + ']]</text></revision></page></mediawiki>' # Create temp xml-file $filename = (gc env:\temp) + "\tmp_wikitemplate.xml" $wikiXML > $filename # HTTP Protocol helper $boundary = "----------------------------" + [system.DateTime]::Now.Ticks.ToString("x") # Create multipart/form-data file upload HTTP POST Request $req = [system.Net.HttpWebRequest]::Create($url) $req.ContentType = "multipart/form-data; boundary=" + $boundary $req.Method = "POST" $req.KeepAlive = $true $req.Credentials = [System.Net.CredentialCache]::DefaultCredentials $memStream = new-object System.IO.MemoryStream $boundarybytes = [System.Text.Encoding]::ASCII.GetBytes("`r`n--" + $boundary + "`r`n") $header = "`r`n--" + $boundary + "`r`n" + "Content-Disposition: form-data; name=`"xmlimport`";filename=`"wikitext.xml`"`r`n Content-Type: application/octet-stream`r`n`r`n" $headerbytes = [System.Text.Encoding]::UTF8.GetBytes($header) # send http header to webserver $memStream.Write($headerbytes,0,$headerbytes.Length) # open xml file to upload $fileStream = new-object System.IO.FileStream( $filename, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read ) $buffer = new-object byte[] 1024 $bytesRead = 0; while ( ($bytesRead = $fileStream.Read($buffer, 0, $buffer.Length)) -ne 0 ) { $memStream.Write($buffer, 0, $bytesRead) } # send file in http request $memStream.Write($boundarybytes, 0, $boundarybytes.Length) $fileStream.Close() $req.ContentLength = $memStream.Length $requestStream = $req.GetRequestStream() $memStream.Position = 0 $tempBuffer = New-Object byte[] $memStream.Length $memStream.Read($tempBuffer, 0, $tempBuffer.Length) $memStream.Close() $requestStream.Write($tempBuffer, 0, $tempBuffer.Length) $requestStream.Close() # get response from webserver $response = $req.GetResponse() $stream = $response.GetResponseStream() $reader = new-object System.IO.StreamReader($stream) # this will output http response to your shell ... assign it to a variable or redirect to a file to get rid of this. but its usefull for debugging the wiki response if something doesnt work. $reader.ReadToEnd() $response.close() $req = $null $response = $null }
Usage
Users / Security
If your wiki is accessible over internet you should setup an import user and restrict access to your Import page for this user only. Be careful because opening this page for anonymous usage could allow others to automatically import pages to your wiki.
You will have to modify your LocalSettings.php in order to grant the rights for your user. Please read the mediawiki documentation to find a suitable security setting for your special environment ...
$wgGroupPermissions['YourImportUserGroup']['import'] = true; $wgGroupPermissions['YourImportUserGroup']['importupload'] = true;
Command Syntax
You can just copy and paste the source in your powershell. Or you save it as wiki.ps1 and then "dot source it"
PS: . .\wiki.ps1
Then run the following command in a powershell (v2):
new-WikiPage "My Page Title" "== My Headline == `n* my iteration1 `n*my iteration2 `n infobox with blank after newline " "My Category"
The " `n " in the command is translated into a "newline".
XML Syntax
This will first create an XML file like that and replaces the parameters for $pageName, $text and $category.
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd" version="0.3" xml:lang="de"> <siteinfo> <sitename>slash4 Wiki</sitename> <base>http://mydomain.com/myMediaWiki/index.php/Main_Page</base> <case>first-letter</case> </siteinfo> <page> <title>' + $pageName + '</title> <revision> <text xml:space="preserve">' + $text + ' [[Category: ' + $category + ']]</text> </revision> </page> </mediawiki>
- Then it will upload this xml file to your $wikiURL like http://mydomain.com/myMediaWiki/index.php?title=Special:Import ...
- The resulting wiki page will look like this:...
My Headline
- my iteration1
- my iteration2
infobox with blank after newline
[Category: My Category]
Was this helpful for you? ... thanks for your comments in your blog ... or leave a comment in our blog