Flare to MediaWiki to Flare (part 3, Placeholder Flare Topics)

In the previous post about this effort, I discussed the MediaWiki API and how to create a Flare TOC from the response from an API call. The same approach is possible for the Flare topics. But since the wiki page content is not maintained as XML, the conversion is more complicated from wiki page to Flare topic. That is why this post is called Placeholder Flare Topics. Converting the wiki markup will be tackled in a subsequent post.

It may also be a good idea at this point to note some technical debt in the work so far. Although an API call was made and a TOC was created, there are a few other considerations depending on the MediaWiki instance. Firstly, some calls may require authentication. The example so far did not because I did not require a password for myself as the administrator on my local instance. But down the road, the issue of authentication will come up. But the issue has been encountered by many others and handled before, so I feel it is okay to leave that one for later. Secondly, some calls to the MediaWiki API may have limitations on the number of items returned. This is this case for the list of pages call from the previous post. If the wiki becomes large enough, that will have to be addressed.

Creating a Flare topic can be accomplished in the same way as the creating the TOC. Determining what topics to create can be accomplished by cycling through the TocEntry elements in the TOC and using Linq to XML to create one. Populating the topic with content can be accomplished through a MediaWiki API call to retrieve the content and then parsing the wiki markup to create XML. This post’s sample will only show how to create the topics and touch on the API call. For now, I haven’t broken anything out into separate procedures so that you can see the similarities between the TOC creation and the topic creation. Here is the updated (and only partially checked) code:

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Sub Main()
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Try
            request = DirectCast(WebRequest.Create("http://localhost/mediawiki-1.19.1/api.php?format=xml&action=query&generator=allpages"), HttpWebRequest)
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            Dim WikiPagesXML As XDocument = XDocument.Load(response.GetResponseStream())
            Dim FlareTOC As XDocument =
<?xml version="1.0" encoding="utf-8"?>
<CatapultToc Version="1"/>
            For Each el In WikiPagesXML.Descendants
                If el.NodeType = XmlNodeType.Element And el.Name = "page" Then
                    FlareTOC.Root.Add(<TocEntry Title=<%= el.Attribute("title").Value %> Link=<%= el.Attribute("title").Value & ".htm" %>/>)
                End If
            Next
            FlareTOC.Save("C:\WikiToFlare\Project\TOCs\WikiPages.fltoc")
            For Each el2 In FlareTOC.Descendants
                If el2.NodeType = XmlNodeType.Element And el2.Name = "TocEntry" Then
                    Dim fixedCallString As String = el2.Attribute("Title").Value.ToString.Replace(" ", "%20")
                    Dim request2 = DirectCast(WebRequest.Create("http://localhost/mediawiki-1.19.1/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=" & fixedCallString), HttpWebRequest)
                    Dim response2 = DirectCast(request2.GetResponse(), HttpWebResponse)
                    Dim TopicContent As XElement = _
                        <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">
                            <head>
                                <h1><%= el2.Attribute("Title").Value %></h1>
                            </head>
                            <body>
                                <pre><%= response2.ToString %></pre>
                            </body>
                        </html>
                    Dim FlareTopic As XDocument = _
                        <?xml version="1.0" encoding="utf-8"?>
                        <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">
                        </html>
                    FlareTopic.Save("C:\WikiToFlare\Content\" & el2.Attribute("Title").Value & ".htm")
                End If
            Next
        Finally
            If Not response Is Nothing Then response.Close()
        End Try
    End Sub
End Module

Leave a comment

Your email address will not be published.

HTML tags are not allowed.

250,812 Spambots Blocked by Simple Comments