MediaWiki + Graphviz + Image Cards + Pagelinks

Background : working with MediaWiki 1.19.1, Graphviz 2.28.0, extension: GraphViz 0.9 on the WAMP stack (Server 2008, Apache 2.4.2, MySQL 5.5.27, PHP 5.4.5), Everything works fine and, as expected, for the basic functions of rendering a clickable image from a Graphviz chart using the GraphViz extension in MediaWiki.

Problem . Image map links are not added to the pagelinks MediaWiki table. I understand why they are not added, but it becomes a problem if it is not possible to follow links with the functionality of "What links are here."

Desired solution . While processing the chart in the GraphViz extension, I would like to use the generated .map file to then create a list of wikilinks to add to the page to get lifted by MediaWiki and added to the pagelinks table.

Read more : This GraphViz extension code:

<graphviz border='frame' format='png'>
digraph example1 {
  // define nodes
  nodeHello [
    label="I say Hello", 
    URL="Hello"
  ]
  nodeWorld [
    label="You say World!",
    URL="World"
  ]
  // link nodes
  nodeHello -> nodeWorld!
}
</graphviz>

Creates this image:

hello world graphviz diagram

And this image card code in the corresponding .map file on the server:

<map id="example1" name="example1">
<area shape="poly" id="node1" href="Hello" title="I say Hello" alt="" coords="164,29,161,22,151,15,137,10,118,7,97,5,77,7,58,10,43,15,34,22,31,29,34,37,43,43,58,49,77,52,97,53,118,52,137,49,151,43,161,37"/>
<area shape="poly" id="node2" href="World" title="You say World!" alt="" coords="190,125,186,118,172,111,152,106,126,103,97,101,69,103,43,106,22,111,9,118,5,125,9,133,22,139,43,145,69,148,97,149,126,148,152,145,172,139,186,133"/>
</map>

From this image map file, I would like to be able to extract href and title to create wikilinks as follows:

[[Hello|I say Hello]]
[[World|You say World!]]

, , .map XML, XPATH , . PHP , , XML/XPATH, , .

/ wikilinks .map, , GraphViz.php, , pagelinks.

. , . , , , XPATH, , . , , , .

$xml = new SimpleXMLElement( $map );
foreach($xml->area as $item) {
  $links .= "[[" . $item->attributes()->href . "|" . $item->attributes()->title . "]]";
}

. . . , . , .

+5
1

, , , , wiki. GraphViz, , , , . / :

  • MscGen: Graphviz.
  • imageAtrributes: , , imageAttributes , .
  • wikilinks: wiki Graphviz, , Graphviz - . "wikilinks", . : Wikilinks , Graphviz node . , wikilink : ", wikilinks, ". wikilinks , .

: Wiki Graphviz enhancements

,

:

// We don't want to support wikilinks so don't replace them
//$timelinesrc = rewriteWikiUrls( $timelinesrc ); // if we use wiki-links we transform them to real urls

:

// clean up map-name
$map  = preg_replace( '#<ma(.*)>#', ' ', $map );
$map  = str_replace( '</map>', '', $map );
if ( $renderer == 'mscgen' ) {
    $mapbefore = $map;
    $map = preg_replace( '/(\w+)\s([_:%#/\w]+)\s(\d+,\d+)\s(\d+,\d+)/',
   '<area shape="$1" href="$2" title="$2" alt="$2" coords="$3,$4" />',
    $map );
}

/* Procduce html
 */
if ( $wgGraphVizSettings->imageFormatting )
{
    $txt = imageAtrributes( $args, $storagename, $map, $outputType, $wgUploadPath ); // if we want borders/position/...
} else {
    $txt  = '<map name="' . $storagename . '">' . $map . '</map>' .
         '<img src="' . $wgUploadPath . '/graphviz/' . $storagename . '.' . $outputType . '"' .
                   ' usemap="#' . $storagename . '" />';
}

:

$intHtml = '';
$extHtml = '';
$badHtml = '';

// Wrap the map/area info with top level nodes and load into xml object
$xmlObj = new SimpleXMLElement( $map );

// What does map look like before we start working with it?
wfDebugLog( 'graphviz', 'map before: ' . $map . "\n" );

// loop through each of the <area> nodes
foreach($xmlObj->area as $areaNode) {

    wfDebugLog( 'graphviz', "areaNode: " . $areaNode->asXML() . "\n" );

    // Get the data from the XML attributes
    $hrefValue = (string)$areaNode->attributes()->href;
    $textValue = (string)$areaNode->attributes()->title;

    wfDebugLog( 'graphviz', '$hrefValue before: ' . $hrefValue . "\n" );
    wfDebugLog( 'graphviz', '$textValue before: ' . $textValue . "\n" );

    // For the text fields, multiple spaces ("   ") in the Graphviz source (label)
    // turns into a regular space followed by encoded representations of
    // non-breaking spaces (" &#xA0;&#xA0;") in the .map file which then turns
    // into the following in the local variables: ("   ").
    // The following two options appear to convert/decode the characters
    // appropriately. Leaving the lines commented out for now, as we have
    // not seen a graph in the wild with multiple spaces in the label -
    // just happened to stumble on the scenario.
    // See http://www.php.net/manual/en/simplexmlelement.asxml.php
    // and http://stackoverflow.com/questions/2050723/how-can-i-preg-replace-special-character-like-pret-a-porter
    //$textValue = iconv("UTF-8", "ASCII//TRANSLIT", $textValue);
    //$textValue = html_entity_decode($textValue, ENT_NOQUOTES, 'UTF-8');

    // Now we need to deal with the whitespace characters like tabs and newlines
    // and also deal with them correctly to replace multiple occurences.
    // Unfortunately, the \n and \t values in the variable aren't actually
    // tab or newline characters but literal characters '\' + 't' or '\' + 'n'.
    // So the normally recommended regex '/\s+/u' to replace the whitespace 
    // characters does not work.
    // See http://stackoverflow.com/questions/6579636/preg-replace-n-in-string
    $hrefValue = preg_replace("/( |\\\\n|\\\\t)+/", ' ', $hrefValue);
    $textValue = preg_replace("/( |\\\\n|\\\\t)+/", ' ', $textValue);

    // check to see if the url matches any of the
    // allowed protocols for external links
    if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $hrefValue ) ) {
        // external link
        $parser->mOutput->addExternalLink( $hrefValue );
        $extHtml .= Linker::makeExternalLink( $hrefValue, $textValue ) . ', ';
    }
    else {
        $first = substr( $hrefValue, 0, 1 );
        if ( $first == '\\' || $first == '[' || $first == '/' ) {
            // potential UNC path, wikilink, absolute or relative path
            $hrefValue = '#InvalidLink';
            $badHtml .= Linker::makeExternalLink( $hrefValue, $textValue ) . ', ';
            $textValue = 'Invalid link. Check Graphviz source.';
        }
        else {
            $title = Title::newFromText( $hrefValue );
            if ( is_null( $title ) ) {
                // invalid link
                $hrefValue = '#InvalidLink';
                $badHtml .= Linker::makeExternalLink( $hrefValue, $textValue ) . ', ';
                $textValue = 'Invalid link. Check Graphviz source.';
            }
            else {
                // internal link
                $parser->mOutput->addLink( $title );
                $intHtml .= Linker::link( $title, $textValue ) . ', ';
                $hrefValue = $title->getFullURL();
            }
        }
    }

    $areaNode->attributes()->href = $hrefValue;
    $areaNode->attributes()->title = $textValue;

}

$map = $xmlObj->asXML();

// The contents of $map, which is now XML, gets embedded
// in the HTML sent to the browser so we need to strip
// the XML version tag and we also strip the <map> because
// it will get replaced with a new one with the correct name.
$map = str_replace( '<?xml version="1.0"?>', '', $map );
$map = preg_replace( '#<ma(.*)>#', ' ', $map );
$map = str_replace( '</map>', '', $map );

// Let see what it looks like now that we are done with it.
wfDebugLog( 'graphviz', 'map after: ' . $map . "\n" );

$txt = '' .
    '<table style="background-color:#f9f9f9;border:1px solid #ddd;">' .
        '<tr>' .
            '<td style="border:1px solid #ddd;text-align:center;">' .
                '<map name="' . $storagename . '">' . $map . '</map>' .
                '<img src="' . $wgUploadPath . '/graphviz/' . $storagename . '.' . $outputType . '"' . ' usemap="#' . $storagename . '" />' .
            '</td>' .
        '</tr>' .
        '<tr>' .
            '<td style="font:10px verdana;">' .
                'This Graphviz diagram links to the following pages:' .
                '<br /><strong>Internal</strong>: ' . ( $intHtml != '' ?  rtrim( $intHtml, ' ,' ) : '<em>none</em>' ) .
                '<br /><strong>External</strong>: ' . ( $extHtml != '' ?  rtrim( $extHtml, ' ,' ) : '<em>none</em>' ) .
                ( $badHtml != '' ? '<br /><strong>Invalid</strong>: ' . rtrim($badHtml, ' ,') .
                '<br /><em>Tip: Do not use wikilinks ([]), UNC paths (\\) or relative links (/) when creating links in Graphviz diagrams.</em>' : '' ) .
            '</td>' .
        '</tr>' .
    '</table>';

:

  • , .
+4

All Articles