Extension:ExternalUML

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
ExternalUML

Release status: beta

Implementation Tag
Description Renders a UML model from text using yUML and websequencediagrams
Author(s) Various
Last version 0.2.0 (2011-06-26)
License "No Blame"
Download see below

Check usage (experimental)

Contents


ExternalUML is an extension written for MediaWiki that renders a UML model from text using the online services yUML and websequencediagrams. These tools use scripting languages to automatically place the visual elements of diagrams. This supports class diagrams, activity diagrams, use case diagrams, and sequence diagrams.


[edit] License

The "No Blame" License.

Feel free to use this code as you wish, so long as you assume full responsibility for the consequences of said use. That is to say, don't blame the author(s) if something goes wrong. This code can be shared without any restrictions, as long as you accept the fact that it is your fault if something goes wrong. Although this program is distributed in the hope that it will be useful, it is provided WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


[edit] Usage

Use the corresponding scripting language inside of the <uml></uml> tag.


[edit] Example Class

 <uml class>
 [Customer]+1->*[Order]
 [Order]++1-items >*[LineItem]
 [Order]-0..1>[PaymentMethod{bg:orange}]
 </uml>


[edit] Example Sequence

 <uml sequence>
 Janice->Greg: Status Request
 alt normal case
     Greg->Janice: Everything's fine
 else exceptional case
     Greg->Janice: Things are out of sync
     note right of Greg: Greg clears the cache 
 end
 </uml>


[edit] Example Activity

 <uml activity>
 (start)-><d1>logged in->(Show Dashboard)->|a|->(end)
 <d1>not logged in->(Show Login)->|a|
 </uml>


[edit] Example Use Case

 <uml usecase>
 [Customer]-(Login)
 [Customer]-(note: Cust can be registered or not{bg:beige})
 </uml>


[edit] Installation

  1. Copy & paste the code below in a file called ExternalUML.php and place it in your extensions directory of your MediaWiki folder.
  2. Put this line near the end of your LocalSettings.php in the MediaWiki root-folder to include the extension:
require_once('extensions/ExternalUML.php');


[edit] Code

[edit] Version 0.2.0

<?php
/**
 * Parser hook extension adds a <uml> tag to wiki markup for rendering UML
 * diagrams within a wiki page using yUML and websequencediagrams.
 *
 * Version 0.1 for sequence diagrams by James Kilts.
 * Version 0.2 changes to support other diagram types using yUML
 * based on code by Sindri Traustason
 * <http://wiki.sindri.info/wiki/YUML_MediaWiki_Extension>
 * Which had also been modified by dave@davesmith.me <http://davesmith.me/>
 * to support activity diagrams and the direction attribute.
 *
 * Parameters:
 *   class|activity|usecase|sequence
 *   style                             scruffy|nofunky|plain (except for sequence)
 *                                     default|modern-blue|vs2010 (sequence only)
 *   scale                             y|n  (except for sequence)
 *   direction                         lr|td  (except for sequence)
 *
 * Syntax:
 *  <uml class style="scruffy|nofunky|plain" scale="n" direction="lr|td">
 *   [Person]->[Address]
 *  </uml>
 *  <uml sequence style="default|vs2010">
 *   Alice->Bob: Authentication Request
 *   Bob->Alice: Authentication Failure
 *  </uml>
 *  <uml usecase style="scruffy|nofunky|plain" scale="n" direction="lr|td">
 *   [User]-(Login)
 *   [User]-(Logout)
 *  </uml>
 *  <uml activity style="scruffy|nofunky|plain" scale="n" direction="lr|td">
 *   (start)-><d1>logged in->(Show Dashboard)->|a|->(end)<d1>not logged in->(Show Login)->|a|
 *  </uml>
 */
 
if ( !defined( 'MEDIAWIKI' ) ) {
    echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
    die( -1 );
}
 
/* Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980
 */
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
    $wgHooks['ParserFirstCallInit'][] = 'wfMetaUMLExtension';
}
else {
    $wgExtensionFunctions[] = 'wfMetaUMLExtension';
}
 
function wfMetaUMLExtension() {
    global $wgParser;
    $wgParser->setHook( 'uml', 'callbackGenerateUmlHtml' );
    return true;
}
 
function callbackGenerateUmlHtml( $input, $args ) {
    if (array_key_exists('class', $args))
      $url = getYumlImageUrl($input, $args, "class");
    else if (array_key_exists('activity', $args))
      $url = getYumlImageUrl($input, $args, "activity");
    else if (array_key_exists('usecase', $args))
      $url = getYumlImageUrl($input, $args, "usecase");
    else
      $url = getSequenceDiagramURL($input, $args);
 
    if ($url == false) {
        return "[An error occured generating the UML Diagram in the ExternalUML extension]";
    }
    else {
        return "<img src='$url'>";
    }
}
 
function getYumlImageUrl( $input, $args, $diagramType ) {
    $style = "";
    if (array_key_exists('style', $args)) {
        $style = "/" . $args["style"];
    }
 
    $scale = "";
    if (array_key_exists('scale', $args)) {
        $scale=";scale:".$args["scale"];
    }
 
    $direction = '';
    if (array_key_exists('direction', $args)) {
        $direction=";dir:".$args['direction'];
    }
 
    $uml_code = preg_replace(
                    array("/\n/", "/,,/"),
                    array(", ",   ","   ),
                    trim($input));
 
    return "http://yUML.me/diagram"
        .$style.$scale.$direction.'/'
        .$diagramType.'/'
        .$uml_code;
}
 
function getSequenceDiagramURL( $input, $args ) {
    /* HTTP Post to websequencediagrams.com to generate and retrieve the corresponding image
     */
    $style = "vs2010";
    if (array_key_exists('style', $args)) {
        $style = $args["style"];
    }
 
    $postdata = http_build_query(
        array(
            'message' => $input,
            'style' => $style,
            'apiVersion' => '1'
        )
    );
 
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
 
    $context  = stream_context_create($opts);
    $result = file_get_contents('http://www.websequencediagrams.com', false, $context);
 
    /* Extract the "img" attribute of the JSON response.
     * Note:  If json isn't available, use hardcoded:
     *          strstr(substr($result, 9), '"', true);
     */
    $json_o = json_decode($result);
    return "http://www.websequencediagrams.com".$json_o->img;
}
 
?>

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox