Migrating Wiki Pages Remotely – Part 08

Note, this series starts at blogs.msdn.com/dwinter/archive/2008/06/28/migrating-wiki-pages-remotely-part-01.aspx

So there are a lot of little things to think about here. Once you start playing with this—the next one that comes to mind is… what about the images? It is a good question. I found in practice that most images would end up in the same Document Library or Images Library for any given segment of Wiki data created by a single person. However, it can vary… so I worked in something to parse where images are being used and report it back so that the admin knows they need to copy the images as well. It would be doable to automatically take care of these, but it was out of spec for what I wanted since if told what Document Libraries to grab, I could easily copy everything through explorer view to a list with the same name on the other side. At the beginning of my copy method I declared:

System.Collections.Hashtable imageLibraries = new System.Collections.Hashtable();

Here’s my code for image detection in the wiki pages:

// Try to find image tags and note their lists

MatchCollection imageMatches = Regex.Matches(sourceWikiField, "<img.*src=\"(.*)\">", RegexOptions.IgnoreCase);

foreach (Match imageMatch in imageMatches)

{

    MatchCollection srcMatches = Regex.Matches(sourceWikiField, "src=\"(.*)\"", RegexOptions.IgnoreCase);

    foreach (Match srcMatch in srcMatches)

    {

        string imageUrl = srcMatch.Value.TrimStart("src=".ToCharArray()).TrimStart("\"".ToCharArray()).TrimEnd("\"".ToCharArray());

        int lastSlash = imageUrl.LastIndexOf("/");

        string imageParentUrl = string.Empty;

        if (lastSlash > 0)

        {

            imageParentUrl = imageUrl.Substring(0, lastSlash - 1);

        }

        Trace.WriteLine("Found image: " + imageUrl);

        if (!string.IsNullOrEmpty(imageParentUrl))

      {

            if (!imageLibraries.Contains(imageParentUrl))

            {

                imageLibraries.Add(imageParentUrl, string.Empty);

                Trace.WriteLine("Added Library: " + imageParentUrl);

            }

        }

    }

}

Then at the end I just loop through the imageLibraries collection and reported it back to the admin. I thought this was slick because I only would see a distinct list of libraries and not every image in use. Take a look here:

if (imageLibraries.Count > 0)

{

    Trace.WriteLine("===============================================");

    Trace.WriteLine("The following location(s) contain images used by the coppied wikis. They should be coppied manually to new locations matching the structure from:");

    Trace.WriteLine("");

    txt_Status.Text += "===============================================\r\nThe following location(s) contain wiki images to be coppied:\r\n";

    txt_Status.Select(txt_Status.Text.Length, 0);

    txt_Status.ScrollToCaret();

    foreach (System.Collections.DictionaryEntry imageLibrary in imageLibraries)

    {

        Trace.WriteLine(imageLibrary.Key);

        txt_Status.Text += imageLibrary.Key + "\r\n";

        txt_Status.Select(txt_Status.Text.Length, 0);

        txt_Status.ScrollToCaret();

    }

    Trace.WriteLine("");

    txt_Status.Text += "\r\n";

    txt_Status.Select(txt_Status.Text.Length, 0);

    txt_Status.ScrollToCaret();

}

 

Part 09:
blogs.msdn.com/dwinter/archive/2008/06/28/migrating-wiki-pages-remotely-part-09.aspx